mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-23 15:42:09 -05:00
feat: code
This commit is contained in:
@@ -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<string, any> & EndpointContext<string, any>
|
||||
@@ -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 =
|
||||
|
||||
@@ -20,7 +20,9 @@ interface BetterAuthGlobal {
|
||||
const symbol = Symbol.for("better-auth:global");
|
||||
let bind: BetterAuthGlobal | null = null;
|
||||
|
||||
const context: Record<string, AsyncLocalStorage<unknown>> = {};
|
||||
const __context: Record<string, AsyncLocalStorage<unknown>> = {};
|
||||
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<string, AsyncLocalStorage<unknown>> = {};
|
||||
* 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;
|
||||
}
|
||||
|
||||
@@ -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<object, any>;
|
||||
|
||||
const ensureAsyncStorage = async () => {
|
||||
const betterAuthGlobal = getBetterAuthGlobal();
|
||||
const betterAuthGlobal = __getBetterAuthGlobal();
|
||||
if (!betterAuthGlobal.context.requestStateAsyncStorage) {
|
||||
const AsyncLocalStorage = await getAsyncLocalStorage();
|
||||
betterAuthGlobal.context.requestStateAsyncStorage =
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user