From 1978f1caff7e016a74c82a4af79ad0f3c5d3b334 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Sat, 10 Feb 2024 00:15:30 +0200 Subject: [PATCH 1/3] fix: Conditionally import `async_hooks` to avoid leaking into browser chunks with conditional imports --- packages/astro-clerk-auth/package.esm.json | 8 +++++ .../src/async-local-storage.client.ts | 34 +++++++++++++++++++ .../src/async-local-storage.server.ts | 8 +++++ packages/astro-clerk-auth/tsup.config.ts | 2 ++ 4 files changed, 52 insertions(+) create mode 100644 packages/astro-clerk-auth/package.esm.json create mode 100644 packages/astro-clerk-auth/src/async-local-storage.client.ts create mode 100644 packages/astro-clerk-auth/src/async-local-storage.server.ts diff --git a/packages/astro-clerk-auth/package.esm.json b/packages/astro-clerk-auth/package.esm.json new file mode 100644 index 0000000..c0beb2a --- /dev/null +++ b/packages/astro-clerk-auth/package.esm.json @@ -0,0 +1,8 @@ +{ + "imports": { + "#async-local-storage": { + "node": "./async-local-storage.server.js", + "default": "./async-local-storage.client.js" + } + } +} diff --git a/packages/astro-clerk-auth/src/async-local-storage.client.ts b/packages/astro-clerk-auth/src/async-local-storage.client.ts new file mode 100644 index 0000000..d29ad58 --- /dev/null +++ b/packages/astro-clerk-auth/src/async-local-storage.client.ts @@ -0,0 +1,34 @@ +import type { AsyncLocalStorage } from 'async_hooks'; + +const sharedAsyncLocalStorageNotAvailableError = new Error( + 'Invariant: AsyncLocalStorage accessed in runtime where it is not available', +); + +class FakeAsyncLocalStorage implements AsyncLocalStorage { + disable(): void { + throw sharedAsyncLocalStorageNotAvailableError; + } + + getStore(): Store | undefined { + // This fake implementation of AsyncLocalStorage always returns `undefined`. + return undefined; + } + + run(): R { + throw sharedAsyncLocalStorageNotAvailableError; + } + + exit(): R { + throw sharedAsyncLocalStorageNotAvailableError; + } + + enterWith(): void { + throw sharedAsyncLocalStorageNotAvailableError; + } +} + +export function createAsyncLocalStorage(): AsyncLocalStorage { + return new FakeAsyncLocalStorage(); +} + +export const authAsyncStorage = createAsyncLocalStorage(); diff --git a/packages/astro-clerk-auth/src/async-local-storage.server.ts b/packages/astro-clerk-auth/src/async-local-storage.server.ts new file mode 100644 index 0000000..b6e1081 --- /dev/null +++ b/packages/astro-clerk-auth/src/async-local-storage.server.ts @@ -0,0 +1,8 @@ +import { AsyncLocalStorage } from 'node:async_hooks'; + +function createAsyncLocalStorage(): AsyncLocalStorage { + return new AsyncLocalStorage(); +} + + +export const authAsyncStorage = createAsyncLocalStorage(); \ No newline at end of file diff --git a/packages/astro-clerk-auth/tsup.config.ts b/packages/astro-clerk-auth/tsup.config.ts index 2bd3d6e..fa37815 100644 --- a/packages/astro-clerk-auth/tsup.config.ts +++ b/packages/astro-clerk-auth/tsup.config.ts @@ -17,6 +17,8 @@ export default defineConfig(() => { './src/v0/index.ts', './src/integration/index.ts', './src/integration/hotload.ts', + './src/async-local-storage.client.ts', + './src/async-local-storage.server.ts', ], dts: true, minify: false, From 727c40debaab0a58a128405570f8cc6641d0c81f Mon Sep 17 00:00:00 2001 From: panteliselef Date: Fri, 31 May 2024 21:41:46 +0300 Subject: [PATCH 2/3] WIP --- packages/astro-clerk-auth/package.esm.json | 8 -------- packages/astro-clerk-auth/package.json | 6 ++++++ .../src/async-local-storage.client.ts | 2 +- .../src/async-local-storage.server.ts | 14 +++++++++----- .../astro-clerk-auth/src/client/react/hooks.ts | 3 ++- .../src/server/async-local-storage.ts | 12 ------------ .../src/server/clerk-middleware.ts | 3 ++- packages/astro-clerk-auth/src/server/index.ts | 4 ++-- packages/astro-clerk-auth/tsup.config.ts | 2 +- 9 files changed, 23 insertions(+), 31 deletions(-) delete mode 100644 packages/astro-clerk-auth/package.esm.json delete mode 100644 packages/astro-clerk-auth/src/server/async-local-storage.ts diff --git a/packages/astro-clerk-auth/package.esm.json b/packages/astro-clerk-auth/package.esm.json deleted file mode 100644 index c0beb2a..0000000 --- a/packages/astro-clerk-auth/package.esm.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "imports": { - "#async-local-storage": { - "node": "./async-local-storage.server.js", - "default": "./async-local-storage.client.js" - } - } -} diff --git a/packages/astro-clerk-auth/package.json b/packages/astro-clerk-auth/package.json index bc1479d..5dc9f1d 100644 --- a/packages/astro-clerk-auth/package.json +++ b/packages/astro-clerk-auth/package.json @@ -102,6 +102,12 @@ "./components/*": "./components/*", "./package.json": "./package.json" }, + "imports": { + "#async-local-storage": { + "node": "./async-local-storage.server.js", + "default": "./async-local-storage.client.js" + } + }, "dependencies": { "@clerk/backend": "1.1.1", "@clerk/clerk-js": "5.2.1", diff --git a/packages/astro-clerk-auth/src/async-local-storage.client.ts b/packages/astro-clerk-auth/src/async-local-storage.client.ts index d29ad58..6819823 100644 --- a/packages/astro-clerk-auth/src/async-local-storage.client.ts +++ b/packages/astro-clerk-auth/src/async-local-storage.client.ts @@ -27,7 +27,7 @@ class FakeAsyncLocalStorage implements AsyncLocalStorage(): AsyncLocalStorage { +function createAsyncLocalStorage(): AsyncLocalStorage { return new FakeAsyncLocalStorage(); } diff --git a/packages/astro-clerk-auth/src/async-local-storage.server.ts b/packages/astro-clerk-auth/src/async-local-storage.server.ts index b6e1081..0cc94de 100644 --- a/packages/astro-clerk-auth/src/async-local-storage.server.ts +++ b/packages/astro-clerk-auth/src/async-local-storage.server.ts @@ -1,8 +1,12 @@ -import { AsyncLocalStorage } from 'node:async_hooks'; +import { type AsyncLocalStorage } from 'node:async_hooks'; -function createAsyncLocalStorage(): AsyncLocalStorage { - return new AsyncLocalStorage(); -} +async function createAsyncLocalStorage(): Promise> { + if (typeof window === 'undefined') { + const { AsyncLocalStorage } = await import('node:async_hooks'); + return new AsyncLocalStorage(); + } + return {} as AsyncLocalStorage; +} -export const authAsyncStorage = createAsyncLocalStorage(); \ No newline at end of file +export const authAsyncStorage = await createAsyncLocalStorage(); diff --git a/packages/astro-clerk-auth/src/client/react/hooks.ts b/packages/astro-clerk-auth/src/client/react/hooks.ts index 824d851..26011dd 100644 --- a/packages/astro-clerk-auth/src/client/react/hooks.ts +++ b/packages/astro-clerk-auth/src/client/react/hooks.ts @@ -10,7 +10,8 @@ import type { import type { Store, StoreValue } from 'nanostores'; import { useCallback, useSyncExternalStore } from 'react'; import { $authStore, $clerk, $csrState } from '../../stores/internal'; -import { authAsyncStorage } from '../../server/async-local-storage'; +// @ts-ignore +import { authAsyncStorage } from '#async-local-storage'; type CheckAuthorizationSignedOut = undefined; type CheckAuthorizationWithoutOrgOrUser = (params?: Parameters[0]) => false; diff --git a/packages/astro-clerk-auth/src/server/async-local-storage.ts b/packages/astro-clerk-auth/src/server/async-local-storage.ts deleted file mode 100644 index 0cc94de..0000000 --- a/packages/astro-clerk-auth/src/server/async-local-storage.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { type AsyncLocalStorage } from 'node:async_hooks'; - -async function createAsyncLocalStorage(): Promise> { - if (typeof window === 'undefined') { - const { AsyncLocalStorage } = await import('node:async_hooks'); - return new AsyncLocalStorage(); - } - - return {} as AsyncLocalStorage; -} - -export const authAsyncStorage = await createAsyncLocalStorage(); diff --git a/packages/astro-clerk-auth/src/server/clerk-middleware.ts b/packages/astro-clerk-auth/src/server/clerk-middleware.ts index 64a0396..a5f64eb 100644 --- a/packages/astro-clerk-auth/src/server/clerk-middleware.ts +++ b/packages/astro-clerk-auth/src/server/clerk-middleware.ts @@ -14,7 +14,8 @@ import { APIContext } from 'astro'; import { createCurrentUser } from './current-user'; import { isRedirect, setHeader } from './utils'; import { serverRedirectWithAuth } from './server-redirect-with-auth'; -import { authAsyncStorage } from './async-local-storage'; +// @ts-ignore +import { authAsyncStorage } from "#async-local-storage"; import { buildClerkHotloadScript } from './build-clerk-hotload-script'; const CONTROL_FLOW_ERROR = { diff --git a/packages/astro-clerk-auth/src/server/index.ts b/packages/astro-clerk-auth/src/server/index.ts index 788f96e..209f067 100644 --- a/packages/astro-clerk-auth/src/server/index.ts +++ b/packages/astro-clerk-auth/src/server/index.ts @@ -41,5 +41,5 @@ export type { GetAuthReturn } from './get-auth'; /** * Export async storage for astro component to use directly */ -import { authAsyncStorage } from '../server/async-local-storage'; -export const __internal_authAsyncStorage = authAsyncStorage; +// import { authAsyncStorage } from '../server/async-local-storage'; +// export const __internal_authAsyncStorage = authAsyncStorage; diff --git a/packages/astro-clerk-auth/tsup.config.ts b/packages/astro-clerk-auth/tsup.config.ts index fa37815..6bd3dbc 100644 --- a/packages/astro-clerk-auth/tsup.config.ts +++ b/packages/astro-clerk-auth/tsup.config.ts @@ -29,6 +29,6 @@ export default defineConfig(() => { bundle: true, sourcemap: true, format: ['esm'], - external: ['astro', 'react', 'react-dom', 'node:async_hooks'], + external: ['astro', 'react', 'react-dom', 'node:async_hooks','#async-local-storage'], }; }); From eebac3fb96d87b9ed5e4749bdc34cf3eebb16b56 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Fri, 31 May 2024 21:55:18 +0300 Subject: [PATCH 3/3] update path --- packages/astro-clerk-auth/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/astro-clerk-auth/package.json b/packages/astro-clerk-auth/package.json index 5dc9f1d..bb3b0e2 100644 --- a/packages/astro-clerk-auth/package.json +++ b/packages/astro-clerk-auth/package.json @@ -104,8 +104,8 @@ }, "imports": { "#async-local-storage": { - "node": "./async-local-storage.server.js", - "default": "./async-local-storage.client.js" + "node": "./dist/async-local-storage.server.js", + "default": "./dist/async-local-storage.client.js" } }, "dependencies": {