/** * The SDK's own copy of @fast-simon/session-identity (services/frontends/session-identity/src/main.ts), * below this header unchanged. That package is restricted on npm, so it cannot be a dependency of * fast-simon-sdk, a public package: as one it would make `npm install fast-simon-sdk` fail with * E404 for anyone without Fast Simon's npm token, as @fast-simon/widget-console once did. * * The copies meet only through localStorage and two window globals, so the contract is the key * names, the stored shapes and the global names: keep them identical to the package's. Drift here * is the bug this module exists to remove (two bundles stamping different starts for one visit), * so .github/scripts/tests/test_session_identity_copies.sh fails a PR on which this file does not * end with the package's main.ts verbatim. Re-copy on any change to it. */ /** * Fast Simon storefront session identity. * * Two values identify a billed session and both are chosen by the browser: the visit START time * in epoch seconds (query param `session`, stored as `session_count`) and the visitor TOKEN the * server minted once (query param `st`). A visit ends after SESSION_TOKEN_LIFE seconds without * activity; the next activity stamps a new start. * * Several bundles run on one page (reporting library, SSR grid, SDK, pixel, kits) and historically * each kept its own copy of this state under its own localStorage key. On a returning visitor's * first page two of them would each decide "new visit" and stamp starts seconds apart, and the * pipeline billed both. This module is the one owner of that state, and it is written so that * bundles at DIFFERENT versions cannot split a visit: * * - merge-on-read: every legacy start record is read and the freshest one wins; * - fan-out write: the result is written back to every legacy key, each in its legacy shape, so an * older bundle that still reads only its own key sees the same record. * * The visitor token gets the same treatment, plus a page-wide single-flight mint so the bundles on * one page share one `/post_load` instead of racing to create tokens. * * Keep the surface small: every bundle, the inline Liquid copies included, depends on it. */ export interface SessionRecord { /** Visit start, epoch seconds. Sent as `session`. */ start: number; /** Last activity, epoch seconds. */ lastActive: number; } /** Inactivity after which the next activity starts a new visit. The server's own per-session * dedup (isp_events) uses the same 30 minutes. */ export declare const SESSION_TOKEN_LIFE = 1800; /** * The current visit WITHOUT marking activity: the freshest stored record, or a fresh one that is * not written anywhere. For read-only consumers that must not extend a visit (debug views, * a report built before init). Everything that sends an event uses getSession(). */ export declare function peekSession(now?: number): SessionRecord; /** * The current visit, with this call counted as activity: lastActive becomes now, the start is kept * while the visit is alive and replaced once SESSION_TOKEN_LIFE seconds have passed since the * freshest activity any bundle recorded. The result is written to every legacy key. * * Call it from every event send, not only at init, so the 30 minutes measure inactivity. */ export declare function getSession(now?: number): SessionRecord; /** The stored visitor token, or null when none is usable. Reads the reporting key first, then the * SDK's, and repairs the other key so both agree. With storage blocked, the token minted on this * page is remembered on the page instead, so the bundles do not mint once per call. */ export declare function getVisitorToken(): string | null; /** Stores the token under every legacy key. Ignores a value that is not a plausible token. */ export declare function setVisitorToken(token: string): void; /** * The visitor token, minting it through `mint` when none is stored. One mint per page: concurrent * callers (the reporting library, the SDK, a widget, the pixel) share the same in-flight promise. * `mint` sends the `/post_load` and resolves with the `isp_token` from the response; a rejection or * an unusable value resolves this to null and clears the shared promise so a later caller can retry. */ export declare function ensureVisitorToken(mint: () => Promise): Promise;