@hammadj/better-auth-core 1.5.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +266 -0
- package/.turbo/turbo-test.log +2 -0
- package/LICENSE.md +20 -0
- package/dist/api/index.d.mts +181 -0
- package/dist/api/index.mjs +34 -0
- package/dist/api/index.mjs.map +1 -0
- package/dist/async_hooks/index.d.mts +7 -0
- package/dist/async_hooks/index.mjs +22 -0
- package/dist/async_hooks/index.mjs.map +1 -0
- package/dist/async_hooks/pure.index.d.mts +7 -0
- package/dist/async_hooks/pure.index.mjs +35 -0
- package/dist/async_hooks/pure.index.mjs.map +1 -0
- package/dist/context/endpoint-context.d.mts +19 -0
- package/dist/context/endpoint-context.mjs +32 -0
- package/dist/context/endpoint-context.mjs.map +1 -0
- package/dist/context/global.d.mts +7 -0
- package/dist/context/global.mjs +38 -0
- package/dist/context/global.mjs.map +1 -0
- package/dist/context/index.d.mts +5 -0
- package/dist/context/index.mjs +6 -0
- package/dist/context/request-state.d.mts +26 -0
- package/dist/context/request-state.mjs +50 -0
- package/dist/context/request-state.mjs.map +1 -0
- package/dist/context/transaction.d.mts +25 -0
- package/dist/context/transaction.mjs +96 -0
- package/dist/context/transaction.mjs.map +1 -0
- package/dist/db/adapter/factory.d.mts +28 -0
- package/dist/db/adapter/factory.mjs +716 -0
- package/dist/db/adapter/factory.mjs.map +1 -0
- package/dist/db/adapter/get-default-field-name.d.mts +19 -0
- package/dist/db/adapter/get-default-field-name.mjs +39 -0
- package/dist/db/adapter/get-default-field-name.mjs.map +1 -0
- package/dist/db/adapter/get-default-model-name.d.mts +13 -0
- package/dist/db/adapter/get-default-model-name.mjs +33 -0
- package/dist/db/adapter/get-default-model-name.mjs.map +1 -0
- package/dist/db/adapter/get-field-attributes.d.mts +30 -0
- package/dist/db/adapter/get-field-attributes.mjs +40 -0
- package/dist/db/adapter/get-field-attributes.mjs.map +1 -0
- package/dist/db/adapter/get-field-name.d.mts +19 -0
- package/dist/db/adapter/get-field-name.mjs +34 -0
- package/dist/db/adapter/get-field-name.mjs.map +1 -0
- package/dist/db/adapter/get-id-field.d.mts +40 -0
- package/dist/db/adapter/get-id-field.mjs +68 -0
- package/dist/db/adapter/get-id-field.mjs.map +1 -0
- package/dist/db/adapter/get-model-name.d.mts +13 -0
- package/dist/db/adapter/get-model-name.mjs +24 -0
- package/dist/db/adapter/get-model-name.mjs.map +1 -0
- package/dist/db/adapter/index.d.mts +515 -0
- package/dist/db/adapter/index.mjs +10 -0
- package/dist/db/adapter/types.d.mts +140 -0
- package/dist/db/adapter/utils.d.mts +8 -0
- package/dist/db/adapter/utils.mjs +39 -0
- package/dist/db/adapter/utils.mjs.map +1 -0
- package/dist/db/get-tables.d.mts +9 -0
- package/dist/db/get-tables.mjs +267 -0
- package/dist/db/get-tables.mjs.map +1 -0
- package/dist/db/index.d.mts +10 -0
- package/dist/db/index.mjs +9 -0
- package/dist/db/plugin.d.mts +13 -0
- package/dist/db/schema/account.d.mts +27 -0
- package/dist/db/schema/account.mjs +20 -0
- package/dist/db/schema/account.mjs.map +1 -0
- package/dist/db/schema/rate-limit.d.mts +15 -0
- package/dist/db/schema/rate-limit.mjs +12 -0
- package/dist/db/schema/rate-limit.mjs.map +1 -0
- package/dist/db/schema/session.d.mts +22 -0
- package/dist/db/schema/session.mjs +15 -0
- package/dist/db/schema/session.mjs.map +1 -0
- package/dist/db/schema/shared.d.mts +11 -0
- package/dist/db/schema/shared.mjs +12 -0
- package/dist/db/schema/shared.mjs.map +1 -0
- package/dist/db/schema/user.d.mts +21 -0
- package/dist/db/schema/user.mjs +14 -0
- package/dist/db/schema/user.mjs.map +1 -0
- package/dist/db/schema/verification.d.mts +20 -0
- package/dist/db/schema/verification.mjs +13 -0
- package/dist/db/schema/verification.mjs.map +1 -0
- package/dist/db/type.d.mts +147 -0
- package/dist/env/color-depth.d.mts +5 -0
- package/dist/env/color-depth.mjs +89 -0
- package/dist/env/color-depth.mjs.map +1 -0
- package/dist/env/env-impl.d.mts +33 -0
- package/dist/env/env-impl.mjs +83 -0
- package/dist/env/env-impl.mjs.map +1 -0
- package/dist/env/index.d.mts +4 -0
- package/dist/env/index.mjs +5 -0
- package/dist/env/logger.d.mts +49 -0
- package/dist/env/logger.mjs +82 -0
- package/dist/env/logger.mjs.map +1 -0
- package/dist/error/codes.d.mts +199 -0
- package/dist/error/codes.mjs +57 -0
- package/dist/error/codes.mjs.map +1 -0
- package/dist/error/index.d.mts +20 -0
- package/dist/error/index.mjs +30 -0
- package/dist/error/index.mjs.map +1 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.mjs +1 -0
- package/dist/oauth2/client-credentials-token.d.mts +37 -0
- package/dist/oauth2/client-credentials-token.mjs +55 -0
- package/dist/oauth2/client-credentials-token.mjs.map +1 -0
- package/dist/oauth2/create-authorization-url.d.mts +46 -0
- package/dist/oauth2/create-authorization-url.mjs +43 -0
- package/dist/oauth2/create-authorization-url.mjs.map +1 -0
- package/dist/oauth2/index.d.mts +8 -0
- package/dist/oauth2/index.mjs +8 -0
- package/dist/oauth2/oauth-provider.d.mts +195 -0
- package/dist/oauth2/refresh-access-token.d.mts +36 -0
- package/dist/oauth2/refresh-access-token.mjs +59 -0
- package/dist/oauth2/refresh-access-token.mjs.map +1 -0
- package/dist/oauth2/utils.d.mts +8 -0
- package/dist/oauth2/utils.mjs +28 -0
- package/dist/oauth2/utils.mjs.map +1 -0
- package/dist/oauth2/validate-authorization-code.d.mts +56 -0
- package/dist/oauth2/validate-authorization-code.mjs +72 -0
- package/dist/oauth2/validate-authorization-code.mjs.map +1 -0
- package/dist/oauth2/verify.d.mts +43 -0
- package/dist/oauth2/verify.mjs +96 -0
- package/dist/oauth2/verify.mjs.map +1 -0
- package/dist/social-providers/apple.d.mts +120 -0
- package/dist/social-providers/apple.mjs +105 -0
- package/dist/social-providers/apple.mjs.map +1 -0
- package/dist/social-providers/atlassian.d.mts +73 -0
- package/dist/social-providers/atlassian.mjs +84 -0
- package/dist/social-providers/atlassian.mjs.map +1 -0
- package/dist/social-providers/cognito.d.mts +88 -0
- package/dist/social-providers/cognito.mjs +166 -0
- package/dist/social-providers/cognito.mjs.map +1 -0
- package/dist/social-providers/discord.d.mts +127 -0
- package/dist/social-providers/discord.mjs +65 -0
- package/dist/social-providers/discord.mjs.map +1 -0
- package/dist/social-providers/dropbox.d.mts +72 -0
- package/dist/social-providers/dropbox.mjs +76 -0
- package/dist/social-providers/dropbox.mjs.map +1 -0
- package/dist/social-providers/facebook.d.mts +82 -0
- package/dist/social-providers/facebook.mjs +121 -0
- package/dist/social-providers/facebook.mjs.map +1 -0
- package/dist/social-providers/figma.d.mts +64 -0
- package/dist/social-providers/figma.mjs +87 -0
- package/dist/social-providers/figma.mjs.map +1 -0
- package/dist/social-providers/github.d.mts +105 -0
- package/dist/social-providers/github.mjs +97 -0
- package/dist/social-providers/github.mjs.map +1 -0
- package/dist/social-providers/gitlab.d.mts +126 -0
- package/dist/social-providers/gitlab.mjs +83 -0
- package/dist/social-providers/gitlab.mjs.map +1 -0
- package/dist/social-providers/google.d.mts +100 -0
- package/dist/social-providers/google.mjs +109 -0
- package/dist/social-providers/google.mjs.map +1 -0
- package/dist/social-providers/huggingface.d.mts +86 -0
- package/dist/social-providers/huggingface.mjs +76 -0
- package/dist/social-providers/huggingface.mjs.map +1 -0
- package/dist/social-providers/index.d.mts +1725 -0
- package/dist/social-providers/index.mjs +77 -0
- package/dist/social-providers/index.mjs.map +1 -0
- package/dist/social-providers/kakao.d.mts +164 -0
- package/dist/social-providers/kakao.mjs +73 -0
- package/dist/social-providers/kakao.mjs.map +1 -0
- package/dist/social-providers/kick.d.mts +76 -0
- package/dist/social-providers/kick.mjs +72 -0
- package/dist/social-providers/kick.mjs.map +1 -0
- package/dist/social-providers/line.d.mts +108 -0
- package/dist/social-providers/line.mjs +114 -0
- package/dist/social-providers/line.mjs.map +1 -0
- package/dist/social-providers/linear.d.mts +71 -0
- package/dist/social-providers/linear.mjs +89 -0
- package/dist/social-providers/linear.mjs.map +1 -0
- package/dist/social-providers/linkedin.d.mts +70 -0
- package/dist/social-providers/linkedin.mjs +77 -0
- package/dist/social-providers/linkedin.mjs.map +1 -0
- package/dist/social-providers/microsoft-entra-id.d.mts +175 -0
- package/dist/social-providers/microsoft-entra-id.mjs +107 -0
- package/dist/social-providers/microsoft-entra-id.mjs.map +1 -0
- package/dist/social-providers/naver.d.mts +95 -0
- package/dist/social-providers/naver.mjs +68 -0
- package/dist/social-providers/naver.mjs.map +1 -0
- package/dist/social-providers/notion.d.mts +67 -0
- package/dist/social-providers/notion.mjs +76 -0
- package/dist/social-providers/notion.mjs.map +1 -0
- package/dist/social-providers/paybin.d.mts +74 -0
- package/dist/social-providers/paybin.mjs +86 -0
- package/dist/social-providers/paybin.mjs.map +1 -0
- package/dist/social-providers/paypal.d.mts +132 -0
- package/dist/social-providers/paypal.mjs +145 -0
- package/dist/social-providers/paypal.mjs.map +1 -0
- package/dist/social-providers/polar.d.mts +77 -0
- package/dist/social-providers/polar.mjs +74 -0
- package/dist/social-providers/polar.mjs.map +1 -0
- package/dist/social-providers/reddit.d.mts +65 -0
- package/dist/social-providers/reddit.mjs +84 -0
- package/dist/social-providers/reddit.mjs.map +1 -0
- package/dist/social-providers/roblox.d.mts +73 -0
- package/dist/social-providers/roblox.mjs +60 -0
- package/dist/social-providers/roblox.mjs.map +1 -0
- package/dist/social-providers/salesforce.d.mts +82 -0
- package/dist/social-providers/salesforce.mjs +92 -0
- package/dist/social-providers/salesforce.mjs.map +1 -0
- package/dist/social-providers/slack.d.mts +86 -0
- package/dist/social-providers/slack.mjs +69 -0
- package/dist/social-providers/slack.mjs.map +1 -0
- package/dist/social-providers/spotify.d.mts +66 -0
- package/dist/social-providers/spotify.mjs +72 -0
- package/dist/social-providers/spotify.mjs.map +1 -0
- package/dist/social-providers/tiktok.d.mts +171 -0
- package/dist/social-providers/tiktok.mjs +63 -0
- package/dist/social-providers/tiktok.mjs.map +1 -0
- package/dist/social-providers/twitch.d.mts +82 -0
- package/dist/social-providers/twitch.mjs +79 -0
- package/dist/social-providers/twitch.mjs.map +1 -0
- package/dist/social-providers/twitter.d.mts +129 -0
- package/dist/social-providers/twitter.mjs +88 -0
- package/dist/social-providers/twitter.mjs.map +1 -0
- package/dist/social-providers/vercel.d.mts +65 -0
- package/dist/social-providers/vercel.mjs +62 -0
- package/dist/social-providers/vercel.mjs.map +1 -0
- package/dist/social-providers/vk.d.mts +73 -0
- package/dist/social-providers/vk.mjs +84 -0
- package/dist/social-providers/vk.mjs.map +1 -0
- package/dist/social-providers/zoom.d.mts +173 -0
- package/dist/social-providers/zoom.mjs +73 -0
- package/dist/social-providers/zoom.mjs.map +1 -0
- package/dist/types/context.d.mts +267 -0
- package/dist/types/cookie.d.mts +16 -0
- package/dist/types/helper.d.mts +10 -0
- package/dist/types/index.d.mts +8 -0
- package/dist/types/init-options.d.mts +1314 -0
- package/dist/types/plugin-client.d.mts +112 -0
- package/dist/types/plugin.d.mts +125 -0
- package/dist/utils/db.d.mts +12 -0
- package/dist/utils/db.mjs +17 -0
- package/dist/utils/db.mjs.map +1 -0
- package/dist/utils/deprecate.d.mts +10 -0
- package/dist/utils/deprecate.mjs +18 -0
- package/dist/utils/deprecate.mjs.map +1 -0
- package/dist/utils/error-codes.d.mts +13 -0
- package/dist/utils/error-codes.mjs +12 -0
- package/dist/utils/error-codes.mjs.map +1 -0
- package/dist/utils/id.d.mts +5 -0
- package/dist/utils/id.mjs +10 -0
- package/dist/utils/id.mjs.map +1 -0
- package/dist/utils/ip.d.mts +55 -0
- package/dist/utils/ip.mjs +119 -0
- package/dist/utils/ip.mjs.map +1 -0
- package/dist/utils/json.d.mts +5 -0
- package/dist/utils/json.mjs +26 -0
- package/dist/utils/json.mjs.map +1 -0
- package/dist/utils/string.d.mts +5 -0
- package/dist/utils/string.mjs +8 -0
- package/dist/utils/string.mjs.map +1 -0
- package/dist/utils/url.d.mts +21 -0
- package/dist/utils/url.mjs +33 -0
- package/dist/utils/url.mjs.map +1 -0
- package/package.json +147 -0
- package/src/api/index.ts +106 -0
- package/src/async_hooks/index.ts +40 -0
- package/src/async_hooks/pure.index.ts +46 -0
- package/src/context/endpoint-context.ts +50 -0
- package/src/context/global.ts +57 -0
- package/src/context/index.ts +23 -0
- package/src/context/request-state.test.ts +94 -0
- package/src/context/request-state.ts +91 -0
- package/src/context/transaction.ts +136 -0
- package/src/db/adapter/factory.ts +1362 -0
- package/src/db/adapter/get-default-field-name.ts +59 -0
- package/src/db/adapter/get-default-model-name.ts +51 -0
- package/src/db/adapter/get-field-attributes.ts +62 -0
- package/src/db/adapter/get-field-name.ts +43 -0
- package/src/db/adapter/get-id-field.ts +141 -0
- package/src/db/adapter/get-model-name.ts +36 -0
- package/src/db/adapter/index.ts +554 -0
- package/src/db/adapter/types.ts +171 -0
- package/src/db/adapter/utils.ts +61 -0
- package/src/db/get-tables.ts +296 -0
- package/src/db/index.ts +18 -0
- package/src/db/plugin.ts +11 -0
- package/src/db/schema/account.ts +34 -0
- package/src/db/schema/rate-limit.ts +21 -0
- package/src/db/schema/session.ts +17 -0
- package/src/db/schema/shared.ts +7 -0
- package/src/db/schema/user.ts +16 -0
- package/src/db/schema/verification.ts +15 -0
- package/src/db/test/get-tables.test.ts +116 -0
- package/src/db/type.ts +180 -0
- package/src/env/color-depth.ts +172 -0
- package/src/env/env-impl.ts +124 -0
- package/src/env/index.ts +23 -0
- package/src/env/logger.test.ts +34 -0
- package/src/env/logger.ts +145 -0
- package/src/error/codes.ts +58 -0
- package/src/error/index.ts +35 -0
- package/src/index.ts +1 -0
- package/src/oauth2/client-credentials-token.ts +102 -0
- package/src/oauth2/create-authorization-url.ts +87 -0
- package/src/oauth2/index.ts +26 -0
- package/src/oauth2/oauth-provider.ts +222 -0
- package/src/oauth2/refresh-access-token.ts +124 -0
- package/src/oauth2/utils.ts +38 -0
- package/src/oauth2/validate-authorization-code.ts +149 -0
- package/src/oauth2/validate-token.test.ts +174 -0
- package/src/oauth2/verify.ts +221 -0
- package/src/social-providers/apple.ts +223 -0
- package/src/social-providers/atlassian.ts +132 -0
- package/src/social-providers/cognito.ts +279 -0
- package/src/social-providers/discord.ts +169 -0
- package/src/social-providers/dropbox.ts +112 -0
- package/src/social-providers/facebook.ts +206 -0
- package/src/social-providers/figma.ts +117 -0
- package/src/social-providers/github.ts +184 -0
- package/src/social-providers/gitlab.ts +155 -0
- package/src/social-providers/google.ts +199 -0
- package/src/social-providers/huggingface.ts +118 -0
- package/src/social-providers/index.ts +127 -0
- package/src/social-providers/kakao.ts +178 -0
- package/src/social-providers/kick.ts +109 -0
- package/src/social-providers/line.ts +169 -0
- package/src/social-providers/linear.ts +121 -0
- package/src/social-providers/linkedin.ts +110 -0
- package/src/social-providers/microsoft-entra-id.ts +259 -0
- package/src/social-providers/naver.ts +112 -0
- package/src/social-providers/notion.ts +108 -0
- package/src/social-providers/paybin.ts +122 -0
- package/src/social-providers/paypal.ts +263 -0
- package/src/social-providers/polar.ts +110 -0
- package/src/social-providers/reddit.ts +122 -0
- package/src/social-providers/roblox.ts +111 -0
- package/src/social-providers/salesforce.ts +159 -0
- package/src/social-providers/slack.ts +111 -0
- package/src/social-providers/spotify.ts +93 -0
- package/src/social-providers/tiktok.ts +209 -0
- package/src/social-providers/twitch.ts +111 -0
- package/src/social-providers/twitter.ts +198 -0
- package/src/social-providers/vercel.ts +87 -0
- package/src/social-providers/vk.ts +124 -0
- package/src/social-providers/zoom.ts +238 -0
- package/src/types/context.ts +396 -0
- package/src/types/cookie.ts +10 -0
- package/src/types/helper.ts +26 -0
- package/src/types/index.ts +32 -0
- package/src/types/init-options.ts +1529 -0
- package/src/types/plugin-client.ts +127 -0
- package/src/types/plugin.ts +157 -0
- package/src/utils/db.ts +20 -0
- package/src/utils/deprecate.test.ts +72 -0
- package/src/utils/deprecate.ts +21 -0
- package/src/utils/error-codes.ts +65 -0
- package/src/utils/id.ts +5 -0
- package/src/utils/ip.test.ts +255 -0
- package/src/utils/ip.ts +211 -0
- package/src/utils/json.ts +25 -0
- package/src/utils/string.ts +3 -0
- package/src/utils/url.ts +43 -0
- package/tsconfig.json +7 -0
- package/tsdown.config.ts +35 -0
- package/vitest.config.ts +3 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { AsyncLocalStorage } from "@better-auth/core/async_hooks";
|
|
2
|
+
import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
|
|
3
|
+
import { __getBetterAuthGlobal } from "./global";
|
|
4
|
+
|
|
5
|
+
export type RequestStateWeakMap = WeakMap<object, any>;
|
|
6
|
+
|
|
7
|
+
const ensureAsyncStorage = async () => {
|
|
8
|
+
const betterAuthGlobal = __getBetterAuthGlobal();
|
|
9
|
+
if (!betterAuthGlobal.context.requestStateAsyncStorage) {
|
|
10
|
+
const AsyncLocalStorage = await getAsyncLocalStorage();
|
|
11
|
+
betterAuthGlobal.context.requestStateAsyncStorage =
|
|
12
|
+
new AsyncLocalStorage<RequestStateWeakMap>();
|
|
13
|
+
}
|
|
14
|
+
return betterAuthGlobal.context
|
|
15
|
+
.requestStateAsyncStorage as AsyncLocalStorage<RequestStateWeakMap>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export async function getRequestStateAsyncLocalStorage() {
|
|
19
|
+
return ensureAsyncStorage();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function hasRequestState() {
|
|
23
|
+
const als = await ensureAsyncStorage();
|
|
24
|
+
return als.getStore() !== undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function getCurrentRequestState(): Promise<RequestStateWeakMap> {
|
|
28
|
+
const als = await ensureAsyncStorage();
|
|
29
|
+
const store = als.getStore();
|
|
30
|
+
if (!store) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
"No request state found. Please make sure you are calling this function within a `runWithRequestState` callback.",
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return store;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function runWithRequestState<T>(
|
|
39
|
+
store: RequestStateWeakMap,
|
|
40
|
+
fn: () => T,
|
|
41
|
+
): Promise<T> {
|
|
42
|
+
const als = await ensureAsyncStorage();
|
|
43
|
+
return als.run(store, fn);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface RequestState<T> {
|
|
47
|
+
get(): Promise<T>;
|
|
48
|
+
set(value: T): Promise<void>;
|
|
49
|
+
|
|
50
|
+
// A unique reference used as a key to identify this state within the request's WeakMap. Useful for debugging purposes.
|
|
51
|
+
readonly ref: Readonly<object>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Defines a request-scoped state with lazy initialization.
|
|
56
|
+
*
|
|
57
|
+
* @param initFn - A function that initializes the state. It is called the first time `get()` is invoked within each request context, and only once per context.
|
|
58
|
+
* @returns A RequestState object with `get` and `set` methods, and a unique `ref` for debugging.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const userState = defineRequestState(() => ({ id: '', name: '' }));
|
|
62
|
+
* // Later, within a request context:
|
|
63
|
+
* const user = await userState.get();
|
|
64
|
+
*/
|
|
65
|
+
export function defineRequestState<T>(
|
|
66
|
+
initFn: () => T | Promise<T>,
|
|
67
|
+
): RequestState<T>;
|
|
68
|
+
export function defineRequestState(
|
|
69
|
+
initFn: () => any | Promise<any>,
|
|
70
|
+
): RequestState<any> {
|
|
71
|
+
const ref = Object.freeze({});
|
|
72
|
+
return {
|
|
73
|
+
get ref(): Readonly<object> {
|
|
74
|
+
return ref;
|
|
75
|
+
},
|
|
76
|
+
async get() {
|
|
77
|
+
const store = await getCurrentRequestState();
|
|
78
|
+
if (!store.has(ref)) {
|
|
79
|
+
const initialValue = await initFn();
|
|
80
|
+
store.set(ref, initialValue);
|
|
81
|
+
return initialValue;
|
|
82
|
+
}
|
|
83
|
+
return store.get(ref);
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
async set(value) {
|
|
87
|
+
const store = await getCurrentRequestState();
|
|
88
|
+
store.set(ref, value);
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
|
|
3
|
+
import type { DBAdapter, DBTransactionAdapter } from "../db/adapter";
|
|
4
|
+
import { __getBetterAuthGlobal } from "./global";
|
|
5
|
+
|
|
6
|
+
type HookContext = {
|
|
7
|
+
adapter: DBTransactionAdapter;
|
|
8
|
+
pendingHooks: Array<() => Promise<void>>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const ensureAsyncStorage = async () => {
|
|
12
|
+
const betterAuthGlobal = __getBetterAuthGlobal();
|
|
13
|
+
if (!betterAuthGlobal.context.adapterAsyncStorage) {
|
|
14
|
+
const AsyncLocalStorage = await getAsyncLocalStorage();
|
|
15
|
+
betterAuthGlobal.context.adapterAsyncStorage = new AsyncLocalStorage();
|
|
16
|
+
}
|
|
17
|
+
return betterAuthGlobal.context
|
|
18
|
+
.adapterAsyncStorage as AsyncLocalStorage<HookContext>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* This is for internal use only. Most users should use `getCurrentAdapter` instead.
|
|
23
|
+
*
|
|
24
|
+
* It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
|
|
25
|
+
*/
|
|
26
|
+
export const getCurrentDBAdapterAsyncLocalStorage = async () => {
|
|
27
|
+
return ensureAsyncStorage();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const getCurrentAdapter = async (
|
|
31
|
+
fallback: DBTransactionAdapter,
|
|
32
|
+
): Promise<DBTransactionAdapter> => {
|
|
33
|
+
return ensureAsyncStorage()
|
|
34
|
+
.then((als) => {
|
|
35
|
+
const store = als.getStore();
|
|
36
|
+
return store?.adapter || fallback;
|
|
37
|
+
})
|
|
38
|
+
.catch(() => {
|
|
39
|
+
return fallback;
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const runWithAdapter = async <R>(
|
|
44
|
+
adapter: DBAdapter,
|
|
45
|
+
fn: () => R,
|
|
46
|
+
): Promise<R> => {
|
|
47
|
+
let called = false;
|
|
48
|
+
return ensureAsyncStorage()
|
|
49
|
+
.then(async (als) => {
|
|
50
|
+
called = true;
|
|
51
|
+
const pendingHooks: Array<() => Promise<void>> = [];
|
|
52
|
+
let result: Awaited<R>;
|
|
53
|
+
let error: unknown;
|
|
54
|
+
let hasError = false;
|
|
55
|
+
try {
|
|
56
|
+
result = await als.run({ adapter, pendingHooks }, fn);
|
|
57
|
+
} catch (err) {
|
|
58
|
+
error = err;
|
|
59
|
+
hasError = true;
|
|
60
|
+
}
|
|
61
|
+
// Execute pending hooks after the function completes (even if it threw)
|
|
62
|
+
for (const hook of pendingHooks) {
|
|
63
|
+
await hook();
|
|
64
|
+
}
|
|
65
|
+
if (hasError) {
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
return result!;
|
|
69
|
+
})
|
|
70
|
+
.catch((err) => {
|
|
71
|
+
if (!called) {
|
|
72
|
+
return fn();
|
|
73
|
+
}
|
|
74
|
+
throw err;
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const runWithTransaction = async <R>(
|
|
79
|
+
adapter: DBAdapter,
|
|
80
|
+
fn: () => R,
|
|
81
|
+
): Promise<R> => {
|
|
82
|
+
let called = true;
|
|
83
|
+
return ensureAsyncStorage()
|
|
84
|
+
.then(async (als) => {
|
|
85
|
+
called = true;
|
|
86
|
+
const pendingHooks: Array<() => Promise<void>> = [];
|
|
87
|
+
let result: Awaited<R>;
|
|
88
|
+
let error: unknown;
|
|
89
|
+
let hasError = false;
|
|
90
|
+
try {
|
|
91
|
+
result = await adapter.transaction(async (trx) => {
|
|
92
|
+
return als.run({ adapter: trx, pendingHooks }, fn);
|
|
93
|
+
});
|
|
94
|
+
} catch (e) {
|
|
95
|
+
hasError = true;
|
|
96
|
+
error = e;
|
|
97
|
+
}
|
|
98
|
+
for (const hook of pendingHooks) {
|
|
99
|
+
await hook();
|
|
100
|
+
}
|
|
101
|
+
if (hasError) {
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
return result!;
|
|
105
|
+
})
|
|
106
|
+
.catch((err) => {
|
|
107
|
+
if (!called) {
|
|
108
|
+
return fn();
|
|
109
|
+
}
|
|
110
|
+
throw err;
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Queue a hook to be executed after the current transaction commits.
|
|
116
|
+
* If not in a transaction, the hook will execute immediately.
|
|
117
|
+
*/
|
|
118
|
+
export const queueAfterTransactionHook = async (
|
|
119
|
+
hook: () => Promise<void>,
|
|
120
|
+
): Promise<void> => {
|
|
121
|
+
return ensureAsyncStorage()
|
|
122
|
+
.then((als) => {
|
|
123
|
+
const store = als.getStore();
|
|
124
|
+
if (store) {
|
|
125
|
+
// We're in a transaction context, queue the hook
|
|
126
|
+
store.pendingHooks.push(hook);
|
|
127
|
+
} else {
|
|
128
|
+
// Not in a transaction, execute immediately
|
|
129
|
+
return hook();
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
.catch(() => {
|
|
133
|
+
// No async storage available, execute immediately
|
|
134
|
+
return hook();
|
|
135
|
+
});
|
|
136
|
+
};
|