/** * The one gate every HTTP request awaits before it is sent. * * A page that fetches viewer-dependent data on mount (`GET /accounts/:user`, * a dashboard, a saved-item flag) can fire BEFORE the stored session has been * restored, so the request leaves with no `Authorization` header and the server * answers for an anonymous viewer — `saved: false` on an item you saved. * It is invisible on localhost, where the restore wins the race against the * first effect, and appears over the network as soon as the restore takes one * round trip: on a deployed URL it reads as stale server data rather than as a * request that left without its header. * * So the auth bond registers a gate here (`setAuthGate`) and every request * waits for it (`awaitAuthGate`) — the race is impossible to write by hand. * `@molecule/app-bonds-default-react`'s auth helpers register the gate for * apps that bond auth, so an app does not have to get it right itself. * * Waiting for the restore does NOT attach its token: a separately created HTTP * client needs `createDefaultHttpClientWithAuthBearer({ baseURL, * getToken: () => authClient.getAccessToken(), authScheme })` from * `@molecule/app-bonds-default-react` (or the auth client built with * `createDefaultAuthClientWithHttpSync`, which mirrors the token into the * bonded client) plus `setAuthGate(() => authClient.whenRestored())`. * * The gate is module-level, not per client, on purpose: the client an app ends * up using is often created AFTER the auth bond ran (`setupAuthDefault()` then * `setupHttpDefault()`), and a gate installed on the throwaway client the auth * bond saw would be silently lost. * * ## The gate's own restore requests are never held * * `awaitAuthGate` holds every request while the gate is pending — except the ones the gate's * settle function issues itself. Those are part of the restore it is waiting for; holding them * makes the gate wait on itself, and nothing but the timeout releases anything: an app * registering `setAuthGate(() => restoreSession())` over a restore that fetched through the * same client holds every page for the full bound, and one console line can name hundreds of * `GET /api/user` requests. So `setAuthGate(() => restoreSession())` works as written when * `restoreSession` calls the gated client. * * "Issued by the gate" means issued while the settle function is running: this package's * fetch client awaits the gate before anything else, so a restore's call is made in the settle * function's own synchronous execution. A restore that awaits something else first (a storage * read, a `Promise.resolve()`) and only then calls the client issues its request from a later * continuation, which nothing can attribute to the gate — browsers ship no async-context API * (`AsyncContext`/`AsyncLocalStorage`) to ask, and releasing requests merely because the gate is * pending would drop exactly the first-paint requests the gate exists to hold. A provider * whose request pipeline awaits the gate in a scheduled interceptor (the Axios bond) reaches it * a microtask after the settle function returns, outside the window, for the same reason. Such a * request must pass `skipAuthGate: true` (`RequestConfig.skipAuthGate`), and a timeout with * requests held is reported at error level naming it. * * @module */ import type { HttpClient } from './client.js'; import type { AuthGateRequest } from './types.js'; /** Wiring for an application that restores its session through its own HTTP client. */ export interface HttpAuthRestore { /** The application's configured transport, including its token resolver and interceptors. */ client: Pick; /** Restore the session using the supplied request function, which bypasses this gate. */ restore: (request: HttpClient['request']) => Promise; } /** * Registers the gate every HTTP request awaits before it is sent, or clears it * with `null`. * * For an app restoring its own session through HTTP, pass `{ client, restore }` after * configuring the client. This starts one restore. The callback's request function bypasses * the gate even after an asynchronous storage read, and other requests wait for it to settle. * The returned promise resolves when the restore settles. * * ```typescript * void setAuthGate({ * client, * async restore(request) { * const { data } = await request({ method: 'GET', url: currentProfilePath }) * session.setProfile(data) * }, * }) * ``` * * Pass a function that resolves once the session restore has settled — with the * template auth that is `() => authClient.whenRestored()`, which resolves * immediately when no restore is in flight, so registering it never makes a * request wait longer than the restore it was about to race anyway. * * Waiting for the restore does NOT attach its token: a separately created HTTP * client needs `createDefaultHttpClientWithAuthBearer({ baseURL, * getToken: () => authClient.getAccessToken(), authScheme })` from * `@molecule/app-bonds-default-react` (or the auth client built with * `createDefaultAuthClientWithHttpSync`, which mirrors the token into the * bonded client) plus `setAuthGate(() => authClient.whenRestored())`. * * The restore's own requests are never held: a request the settle function issues while it * runs goes out immediately, so `setAuthGate(() => restoreSession())` cannot deadlock even * when `restoreSession` calls the gated client. A restore that awaits something else first * (a storage read, a `Promise.resolve()`) issues its request from a later continuation, which * the gate cannot recognize — that request needs `skipAuthGate: true` (see * `RequestConfig.skipAuthGate`). `@molecule/app-auth`'s own restore needs neither because it * uses `fetch` rather than the bonded client. * * @param next - A session wait function, HTTP restore wiring, or `null` to clear the gate. * * @example * ```typescript * import { setAuthGate } from '@molecule/app-http' * * setAuthGate(() => authClient.whenRestored()) * ``` */ export declare function setAuthGate(next: HttpAuthRestore): Promise; export declare function setAuthGate(next: (() => Promise) | null): void; /** * Awaits the registered gate, if any. Every HTTP provider calls this before it * sends a request; callers never need to. * * A gate that rejects (a restore that failed) is logged and cleared of consequence: the request * is sent rather than held forever, because a dead restore must not block every request for the * life of the page. The same holds for a gate that never settles — the wait is bounded by * `GATE_TIMEOUT_MS`, and a timeout with requests held is reported at error level naming the fix * (`skipAuthGate`). * * @param request - The request being gated, so a request the gate itself depends on can opt * out (`skipAuthGate`) and a stalled gate can name what it held. A provider passes its own * request config; a caller passing nothing gets the plain wait. * @returns A promise that settles when the gate has released the request. */ export declare const awaitAuthGate: (request?: AuthGateRequest) => Promise; //# sourceMappingURL=auth-gate.d.ts.map