@better-auth/core 1.7.0-rc.6 → 1.7.0

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.
@@ -3,10 +3,10 @@ import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
3
  //#region src/context/endpoint-context.ts
4
4
  const ensureAsyncStorage = async () => {
5
5
  const betterAuthGlobal = __getBetterAuthGlobal();
6
- if (!betterAuthGlobal.context.endpointContextAsyncStorage) {
7
- const AsyncLocalStorage = await getAsyncLocalStorage();
8
- betterAuthGlobal.context.endpointContextAsyncStorage = new AsyncLocalStorage();
9
- }
6
+ const existing = betterAuthGlobal.context.endpointContextAsyncStorage;
7
+ if (existing) return existing;
8
+ const AsyncLocalStorage = await getAsyncLocalStorage();
9
+ betterAuthGlobal.context.endpointContextAsyncStorage ??= new AsyncLocalStorage();
10
10
  return betterAuthGlobal.context.endpointContextAsyncStorage;
11
11
  };
12
12
  /**
@@ -2,7 +2,7 @@
2
2
  const symbol = Symbol.for("better-auth:global");
3
3
  let bind = null;
4
4
  const __context = {};
5
- const __betterAuthVersion = "1.7.0-rc.6";
5
+ const __betterAuthVersion = "1.7.0";
6
6
  /**
7
7
  * We store context instance in the globalThis.
8
8
  *
@@ -1,16 +1,13 @@
1
1
  import { __getBetterAuthGlobal } from "./global.mjs";
2
2
  import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
3
  //#region src/context/request-state.ts
4
- let asyncStorageInit = null;
5
4
  const ensureAsyncStorage = async () => {
6
5
  const betterAuthGlobal = __getBetterAuthGlobal();
7
6
  const existing = betterAuthGlobal.context.requestStateAsyncStorage;
8
7
  if (existing) return existing;
9
- if (!asyncStorageInit) asyncStorageInit = getAsyncLocalStorage().then((AsyncLocalStorage) => {
10
- betterAuthGlobal.context.requestStateAsyncStorage ??= new AsyncLocalStorage();
11
- return betterAuthGlobal.context.requestStateAsyncStorage;
12
- });
13
- return asyncStorageInit;
8
+ const AsyncLocalStorage = await getAsyncLocalStorage();
9
+ betterAuthGlobal.context.requestStateAsyncStorage ??= new AsyncLocalStorage();
10
+ return betterAuthGlobal.context.requestStateAsyncStorage;
14
11
  };
15
12
  async function getRequestStateAsyncLocalStorage() {
16
13
  return ensureAsyncStorage();
@@ -1,6 +1,6 @@
1
1
  import { DBAdapter, DBTransactionAdapter } from "../db/adapter/index.mjs";
2
2
  import { BetterAuthOptions } from "../types/init-options.mjs";
3
- import { AsyncLocalStorage } from "node:async_hooks";
3
+ import { AsyncLocalStorage } from "@better-auth/core/async_hooks";
4
4
 
5
5
  //#region src/context/transaction.d.ts
6
6
  type StoredAdapter = DBTransactionAdapter<BetterAuthOptions>;
@@ -3,10 +3,10 @@ import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
3
  //#region src/context/transaction.ts
4
4
  const ensureAsyncStorage = async () => {
5
5
  const betterAuthGlobal = __getBetterAuthGlobal();
6
- if (!betterAuthGlobal.context.adapterAsyncStorage) {
7
- const AsyncLocalStorage = await getAsyncLocalStorage();
8
- betterAuthGlobal.context.adapterAsyncStorage = new AsyncLocalStorage();
9
- }
6
+ const existing = betterAuthGlobal.context.adapterAsyncStorage;
7
+ if (existing) return existing;
8
+ const AsyncLocalStorage = await getAsyncLocalStorage();
9
+ betterAuthGlobal.context.adapterAsyncStorage ??= new AsyncLocalStorage();
10
10
  return betterAuthGlobal.context.adapterAsyncStorage;
11
11
  };
12
12
  /**
@@ -2,7 +2,7 @@ import { ATTR_HTTP_RESPONSE_STATUS_CODE } from "./attributes.mjs";
2
2
  import { getOpenTelemetryAPI } from "./api.mjs";
3
3
  //#region src/instrumentation/tracer.ts
4
4
  const INSTRUMENTATION_SCOPE = "better-auth";
5
- const INSTRUMENTATION_VERSION = "1.7.0-rc.6";
5
+ const INSTRUMENTATION_VERSION = "1.7.0";
6
6
  /**
7
7
  * Better-auth uses `throw ctx.redirect(url)` for flow control (e.g. OAuth
8
8
  * callbacks). These are APIErrors with 3xx status codes and should not be
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/core",
3
- "version": "1.7.0-rc.6",
3
+ "version": "1.7.0",
4
4
  "description": "The most comprehensive authentication framework for TypeScript.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -12,11 +12,13 @@ export type AuthEndpointContext = Partial<
12
12
 
13
13
  const ensureAsyncStorage = async () => {
14
14
  const betterAuthGlobal = __getBetterAuthGlobal();
15
- if (!betterAuthGlobal.context.endpointContextAsyncStorage) {
16
- const AsyncLocalStorage = await getAsyncLocalStorage();
17
- betterAuthGlobal.context.endpointContextAsyncStorage =
18
- new AsyncLocalStorage<AuthEndpointContext>();
15
+ const existing = betterAuthGlobal.context.endpointContextAsyncStorage;
16
+ if (existing) {
17
+ return existing as AsyncLocalStorage<AuthEndpointContext>;
19
18
  }
19
+ const AsyncLocalStorage = await getAsyncLocalStorage();
20
+ betterAuthGlobal.context.endpointContextAsyncStorage ??=
21
+ new AsyncLocalStorage<AuthEndpointContext>();
20
22
  return betterAuthGlobal.context
21
23
  .endpointContextAsyncStorage as AsyncLocalStorage<AuthEndpointContext>;
22
24
  };
@@ -4,35 +4,17 @@ import { __getBetterAuthGlobal } from "./global";
4
4
 
5
5
  export type RequestStateWeakMap = WeakMap<object, any>;
6
6
 
7
- // Memoizes the in-flight AsyncLocalStorage initialization so concurrent
8
- // first-callers share a single instance instead of each constructing one.
9
- let asyncStorageInit: Promise<AsyncLocalStorage<RequestStateWeakMap>> | null =
10
- null;
11
-
12
7
  const ensureAsyncStorage = async () => {
13
8
  const betterAuthGlobal = __getBetterAuthGlobal();
14
9
  const existing = betterAuthGlobal.context.requestStateAsyncStorage;
15
10
  if (existing) {
16
11
  return existing as AsyncLocalStorage<RequestStateWeakMap>;
17
12
  }
18
- // Without memoizing the init, several concurrent callers can each pass the
19
- // check above, each `await getAsyncLocalStorage()`, and each assign a fresh
20
- // instance — last write wins. `runWithRequestState().run()` then executes on
21
- // one instance while a nested `getCurrentRequestState()` reads the
22
- // overwritten one, throwing "No request state found". This shows up
23
- // intermittently on serverless cold start (e.g. Cloudflare Workers), where
24
- // the first requests hit before the lazy `node:async_hooks` import settles.
25
- // The idempotent `??=` keeps the global singleton stable even if multiple
26
- // BetterAuth copies (Dual Module Hazard) race here.
27
- if (!asyncStorageInit) {
28
- asyncStorageInit = getAsyncLocalStorage().then((AsyncLocalStorage) => {
29
- betterAuthGlobal.context.requestStateAsyncStorage ??=
30
- new AsyncLocalStorage<RequestStateWeakMap>();
31
- return betterAuthGlobal.context
32
- .requestStateAsyncStorage as AsyncLocalStorage<RequestStateWeakMap>;
33
- });
34
- }
35
- return asyncStorageInit;
13
+ const AsyncLocalStorage = await getAsyncLocalStorage();
14
+ betterAuthGlobal.context.requestStateAsyncStorage ??=
15
+ new AsyncLocalStorage<RequestStateWeakMap>();
16
+ return betterAuthGlobal.context
17
+ .requestStateAsyncStorage as AsyncLocalStorage<RequestStateWeakMap>;
36
18
  };
37
19
 
38
20
  export async function getRequestStateAsyncLocalStorage() {
@@ -1,4 +1,4 @@
1
- import type { AsyncLocalStorage } from "node:async_hooks";
1
+ 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
  import type { BetterAuthOptions } from "../types";
@@ -14,10 +14,12 @@ type HookContext = {
14
14
 
15
15
  const ensureAsyncStorage = async () => {
16
16
  const betterAuthGlobal = __getBetterAuthGlobal();
17
- if (!betterAuthGlobal.context.adapterAsyncStorage) {
18
- const AsyncLocalStorage = await getAsyncLocalStorage();
19
- betterAuthGlobal.context.adapterAsyncStorage = new AsyncLocalStorage();
17
+ const existing = betterAuthGlobal.context.adapterAsyncStorage;
18
+ if (existing) {
19
+ return existing as AsyncLocalStorage<HookContext>;
20
20
  }
21
+ const AsyncLocalStorage = await getAsyncLocalStorage();
22
+ betterAuthGlobal.context.adapterAsyncStorage ??= new AsyncLocalStorage();
21
23
  return betterAuthGlobal.context
22
24
  .adapterAsyncStorage as AsyncLocalStorage<HookContext>;
23
25
  };