@korajs/store 1.0.0-beta.11 → 1.0.0-beta.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -37
- package/dist/adapters/better-sqlite3.cjs +90 -4
- package/dist/adapters/better-sqlite3.cjs.map +1 -1
- package/dist/adapters/better-sqlite3.d.cts +21 -12
- package/dist/adapters/better-sqlite3.d.ts +21 -12
- package/dist/adapters/better-sqlite3.js +69 -5
- package/dist/adapters/better-sqlite3.js.map +1 -1
- package/dist/adapters/indexeddb.cjs +1316 -610
- package/dist/adapters/indexeddb.cjs.map +1 -1
- package/dist/adapters/indexeddb.d.cts +29 -3
- package/dist/adapters/indexeddb.d.ts +29 -3
- package/dist/adapters/indexeddb.js +247 -221
- package/dist/adapters/indexeddb.js.map +1 -1
- package/dist/adapters/sqlite-wasm-worker.cjs +803 -62
- package/dist/adapters/sqlite-wasm-worker.cjs.map +1 -1
- package/dist/adapters/sqlite-wasm-worker.js +480 -62
- package/dist/adapters/sqlite-wasm-worker.js.map +1 -1
- package/dist/adapters/sqlite-wasm.cjs +1032 -344
- package/dist/adapters/sqlite-wasm.cjs.map +1 -1
- package/dist/adapters/sqlite-wasm.d.cts +239 -18
- package/dist/adapters/sqlite-wasm.d.ts +239 -18
- package/dist/adapters/sqlite-wasm.js +111 -5
- package/dist/adapters/sqlite-wasm.js.map +1 -1
- package/dist/backup-4NUPDEMB.js +25 -0
- package/dist/chunk-32EDKWMI.js +876 -0
- package/dist/chunk-32EDKWMI.js.map +1 -0
- package/dist/{chunk-CYO3YKFL.js → chunk-5ZRRF6R2.js} +2 -2
- package/dist/chunk-A6GFH5IG.js +36 -0
- package/dist/chunk-A6GFH5IG.js.map +1 -0
- package/dist/chunk-DUKTWVZW.js +175 -0
- package/dist/chunk-DUKTWVZW.js.map +1 -0
- package/dist/chunk-HZTDBLDB.js +607 -0
- package/dist/chunk-HZTDBLDB.js.map +1 -0
- package/dist/{chunk-GSJ7EEUC.js → chunk-J5AGXF3D.js} +58 -9
- package/dist/chunk-J5AGXF3D.js.map +1 -0
- package/dist/chunk-MCQKEHOD.js +112 -0
- package/dist/chunk-MCQKEHOD.js.map +1 -0
- package/dist/chunk-P4V375FY.js +810 -0
- package/dist/chunk-P4V375FY.js.map +1 -0
- package/dist/chunk-QDUSJMFV.js +1095 -0
- package/dist/chunk-QDUSJMFV.js.map +1 -0
- package/dist/chunk-SJP2HTYZ.js +1003 -0
- package/dist/chunk-SJP2HTYZ.js.map +1 -0
- package/dist/chunk-W5F2LWG6.js +591 -0
- package/dist/chunk-W5F2LWG6.js.map +1 -0
- package/dist/chunk-Y5LRH7KI.js +5341 -0
- package/dist/chunk-Y5LRH7KI.js.map +1 -0
- package/dist/{export-audit-ODGQTQIK.js → export-audit-J2QXRHD2.js} +3 -3
- package/dist/index.cjs +9354 -4157
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1064 -108
- package/dist/index.d.ts +1064 -108
- package/dist/index.js +249 -3092
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +580 -264
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +15 -85
- package/dist/internal.d.ts +15 -85
- package/dist/internal.js +10 -8
- package/dist/restore-FR7XIKT2.js +299 -0
- package/dist/restore-FR7XIKT2.js.map +1 -0
- package/dist/sql-builder-C4u55jKW.d.cts +608 -0
- package/dist/sql-builder-CGckdBDL.d.ts +608 -0
- package/dist/sqlite-wasm-channel-DVX93Uiq.d.cts +141 -0
- package/dist/sqlite-wasm-channel-DVX93Uiq.d.ts +141 -0
- package/dist/sqlite-wasm-channel-MLHLKEED.js +12 -0
- package/dist/store-IMWKZLWB.js +16 -0
- package/dist/store-IMWKZLWB.js.map +1 -0
- package/dist/{types-BslDOTk0.d.ts → types-Dh-xyUjP.d.cts} +99 -26
- package/dist/{types-BslDOTk0.d.cts → types-Dh-xyUjP.d.ts} +99 -26
- package/package.json +3 -2
- package/dist/backup-5S7K2Z35.js +0 -14
- package/dist/chunk-GSJ7EEUC.js.map +0 -1
- package/dist/chunk-HS6DBMST.js +0 -383
- package/dist/chunk-HS6DBMST.js.map +0 -1
- package/dist/chunk-RJIEKEN4.js +0 -751
- package/dist/chunk-RJIEKEN4.js.map +0 -1
- package/dist/chunk-TUR7RGEP.js +0 -84
- package/dist/chunk-TUR7RGEP.js.map +0 -1
- package/dist/chunk-VDBJQ75K.js +0 -78
- package/dist/chunk-VDBJQ75K.js.map +0 -1
- package/dist/chunk-YT7MRFCP.js +0 -687
- package/dist/chunk-YT7MRFCP.js.map +0 -1
- package/dist/sqlite-wasm-channel-GGHA2BXU.js +0 -12
- package/dist/sqlite-wasm-channel-L6o3bWQK.d.cts +0 -92
- package/dist/sqlite-wasm-channel-L6o3bWQK.d.ts +0 -92
- package/dist/types-BOO9EPt1.d.ts +0 -170
- package/dist/types-C_GDavG-.d.cts +0 -170
- /package/dist/{backup-5S7K2Z35.js.map → backup-4NUPDEMB.js.map} +0 -0
- /package/dist/{chunk-CYO3YKFL.js.map → chunk-5ZRRF6R2.js.map} +0 -0
- /package/dist/{export-audit-ODGQTQIK.js.map → export-audit-J2QXRHD2.js.map} +0 -0
- /package/dist/{sqlite-wasm-channel-GGHA2BXU.js.map → sqlite-wasm-channel-MLHLKEED.js.map} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/errors.ts","../../src/adapters/mutex.ts","../../src/adapters/sqlite-wasm-channel.ts","../../src/adapters/sqlite-wasm.ts","../../src/adapters/sqlite-wasm-adapter.ts","../../src/multi-tab/tab-storage.ts"],"sourcesContent":["import { KoraError } from '@korajs/core'\n\n/**\n * Thrown when a query is invalid (bad field names, invalid operators, etc.).\n */\nexport class QueryError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'QUERY_ERROR', context)\n\t\tthis.name = 'QueryError'\n\t}\n}\n\n/**\n * Thrown when a record is not found by ID (findById, update, delete on missing record).\n */\nexport class RecordNotFoundError extends KoraError {\n\tconstructor(collection: string, recordId: string) {\n\t\tsuper(`Record \"${recordId}\" not found in collection \"${collection}\"`, 'RECORD_NOT_FOUND', {\n\t\t\tcollection,\n\t\t\trecordId,\n\t\t})\n\t\tthis.name = 'RecordNotFoundError'\n\t}\n}\n\n/**\n * Thrown inside `applyRemoteOperation` when the row's version state changed\n * between the caller's snapshot read and the guarded write (a concurrent local\n * mutation landed in the window). The transaction is rolled back with nothing\n * written; the caller re-reads fresh state, recomputes its merge, and retries.\n * This is the optimistic-concurrency guard that keeps merge-engine results\n * (richtext, add-wins arrays, constraint resolutions) from clobbering newer\n * local edits they never saw.\n */\nexport class OptimisticLockError extends KoraError {\n\tconstructor(collection: string, recordId: string) {\n\t\tsuper(\n\t\t\t`Row version changed while merging record \"${recordId}\" in \"${collection}\"; retry with fresh state`,\n\t\t\t'OPTIMISTIC_LOCK',\n\t\t\t{ collection, recordId },\n\t\t)\n\t\tthis.name = 'OptimisticLockError'\n\t}\n}\n\n/**\n * Thrown when a storage adapter operation fails.\n */\nexport class AdapterError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'ADAPTER_ERROR', context)\n\t\tthis.name = 'AdapterError'\n\t}\n}\n\n/**\n * Thrown when an operation is attempted on a store that has not been opened.\n */\nexport class StoreNotOpenError extends KoraError {\n\tconstructor() {\n\t\tsuper('Store is not open. Call store.open() before performing operations.', 'STORE_NOT_OPEN')\n\t\tthis.name = 'StoreNotOpenError'\n\t}\n}\n\n/**\n * Thrown when the Web Worker fails to initialize (WASM load failure, OPFS unavailable, etc.).\n */\nexport class WorkerInitError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(`Worker initialization failed: ${message}`, 'WORKER_INIT_ERROR', context)\n\t\tthis.name = 'WorkerInitError'\n\t}\n}\n\n/**\n * Thrown when the Web Worker does not respond within the configured timeout.\n */\nexport class WorkerTimeoutError extends KoraError {\n\tconstructor(operation: string, timeoutMs: number) {\n\t\tsuper(\n\t\t\t`Worker did not respond within ${timeoutMs}ms for operation \"${operation}\"`,\n\t\t\t'WORKER_TIMEOUT',\n\t\t\t{ operation, timeoutMs },\n\t\t)\n\t\tthis.name = 'WorkerTimeoutError'\n\t}\n}\n\n/**\n * Thrown when a follower tab cannot reach a live leader tab over the multi-tab\n * broadcast channel (the leader tab was closed, crashed, or is unresponsive).\n * Distinct from {@link WorkerTimeoutError}: this fails fast on a confirmed-absent\n * leader instead of waiting out the full RPC timeout.\n */\nexport class NoLeaderError extends KoraError {\n\tconstructor(operation: string) {\n\t\tsuper(\n\t\t\t`No live leader tab is answering multi-tab storage RPC for operation \"${operation}\"`,\n\t\t\t'NO_LEADER',\n\t\t\t{ operation },\n\t\t)\n\t\tthis.name = 'NoLeaderError'\n\t}\n}\n\n/**\n * Thrown when IndexedDB persistence operations fail (serialize/deserialize).\n */\nexport class PersistenceError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(`Persistence error: ${message}`, 'PERSISTENCE_ERROR', context)\n\t\tthis.name = 'PersistenceError'\n\t}\n}\n","/**\n * Async mutex for serializing access to a resource across `await` boundaries.\n *\n * This exists because an adapter's `transaction()` is async (its callback may\n * await), but the underlying database transaction (BEGIN…COMMIT) is not\n * reentrant: starting a second transaction while one is open is an error. When\n * a client applies a relayed remote operation while a local write is in flight,\n * the two `transaction()` calls interleave at their `await` points, and without\n * serialization the second `BEGIN` collides with the first, silently dropping\n * the operation. Serializing every transaction through this mutex guarantees at\n * most one is active at a time.\n */\nexport class Mutex {\n\tprivate locked = false\n\tprivate readonly waiters: Array<() => void> = []\n\n\t/**\n\t * Acquire the mutex. Resolves with a release function once the lock is held.\n\t * If the mutex is already held, the caller waits (FIFO) until it is released.\n\t */\n\tasync acquire(): Promise<() => void> {\n\t\tif (!this.locked) {\n\t\t\tthis.locked = true\n\t\t\treturn this.createRelease()\n\t\t}\n\n\t\treturn new Promise<() => void>((resolve) => {\n\t\t\tthis.waiters.push(() => {\n\t\t\t\tresolve(this.createRelease())\n\t\t\t})\n\t\t})\n\t}\n\n\tprivate createRelease(): () => void {\n\t\tlet released = false\n\t\treturn () => {\n\t\t\tif (released) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\treleased = true\n\t\t\tconst next = this.waiters.shift()\n\t\t\tif (next) {\n\t\t\t\tnext()\n\t\t\t} else {\n\t\t\t\tthis.locked = false\n\t\t\t}\n\t\t}\n\t}\n}\n","/// <reference lib=\"dom\" />\nimport { WorkerTimeoutError } from '../errors'\n\n// === Message Protocol ===\n\n/**\n * Request message sent from the main thread to the SQLite WASM worker.\n * Each request carries a unique `id` for response correlation.\n */\nexport type WorkerRequest =\n\t| { id: number; type: 'open'; ddlStatements: string[]; dbName?: string }\n\t| { id: number; type: 'close' }\n\t| { id: number; type: 'execute'; sql: string; params?: unknown[] }\n\t| { id: number; type: 'query'; sql: string; params?: unknown[] }\n\t| { id: number; type: 'begin' }\n\t| { id: number; type: 'commit' }\n\t| { id: number; type: 'rollback' }\n\t| { id: number; type: 'migrate'; from: number; to: number; statements: string[] }\n\t| { id: number; type: 'export' }\n\t| { id: number; type: 'import'; data: Uint8Array }\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// === WorkerBridge Interface ===\n\n/**\n * Abstraction over the communication channel with the SQLite WASM worker.\n * In browsers, this is backed by a real Web Worker via MessagePort.\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(request: WorkerRequest, clientId?: string): 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/**\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 }\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 */\n\tconstructor(workerUrl: string | URL, timeoutMs = 30000) {\n\t\tthis.timeoutMs = timeoutMs\n\t\tthis.worker = new Worker(workerUrl, { type: 'module' })\n\t\tthis.worker.onmessage = (event: MessageEvent<WorkerResponse>) => {\n\t\t\tconst response = event.data\n\t\t\tconst entry = this.pending.get(response.id)\n\t\t\tif (entry) {\n\t\t\t\tthis.pending.delete(response.id)\n\t\t\t\tentry.resolve(response)\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\tasync send(request: WorkerRequest): 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 req = { ...request, id }\n\n\t\treturn new Promise<WorkerResponse>((resolve, reject) => {\n\t\t\tconst timer = setTimeout(() => {\n\t\t\t\tthis.pending.delete(id)\n\t\t\t\treject(new WorkerTimeoutError(req.type, this.timeoutMs))\n\t\t\t}, this.timeoutMs)\n\n\t\t\tthis.pending.set(id, {\n\t\t\t\tresolve: (response) => {\n\t\t\t\t\tclearTimeout(timer)\n\t\t\t\t\tresolve(response)\n\t\t\t\t},\n\t\t\t\treject: (error) => {\n\t\t\t\t\tclearTimeout(timer)\n\t\t\t\t\treject(error)\n\t\t\t\t},\n\t\t\t})\n\n\t\t\tthis.worker.postMessage(req)\n\t\t})\n\t}\n\n\tterminate(): void {\n\t\tif (this.terminated) return\n\t\tthis.terminated = true\n\t\tthis.worker.terminate()\n\t\t// Reject any pending requests\n\t\tfor (const [id, entry] of this.pending) {\n\t\t\tthis.pending.delete(id)\n\t\t\tentry.reject(new Error('Worker terminated'))\n\t\t}\n\t}\n}\n","// Entry point for @korajs/store/sqlite-wasm\nexport { SqliteWasmAdapter } from './sqlite-wasm-adapter'\nexport type { SqliteWasmAdapterOptions } from './sqlite-wasm-adapter'\nexport { WebWorkerBridge, Mutex } from './sqlite-wasm-channel'\nexport type { WorkerBridge, WorkerRequest, WorkerResponse } from './sqlite-wasm-channel'\n","import { generateFullDDL } from '@korajs/core'\nimport type { KoraEventEmitter, SchemaDefinition } from '@korajs/core'\nimport { AdapterError, StoreNotOpenError } from '../errors'\nimport {\n\tFollowerBroadcastBridge,\n\tTransactionSerializingWorkerBridge,\n\tacquireTabStorageSession,\n\tstartLeaderRpcRelay,\n} from '../multi-tab/tab-storage'\nimport type {\n\tMigrationPlan,\n\tStorageAdapter,\n\tStorageFallbackReason,\n\tStorageOpenState,\n\tTransaction,\n} from '../types'\nimport { Mutex } from './sqlite-wasm-channel'\nimport type { WorkerBridge, WorkerRequest, WorkerResponse } from './sqlite-wasm-channel'\n\ntype WorkerSuccessResponse = Extract<WorkerResponse, { type: 'success' }>\nlet warnedSharedWorkerDeprecated = false\n\n/**\n * Options for creating a SqliteWasmAdapter.\n */\nexport interface SqliteWasmAdapterOptions {\n\t/**\n\t * Injected WorkerBridge for testing. If omitted, a WebWorkerBridge is created\n\t * in browser environments.\n\t */\n\tbridge?: WorkerBridge\n\n\t/**\n\t * Database name for persistence. Used as the OPFS file name or IDB key.\n\t */\n\tdbName?: string\n\n\t/**\n\t * URL to the sqlite-wasm-worker script. Required in browsers if no bridge is provided.\n\t */\n\tworkerUrl?: string | URL\n\n\t/** Timeout for worker / follower RPC responses. Defaults to 30000ms. */\n\tworkerResponseTimeoutMs?: number\n\n\t/**\n\t * @deprecated SharedWorker-hosted SQLite cannot use OPFS SyncAccessHandle and\n\t * is never durable. Kora ignores this option and uses the dedicated-worker\n\t * leader/follower path for durable multi-tab storage.\n\t */\n\tsharedWorkerUrl?: string | URL\n\n\t/**\n\t * When false, the adapter records a non-persistent open without emitting\n\t * `store:opfs-unavailable`. `createApp()` uses this while it promotes the store\n\t * to durable IndexedDB; direct adapter users keep the diagnostic by default.\n\t */\n\temitNonPersistentDiagnostic?: boolean\n\n\t/**\n\t * When set, storage diagnostics are emitted here: `store:opfs-unavailable` when\n\t * persistence silently degraded to a non-persistent in-memory database, and\n\t * `store:db-name-collision` when another runtime on this origin was already\n\t * using this database name.\n\t */\n\temitter?: KoraEventEmitter\n}\n\n/**\n * SQLite WASM adapter that communicates with a SQLite instance through a WorkerBridge.\n *\n * In browsers, the bridge is backed by a Web Worker running SQLite WASM with OPFS persistence.\n * In Node.js tests, the bridge is backed by MockWorkerBridge wrapping better-sqlite3.\n *\n * @example\n * ```typescript\n * // Browser usage\n * const adapter = new SqliteWasmAdapter({ workerUrl: '/sqlite-wasm-worker.js' })\n *\n * // Test usage with MockWorkerBridge\n * import { MockWorkerBridge } from './sqlite-wasm-mock-bridge'\n * const adapter = new SqliteWasmAdapter({ bridge: new MockWorkerBridge() })\n * ```\n */\nexport class SqliteWasmAdapter implements StorageAdapter {\n\tprivate bridge: WorkerBridge | null = null\n\tprivate opened = false\n\tprivate readonly mutex = new Mutex()\n\tprivate readonly injectedBridge: WorkerBridge | undefined\n\tprivate readonly workerUrl: string | URL | undefined\n\tprivate readonly sharedWorkerUrl: string | URL | undefined\n\tprivate readonly workerResponseTimeoutMs: number\n\tprivate readonly dbName: string\n\tprivate readonly emitter: KoraEventEmitter | undefined\n\tprivate tabSession: Awaited<ReturnType<typeof acquireTabStorageSession>> | null = null\n\tprivate readonly emitNonPersistentDiagnostic: boolean\n\tprivate storageOpenState: StorageOpenState | null = null\n\t/** Retained so a follower promoted to leader can re-open its own worker. */\n\tprivate schema: SchemaDefinition | null = null\n\tprivate promoting = false\n\n\tconstructor(options: SqliteWasmAdapterOptions = {}) {\n\t\tthis.injectedBridge = options.bridge\n\t\tthis.workerUrl = options.workerUrl\n\t\tthis.sharedWorkerUrl = options.sharedWorkerUrl\n\t\tthis.workerResponseTimeoutMs = options.workerResponseTimeoutMs ?? 30_000\n\t\tthis.dbName = options.dbName ?? 'kora-db'\n\t\tthis.emitter = options.emitter\n\t\tthis.emitNonPersistentDiagnostic = options.emitNonPersistentDiagnostic ?? true\n\t}\n\n\tasync open(schema: SchemaDefinition): Promise<void> {\n\t\tif (this.opened) return\n\n\t\tconst ddlStatements = generateFullDDL(schema)\n\t\tif (this.injectedBridge) {\n\t\t\tthis.bridge = this.injectedBridge\n\t\t\tconst response = await this.openCurrentBridge(ddlStatements)\n\t\t\tthis.reportStorageMode(response.data)\n\t\t\tthis.opened = true\n\t\t\treturn\n\t\t}\n\t\tif (this.workerUrl) {\n\t\t\tthis.warnSharedWorkerDeprecated()\n\t\t\tawait this.openDurableWorkerBridge(schema)\n\t\t\tconst response = await this.openCurrentBridge(ddlStatements)\n\t\t\tthis.reportStorageMode(response.data)\n\t\t\tthis.opened = true\n\t\t\treturn\n\t\t}\n\t\tthrow new AdapterError(\n\t\t\t'SqliteWasmAdapter requires either a bridge (for testing) or a workerUrl (for browsers). ' +\n\t\t\t\t'Pass { bridge: new MockWorkerBridge() } for tests, or { workerUrl: \"/worker.js\" } for browsers.',\n\t\t)\n\t}\n\n\tprivate async openDurableWorkerBridge(schema: SchemaDefinition): Promise<void> {\n\t\tif (!this.workerUrl) {\n\t\t\tthrow new AdapterError('Durable SQLite WASM storage requires workerUrl.')\n\t\t}\n\n\t\tthis.schema = schema\n\t\tthis.tabSession = await acquireTabStorageSession(this.dbName, {\n\t\t\tonPromote: () => {\n\t\t\t\tvoid this.promoteToLeader()\n\t\t\t},\n\t\t})\n\t\tconst { WebWorkerBridge } = await import('./sqlite-wasm-channel')\n\n\t\tif (this.tabSession.role === 'leader') {\n\t\t\tconst workerBridge = new TransactionSerializingWorkerBridge(\n\t\t\t\tnew WebWorkerBridge(this.workerUrl, this.workerResponseTimeoutMs),\n\t\t\t)\n\t\t\tthis.tabSession.stopRelay = startLeaderRpcRelay(this.tabSession.channelName, workerBridge)\n\t\t\tthis.bridge = workerBridge\n\t\t\treturn\n\t\t}\n\n\t\tconst followerBridge = new FollowerBroadcastBridge(\n\t\t\tthis.tabSession.channelName,\n\t\t\tthis.workerResponseTimeoutMs,\n\t\t)\n\t\tthis.bridge = followerBridge\n\t\t// Another runtime on this origin already owns this database name. That is\n\t\t// expected for multiple tabs of the same app (they share one leader),\n\t\t// but a bug if these are logically separate apps, so surface it so the\n\t\t// developer can give them distinct store names.\n\t\tthis.emitter?.emit({\n\t\t\ttype: 'store:db-name-collision',\n\t\t\tdbName: this.dbName,\n\t\t\tmessage: `Another runtime on this origin is already using database \"${this.dbName}\"; this runtime is sharing it as a follower. If these are separate apps, give each a distinct store name.`,\n\t\t})\n\t\t// Readiness handshake: give the leader relay a moment to answer before the\n\t\t// first RPC, so a follower opened during a leader's startup race retries the\n\t\t// handshake instead of firing into the void and waiting out the full timeout.\n\t\tawait followerBridge.waitForLeader()\n\t}\n\n\tprivate async openCurrentBridge(ddlStatements: string[]): Promise<WorkerSuccessResponse> {\n\t\tconst response = await this.sendRequest({\n\t\t\tid: 0,\n\t\t\ttype: 'open',\n\t\t\tddlStatements,\n\t\t\tdbName: this.dbName,\n\t\t})\n\t\tif (response.type === 'error') {\n\t\t\tthrow new AdapterError(`Failed to open database: ${response.message}`, {\n\t\t\t\tcode: response.code,\n\t\t\t\tdbName: this.dbName,\n\t\t\t})\n\t\t}\n\t\treturn response\n\t}\n\n\t/**\n\t * Surface a `store:opfs-unavailable` diagnostic when the worker reported that\n\t * persistence silently degraded to an in-memory database, so a data-loss\n\t * condition is observable instead of failing silently. A persistent open (or a\n\t * bridge that does not report a mode, e.g. the Node mock) emits nothing.\n\t */\n\tprivate reportStorageMode(data: unknown): void {\n\t\tthis.storageOpenState = parseStorageOpenState(data)\n\t\tif (!this.emitNonPersistentDiagnostic || !this.emitter || !this.storageOpenState) {\n\t\t\treturn\n\t\t}\n\t\tif (this.storageOpenState.persistent === false) {\n\t\t\tconst reason = this.storageOpenState.fallbackReason ?? 'unsupported'\n\t\t\tthis.emitter.emit({\n\t\t\t\ttype: 'store:opfs-unavailable',\n\t\t\t\tdbName: this.dbName,\n\t\t\t\treason,\n\t\t\t\tmessage: `OPFS persistence is unavailable (${reason}) for database \"${this.dbName}\"; the store is running in memory and data will not survive a reload.`,\n\t\t\t})\n\t\t}\n\t}\n\n\tgetStorageOpenState(): StorageOpenState | null {\n\t\treturn this.storageOpenState\n\t}\n\n\tprivate warnSharedWorkerDeprecated(): void {\n\t\tif (!this.sharedWorkerUrl || warnedSharedWorkerDeprecated) {\n\t\t\treturn\n\t\t}\n\t\twarnedSharedWorkerDeprecated = true\n\t\tconsole.warn(\n\t\t\t'[kora] sharedWorkerUrl is deprecated and ignored: SharedWorker-hosted SQLite ' +\n\t\t\t\t'cannot use OPFS and is never durable. Kora uses the durable dedicated-worker ' +\n\t\t\t\t'leader/follower path for multi-tab storage.',\n\t\t)\n\t}\n\n\tasync close(): Promise<void> {\n\t\tif (!this.bridge) return\n\n\t\ttry {\n\t\t\tawait this.sendRequest({ id: 0, type: 'close' })\n\t\t} finally {\n\t\t\tthis.tabSession?.stopRelay?.()\n\t\t\tthis.tabSession?.cancelPromotionWatch?.()\n\t\t\tif (this.tabSession?.releaseLock) {\n\t\t\t\tawait this.tabSession.releaseLock()\n\t\t\t}\n\t\t\tthis.tabSession = null\n\t\t\tthis.bridge.terminate()\n\t\t\tthis.bridge = null\n\t\t\tthis.opened = false\n\t\t}\n\t}\n\n\t/**\n\t * Promotes a follower to leader after the previous leader released the storage\n\t * lock (its tab closed or crashed). The lock is only granted once the old leader\n\t * is gone, so opening our own worker here is safe from the OPFS single-writer\n\t * rule. In-flight follower RPCs are rejected and left to the caller / reactive\n\t * layer to retry against the new leader.\n\t */\n\tprivate async promoteToLeader(): Promise<void> {\n\t\tif (this.promoting || !this.opened || this.workerUrl === undefined || this.schema === null) {\n\t\t\treturn\n\t\t}\n\t\tthis.promoting = true\n\n\t\tconst { WebWorkerBridge } = await import('./sqlite-wasm-channel')\n\t\tconst workerBridge = new TransactionSerializingWorkerBridge(\n\t\t\tnew WebWorkerBridge(this.workerUrl, this.workerResponseTimeoutMs),\n\t\t)\n\n\t\tconst previousBridge = this.bridge\n\t\tthis.bridge = workerBridge\n\t\tif (this.tabSession) {\n\t\t\tthis.tabSession.role = 'leader'\n\t\t\tthis.tabSession.stopRelay = startLeaderRpcRelay(this.tabSession.channelName, workerBridge)\n\t\t}\n\t\tpreviousBridge?.terminate()\n\n\t\t// Re-open against our own worker. DDL is idempotent, and the OPFS data the old\n\t\t// leader persisted is now readable by this worker.\n\t\tconst ddlStatements = generateFullDDL(this.schema)\n\t\tawait this.sendRequest({ id: 0, type: 'open', ddlStatements, dbName: this.dbName })\n\t}\n\n\tasync execute(sql: string, params?: unknown[]): Promise<void> {\n\t\tthis.guardOpen()\n\t\tconst response = await this.sendRequest({ id: 0, type: 'execute', sql, params })\n\t\tif (response.type === 'error') {\n\t\t\tthrow new AdapterError(`Execute failed: ${response.message}`, { sql, params })\n\t\t}\n\t}\n\n\tasync query<T>(sql: string, params?: unknown[]): Promise<T[]> {\n\t\tthis.guardOpen()\n\t\tconst response = await this.sendRequest({ id: 0, type: 'query', sql, params })\n\t\tif (response.type === 'error') {\n\t\t\tthrow new AdapterError(`Query failed: ${response.message}`, { sql, params })\n\t\t}\n\t\treturn (response.data as T[]) ?? []\n\t}\n\n\tasync transaction(fn: (tx: Transaction) => Promise<void>): Promise<void> {\n\t\tthis.guardOpen()\n\n\t\tconst release = await this.mutex.acquire()\n\t\ttry {\n\t\t\tawait this.sendChecked({ id: 0, type: 'begin' }, 'BEGIN transaction')\n\n\t\t\tconst tx: Transaction = {\n\t\t\t\texecute: async (sql: string, params?: unknown[]): Promise<void> => {\n\t\t\t\t\tconst response = await this.sendRequest({ id: 0, type: 'execute', sql, params })\n\t\t\t\t\tif (response.type === 'error') {\n\t\t\t\t\t\tthrow new AdapterError(`Transaction execute failed: ${response.message}`, {\n\t\t\t\t\t\t\tsql,\n\t\t\t\t\t\t\tparams,\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tquery: async <T>(sql: string, params?: unknown[]): Promise<T[]> => {\n\t\t\t\t\tconst response = await this.sendRequest({ id: 0, type: 'query', sql, params })\n\t\t\t\t\tif (response.type === 'error') {\n\t\t\t\t\t\tthrow new AdapterError(`Transaction query failed: ${response.message}`, { sql, params })\n\t\t\t\t\t}\n\t\t\t\t\treturn (response.data as T[]) ?? []\n\t\t\t\t},\n\t\t\t}\n\n\t\t\tawait fn(tx)\n\t\t\tawait this.sendChecked({ id: 0, type: 'commit' }, 'COMMIT transaction')\n\t\t} catch (error) {\n\t\t\t// Attempt rollback, but don't mask the original error\n\t\t\ttry {\n\t\t\t\tawait this.sendRequest({ id: 0, type: 'rollback' })\n\t\t\t} catch {\n\t\t\t\t// Rollback failure is secondary to the original error\n\t\t\t}\n\t\t\tthrow error\n\t\t} finally {\n\t\t\trelease()\n\t\t}\n\t}\n\n\tasync migrate(from: number, to: number, migration: MigrationPlan): Promise<void> {\n\t\tthis.guardOpen()\n\n\t\tconst release = await this.mutex.acquire()\n\t\ttry {\n\t\t\tawait this.sendChecked({ id: 0, type: 'begin' }, 'BEGIN migration')\n\n\t\t\tfor (const sql of migration.statements) {\n\t\t\t\tconst response = await this.sendRequest({ id: 0, type: 'execute', sql })\n\t\t\t\tif (response.type === 'error') {\n\t\t\t\t\tthrow new AdapterError(`Migration from v${from} to v${to} failed: ${response.message}`, {\n\t\t\t\t\t\tfrom,\n\t\t\t\t\t\tto,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tawait this.sendChecked({ id: 0, type: 'commit' }, 'COMMIT migration')\n\t\t} catch (error) {\n\t\t\ttry {\n\t\t\t\tawait this.sendRequest({ id: 0, type: 'rollback' })\n\t\t\t} catch {\n\t\t\t\t// Rollback failure is secondary\n\t\t\t}\n\t\t\tif (error instanceof AdapterError) throw error\n\t\t\tthrow new AdapterError(\n\t\t\t\t`Migration from v${from} to v${to} failed: ${(error as Error).message}`,\n\t\t\t\t{ from, to },\n\t\t\t)\n\t\t} finally {\n\t\t\trelease()\n\t\t}\n\t}\n\n\t/**\n\t * Export the database as a Uint8Array (for IndexedDB persistence).\n\t * Only available when the database is open.\n\t */\n\tasync exportDatabase(): Promise<Uint8Array> {\n\t\tthis.guardOpen()\n\t\tconst response = await this.sendRequest({ id: 0, type: 'export' })\n\t\tif (response.type === 'error') {\n\t\t\tthrow new AdapterError(`Export failed: ${response.message}`, {\n\t\t\t\tcode: response.code,\n\t\t\t\tcontext: response.context,\n\t\t\t})\n\t\t}\n\t\treturn response.data as Uint8Array\n\t}\n\n\t/**\n\t * Import a serialized database snapshot.\n\t */\n\tasync importDatabase(data: Uint8Array): Promise<void> {\n\t\tthis.guardOpen()\n\t\tconst response = await this.sendRequest({ id: 0, type: 'import', data })\n\t\tif (response.type === 'error') {\n\t\t\tthrow new AdapterError(`Import failed: ${response.message}`)\n\t\t}\n\t}\n\n\tprivate guardOpen(): void {\n\t\tif (!this.opened || !this.bridge) {\n\t\t\tthrow new StoreNotOpenError()\n\t\t}\n\t}\n\n\tprivate async sendRequest(request: WorkerRequest): Promise<WorkerResponse> {\n\t\t// guardOpen() is always called before sendRequest, so bridge is guaranteed non-null\n\t\tconst bridge = this.bridge\n\t\tif (!bridge) {\n\t\t\tthrow new StoreNotOpenError()\n\t\t}\n\t\treturn bridge.send(request)\n\t}\n\n\tprivate async sendChecked(request: WorkerRequest, description: string): Promise<void> {\n\t\tconst response = await this.sendRequest(request)\n\t\tif (response.type === 'error') {\n\t\t\tthrow new AdapterError(`${description} failed: ${response.message}`)\n\t\t}\n\t}\n}\n\nfunction parseStorageOpenState(data: unknown): StorageOpenState | null {\n\tif (typeof data !== 'object' || data === null) {\n\t\treturn null\n\t}\n\tconst mode = data as {\n\t\tpersistent?: boolean\n\t\tfallbackReason?: StorageFallbackReason\n\t}\n\tif (typeof mode.persistent !== 'boolean') {\n\t\treturn null\n\t}\n\treturn {\n\t\tpersistent: mode.persistent,\n\t\tmode: mode.persistent ? 'opfs' : 'memory',\n\t\t...(mode.fallbackReason ? { fallbackReason: mode.fallbackReason } : {}),\n\t}\n}\n","/**\n * Multi-tab SQLite storage coordination via `navigator.locks` and `BroadcastChannel`.\n *\n * One tab holds the exclusive `kora-leader-${dbName}` lock and owns the SQLite worker.\n * Other tabs send worker RPC over a named broadcast channel.\n */\n\nimport type { WorkerBridge, WorkerRequest, WorkerResponse } from '../adapters/sqlite-wasm-channel'\nimport { NoLeaderError, WorkerTimeoutError } from '../errors'\n\nconst RPC_REQUEST = 'kora-worker-request'\nconst RPC_RESPONSE = 'kora-worker-response'\nconst CLIENT_LEAVE = 'kora-client-leave'\nconst LEADER_PING = 'kora-leader-ping'\nconst LEADER_PONG = 'kora-leader-pong'\n\n/** Default delay before a stalled follower RPC probes the leader for liveness. */\nconst DEFAULT_LIVENESS_PROBE_MS = 2000\n/** Default idle budget for a client that owns an open transaction span. */\nconst DEFAULT_TRANSACTION_IDLE_TIMEOUT_MS = 10_000\n\ninterface RpcRequestMessage {\n\ttype: typeof RPC_REQUEST\n\trequestId: string\n\tclientId: string\n\trequest: WorkerRequest\n}\n\ninterface RpcResponseMessage {\n\ttype: typeof RPC_RESPONSE\n\trequestId: string\n\tresponse: WorkerResponse\n}\n\ninterface ClientLeaveMessage {\n\ttype: typeof CLIENT_LEAVE\n\tclientId: string\n}\n\ninterface ReclaimingWorkerBridge extends WorkerBridge {\n\treclaimClient(clientId: string, reason: string): void\n}\n\nexport type TabStorageRole = 'leader' | 'follower'\n\nexport interface AcquireTabStorageOptions {\n\t/**\n\t * Invoked when a follower is promoted to leader because the previous leader\n\t * released the lock (its tab closed or crashed). The adapter uses this to\n\t * rebuild its bridge as a leader. Never fires for a tab that started as leader.\n\t */\n\tonPromote?: () => void\n}\n\nexport interface TabStorageSession {\n\trole: TabStorageRole\n\tchannelName: string\n\t/** Leader only: release the navigator lock when closing the database. */\n\treleaseLock?: () => Promise<void>\n\t/** Leader only: stop the broadcast RPC relay. */\n\tstopRelay?: () => void\n\t/**\n\t * Follower only: resolves when this tab has been promoted to leader (the old\n\t * leader released the lock). The adapter awaits nothing here directly; it reacts\n\t * via {@link AcquireTabStorageOptions.onPromote}. Present so callers can cancel\n\t * the promotion watch on close.\n\t */\n\tcancelPromotionWatch?: () => void\n}\n\n/**\n * Returns whether multi-tab coordination APIs exist in this runtime.\n */\nexport function isMultiTabStorageSupported(): boolean {\n\treturn (\n\t\ttypeof globalThis !== 'undefined' &&\n\t\ttypeof BroadcastChannel !== 'undefined' &&\n\t\ttypeof navigator !== 'undefined' &&\n\t\ttypeof navigator.locks?.request === 'function'\n\t)\n}\n\n/**\n * Resolve leader vs follower for a database name.\n * Without lock APIs, every instance is treated as leader (single-tab / Node).\n *\n * A follower additionally queues a blocking request for the same lock. When the\n * current leader releases it (its tab closed or crashed), the browser grants the\n * lock to this follower and {@link AcquireTabStorageOptions.onPromote} fires so the\n * adapter can rebuild itself as the new leader. This is the automatic failover\n * path; a leader that is merely throttled (backgrounded but alive) keeps the lock,\n * and followers stay resilient through the liveness probe on the RPC bridge.\n */\nexport async function acquireTabStorageSession(\n\tdbName: string,\n\toptions?: AcquireTabStorageOptions,\n): Promise<TabStorageSession> {\n\tconst channelName = `kora-storage-${dbName}`\n\tconst lockName = `kora-leader-${dbName}`\n\n\tif (!isMultiTabStorageSupported()) {\n\t\treturn { role: 'leader', channelName }\n\t}\n\n\treturn new Promise<TabStorageSession>((resolve) => {\n\t\tlet releaseHeld: (() => void) | undefined\n\n\t\tvoid navigator.locks.request(lockName, { mode: 'exclusive', ifAvailable: true }, (lock) => {\n\t\t\tif (lock === null) {\n\t\t\t\tresolve(startFollowerSession(channelName, lockName, options))\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tresolve({\n\t\t\t\trole: 'leader',\n\t\t\t\tchannelName,\n\t\t\t\treleaseLock: async () => {\n\t\t\t\t\treleaseHeld?.()\n\t\t\t\t},\n\t\t\t})\n\n\t\t\treturn new Promise<void>((release) => {\n\t\t\t\treleaseHeld = release\n\t\t\t})\n\t\t})\n\t})\n}\n\n/**\n * Builds a follower session and starts watching for promotion. The follower holds\n * a queued lock request; when it is finally granted (old leader gone), it becomes\n * the leader for the rest of its lifetime and notifies via `onPromote`.\n */\nfunction startFollowerSession(\n\tchannelName: string,\n\tlockName: string,\n\toptions?: AcquireTabStorageOptions,\n): TabStorageSession {\n\tlet promoted = false\n\tlet releasePromotedLock: (() => void) | undefined\n\tconst abort = new AbortController()\n\n\tvoid navigator.locks\n\t\t.request(lockName, { mode: 'exclusive', signal: abort.signal }, () => {\n\t\t\t// Reaching here means the previous leader released the lock and this tab\n\t\t\t// won it. Hold it (never-resolving promise) so this tab is now the leader.\n\t\t\tpromoted = true\n\t\t\toptions?.onPromote?.()\n\t\t\treturn new Promise<void>((release) => {\n\t\t\t\treleasePromotedLock = release\n\t\t\t})\n\t\t})\n\t\t.catch(() => {\n\t\t\t// AbortError when the tab closes before promotion. Nothing to do.\n\t\t})\n\n\treturn {\n\t\trole: 'follower',\n\t\tchannelName,\n\t\tcancelPromotionWatch: () => {\n\t\t\tif (promoted) {\n\t\t\t\treleasePromotedLock?.()\n\t\t\t} else {\n\t\t\t\tabort.abort()\n\t\t\t}\n\t\t},\n\t}\n}\n\n/**\n * Leader tab: forward follower RPC to the real worker bridge.\n */\nexport function startLeaderRpcRelay(channelName: string, bridge: WorkerBridge): () => void {\n\tconst channel = new BroadcastChannel(channelName)\n\n\tconst onMessage = (\n\t\tevent: MessageEvent<RpcRequestMessage | ClientLeaveMessage | { type: typeof LEADER_PING }>,\n\t): void => {\n\t\tconst data = event.data\n\t\t// Answer liveness probes immediately, without touching the worker, so a\n\t\t// follower can distinguish a slow-but-alive leader from an absent one.\n\t\tif (data?.type === LEADER_PING) {\n\t\t\tchannel.postMessage({ type: LEADER_PONG })\n\t\t\treturn\n\t\t}\n\t\tif (data?.type === CLIENT_LEAVE) {\n\t\t\tif (hasReclaimClient(bridge)) {\n\t\t\t\tbridge.reclaimClient(data.clientId, 'client-left')\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif (data?.type !== RPC_REQUEST) {\n\t\t\treturn\n\t\t}\n\n\t\tvoid bridge\n\t\t\t.send(data.request, data.clientId)\n\t\t\t.then((response) => {\n\t\t\t\tconst msg: RpcResponseMessage = {\n\t\t\t\t\ttype: RPC_RESPONSE,\n\t\t\t\t\trequestId: data.requestId,\n\t\t\t\t\tresponse,\n\t\t\t\t}\n\t\t\t\tchannel.postMessage(msg)\n\t\t\t})\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tconst message = error instanceof Error ? error.message : 'Worker RPC failed'\n\t\t\t\tconst msg: RpcResponseMessage = {\n\t\t\t\t\ttype: RPC_RESPONSE,\n\t\t\t\t\trequestId: data.requestId,\n\t\t\t\t\tresponse: {\n\t\t\t\t\t\tid: data.request.id,\n\t\t\t\t\t\ttype: 'error',\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\tcode: 'LEADER_RPC_ERROR',\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t\tchannel.postMessage(msg)\n\t\t\t})\n\t}\n\n\tchannel.addEventListener('message', onMessage)\n\treturn () => {\n\t\tchannel.removeEventListener('message', onMessage)\n\t\tchannel.close()\n\t}\n}\n\n/**\n * Follower tab: proxy {@link WorkerBridge} over BroadcastChannel to the leader.\n */\nexport class FollowerBroadcastBridge implements WorkerBridge {\n\tprivate readonly channel: BroadcastChannel\n\tprivate readonly clientId = createClientId()\n\tprivate readonly onPageHide = (): void => {\n\t\tthis.leaveLeader()\n\t}\n\tprivate readonly pending = new Map<\n\t\tstring,\n\t\t{ resolve: (r: WorkerResponse) => void; reject: (e: Error) => void }\n\t>()\n\tprivate readonly timeoutMs: number\n\tprivate readonly livenessProbeMs: number\n\tprivate terminated = false\n\n\tconstructor(channelName: string, timeoutMs = 30000, livenessProbeMs = DEFAULT_LIVENESS_PROBE_MS) {\n\t\tthis.timeoutMs = timeoutMs\n\t\tthis.livenessProbeMs = Math.min(livenessProbeMs, timeoutMs)\n\t\tthis.channel = new BroadcastChannel(channelName)\n\t\tif (typeof addEventListener === 'function') {\n\t\t\taddEventListener('pagehide', this.onPageHide)\n\t\t}\n\t\tthis.channel.addEventListener('message', (event: MessageEvent<RpcResponseMessage>) => {\n\t\t\tconst data = event.data\n\t\t\tif (data?.type !== RPC_RESPONSE) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst entry = this.pending.get(data.requestId)\n\t\t\tif (entry) {\n\t\t\t\tthis.pending.delete(data.requestId)\n\t\t\t\tentry.resolve(data.response)\n\t\t\t}\n\t\t})\n\t}\n\n\t/**\n\t * Readiness handshake: resolves `true` as soon as a live leader answers a ping,\n\t * or `false` if none answers within the budget. The adapter calls this before\n\t * its first RPC so a follower created before a leader relay is live retries the\n\t * handshake instead of firing into the void and waiting out the full timeout.\n\t */\n\tasync waitForLeader(timeoutMs = 3000, attempts = 3): Promise<boolean> {\n\t\tconst perAttempt = Math.max(50, Math.floor(timeoutMs / Math.max(1, attempts)))\n\t\tfor (let attempt = 0; attempt < attempts; attempt += 1) {\n\t\t\tif (this.terminated) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tif (await this.pingLeader(perAttempt)) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t\treturn false\n\t}\n\n\t/** Sends one liveness ping and resolves whether a leader answered in time. */\n\tprivate pingLeader(timeoutMs: number): Promise<boolean> {\n\t\tif (this.terminated) {\n\t\t\treturn Promise.resolve(false)\n\t\t}\n\t\treturn new Promise<boolean>((resolve) => {\n\t\t\tconst onPong = (event: MessageEvent<{ type: string }>): void => {\n\t\t\t\tif (event.data?.type === LEADER_PONG) {\n\t\t\t\t\tcleanup()\n\t\t\t\t\tresolve(true)\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst cleanup = (): void => {\n\t\t\t\tclearTimeout(timer)\n\t\t\t\tthis.channel.removeEventListener('message', onPong)\n\t\t\t}\n\t\t\tconst timer = setTimeout(() => {\n\t\t\t\tcleanup()\n\t\t\t\tresolve(false)\n\t\t\t}, timeoutMs)\n\t\t\tthis.channel.addEventListener('message', onPong)\n\t\t\tthis.channel.postMessage({ type: LEADER_PING })\n\t\t})\n\t}\n\n\tasync send(request: WorkerRequest): 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: 'Follower bridge terminated',\n\t\t\t\tcode: 'BRIDGE_TERMINATED',\n\t\t\t}\n\t\t}\n\n\t\tconst requestId = createClientId()\n\t\tconst msg: RpcRequestMessage = {\n\t\t\ttype: RPC_REQUEST,\n\t\t\trequestId,\n\t\t\tclientId: this.clientId,\n\t\t\trequest,\n\t\t}\n\n\t\treturn new Promise<WorkerResponse>((resolve, reject) => {\n\t\t\tconst settle = (fn: () => void): void => {\n\t\t\t\tclearTimeout(timer)\n\t\t\t\tclearTimeout(probe)\n\t\t\t\tthis.pending.delete(requestId)\n\t\t\t\tfn()\n\t\t\t}\n\n\t\t\tconst timer = setTimeout(() => {\n\t\t\t\tsettle(() => reject(new WorkerTimeoutError(`follower-rpc:${request.type}`, this.timeoutMs)))\n\t\t\t}, this.timeoutMs)\n\n\t\t\t// Liveness watchdog: if the request stalls, probe the leader. A confirmed\n\t\t\t// absent leader fails fast (NoLeaderError) instead of waiting out the full\n\t\t\t// timeout; a slow-but-alive leader keeps its remaining time.\n\t\t\tconst probe = setTimeout(() => {\n\t\t\t\tvoid this.pingLeader(this.livenessProbeMs).then((alive) => {\n\t\t\t\t\tif (!alive && this.pending.has(requestId)) {\n\t\t\t\t\t\tsettle(() => reject(new NoLeaderError(`follower-rpc:${request.type}`)))\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t}, this.livenessProbeMs)\n\n\t\t\tthis.pending.set(requestId, {\n\t\t\t\tresolve: (response) => settle(() => resolve(response)),\n\t\t\t\treject: (error) => settle(() => reject(error)),\n\t\t\t})\n\n\t\t\tthis.channel.postMessage(msg)\n\t\t})\n\t}\n\n\tterminate(): void {\n\t\tif (this.terminated) {\n\t\t\treturn\n\t\t}\n\t\tthis.terminated = true\n\t\tthis.leaveLeader()\n\t\tif (typeof removeEventListener === 'function') {\n\t\t\tremoveEventListener('pagehide', this.onPageHide)\n\t\t}\n\t\tthis.channel.close()\n\t\tfor (const [, entry] of this.pending) {\n\t\t\tentry.reject(new Error('Follower bridge terminated'))\n\t\t}\n\t\tthis.pending.clear()\n\t}\n\n\tprivate leaveLeader(): void {\n\t\ttry {\n\t\t\tconst message: ClientLeaveMessage = {\n\t\t\t\ttype: CLIENT_LEAVE,\n\t\t\t\tclientId: this.clientId,\n\t\t\t}\n\t\t\tthis.channel.postMessage(message)\n\t\t} catch {\n\t\t\t// Best effort: the serializer's idle rollback is the guaranteed backstop.\n\t\t}\n\t}\n}\n\ninterface QueuedWorkerRequest {\n\tclientId: string\n\trequest: WorkerRequest\n\tresolve: (response: WorkerResponse) => void\n\treject: (error: Error) => void\n}\n\n/**\n * Serializes one SQLite worker across the leader tab and all follower tabs.\n *\n * SQLite transactions are represented as multiple worker messages\n * (`begin`, one or more reads/writes, then `commit`/`rollback`). Per-tab mutexes\n * cannot protect that span because follower messages converge at the leader.\n * This bridge promotes the worker boundary into the serialization point, so no\n * other client can interleave while a client owns an active transaction.\n */\nexport class TransactionSerializingWorkerBridge implements WorkerBridge {\n\tprivate readonly inner: WorkerBridge\n\tprivate readonly leaderClientId = createClientId()\n\tprivate readonly transactionIdleTimeoutMs: number\n\tprivate queue: QueuedWorkerRequest[] = []\n\tprivate activeTransactionClient: string | null = null\n\tprivate abortedClients = new Set<string>()\n\tprivate transactionIdleTimer: ReturnType<typeof setTimeout> | null = null\n\tprivate reclaiming = false\n\tprivate processing = false\n\tprivate terminated = false\n\n\tconstructor(inner: WorkerBridge, transactionIdleTimeoutMs = DEFAULT_TRANSACTION_IDLE_TIMEOUT_MS) {\n\t\tthis.inner = inner\n\t\tthis.transactionIdleTimeoutMs = transactionIdleTimeoutMs\n\t}\n\n\tsend(request: WorkerRequest, clientId = this.leaderClientId): Promise<WorkerResponse> {\n\t\tif (this.terminated) {\n\t\t\treturn Promise.resolve({\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\t\tif (this.abortedClients.has(clientId)) {\n\t\t\tif (request.type === 'begin') {\n\t\t\t\tthis.abortedClients.delete(clientId)\n\t\t\t} else {\n\t\t\t\treturn Promise.resolve({\n\t\t\t\t\tid: request.id,\n\t\t\t\t\ttype: 'error',\n\t\t\t\t\tmessage: 'Previous transaction was aborted because the client stopped sending requests.',\n\t\t\t\t\tcode: 'TRANSACTION_ABORTED',\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\n\t\treturn new Promise<WorkerResponse>((resolve, reject) => {\n\t\t\tthis.queue.push({ clientId, request, resolve, reject })\n\t\t\tvoid this.processQueue()\n\t\t})\n\t}\n\n\tterminate(): void {\n\t\tif (this.terminated) {\n\t\t\treturn\n\t\t}\n\t\tthis.terminated = true\n\t\tthis.clearTransactionIdleTimer()\n\t\tthis.inner.terminate()\n\t\tconst pending = this.queue.splice(0)\n\t\tfor (const entry of pending) {\n\t\t\tentry.reject(new Error('Worker terminated'))\n\t\t}\n\t}\n\n\treclaimClient(clientId: string, reason: string): void {\n\t\tif (this.terminated) {\n\t\t\treturn\n\t\t}\n\t\tvoid this.reclaimClientNow(clientId, reason)\n\t}\n\n\tprivate async processQueue(): Promise<void> {\n\t\tif (this.processing || this.reclaiming) {\n\t\t\treturn\n\t\t}\n\t\tthis.processing = true\n\t\ttry {\n\t\t\twhile (!this.terminated) {\n\t\t\t\tconst index = this.nextRunnableIndex()\n\t\t\t\tif (index === -1) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tconst [entry] = this.queue.splice(index, 1)\n\t\t\t\tif (!entry) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await this.inner.send(entry.request)\n\t\t\t\t\tthis.recordTransactionState(entry, response)\n\t\t\t\t\tentry.resolve(response)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis.resetFailedTransaction(entry)\n\t\t\t\t\tentry.reject(error instanceof Error ? error : new Error(String(error)))\n\t\t\t\t}\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.processing = false\n\t\t\tif (!this.terminated && !this.reclaiming && this.nextRunnableIndex() !== -1) {\n\t\t\t\tvoid this.processQueue()\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate nextRunnableIndex(): number {\n\t\tif (this.queue.length === 0) {\n\t\t\treturn -1\n\t\t}\n\t\tif (this.activeTransactionClient === null) {\n\t\t\treturn 0\n\t\t}\n\t\treturn this.queue.findIndex((entry) => entry.clientId === this.activeTransactionClient)\n\t}\n\n\tprivate recordTransactionState(entry: QueuedWorkerRequest, response: WorkerResponse): void {\n\t\tif (response.type === 'error') {\n\t\t\tthis.resetFailedTransaction(entry)\n\t\t\treturn\n\t\t}\n\t\tif (entry.request.type === 'begin') {\n\t\t\tthis.activeTransactionClient = entry.clientId\n\t\t\tthis.armTransactionIdleTimer(entry.clientId)\n\t\t\treturn\n\t\t}\n\t\tif (this.activeTransactionClient === entry.clientId) {\n\t\t\tthis.armTransactionIdleTimer(entry.clientId)\n\t\t}\n\t\tif (\n\t\t\tthis.activeTransactionClient === entry.clientId &&\n\t\t\t(entry.request.type === 'commit' || entry.request.type === 'rollback')\n\t\t) {\n\t\t\tthis.activeTransactionClient = null\n\t\t\tthis.clearTransactionIdleTimer()\n\t\t}\n\t}\n\n\tprivate resetFailedTransaction(entry: QueuedWorkerRequest): void {\n\t\tif (this.activeTransactionClient === entry.clientId) {\n\t\t\tthis.activeTransactionClient = null\n\t\t\tthis.clearTransactionIdleTimer()\n\t\t}\n\t}\n\n\tprivate armTransactionIdleTimer(clientId: string): void {\n\t\tthis.clearTransactionIdleTimer()\n\t\tthis.transactionIdleTimer = setTimeout(() => {\n\t\t\tvoid this.reclaimClientNow(clientId, 'transaction-idle-timeout')\n\t\t}, this.transactionIdleTimeoutMs)\n\t}\n\n\tprivate clearTransactionIdleTimer(): void {\n\t\tif (this.transactionIdleTimer) {\n\t\t\tclearTimeout(this.transactionIdleTimer)\n\t\t\tthis.transactionIdleTimer = null\n\t\t}\n\t}\n\n\tprivate async reclaimClientNow(clientId: string, reason: string): Promise<void> {\n\t\tif (this.reclaiming || this.activeTransactionClient !== clientId) {\n\t\t\treturn\n\t\t}\n\t\tthis.reclaiming = true\n\t\tthis.clearTransactionIdleTimer()\n\t\tthis.activeTransactionClient = null\n\t\tthis.abortedClients.add(clientId)\n\t\tthis.rejectQueuedClientRequests(clientId, reason)\n\n\t\ttry {\n\t\t\tconst response = await this.inner.send({ id: 0, type: 'rollback' })\n\t\t\tif (response.type === 'error') {\n\t\t\t\t// If there was no active SQLite transaction left, the important state\n\t\t\t\t// is already reclaimed in JS. The next request will establish a fresh\n\t\t\t\t// transaction if needed.\n\t\t\t}\n\t\t} catch {\n\t\t\t// Keep the queue moving. The inner worker will surface any unrecoverable\n\t\t\t// state through subsequent requests.\n\t\t} finally {\n\t\t\tthis.reclaiming = false\n\t\t\tif (!this.terminated && this.nextRunnableIndex() !== -1) {\n\t\t\t\tvoid this.processQueue()\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate rejectQueuedClientRequests(clientId: string, reason: string): void {\n\t\tconst keep: QueuedWorkerRequest[] = []\n\t\tfor (const entry of this.queue) {\n\t\t\tif (entry.clientId === clientId) {\n\t\t\t\tentry.resolve({\n\t\t\t\t\tid: entry.request.id,\n\t\t\t\t\ttype: 'error',\n\t\t\t\t\tmessage: `Transaction client was reclaimed: ${reason}`,\n\t\t\t\t\tcode: 'TRANSACTION_ABORTED',\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tkeep.push(entry)\n\t\t\t}\n\t\t}\n\t\tthis.queue = keep\n\t}\n}\n\nfunction hasReclaimClient(bridge: WorkerBridge): bridge is ReclaimingWorkerBridge {\n\treturn typeof (bridge as Partial<ReclaimingWorkerBridge>).reclaimClient === 'function'\n}\n\nfunction createClientId(): string {\n\tif (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {\n\t\treturn crypto.randomUUID()\n\t}\n\treturn `${Date.now()}-${Math.random().toString(36).slice(2)}`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA,iBAgDa,cAUA,mBAoBA,oBAiBA;AA/Fb;AAAA;AAAA;AAAA,kBAA0B;AAgDnB,IAAM,eAAN,cAA2B,sBAAU;AAAA,MAC3C,YAAY,SAAiB,SAAmC;AAC/D,cAAM,SAAS,iBAAiB,OAAO;AACvC,aAAK,OAAO;AAAA,MACb;AAAA,IACD;AAKO,IAAM,oBAAN,cAAgC,sBAAU;AAAA,MAChD,cAAc;AACb,cAAM,sEAAsE,gBAAgB;AAC5F,aAAK,OAAO;AAAA,MACb;AAAA,IACD;AAeO,IAAM,qBAAN,cAAiC,sBAAU;AAAA,MACjD,YAAY,WAAmB,WAAmB;AACjD;AAAA,UACC,iCAAiC,SAAS,qBAAqB,SAAS;AAAA,UACxE;AAAA,UACA,EAAE,WAAW,UAAU;AAAA,QACxB;AACA,aAAK,OAAO;AAAA,MACb;AAAA,IACD;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E,UAAM,SAAS,KAAK;AACpB,QAAI,CAAC,QAAQ;AACZ,YAAM,IAAI,kBAAkB;AAAA,IAC7B;AACA,WAAO,OAAO,KAAK,OAAO;AAAA,EAC3B;AAAA,EAEA,MAAc,YAAY,SAAwB,aAAoC;AACrF,UAAM,WAAW,MAAM,KAAK,YAAY,OAAO;AAC/C,QAAI,SAAS,SAAS,SAAS;AAC9B,YAAM,IAAI,aAAa,GAAG,WAAW,YAAY,SAAS,OAAO,EAAE;AAAA,IACpE;AAAA,EACD;AACD;AAEA,SAAS,sBAAsB,MAAwC;AACtE,MAAI,OAAO,SAAS,YAAY,SAAS,MAAM;AAC9C,WAAO;AAAA,EACR;AACA,QAAM,OAAO;AAIb,MAAI,OAAO,KAAK,eAAe,WAAW;AACzC,WAAO;AAAA,EACR;AACA,SAAO;AAAA,IACN,YAAY,KAAK;AAAA,IACjB,MAAM,KAAK,aAAa,SAAS;AAAA,IACjC,GAAI,KAAK,iBAAiB,EAAE,gBAAgB,KAAK,eAAe,IAAI,CAAC;AAAA,EACtE;AACD;;;ADrbA;","names":["import_core","WebWorkerBridge"]}
|
|
1
|
+
{"version":3,"sources":["../../src/errors.ts","../../src/adapters/mutex.ts","../../src/adapters/sqlite-wasm-channel.ts","../../src/adapters/sqlite-wasm.ts","../../src/adapters/sqlite-wasm-adapter.ts","../../src/adapters/opfs-names.ts","../../src/multi-tab/tab-storage.ts","../../src/adapters/database-dump.ts","../../src/adapters/sqlite-wasm-persistence.ts","../../src/adapters/storage-manifest.ts","../../src/adapters/local-databases.ts"],"sourcesContent":["import { KoraError } from '@korajs/core'\n\n/**\n * Thrown when a query is invalid (bad field names, invalid operators, etc.).\n */\nexport class QueryError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'QUERY_ERROR', context)\n\t\tthis.name = 'QueryError'\n\t}\n}\n\n/**\n * Thrown when a record is not found by ID (findById, update, delete on missing record).\n */\nexport class RecordNotFoundError extends KoraError {\n\tconstructor(collection: string, recordId: string) {\n\t\tsuper(`Record \"${recordId}\" not found in collection \"${collection}\"`, 'RECORD_NOT_FOUND', {\n\t\t\tcollection,\n\t\t\trecordId,\n\t\t})\n\t\tthis.name = 'RecordNotFoundError'\n\t}\n}\n\n/**\n * Thrown inside `applyRemoteOperation` when the row's version state changed\n * between the caller's snapshot read and the guarded write (a concurrent local\n * mutation landed in the window). The transaction is rolled back with nothing\n * written; the caller re-reads fresh state, recomputes its merge, and retries.\n * This is the optimistic-concurrency guard that keeps merge-engine results\n * (richtext, add-wins arrays, constraint resolutions) from clobbering newer\n * local edits they never saw.\n */\nexport class OptimisticLockError extends KoraError {\n\tconstructor(collection: string, recordId: string) {\n\t\tsuper(\n\t\t\t`Row version changed while merging record \"${recordId}\" in \"${collection}\"; retry with fresh state`,\n\t\t\t'OPTIMISTIC_LOCK',\n\t\t\t{ collection, recordId },\n\t\t)\n\t\tthis.name = 'OptimisticLockError'\n\t}\n}\n\n/**\n * Thrown when a storage adapter operation fails.\n */\nexport class AdapterError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'ADAPTER_ERROR', context)\n\t\tthis.name = 'AdapterError'\n\t}\n}\n\n/**\n * Thrown when a write reaches a storage adapter that could not obtain durable\n * storage (it opened, or was promoted to leader, on a non-persistent in-memory\n * database). Kora refuses such writes instead of accepting data that would be\n * lost on reload. Reads still work.\n *\n * Fix: close other tabs or apps using the same origin's OPFS storage and reopen,\n * or opt in explicitly with `allowNonDurable: true` if in-memory storage is\n * acceptable for this app.\n */\nexport class StorageDurabilityError extends KoraError {\n\tconstructor(dbName: string, phase: 'open' | 'promotion', reason: string) {\n\t\tsuper(\n\t\t\t`Database \"${dbName}\" has no durable storage (${reason} during ${phase}); writes are refused so they are not silently lost on reload. Close other tabs or apps using this origin's storage and reload, or set allowNonDurable: true to accept in-memory storage.`,\n\t\t\t'STORAGE_DURABILITY_LOST',\n\t\t\t{ dbName, phase, reason },\n\t\t)\n\t\tthis.name = 'StorageDurabilityError'\n\t}\n}\n\n/**\n * Thrown when the store opens a database a NEWER build of the app already migrated: its\n * stored schema version is above this code's `schema.version` (RT-109). Running older\n * code against it would write operations of the old schema into the new one, or fail\n * part-way, so the store refuses before changing anything and emits `store:schema-ahead`.\n *\n * Fix: run the newer build (reload while online, or accept the app's update prompt).\n * Never lower `schema.version` in a deployed app.\n */\nexport class SchemaVersionAheadError extends KoraError {\n\tconstructor(\n\t\tpublic readonly dbName: string,\n\t\tpublic readonly storedVersion: number,\n\t\tpublic readonly codeVersion: number,\n\t) {\n\t\tsuper(\n\t\t\t`Database \"${dbName}\" is at schema version ${storedVersion}, newer than this build's schema version ${codeVersion}: a newer build of the app already migrated it. It was not opened and nothing in it changed. Run the newer build (reload while online, or accept the update); never lower schema.version in a deployed app.`,\n\t\t\t'SCHEMA_VERSION_AHEAD',\n\t\t\t{ dbName, storedVersion, codeVersion },\n\t\t)\n\t\tthis.name = 'SchemaVersionAheadError'\n\t}\n}\n\n/**\n * Thrown when an operation is attempted on a store that has not been opened.\n */\nexport class StoreNotOpenError extends KoraError {\n\tconstructor() {\n\t\tsuper('Store is not open. Call store.open() before performing operations.', 'STORE_NOT_OPEN')\n\t\tthis.name = 'StoreNotOpenError'\n\t}\n}\n\n/**\n * Thrown when the Web Worker fails to initialize (WASM load failure, OPFS unavailable, etc.).\n */\nexport class WorkerInitError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(`Worker initialization failed: ${message}`, 'WORKER_INIT_ERROR', context)\n\t\tthis.name = 'WorkerInitError'\n\t}\n}\n\n/**\n * Thrown when the Web Worker does not respond within the configured timeout.\n */\nexport class WorkerTimeoutError extends KoraError {\n\tconstructor(operation: string, timeoutMs: number) {\n\t\tsuper(\n\t\t\t`Worker did not respond within ${timeoutMs}ms for operation \"${operation}\"`,\n\t\t\t'WORKER_TIMEOUT',\n\t\t\t{ operation, timeoutMs },\n\t\t)\n\t\tthis.name = 'WorkerTimeoutError'\n\t}\n}\n\n/**\n * Thrown when a follower tab cannot reach a live leader tab over the multi-tab\n * broadcast channel (the leader tab was closed, crashed, or is unresponsive).\n * Distinct from {@link WorkerTimeoutError}: this fails fast on a confirmed-absent\n * leader instead of waiting out the full RPC timeout.\n */\nexport class NoLeaderError extends KoraError {\n\tconstructor(\n\t\toperation: string,\n\t\tmessage = `No live leader tab is answering multi-tab storage RPC for operation \"${operation}\"`,\n\t\tcode = 'NO_LEADER',\n\t\tcontext: Record<string, unknown> = {},\n\t) {\n\t\tsuper(message, code, { operation, ...context })\n\t\tthis.name = 'NoLeaderError'\n\t}\n}\n\n/**\n * Thrown to a follower tab's pending storage requests when the leader tab that\n * was answering stopped sending heartbeats (hung, frozen, or suspended). The\n * request may or may not have been applied: retry it with the same `requestId`\n * (in the error context) and the leader de-duplicates it. Retriable.\n */\nexport class LeaderUnresponsiveError extends NoLeaderError {\n\tconstructor(operation: string, silentMs: number, requestId: string) {\n\t\tsuper(\n\t\t\toperation,\n\t\t\t`The leader tab stopped responding (no heartbeat for ${silentMs}ms) during \"${operation}\". The request may or may not have been applied; retry it with the same requestId and the leader de-duplicates it.`,\n\t\t\t'LEADER_UNRESPONSIVE',\n\t\t\t{ silentMs, requestId, retriable: true },\n\t\t)\n\t\tthis.name = 'LeaderUnresponsiveError'\n\t}\n}\n\n/**\n * Thrown to pending storage requests when their bridge was torn down, typically\n * because this tab was just promoted to leader or the leader handed over.\n * Retriable: the next attempt goes to the new leader.\n */\nexport class BridgeTerminatedError extends KoraError {\n\tconstructor(operation: string, reason: string) {\n\t\tsuper(\n\t\t\t`Storage request \"${operation}\" was interrupted (${reason}); retry it against the new storage leader.`,\n\t\t\t'BRIDGE_TERMINATED',\n\t\t\t{ operation, reason, retriable: true },\n\t\t)\n\t\tthis.name = 'BridgeTerminatedError'\n\t}\n}\n\n/** Thrown when a storage request is cancelled through its `AbortSignal`. */\nexport class RequestAbortedError extends KoraError {\n\tconstructor(operation: string, requestId: string) {\n\t\tsuper(\n\t\t\t`Storage request \"${operation}\" was aborted by the caller. A write may already have reached the leader; retry with the same requestId to stay idempotent.`,\n\t\t\t'REQUEST_ABORTED',\n\t\t\t{ operation, requestId },\n\t\t)\n\t\tthis.name = 'RequestAbortedError'\n\t}\n}\n\n/**\n * Thrown by `deleteDatabase()` when the database is open in some tab or worker\n * on this origin. Close it everywhere (or call `app.close()`) and retry.\n */\nexport class StorageInUseError extends KoraError {\n\tconstructor(dbName: string) {\n\t\tsuper(\n\t\t\t`Database \"${dbName}\" is open in a tab on this origin; close it before deleting it.`,\n\t\t\t'STORAGE_IN_USE',\n\t\t\t{ dbName },\n\t\t)\n\t\tthis.name = 'StorageInUseError'\n\t}\n}\n\n/**\n * Thrown by `deleteDatabase()` when the database still holds operations that\n * never reached the server. Sync first, or pass `{ force: true }` to discard them.\n */\nexport class UnsyncedDataError extends KoraError {\n\tconstructor(dbName: string) {\n\t\tsuper(\n\t\t\t`Database \"${dbName}\" has unsynced operations; deleting it would lose them. Sync first, or pass { force: true } to discard them deliberately.`,\n\t\t\t'UNSYNCED_DATA',\n\t\t\t{ dbName },\n\t\t)\n\t\tthis.name = 'UnsyncedDataError'\n\t}\n}\n\n/**\n * Thrown when a database's data lives in a storage backend this runtime cannot\n * read (for example it was written to OPFS, and OPFS is unavailable now). Kora\n * refuses to start an empty, disjoint copy rather than hide the existing data.\n */\nexport class StorageBackendMismatchError extends KoraError {\n\tconstructor(dbName: string, recorded: string, requested: string, reason: string) {\n\t\tsuper(\n\t\t\t`Database \"${dbName}\" is stored in ${recorded}, but this session can only use ${requested} (${reason}). Kora will not start a separate empty copy; reload in a browser profile where ${recorded} is available.`,\n\t\t\t'STORAGE_BACKEND_MISMATCH',\n\t\t\t{ dbName, recorded, requested, reason },\n\t\t)\n\t\tthis.name = 'StorageBackendMismatchError'\n\t}\n}\n\n/**\n * Thrown when IndexedDB persistence operations fail (serialize/deserialize).\n */\nexport class PersistenceError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(`Persistence error: ${message}`, 'PERSISTENCE_ERROR', context)\n\t\tthis.name = 'PersistenceError'\n\t}\n}\n","/**\n * Async mutex for serializing access to a resource across `await` boundaries.\n *\n * This exists because an adapter's `transaction()` is async (its callback may\n * await), but the underlying database transaction (BEGIN…COMMIT) is not\n * reentrant: starting a second transaction while one is open is an error. When\n * a client applies a relayed remote operation while a local write is in flight,\n * the two `transaction()` calls interleave at their `await` points, and without\n * serialization the second `BEGIN` collides with the first, silently dropping\n * the operation. Serializing every transaction through this mutex guarantees at\n * most one is active at a time.\n */\nexport class Mutex {\n\tprivate locked = false\n\tprivate readonly waiters: Array<() => void> = []\n\n\t/**\n\t * Acquire the mutex. Resolves with a release function once the lock is held.\n\t * If the mutex is already held, the caller waits (FIFO) until it is released.\n\t */\n\tasync acquire(): Promise<() => void> {\n\t\tif (!this.locked) {\n\t\t\tthis.locked = true\n\t\t\treturn this.createRelease()\n\t\t}\n\n\t\treturn new Promise<() => void>((resolve) => {\n\t\t\tthis.waiters.push(() => {\n\t\t\t\tresolve(this.createRelease())\n\t\t\t})\n\t\t})\n\t}\n\n\tprivate createRelease(): () => void {\n\t\tlet released = false\n\t\treturn () => {\n\t\t\tif (released) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\treleased = true\n\t\t\tconst next = this.waiters.shift()\n\t\t\tif (next) {\n\t\t\t\tnext()\n\t\t\t} else {\n\t\t\t\tthis.locked = false\n\t\t\t}\n\t\t}\n\t}\n}\n","/// <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/**\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 & { 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 })\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\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 real Web Worker via MessagePort.\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\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","// Entry point for @korajs/store/sqlite-wasm\nexport { SqliteWasmAdapter } from './sqlite-wasm-adapter'\nexport type { SqliteWasmAdapterOptions, StorageRequestOptions } from './sqlite-wasm-adapter'\nexport { WebWorkerBridge, Mutex } from './sqlite-wasm-channel'\nexport type {\n\tWorkerBridge,\n\tWorkerRequest,\n\tWorkerResponse,\n\tWorkerSendOptions,\n\tWorkerStatusEvent,\n} from './sqlite-wasm-channel'\nexport { deleteLocalDatabase, listLocalDatabases } from './local-databases'\nexport type { DeleteLocalDatabaseOptions, LocalDatabaseReader } from './local-databases'\nexport type { LocalDatabaseBackend, LocalDatabaseRecord } from './storage-manifest'\nexport { opfsPoolNameFor } from './opfs-names'\n","import { generateFullDDL } from '@korajs/core'\nimport type { KoraEventEmitter, SchemaDefinition } from '@korajs/core'\nimport {\n\tAdapterError,\n\tBridgeTerminatedError,\n\tStorageDurabilityError,\n\tStoreNotOpenError,\n} from '../errors'\nimport {\n\tFollowerBroadcastBridge,\n\ttype TabStorageSession,\n\tacquireTabStorageSession,\n} from '../multi-tab/tab-storage'\nimport type {\n\tMigrationPlan,\n\tStorageAdapter,\n\tStorageFallbackReason,\n\tStorageOpenState,\n\tTransaction,\n} from '../types'\nimport { restoreDumpStatements } from './database-dump'\nimport type { DatabaseDump } from './database-dump'\nimport { Mutex } from './sqlite-wasm-channel'\nimport type {\n\tWebWorkerBridge,\n\tWorkerBridge,\n\tWorkerRequest,\n\tWorkerResponse,\n\tWorkerSendOptions,\n\tWorkerStatusEvent,\n} from './sqlite-wasm-channel'\nimport { deleteFromIndexedDB, loadDumpFromIndexedDB } from './sqlite-wasm-persistence'\nimport { isManifestAvailable, readManifestRecord, recordDatabase } from './storage-manifest'\n\ntype WorkerSuccessResponse = Extract<WorkerResponse, { type: 'success' }>\nlet warnedSharedWorkerDeprecated = false\n\n/** Per-request options for {@link SqliteWasmAdapter.execute} and `query`. */\nexport interface StorageRequestOptions {\n\t/**\n\t * Stop waiting for the response. The leader may still apply a write that\n\t * already reached it; retry with the same `requestId` to stay idempotent.\n\t */\n\tsignal?: AbortSignal\n\t/**\n\t * Stable id for this request. A follower tab's retry with the same id is\n\t * answered from the leader's response cache instead of being applied twice.\n\t */\n\trequestId?: string\n}\n\n/**\n * Options for creating a SqliteWasmAdapter.\n */\nexport interface SqliteWasmAdapterOptions {\n\t/**\n\t * Injected WorkerBridge for testing. If omitted, a WebWorkerBridge is created\n\t * in browser environments.\n\t */\n\tbridge?: WorkerBridge\n\n\t/**\n\t * Database name for persistence. Each database gets its own OPFS pool.\n\t */\n\tdbName?: string\n\n\t/**\n\t * URL to the sqlite-wasm-worker script. Required in browsers if no bridge is provided.\n\t */\n\tworkerUrl?: string | URL\n\n\t/** Timeout for worker / follower RPC responses. Defaults to 30000ms. */\n\tworkerResponseTimeoutMs?: number\n\n\t/**\n\t * @deprecated SharedWorker-hosted SQLite cannot use OPFS SyncAccessHandle and\n\t * is never durable. Kora ignores this option and uses the dedicated-worker\n\t * leader/follower path for durable multi-tab storage.\n\t */\n\tsharedWorkerUrl?: string | URL\n\n\t/**\n\t * When false, the adapter records a non-persistent open without emitting\n\t * `store:opfs-unavailable`. `createApp()` uses this while it promotes the store\n\t * to durable IndexedDB; direct adapter users keep the diagnostic by default.\n\t */\n\temitNonPersistentDiagnostic?: boolean\n\n\t/**\n\t * Accept writes when durable OPFS storage could not be obtained and the\n\t * database runs in memory. Defaults to false: a non-durable open or promotion\n\t * emits the blocking `store:durability-lost` event and every write is refused\n\t * with {@link StorageDurabilityError}, so data is never accepted and then lost\n\t * on reload. Set true only for apps that can tolerate losing local writes.\n\t */\n\tallowNonDurable?: boolean\n\n\t/**\n\t * For `createApp()`: skip the durability check at open only, because the caller\n\t * inspects {@link SqliteWasmAdapter.getStorageOpenState} right after open and\n\t * switches to a durable backend itself. A later leader promotion that loses\n\t * durable storage is still enforced.\n\t */\n\tdeferOpenDurabilityCheck?: boolean\n\n\t/**\n\t * Release storage ownership when the page is frozen or hidden into the\n\t * back/forward cache (`freeze` / `pagehide`), so a visible tab can take over\n\t * instead of waiting on a tab that cannot run; ownership is re-acquired on\n\t * `resume` / `pageshow`. Defaults to true.\n\t */\n\treleaseOnFreeze?: boolean\n\n\t/**\n\t * When set, storage diagnostics are emitted here: `store:opfs-unavailable`,\n\t * `store:durability-lost`, `store:storage-blocked` (another holder has the\n\t * storage; the open waits), `store:storage-migrated` and\n\t * `store:db-name-collision`.\n\t */\n\temitter?: KoraEventEmitter\n\n\t/**\n\t * @internal Used by the IndexedDB adapter: open an in-memory database that\n\t * never touches OPFS (the IndexedDB adapter persists snapshots itself).\n\t */\n\tstorage?: 'opfs' | 'memory'\n\n\t/**\n\t * @internal Do not read or write the storage manifest (the IndexedDB adapter's\n\t * inner database and its one-shot OPFS reader manage the manifest themselves).\n\t */\n\tskipBackendRecord?: boolean\n\n\t/**\n\t * @internal Used by the IndexedDB adapter (STORE-6): runs in one transaction on this\n\t * tab's OWN worker right after it opened the database as storage leader (first open\n\t * or promotion), before the worker serves any other tab. The IndexedDB adapter\n\t * restores its persisted snapshot here, so a snapshot is only ever restored into a\n\t * freshly created worker database and never over a leader's live one. Throwing fails\n\t * the open (or the promotion, which then refuses writes).\n\t */\n\tonLeaderWorkerOpened?: (tx: Transaction) => Promise<void>\n}\n\ninterface Gate {\n\tpromise: Promise<void>\n\topen(): void\n}\n\nfunction createGate(): Gate {\n\tlet open: () => void = () => {}\n\tconst promise = new Promise<void>((resolve) => {\n\t\topen = resolve\n\t})\n\treturn { promise, open }\n}\n\n/**\n * SQLite WASM adapter that communicates with a SQLite instance through a WorkerBridge.\n *\n * In browsers, one tab per database (the leader) runs a dedicated worker that\n * owns the database's OPFS pool; other tabs reach that worker over a\n * BroadcastChannel. In Node.js tests, the bridge is backed by MockWorkerBridge\n * wrapping better-sqlite3.\n *\n * Storage is durable or loud, never silently in memory (W8a): another holder of\n * the storage makes the open wait (reported with `store:storage-blocked`); only a\n * runtime without OPFS SAH support gets a non-durable database, which refuses\n * writes unless the app opted in with `allowNonDurable`.\n *\n * @example\n * ```typescript\n * // Browser usage\n * const adapter = new SqliteWasmAdapter({ workerUrl: '/sqlite-wasm-worker.js' })\n *\n * // Test usage with MockWorkerBridge\n * import { MockWorkerBridge } from './sqlite-wasm-mock-bridge'\n * const adapter = new SqliteWasmAdapter({ bridge: new MockWorkerBridge() })\n * ```\n */\nexport class SqliteWasmAdapter implements StorageAdapter {\n\tprivate bridge: WorkerBridge | null = null\n\t/** The dedicated worker this tab runs while it is the database's leader. */\n\tprivate ownWorker: WebWorkerBridge | null = null\n\tprivate opened = false\n\tprivate readonly mutex = new Mutex()\n\tprivate readonly injectedBridge: WorkerBridge | undefined\n\tprivate readonly workerUrl: string | URL | undefined\n\tprivate readonly sharedWorkerUrl: string | URL | undefined\n\tprivate readonly workerResponseTimeoutMs: number\n\tprivate readonly dbName: string\n\tprivate readonly emitter: KoraEventEmitter | undefined\n\tprivate tabSession: TabStorageSession | null = null\n\tprivate readonly emitNonPersistentDiagnostic: boolean\n\tprivate storageOpenState: StorageOpenState | null = null\n\t/** Retained so a follower promoted to leader can re-open its own worker. */\n\tprivate schema: SchemaDefinition | null = null\n\tprivate promoting = false\n\tprivate closing = false\n\tprivate suspended = false\n\t/** Requests wait here while leadership changes hands (promotion, resume). */\n\tprivate gate: Gate | null = null\n\tprivate removeLifecycleListeners: (() => void) | null = null\n\tprivate readonly allowNonDurable: boolean\n\tprivate readonly deferOpenDurabilityCheck: boolean\n\tprivate readonly releaseOnFreeze: boolean\n\tprivate readonly storage: 'opfs' | 'memory'\n\tprivate readonly skipBackendRecord: boolean\n\t/** Set when this adapter lost (or never had) durable storage; writes are refused. */\n\tprivate durabilityLoss: { phase: 'open' | 'promotion'; reason: string } | null = null\n\tprivate readonly onLeaderWorkerOpened: ((tx: Transaction) => Promise<void>) | undefined\n\n\tconstructor(options: SqliteWasmAdapterOptions = {}) {\n\t\tthis.injectedBridge = options.bridge\n\t\tthis.workerUrl = options.workerUrl\n\t\tthis.sharedWorkerUrl = options.sharedWorkerUrl\n\t\tthis.workerResponseTimeoutMs = options.workerResponseTimeoutMs ?? 30_000\n\t\tthis.dbName = options.dbName ?? 'kora-db'\n\t\tthis.emitter = options.emitter\n\t\tthis.emitNonPersistentDiagnostic = options.emitNonPersistentDiagnostic ?? true\n\t\tthis.allowNonDurable = options.allowNonDurable ?? false\n\t\tthis.deferOpenDurabilityCheck = options.deferOpenDurabilityCheck ?? false\n\t\tthis.releaseOnFreeze = options.releaseOnFreeze ?? true\n\t\tthis.storage = options.storage ?? 'opfs'\n\t\tthis.skipBackendRecord = options.skipBackendRecord ?? false\n\t\tthis.onLeaderWorkerOpened = options.onLeaderWorkerOpened\n\t}\n\n\tasync open(schema: SchemaDefinition): Promise<void> {\n\t\tif (this.opened) return\n\n\t\tconst ddlStatements = generateFullDDL(schema)\n\t\tif (this.injectedBridge) {\n\t\t\tthis.bridge = this.injectedBridge\n\t\t\tconst response = await this.openCurrentBridge(ddlStatements)\n\t\t\tthis.reportStorageMode(response.data)\n\t\t\tthis.enforceDurability('open')\n\t\t\tthis.opened = true\n\t\t\treturn\n\t\t}\n\t\tif (this.workerUrl) {\n\t\t\tthis.warnSharedWorkerDeprecated()\n\t\t\tthis.schema = schema\n\t\t\ttry {\n\t\t\t\tconst response = await this.attachAndOpen(ddlStatements)\n\t\t\t\tthis.reportStorageMode(response.data)\n\t\t\t\tthis.enforceDurability('open')\n\t\t\t\tawait this.afterLeaderOpen(response.data)\n\t\t\t\tthis.opened = true\n\t\t\t} catch (error) {\n\t\t\t\t// A failed open must not keep the worker, the OPFS pool or the leader\n\t\t\t\t// lock for the rest of the page's life (NEW-STORE-8).\n\t\t\t\tawait this.teardown()\n\t\t\t\tthrow error\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tthrow new AdapterError(\n\t\t\t'SqliteWasmAdapter requires either a bridge (for testing) or a workerUrl (for browsers). ' +\n\t\t\t\t'Pass { bridge: new MockWorkerBridge() } for tests, or { workerUrl: \"/worker.js\" } for browsers.',\n\t\t)\n\t}\n\n\t/**\n\t * Join the database as leader (own worker) or follower (RPC to the leader's\n\t * worker), then open it. The open waits, without a timeout, while another\n\t * holder still has the storage; the worker reports that as a blocking state.\n\t */\n\tprivate async attachAndOpen(ddlStatements: string[]): Promise<WorkerSuccessResponse> {\n\t\tconst workerUrl = this.workerUrl\n\t\tif (!workerUrl) {\n\t\t\tthrow new AdapterError('Durable SQLite WASM storage requires workerUrl.')\n\t\t}\n\t\tconst session = await acquireTabStorageSession(this.dbName, {\n\t\t\tonPromote: () => {\n\t\t\t\tvoid this.promoteToLeader()\n\t\t\t},\n\t\t})\n\t\tthis.tabSession = session\n\n\t\tif (session.role === 'leader') {\n\t\t\tconst { WebWorkerBridge } = await import('./sqlite-wasm-channel')\n\t\t\tconst worker = new WebWorkerBridge(workerUrl, this.workerResponseTimeoutMs, {\n\t\t\t\tonEvent: (event) => this.onWorkerEvent(event),\n\t\t\t})\n\t\t\tthis.ownWorker = worker\n\t\t\tthis.bridge = worker\n\t\t\tconst response = await this.openLeaderWorker(worker, ddlStatements)\n\t\t\tthis.installLifecycleListeners()\n\t\t\treturn response\n\t\t}\n\n\t\tconst followerBridge = new FollowerBroadcastBridge(\n\t\t\tsession.channelName,\n\t\t\tthis.workerResponseTimeoutMs,\n\t\t)\n\t\tthis.bridge = followerBridge\n\t\t// Another runtime on this origin already owns this database name. That is\n\t\t// expected for multiple tabs of the same app (they share one leader),\n\t\t// but a bug if these are logically separate apps, so surface it so the\n\t\t// developer can give them distinct store names.\n\t\tthis.emitter?.emit({\n\t\t\ttype: 'store:db-name-collision',\n\t\t\tdbName: this.dbName,\n\t\t\tmessage: `Another runtime on this origin is already using database \"${this.dbName}\"; this runtime is sharing it as a follower. If these are separate apps, give each a distinct store name.`,\n\t\t})\n\t\t// Readiness handshake: give the leader relay a moment to answer before the\n\t\t// first RPC, so a follower opened during a leader's startup race retries the\n\t\t// handshake instead of firing into the void.\n\t\tawait followerBridge.waitForLeader()\n\t\tthis.installLifecycleListeners()\n\t\treturn this.checkedOpen(followerBridge, ddlStatements)\n\t}\n\n\t/** Open the database in this tab's own worker and start serving followers. */\n\tprivate async openLeaderWorker(\n\t\tworker: WebWorkerBridge,\n\t\tddlStatements: string[],\n\t): Promise<WorkerSuccessResponse> {\n\t\tconst hook = this.onLeaderWorkerOpened\n\t\tif (!hook) {\n\t\t\tconst pending = this.checkedOpen(worker, ddlStatements)\n\t\t\t// Posted after the open so follower requests queue behind it in the worker.\n\t\t\tworker.post({ id: 0, type: 'serve', channelName: this.tabSession?.channelName ?? '' })\n\t\t\treturn pending\n\t\t}\n\t\tconst response = await this.checkedOpen(worker, ddlStatements)\n\t\t// The hook (snapshot restore, STORE-6) runs before the worker serves other tabs,\n\t\t// so no follower ever reads or writes the database before it holds its data.\n\t\tawait runWorkerTransaction(worker, hook)\n\t\tworker.post({ id: 0, type: 'serve', channelName: this.tabSession?.channelName ?? '' })\n\t\treturn response\n\t}\n\n\t/**\n\t * Whether this runtime owns the database's worker: the storage leader in a browser,\n\t * or any adapter with an injected bridge (tests, Node). A follower tab relays every\n\t * request to the leader's worker (STORE-6).\n\t */\n\tisLeader(): boolean {\n\t\tif (this.injectedBridge) return true\n\t\treturn this.tabSession?.role === 'leader'\n\t}\n\n\tprivate async checkedOpen(\n\t\tbridge: WorkerBridge,\n\t\tddlStatements: string[],\n\t): Promise<WorkerSuccessResponse> {\n\t\tconst response = await bridge.send(\n\t\t\t{\n\t\t\t\tid: 0,\n\t\t\t\ttype: 'open',\n\t\t\t\tddlStatements,\n\t\t\t\tdbName: this.dbName,\n\t\t\t\t...(this.storage === 'memory' ? { storage: 'memory' as const } : {}),\n\t\t\t},\n\t\t\tundefined,\n\t\t\t{ timeoutMs: Number.POSITIVE_INFINITY },\n\t\t)\n\t\tif (response.type === 'error') {\n\t\t\tthrow new AdapterError(`Failed to open database: ${response.message}`, {\n\t\t\t\tcode: response.code,\n\t\t\t\tdbName: this.dbName,\n\t\t\t\t...(response.context ?? {}),\n\t\t\t})\n\t\t}\n\t\treturn response\n\t}\n\n\tprivate async openCurrentBridge(ddlStatements: string[]): Promise<WorkerSuccessResponse> {\n\t\tconst bridge = this.bridge\n\t\tif (!bridge) throw new StoreNotOpenError()\n\t\treturn this.checkedOpen(bridge, ddlStatements)\n\t}\n\n\tprivate onWorkerEvent(event: WorkerStatusEvent): void {\n\t\tif (!this.emitter) return\n\t\tconst holder =\n\t\t\tevent.resource === 'legacy-pool'\n\t\t\t\t? 'the shared OPFS pool used by older Kora versions (a tab running an older version of this app may still be open)'\n\t\t\t\t: 'another tab or worker that is still releasing it'\n\t\tif (event.kind === 'storage-blocked') {\n\t\t\tthis.emitter.emit({\n\t\t\t\ttype: 'store:storage-blocked',\n\t\t\t\tdbName: this.dbName,\n\t\t\t\tresource: event.resource,\n\t\t\t\tstate: 'waiting',\n\t\t\t\tmessage: `Database \"${this.dbName}\" is waiting for ${holder}. Close other tabs of this app if this persists; Kora will not fall back to non-durable storage.`,\n\t\t\t})\n\t\t\treturn\n\t\t}\n\t\tthis.emitter.emit({\n\t\t\ttype: 'store:storage-blocked',\n\t\t\tdbName: this.dbName,\n\t\t\tresource: event.resource,\n\t\t\tstate: 'resolved',\n\t\t\twaitedMs: event.waitedMs,\n\t\t\tmessage: `Database \"${this.dbName}\" obtained its storage after waiting ${event.waitedMs}ms.`,\n\t\t})\n\t}\n\n\t/**\n\t * Leader-only bookkeeping after a durable open: report a legacy-pool\n\t * migration, move an IndexedDB-held copy into OPFS explicitly (never two\n\t * disjoint copies), and record OPFS as this database's backend.\n\t */\n\tprivate async afterLeaderOpen(data: unknown): Promise<void> {\n\t\tif (!this.ownWorker || this.skipBackendRecord || this.storageOpenState?.persistent !== true) {\n\t\t\treturn\n\t\t}\n\t\tconst info = (typeof data === 'object' && data !== null ? data : {}) as {\n\t\t\tmigratedFromLegacy?: boolean\n\t\t}\n\t\tif (info.migratedFromLegacy === true) {\n\t\t\tthis.emitter?.emit({\n\t\t\t\ttype: 'store:storage-migrated',\n\t\t\t\tdbName: this.dbName,\n\t\t\t\tfrom: 'legacy-opfs-pool',\n\t\t\t\tto: 'opfs',\n\t\t\t\tmessage: `Database \"${this.dbName}\" moved from the shared pre-1.0 OPFS pool into its own pool.`,\n\t\t\t})\n\t\t}\n\t\tif (!isManifestAvailable()) return\n\n\t\tlet recordedBackend: string | null = null\n\t\ttry {\n\t\t\trecordedBackend = (await readManifestRecord(this.dbName))?.backend ?? null\n\t\t} catch (error) {\n\t\t\tconsole.warn(`[kora] Could not read the storage manifest for \"${this.dbName}\":`, error)\n\t\t}\n\t\tif (recordedBackend === 'indexeddb') {\n\t\t\t// The authoritative copy is in IndexedDB (an earlier session ran without\n\t\t\t// OPFS). Move it here before anything reads or writes; a failure fails\n\t\t\t// the open instead of showing the older OPFS copy.\n\t\t\tconst dump = await loadDumpFromIndexedDB<DatabaseDump>(this.dbName)\n\t\t\tif (dump) {\n\t\t\t\tawait this.applyDumpThroughWorker(this.ownWorker, dump)\n\t\t\t}\n\t\t\tthis.emitter?.emit({\n\t\t\t\ttype: 'store:storage-migrated',\n\t\t\t\tdbName: this.dbName,\n\t\t\t\tfrom: 'indexeddb',\n\t\t\t\tto: 'opfs',\n\t\t\t\tmessage: `Database \"${this.dbName}\" moved from IndexedDB into OPFS.`,\n\t\t\t})\n\t\t}\n\t\ttry {\n\t\t\tawait recordDatabase({\n\t\t\t\tdbName: this.dbName,\n\t\t\t\tbackend: 'opfs',\n\t\t\t\t...(this.storageOpenState.poolName ? { poolName: this.storageOpenState.poolName } : {}),\n\t\t\t})\n\t\t\tif (recordedBackend === 'indexeddb') {\n\t\t\t\tawait deleteFromIndexedDB(this.dbName)\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.warn(`[kora] Could not update the storage manifest for \"${this.dbName}\":`, error)\n\t\t}\n\t}\n\n\tprivate async applyDumpThroughWorker(worker: WorkerBridge, dump: DatabaseDump): Promise<void> {\n\t\tconst check = async (request: WorkerRequest, what: string): Promise<void> => {\n\t\t\tconst response = await worker.send(request)\n\t\t\tif (response.type === 'error') {\n\t\t\t\tthrow new AdapterError(`${what} failed: ${response.message}`, { dbName: this.dbName })\n\t\t\t}\n\t\t}\n\t\tawait check({ id: 0, type: 'begin' }, 'BEGIN IndexedDB import')\n\t\ttry {\n\t\t\tfor (const statement of restoreDumpStatements(dump)) {\n\t\t\t\tawait check(\n\t\t\t\t\t{ id: 0, type: 'execute', sql: statement.sql, params: statement.params },\n\t\t\t\t\t'IndexedDB import',\n\t\t\t\t)\n\t\t\t}\n\t\t\tawait check({ id: 0, type: 'commit' }, 'COMMIT IndexedDB import')\n\t\t} catch (error) {\n\t\t\tawait worker.send({ id: 0, type: 'rollback' }).catch(() => undefined)\n\t\t\tthrow error\n\t\t}\n\t}\n\n\t/**\n\t * Surface a `store:opfs-unavailable` diagnostic when the worker reported that\n\t * persistence silently degraded to an in-memory database, so a data-loss\n\t * condition is observable instead of failing silently. A persistent open (or a\n\t * bridge that does not report a mode, e.g. the Node mock) emits nothing.\n\t */\n\tprivate reportStorageMode(data: unknown): void {\n\t\tthis.storageOpenState = parseStorageOpenState(data)\n\t\tif (!this.emitNonPersistentDiagnostic || !this.emitter || !this.storageOpenState) {\n\t\t\treturn\n\t\t}\n\t\tif (this.storageOpenState.persistent === false && this.storage === 'opfs') {\n\t\t\tconst reason = this.storageOpenState.fallbackReason ?? 'unsupported'\n\t\t\tthis.emitter.emit({\n\t\t\t\ttype: 'store:opfs-unavailable',\n\t\t\t\tdbName: this.dbName,\n\t\t\t\treason,\n\t\t\t\tmessage: `OPFS persistence is unavailable (${reason}) for database \"${this.dbName}\"; the store is running in memory and data will not survive a reload.`,\n\t\t\t})\n\t\t}\n\t}\n\n\tgetStorageOpenState(): StorageOpenState | null {\n\t\treturn this.storageOpenState\n\t}\n\n\t/**\n\t * Durable or loud, never silent (NEW-STORE-6). When the open or a promotion\n\t * ended on non-persistent storage and the app did not opt into that, emit the\n\t * blocking `store:durability-lost` event and refuse every later write. A\n\t * bridge that reports no mode (the Node mock) is not judged.\n\t */\n\tprivate enforceDurability(\n\t\tphase: 'open' | 'promotion',\n\t\tfailure?: { reason: 'open-failed'; message: string },\n\t): void {\n\t\tif (this.allowNonDurable) return\n\t\tif (phase === 'open' && this.deferOpenDurabilityCheck && !failure) return\n\t\tlet reason: 'lock-conflict' | 'timeout' | 'unsupported' | 'open-failed'\n\t\tif (failure) {\n\t\t\treason = failure.reason\n\t\t} else if (this.storageOpenState?.persistent === false) {\n\t\t\treason = this.storageOpenState.fallbackReason ?? 'unsupported'\n\t\t} else {\n\t\t\tthis.durabilityLoss = null\n\t\t\treturn\n\t\t}\n\t\tthis.durabilityLoss = { phase, reason }\n\t\tthis.emitter?.emit({\n\t\t\ttype: 'store:durability-lost',\n\t\t\tdbName: this.dbName,\n\t\t\tphase,\n\t\t\treason,\n\t\t\tmessage:\n\t\t\t\t`Database \"${this.dbName}\" has no durable storage (${reason} during ${phase}); ` +\n\t\t\t\t`writes are refused until the app reloads with durable storage.${failure ? ` ${failure.message}` : ''}`,\n\t\t})\n\t}\n\n\tprivate guardDurableWrite(): void {\n\t\tif (this.durabilityLoss) {\n\t\t\tthrow new StorageDurabilityError(\n\t\t\t\tthis.dbName,\n\t\t\t\tthis.durabilityLoss.phase,\n\t\t\t\tthis.durabilityLoss.reason,\n\t\t\t)\n\t\t}\n\t}\n\n\tprivate warnSharedWorkerDeprecated(): void {\n\t\tif (!this.sharedWorkerUrl || warnedSharedWorkerDeprecated) {\n\t\t\treturn\n\t\t}\n\t\twarnedSharedWorkerDeprecated = true\n\t\tconsole.warn(\n\t\t\t'[kora] sharedWorkerUrl is deprecated and ignored: SharedWorker-hosted SQLite ' +\n\t\t\t\t'cannot use OPFS and is never durable. Kora uses the durable dedicated-worker ' +\n\t\t\t\t'leader/follower path for multi-tab storage.',\n\t\t)\n\t}\n\n\t/**\n\t * Close the database. A leader closes the database file and releases the OPFS\n\t * pool inside its worker, terminates the worker, and only then releases the\n\t * tab leader lock, so the next leader never races a still-running worker for\n\t * the file handles (NEW-STORE-10). A follower just leaves.\n\t */\n\tasync close(): Promise<void> {\n\t\tif (!this.bridge && !this.tabSession) return\n\t\tthis.closing = true\n\t\ttry {\n\t\t\tif (this.injectedBridge && this.bridge) {\n\t\t\t\tawait this.bridge.send({ id: 0, type: 'close' })\n\t\t\t} else if (this.ownWorker && !this.ownWorker.isTerminated()) {\n\t\t\t\tconst response = await this.ownWorker\n\t\t\t\t\t.send({ id: 0, type: 'close' }, undefined, { timeoutMs: 5000 })\n\t\t\t\t\t.catch((error: unknown) => ({\n\t\t\t\t\t\tid: 0,\n\t\t\t\t\t\ttype: 'error' as const,\n\t\t\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\t\t\tcode: 'CLOSE_FAILED',\n\t\t\t\t\t}))\n\t\t\t\tif (response.type === 'error') {\n\t\t\t\t\t// Terminating the worker below still releases the pool.\n\t\t\t\t\tconsole.warn(`[kora] Closing \"${this.dbName}\" in its worker failed:`, response.message)\n\t\t\t\t}\n\t\t\t}\n\t\t} finally {\n\t\t\tawait this.teardown()\n\t\t\tthis.closing = false\n\t\t}\n\t}\n\n\t/**\n\t * Release everything this adapter holds, in ownership order: lifecycle\n\t * listeners, then the worker (which holds the OPFS pool and its Web Lock),\n\t * then the tab leader lock or queued promotion request.\n\t */\n\tprivate async teardown(): Promise<void> {\n\t\tthis.removeLifecycleListeners?.()\n\t\tthis.removeLifecycleListeners = null\n\t\tconst session = this.tabSession\n\t\tthis.tabSession = null\n\t\tsession?.stopRelay?.()\n\t\tthis.bridge?.terminate()\n\t\tthis.bridge = null\n\t\tthis.ownWorker = null\n\t\tif (session) {\n\t\t\tif (session.releaseLock) {\n\t\t\t\tawait session.releaseLock()\n\t\t\t}\n\t\t\tsession.cancelPromotionWatch?.()\n\t\t}\n\t\tthis.opened = false\n\t\tthis.suspended = false\n\t\tconst gate = this.gate\n\t\tthis.gate = null\n\t\tgate?.open()\n\t}\n\n\t/**\n\t * Promotes a follower to leader after the previous leader released the storage\n\t * lock (its tab closed, crashed, or suspended on freeze). The new worker waits\n\t * on the pool's Web Lock and handles, so it never races the previous worker.\n\t * Requests issued during the hand-off wait for it; requests that were in\n\t * flight to the old leader fail with a retriable `BridgeTerminatedError`.\n\t */\n\tprivate async promoteToLeader(): Promise<void> {\n\t\tif (\n\t\t\tthis.promoting ||\n\t\t\tthis.closing ||\n\t\t\t!this.opened ||\n\t\t\tthis.workerUrl === undefined ||\n\t\t\tthis.schema === null\n\t\t) {\n\t\t\treturn\n\t\t}\n\t\tthis.promoting = true\n\t\tconst gate = createGate()\n\t\tthis.gate = gate\n\n\t\ttry {\n\t\t\tconst { WebWorkerBridge } = await import('./sqlite-wasm-channel')\n\t\t\tif (this.closing || !this.opened) return\n\t\t\tconst worker = new WebWorkerBridge(this.workerUrl, this.workerResponseTimeoutMs, {\n\t\t\t\tonEvent: (event) => this.onWorkerEvent(event),\n\t\t\t})\n\t\t\tconst previousBridge = this.bridge\n\t\t\tthis.bridge = worker\n\t\t\tthis.ownWorker = worker\n\t\t\tif (this.tabSession) {\n\t\t\t\tthis.tabSession.role = 'leader'\n\t\t\t}\n\t\t\tpreviousBridge?.terminate()\n\n\t\t\t// Re-open against our own worker. DDL is idempotent, and the OPFS data the\n\t\t\t// old leader persisted is readable once its worker is gone.\n\t\t\tconst ddlStatements = generateFullDDL(this.schema)\n\t\t\tlet response: WorkerSuccessResponse\n\t\t\ttry {\n\t\t\t\tresponse = await this.openLeaderWorker(worker, ddlStatements)\n\t\t\t} catch (error) {\n\t\t\t\t// The promoted worker could not obtain durable storage. Never let it\n\t\t\t\t// run silently in memory.\n\t\t\t\tthis.storageOpenState = { persistent: false, mode: 'memory' }\n\t\t\t\tthis.enforceDurability('promotion', {\n\t\t\t\t\treason: 'open-failed',\n\t\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\t})\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.storageOpenState = parseStorageOpenState(response.data) ?? this.storageOpenState\n\t\t\tthis.enforceDurability('promotion')\n\t\t\ttry {\n\t\t\t\tawait this.afterLeaderOpen(response.data)\n\t\t\t} catch (error) {\n\t\t\t\tthis.enforceDurability('promotion', {\n\t\t\t\t\treason: 'open-failed',\n\t\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\t})\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.promoting = false\n\t\t\tif (this.gate === gate) this.gate = null\n\t\t\tgate.open()\n\t\t}\n\t}\n\n\t/**\n\t * Page Lifecycle: a frozen or bfcached page cannot run, so it must not keep\n\t * other tabs waiting on its storage. Leaders terminate their worker (freeing\n\t * the OPFS pool) and then release the leader lock, so a visible tab is\n\t * promoted; followers withdraw their queued lock request so a frozen tab is\n\t * never granted leadership it cannot use. Never uses Web Locks `steal`.\n\t */\n\tprivate installLifecycleListeners(): void {\n\t\tif (!this.releaseOnFreeze || this.removeLifecycleListeners) return\n\t\tif (typeof addEventListener !== 'function' || typeof document === 'undefined') return\n\t\tconst onFreeze = (): void => this.suspend()\n\t\tconst onPageHide = (): void => this.suspend()\n\t\tconst onResume = (): void => {\n\t\t\tvoid this.resume()\n\t\t}\n\t\tconst onPageShow = (event: Event): void => {\n\t\t\tif ((event as PageTransitionEvent).persisted) void this.resume()\n\t\t}\n\t\tdocument.addEventListener('freeze', onFreeze)\n\t\tdocument.addEventListener('resume', onResume)\n\t\taddEventListener('pagehide', onPageHide)\n\t\taddEventListener('pageshow', onPageShow)\n\t\tthis.removeLifecycleListeners = () => {\n\t\t\tdocument.removeEventListener('freeze', onFreeze)\n\t\t\tdocument.removeEventListener('resume', onResume)\n\t\t\tremoveEventListener('pagehide', onPageHide)\n\t\t\tremoveEventListener('pageshow', onPageShow)\n\t\t}\n\t}\n\n\t/** Synchronous by necessity: a `freeze` handler gets no further turns. */\n\tprivate suspend(): void {\n\t\tif (!this.opened || this.suspended || this.closing || this.promoting) return\n\t\tthis.suspended = true\n\t\tthis.gate = createGate()\n\t\tconst session = this.tabSession\n\t\tthis.tabSession = null\n\t\t// Worker (pool holder) first, then the leader lock (NEW-STORE-10).\n\t\tthis.bridge?.terminate()\n\t\tthis.bridge = null\n\t\tthis.ownWorker = null\n\t\tif (session) {\n\t\t\tvoid session.releaseLock?.()\n\t\t\tsession.cancelPromotionWatch?.()\n\t\t}\n\t}\n\n\tprivate async resume(): Promise<void> {\n\t\tif (!this.suspended || this.closing || this.schema === null) return\n\t\tconst gate = this.gate\n\t\ttry {\n\t\t\tconst response = await this.attachAndOpen(generateFullDDL(this.schema))\n\t\t\tthis.reportStorageMode(response.data)\n\t\t\tthis.enforceDurability('promotion')\n\t\t\tawait this.afterLeaderOpen(response.data)\n\t\t} catch (error) {\n\t\t\tthis.enforceDurability('promotion', {\n\t\t\t\treason: 'open-failed',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t})\n\t\t} finally {\n\t\t\tthis.suspended = false\n\t\t\tif (this.gate === gate) this.gate = null\n\t\t\tgate?.open()\n\t\t}\n\t}\n\n\t/**\n\t * Non-transactional write. Takes the same mutex as {@link transaction}, so it\n\t * never lands inside this tab's open transaction and is never rolled back\n\t * with it (STORE-8). Inside a transaction callback, use the `tx` handle.\n\t */\n\tasync execute(sql: string, params?: unknown[], options?: StorageRequestOptions): Promise<void> {\n\t\tthis.guardOpen()\n\t\tthis.guardDurableWrite()\n\t\tconst release = await this.mutex.acquire()\n\t\ttry {\n\t\t\tconst response = await this.sendRequest(\n\t\t\t\t{\n\t\t\t\t\tid: 0,\n\t\t\t\t\ttype: 'execute',\n\t\t\t\t\tsql,\n\t\t\t\t\tparams,\n\t\t\t\t\t...(options?.requestId ? { requestId: options.requestId } : {}),\n\t\t\t\t},\n\t\t\t\toptions?.signal ? { signal: options.signal } : undefined,\n\t\t\t)\n\t\t\tif (response.type === 'error') {\n\t\t\t\tthrow new AdapterError(`Execute failed: ${response.message}`, { sql, params })\n\t\t\t}\n\t\t} finally {\n\t\t\trelease()\n\t\t}\n\t}\n\n\t/**\n\t * Non-transactional read. Waits for this tab's open transaction to finish, so\n\t * it never observes uncommitted (possibly rolled-back) rows (STORE-8).\n\t */\n\tasync query<T>(sql: string, params?: unknown[], options?: StorageRequestOptions): Promise<T[]> {\n\t\tthis.guardOpen()\n\t\tconst release = await this.mutex.acquire()\n\t\ttry {\n\t\t\tconst response = await this.sendRequest(\n\t\t\t\t{\n\t\t\t\t\tid: 0,\n\t\t\t\t\ttype: 'query',\n\t\t\t\t\tsql,\n\t\t\t\t\tparams,\n\t\t\t\t\t...(options?.requestId ? { requestId: options.requestId } : {}),\n\t\t\t\t},\n\t\t\t\toptions?.signal ? { signal: options.signal } : undefined,\n\t\t\t)\n\t\t\tif (response.type === 'error') {\n\t\t\t\tthrow new AdapterError(`Query failed: ${response.message}`, { sql, params })\n\t\t\t}\n\t\t\treturn (response.data as T[]) ?? []\n\t\t} finally {\n\t\t\trelease()\n\t\t}\n\t}\n\n\tasync transaction(fn: (tx: Transaction) => Promise<void>): Promise<void> {\n\t\tthis.guardOpen()\n\t\tthis.guardDurableWrite()\n\n\t\tconst release = await this.mutex.acquire()\n\t\tlet txBridge: WorkerBridge | null = null\n\t\t// Every statement of the span must reach the worker that ran BEGIN. If\n\t\t// leadership changed hands mid-transaction, the old worker rolled it back\n\t\t// (or is gone); sending the rest to the new worker would autocommit a\n\t\t// partial transaction, so fail with a retriable error instead.\n\t\tconst sendInSpan = async (request: WorkerRequest): Promise<WorkerResponse> => {\n\t\t\tif (txBridge === null || this.bridge !== txBridge) {\n\t\t\t\tthrow new BridgeTerminatedError(request.type, 'storage leader changed during transaction')\n\t\t\t}\n\t\t\treturn txBridge.send(request)\n\t\t}\n\t\ttry {\n\t\t\twhile (this.gate) {\n\t\t\t\tawait this.gate.promise\n\t\t\t}\n\t\t\ttxBridge = this.bridge\n\t\t\tif (!txBridge) throw new StoreNotOpenError()\n\t\t\tconst begin = await sendInSpan({ id: 0, type: 'begin' })\n\t\t\tif (begin.type === 'error') {\n\t\t\t\tthrow new AdapterError(`BEGIN transaction failed: ${begin.message}`)\n\t\t\t}\n\n\t\t\tconst tx: Transaction = {\n\t\t\t\texecute: async (sql: string, params?: unknown[]): Promise<void> => {\n\t\t\t\t\tconst response = await sendInSpan({ id: 0, type: 'execute', sql, params })\n\t\t\t\t\tif (response.type === 'error') {\n\t\t\t\t\t\tthrow new AdapterError(`Transaction execute failed: ${response.message}`, {\n\t\t\t\t\t\t\tsql,\n\t\t\t\t\t\t\tparams,\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tquery: async <T>(sql: string, params?: unknown[]): Promise<T[]> => {\n\t\t\t\t\tconst response = await sendInSpan({ id: 0, type: 'query', sql, params })\n\t\t\t\t\tif (response.type === 'error') {\n\t\t\t\t\t\tthrow new AdapterError(`Transaction query failed: ${response.message}`, { sql, params })\n\t\t\t\t\t}\n\t\t\t\t\treturn (response.data as T[]) ?? []\n\t\t\t\t},\n\t\t\t}\n\n\t\t\tawait fn(tx)\n\t\t\tconst commit = await sendInSpan({ id: 0, type: 'commit' })\n\t\t\tif (commit.type === 'error') {\n\t\t\t\tthrow new AdapterError(`COMMIT transaction failed: ${commit.message}`)\n\t\t\t}\n\t\t} catch (error) {\n\t\t\t// Attempt rollback on the worker that ran BEGIN, but don't mask the\n\t\t\t// original error.\n\t\t\ttry {\n\t\t\t\tif (txBridge && this.bridge === txBridge) {\n\t\t\t\t\tawait txBridge.send({ id: 0, type: 'rollback' })\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// Rollback failure is secondary to the original error\n\t\t\t}\n\t\t\tthrow error\n\t\t} finally {\n\t\t\trelease()\n\t\t}\n\t}\n\n\tasync migrate(from: number, to: number, migration: MigrationPlan): Promise<void> {\n\t\tthis.guardOpen()\n\t\tthis.guardDurableWrite()\n\n\t\tconst release = await this.mutex.acquire()\n\t\ttry {\n\t\t\tawait this.sendChecked({ id: 0, type: 'begin' }, 'BEGIN migration')\n\n\t\t\tfor (const sql of migration.statements) {\n\t\t\t\tconst response = await this.sendRequest({ id: 0, type: 'execute', sql })\n\t\t\t\tif (response.type === 'error') {\n\t\t\t\t\tthrow new AdapterError(`Migration from v${from} to v${to} failed: ${response.message}`, {\n\t\t\t\t\t\tfrom,\n\t\t\t\t\t\tto,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tawait this.sendChecked({ id: 0, type: 'commit' }, 'COMMIT migration')\n\t\t} catch (error) {\n\t\t\ttry {\n\t\t\t\tawait this.sendRequest({ id: 0, type: 'rollback' })\n\t\t\t} catch {\n\t\t\t\t// Rollback failure is secondary\n\t\t\t}\n\t\t\tif (error instanceof AdapterError) throw error\n\t\t\tthrow new AdapterError(\n\t\t\t\t`Migration from v${from} to v${to} failed: ${(error as Error).message}`,\n\t\t\t\t{ from, to },\n\t\t\t)\n\t\t} finally {\n\t\t\trelease()\n\t\t}\n\t}\n\n\t/**\n\t * Export the database as a Uint8Array (for IndexedDB persistence).\n\t * Only available when the database is open.\n\t */\n\tasync exportDatabase(): Promise<Uint8Array> {\n\t\tthis.guardOpen()\n\t\tconst release = await this.mutex.acquire()\n\t\ttry {\n\t\t\tconst response = await this.sendRequest({ id: 0, type: 'export' })\n\t\t\tif (response.type === 'error') {\n\t\t\t\tthrow new AdapterError(`Export failed: ${response.message}`, {\n\t\t\t\t\tcode: response.code,\n\t\t\t\t\tcontext: response.context,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn response.data as Uint8Array\n\t\t} finally {\n\t\t\trelease()\n\t\t}\n\t}\n\n\t/**\n\t * Import a serialized database snapshot.\n\t */\n\tasync importDatabase(data: Uint8Array): Promise<void> {\n\t\tthis.guardOpen()\n\t\tthis.guardDurableWrite()\n\t\tconst release = await this.mutex.acquire()\n\t\ttry {\n\t\t\tconst response = await this.sendRequest({ id: 0, type: 'import', data })\n\t\t\tif (response.type === 'error') {\n\t\t\t\tthrow new AdapterError(`Import failed: ${response.message}`)\n\t\t\t}\n\t\t} finally {\n\t\t\trelease()\n\t\t}\n\t}\n\n\tprivate guardOpen(): void {\n\t\tif (!this.opened || (!this.bridge && !this.gate)) {\n\t\t\tthrow new StoreNotOpenError()\n\t\t}\n\t}\n\n\tprivate async sendRequest(\n\t\trequest: WorkerRequest,\n\t\toptions?: WorkerSendOptions,\n\t): Promise<WorkerResponse> {\n\t\t// While leadership changes hands, wait for the new bridge instead of\n\t\t// sending into a torn-down one.\n\t\twhile (this.gate) {\n\t\t\tawait this.gate.promise\n\t\t}\n\t\tconst bridge = this.bridge\n\t\tif (!bridge) {\n\t\t\tthrow new StoreNotOpenError()\n\t\t}\n\t\treturn bridge.send(request, undefined, options)\n\t}\n\n\tprivate async sendChecked(request: WorkerRequest, description: string): Promise<void> {\n\t\tconst response = await this.sendRequest(request)\n\t\tif (response.type === 'error') {\n\t\t\tthrow new AdapterError(`${description} failed: ${response.message}`)\n\t\t}\n\t}\n}\n\n/**\n * Run `fn` in one transaction sent straight to `worker`, bypassing the adapter's mutex\n * and leadership gate (the caller is the leader opening its own worker).\n */\nasync function runWorkerTransaction(\n\tworker: WorkerBridge,\n\tfn: (tx: Transaction) => Promise<void>,\n): Promise<void> {\n\tconst send = async (request: WorkerRequest, what: string): Promise<WorkerResponse> => {\n\t\tconst response = await worker.send(request)\n\t\tif (response.type === 'error') {\n\t\t\tthrow new AdapterError(`${what} failed: ${response.message}`, {\n\t\t\t\tcode: response.code,\n\t\t\t\t...(request.type === 'execute' || request.type === 'query' ? { sql: request.sql } : {}),\n\t\t\t})\n\t\t}\n\t\treturn response\n\t}\n\tawait send({ id: 0, type: 'begin' }, 'BEGIN')\n\ttry {\n\t\tawait fn({\n\t\t\texecute: async (sql, params) => {\n\t\t\t\tawait send({ id: 0, type: 'execute', sql, params }, 'Leader open execute')\n\t\t\t},\n\t\t\tquery: async <T>(sql: string, params?: unknown[]): Promise<T[]> => {\n\t\t\t\tconst response = await send({ id: 0, type: 'query', sql, params }, 'Leader open query')\n\t\t\t\treturn ((response as WorkerSuccessResponse).data as T[]) ?? []\n\t\t\t},\n\t\t})\n\t\tawait send({ id: 0, type: 'commit' }, 'COMMIT')\n\t} catch (error) {\n\t\tawait worker.send({ id: 0, type: 'rollback' }).catch(() => undefined)\n\t\tthrow error\n\t}\n}\n\nfunction parseStorageOpenState(data: unknown): StorageOpenState | null {\n\tif (typeof data !== 'object' || data === null) {\n\t\treturn null\n\t}\n\tconst mode = data as {\n\t\tpersistent?: boolean\n\t\tfallbackReason?: StorageFallbackReason\n\t\tpoolName?: string\n\t\tjournalMode?: string\n\t}\n\tif (typeof mode.persistent !== 'boolean') {\n\t\treturn null\n\t}\n\treturn {\n\t\tpersistent: mode.persistent,\n\t\tmode: mode.persistent ? 'opfs' : 'memory',\n\t\t...(mode.fallbackReason ? { fallbackReason: mode.fallbackReason } : {}),\n\t\t...(typeof mode.poolName === 'string' ? { poolName: mode.poolName } : {}),\n\t\t...(typeof mode.journalMode === 'string' ? { journalMode: mode.journalMode } : {}),\n\t}\n}\n","/**\n * Names shared by the main thread and the SQLite worker for OPFS pools, Web Locks\n * and database files. Pure functions, so ownership rules are testable in Node.\n *\n * Ownership model (W8a, NEW-STORE-5): one OPFS SyncAccessHandle pool per\n * database. The tab leader lock (`kora-leader-<db>`) already guarantees one\n * leader tab per database, so a pool per database means two different databases\n * on one origin (per-user databases, two Kora apps) never contend for one pool.\n */\n\n/** Pool name every database used before W8a (beta.12 and earlier): one per origin. */\nexport const LEGACY_OPFS_POOL_NAME = 'kora-opfs'\n\n/** Prefix of the per-database pool names. */\nexport const OPFS_POOL_PREFIX = 'kora-opfs-'\n\n/**\n * Longest readable database slug kept in a pool name. Pool names become OPFS\n * directory names (`.<pool>`) and VFS names, so they stay short and portable.\n */\nconst MAX_POOL_SLUG_LENGTH = 48\n\n/**\n * 32-bit FNV-1a hash as 8 hex digits. Used only to keep pool names unique when\n * a database name had to be sanitized or truncated; not a security primitive.\n */\nexport function fnv1a32Hex(value: string): string {\n\tlet hash = 0x811c9dc5\n\tfor (let i = 0; i < value.length; i += 1) {\n\t\thash ^= value.charCodeAt(i)\n\t\thash = Math.imul(hash, 0x01000193)\n\t}\n\treturn (hash >>> 0).toString(16).padStart(8, '0')\n}\n\n/**\n * The OPFS SAH pool that owns `dbName`. Database names that are already safe\n * map to `kora-opfs-<dbName>`; anything sanitized or truncated gets a hash suffix\n * so two distinct names can never share a pool.\n *\n * @example opfsPoolNameFor('school__user_alice') === 'kora-opfs-school__user_alice'\n */\nexport function opfsPoolNameFor(dbName: string): string {\n\tconst sanitized = dbName.replace(/[^a-zA-Z0-9_-]/g, '_')\n\tconst slug = sanitized.slice(0, MAX_POOL_SLUG_LENGTH)\n\tif (slug === dbName) {\n\t\treturn `${OPFS_POOL_PREFIX}${slug}`\n\t}\n\treturn `${OPFS_POOL_PREFIX}${slug}-${fnv1a32Hex(dbName)}`\n}\n\n/** OPFS directory sqlite-wasm uses for a pool (its default is `.` + pool name). */\nexport function opfsPoolDirectory(poolName: string): string {\n\treturn `.${poolName}`\n}\n\n/**\n * Web Lock held by the worker that owns a pool, for the pool's entire life. A\n * new owner (a promoted tab, a reopened database) waits on it instead of racing\n * the previous worker for the file handles.\n */\nexport function opfsPoolLockName(poolName: string): string {\n\treturn `kora-opfs-pool:${poolName}`\n}\n\n/** Tab leader lock for a database (one leader tab per database per origin). */\nexport function leaderLockName(dbName: string): string {\n\treturn `kora-leader-${dbName}`\n}\n\n/** BroadcastChannel carrying follower RPC, heartbeats and pings for a database. */\nexport function storageChannelName(dbName: string): string {\n\treturn `kora-storage-${dbName}`\n}\n\n/** File name of a database inside its pool (unchanged from beta.12 for migration). */\nexport function opfsDatabaseFilename(dbName: string): string {\n\tconst base = dbName.replace(/[^a-zA-Z0-9._-]/g, '_')\n\treturn base.endsWith('.db') ? base : `${base}.db`\n}\n\n/** Absolute path under which the SAH pool tracks a database file. */\nexport function opfsPoolPath(dbName: string): string {\n\treturn `/${opfsDatabaseFilename(dbName)}`\n}\n","/**\n * Multi-tab SQLite storage coordination via `navigator.locks` and `BroadcastChannel`.\n *\n * One tab holds the exclusive `kora-leader-${dbName}` lock and owns the SQLite\n * worker. Other tabs send worker RPC over a named broadcast channel. In browsers\n * the leader's WORKER answers that channel itself ({@link startWorkerRpcRelay}),\n * so a hung leader main thread never blocks other tabs, and it pushes heartbeats\n * (also from inside long statements) that followers use to detect a hung or\n * frozen leader (NEW-STORE-9).\n */\n\nimport { leaderLockName, storageChannelName } from '../adapters/opfs-names'\nimport type {\n\tWorkerBridge,\n\tWorkerRequest,\n\tWorkerResponse,\n\tWorkerSendOptions,\n} from '../adapters/sqlite-wasm-channel'\nimport {\n\tBridgeTerminatedError,\n\tLeaderUnresponsiveError,\n\tNoLeaderError,\n\tRequestAbortedError,\n\tWorkerTimeoutError,\n} from '../errors'\n\nconst RPC_REQUEST = 'kora-worker-request'\nconst RPC_RESPONSE = 'kora-worker-response'\nconst CLIENT_LEAVE = 'kora-client-leave'\nconst LEADER_PING = 'kora-leader-ping'\nconst LEADER_PONG = 'kora-leader-pong'\nconst LEADER_HEARTBEAT = 'kora-leader-heartbeat'\n\n/** Default interval between leader heartbeats and follower watchdog ticks. */\nexport const DEFAULT_HEARTBEAT_MS = 1000\n/** Missed heartbeat intervals after which a follower gives up on a leader it heard. */\nconst MISSED_BEATS_UNRESPONSIVE = 3\n/** Silent intervals after which a follower that never heard a leader gives up. */\nconst MISSED_BEATS_ABSENT = 2\n/** Default idle budget for a client that owns an open transaction span. */\nconst DEFAULT_TRANSACTION_IDLE_TIMEOUT_MS = 10_000\n/** Completed follower responses remembered for de-duplicating retried requests. */\nconst DEDUP_CAPACITY = 512\n\ninterface RpcRequestMessage {\n\ttype: typeof RPC_REQUEST\n\trequestId: string\n\tclientId: string\n\trequest: WorkerRequest\n}\n\ninterface RpcResponseMessage {\n\ttype: typeof RPC_RESPONSE\n\trequestId: string\n\tresponse: WorkerResponse\n}\n\ninterface ClientLeaveMessage {\n\ttype: typeof CLIENT_LEAVE\n\tclientId: string\n}\n\ninterface HeartbeatMessage {\n\ttype: typeof LEADER_HEARTBEAT\n\tepoch: string\n\tsource: 'main' | 'worker'\n}\n\ntype ChannelMessage =\n\t| RpcRequestMessage\n\t| RpcResponseMessage\n\t| ClientLeaveMessage\n\t| HeartbeatMessage\n\t| { type: typeof LEADER_PING }\n\t| { type: typeof LEADER_PONG }\n\ninterface ReclaimingWorkerBridge extends WorkerBridge {\n\treclaimClient(clientId: string, reason: string): void\n}\n\nexport type TabStorageRole = 'leader' | 'follower'\n\nexport interface AcquireTabStorageOptions {\n\t/**\n\t * Invoked when a follower is promoted to leader because the previous leader\n\t * released the lock (its tab closed, crashed, or suspended itself on freeze).\n\t * The adapter uses this to rebuild its bridge as a leader. Never fires for a\n\t * tab that started as leader.\n\t */\n\tonPromote?: () => void\n}\n\nexport interface TabStorageSession {\n\trole: TabStorageRole\n\tchannelName: string\n\t/** Leader only: release the navigator lock when closing the database. */\n\treleaseLock?: () => Promise<void>\n\t/** Leader only: stop the broadcast RPC relay or heartbeat. */\n\tstopRelay?: () => void\n\t/**\n\t * Follower only: cancels the queued lock request (or, once promoted, releases\n\t * the lock this tab won). Present so callers can tear the session down.\n\t */\n\tcancelPromotionWatch?: () => void\n}\n\n/**\n * Returns whether multi-tab coordination APIs exist in this runtime.\n */\nexport function isMultiTabStorageSupported(): boolean {\n\treturn (\n\t\ttypeof globalThis !== 'undefined' &&\n\t\ttypeof BroadcastChannel !== 'undefined' &&\n\t\ttypeof navigator !== 'undefined' &&\n\t\ttypeof navigator.locks?.request === 'function'\n\t)\n}\n\n/**\n * Resolve leader vs follower for a database name.\n * Without lock APIs, every instance is treated as leader (single-tab / Node).\n *\n * A follower additionally queues a blocking request for the same lock. When the\n * current leader releases it (its tab closed, crashed, or suspended on freeze),\n * the browser grants the lock to this follower and\n * {@link AcquireTabStorageOptions.onPromote} fires so the adapter can rebuild\n * itself as the new leader. Kora never uses the Web Locks `steal` option: a\n * frozen leader's worker would still hold the OPFS file handles.\n */\nexport async function acquireTabStorageSession(\n\tdbName: string,\n\toptions?: AcquireTabStorageOptions,\n): Promise<TabStorageSession> {\n\tconst channelName = storageChannelName(dbName)\n\tconst lockName = leaderLockName(dbName)\n\n\tif (!isMultiTabStorageSupported()) {\n\t\treturn { role: 'leader', channelName }\n\t}\n\n\treturn new Promise<TabStorageSession>((resolve) => {\n\t\tlet releaseHeld: (() => void) | undefined\n\n\t\tvoid navigator.locks.request(lockName, { mode: 'exclusive', ifAvailable: true }, (lock) => {\n\t\t\tif (lock === null) {\n\t\t\t\tresolve(startFollowerSession(channelName, lockName, options))\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tresolve({\n\t\t\t\trole: 'leader',\n\t\t\t\tchannelName,\n\t\t\t\treleaseLock: async () => {\n\t\t\t\t\treleaseHeld?.()\n\t\t\t\t},\n\t\t\t})\n\n\t\t\treturn new Promise<void>((release) => {\n\t\t\t\treleaseHeld = release\n\t\t\t})\n\t\t})\n\t})\n}\n\n/**\n * Builds a follower session and starts watching for promotion. The follower holds\n * a queued lock request; when it is finally granted (old leader gone), it becomes\n * the leader for the rest of its lifetime and notifies via `onPromote`.\n */\nfunction startFollowerSession(\n\tchannelName: string,\n\tlockName: string,\n\toptions?: AcquireTabStorageOptions,\n): TabStorageSession {\n\tlet promoted = false\n\tlet releasePromotedLock: (() => void) | undefined\n\tconst abort = new AbortController()\n\n\tvoid navigator.locks\n\t\t.request(lockName, { mode: 'exclusive', signal: abort.signal }, () => {\n\t\t\t// Reaching here means the previous leader released the lock and this tab\n\t\t\t// won it. Hold it (never-resolving promise) so this tab is now the leader.\n\t\t\tpromoted = true\n\t\t\toptions?.onPromote?.()\n\t\t\treturn new Promise<void>((release) => {\n\t\t\t\treleasePromotedLock = release\n\t\t\t})\n\t\t})\n\t\t.catch(() => {\n\t\t\t// AbortError when the tab closes before promotion. Nothing to do.\n\t\t})\n\n\treturn {\n\t\trole: 'follower',\n\t\tchannelName,\n\t\tcancelPromotionWatch: () => {\n\t\t\tif (promoted) {\n\t\t\t\treleasePromotedLock?.()\n\t\t\t} else {\n\t\t\t\tabort.abort()\n\t\t\t}\n\t\t},\n\t}\n}\n\n/**\n * Answers follower RPC with a bounded response cache keyed by request id, so a\n * follower that retries a request it lost track of (leader looked unresponsive,\n * caller aborted) gets the original response instead of a second application.\n */\nfunction createRequestDeduper(): {\n\trun(requestId: string, start: () => Promise<WorkerResponse>): Promise<WorkerResponse> | null\n} {\n\tconst inflight = new Map<string, Promise<WorkerResponse>>()\n\tconst completed = new Map<string, WorkerResponse>()\n\treturn {\n\t\trun(requestId, start) {\n\t\t\tconst done = completed.get(requestId)\n\t\t\tif (done) return Promise.resolve(done)\n\t\t\t// Already running: the original response is broadcast when it lands.\n\t\t\tif (inflight.has(requestId)) return null\n\t\t\tconst promise = start().then((response) => {\n\t\t\t\tinflight.delete(requestId)\n\t\t\t\tcompleted.set(requestId, response)\n\t\t\t\tif (completed.size > DEDUP_CAPACITY) {\n\t\t\t\t\tconst oldest = completed.keys().next().value\n\t\t\t\t\tif (oldest !== undefined) completed.delete(oldest)\n\t\t\t\t}\n\t\t\t\treturn response\n\t\t\t})\n\t\t\tinflight.set(requestId, promise)\n\t\t\treturn promise\n\t\t},\n\t}\n}\n\n/** Options for the leader-side relays. */\nexport interface LeaderRelayOptions {\n\t/** Heartbeat interval; 0 disables heartbeats. */\n\theartbeatMs?: number\n}\n\n/** Handle returned by {@link startWorkerRpcRelay}. */\nexport interface WorkerRpcRelay {\n\t/** Push a heartbeat now (called from inside long statements). */\n\tbeat(): void\n\tstop(): void\n}\n\nfunction serveChannel(\n\tchannel: BroadcastChannel,\n\tbridge: WorkerBridge,\n\tsource: 'main' | 'worker',\n\theartbeatMs: number,\n): WorkerRpcRelay {\n\tconst epoch = createClientId()\n\tconst dedup = createRequestDeduper()\n\tconst beat = (): void => {\n\t\tconst message: HeartbeatMessage = { type: LEADER_HEARTBEAT, epoch, source }\n\t\tchannel.postMessage(message)\n\t}\n\tconst respond = (requestId: string, response: WorkerResponse): void => {\n\t\tconst msg: RpcResponseMessage = { type: RPC_RESPONSE, requestId, response }\n\t\tchannel.postMessage(msg)\n\t}\n\n\tconst onMessage = (event: MessageEvent<ChannelMessage>): void => {\n\t\tconst data = event.data\n\t\t// Answer liveness probes immediately, without touching the database.\n\t\tif (data?.type === LEADER_PING) {\n\t\t\tchannel.postMessage({ type: LEADER_PONG })\n\t\t\treturn\n\t\t}\n\t\tif (data?.type === CLIENT_LEAVE) {\n\t\t\tif (hasReclaimClient(bridge)) {\n\t\t\t\tbridge.reclaimClient(data.clientId, 'client-left')\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif (data?.type !== RPC_REQUEST) {\n\t\t\treturn\n\t\t}\n\t\tif (\n\t\t\tdata.request.type === 'close' ||\n\t\t\tdata.request.type === 'destroy' ||\n\t\t\tdata.request.type === 'serve'\n\t\t) {\n\t\t\t// Only the owning tab may close or remove the database; a follower\n\t\t\t// leaving just stops sending requests.\n\t\t\trespond(data.requestId, { id: data.request.id, type: 'success' })\n\t\t\treturn\n\t\t}\n\n\t\tconst pending = dedup.run(data.requestId, () =>\n\t\t\tbridge.send(data.request, data.clientId).catch(\n\t\t\t\t(error: unknown): WorkerResponse => ({\n\t\t\t\t\tid: data.request.id,\n\t\t\t\t\ttype: 'error',\n\t\t\t\t\tmessage: error instanceof Error ? error.message : 'Worker RPC failed',\n\t\t\t\t\tcode: 'LEADER_RPC_ERROR',\n\t\t\t\t}),\n\t\t\t),\n\t\t)\n\t\tvoid pending?.then((response) => respond(data.requestId, response))\n\t}\n\n\tchannel.addEventListener('message', onMessage)\n\tconst interval = heartbeatMs > 0 ? setInterval(beat, heartbeatMs) : undefined\n\tif (heartbeatMs > 0) beat()\n\treturn {\n\t\tbeat,\n\t\tstop: () => {\n\t\t\tif (interval !== undefined) clearInterval(interval)\n\t\t\tchannel.removeEventListener('message', onMessage)\n\t\t\tchannel.close()\n\t\t},\n\t}\n}\n\n/**\n * Leader main thread: forward follower RPC to the worker bridge. Used where the\n * worker cannot host the relay itself (Node tests, custom bridges). Heartbeats\n * are off by default here; pass `heartbeatMs` to enable them.\n */\nexport function startLeaderRpcRelay(\n\tchannelName: string,\n\tbridge: WorkerBridge,\n\toptions: LeaderRelayOptions = {},\n): () => void {\n\treturn serveChannel(new BroadcastChannel(channelName), bridge, 'main', options.heartbeatMs ?? 0)\n\t\t.stop\n}\n\n/**\n * Leader WORKER: answer follower RPC, pings and heartbeats from inside the\n * dedicated SQLite worker, through the worker's transaction serializer. The\n * leader tab's main thread is not on this path, so a hung or busy main thread\n * does not stall other tabs (NEW-STORE-9).\n */\nexport function startWorkerRpcRelay(\n\tchannelName: string,\n\tbridge: WorkerBridge,\n\toptions: LeaderRelayOptions = {},\n): WorkerRpcRelay {\n\treturn serveChannel(\n\t\tnew BroadcastChannel(channelName),\n\t\tbridge,\n\t\t'worker',\n\t\toptions.heartbeatMs ?? DEFAULT_HEARTBEAT_MS,\n\t)\n}\n\ninterface PendingFollowerRequest {\n\ttype: string\n\tresolve: (r: WorkerResponse) => void\n\treject: (e: Error) => void\n}\n\n/**\n * Follower tab: proxy {@link WorkerBridge} over BroadcastChannel to the leader.\n *\n * A single watchdog runs while requests are pending. Any message from the leader\n * (heartbeat, pong, response) is a sign of life; when the leader goes quiet the\n * watchdog pings it, and after {@link MISSED_BEATS_UNRESPONSIVE} silent intervals\n * it fails every pending request with a typed, retriable\n * {@link LeaderUnresponsiveError} (or {@link NoLeaderError} if no leader was\n * heard at all) instead of waiting out the full RPC timeout.\n */\nexport class FollowerBroadcastBridge implements WorkerBridge {\n\tprivate readonly channel: BroadcastChannel\n\tprivate readonly clientId = createClientId()\n\tprivate readonly onPageHide = (): void => {\n\t\tthis.leaveLeader()\n\t}\n\tprivate readonly pending = new Map<string, PendingFollowerRequest>()\n\tprivate readonly timeoutMs: number\n\tprivate readonly heartbeatMs: number\n\tprivate terminated = false\n\tprivate lastHeardAt = 0\n\tprivate heardSinceWatch = false\n\tprivate watchdog: ReturnType<typeof setInterval> | undefined\n\n\t/**\n\t * @param channelName - Storage channel of the database\n\t * @param timeoutMs - Hard ceiling per request (default 30s)\n\t * @param heartbeatMs - Expected leader heartbeat interval, also the watchdog tick\n\t */\n\tconstructor(channelName: string, timeoutMs = 30000, heartbeatMs = DEFAULT_HEARTBEAT_MS) {\n\t\tthis.timeoutMs = timeoutMs\n\t\tthis.heartbeatMs = Math.max(10, Math.min(heartbeatMs, timeoutMs))\n\t\tthis.channel = new BroadcastChannel(channelName)\n\t\tif (typeof addEventListener === 'function') {\n\t\t\taddEventListener('pagehide', this.onPageHide)\n\t\t}\n\t\tthis.channel.addEventListener('message', (event: MessageEvent<ChannelMessage>) => {\n\t\t\tconst data = event.data\n\t\t\tif (\n\t\t\t\tdata?.type === LEADER_HEARTBEAT ||\n\t\t\t\tdata?.type === LEADER_PONG ||\n\t\t\t\tdata?.type === RPC_RESPONSE\n\t\t\t) {\n\t\t\t\tthis.markHeard()\n\t\t\t}\n\t\t\tif (data?.type !== RPC_RESPONSE) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst entry = this.pending.get(data.requestId)\n\t\t\tif (entry) {\n\t\t\t\tthis.pending.delete(data.requestId)\n\t\t\t\tentry.resolve(data.response)\n\t\t\t}\n\t\t})\n\t}\n\n\t/**\n\t * Readiness handshake: resolves `true` as soon as a live leader answers a ping,\n\t * or `false` if none answers within the budget. The adapter calls this before\n\t * its first RPC so a follower created before a leader relay is live retries the\n\t * handshake instead of firing into the void.\n\t */\n\tasync waitForLeader(timeoutMs = 3000, attempts = 3): Promise<boolean> {\n\t\tconst perAttempt = Math.max(50, Math.floor(timeoutMs / Math.max(1, attempts)))\n\t\tfor (let attempt = 0; attempt < attempts; attempt += 1) {\n\t\t\tif (this.terminated) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tif (await this.pingLeader(perAttempt)) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t\treturn false\n\t}\n\n\t/** Sends one liveness ping and resolves whether a leader answered in time. */\n\tprivate pingLeader(timeoutMs: number): Promise<boolean> {\n\t\tif (this.terminated) {\n\t\t\treturn Promise.resolve(false)\n\t\t}\n\t\treturn new Promise<boolean>((resolve) => {\n\t\t\tconst onPong = (event: MessageEvent<{ type: string }>): void => {\n\t\t\t\tif (event.data?.type === LEADER_PONG) {\n\t\t\t\t\tcleanup()\n\t\t\t\t\tresolve(true)\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst cleanup = (): void => {\n\t\t\t\tclearTimeout(timer)\n\t\t\t\tthis.channel.removeEventListener('message', onPong)\n\t\t\t}\n\t\t\tconst timer = setTimeout(() => {\n\t\t\t\tcleanup()\n\t\t\t\tresolve(false)\n\t\t\t}, timeoutMs)\n\t\t\tthis.channel.addEventListener('message', onPong)\n\t\t\tthis.channel.postMessage({ type: LEADER_PING })\n\t\t})\n\t}\n\n\tasync send(\n\t\trequest: WorkerRequest,\n\t\t_clientId?: 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: 'Follower bridge terminated',\n\t\t\t\tcode: 'BRIDGE_TERMINATED',\n\t\t\t}\n\t\t}\n\n\t\tconst requestId = request.requestId ?? createClientId()\n\t\tconst signal = options.signal\n\t\tif (signal?.aborted) {\n\t\t\tthrow new RequestAbortedError(request.type, requestId)\n\t\t}\n\t\tconst msg: RpcRequestMessage = {\n\t\t\ttype: RPC_REQUEST,\n\t\t\trequestId,\n\t\t\tclientId: this.clientId,\n\t\t\trequest: { ...request, requestId },\n\t\t}\n\t\tconst timeoutMs = options.timeoutMs ?? this.timeoutMs\n\n\t\treturn new Promise<WorkerResponse>((resolve, reject) => {\n\t\t\tlet timer: ReturnType<typeof setTimeout> | undefined\n\t\t\tconst onAbort = (): void => {\n\t\t\t\tsettle(() => reject(new RequestAbortedError(request.type, requestId)))\n\t\t\t}\n\t\t\tconst settle = (fn: () => void): void => {\n\t\t\t\tif (timer !== undefined) clearTimeout(timer)\n\t\t\t\tsignal?.removeEventListener('abort', onAbort)\n\t\t\t\tthis.pending.delete(requestId)\n\t\t\t\tthis.stopWatchdogIfIdle()\n\t\t\t\tfn()\n\t\t\t}\n\t\t\tif (Number.isFinite(timeoutMs)) {\n\t\t\t\ttimer = setTimeout(() => {\n\t\t\t\t\tsettle(() => reject(new WorkerTimeoutError(`follower-rpc:${request.type}`, timeoutMs)))\n\t\t\t\t}, timeoutMs)\n\t\t\t}\n\t\t\tsignal?.addEventListener('abort', onAbort, { once: true })\n\n\t\t\tthis.pending.set(requestId, {\n\t\t\t\ttype: request.type,\n\t\t\t\tresolve: (response) => settle(() => resolve(response)),\n\t\t\t\treject: (error) => settle(() => reject(error)),\n\t\t\t})\n\t\t\tthis.startWatchdog()\n\t\t\tthis.channel.postMessage(msg)\n\t\t})\n\t}\n\n\tterminate(): void {\n\t\tif (this.terminated) {\n\t\t\treturn\n\t\t}\n\t\tthis.terminated = true\n\t\tthis.leaveLeader()\n\t\tif (typeof removeEventListener === 'function') {\n\t\t\tremoveEventListener('pagehide', this.onPageHide)\n\t\t}\n\t\tthis.channel.close()\n\t\tfor (const [, entry] of [...this.pending]) {\n\t\t\tentry.reject(new BridgeTerminatedError(entry.type, 'follower bridge terminated'))\n\t\t}\n\t\tthis.pending.clear()\n\t\tthis.stopWatchdog()\n\t}\n\n\tprivate markHeard(): void {\n\t\tthis.lastHeardAt = Date.now()\n\t\tthis.heardSinceWatch = true\n\t}\n\n\tprivate startWatchdog(): void {\n\t\tif (this.watchdog !== undefined) return\n\t\t// A recent beat means a leader is known to exist; otherwise the silence\n\t\t// clock starts now.\n\t\tconst now = Date.now()\n\t\tthis.heardSinceWatch = now - this.lastHeardAt < this.heartbeatMs * 2\n\t\tif (!this.heardSinceWatch) this.lastHeardAt = now\n\t\tthis.watchdog = setInterval(() => this.checkLiveness(), this.heartbeatMs)\n\t}\n\n\tprivate stopWatchdogIfIdle(): void {\n\t\tif (this.pending.size === 0) this.stopWatchdog()\n\t}\n\n\tprivate stopWatchdog(): void {\n\t\tif (this.watchdog !== undefined) {\n\t\t\tclearInterval(this.watchdog)\n\t\t\tthis.watchdog = undefined\n\t\t}\n\t}\n\n\tprivate checkLiveness(): void {\n\t\tif (this.pending.size === 0) {\n\t\t\tthis.stopWatchdog()\n\t\t\treturn\n\t\t}\n\t\tconst silentMs = Date.now() - this.lastHeardAt\n\t\tconst heard = this.heardSinceWatch\n\t\tconst limit = this.heartbeatMs * (heard ? MISSED_BEATS_UNRESPONSIVE : MISSED_BEATS_ABSENT)\n\t\tif (silentMs >= limit) {\n\t\t\tfor (const [requestId, entry] of [...this.pending]) {\n\t\t\t\tentry.reject(\n\t\t\t\t\theard\n\t\t\t\t\t\t? new LeaderUnresponsiveError(`follower-rpc:${entry.type}`, silentMs, requestId)\n\t\t\t\t\t\t: new NoLeaderError(`follower-rpc:${entry.type}`),\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif (silentMs >= this.heartbeatMs) {\n\t\t\t// Pull a sign of life from leaders that do not push heartbeats.\n\t\t\tthis.channel.postMessage({ type: LEADER_PING })\n\t\t}\n\t}\n\n\tprivate leaveLeader(): void {\n\t\ttry {\n\t\t\tconst message: ClientLeaveMessage = {\n\t\t\t\ttype: CLIENT_LEAVE,\n\t\t\t\tclientId: this.clientId,\n\t\t\t}\n\t\t\tthis.channel.postMessage(message)\n\t\t} catch {\n\t\t\t// Best effort: the serializer's idle rollback is the guaranteed backstop.\n\t\t}\n\t}\n}\n\ninterface QueuedWorkerRequest {\n\tclientId: string\n\trequest: WorkerRequest\n\tresolve: (response: WorkerResponse) => void\n\treject: (error: Error) => void\n}\n\n/**\n * Serializes one SQLite worker across the leader tab and all follower tabs.\n *\n * SQLite transactions are represented as multiple worker messages\n * (`begin`, one or more reads/writes, then `commit`/`rollback`). Per-tab mutexes\n * cannot protect that span because follower messages converge at the leader.\n * This bridge promotes the worker boundary into the serialization point, so no\n * other client can interleave while a client owns an active transaction.\n */\nexport class TransactionSerializingWorkerBridge implements WorkerBridge {\n\tprivate readonly inner: WorkerBridge\n\tprivate readonly leaderClientId = createClientId()\n\tprivate readonly transactionIdleTimeoutMs: number\n\tprivate queue: QueuedWorkerRequest[] = []\n\tprivate activeTransactionClient: string | null = null\n\tprivate abortedClients = new Set<string>()\n\tprivate transactionIdleTimer: ReturnType<typeof setTimeout> | null = null\n\tprivate reclaiming = false\n\tprivate processing = false\n\tprivate terminated = false\n\n\tconstructor(inner: WorkerBridge, transactionIdleTimeoutMs = DEFAULT_TRANSACTION_IDLE_TIMEOUT_MS) {\n\t\tthis.inner = inner\n\t\tthis.transactionIdleTimeoutMs = transactionIdleTimeoutMs\n\t}\n\n\tsend(request: WorkerRequest, clientId = this.leaderClientId): Promise<WorkerResponse> {\n\t\tif (this.terminated) {\n\t\t\treturn Promise.resolve({\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\t\tif (this.abortedClients.has(clientId)) {\n\t\t\tif (request.type === 'begin') {\n\t\t\t\tthis.abortedClients.delete(clientId)\n\t\t\t} else {\n\t\t\t\treturn Promise.resolve({\n\t\t\t\t\tid: request.id,\n\t\t\t\t\ttype: 'error',\n\t\t\t\t\tmessage: 'Previous transaction was aborted because the client stopped sending requests.',\n\t\t\t\t\tcode: 'TRANSACTION_ABORTED',\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\n\t\treturn new Promise<WorkerResponse>((resolve, reject) => {\n\t\t\tthis.queue.push({ clientId, request, resolve, reject })\n\t\t\tvoid this.processQueue()\n\t\t})\n\t}\n\n\tterminate(): void {\n\t\tif (this.terminated) {\n\t\t\treturn\n\t\t}\n\t\tthis.terminated = true\n\t\tthis.clearTransactionIdleTimer()\n\t\tthis.inner.terminate()\n\t\tconst pending = this.queue.splice(0)\n\t\tfor (const entry of pending) {\n\t\t\tentry.reject(new BridgeTerminatedError(entry.request.type, 'worker terminated'))\n\t\t}\n\t}\n\n\treclaimClient(clientId: string, reason: string): void {\n\t\tif (this.terminated) {\n\t\t\treturn\n\t\t}\n\t\tvoid this.reclaimClientNow(clientId, reason)\n\t}\n\n\tprivate async processQueue(): Promise<void> {\n\t\tif (this.processing || this.reclaiming) {\n\t\t\treturn\n\t\t}\n\t\tthis.processing = true\n\t\ttry {\n\t\t\twhile (!this.terminated) {\n\t\t\t\tconst index = this.nextRunnableIndex()\n\t\t\t\tif (index === -1) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tconst [entry] = this.queue.splice(index, 1)\n\t\t\t\tif (!entry) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await this.inner.send(entry.request)\n\t\t\t\t\tthis.recordTransactionState(entry, response)\n\t\t\t\t\tentry.resolve(response)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis.resetFailedTransaction(entry)\n\t\t\t\t\tentry.reject(error instanceof Error ? error : new Error(String(error)))\n\t\t\t\t}\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.processing = false\n\t\t\tif (!this.terminated && !this.reclaiming && this.nextRunnableIndex() !== -1) {\n\t\t\t\tvoid this.processQueue()\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate nextRunnableIndex(): number {\n\t\tif (this.queue.length === 0) {\n\t\t\treturn -1\n\t\t}\n\t\tif (this.activeTransactionClient === null) {\n\t\t\treturn 0\n\t\t}\n\t\treturn this.queue.findIndex((entry) => entry.clientId === this.activeTransactionClient)\n\t}\n\n\tprivate recordTransactionState(entry: QueuedWorkerRequest, response: WorkerResponse): void {\n\t\tif (response.type === 'error') {\n\t\t\tthis.resetFailedTransaction(entry)\n\t\t\treturn\n\t\t}\n\t\tif (entry.request.type === 'begin') {\n\t\t\tthis.activeTransactionClient = entry.clientId\n\t\t\tthis.armTransactionIdleTimer(entry.clientId)\n\t\t\treturn\n\t\t}\n\t\tif (this.activeTransactionClient === entry.clientId) {\n\t\t\tthis.armTransactionIdleTimer(entry.clientId)\n\t\t}\n\t\tif (\n\t\t\tthis.activeTransactionClient === entry.clientId &&\n\t\t\t(entry.request.type === 'commit' || entry.request.type === 'rollback')\n\t\t) {\n\t\t\tthis.activeTransactionClient = null\n\t\t\tthis.clearTransactionIdleTimer()\n\t\t}\n\t}\n\n\tprivate resetFailedTransaction(entry: QueuedWorkerRequest): void {\n\t\tif (this.activeTransactionClient === entry.clientId) {\n\t\t\tthis.activeTransactionClient = null\n\t\t\tthis.clearTransactionIdleTimer()\n\t\t}\n\t}\n\n\tprivate armTransactionIdleTimer(clientId: string): void {\n\t\tthis.clearTransactionIdleTimer()\n\t\tthis.transactionIdleTimer = setTimeout(() => {\n\t\t\tvoid this.reclaimClientNow(clientId, 'transaction-idle-timeout')\n\t\t}, this.transactionIdleTimeoutMs)\n\t}\n\n\tprivate clearTransactionIdleTimer(): void {\n\t\tif (this.transactionIdleTimer) {\n\t\t\tclearTimeout(this.transactionIdleTimer)\n\t\t\tthis.transactionIdleTimer = null\n\t\t}\n\t}\n\n\tprivate async reclaimClientNow(clientId: string, reason: string): Promise<void> {\n\t\tif (this.reclaiming || this.activeTransactionClient !== clientId) {\n\t\t\treturn\n\t\t}\n\t\tthis.reclaiming = true\n\t\tthis.clearTransactionIdleTimer()\n\t\tthis.activeTransactionClient = null\n\t\tthis.abortedClients.add(clientId)\n\t\tthis.rejectQueuedClientRequests(clientId, reason)\n\n\t\ttry {\n\t\t\tconst response = await this.inner.send({ id: 0, type: 'rollback' })\n\t\t\tif (response.type === 'error') {\n\t\t\t\t// If there was no active SQLite transaction left, the important state\n\t\t\t\t// is already reclaimed in JS. The next request will establish a fresh\n\t\t\t\t// transaction if needed.\n\t\t\t}\n\t\t} catch {\n\t\t\t// Keep the queue moving. The inner worker will surface any unrecoverable\n\t\t\t// state through subsequent requests.\n\t\t} finally {\n\t\t\tthis.reclaiming = false\n\t\t\tif (!this.terminated && this.nextRunnableIndex() !== -1) {\n\t\t\t\tvoid this.processQueue()\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate rejectQueuedClientRequests(clientId: string, reason: string): void {\n\t\tconst keep: QueuedWorkerRequest[] = []\n\t\tfor (const entry of this.queue) {\n\t\t\tif (entry.clientId === clientId) {\n\t\t\t\tentry.resolve({\n\t\t\t\t\tid: entry.request.id,\n\t\t\t\t\ttype: 'error',\n\t\t\t\t\tmessage: `Transaction client was reclaimed: ${reason}`,\n\t\t\t\t\tcode: 'TRANSACTION_ABORTED',\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tkeep.push(entry)\n\t\t\t}\n\t\t}\n\t\tthis.queue = keep\n\t}\n}\n\nfunction hasReclaimClient(bridge: WorkerBridge): bridge is ReclaimingWorkerBridge {\n\treturn typeof (bridge as Partial<ReclaimingWorkerBridge>).reclaimClient === 'function'\n}\n\nfunction createClientId(): string {\n\tif (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {\n\t\treturn crypto.randomUUID()\n\t}\n\treturn `${Date.now()}-${Math.random().toString(36).slice(2)}`\n}\n","import { quoteIdent } from '@korajs/core'\nimport { PersistenceError } from '../errors'\n\n/**\n * Logical copy of every user table of a SQLite database. Used to move a\n * database between storage backends (OPFS <-> IndexedDB) explicitly, and as the\n * IndexedDB adapter's durable snapshot format.\n */\nexport interface DatabaseDump {\n\ttables: Array<{\n\t\tname: string\n\t\t/**\n\t\t * The table's `CREATE TABLE` statement (from `sqlite_master`), so a restore can\n\t\t * recreate a table the target database does not have yet with its constraints.\n\t\t * Absent in dumps written before it was recorded.\n\t\t */\n\t\tsql?: string\n\t\tcolumns: string[]\n\t\trows: Array<Record<string, unknown>>\n\t}>\n}\n\n/** A parameterized SQL statement. */\nexport interface DumpStatement {\n\tsql: string\n\tparams?: unknown[]\n}\n\ntype QueryFn = <T>(sql: string, params?: unknown[]) => Promise<T[]>\n\n/** Throws unless `identifier` is a plain SQL identifier (dump data is untrusted). */\nexport function ensureSafeIdentifier(identifier: string): string {\n\tif (!/^[a-zA-Z0-9_]+$/.test(identifier)) {\n\t\tthrow new PersistenceError(`Unsafe SQL identifier: ${identifier}`, {\n\t\t\tcode: 'UNSAFE_IDENTIFIER',\n\t\t\tidentifier,\n\t\t})\n\t}\n\treturn identifier\n}\n\n/**\n * Read every user table through `query`. Pass a transaction's `query` to get a\n * consistent snapshot.\n */\nexport async function exportDump(query: QueryFn): Promise<DatabaseDump> {\n\tconst tableRows = await query<{ name: string; sql: string | null }>(\n\t\t\"SELECT name, sql FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'\",\n\t)\n\tconst tables: DatabaseDump['tables'] = []\n\tfor (const tableRow of tableRows) {\n\t\tconst tableName = ensureSafeIdentifier(tableRow.name)\n\t\tconst columns = await query<{ name: string }>(`PRAGMA table_info(${quoteIdent(tableName)})`)\n\t\tconst rows = await query<Record<string, unknown>>(`SELECT * FROM ${quoteIdent(tableName)}`)\n\t\ttables.push({\n\t\t\tname: tableName,\n\t\t\t...(typeof tableRow.sql === 'string' ? { sql: tableRow.sql } : {}),\n\t\t\tcolumns: columns.map((column) => column.name),\n\t\t\trows,\n\t\t})\n\t}\n\treturn { tables }\n}\n\n/**\n * Statements that replace the contents of each dumped table with the dump's\n * rows. Run them inside one transaction so a failure leaves the target as it was.\n *\n * A table the target does not have yet (the store creates some of its bookkeeping\n * tables after the schema DDL) is created first from the dump's recorded `CREATE TABLE`\n * statement, constraints included. A dump written before statements were recorded\n * creates it untyped, so its rows are still restored.\n *\n * @param createMissingTables - Kept for callers that restore into scratch databases\n * with no schema; missing tables are always created now.\n */\nexport function restoreDumpStatements(\n\tdump: DatabaseDump,\n\t_createMissingTables = true,\n): DumpStatement[] {\n\tconst statements: DumpStatement[] = []\n\tfor (const table of dump.tables) {\n\t\tconst name = quoteIdent(ensureSafeIdentifier(table.name))\n\t\tconst create = createTableIfNotExists(table.name, table.sql)\n\t\tif (create) {\n\t\t\tstatements.push({ sql: create })\n\t\t} else {\n\t\t\tconst columns = table.columns.map((column) => quoteIdent(ensureSafeIdentifier(column)))\n\t\t\tif (columns.length === 0) continue\n\t\t\tstatements.push({ sql: `CREATE TABLE IF NOT EXISTS ${name} (${columns.join(', ')})` })\n\t\t}\n\t\tstatements.push({ sql: `DELETE FROM ${name}` })\n\t\tfor (const row of table.rows) {\n\t\t\tconst columns = table.columns.filter((column) =>\n\t\t\t\tObject.prototype.hasOwnProperty.call(row, column),\n\t\t\t)\n\t\t\tif (columns.length === 0) continue\n\t\t\tconst quotedColumns = columns\n\t\t\t\t.map((column) => quoteIdent(ensureSafeIdentifier(column)))\n\t\t\t\t.join(', ')\n\t\t\tstatements.push({\n\t\t\t\tsql: `INSERT INTO ${name} (${quotedColumns}) VALUES (${columns.map(() => '?').join(', ')})`,\n\t\t\t\tparams: columns.map((column) => row[column]),\n\t\t\t})\n\t\t}\n\t}\n\treturn statements\n}\n\n/**\n * The recorded `CREATE TABLE` statement made idempotent, or null when there is none or\n * it is not a single plain `CREATE TABLE` of exactly this table (dump data is untrusted).\n */\nfunction createTableIfNotExists(tableName: string, sql: string | undefined): string | null {\n\tif (typeof sql !== 'string' || sql.includes(';')) return null\n\tconst match = /^\\s*CREATE\\s+TABLE\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?(\"?)([A-Za-z0-9_]+)\\1\\s*\\(/i.exec(\n\t\tsql,\n\t)\n\tif (!match || match[2] !== tableName) return null\n\treturn `CREATE TABLE IF NOT EXISTS ${quoteIdent(tableName)} ${sql.slice(match[0].length - 1)}`\n}\n","/// <reference lib=\"dom\" />\nimport { PersistenceError } from '../errors'\n\nconst IDB_DATABASE_NAME = 'kora-persistence'\nconst IDB_STORE_NAME = 'databases'\nconst IDB_VERSION = 1\n\nconst DUMP_SUFFIX = '::dump'\n\n/**\n * Returns true when an IndexedDB error indicates storage quota was exceeded.\n */\nexport function isIndexedDbQuotaError(error: unknown): boolean {\n\tif (error instanceof DOMException && error.name === 'QuotaExceededError') {\n\t\treturn true\n\t}\n\tif (error instanceof PersistenceError && error.context?.quotaExceeded === true) {\n\t\treturn true\n\t}\n\tif (error instanceof Error && /quota/i.test(error.message)) {\n\t\treturn true\n\t}\n\treturn false\n}\n\nfunction rejectWithIdbError(\n\treject: (reason: unknown) => void,\n\tmessage: string,\n\tcontext: Record<string, unknown>,\n\tevent: Event,\n): void {\n\tconst domError = (event.target as IDBRequest | IDBTransaction | undefined)?.error\n\tif (domError && isIndexedDbQuotaError(domError)) {\n\t\treject(\n\t\t\tnew PersistenceError(message, {\n\t\t\t\t...context,\n\t\t\t\tquotaExceeded: true,\n\t\t\t\tcause: domError.message,\n\t\t\t}),\n\t\t)\n\t\treturn\n\t}\n\treject(new PersistenceError(message, context))\n}\n\n/**\n * Open the IndexedDB database used for SQLite persistence.\n * Creates the object store on first access.\n */\nfunction openIdb(): Promise<IDBDatabase> {\n\treturn new Promise<IDBDatabase>((resolve, reject) => {\n\t\tconst request = indexedDB.open(IDB_DATABASE_NAME, IDB_VERSION)\n\t\trequest.onupgradeneeded = () => {\n\t\t\tconst db = request.result\n\t\t\tif (!db.objectStoreNames.contains(IDB_STORE_NAME)) {\n\t\t\t\tdb.createObjectStore(IDB_STORE_NAME)\n\t\t\t}\n\t\t}\n\t\trequest.onsuccess = () => resolve(request.result)\n\t\trequest.onerror = () =>\n\t\t\treject(\n\t\t\t\tnew PersistenceError(\n\t\t\t\t\t`Failed to open IndexedDB: ${request.error?.message ?? 'unknown error'}`,\n\t\t\t\t\t{ database: IDB_DATABASE_NAME },\n\t\t\t\t),\n\t\t\t)\n\t})\n}\n\n/**\n * Save a serialized SQLite database to IndexedDB.\n *\n * @param dbName - Key under which to store the data\n * @param data - Serialized database as Uint8Array\n */\nexport async function saveToIndexedDB(dbName: string, data: Uint8Array): Promise<void> {\n\tconst idb = await openIdb()\n\ttry {\n\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\tconst tx = idb.transaction(IDB_STORE_NAME, 'readwrite')\n\t\t\tconst store = tx.objectStore(IDB_STORE_NAME)\n\t\t\tstore.put(data, dbName)\n\t\t\ttx.oncomplete = () => resolve()\n\t\t\ttx.onerror = (event) =>\n\t\t\t\trejectWithIdbError(\n\t\t\t\t\treject,\n\t\t\t\t\t`Failed to save database \"${dbName}\" to IndexedDB`,\n\t\t\t\t\t{ dbName },\n\t\t\t\t\tevent,\n\t\t\t\t)\n\t\t})\n\t} finally {\n\t\tidb.close()\n\t}\n}\n\n/**\n * Save a logical SQL dump payload to IndexedDB for import-fallback restore.\n */\nexport async function saveDumpToIndexedDB(dbName: string, dump: unknown): Promise<void> {\n\tconst idb = await openIdb()\n\ttry {\n\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\tconst tx = idb.transaction(IDB_STORE_NAME, 'readwrite')\n\t\t\tconst store = tx.objectStore(IDB_STORE_NAME)\n\t\t\tstore.put(dump, `${dbName}${DUMP_SUFFIX}`)\n\t\t\ttx.oncomplete = () => resolve()\n\t\t\ttx.onerror = (event) =>\n\t\t\t\trejectWithIdbError(\n\t\t\t\t\treject,\n\t\t\t\t\t`Failed to save dump for database \"${dbName}\"`,\n\t\t\t\t\t{ dbName },\n\t\t\t\t\tevent,\n\t\t\t\t)\n\t\t})\n\t} finally {\n\t\tidb.close()\n\t}\n}\n\n/**\n * Load a serialized SQLite database from IndexedDB.\n *\n * @param dbName - Key under which the data was stored\n * @returns The serialized database, or null if not found\n */\nexport async function loadFromIndexedDB(dbName: string): Promise<Uint8Array | null> {\n\tconst idb = await openIdb()\n\ttry {\n\t\treturn await new Promise<Uint8Array | null>((resolve, reject) => {\n\t\t\tconst tx = idb.transaction(IDB_STORE_NAME, 'readonly')\n\t\t\tconst store = tx.objectStore(IDB_STORE_NAME)\n\t\t\tconst request = store.get(dbName)\n\t\t\trequest.onsuccess = () => {\n\t\t\t\tconst result = request.result\n\t\t\t\tif (result instanceof Uint8Array) {\n\t\t\t\t\tresolve(result)\n\t\t\t\t} else if (result) {\n\t\t\t\t\t// Handle ArrayBuffer or other typed array forms\n\t\t\t\t\tresolve(new Uint8Array(result as ArrayBuffer))\n\t\t\t\t} else {\n\t\t\t\t\tresolve(null)\n\t\t\t\t}\n\t\t\t}\n\t\t\trequest.onerror = () =>\n\t\t\t\treject(\n\t\t\t\t\tnew PersistenceError(`Failed to load database \"${dbName}\" from IndexedDB`, { dbName }),\n\t\t\t\t)\n\t\t})\n\t} finally {\n\t\tidb.close()\n\t}\n}\n\n/**\n * Load a logical SQL dump payload from IndexedDB.\n */\nexport async function loadDumpFromIndexedDB<T>(dbName: string): Promise<T | null> {\n\tconst idb = await openIdb()\n\ttry {\n\t\treturn await new Promise<T | null>((resolve, reject) => {\n\t\t\tconst tx = idb.transaction(IDB_STORE_NAME, 'readonly')\n\t\t\tconst store = tx.objectStore(IDB_STORE_NAME)\n\t\t\tconst request = store.get(`${dbName}${DUMP_SUFFIX}`)\n\t\t\trequest.onsuccess = () => {\n\t\t\t\tresolve((request.result as T | undefined) ?? null)\n\t\t\t}\n\t\t\trequest.onerror = () =>\n\t\t\t\treject(\n\t\t\t\t\tnew PersistenceError(`Failed to load dump for database \"${dbName}\" from IndexedDB`, {\n\t\t\t\t\t\tdbName,\n\t\t\t\t\t}),\n\t\t\t\t)\n\t\t})\n\t} finally {\n\t\tidb.close()\n\t}\n}\n\n/**\n * Delete only the binary SQLite database snapshot from IndexedDB.\n *\n * The logical dump is stored under a separate key and may be the authoritative\n * durable fallback when a browser worker cannot export a binary SQLite image.\n */\nexport async function deleteSnapshotFromIndexedDB(dbName: string): Promise<void> {\n\tconst idb = await openIdb()\n\ttry {\n\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\tconst tx = idb.transaction(IDB_STORE_NAME, 'readwrite')\n\t\t\tconst store = tx.objectStore(IDB_STORE_NAME)\n\t\t\tstore.delete(dbName)\n\t\t\ttx.oncomplete = () => resolve()\n\t\t\ttx.onerror = () =>\n\t\t\t\treject(\n\t\t\t\t\tnew PersistenceError(`Failed to delete database snapshot \"${dbName}\" from IndexedDB`, {\n\t\t\t\t\t\tdbName,\n\t\t\t\t\t}),\n\t\t\t\t)\n\t\t})\n\t} finally {\n\t\tidb.close()\n\t}\n}\n\n/**\n * Delete a serialized SQLite database from IndexedDB.\n *\n * @param dbName - Key to delete\n */\nexport async function deleteFromIndexedDB(dbName: string): Promise<void> {\n\tconst idb = await openIdb()\n\ttry {\n\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\tconst tx = idb.transaction(IDB_STORE_NAME, 'readwrite')\n\t\t\tconst store = tx.objectStore(IDB_STORE_NAME)\n\t\t\tstore.delete(dbName)\n\t\t\tstore.delete(`${dbName}${DUMP_SUFFIX}`)\n\t\t\ttx.oncomplete = () => resolve()\n\t\t\ttx.onerror = () =>\n\t\t\t\treject(\n\t\t\t\t\tnew PersistenceError(`Failed to delete database \"${dbName}\" from IndexedDB`, { dbName }),\n\t\t\t\t)\n\t\t})\n\t} finally {\n\t\tidb.close()\n\t}\n}\n","/// <reference lib=\"dom\" />\n/**\n * Kora-owned manifest of the local databases this origin created (W8a).\n *\n * One record per database name, kept in its own IndexedDB database so it is\n * readable from any tab without opening the OPFS pools. It records which backend\n * holds the database's data, which is how Kora keeps OPFS and IndexedDB from\n * ever holding two disjoint copies: a backend switch is a recorded, explicit\n * migration, never a silent fresh start (NEW-STORE-5, LMS-5c).\n *\n * The manifest is advisory for listing and authoritative for the backend\n * choice. Nothing is ever evicted automatically; deletion is the explicit\n * `deleteDatabase()` API.\n */\n\nconst MANIFEST_DB_NAME = 'kora-manifest'\nconst MANIFEST_STORE = 'databases'\nconst MANIFEST_VERSION = 1\n\n/** Where a database's data lives. */\nexport type LocalDatabaseBackend = 'opfs' | 'indexeddb'\n\n/** One manifest entry, as returned by `listDatabases()`. */\nexport interface LocalDatabaseRecord {\n\t/** The store name (`store.name`, plus the per-user suffix if namespaced). */\n\tdbName: string\n\t/** The backend that holds the authoritative copy. */\n\tbackend: LocalDatabaseBackend\n\t/** OPFS pool holding the file (opfs backend only). */\n\tpoolName?: string\n\t/** Wall-clock milliseconds; informational only, never used for ordering. */\n\tcreatedAt: number\n\t/** Wall-clock milliseconds of the last durable open; informational only. */\n\tlastOpenedAt: number\n}\n\n/** True when this runtime can keep a manifest (browsers; not Node tests). */\nexport function isManifestAvailable(): boolean {\n\treturn typeof indexedDB !== 'undefined'\n}\n\nfunction openManifest(): Promise<IDBDatabase> {\n\treturn new Promise<IDBDatabase>((resolve, reject) => {\n\t\tconst request = indexedDB.open(MANIFEST_DB_NAME, MANIFEST_VERSION)\n\t\trequest.onupgradeneeded = () => {\n\t\t\tconst db = request.result\n\t\t\tif (!db.objectStoreNames.contains(MANIFEST_STORE)) {\n\t\t\t\tdb.createObjectStore(MANIFEST_STORE, { keyPath: 'dbName' })\n\t\t\t}\n\t\t}\n\t\trequest.onsuccess = () => resolve(request.result)\n\t\trequest.onerror = () =>\n\t\t\treject(request.error ?? new Error('Failed to open the Kora storage manifest'))\n\t})\n}\n\nasync function withStore<T>(\n\tmode: IDBTransactionMode,\n\trun: (store: IDBObjectStore) => IDBRequest<T> | null,\n): Promise<T | undefined> {\n\tconst db = await openManifest()\n\ttry {\n\t\treturn await new Promise<T | undefined>((resolve, reject) => {\n\t\t\tconst tx = db.transaction(MANIFEST_STORE, mode)\n\t\t\tconst request = run(tx.objectStore(MANIFEST_STORE))\n\t\t\tlet result: T | undefined\n\t\t\tif (request) {\n\t\t\t\trequest.onsuccess = () => {\n\t\t\t\t\tresult = request.result\n\t\t\t\t}\n\t\t\t}\n\t\t\ttx.oncomplete = () => resolve(result)\n\t\t\ttx.onerror = () => reject(tx.error ?? new Error('Kora storage manifest transaction failed'))\n\t\t\ttx.onabort = () => reject(tx.error ?? new Error('Kora storage manifest transaction aborted'))\n\t\t})\n\t} finally {\n\t\tdb.close()\n\t}\n}\n\nfunction isRecord(value: unknown): value is LocalDatabaseRecord {\n\tif (typeof value !== 'object' || value === null) return false\n\tconst record = value as Partial<LocalDatabaseRecord>\n\treturn (\n\t\ttypeof record.dbName === 'string' &&\n\t\t(record.backend === 'opfs' || record.backend === 'indexeddb')\n\t)\n}\n\n/** Read the manifest entry for a database, or null when Kora never recorded it. */\nexport async function readManifestRecord(dbName: string): Promise<LocalDatabaseRecord | null> {\n\tif (!isManifestAvailable()) return null\n\tconst value = await withStore<unknown>('readonly', (store) => store.get(dbName))\n\treturn isRecord(value) ? value : null\n}\n\n/** Every database Kora recorded on this origin, sorted by name. */\nexport async function listManifestRecords(): Promise<LocalDatabaseRecord[]> {\n\tif (!isManifestAvailable()) return []\n\tconst values = await withStore<unknown[]>('readonly', (store) => store.getAll())\n\treturn (values ?? []).filter(isRecord).sort((a, b) => (a.dbName < b.dbName ? -1 : 1))\n}\n\n/**\n * Record (or refresh) which backend holds a database after a durable open,\n * keeping the original creation time.\n */\nexport async function recordDatabase(entry: {\n\tdbName: string\n\tbackend: LocalDatabaseBackend\n\tpoolName?: string\n}): Promise<void> {\n\tif (!isManifestAvailable()) return\n\tconst existing = await readManifestRecord(entry.dbName)\n\tconst now = Date.now()\n\tconst record: LocalDatabaseRecord = {\n\t\tdbName: entry.dbName,\n\t\tbackend: entry.backend,\n\t\t...(entry.poolName ? { poolName: entry.poolName } : {}),\n\t\tcreatedAt: existing?.createdAt ?? now,\n\t\tlastOpenedAt: now,\n\t}\n\tawait withStore('readwrite', (store) => store.put(record))\n}\n\n/** Remove a database's manifest entry (after its data was deleted). */\nexport async function deleteManifestRecord(dbName: string): Promise<void> {\n\tif (!isManifestAvailable()) return\n\tawait withStore('readwrite', (store) => store.delete(dbName))\n}\n","/// <reference lib=\"dom\" />\n/**\n * Explicit management of the local databases on this origin (W8a, NEW-STORE-7).\n *\n * Kora never evicts a database on its own: a full pool is prevented by\n * per-database pools and capacity reservation, and removing data is always an\n * explicit call that refuses while the database is open or still holds\n * operations the server has not acknowledged.\n */\n\nimport { AdapterError, StorageInUseError, UnsyncedDataError } from '../errors'\nimport { type DatabaseDump, restoreDumpStatements } from './database-dump'\nimport { leaderLockName } from './opfs-names'\nimport type { WorkerRequest, WorkerResponse } from './sqlite-wasm-channel'\nimport { deleteFromIndexedDB, loadDumpFromIndexedDB } from './sqlite-wasm-persistence'\nimport {\n\ttype LocalDatabaseRecord,\n\tdeleteManifestRecord,\n\tlistManifestRecords,\n\treadManifestRecord,\n} from './storage-manifest'\n\n/** Read-only access to a database handed to the unsynced-data check. */\nexport interface LocalDatabaseReader {\n\tdbName: string\n\tquery<T>(sql: string, params?: unknown[]): Promise<T[]>\n}\n\n/** Options for {@link deleteLocalDatabase}. */\nexport interface DeleteLocalDatabaseOptions {\n\t/** URL of the Kora SQLite worker (the same one the app's store uses). */\n\tworkerUrl: string | URL\n\t/**\n\t * Decides whether the database still holds operations the server never\n\t * acknowledged. Injected by the app layer, which knows its sync setup.\n\t * When it returns true the delete is refused with {@link UnsyncedDataError}.\n\t */\n\thasUnsyncedOperations?: (db: LocalDatabaseReader) => Promise<boolean>\n\t/** Delete even when unsynced operations exist (they are lost). */\n\tforce?: boolean\n\t/** Worker response timeout. Defaults to 30000ms. */\n\tworkerResponseTimeoutMs?: number\n}\n\n/**\n * Databases Kora has recorded on this origin (created or opened since W8a).\n * A database still in the pre-W8a shared pool appears after its first open.\n */\nexport async function listLocalDatabases(): Promise<LocalDatabaseRecord[]> {\n\treturn listManifestRecords()\n}\n\n/**\n * Hold a database's leader lock for the duration of `run`, so no tab can open\n * it meanwhile. Throws {@link StorageInUseError} when it is open somewhere.\n */\nasync function withExclusiveDatabase<T>(dbName: string, run: () => Promise<T>): Promise<T> {\n\tconst locks = typeof navigator !== 'undefined' ? navigator.locks : undefined\n\tif (!locks || typeof locks.request !== 'function') {\n\t\treturn run()\n\t}\n\tlet ran = false\n\tlet result: T | undefined\n\tawait locks.request(\n\t\tleaderLockName(dbName),\n\t\t{ mode: 'exclusive', ifAvailable: true },\n\t\tasync (lock) => {\n\t\t\tif (lock === null) return\n\t\t\tran = true\n\t\t\tresult = await run()\n\t\t},\n\t)\n\tif (!ran) {\n\t\tthrow new StorageInUseError(dbName)\n\t}\n\treturn result as T\n}\n\n/**\n * Permanently delete a local database and its OPFS pool or IndexedDB copy.\n *\n * Refuses with {@link StorageInUseError} while any tab has the database open,\n * and with {@link UnsyncedDataError} while `hasUnsyncedOperations` reports\n * unsynced operations (unless `force`). Never touches any other database.\n *\n * @returns true when something was deleted, false when the database did not exist\n */\nexport async function deleteLocalDatabase(\n\tdbName: string,\n\toptions: DeleteLocalDatabaseOptions,\n): Promise<boolean> {\n\tconst record = await readManifestRecord(dbName)\n\tconst backend = record?.backend ?? 'opfs'\n\treturn withExclusiveDatabase(dbName, async () => {\n\t\tconst { WebWorkerBridge } = await import('./sqlite-wasm-channel')\n\t\tconst worker = new WebWorkerBridge(options.workerUrl, options.workerResponseTimeoutMs ?? 30_000)\n\t\tconst send = async (request: WorkerRequest): Promise<WorkerResponse> => worker.send(request)\n\t\tconst reader: LocalDatabaseReader = {\n\t\t\tdbName,\n\t\t\tquery: async <T>(sql: string, params?: unknown[]): Promise<T[]> => {\n\t\t\t\tconst response = await send({ id: 0, type: 'query', sql, params })\n\t\t\t\tif (response.type === 'error') {\n\t\t\t\t\tthrow new AdapterError(`Query failed: ${response.message}`, { dbName, sql })\n\t\t\t\t}\n\t\t\t\treturn (response.data as T[]) ?? []\n\t\t\t},\n\t\t}\n\t\ttry {\n\t\t\tlet exists: boolean\n\t\t\tif (backend === 'opfs') {\n\t\t\t\tconst opened = await worker.send(\n\t\t\t\t\t{ id: 0, type: 'open', ddlStatements: [], dbName, mustExist: true },\n\t\t\t\t\tundefined,\n\t\t\t\t\t{ timeoutMs: Number.POSITIVE_INFINITY },\n\t\t\t\t)\n\t\t\t\tif (opened.type === 'error' && opened.code !== 'NOT_FOUND') {\n\t\t\t\t\tthrow new AdapterError(`Could not open \"${dbName}\" for deletion: ${opened.message}`, {\n\t\t\t\t\t\tdbName,\n\t\t\t\t\t\tcode: opened.code,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\texists = opened.type === 'success'\n\t\t\t} else {\n\t\t\t\tconst dump = await loadDumpFromIndexedDB<DatabaseDump>(dbName)\n\t\t\t\texists = dump !== null\n\t\t\t\tif (dump) {\n\t\t\t\t\tawait send({ id: 0, type: 'open', ddlStatements: [], dbName, storage: 'memory' })\n\t\t\t\t\tfor (const statement of restoreDumpStatements(dump, true)) {\n\t\t\t\t\t\tawait send({ id: 0, type: 'execute', sql: statement.sql, params: statement.params })\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (exists && !options.force && options.hasUnsyncedOperations) {\n\t\t\t\tif (await options.hasUnsyncedOperations(reader)) {\n\t\t\t\t\tthrow new UnsyncedDataError(dbName)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (backend === 'opfs' && exists) {\n\t\t\t\tconst destroyed = await send({ id: 0, type: 'destroy' })\n\t\t\t\tif (destroyed.type === 'error') {\n\t\t\t\t\tthrow new AdapterError(`Could not delete \"${dbName}\": ${destroyed.message}`, { dbName })\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (backend === 'indexeddb') {\n\t\t\t\tawait deleteFromIndexedDB(dbName)\n\t\t\t}\n\t\t\tawait deleteManifestRecord(dbName)\n\t\t\treturn exists || record !== null\n\t\t} finally {\n\t\t\tworker.terminate()\n\t\t}\n\t})\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA,iBAgDa,cAiBA,wBAsCA,mBAoBA,oBAiBA,eAkBA,yBAiBA,uBAYA,qBAeA,mBAeA,mBA8BA;AAvPb;AAAA;AAAA;AAAA,kBAA0B;AAgDnB,IAAM,eAAN,cAA2B,sBAAU;AAAA,MAC3C,YAAY,SAAiB,SAAmC;AAC/D,cAAM,SAAS,iBAAiB,OAAO;AACvC,aAAK,OAAO;AAAA,MACb;AAAA,IACD;AAYO,IAAM,yBAAN,cAAqC,sBAAU;AAAA,MACrD,YAAY,QAAgB,OAA6B,QAAgB;AACxE;AAAA,UACC,aAAa,MAAM,6BAA6B,MAAM,WAAW,KAAK;AAAA,UACtE;AAAA,UACA,EAAE,QAAQ,OAAO,OAAO;AAAA,QACzB;AACA,aAAK,OAAO;AAAA,MACb;AAAA,IACD;AA6BO,IAAM,oBAAN,cAAgC,sBAAU;AAAA,MAChD,cAAc;AACb,cAAM,sEAAsE,gBAAgB;AAC5F,a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