Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elef/wip #155

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/astro-clerk-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions packages/astro-clerk-auth/src/async-local-storage.client.ts
Original file line number Diff line number Diff line change
@@ -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<Store extends {}> implements AsyncLocalStorage<Store> {
disable(): void {
throw sharedAsyncLocalStorageNotAvailableError;
}

getStore(): Store | undefined {
// This fake implementation of AsyncLocalStorage always returns `undefined`.
return undefined;
}

run<R>(): R {
throw sharedAsyncLocalStorageNotAvailableError;
}

exit<R>(): R {
throw sharedAsyncLocalStorageNotAvailableError;
}

enterWith(): void {
throw sharedAsyncLocalStorageNotAvailableError;
}
}

function createAsyncLocalStorage<Store extends {}>(): AsyncLocalStorage<Store> {
return new FakeAsyncLocalStorage();
}

export const authAsyncStorage = createAsyncLocalStorage();
3 changes: 2 additions & 1 deletion packages/astro-clerk-auth/src/client/react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CheckAuthorizationWithCustomPermissions>[0]) => false;
Expand Down
3 changes: 2 additions & 1 deletion packages/astro-clerk-auth/src/server/clerk-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions packages/astro-clerk-auth/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 3 additions & 1 deletion packages/astro-clerk-auth/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'],
};
});
Loading