From fa44fae99185777b0f031a6d9c91c9aefe80723e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Jan 2023 07:52:01 -0800 Subject: [PATCH 1/9] perf!: allow tree-shaking unstorage drivers (#1516) * perf: allow tree-shaking unstorage drivers * fix: allow overriding fsBase at runtime * fix: remove `fsBase` export --- modules/tauri/index.ts | 1 + modules/tauri/runtime/storage-config.ts | 2 ++ nuxt.config.ts | 17 ++++++++++++++--- server/shared.ts | 12 +++++++----- 4 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 modules/tauri/runtime/storage-config.ts diff --git a/modules/tauri/index.ts b/modules/tauri/index.ts index 5c0fec95..9d445922 100644 --- a/modules/tauri/index.ts +++ b/modules/tauri/index.ts @@ -22,6 +22,7 @@ export default defineNuxtModule({ ...nuxt.options.alias, 'unstorage/drivers/fs': 'unenv/runtime/mock/proxy', 'unstorage/drivers/cloudflare-kv-http': 'unenv/runtime/mock/proxy', + '#storage-config': resolve('./runtime/storage-config'), 'node:events': 'unenv/runtime/node/events/index', '#build-info': resolve('./runtime/build-info'), } diff --git a/modules/tauri/runtime/storage-config.ts b/modules/tauri/runtime/storage-config.ts new file mode 100644 index 00000000..2f1ae291 --- /dev/null +++ b/modules/tauri/runtime/storage-config.ts @@ -0,0 +1,2 @@ +export const driver = undefined +export const fsBase = '' diff --git a/nuxt.config.ts b/nuxt.config.ts index e0bd7c8d..266601be 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -1,4 +1,4 @@ -import { createResolver } from '@nuxt/kit' +import { createResolver, useNuxt } from '@nuxt/kit' import Inspect from 'vite-plugin-inspect' import { isCI, isDevelopment, isWindows } from 'std-env' import { isPreview } from './config/env' @@ -86,6 +86,11 @@ export default defineNuxtConfig({ 'postcss-nested': {}, }, }, + appConfig: { + storage: { + driver: process.env.NUXT_STORAGE_DRIVER ?? (isCI ? 'cloudflare' : 'fs'), + }, + }, runtimeConfig: { adminKey: '', cloudflare: { @@ -102,8 +107,7 @@ export default defineNuxtConfig({ defaultServer: 'm.webtoo.ls', }, storage: { - driver: isCI ? 'cloudflare' : 'fs', - fsBase: 'node_modules/.cache/servers', + fsBase: 'node_modules/.cache/app', }, }, routeRules: { @@ -126,6 +130,13 @@ export default defineNuxtConfig({ ignore: ['/settings'], }, }, + hooks: { + 'nitro:config': function (config) { + const nuxt = useNuxt() + config.virtual = config.virtual || {} + config.virtual['#storage-config'] = `export const driver = ${JSON.stringify(nuxt.options.appConfig.storage.driver)}` + }, + }, app: { keepalive: true, head: { diff --git a/server/shared.ts b/server/shared.ts index 37f4b2bd..5a0d97e5 100644 --- a/server/shared.ts +++ b/server/shared.ts @@ -14,29 +14,31 @@ import cached from './cache-driver' // @ts-expect-error virtual import import { env } from '#build-info' +// @ts-expect-error virtual import +import { driver } from '#storage-config' import type { AppInfo } from '~/types' import { APP_NAME } from '~/constants' -const config = useRuntimeConfig() - const fs = _fs as typeof import('unstorage/dist/drivers/fs')['default'] const kv = _kv as typeof import('unstorage/dist/drivers/cloudflare-kv-http')['default'] const memory = _memory as typeof import('unstorage/dist/drivers/memory')['default'] const storage = useStorage() as Storage -if (config.storage.driver === 'fs') { +if (driver === 'fs') { + const config = useRuntimeConfig() storage.mount('servers', fs({ base: config.storage.fsBase })) } -else if (config.storage.driver === 'cloudflare') { +else if (driver === 'cloudflare') { + const config = useRuntimeConfig() storage.mount('servers', cached(kv({ accountId: config.cloudflare.accountId, namespaceId: config.cloudflare.namespaceId, apiToken: config.cloudflare.apiToken, }))) } -else if (config.storage.driver === 'memory') { +else if (driver === 'memory') { storage.mount('servers', memory()) } From 72bf6fb6f0e81f0509b7be39e15255aeccf367b2 Mon Sep 17 00:00:00 2001 From: Stanislas Date: Sun, 29 Jan 2023 16:58:32 +0100 Subject: [PATCH 2/9] fix: handle account switching when accounts have the same id (#1510) --- components/user/UserPicker.vue | 4 ++-- composables/users.ts | 35 ++++++++++++++-------------------- constants/index.ts | 1 - 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/components/user/UserPicker.vue b/components/user/UserPicker.vue index 23ea40ae..f3c3d486 100644 --- a/components/user/UserPicker.vue +++ b/components/user/UserPicker.vue @@ -5,7 +5,7 @@ const all = useUsers() const router = useRouter() const clickUser = (user: UserLogin) => { - if (user.account.id === currentUser.value?.account.id) + if (user.account.acct === currentUser.value?.account.acct) router.push(getAccountRoute(user.account)) else switchUser(user) @@ -21,7 +21,7 @@ const clickUser = (user: UserLogin) => { flex rounded cursor-pointer aria-label="Switch user" - :class="user.account.id === currentUser?.account.id ? '' : 'op25 grayscale'" + :class="user.account.acct === currentUser?.account.acct ? '' : 'op25 grayscale'" hover="filter-none op100" @click="clickUser(user)" > diff --git a/composables/users.ts b/composables/users.ts index f5925e7a..e0ff87b1 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -7,7 +7,6 @@ import type { UserLogin } from '~/types' import type { Overwrite } from '~/types/utils' import { DEFAULT_POST_CHARS_LIMIT, - STORAGE_KEY_CURRENT_USER, STORAGE_KEY_CURRENT_USER_HANDLE, STORAGE_KEY_NODES, STORAGE_KEY_NOTIFICATION, @@ -46,7 +45,7 @@ const initializeUsers = async (): Promise | RemovableRef>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true }) export const nodes = useLocalStorage>(STORAGE_KEY_NODES, {}, { deep: true }) -const currentUserId = useLocalStorage(STORAGE_KEY_CURRENT_USER, mock ? mock.user.account.id : '') +const currentUserHandle = useLocalStorage(STORAGE_KEY_CURRENT_USER_HANDLE, mock ? mock.user.account.id : '') export type ElkInstance = Partial & { uri: string @@ -56,8 +55,8 @@ export type ElkInstance = Partial & { export const getInstanceCache = (server: string): mastodon.v1.Instance | undefined => instances.value[server] export const currentUser = computed(() => { - if (currentUserId.value) { - const user = users.value.find(user => user.account?.id === currentUserId.value) + if (currentUserHandle.value) { + const user = users.value.find(user => user.account?.acct === currentUserHandle.value) if (user) return user } @@ -84,12 +83,12 @@ if (process.client) { const windowReload = () => { document.visibilityState === 'visible' && window.location.reload() } - watch(currentUserId, async (id, oldId) => { + watch(currentUserHandle, async (handle, oldHandle) => { // when sign in or switch account - if (id) { - if (id === currentUser.value?.account?.id) { + if (handle) { + if (handle === currentUser.value?.account?.acct) { // when sign in, the other tab will not have the user, idb is not reactive - const newUser = users.value.find(user => user.account?.id === id) + const newUser = users.value.find(user => user.account?.acct === handle) // if the user is there, then we are switching account if (newUser) { // check if the change is on current tab: if so, don't reload @@ -101,19 +100,13 @@ if (process.client) { window.addEventListener('visibilitychange', windowReload, { capture: true }) } // when sign out - else if (oldId) { - const oldUser = users.value.find(user => user.account?.id === oldId) + else if (oldHandle) { + const oldUser = users.value.find(user => user.account?.acct === oldHandle) // when sign out, the other tab will not have the user, idb is not reactive if (oldUser) window.addEventListener('visibilitychange', windowReload, { capture: true }) } }, { immediate: true, flush: 'post' }) - - // for injected script to read - const currentUserHandle = computed(() => currentUser.value?.account.acct || '') - watchEffect(() => { - localStorage.setItem(STORAGE_KEY_CURRENT_USER_HANDLE, currentUserHandle.value) - }) } export const useUsers = () => users @@ -144,7 +137,7 @@ export async function loginTo(masto: ElkMasto, user: Overwrite Date: Sun, 29 Jan 2023 16:20:47 -0800 Subject: [PATCH 3/9] chore(deps): update dependency vitest to ^0.28.3 (#1523) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- package.json | 2 +- pnpm-lock.yaml | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 780d5390..1e17a63e 100644 --- a/package.json +++ b/package.json @@ -118,7 +118,7 @@ "unplugin-vue-inspector": "^0.0.2", "vite-plugin-inspect": "^0.7.14", "vite-plugin-pwa": "^0.14.1", - "vitest": "^0.28.1", + "vitest": "^0.28.3", "vue-tsc": "^1.0.24", "workbox-build": "^6.5.4", "workbox-window": "^6.5.4" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 40e972d8..dac482df 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -119,7 +119,7 @@ importers: unplugin-vue-inspector: ^0.0.2 vite-plugin-inspect: ^0.7.14 vite-plugin-pwa: ^0.14.1 - vitest: ^0.28.1 + vitest: ^0.28.3 vue-advanced-cropper: ^2.8.8 vue-tsc: ^1.0.24 vue-virtual-scroller: 2.0.0-beta.7 @@ -216,7 +216,7 @@ importers: unplugin-auto-import: 0.13.0_@vueuse+core@9.11.1 unplugin-vue-inspector: 0.0.2 vite-plugin-inspect: 0.7.14 - vite-plugin-pwa: 0.14.1_tz3vz2xt4jvid2diblkpydcyn4 + vite-plugin-pwa: 0.14.1 vitest: 0.28.3_jsdom@21.1.0 vue-tsc: 1.0.24_typescript@4.9.4 workbox-build: 6.5.4 @@ -12949,12 +12949,10 @@ packages: - supports-color dev: true - /vite-plugin-pwa/0.14.1_tz3vz2xt4jvid2diblkpydcyn4: + /vite-plugin-pwa/0.14.1: resolution: {integrity: sha512-5zx7yhQ8RTLwV71+GA9YsQQ63ALKG8XXIMqRJDdZkR8ZYftFcRgnzM7wOWmQZ/DATspyhPih5wCdcZnAIsM+mA==} peerDependencies: vite: ^3.1.0 || ^4.0.0 - workbox-build: ^6.5.4 - workbox-window: ^6.5.4 dependencies: '@rollup/plugin-replace': 5.0.2_rollup@3.10.1 debug: 4.3.4 @@ -12964,6 +12962,7 @@ packages: workbox-build: 6.5.4 workbox-window: 6.5.4 transitivePeerDependencies: + - '@types/babel__core' - supports-color dev: true From fbdaf8bbef166c894350c67ef112e28fd020c17c Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 30 Jan 2023 10:36:39 +0100 Subject: [PATCH 4/9] chore: cleanup --- tests/__snapshots__/content-rich.test.ts.snap | 164 ------------------ tests/__snapshots__/html-parse.test.ts.snap | 144 --------------- tests/nuxt/content-rich.test.ts | 13 +- 3 files changed, 4 insertions(+), 317 deletions(-) delete mode 100644 tests/__snapshots__/content-rich.test.ts.snap delete mode 100644 tests/__snapshots__/html-parse.test.ts.snap diff --git a/tests/__snapshots__/content-rich.test.ts.snap b/tests/__snapshots__/content-rich.test.ts.snap deleted file mode 100644 index 289bfb73..00000000 --- a/tests/__snapshots__/content-rich.test.ts.snap +++ /dev/null @@ -1,164 +0,0 @@ -// Vitest Snapshot v1 - -exports[`content-rich > block with backticks 1`] = `"

[(\`number string) (\`tag string)]

"`; - -exports[`content-rich > block with injected html, with a known language 1`] = ` -"
-        
-          <a href="javascript:alert(1)">click me</a>
-        
-      
-" -`; - -exports[`content-rich > block with injected html, with an unknown language 1`] = ` -"
-        
-          <a href="javascript:alert(1)">click me</a>
-        
-      
-" -`; - -exports[`content-rich > block with injected html, without language 1`] = ` -"
-        
-          <a href="javascript:alert(1)">click me</a>
-        
-      
-" -`; - -exports[`content-rich > code frame 1`] = ` -"

Testing code block

import { useMouse, usePreferredDark } from '@vueuse/core'
-// tracks mouse position
-const { x, y } = useMouse()
-// is the user prefers dark theme
-const isDark = usePreferredDark()

" -`; - -exports[`content-rich > code frame 2 1`] = ` -"

- - Testing
-

const a = hello
-

-" -`; - -exports[`content-rich > code frame empty 1`] = `"


"`; - -exports[`content-rich > code frame no lang 1`] = `"

hello world

no lang

"`; - -exports[`content-rich > custom emoji 1`] = ` -"Daniel Roe - - \\":nuxt:\\" -" -`; - -exports[`content-rich > empty 1`] = `""`; - -exports[`content-rich > group mention > html 1`] = ` -"

- -

-" -`; - -exports[`content-rich > handles formatting from servers 1`] = ` -"

Fedi HTML Support Survey

-

Does the following formatting come through accurately for you?

-

-
    -
  • This is an indented bulleted list (not just asterisks).
  • -
  • This line is bold.
  • -
  • This line is italic.
  • -
-
    -
  1. This list...
  2. -
  3. ...is numbered and indented
  4. -
-

This line is larger.

-" -`; - -exports[`content-rich > handles html within code blocks 1`] = ` -"

- HTML block code:
-

-<span class="icon--noto icon--noto--1st-place-medal"></span>
-<span class="icon--noto icon--noto--2nd-place-medal-medal"></span>
-

-" -`; - -exports[`content-rich > inline code with link 1`] = ` -"

- Inline code with link: - https://api.iconify.design/noto.css?icons=1st-place-medal,2nd-place-medal -

-" -`; - -exports[`content-rich > link + mention 1`] = ` -"

- Happy - \\"πŸ€—\\" - we’re now using - - (migrated from chai+mocha) - https://github.com/ayoayco/astro-reactive-library/pull/203 -

-" -`; - -exports[`content-rich > plain text 1`] = ` -"hello there -" -`; - -exports[`editor > transform mentions 1`] = ` -" -@elk Hello" -`; diff --git a/tests/__snapshots__/html-parse.test.ts.snap b/tests/__snapshots__/html-parse.test.ts.snap deleted file mode 100644 index d7f36a0d..00000000 --- a/tests/__snapshots__/html-parse.test.ts.snap +++ /dev/null @@ -1,144 +0,0 @@ -// Vitest Snapshot v1 - -exports[`html-parse > code frame > html 1`] = ` -"

Testing code block

import { useMouse, usePreferredDark } from '@vueuse/core'
-// tracks mouse position
-const { x, y } = useMouse()
-// is the user prefers dark theme
-const isDark = usePreferredDark()

" -`; - -exports[`html-parse > code frame > text 1`] = ` -"Testing code block - - -\`\`\`ts -import { useMouse, usePreferredDark } from '@vueuse/core' -// tracks mouse position -const { x, y } = useMouse() -// is the user prefers dark theme -const isDark = usePreferredDark() -\`\`\`" -`; - -exports[`html-parse > code frame 2 > html 1`] = ` -"

- @antfu - Testing
-

const a = hello
-

-" -`; - -exports[`html-parse > code frame 2 > text 1`] = ` -"@antfu Testing - -\`\`\`ts -const a = hello -\`\`\`" -`; - -exports[`html-parse > custom emoji > html 1`] = ` -"Daniel Roe - - \\":nuxt:\\" -" -`; - -exports[`html-parse > custom emoji > text 1`] = `"Daniel Roe :nuxt:"`; - -exports[`html-parse > emojis > html 1`] = ` -"\\"πŸ‡«πŸ‡·\\" -\\"πŸ‘¨β€πŸ‘©β€πŸ‘¦\\" -\\"πŸ‘©β€πŸš’\\"\\"πŸ§‘πŸ½β€πŸš€\\" -" -`; - -exports[`html-parse > emojis > text 1`] = `"πŸ‡«πŸ‡· πŸ‘¨β€πŸ‘©β€πŸ‘¦ πŸ‘©β€πŸš’πŸ§‘πŸ½β€πŸš€"`; - -exports[`html-parse > empty > html 1`] = `""`; - -exports[`html-parse > empty > text 1`] = `""`; - -exports[`html-parse > html entities > html 1`] = ` -"

Hello <World />.

-" -`; - -exports[`html-parse > html entities > text 1`] = `"Hello ."`; - -exports[`html-parse > inline markdown > html 1`] = `"

text code bold italic del

code block

"`; - -exports[`html-parse > inline markdown > text 1`] = ` -"text \`code\` **bold** *italic* ~~del~~ - - -\`\`\`js -code block -\`\`\`" -`; - -exports[`html-parse > link + mention > html 1`] = ` -"

- Happy - \\"πŸ€—\\" - we’re now using - @vitest - (migrated from chai+mocha) - https://github.com/ayoayco/astro-reactive-library/pull/203 -

-" -`; - -exports[`html-parse > link + mention > text 1`] = `"Happy πŸ€— we’re now using @vitest (migrated from chai+mocha) https://github.com/ayoayco/astro-reactive-library/pull/203"`; diff --git a/tests/nuxt/content-rich.test.ts b/tests/nuxt/content-rich.test.ts index 9d32b592..d21bc30e 100644 --- a/tests/nuxt/content-rich.test.ts +++ b/tests/nuxt/content-rich.test.ts @@ -287,15 +287,10 @@ vi.mock('shiki-es', async (importOriginal) => { } }) -vi.mock('~/components/content/ContentMentionGroup.vue', async () => { - const { defineComponent, h } = await import('vue') - return { - default: defineComponent({ - setup(props, { slots }) { - return () => h('mention-group', null, { default: () => slots?.default?.() }) - }, - }), - } +mockComponent('ContentMentionGroup', { + setup(props, { slots }) { + return () => h('mention-group', null, { default: () => slots?.default?.() }) + }, }) mockComponent('AccountHoverWrapper', { From 8d792d003ddad681e7b2f680d30a0128561ebd46 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 30 Jan 2023 11:58:18 +0100 Subject: [PATCH 5/9] refactor: make auto import more explicit --- components/help/HelpPreview.vue | 2 +- components/publish/PublishAttachment.vue | 2 +- components/publish/PublishWidget.vue | 2 +- components/user/UserSwitcher.vue | 2 +- composables/about.ts | 2 +- composables/command.ts | 2 +- composables/content-render.ts | 2 +- composables/masto/account.ts | 4 ++-- composables/masto/masto.ts | 2 +- composables/screen.ts | 2 -- composables/users.ts | 12 ++++++------ pages/settings/about/index.vue | 2 +- scripts/avatars.ts | 4 ++-- 13 files changed, 19 insertions(+), 21 deletions(-) diff --git a/components/help/HelpPreview.vue b/components/help/HelpPreview.vue index 90cc4e58..8e37f4f6 100644 --- a/components/help/HelpPreview.vue +++ b/components/help/HelpPreview.vue @@ -30,7 +30,7 @@ const emit = defineEmits<{

{{ $t('help.desc_para3') }}

-