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cjs","sourcesContent":[null,"/// <reference lib=\"dom\" />\nimport { BridgeTerminatedError, RequestAbortedError, WorkerTimeoutError } from '../errors'\n\n// === Message Protocol ===\n\n/** Fields every request carries besides its type. */\ninterface RequestMeta {\n\t/** Correlates a worker response with its request on one postMessage channel. */\n\tid: number\n\t/**\n\t * Stable id for de-duplication. A follower retrying a request (after a\n\t * `LeaderUnresponsiveError`, say) sends the same id and the leader worker\n\t * answers from its response cache instead of applying it twice.\n\t */\n\trequestId?: string\n}\n\n/** One statement of a `batch` request. */\nexport interface BatchStatement {\n\tsql: string\n\tparams?: unknown[]\n}\n\n/**\n * Request message sent from the main thread (or a follower tab) to the SQLite\n * WASM worker.\n */\nexport type WorkerRequest =\n\t| (RequestMeta & {\n\t\t\ttype: 'open'\n\t\t\tddlStatements: string[]\n\t\t\tdbName?: string\n\t\t\t/**\n\t\t\t * `memory` opens an in-memory database without touching OPFS (the\n\t\t\t * IndexedDB adapter persists snapshots itself). Defaults to `opfs`.\n\t\t\t */\n\t\t\tstorage?: 'opfs' | 'memory'\n\t\t\t/** Fail with `NOT_FOUND` instead of creating the database file. */\n\t\t\tmustExist?: boolean\n\t  })\n\t| (RequestMeta & { type: 'close' })\n\t| (RequestMeta & { type: 'destroy' })\n\t| (RequestMeta & { type: 'execute'; sql: string; params?: unknown[] })\n\t| (RequestMeta & { type: 'query'; sql: string; params?: unknown[] })\n\t| (RequestMeta & {\n\t\t\t/**\n\t\t\t * Write statements of an open transaction, run in order in one message. The\n\t\t\t * worker stops at the first failing statement and answers with an error whose\n\t\t\t * `context.index` names it; statements before it ran inside the transaction,\n\t\t\t * which the caller then rolls back. Never sent outside a transaction.\n\t\t\t */\n\t\t\ttype: 'batch'\n\t\t\tstatements: BatchStatement[]\n\t  })\n\t| (RequestMeta & { type: 'begin' })\n\t| (RequestMeta & { type: 'commit' })\n\t| (RequestMeta & { type: 'rollback' })\n\t| (RequestMeta & { type: 'migrate'; from: number; to: number; statements: string[] })\n\t| (RequestMeta & { type: 'export' })\n\t| (RequestMeta & { type: 'import'; data: Uint8Array })\n\t| (RequestMeta & {\n\t\t\t/**\n\t\t\t * Leader worker only: answer follower RPC, pings and heartbeats on this\n\t\t\t * BroadcastChannel directly, so a hung leader main thread never blocks\n\t\t\t * other tabs (NEW-STORE-9).\n\t\t\t */\n\t\t\ttype: 'serve'\n\t\t\tchannelName: string\n\t\t\t/** The leader tab's version gate identity; its heartbeats carry it. */\n\t\t\tidentity?: { build: string; format: number; openSeq: number; tabId: string }\n\t  })\n\n/**\n * Response message sent from the worker back to the main thread.\n * Matches the request `id` for correlation.\n */\nexport type WorkerResponse =\n\t| { id: number; type: 'success'; data?: unknown }\n\t| { id: number; type: 'error'; message: string; code: string; context?: Record<string, unknown> }\n\n/**\n * Unsolicited status the worker reports while it works on a request, such as\n * waiting for another holder of the OPFS pool (a blocking state the app shows).\n */\nexport type WorkerStatusEvent =\n\t| {\n\t\t\tkind: 'storage-blocked'\n\t\t\tresource: 'pool' | 'legacy-pool'\n\t\t\tpoolName: string\n\t  }\n\t| {\n\t\t\tkind: 'storage-unblocked'\n\t\t\tresource: 'pool' | 'legacy-pool'\n\t\t\tpoolName: string\n\t\t\twaitedMs: number\n\t  }\n\t| {\n\t\t\t/**\n\t\t\t * The worker stopped serving because its transaction state is unknown (its\n\t\t\t * own ROLLBACK of an aborted transaction failed). The owning tab replaces it.\n\t\t\t */\n\t\t\tkind: 'worker-broken'\n\t\t\treason: string\n\t  }\n\n/** Envelope for {@link WorkerStatusEvent}s on the worker's postMessage channel. */\nexport interface WorkerEventMessage {\n\tid: -1\n\ttype: 'event'\n\tevent: WorkerStatusEvent\n}\n\n/** Message the main thread posts to a dedicated worker. */\nexport type WorkerInboundMessage = WorkerRequest & { clientId?: string }\n\n/** Per-request options accepted by bridges. */\nexport interface WorkerSendOptions {\n\t/** Cancels the wait for the response (the worker may still apply the request). */\n\tsignal?: AbortSignal\n\t/** Overrides the bridge timeout for this request; `Infinity` disables it. */\n\ttimeoutMs?: number\n}\n\n// === WorkerBridge Interface ===\n\n/**\n * Abstraction over the communication channel with the SQLite WASM worker.\n * In browsers, this is backed by a dedicated Web Worker (`Worker.postMessage`).\n * In Node.js tests, this is backed by better-sqlite3 via MockWorkerBridge.\n */\nexport interface WorkerBridge {\n\t/** Send a request to the worker and wait for a response. */\n\tsend(\n\t\trequest: WorkerRequest,\n\t\tclientId?: string,\n\t\toptions?: WorkerSendOptions,\n\t): Promise<WorkerResponse>\n\n\t/** Terminate the worker. Safe to call multiple times. */\n\tterminate(): void\n}\n\n// === Mutex ===\n\n// The Mutex now lives in its own module so non-browser adapters (better-sqlite3)\n// can serialize transactions without importing browser-worker code.\nexport { Mutex } from './mutex'\n\n// === WebWorkerBridge ===\n\n/** Options for {@link WebWorkerBridge}. */\nexport interface WebWorkerBridgeOptions {\n\t/** Receives status events (blocking state) the worker reports. */\n\tonEvent?: (event: WorkerStatusEvent) => void\n}\n\n/**\n * WorkerBridge implementation for browser environments.\n * Communicates with an actual Web Worker running SQLite WASM.\n */\nexport class WebWorkerBridge implements WorkerBridge {\n\tprivate worker: Worker\n\tprivate pending = new Map<\n\t\tnumber,\n\t\t{ resolve: (r: WorkerResponse) => void; reject: (e: Error) => void; type: string }\n\t>()\n\tprivate nextId = 1\n\tprivate terminated = false\n\tprivate timeoutMs: number\n\n\t/**\n\t * @param workerUrl - URL to the sqlite-wasm-worker script\n\t * @param timeoutMs - Timeout for worker responses in milliseconds (default: 30000)\n\t * @param options - Status event listener\n\t */\n\tconstructor(workerUrl: string | URL, timeoutMs = 30000, options: WebWorkerBridgeOptions = {}) {\n\t\tthis.timeoutMs = timeoutMs\n\t\tthis.worker = new Worker(workerUrl, { type: 'module' })\n\t\tthis.worker.onmessage = (event: MessageEvent<WorkerResponse | WorkerEventMessage>) => {\n\t\t\tconst message = event.data\n\t\t\tif (message.type === 'event') {\n\t\t\t\toptions.onEvent?.(message.event)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst entry = this.pending.get(message.id)\n\t\t\tif (entry) {\n\t\t\t\tthis.pending.delete(message.id)\n\t\t\t\tif (message.type === 'error' && message.code === 'BRIDGE_TERMINATED') {\n\t\t\t\t\t// The worker stopped serving without running this request (its\n\t\t\t\t\t// transaction state became unknown); retriable on its replacement.\n\t\t\t\t\tentry.reject(new BridgeTerminatedError(entry.type, message.message))\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tentry.resolve(message)\n\t\t\t}\n\t\t}\n\t\tthis.worker.onerror = (event) => {\n\t\t\t// Reject all pending requests on worker error\n\t\t\tconst error = new Error(`Worker error: ${event.message}`)\n\t\t\tfor (const [id, entry] of this.pending) {\n\t\t\t\tthis.pending.delete(id)\n\t\t\t\tentry.reject(error)\n\t\t\t}\n\t\t}\n\t}\n\n\t/** True once {@link terminate} ran. */\n\tisTerminated(): boolean {\n\t\treturn this.terminated\n\t}\n\n\t/** Post a request without awaiting a response (fire-and-forget control messages). */\n\tpost(request: WorkerRequest, clientId?: string): void {\n\t\tif (this.terminated) return\n\t\tconst message: WorkerInboundMessage = { ...request, id: this.nextId++, clientId }\n\t\tthis.worker.postMessage(message)\n\t}\n\n\tasync send(\n\t\trequest: WorkerRequest,\n\t\tclientId?: string,\n\t\toptions: WorkerSendOptions = {},\n\t): Promise<WorkerResponse> {\n\t\tif (this.terminated) {\n\t\t\treturn {\n\t\t\t\tid: request.id,\n\t\t\t\ttype: 'error',\n\t\t\t\tmessage: 'Worker has been terminated',\n\t\t\t\tcode: 'WORKER_TERMINATED',\n\t\t\t}\n\t\t}\n\n\t\tconst id = this.nextId++\n\t\tconst message: WorkerInboundMessage = { ...request, id, clientId }\n\t\tconst timeoutMs = options.timeoutMs ?? this.timeoutMs\n\t\tconst signal = options.signal\n\t\tif (signal?.aborted) {\n\t\t\tthrow new RequestAbortedError(request.type, request.requestId ?? String(id))\n\t\t}\n\n\t\treturn new Promise<WorkerResponse>((resolve, reject) => {\n\t\t\tconst timer = Number.isFinite(timeoutMs)\n\t\t\t\t? setTimeout(() => {\n\t\t\t\t\t\tthis.pending.delete(id)\n\t\t\t\t\t\tcleanup()\n\t\t\t\t\t\treject(new WorkerTimeoutError(message.type, timeoutMs))\n\t\t\t\t\t}, timeoutMs)\n\t\t\t\t: undefined\n\t\t\tconst onAbort = (): void => {\n\t\t\t\tthis.pending.delete(id)\n\t\t\t\tcleanup()\n\t\t\t\treject(new RequestAbortedError(request.type, request.requestId ?? String(id)))\n\t\t\t}\n\t\t\tconst cleanup = (): void => {\n\t\t\t\tif (timer !== undefined) clearTimeout(timer)\n\t\t\t\tsignal?.removeEventListener('abort', onAbort)\n\t\t\t}\n\t\t\tsignal?.addEventListener('abort', onAbort, { once: true })\n\n\t\t\tthis.pending.set(id, {\n\t\t\t\ttype: request.type,\n\t\t\t\tresolve: (response) => {\n\t\t\t\t\tcleanup()\n\t\t\t\t\tresolve(response)\n\t\t\t\t},\n\t\t\t\treject: (error) => {\n\t\t\t\t\tcleanup()\n\t\t\t\t\treject(error)\n\t\t\t\t},\n\t\t\t})\n\n\t\t\tthis.worker.postMessage(message)\n\t\t})\n\t}\n\n\tterminate(): void {\n\t\tif (this.terminated) return\n\t\tthis.terminated = true\n\t\t// Terminating the worker releases its OPFS handles and the pool Web Lock it\n\t\t// holds; callers release the tab leader lock only after this (NEW-STORE-10).\n\t\tthis.worker.terminate()\n\t\tfor (const [id, entry] of this.pending) {\n\t\t\tthis.pending.delete(id)\n\t\t\tentry.reject(new BridgeTerminatedError(entry.type, 'worker terminated'))\n\t\t}\n\t}\n}\n"]}