/** * Resumability hooks. * * A *very* small primitive that lets the server publish JSON-serializable * values which the client can read back without re-running the producer. * Think of it as a typed key/value store that survives the serialization * boundary. * * It is intentionally not a full Qwik-style resumability system — bQuery's * reactive graph is rebuilt on the client. Instead, this primitive helps * applications avoid double-fetching loader-style data by parking it on * `window.__BQUERY_RESUME__` (or a custom global) inside a CSP-nonce-aware * ``; }, }; }; /** Reader returned by `resumeState()`. */ export interface ResumeReader { /** Get a typed value from the resumable snapshot. */ get: (key: string) => T | undefined; /** Whether the snapshot was found. */ hasSnapshot: boolean; /** All entries (read-only). */ entries: () => Record; } /** * Reads a previously-emitted resumable snapshot from `window` and cleans it * up. Safe to call in any environment; returns an empty reader when no * snapshot is present (server, tests, etc.). */ export const resumeState = ( globalKey = '__BQUERY_RESUME__', scriptId = '__BQUERY_RESUME__' ): ResumeReader => { const empty: ResumeReader = { get: () => undefined, hasSnapshot: false, entries: () => ({}), }; if (isPrototypePollutionKey(globalKey) || isPrototypePollutionKey(scriptId)) return empty; if (typeof window === 'undefined') return empty; const raw = (window as unknown as Record)[globalKey]; try { delete (window as unknown as Record)[globalKey]; } catch { (window as unknown as Record)[globalKey] = undefined; } if (typeof document !== 'undefined' && typeof document.getElementById === 'function') { const el = document.getElementById(scriptId); if (el) el.remove(); } if (!raw || typeof raw !== 'object') return empty; const data = raw as Record; return { hasSnapshot: true, get(key: string): T | undefined { if (isPrototypePollutionKey(key)) return undefined; return data[key] as T | undefined; }, entries() { return cloneResumableEntries(data); }, }; };