diff --git a/packages/astro-clerk-auth/package.json b/packages/astro-clerk-auth/package.json index bc1479d..bb3b0e2 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": "./dist/async-local-storage.server.js", + "default": "./dist/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 new file mode 100644 index 0000000..6819823 --- /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; + } +} + +function createAsyncLocalStorage(): AsyncLocalStorage { + return new FakeAsyncLocalStorage(); +} + +export const authAsyncStorage = createAsyncLocalStorage(); diff --git a/packages/astro-clerk-auth/src/server/async-local-storage.ts b/packages/astro-clerk-auth/src/async-local-storage.server.ts similarity index 100% rename from packages/astro-clerk-auth/src/server/async-local-storage.ts rename to packages/astro-clerk-auth/src/async-local-storage.server.ts 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/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 2bd3d6e..6bd3dbc 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, @@ -27,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'], }; });