@gadicc/loom 1.28.0 → 1.30.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.
package/esm/mod.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import "./_dnt.polyfills.js";
2
2
  export * from "./src/apiKeys/mod.js";
3
+ export * from "./src/clientData/mod.js";
3
4
  export * from "./src/files/mod.js";
4
5
  export * from "./src/jobs/mod.js";
5
6
  export * from "./src/mail/mod.js";
package/esm/mod.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC"}
package/esm/mod.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import "./_dnt.polyfills.js";
2
2
  export * from "./src/apiKeys/mod.js";
3
+ export * from "./src/clientData/mod.js";
3
4
  export * from "./src/files/mod.js";
4
5
  export * from "./src/jobs/mod.js";
5
6
  export * from "./src/mail/mod.js";
@@ -0,0 +1,89 @@
1
+ export type ClientDataIdentityStatus = "active" | "signed-out" | "unknown";
2
+ /**
3
+ * Storage-neutral identity fence for account-owned browser data.
4
+ *
5
+ * Owner IDs are opaque strings. The consuming app remains responsible for
6
+ * validating their format before calling these helpers.
7
+ */
8
+ export interface ClientDataIdentityState<TOwnerId extends string = string> {
9
+ /** Last verified owner, retained while signed out so stored rows stay attributable. */
10
+ readonly lastOwnerId: TOwnerId | null;
11
+ /** Whether the last durable identity decision activated or fenced the owner. */
12
+ readonly status: ClientDataIdentityStatus;
13
+ /** Monotonic ordering for identity writes and cross-context notifications. */
14
+ readonly revision: number;
15
+ /** Revision of the newest sign-out explicitly requested by the user. */
16
+ readonly explicitSignOutRevision: number;
17
+ }
18
+ export type ClientDataIdentitySignal<TOwnerId extends string = string> = {
19
+ readonly type: "owner-active";
20
+ readonly ownerId: TOwnerId;
21
+ readonly revision: number;
22
+ } | {
23
+ readonly type: "signed-out";
24
+ readonly explicit: boolean;
25
+ /** An immediate pre-persistence sign-out fence intentionally has no revision. */
26
+ readonly revision?: number;
27
+ };
28
+ export interface ClientDataIdentitySignalDecision {
29
+ readonly accepted: boolean;
30
+ readonly appliedRevision: number;
31
+ }
32
+ export type ClientDataSignOutTransition<TOwnerId extends string = string> = {
33
+ readonly outcome: "applied";
34
+ readonly revision: number;
35
+ readonly signal: Extract<ClientDataIdentitySignal<TOwnerId>, {
36
+ type: "signed-out";
37
+ }>;
38
+ readonly state: ClientDataIdentityState<TOwnerId>;
39
+ } | {
40
+ readonly outcome: "stale" | "unchanged";
41
+ readonly state: ClientDataIdentityState<TOwnerId>;
42
+ };
43
+ export type ClientDataActivationTransition<TOwnerId extends string = string> = {
44
+ readonly outcome: "applied";
45
+ readonly revision: number;
46
+ readonly signal: Extract<ClientDataIdentitySignal<TOwnerId>, {
47
+ type: "owner-active";
48
+ }>;
49
+ readonly state: ClientDataIdentityState<TOwnerId>;
50
+ } | {
51
+ readonly outcome: "blocked";
52
+ readonly state: ClientDataIdentityState<TOwnerId>;
53
+ };
54
+ /** Creates the durable state used before this device has verified an owner. */
55
+ export declare function createClientDataIdentityState<TOwnerId extends string = string>(): ClientDataIdentityState<TOwnerId>;
56
+ /** Returns the owner that may be reopened offline, or null while fenced. */
57
+ export declare function activeClientDataOwner<TOwnerId extends string>(state: ClientDataIdentityState<TOwnerId>): TOwnerId | null;
58
+ /**
59
+ * Applies a signed-out identity answer to a state read in the same transaction.
60
+ *
61
+ * Pass `expectedRevision` from before an asynchronous session check. A newer
62
+ * identity write then makes that delayed answer stale instead of allowing it to
63
+ * fence the newly activated owner.
64
+ */
65
+ export declare function transitionClientDataSignOut<TOwnerId extends string>(state: ClientDataIdentityState<TOwnerId>, options?: {
66
+ readonly expectedRevision?: number;
67
+ readonly explicit?: boolean;
68
+ }): ClientDataSignOutTransition<TOwnerId>;
69
+ /**
70
+ * Activates a freshly verified owner and advances the durable identity revision.
71
+ *
72
+ * `baselineRevision` only fences an explicit sign-out that happened after the
73
+ * caller began verification. It does not order competing owner verifications.
74
+ * Within one runtime, callers must abort or generation-check asynchronous
75
+ * session requests. Across tabs or workers, the persistence adapter must also
76
+ * compare the durable revision with the request's original revision, or apply
77
+ * equivalent durable ordering, before calling this transition. Otherwise an
78
+ * older owner-A response can overtake a newer owner-B activation.
79
+ */
80
+ export declare function transitionClientDataActivation<TOwnerId extends string>(state: ClientDataIdentityState<TOwnerId>, ownerId: TOwnerId, options?: {
81
+ readonly baselineRevision?: number;
82
+ }): ClientDataActivationTransition<TOwnerId>;
83
+ /**
84
+ * Orders a validated cross-context identity signal against the newest applied
85
+ * durable revision. An unrevisioned sign-out is an immediate fence and always
86
+ * applies; the storage adapter must later reconcile it with durable state.
87
+ */
88
+ export declare function acceptClientDataIdentitySignal<TOwnerId extends string>(appliedRevision: number, signal: ClientDataIdentitySignal<TOwnerId>): ClientDataIdentitySignalDecision;
89
+ //# sourceMappingURL=identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"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QAAQ,CAAC,CA6BvC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,8BAA8B,CAAC,QAAQ,SAAS,MAAM,EACpE,KAAK,EAAE,uBAAuB,CAAC,QAAQ,CAAC,EACxC,OAAO,EAAE,QAAQ,EACjB,OAAO,GAAE;IAAE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAAO,GACnD,8BAA8B,CAAC,QAAQ,CAAC,CAyB1C;AAED;;;;GAIG;AACH,wBAAgB,8BAA8B,CAAC,QAAQ,SAAS,MAAM,EACpE,eAAe,EAAE,MAAM,EACvB,MAAM,EAAE,wBAAwB,CAAC,QAAQ,CAAC,GACzC,gCAAgC,CASlC"}
@@ -0,0 +1,127 @@
1
+ function assertRevision(value, label) {
2
+ if (!Number.isSafeInteger(value) || value < 0) {
3
+ throw new TypeError(`${label} must be a non-negative safe integer.`);
4
+ }
5
+ }
6
+ function assertState(state) {
7
+ assertRevision(state.revision, "Identity revision");
8
+ assertRevision(state.explicitSignOutRevision, "Explicit sign-out revision");
9
+ if (state.explicitSignOutRevision > state.revision) {
10
+ throw new TypeError("Explicit sign-out revision cannot exceed the identity revision.");
11
+ }
12
+ if (state.status !== "active" &&
13
+ state.status !== "signed-out" &&
14
+ state.status !== "unknown") {
15
+ throw new TypeError("Identity status is invalid.");
16
+ }
17
+ if ((state.lastOwnerId !== null && typeof state.lastOwnerId !== "string") ||
18
+ (state.status === "active" && state.lastOwnerId === null)) {
19
+ throw new TypeError("Active client data must have a string owner ID.");
20
+ }
21
+ }
22
+ function nextRevision(revision) {
23
+ if (revision === Number.MAX_SAFE_INTEGER) {
24
+ throw new RangeError("Identity revision is exhausted.");
25
+ }
26
+ return revision + 1;
27
+ }
28
+ /** Creates the durable state used before this device has verified an owner. */
29
+ export function createClientDataIdentityState() {
30
+ return {
31
+ lastOwnerId: null,
32
+ status: "unknown",
33
+ revision: 0,
34
+ explicitSignOutRevision: 0,
35
+ };
36
+ }
37
+ /** Returns the owner that may be reopened offline, or null while fenced. */
38
+ export function activeClientDataOwner(state) {
39
+ assertState(state);
40
+ return state.status === "active" ? state.lastOwnerId : null;
41
+ }
42
+ /**
43
+ * Applies a signed-out identity answer to a state read in the same transaction.
44
+ *
45
+ * Pass `expectedRevision` from before an asynchronous session check. A newer
46
+ * identity write then makes that delayed answer stale instead of allowing it to
47
+ * fence the newly activated owner.
48
+ */
49
+ export function transitionClientDataSignOut(state, options = {}) {
50
+ assertState(state);
51
+ if (options.expectedRevision !== undefined) {
52
+ assertRevision(options.expectedRevision, "Expected identity revision");
53
+ if (options.expectedRevision !== state.revision) {
54
+ return { outcome: "stale", state };
55
+ }
56
+ }
57
+ const explicit = options.explicit === true;
58
+ if (state.status === "signed-out" && !explicit) {
59
+ return { outcome: "unchanged", state };
60
+ }
61
+ const revision = nextRevision(state.revision);
62
+ const next = {
63
+ ...state,
64
+ status: "signed-out",
65
+ revision,
66
+ explicitSignOutRevision: explicit
67
+ ? revision
68
+ : state.explicitSignOutRevision,
69
+ };
70
+ return {
71
+ outcome: "applied",
72
+ revision,
73
+ signal: { type: "signed-out", explicit, revision },
74
+ state: next,
75
+ };
76
+ }
77
+ /**
78
+ * Activates a freshly verified owner and advances the durable identity revision.
79
+ *
80
+ * `baselineRevision` only fences an explicit sign-out that happened after the
81
+ * caller began verification. It does not order competing owner verifications.
82
+ * Within one runtime, callers must abort or generation-check asynchronous
83
+ * session requests. Across tabs or workers, the persistence adapter must also
84
+ * compare the durable revision with the request's original revision, or apply
85
+ * equivalent durable ordering, before calling this transition. Otherwise an
86
+ * older owner-A response can overtake a newer owner-B activation.
87
+ */
88
+ export function transitionClientDataActivation(state, ownerId, options = {}) {
89
+ assertState(state);
90
+ if (typeof ownerId !== "string") {
91
+ throw new TypeError("Client-data owner ID must be a string.");
92
+ }
93
+ if (options.baselineRevision !== undefined) {
94
+ assertRevision(options.baselineRevision, "Baseline identity revision");
95
+ if (state.explicitSignOutRevision > options.baselineRevision) {
96
+ return { outcome: "blocked", state };
97
+ }
98
+ }
99
+ const revision = nextRevision(state.revision);
100
+ const next = {
101
+ ...state,
102
+ lastOwnerId: ownerId,
103
+ status: "active",
104
+ revision,
105
+ };
106
+ return {
107
+ outcome: "applied",
108
+ revision,
109
+ signal: { type: "owner-active", ownerId, revision },
110
+ state: next,
111
+ };
112
+ }
113
+ /**
114
+ * Orders a validated cross-context identity signal against the newest applied
115
+ * durable revision. An unrevisioned sign-out is an immediate fence and always
116
+ * applies; the storage adapter must later reconcile it with durable state.
117
+ */
118
+ export function acceptClientDataIdentitySignal(appliedRevision, signal) {
119
+ assertRevision(appliedRevision, "Applied identity revision");
120
+ if (signal.revision === undefined) {
121
+ return { accepted: true, appliedRevision };
122
+ }
123
+ assertRevision(signal.revision, "Signal identity revision");
124
+ return signal.revision < appliedRevision
125
+ ? { accepted: false, appliedRevision }
126
+ : { accepted: true, appliedRevision: signal.revision };
127
+ }
@@ -0,0 +1,3 @@
1
+ import "../../_dnt.polyfills.js";
2
+ export * from "./identity.js";
3
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../src/src/clientData/mod.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AACjC,cAAc,eAAe,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./identity.js";
@@ -0,0 +1,35 @@
1
+ import "../../../_dnt.polyfills.js";
2
+ /**
3
+ * Browser-side Better Auth helpers. This module imports nothing, so it adds no
4
+ * dependency and is safe to reach from a consumer's own client module. Read
5
+ * globals through `globalThis`: a bare `window` becomes a build shim that is
6
+ * always defined, and pulls Node polyfills into the browser bundle.
7
+ */
8
+ type PendingSessionState = {
9
+ isPending: boolean;
10
+ };
11
+ /**
12
+ * Wraps Better Auth's `useSession` so a session reports pending only until the
13
+ * page's first answer.
14
+ *
15
+ * Better Auth marks a session with no data as pending again for every refetch,
16
+ * which it runs on focus, on reconnect and on a cross-tab session change. For a
17
+ * signed-out visitor that turns each of those into a pending state, so views
18
+ * waiting for the first answer unmount and re-check identity, and anything
19
+ * keyed on the session runs again. After the first answer this keeps the last
20
+ * settled state, which for a pending refetch is always "no session", until the
21
+ * new answer arrives.
22
+ *
23
+ * Server renders never settle, so hydration still starts from the pending
24
+ * state the server rendered.
25
+ *
26
+ * This works around Better Auth deriving `isPending` from "has no data"
27
+ * (`isPending: current.data === null` in its session atom) rather than from
28
+ * "has had no answer yet". If a Better Auth release separates those, this
29
+ * whole module and its tests can go, and consumers can call the client's
30
+ * `useSession` directly again; the manifest's auth client module is the only
31
+ * other place to check.
32
+ */
33
+ export declare function createSettledSessionHook<TState extends PendingSessionState>(useSession: () => TState): () => TState;
34
+ export {};
35
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../src/src/next/auth/client.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,CAAC;AAGpC;;;;;GAKG;AAEH,KAAK,mBAAmB,GAAG;IACzB,SAAS,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,SAAS,mBAAmB,EACzE,UAAU,EAAE,MAAM,MAAM,GACvB,MAAM,MAAM,CAWd"}
@@ -0,0 +1,35 @@
1
+ "use client";
2
+ /**
3
+ * Wraps Better Auth's `useSession` so a session reports pending only until the
4
+ * page's first answer.
5
+ *
6
+ * Better Auth marks a session with no data as pending again for every refetch,
7
+ * which it runs on focus, on reconnect and on a cross-tab session change. For a
8
+ * signed-out visitor that turns each of those into a pending state, so views
9
+ * waiting for the first answer unmount and re-check identity, and anything
10
+ * keyed on the session runs again. After the first answer this keeps the last
11
+ * settled state, which for a pending refetch is always "no session", until the
12
+ * new answer arrives.
13
+ *
14
+ * Server renders never settle, so hydration still starts from the pending
15
+ * state the server rendered.
16
+ *
17
+ * This works around Better Auth deriving `isPending` from "has no data"
18
+ * (`isPending: current.data === null` in its session atom) rather than from
19
+ * "has had no answer yet". If a Better Auth release separates those, this
20
+ * whole module and its tests can go, and consumers can call the client's
21
+ * `useSession` directly again; the manifest's auth client module is the only
22
+ * other place to check.
23
+ */
24
+ export function createSettledSessionHook(useSession) {
25
+ let settled = false;
26
+ return function useSettledSession() {
27
+ const session = useSession();
28
+ if (!session.isPending) {
29
+ if (typeof globalThis.document !== "undefined")
30
+ settled = true;
31
+ return session;
32
+ }
33
+ return settled ? { ...session, isPending: false } : session;
34
+ };
35
+ }
@@ -1,5 +1,6 @@
1
1
  type SessionQuery = {
2
2
  disableCookieCache?: boolean;
3
+ disableRefresh?: boolean;
3
4
  };
4
5
  type GetSessionArgs<THeaders> = {
5
6
  headers: THeaders;
@@ -20,9 +21,24 @@ export type CreateRequestAuthSessionReadersOptions<THeaders, TAuth extends Bette
20
21
  getHeaders: () => THeaders | Promise<THeaders>;
21
22
  profileEnvVar?: string;
22
23
  };
24
+ export type SessionReadOptions = {
25
+ disableCookieCache?: boolean;
26
+ /** Reads the session without extending it, for a check that only decides. */
27
+ disableRefresh?: boolean;
28
+ };
23
29
  export declare function createRequestAuthSessionReaders<THeaders, TAuth extends BetterAuthLike<THeaders, unknown>>(options: CreateRequestAuthSessionReadersOptions<THeaders, TAuth>): {
24
30
  getFreshSession: any;
25
31
  getSession: any;
32
+ /**
33
+ * Reads the session again on every call, unlike the cached readers above.
34
+ * An authorization boundary that re-checks after awaited work needs this:
35
+ * a cached read can still answer from before a revocation. Pass
36
+ * `disableRefresh` for a check that only decides, so deciding does not
37
+ * extend the session.
38
+ */
39
+ readSession: (readOptions?: SessionReadOptions) => Promise<InferAuthSession<TAuth, THeaders>>;
40
+ /** `readSession` with the cookie cache bypassed, and never cached. */
41
+ readFreshSession: (readOptions?: SessionReadOptions) => Promise<InferAuthSession<TAuth, THeaders>>;
26
42
  };
27
43
  export {};
28
44
  //# sourceMappingURL=server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../../src/src/next/auth/server.ts"],"names":[],"mappings":"AAEA,KAAK,YAAY,GAAG;IAClB,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAEF,KAAK,cAAc,CAAC,QAAQ,IAAI;IAC9B,OAAO,EAAE,QAAQ,CAAC;IAClB,KAAK,CAAC,EAAE,YAAY,GAAG,SAAS,CAAC;CAClC,CAAC;AAEF,KAAK,cAAc,CAAC,QAAQ,EAAE,QAAQ,IAAI;IACxC,MAAM,EAAE;QACN,OAAO,EAAE,QAAQ,CAAC;KACnB,CAAC;IACF,GAAG,EAAE;QACH,UAAU,CAAC,IAAI,EAAE,cAAc,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;KACtE,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,gBAAgB,CAC1B,KAAK,SAAS,cAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,EAC/C,QAAQ,IACN,KAAK,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC;AAEtC,MAAM,MAAM,sCAAsC,CAChD,QAAQ,EACR,KAAK,SAAS,cAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,IAC7C;IACF,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IACzC,UAAU,EAAE,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC/C,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAMF,wBAAgB,+BAA+B,CAC7C,QAAQ,EACR,KAAK,SAAS,cAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,EAE/C,OAAO,EAAE,sCAAsC,CAAC,QAAQ,EAAE,KAAK,CAAC;;;EAoCjE"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../../src/src/next/auth/server.ts"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EAE/C,OAAO,EAAE,sCAAsC,CAAC,QAAQ,EAAE,KAAK,CAAC;;;IAqC9D;;;;;;OAMG;gCAtCU,kBAAkB,KAC9B,OAAO,CAAC,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAuC3C,sEAAsE;qCACtC,kBAAkB;EAGrD"}
@@ -4,11 +4,13 @@ export function createRequestAuthSessionReaders(options) {
4
4
  async function readSession(readOptions = {}) {
5
5
  const startedAt = Date.now();
6
6
  const requestHeaders = await options.getHeaders();
7
+ const query = {
8
+ ...(readOptions.disableCookieCache ? { disableCookieCache: true } : {}),
9
+ ...(readOptions.disableRefresh ? { disableRefresh: true } : {}),
10
+ };
7
11
  const session = await options.auth.api.getSession({
8
12
  headers: requestHeaders,
9
- query: readOptions.disableCookieCache
10
- ? { disableCookieCache: true }
11
- : undefined,
13
+ query: Object.keys(query).length ? query : undefined,
12
14
  });
13
15
  if (options.env?.[profileEnvVar] === "1") {
14
16
  console.info(`[perf:authSession] disableCookieCache=${readOptions.disableCookieCache ? 1 : 0} total=${Date.now() - startedAt}ms`);
@@ -20,5 +22,15 @@ export function createRequestAuthSessionReaders(options) {
20
22
  return {
21
23
  getFreshSession,
22
24
  getSession,
25
+ /**
26
+ * Reads the session again on every call, unlike the cached readers above.
27
+ * An authorization boundary that re-checks after awaited work needs this:
28
+ * a cached read can still answer from before a revocation. Pass
29
+ * `disableRefresh` for a check that only decides, so deciding does not
30
+ * extend the session.
31
+ */
32
+ readSession,
33
+ /** `readSession` with the cookie cache bypassed, and never cached. */
34
+ readFreshSession: (readOptions = {}) => readSession({ ...readOptions, disableCookieCache: true }),
23
35
  };
24
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gadicc/loom",
3
- "version": "1.28.0",
3
+ "version": "1.30.0",
4
4
  "description": "Deno-first shared helpers for extracting common Next.js app code.",
5
5
  "homepage": "https://github.com/gadicc/loom#readme",
6
6
  "repository": {
@@ -27,6 +27,10 @@
27
27
  "import": "./esm/src/apiKeys/mod.js",
28
28
  "require": "./script/src/apiKeys/mod.js"
29
29
  },
30
+ "./src/clientData/mod.js": {
31
+ "import": "./esm/src/clientData/mod.js",
32
+ "require": "./script/src/clientData/mod.js"
33
+ },
30
34
  "./src/files/mod.js": {
31
35
  "import": "./esm/src/files/mod.js",
32
36
  "require": "./script/src/files/mod.js"
@@ -55,6 +59,10 @@
55
59
  "import": "./esm/src/next/apiKeys/mod.js",
56
60
  "require": "./script/src/next/apiKeys/mod.js"
57
61
  },
62
+ "./src/next/auth/client.js": {
63
+ "import": "./esm/src/next/auth/client.js",
64
+ "require": "./script/src/next/auth/client.js"
65
+ },
58
66
  "./src/next/files/mod.js": {
59
67
  "import": "./esm/src/next/files/mod.js",
60
68
  "require": "./script/src/next/files/mod.js"
@@ -71,6 +79,11 @@
71
79
  "import": "./esm/src/next/pwa/worker.js",
72
80
  "require": "./script/src/next/pwa/worker.js"
73
81
  },
82
+ "./client-data": {
83
+ "types": "./esm/src/clientData/mod.d.ts",
84
+ "import": "./esm/src/clientData/mod.js",
85
+ "require": "./script/src/clientData/mod.js"
86
+ },
74
87
  "./db/drizzle-config": {
75
88
  "types": "./db/drizzle-config.d.ts",
76
89
  "import": "./db/drizzle-config.js",
@@ -231,7 +244,7 @@
231
244
  },
232
245
  "scripts": {},
233
246
  "loom": {
234
- "gitCommit": "e381c057d5eb321d4982119984d98ec292d79c41"
247
+ "gitCommit": "043c9f47694f82164138ecf0960e367a7aec437d"
235
248
  },
236
249
  "bin": {
237
250
  "loom": "script/bin/loom.js"
@@ -244,6 +257,9 @@
244
257
  "apiKeys": [
245
258
  "esm/src/apiKeys/mod.d.ts"
246
259
  ],
260
+ "client-data": [
261
+ "esm/src/clientData/mod.d.ts"
262
+ ],
247
263
  "files": [
248
264
  "esm/src/files/mod.d.ts"
249
265
  ],
package/script/mod.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import "./_dnt.polyfills.js";
2
2
  export * from "./src/apiKeys/mod.js";
3
+ export * from "./src/clientData/mod.js";
3
4
  export * from "./src/files/mod.js";
4
5
  export * from "./src/jobs/mod.js";
5
6
  export * from "./src/mail/mod.js";
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC"}
package/script/mod.js CHANGED
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  require("./_dnt.polyfills.js");
18
18
  __exportStar(require("./src/apiKeys/mod.js"), exports);
19
+ __exportStar(require("./src/clientData/mod.js"), exports);
19
20
  __exportStar(require("./src/files/mod.js"), exports);
20
21
  __exportStar(require("./src/jobs/mod.js"), exports);
21
22
  __exportStar(require("./src/mail/mod.js"), exports);
@@ -0,0 +1,89 @@
1
+ export type ClientDataIdentityStatus = "active" | "signed-out" | "unknown";
2
+ /**
3
+ * Storage-neutral identity fence for account-owned browser data.
4
+ *
5
+ * Owner IDs are opaque strings. The consuming app remains responsible for
6
+ * validating their format before calling these helpers.
7
+ */
8
+ export interface ClientDataIdentityState<TOwnerId extends string = string> {
9
+ /** Last verified owner, retained while signed out so stored rows stay attributable. */
10
+ readonly lastOwnerId: TOwnerId | null;
11
+ /** Whether the last durable identity decision activated or fenced the owner. */
12
+ readonly status: ClientDataIdentityStatus;
13
+ /** Monotonic ordering for identity writes and cross-context notifications. */
14
+ readonly revision: number;
15
+ /** Revision of the newest sign-out explicitly requested by the user. */
16
+ readonly explicitSignOutRevision: number;
17
+ }
18
+ export type ClientDataIdentitySignal<TOwnerId extends string = string> = {
19
+ readonly type: "owner-active";
20
+ readonly ownerId: TOwnerId;
21
+ readonly revision: number;
22
+ } | {
23
+ readonly type: "signed-out";
24
+ readonly explicit: boolean;
25
+ /** An immediate pre-persistence sign-out fence intentionally has no revision. */
26
+ readonly revision?: number;
27
+ };
28
+ export interface ClientDataIdentitySignalDecision {
29
+ readonly accepted: boolean;
30
+ readonly appliedRevision: number;
31
+ }
32
+ export type ClientDataSignOutTransition<TOwnerId extends string = string> = {
33
+ readonly outcome: "applied";
34
+ readonly revision: number;
35
+ readonly signal: Extract<ClientDataIdentitySignal<TOwnerId>, {
36
+ type: "signed-out";
37
+ }>;
38
+ readonly state: ClientDataIdentityState<TOwnerId>;
39
+ } | {
40
+ readonly outcome: "stale" | "unchanged";
41
+ readonly state: ClientDataIdentityState<TOwnerId>;
42
+ };
43
+ export type ClientDataActivationTransition<TOwnerId extends string = string> = {
44
+ readonly outcome: "applied";
45
+ readonly revision: number;
46
+ readonly signal: Extract<ClientDataIdentitySignal<TOwnerId>, {
47
+ type: "owner-active";
48
+ }>;
49
+ readonly state: ClientDataIdentityState<TOwnerId>;
50
+ } | {
51
+ readonly outcome: "blocked";
52
+ readonly state: ClientDataIdentityState<TOwnerId>;
53
+ };
54
+ /** Creates the durable state used before this device has verified an owner. */
55
+ export declare function createClientDataIdentityState<TOwnerId extends string = string>(): ClientDataIdentityState<TOwnerId>;
56
+ /** Returns the owner that may be reopened offline, or null while fenced. */
57
+ export declare function activeClientDataOwner<TOwnerId extends string>(state: ClientDataIdentityState<TOwnerId>): TOwnerId | null;
58
+ /**
59
+ * Applies a signed-out identity answer to a state read in the same transaction.
60
+ *
61
+ * Pass `expectedRevision` from before an asynchronous session check. A newer
62
+ * identity write then makes that delayed answer stale instead of allowing it to
63
+ * fence the newly activated owner.
64
+ */
65
+ export declare function transitionClientDataSignOut<TOwnerId extends string>(state: ClientDataIdentityState<TOwnerId>, options?: {
66
+ readonly expectedRevision?: number;
67
+ readonly explicit?: boolean;
68
+ }): ClientDataSignOutTransition<TOwnerId>;
69
+ /**
70
+ * Activates a freshly verified owner and advances the durable identity revision.
71
+ *
72
+ * `baselineRevision` only fences an explicit sign-out that happened after the
73
+ * caller began verification. It does not order competing owner verifications.
74
+ * Within one runtime, callers must abort or generation-check asynchronous
75
+ * session requests. Across tabs or workers, the persistence adapter must also
76
+ * compare the durable revision with the request's original revision, or apply
77
+ * equivalent durable ordering, before calling this transition. Otherwise an
78
+ * older owner-A response can overtake a newer owner-B activation.
79
+ */
80
+ export declare function transitionClientDataActivation<TOwnerId extends string>(state: ClientDataIdentityState<TOwnerId>, ownerId: TOwnerId, options?: {
81
+ readonly baselineRevision?: number;
82
+ }): ClientDataActivationTransition<TOwnerId>;
83
+ /**
84
+ * Orders a validated cross-context identity signal against the newest applied
85
+ * durable revision. An unrevisioned sign-out is an immediate fence and always
86
+ * applies; the storage adapter must later reconcile it with durable state.
87
+ */
88
+ export declare function acceptClientDataIdentitySignal<TOwnerId extends string>(appliedRevision: number, signal: ClientDataIdentitySignal<TOwnerId>): ClientDataIdentitySignalDecision;
89
+ //# sourceMappingURL=identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../../src/src/clientData/identity.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,wBAAwB,GAAG,QAAQ,GAA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AAE,uBAAuB,CAAC,QAAQ,CAAC,EACxC,OAAO,EAAE,QAAQ,EACjB,OAAO,GAAE;IAAE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAAO,GACnD,8BAA8B,CAAC,QAAQ,CAAC,CAyB1C;AAED;;;;GAIG;AACH,wBAAgB,8BAA8B,CAAC,QAAQ,SAAS,MAAM,EACpE,eAAe,EAAE,MAAM,EACvB,MAAM,EAAE,wBAAwB,CAAC,QAAQ,CAAC,GACzC,gCAAgC,CASlC"}
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createClientDataIdentityState = createClientDataIdentityState;
4
+ exports.activeClientDataOwner = activeClientDataOwner;
5
+ exports.transitionClientDataSignOut = transitionClientDataSignOut;
6
+ exports.transitionClientDataActivation = transitionClientDataActivation;
7
+ exports.acceptClientDataIdentitySignal = acceptClientDataIdentitySignal;
8
+ function assertRevision(value, label) {
9
+ if (!Number.isSafeInteger(value) || value < 0) {
10
+ throw new TypeError(`${label} must be a non-negative safe integer.`);
11
+ }
12
+ }
13
+ function assertState(state) {
14
+ assertRevision(state.revision, "Identity revision");
15
+ assertRevision(state.explicitSignOutRevision, "Explicit sign-out revision");
16
+ if (state.explicitSignOutRevision > state.revision) {
17
+ throw new TypeError("Explicit sign-out revision cannot exceed the identity revision.");
18
+ }
19
+ if (state.status !== "active" &&
20
+ state.status !== "signed-out" &&
21
+ state.status !== "unknown") {
22
+ throw new TypeError("Identity status is invalid.");
23
+ }
24
+ if ((state.lastOwnerId !== null && typeof state.lastOwnerId !== "string") ||
25
+ (state.status === "active" && state.lastOwnerId === null)) {
26
+ throw new TypeError("Active client data must have a string owner ID.");
27
+ }
28
+ }
29
+ function nextRevision(revision) {
30
+ if (revision === Number.MAX_SAFE_INTEGER) {
31
+ throw new RangeError("Identity revision is exhausted.");
32
+ }
33
+ return revision + 1;
34
+ }
35
+ /** Creates the durable state used before this device has verified an owner. */
36
+ function createClientDataIdentityState() {
37
+ return {
38
+ lastOwnerId: null,
39
+ status: "unknown",
40
+ revision: 0,
41
+ explicitSignOutRevision: 0,
42
+ };
43
+ }
44
+ /** Returns the owner that may be reopened offline, or null while fenced. */
45
+ function activeClientDataOwner(state) {
46
+ assertState(state);
47
+ return state.status === "active" ? state.lastOwnerId : null;
48
+ }
49
+ /**
50
+ * Applies a signed-out identity answer to a state read in the same transaction.
51
+ *
52
+ * Pass `expectedRevision` from before an asynchronous session check. A newer
53
+ * identity write then makes that delayed answer stale instead of allowing it to
54
+ * fence the newly activated owner.
55
+ */
56
+ function transitionClientDataSignOut(state, options = {}) {
57
+ assertState(state);
58
+ if (options.expectedRevision !== undefined) {
59
+ assertRevision(options.expectedRevision, "Expected identity revision");
60
+ if (options.expectedRevision !== state.revision) {
61
+ return { outcome: "stale", state };
62
+ }
63
+ }
64
+ const explicit = options.explicit === true;
65
+ if (state.status === "signed-out" && !explicit) {
66
+ return { outcome: "unchanged", state };
67
+ }
68
+ const revision = nextRevision(state.revision);
69
+ const next = {
70
+ ...state,
71
+ status: "signed-out",
72
+ revision,
73
+ explicitSignOutRevision: explicit
74
+ ? revision
75
+ : state.explicitSignOutRevision,
76
+ };
77
+ return {
78
+ outcome: "applied",
79
+ revision,
80
+ signal: { type: "signed-out", explicit, revision },
81
+ state: next,
82
+ };
83
+ }
84
+ /**
85
+ * Activates a freshly verified owner and advances the durable identity revision.
86
+ *
87
+ * `baselineRevision` only fences an explicit sign-out that happened after the
88
+ * caller began verification. It does not order competing owner verifications.
89
+ * Within one runtime, callers must abort or generation-check asynchronous
90
+ * session requests. Across tabs or workers, the persistence adapter must also
91
+ * compare the durable revision with the request's original revision, or apply
92
+ * equivalent durable ordering, before calling this transition. Otherwise an
93
+ * older owner-A response can overtake a newer owner-B activation.
94
+ */
95
+ function transitionClientDataActivation(state, ownerId, options = {}) {
96
+ assertState(state);
97
+ if (typeof ownerId !== "string") {
98
+ throw new TypeError("Client-data owner ID must be a string.");
99
+ }
100
+ if (options.baselineRevision !== undefined) {
101
+ assertRevision(options.baselineRevision, "Baseline identity revision");
102
+ if (state.explicitSignOutRevision > options.baselineRevision) {
103
+ return { outcome: "blocked", state };
104
+ }
105
+ }
106
+ const revision = nextRevision(state.revision);
107
+ const next = {
108
+ ...state,
109
+ lastOwnerId: ownerId,
110
+ status: "active",
111
+ revision,
112
+ };
113
+ return {
114
+ outcome: "applied",
115
+ revision,
116
+ signal: { type: "owner-active", ownerId, revision },
117
+ state: next,
118
+ };
119
+ }
120
+ /**
121
+ * Orders a validated cross-context identity signal against the newest applied
122
+ * durable revision. An unrevisioned sign-out is an immediate fence and always
123
+ * applies; the storage adapter must later reconcile it with durable state.
124
+ */
125
+ function acceptClientDataIdentitySignal(appliedRevision, signal) {
126
+ assertRevision(appliedRevision, "Applied identity revision");
127
+ if (signal.revision === undefined) {
128
+ return { accepted: true, appliedRevision };
129
+ }
130
+ assertRevision(signal.revision, "Signal identity revision");
131
+ return signal.revision < appliedRevision
132
+ ? { accepted: false, appliedRevision }
133
+ : { accepted: true, appliedRevision: signal.revision };
134
+ }
@@ -0,0 +1,3 @@
1
+ import "../../_dnt.polyfills.js";
2
+ export * from "./identity.js";
3
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../src/src/clientData/mod.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AACjC,cAAc,eAAe,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ require("../../_dnt.polyfills.js");
18
+ __exportStar(require("./identity.js"), exports);
@@ -0,0 +1,35 @@
1
+ import "../../../_dnt.polyfills.js";
2
+ /**
3
+ * Browser-side Better Auth helpers. This module imports nothing, so it adds no
4
+ * dependency and is safe to reach from a consumer's own client module. Read
5
+ * globals through `globalThis`: a bare `window` becomes a build shim that is
6
+ * always defined, and pulls Node polyfills into the browser bundle.
7
+ */
8
+ type PendingSessionState = {
9
+ isPending: boolean;
10
+ };
11
+ /**
12
+ * Wraps Better Auth's `useSession` so a session reports pending only until the
13
+ * page's first answer.
14
+ *
15
+ * Better Auth marks a session with no data as pending again for every refetch,
16
+ * which it runs on focus, on reconnect and on a cross-tab session change. For a
17
+ * signed-out visitor that turns each of those into a pending state, so views
18
+ * waiting for the first answer unmount and re-check identity, and anything
19
+ * keyed on the session runs again. After the first answer this keeps the last
20
+ * settled state, which for a pending refetch is always "no session", until the
21
+ * new answer arrives.
22
+ *
23
+ * Server renders never settle, so hydration still starts from the pending
24
+ * state the server rendered.
25
+ *
26
+ * This works around Better Auth deriving `isPending` from "has no data"
27
+ * (`isPending: current.data === null` in its session atom) rather than from
28
+ * "has had no answer yet". If a Better Auth release separates those, this
29
+ * whole module and its tests can go, and consumers can call the client's
30
+ * `useSession` directly again; the manifest's auth client module is the only
31
+ * other place to check.
32
+ */
33
+ export declare function createSettledSessionHook<TState extends PendingSessionState>(useSession: () => TState): () => TState;
34
+ export {};
35
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../src/src/next/auth/client.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,CAAC;AAGpC;;;;;GAKG;AAEH,KAAK,mBAAmB,GAAG;IACzB,SAAS,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,SAAS,mBAAmB,EACzE,UAAU,EAAE,MAAM,MAAM,GACvB,MAAM,MAAM,CAWd"}
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSettledSessionHook = createSettledSessionHook;
4
+ require("../../../_dnt.polyfills.js");
5
+ "use client";
6
+ /**
7
+ * Wraps Better Auth's `useSession` so a session reports pending only until the
8
+ * page's first answer.
9
+ *
10
+ * Better Auth marks a session with no data as pending again for every refetch,
11
+ * which it runs on focus, on reconnect and on a cross-tab session change. For a
12
+ * signed-out visitor that turns each of those into a pending state, so views
13
+ * waiting for the first answer unmount and re-check identity, and anything
14
+ * keyed on the session runs again. After the first answer this keeps the last
15
+ * settled state, which for a pending refetch is always "no session", until the
16
+ * new answer arrives.
17
+ *
18
+ * Server renders never settle, so hydration still starts from the pending
19
+ * state the server rendered.
20
+ *
21
+ * This works around Better Auth deriving `isPending` from "has no data"
22
+ * (`isPending: current.data === null` in its session atom) rather than from
23
+ * "has had no answer yet". If a Better Auth release separates those, this
24
+ * whole module and its tests can go, and consumers can call the client's
25
+ * `useSession` directly again; the manifest's auth client module is the only
26
+ * other place to check.
27
+ */
28
+ function createSettledSessionHook(useSession) {
29
+ let settled = false;
30
+ return function useSettledSession() {
31
+ const session = useSession();
32
+ if (!session.isPending) {
33
+ if (typeof globalThis.document !== "undefined")
34
+ settled = true;
35
+ return session;
36
+ }
37
+ return settled ? { ...session, isPending: false } : session;
38
+ };
39
+ }
@@ -1,5 +1,6 @@
1
1
  type SessionQuery = {
2
2
  disableCookieCache?: boolean;
3
+ disableRefresh?: boolean;
3
4
  };
4
5
  type GetSessionArgs<THeaders> = {
5
6
  headers: THeaders;
@@ -20,9 +21,24 @@ export type CreateRequestAuthSessionReadersOptions<THeaders, TAuth extends Bette
20
21
  getHeaders: () => THeaders | Promise<THeaders>;
21
22
  profileEnvVar?: string;
22
23
  };
24
+ export type SessionReadOptions = {
25
+ disableCookieCache?: boolean;
26
+ /** Reads the session without extending it, for a check that only decides. */
27
+ disableRefresh?: boolean;
28
+ };
23
29
  export declare function createRequestAuthSessionReaders<THeaders, TAuth extends BetterAuthLike<THeaders, unknown>>(options: CreateRequestAuthSessionReadersOptions<THeaders, TAuth>): {
24
30
  getFreshSession: any;
25
31
  getSession: any;
32
+ /**
33
+ * Reads the session again on every call, unlike the cached readers above.
34
+ * An authorization boundary that re-checks after awaited work needs this:
35
+ * a cached read can still answer from before a revocation. Pass
36
+ * `disableRefresh` for a check that only decides, so deciding does not
37
+ * extend the session.
38
+ */
39
+ readSession: (readOptions?: SessionReadOptions) => Promise<InferAuthSession<TAuth, THeaders>>;
40
+ /** `readSession` with the cookie cache bypassed, and never cached. */
41
+ readFreshSession: (readOptions?: SessionReadOptions) => Promise<InferAuthSession<TAuth, THeaders>>;
26
42
  };
27
43
  export {};
28
44
  //# sourceMappingURL=server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../../src/src/next/auth/server.ts"],"names":[],"mappings":"AAEA,KAAK,YAAY,GAAG;IAClB,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAEF,KAAK,cAAc,CAAC,QAAQ,IAAI;IAC9B,OAAO,EAAE,QAAQ,CAAC;IAClB,KAAK,CAAC,EAAE,YAAY,GAAG,SAAS,CAAC;CAClC,CAAC;AAEF,KAAK,cAAc,CAAC,QAAQ,EAAE,QAAQ,IAAI;IACxC,MAAM,EAAE;QACN,OAAO,EAAE,QAAQ,CAAC;KACnB,CAAC;IACF,GAAG,EAAE;QACH,UAAU,CAAC,IAAI,EAAE,cAAc,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAA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ACtE,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,gBAAgB,CAC1B,KAAK,SAAS,cAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,EAC/C,QAAQ,IACN,KAAK,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC;AAEtC,MAAM,MAAM,sCAAsC,CAChD,QAAQ,EACR,KAAK,SAAS,cAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,IAC7C;IACF,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IACzC,UAAU,EAAE,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC/C,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,6EAA6E;IAC7E,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;AAEF,wBAAgB,+BAA+B,CAC7C,QAAQ,EACR,KAAK,SAAS,cAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,EAE/C,OAAO,EAAE,sCAAsC,CAAC,QAAQ,EAAE,KAAK,CAAC;;;IAqC9D;;;;;;OAMG;gCAtCU,kBAAkB,KAC9B,OAAO,CAAC,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAuC3C,sEAAsE;qCACtC,kBAAkB;EAGrD"}
@@ -7,11 +7,13 @@ function createRequestAuthSessionReaders(options) {
7
7
  async function readSession(readOptions = {}) {
8
8
  const startedAt = Date.now();
9
9
  const requestHeaders = await options.getHeaders();
10
+ const query = {
11
+ ...(readOptions.disableCookieCache ? { disableCookieCache: true } : {}),
12
+ ...(readOptions.disableRefresh ? { disableRefresh: true } : {}),
13
+ };
10
14
  const session = await options.auth.api.getSession({
11
15
  headers: requestHeaders,
12
- query: readOptions.disableCookieCache
13
- ? { disableCookieCache: true }
14
- : undefined,
16
+ query: Object.keys(query).length ? query : undefined,
15
17
  });
16
18
  if (options.env?.[profileEnvVar] === "1") {
17
19
  console.info(`[perf:authSession] disableCookieCache=${readOptions.disableCookieCache ? 1 : 0} total=${Date.now() - startedAt}ms`);
@@ -23,5 +25,15 @@ function createRequestAuthSessionReaders(options) {
23
25
  return {
24
26
  getFreshSession,
25
27
  getSession,
28
+ /**
29
+ * Reads the session again on every call, unlike the cached readers above.
30
+ * An authorization boundary that re-checks after awaited work needs this:
31
+ * a cached read can still answer from before a revocation. Pass
32
+ * `disableRefresh` for a check that only decides, so deciding does not
33
+ * extend the session.
34
+ */
35
+ readSession,
36
+ /** `readSession` with the cookie cache bypassed, and never cached. */
37
+ readFreshSession: (readOptions = {}) => readSession({ ...readOptions, disableCookieCache: true }),
26
38
  };
27
39
  }
@@ -117,12 +117,45 @@ exported `GET` and `POST`.
117
117
 
118
118
  ## Request-scoped sessions
119
119
 
120
- `createRequestAuthSessionReaders` returns request-cached readers, so within one
121
- request `getFreshSession` reads the cookie once. That suits page and layout
122
- reads. An authorization boundary that must re-check after awaited work needs an
123
- uncached read; a cached one can answer from before a revocation. Keep such a
124
- reader app-owned, validate the session where you read it, and record
125
- `requestSessionReaders: "app-owned"` so the check expects it.
120
+ `createRequestAuthSessionReaders` returns four readers. `getSession` and
121
+ `getFreshSession` are request-cached, so within one request each reads the
122
+ cookie once, which suits page and layout reads. `readSession` and
123
+ `readFreshSession` read again on every call.
124
+
125
+ An authorization boundary that re-checks after awaited work needs an uncached
126
+ read: a cached one can still answer from before a revocation. Use
127
+ `readFreshSession` there, and pass `disableRefresh` for a check that only
128
+ decides, so deciding does not extend the session. Validate identity and expiry
129
+ where you read.
130
+
131
+ An app may still own that reader, for example when it takes explicit headers so
132
+ tests and scripts can call it outside a request. Record
133
+ `requestSessionReaders: "app-owned"` then, so the check expects it.
134
+
135
+ ## Browser session state
136
+
137
+ Better Auth marks a session with no data as pending again for every refetch, and
138
+ it refetches on focus, on reconnect and on a cross-tab change. For a signed-out
139
+ visitor each of those becomes a pending state, so views waiting for the first
140
+ answer unmount and re-check identity. Wrap the client's hook once:
141
+
142
+ ```ts
143
+ "use client";
144
+
145
+ import { createSettledSessionHook } from "@gadicc/loom/next/auth/client";
146
+
147
+ export const authClient = createAuthClient({/* app plugins */});
148
+ export const useSession = createSettledSessionHook(authClient.useSession);
149
+ ```
150
+
151
+ The wrapper reports pending only until the page's first answer, then keeps the
152
+ last settled state, which for a pending refetch is always "no session". Server
153
+ renders never settle it, so hydration still starts from the server's state.
154
+
155
+ That module imports nothing and reads globals through `globalThis`. A bare
156
+ `window` in package source becomes a build shim that is always defined, and
157
+ pulls Node polyfills into the browser bundle; `scripts/build_npm.ts` guards the
158
+ emitted browser entrypoints against both.
126
159
 
127
160
  ## Anonymous identity checks
128
161
 
@@ -53,6 +53,12 @@ first. Prepare SQL schemas and pure import planners locally, then exercise
53
53
  protected real input without publishing private fixtures. Build browser recovery
54
54
  before retiring any legacy store or protocol it depends on.
55
55
 
56
+ A migrated app usually ends up with auth wiring that differs from a greenfield
57
+ shape: a factory kept free of environment reads, a guarded catch-all route, or
58
+ app-owned session readers. Record those paths in `loom.json` and adopt the
59
+ feature around them. Reshaping working authentication to match a default layout
60
+ buys nothing and risks a live boundary.
61
+
56
62
  Choose authentication and persistence release boundaries from actual ID/session
57
63
  dependencies. A verified NextAuth-to-Gongo bridge can allow staged replacement;
58
64
  it is an option, not a mandatory intermediate destination. A database adapter is