/** Fields every request carries besides its type. */ interface RequestMeta { /** Correlates a worker response with its request on one postMessage channel. */ id: number; /** * Stable id for de-duplication. A follower retrying a request (after a * `LeaderUnresponsiveError`, say) sends the same id and the leader worker * answers from its response cache instead of applying it twice. */ requestId?: string; } /** One statement of a `batch` request. */ interface BatchStatement { sql: string; params?: unknown[]; } /** * Request message sent from the main thread (or a follower tab) to the SQLite * WASM worker. */ type WorkerRequest = (RequestMeta & { type: 'open'; ddlStatements: string[]; dbName?: string; /** * `memory` opens an in-memory database without touching OPFS (the * IndexedDB adapter persists snapshots itself). Defaults to `opfs`. */ storage?: 'opfs' | 'memory'; /** Fail with `NOT_FOUND` instead of creating the database file. */ mustExist?: boolean; }) | (RequestMeta & { type: 'close'; }) | (RequestMeta & { type: 'destroy'; }) | (RequestMeta & { type: 'execute'; sql: string; params?: unknown[]; }) | (RequestMeta & { type: 'query'; sql: string; params?: unknown[]; }) | (RequestMeta & { /** * Write statements of an open transaction, run in order in one message. The * worker stops at the first failing statement and answers with an error whose * `context.index` names it; statements before it ran inside the transaction, * which the caller then rolls back. Never sent outside a transaction. */ type: 'batch'; statements: BatchStatement[]; }) | (RequestMeta & { type: 'begin'; }) | (RequestMeta & { type: 'commit'; }) | (RequestMeta & { type: 'rollback'; }) | (RequestMeta & { type: 'migrate'; from: number; to: number; statements: string[]; }) | (RequestMeta & { type: 'export'; }) | (RequestMeta & { type: 'import'; data: Uint8Array; }) | (RequestMeta & { /** * Leader worker only: answer follower RPC, pings and heartbeats on this * BroadcastChannel directly, so a hung leader main thread never blocks * other tabs (NEW-STORE-9). */ type: 'serve'; channelName: string; /** The leader tab's version gate identity; its heartbeats carry it. */ identity?: { build: string; format: number; openSeq: number; tabId: string; }; }); /** * Response message sent from the worker back to the main thread. * Matches the request `id` for correlation. */ type WorkerResponse = { id: number; type: 'success'; data?: unknown; } | { id: number; type: 'error'; message: string; code: string; context?: Record; }; /** * Unsolicited status the worker reports while it works on a request, such as * waiting for another holder of the OPFS pool (a blocking state the app shows). */ type WorkerStatusEvent = { kind: 'storage-blocked'; resource: 'pool' | 'legacy-pool'; poolName: string; } | { kind: 'storage-unblocked'; resource: 'pool' | 'legacy-pool'; poolName: string; waitedMs: number; } | { /** * The worker stopped serving because its transaction state is unknown (its * own ROLLBACK of an aborted transaction failed). The owning tab replaces it. */ kind: 'worker-broken'; reason: string; }; /** Per-request options accepted by bridges. */ interface WorkerSendOptions { /** Cancels the wait for the response (the worker may still apply the request). */ signal?: AbortSignal; /** Overrides the bridge timeout for this request; `Infinity` disables it. */ timeoutMs?: number; } /** * Abstraction over the communication channel with the SQLite WASM worker. * In browsers, this is backed by a dedicated Web Worker (`Worker.postMessage`). * In Node.js tests, this is backed by better-sqlite3 via MockWorkerBridge. */ interface WorkerBridge { /** Send a request to the worker and wait for a response. */ send(request: WorkerRequest, clientId?: string, options?: WorkerSendOptions): Promise; /** Terminate the worker. Safe to call multiple times. */ terminate(): void; } /** Options for {@link WebWorkerBridge}. */ interface WebWorkerBridgeOptions { /** Receives status events (blocking state) the worker reports. */ onEvent?: (event: WorkerStatusEvent) => void; } /** * WorkerBridge implementation for browser environments. * Communicates with an actual Web Worker running SQLite WASM. */ declare class WebWorkerBridge implements WorkerBridge { private worker; private pending; private nextId; private terminated; private timeoutMs; /** * @param workerUrl - URL to the sqlite-wasm-worker script * @param timeoutMs - Timeout for worker responses in milliseconds (default: 30000) * @param options - Status event listener */ constructor(workerUrl: string | URL, timeoutMs?: number, options?: WebWorkerBridgeOptions); /** True once {@link terminate} ran. */ isTerminated(): boolean; /** Post a request without awaiting a response (fire-and-forget control messages). */ post(request: WorkerRequest, clientId?: string): void; send(request: WorkerRequest, clientId?: string, options?: WorkerSendOptions): Promise; terminate(): void; } export { type WorkerBridge as W, type WorkerSendOptions as a, type WorkerResponse as b, WebWorkerBridge as c, type WorkerRequest as d, type WorkerStatusEvent as e };