@better-auth/core 1.4.11 → 1.4.12-beta.1

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @better-auth/core@1.4.11 build /home/runner/work/better-auth/better-auth/packages/core
2
+ > @better-auth/core@1.4.12-beta.1 build /home/runner/work/better-auth/better-auth/packages/core
3
3
  > tsdown
4
4
 
5
5
  ℹ tsdown v0.17.2 powered by rolldown v1.0.0-beta.53
@@ -10,7 +10,7 @@
10
10
  ℹ dist/social-providers/index.mjs  80.96 kB │ gzip: 10.43 kB
11
11
  ℹ dist/db/adapter/index.mjs  38.55 kB │ gzip: 7.86 kB
12
12
  ℹ dist/db/index.mjs  1.66 kB │ gzip: 0.54 kB
13
- ℹ dist/api/index.mjs  1.23 kB │ gzip: 0.48 kB
13
+ ℹ dist/api/index.mjs  1.23 kB │ gzip: 0.49 kB
14
14
  ℹ dist/async_hooks/index.mjs  1.03 kB │ gzip: 0.54 kB
15
15
  ℹ dist/async_hooks/pure.index.mjs  0.99 kB │ gzip: 0.49 kB
16
16
  ℹ dist/oauth2/index.mjs  0.75 kB │ gzip: 0.28 kB
@@ -22,7 +22,7 @@
22
22
  ℹ dist/oauth2-BjWM15hm.mjs  12.82 kB │ gzip: 3.16 kB
23
23
  ℹ dist/env-DbssmzoK.mjs  7.67 kB │ gzip: 2.54 kB
24
24
  ℹ dist/get-tables-CMc_Emww.mjs  6.76 kB │ gzip: 1.30 kB
25
- ℹ dist/context-BBNwughv.mjs  4.87 kB │ gzip: 1.40 kB
25
+ ℹ dist/context-BGZ8V6DD.mjs  4.46 kB │ gzip: 1.10 kB
26
26
  ℹ dist/error-DP1xOn7P.mjs  2.76 kB │ gzip: 1.16 kB
27
27
  ℹ dist/utils-U2L7n92V.mjs  1.46 kB │ gzip: 0.70 kB
28
28
  ℹ dist/social-providers/index.d.mts  3.89 kB │ gzip: 1.19 kB
@@ -39,5 +39,5 @@
39
39
  ℹ dist/async_hooks/pure.index.d.mts  0.22 kB │ gzip: 0.16 kB
40
40
  ℹ dist/index-BHaoMSKV.d.mts 227.72 kB │ gzip: 35.92 kB
41
41
  ℹ dist/index-BRBu0-5h.d.mts  3.31 kB │ gzip: 1.11 kB
42
- ℹ 32 files, total: 410.89 kB
43
- ✔ Build complete in 5410ms
42
+ ℹ 32 files, total: 410.48 kB
43
+ ✔ Build complete in 5348ms
@@ -1,4 +1,4 @@
1
- import { f as runWithEndpointContext } from "../context-BBNwughv.mjs";
1
+ import { f as runWithEndpointContext } from "../context-BGZ8V6DD.mjs";
2
2
  import { createEndpoint, createMiddleware } from "better-call";
3
3
 
4
4
  //#region src/api/index.ts
@@ -1,3 +1,3 @@
1
- import { a as defineRequestState, c as hasRequestState, d as getCurrentAuthContextAsyncLocalStorage, f as runWithEndpointContext, i as runWithTransaction, l as runWithRequestState, n as getCurrentDBAdapterAsyncLocalStorage, o as getCurrentRequestState, r as runWithAdapter, s as getRequestStateAsyncLocalStorage, t as getCurrentAdapter, u as getCurrentAuthContext } from "../context-BBNwughv.mjs";
1
+ import { a as defineRequestState, c as hasRequestState, d as getCurrentAuthContextAsyncLocalStorage, f as runWithEndpointContext, i as runWithTransaction, l as runWithRequestState, n as getCurrentDBAdapterAsyncLocalStorage, o as getCurrentRequestState, r as runWithAdapter, s as getRequestStateAsyncLocalStorage, t as getCurrentAdapter, u as getCurrentAuthContext } from "../context-BGZ8V6DD.mjs";
2
2
 
3
3
  export { defineRequestState, getCurrentAdapter, getCurrentAuthContext, getCurrentAuthContextAsyncLocalStorage, getCurrentDBAdapterAsyncLocalStorage, getCurrentRequestState, getRequestStateAsyncLocalStorage, hasRequestState, runWithAdapter, runWithEndpointContext, runWithRequestState, runWithTransaction };
@@ -1,10 +1,14 @@
1
1
  import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
2
2
 
3
3
  //#region src/context/endpoint-context.ts
4
+ const symbol$2 = Symbol.for("better-auth:endpoint-context-async-storage");
4
5
  let currentContextAsyncStorage = null;
5
6
  const ensureAsyncStorage$2 = async () => {
6
- if (!currentContextAsyncStorage) currentContextAsyncStorage = new (await (getAsyncLocalStorage()))();
7
- return currentContextAsyncStorage;
7
+ if (!currentContextAsyncStorage || globalThis[symbol$2] === void 0) {
8
+ currentContextAsyncStorage = new (await (getAsyncLocalStorage()))();
9
+ globalThis[symbol$2] = currentContextAsyncStorage;
10
+ }
11
+ return currentContextAsyncStorage || globalThis[symbol$2];
8
12
  };
9
13
  /**
10
14
  * This is for internal use only. Most users should use `getCurrentAuthContext` instead.
@@ -25,10 +29,14 @@ async function runWithEndpointContext(context, fn) {
25
29
 
26
30
  //#endregion
27
31
  //#region src/context/request-state.ts
32
+ const symbol$1 = Symbol.for("better-auth:request-state-async-storage");
28
33
  let requestStateAsyncStorage = null;
29
34
  const ensureAsyncStorage$1 = async () => {
30
- if (!requestStateAsyncStorage) requestStateAsyncStorage = new (await (getAsyncLocalStorage()))();
31
- return requestStateAsyncStorage;
35
+ if (!requestStateAsyncStorage || globalThis[symbol$1] === void 0) {
36
+ requestStateAsyncStorage = new (await (getAsyncLocalStorage()))();
37
+ globalThis[symbol$1] = requestStateAsyncStorage;
38
+ }
39
+ return requestStateAsyncStorage || globalThis[symbol$1];
32
40
  };
33
41
  async function getRequestStateAsyncLocalStorage() {
34
42
  return ensureAsyncStorage$1();
@@ -67,10 +75,14 @@ function defineRequestState(initFn) {
67
75
 
68
76
  //#endregion
69
77
  //#region src/context/transaction.ts
78
+ const symbol = Symbol.for("better-auth:transaction-adapter-async-storage");
70
79
  let currentAdapterAsyncStorage = null;
71
80
  const ensureAsyncStorage = async () => {
72
- if (!currentAdapterAsyncStorage) currentAdapterAsyncStorage = new (await (getAsyncLocalStorage()))();
73
- return currentAdapterAsyncStorage;
81
+ if (!currentAdapterAsyncStorage || globalThis[symbol] === void 0) {
82
+ currentAdapterAsyncStorage = new (await (getAsyncLocalStorage()))();
83
+ globalThis[symbol] = currentAdapterAsyncStorage;
84
+ }
85
+ return currentAdapterAsyncStorage || globalThis[symbol];
74
86
  };
75
87
  /**
76
88
  * This is for internal use only. Most users should use `getCurrentAdapter` instead.
@@ -110,24 +122,5 @@ const runWithTransaction = async (adapter, fn) => {
110
122
  });
111
123
  };
112
124
 
113
- //#endregion
114
- //#region src/context/index.ts
115
- const glo = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {};
116
- const importIdentifier = "__ $BETTER_AUTH$ __";
117
- if (glo[importIdentifier] === true)
118
- /**
119
- * Dear reader of this message. Please take this seriously.
120
- *
121
- * If you see this message, make sure that you only import one version of Better Auth. In many cases,
122
- * your package manager installs two versions of Better Auth that are used by different packages within your project.
123
- *
124
- * This often leads to issues that are hard to debug. We often need to ensure async local storage instance,
125
- * If you imported different versions of Better Auth, it is impossible for us to
126
- * do status synchronization per request anymore - which might break the states.
127
- *
128
- */
129
- console.error("Better Auth was already imported. This breaks async local storage instance and will lead to issues!");
130
- glo[importIdentifier] = true;
131
-
132
125
  //#endregion
133
126
  export { defineRequestState as a, hasRequestState as c, getCurrentAuthContextAsyncLocalStorage as d, runWithEndpointContext as f, runWithTransaction as i, runWithRequestState as l, getCurrentDBAdapterAsyncLocalStorage as n, getCurrentRequestState as o, runWithAdapter as r, getRequestStateAsyncLocalStorage as s, getCurrentAdapter as t, getCurrentAuthContext as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/core",
3
- "version": "1.4.11",
3
+ "version": "1.4.12-beta.1",
4
4
  "description": "The most comprehensive authentication framework for TypeScript.",
5
5
  "type": "module",
6
6
  "repository": {
@@ -9,15 +9,24 @@ export type AuthEndpointContext = Partial<
9
9
  context: AuthContext;
10
10
  };
11
11
 
12
+ const symbol = Symbol.for("better-auth:endpoint-context-async-storage");
13
+
12
14
  let currentContextAsyncStorage: AsyncLocalStorage<AuthEndpointContext> | null =
13
15
  null;
14
16
 
15
17
  const ensureAsyncStorage = async () => {
16
- if (!currentContextAsyncStorage) {
18
+ if (
19
+ !currentContextAsyncStorage ||
20
+ (globalThis as any)[symbol] === undefined
21
+ ) {
17
22
  const AsyncLocalStorage = await getAsyncLocalStorage();
18
23
  currentContextAsyncStorage = new AsyncLocalStorage();
24
+ (globalThis as any)[symbol] = currentContextAsyncStorage;
19
25
  }
20
- return currentContextAsyncStorage;
26
+ return (
27
+ currentContextAsyncStorage ||
28
+ ((globalThis as any)[symbol] as AsyncLocalStorage<AuthEndpointContext>)
29
+ );
21
30
  };
22
31
 
23
32
  /**
@@ -19,32 +19,3 @@ export {
19
19
  runWithAdapter,
20
20
  runWithTransaction,
21
21
  } from "./transaction";
22
-
23
- const glo: any =
24
- typeof globalThis !== "undefined"
25
- ? globalThis
26
- : typeof window !== "undefined"
27
- ? window
28
- : typeof global !== "undefined"
29
- ? global
30
- : {};
31
-
32
- const importIdentifier = "__ $BETTER_AUTH$ __";
33
-
34
- if (glo[importIdentifier] === true) {
35
- /**
36
- * Dear reader of this message. Please take this seriously.
37
- *
38
- * If you see this message, make sure that you only import one version of Better Auth. In many cases,
39
- * your package manager installs two versions of Better Auth that are used by different packages within your project.
40
- *
41
- * This often leads to issues that are hard to debug. We often need to ensure async local storage instance,
42
- * If you imported different versions of Better Auth, it is impossible for us to
43
- * do status synchronization per request anymore - which might break the states.
44
- *
45
- */
46
- console.error(
47
- "Better Auth was already imported. This breaks async local storage instance and will lead to issues!",
48
- );
49
- }
50
- glo[importIdentifier] = true;
@@ -3,15 +3,21 @@ import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
3
 
4
4
  export type RequestStateWeakMap = WeakMap<object, any>;
5
5
 
6
+ const symbol = Symbol.for("better-auth:request-state-async-storage");
7
+
6
8
  let requestStateAsyncStorage: AsyncLocalStorage<RequestStateWeakMap> | null =
7
9
  null;
8
10
 
9
11
  const ensureAsyncStorage = async () => {
10
- if (!requestStateAsyncStorage) {
12
+ if (!requestStateAsyncStorage || (globalThis as any)[symbol] === undefined) {
11
13
  const AsyncLocalStorage = await getAsyncLocalStorage();
12
14
  requestStateAsyncStorage = new AsyncLocalStorage();
15
+ (globalThis as any)[symbol] = requestStateAsyncStorage;
13
16
  }
14
- return requestStateAsyncStorage;
17
+ return (
18
+ requestStateAsyncStorage ||
19
+ ((globalThis as any)[symbol] as AsyncLocalStorage<RequestStateWeakMap>)
20
+ );
15
21
  };
16
22
 
17
23
  export async function getRequestStateAsyncLocalStorage() {
@@ -2,15 +2,24 @@ import type { AsyncLocalStorage } from "@better-auth/core/async_hooks";
2
2
  import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
3
  import type { DBAdapter, DBTransactionAdapter } from "../db/adapter";
4
4
 
5
+ const symbol = Symbol.for("better-auth:transaction-adapter-async-storage");
6
+
5
7
  let currentAdapterAsyncStorage: AsyncLocalStorage<DBTransactionAdapter> | null =
6
8
  null;
7
9
 
8
10
  const ensureAsyncStorage = async () => {
9
- if (!currentAdapterAsyncStorage) {
11
+ if (
12
+ !currentAdapterAsyncStorage ||
13
+ (globalThis as any)[symbol] === undefined
14
+ ) {
10
15
  const AsyncLocalStorage = await getAsyncLocalStorage();
11
16
  currentAdapterAsyncStorage = new AsyncLocalStorage();
17
+ (globalThis as any)[symbol] = currentAdapterAsyncStorage;
12
18
  }
13
- return currentAdapterAsyncStorage;
19
+ return (
20
+ currentAdapterAsyncStorage ||
21
+ ((globalThis as any)[symbol] as AsyncLocalStorage<DBTransactionAdapter>)
22
+ );
14
23
  };
15
24
 
16
25
  /**