From c75fd2afd35bf68356648b8cf7c2627c8fbc7c87 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Wed, 14 Jan 2026 10:48:39 -0800 Subject: [PATCH] feat: code --- packages/core/src/context/endpoint-context.ts | 4 +-- packages/core/src/context/global.ts | 28 ++++++++++--------- packages/core/src/context/request-state.ts | 4 +-- packages/core/src/context/transaction.ts | 4 +-- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/core/src/context/endpoint-context.ts b/packages/core/src/context/endpoint-context.ts index 9be7b4dcb8..1c1ba1b3af 100644 --- a/packages/core/src/context/endpoint-context.ts +++ b/packages/core/src/context/endpoint-context.ts @@ -2,7 +2,7 @@ import type { AsyncLocalStorage } from "@better-auth/core/async_hooks"; import { getAsyncLocalStorage } from "@better-auth/core/async_hooks"; import type { EndpointContext, InputContext } from "better-call"; import type { AuthContext } from "../types"; -import { getBetterAuthGlobal } from "./global"; +import { __getBetterAuthGlobal } from "./global"; export type AuthEndpointContext = Partial< InputContext & EndpointContext @@ -11,7 +11,7 @@ export type AuthEndpointContext = Partial< }; const ensureAsyncStorage = async () => { - const betterAuthGlobal = getBetterAuthGlobal(); + const betterAuthGlobal = __getBetterAuthGlobal(); if (!betterAuthGlobal.context.endpointContextAsyncStorage) { const AsyncLocalStorage = await getAsyncLocalStorage(); betterAuthGlobal.context.endpointContextAsyncStorage = diff --git a/packages/core/src/context/global.ts b/packages/core/src/context/global.ts index f19693ee95..81bbe34a5f 100644 --- a/packages/core/src/context/global.ts +++ b/packages/core/src/context/global.ts @@ -20,7 +20,9 @@ interface BetterAuthGlobal { const symbol = Symbol.for("better-auth:global"); let bind: BetterAuthGlobal | null = null; -const context: Record> = {}; +const __context: Record> = {}; +const __betterAuthVersion: string = import.meta.env + .BETTER_AUTH_VERSION as string; /** * We store context instance in the globalThis. @@ -29,27 +31,27 @@ const context: Record> = {}; * create multiple copies of BetterAuth in the same process intentionally or unintentionally. * * For example, yarn v1, Next.js, SSR, Vite... + * + * @internal */ -export function getBetterAuthGlobal(): BetterAuthGlobal { +export function __getBetterAuthGlobal(): BetterAuthGlobal { if (!(globalThis as any)[symbol]) { (globalThis as any)[symbol] = { - version: import.meta.env.BETTER_AUTH_VERSION as string, + version: __betterAuthVersion, epoch: 1, - context, + context: __context, }; bind = (globalThis as any)[symbol] as BetterAuthGlobal; - } else { - if (!bind) { - bind = (globalThis as any)[symbol] as BetterAuthGlobal; - if (bind.version !== import.meta.env.BETTER_AUTH_VERSION) { - // Different versions of BetterAuth are loaded in the same process. - bind.epoch++; - } - } + } + bind = (globalThis as any)[symbol] as BetterAuthGlobal; + if (bind.version !== __betterAuthVersion) { + bind.version = __betterAuthVersion; + // Different versions of BetterAuth are loaded in the same process. + bind.epoch++; } return (globalThis as any)[symbol] as BetterAuthGlobal; } export function getBetterAuthVersion(): string { - return getBetterAuthGlobal().version; + return __getBetterAuthGlobal().version; } diff --git a/packages/core/src/context/request-state.ts b/packages/core/src/context/request-state.ts index 35247b7bb7..c829f82c86 100644 --- a/packages/core/src/context/request-state.ts +++ b/packages/core/src/context/request-state.ts @@ -1,11 +1,11 @@ import type { AsyncLocalStorage } from "@better-auth/core/async_hooks"; import { getAsyncLocalStorage } from "@better-auth/core/async_hooks"; -import { getBetterAuthGlobal } from "./global"; +import { __getBetterAuthGlobal } from "./global"; export type RequestStateWeakMap = WeakMap; const ensureAsyncStorage = async () => { - const betterAuthGlobal = getBetterAuthGlobal(); + const betterAuthGlobal = __getBetterAuthGlobal(); if (!betterAuthGlobal.context.requestStateAsyncStorage) { const AsyncLocalStorage = await getAsyncLocalStorage(); betterAuthGlobal.context.requestStateAsyncStorage = diff --git a/packages/core/src/context/transaction.ts b/packages/core/src/context/transaction.ts index 2160b6a5a6..e5018ef630 100644 --- a/packages/core/src/context/transaction.ts +++ b/packages/core/src/context/transaction.ts @@ -1,10 +1,10 @@ import type { AsyncLocalStorage } from "node:async_hooks"; import { getAsyncLocalStorage } from "@better-auth/core/async_hooks"; import type { DBAdapter, DBTransactionAdapter } from "../db/adapter"; -import { getBetterAuthGlobal } from "./global"; +import { __getBetterAuthGlobal } from "./global"; const ensureAsyncStorage = async () => { - const betterAuthGlobal = getBetterAuthGlobal(); + const betterAuthGlobal = __getBetterAuthGlobal(); if (!betterAuthGlobal.context.adapterAsyncStorage) { const AsyncLocalStorage = await getAsyncLocalStorage(); betterAuthGlobal.context.adapterAsyncStorage = new AsyncLocalStorage();