{"version":3,"sources":["/home/runner/work/kora/kora/packages/store/dist/adapters/sqlite-wasm.cjs","../../src/adapters/local-databases.ts"],"names":["WebWorkerBridge"],"mappings":"AAAA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACF,yDAA8B;AAC9B;AACE;AACF,yDAA8B;AAC9B,iCAA8B;AAC9B,iCAA8B;AAC9B;AACE;AACF,yDAA8B;AAC9B,iCAA8B;AAC9B;AACE;AACA;AACF,yDAA8B;AAC9B;AACE;AACA;AACA;AACA;AACF,yDAA8B;AAC9B,iCAA8B;AAC9B;AACA;AC+BA,MAAA,SAAsB,kBAAA,CAAA,EAAqD;AAC1E,EAAA,OAAO,mDAAA,CAAoB;AAC5B;AAaA,MAAA,SAAsB,mBAAA,CACrB,MAAA,EACA,OAAA,EACsB;AACtB,EAAA,MAAM,OAAA,EAAS,MAAM,kDAAA,MAAyB,CAAA;AAC9C,EAAA,GAAA,CAAI,CAAC,MAAA,EAAQ,MAAM,IAAI,iDAAA,CAA2B,MAAM,CAAA;AAExD,EAAA,OAAO,qBAAA,CAAsB,MAAA,EAAQ,MAAA,CAAA,EAAA,GAAY;AAChD,IAAA,MAAM,EAAE,eAAA,EAAAA,iBAAgB,EAAA,EAAI,MAAM,4DAAA,CAAO,qCAAuB,GAAA;AAChE,IAAA,MAAM,OAAA,EAAS,IAAIA,gBAAAA,CAAgB,OAAA,CAAQ,SAAA,mBAAW,OAAA,CAAQ,uBAAA,UAA2B,KAAM,CAAA;AAC/F,IAAA,MAAM,KAAA,EAAO,CAAC,OAAA,EAAA,GAAoD,MAAA,CAAO,IAAA,CAAK,OAAO,CAAA;AACrF,IAAA,IAAI;AACH,MAAA,GAAA,CAAI,MAAA,CAAO,QAAA,IAAY,MAAA,EAAQ;AAC9B,QAAA,MAAM,OAAA,EAAS,MAAM,IAAA,CAAK;AAAA,UACzB,EAAA,EAAI,CAAA;AAAA,UACJ,IAAA,EAAM,MAAA;AAAA,UACN,aAAA,EAAe,CAAC,CAAA;A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EAAA;AACP,IAAA;AACqB,IAAA;AAC6B,IAAA;AACvD,IAAA;AACnC,MAAA;AACmE,MAAA;AACD,QAAA;AAClC,QAAA;AAC6C,UAAA;AAC5E,QAAA;AACkC,QAAA;AACnC,MAAA;AACD,IAAA;AACI,IAAA;AACC,MAAA;AACoB,MAAA;AACK,QAAA;AACuC,UAAA;AAClE,UAAA;AACsC,UAAA;AACvC,QAAA;AAC4D,QAAA;AACe,UAAA;AACzE,YAAA;AACa,YAAA;AACb,UAAA;AACF,QAAA;AACyB,QAAA;AACnB,MAAA;AACuD,QAAA;AAC3C,QAAA;AACR,QAAA;AACuE,UAAA;AACrB,UAAA;AACiB,YAAA;AAC5E,UAAA;AACD,QAAA;AACD,MAAA;AAE+D,MAAA;AACb,QAAA;AACd,UAAA;AACnC,QAAA;AACD,MAAA;AAIyB,MAAA;AACQ,MAAA;AAEC,MAAA;AACsB,QAAA;AACvB,QAAA;AACgD,UAAA;AAChF,QAAA;AACD,MAAA;AAC6B,MAAA;AACI,QAAA;AACjC,MAAA;AAC4B,MAAA;AAC3B,IAAA;AACgB,MAAA;AAClB,IAAA;AACA,EAAA;AACF;ADrE0F;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/kora/kora/packages/store/dist/adapters/sqlite-wasm.cjs","sourcesContent":[null,"/// <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 {\n\tAdapterError,\n\tLocalDatabaseNotFoundError,\n\tStorageInUseError,\n\tUnsyncedDataError,\n} 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 { clearStorageMarker } from './storage-evidence'\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/** Options for {@link exportLocalDatabase}. */\nexport interface ExportLocalDatabaseOptions {\n\t/** URL of the Kora SQLite worker (the same one the app's store uses). */\n\tworkerUrl: string | URL\n\t/** Worker response timeout. Defaults to 30000ms; the export itself has no timeout. */\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 * Export a closed local database as a standard SQLite file.\n *\n * The authoritative backend is selected from Kora's storage manifest. The\n * database leader lock is held for the entire read, so the returned bytes are a\n * consistent committed snapshot and no tab can write concurrently. This method\n * never migrates, verifies, deletes, or otherwise changes the source database.\n *\n * @throws {@link StorageInUseError} when a tab has the database open.\n * @throws {@link LocalDatabaseNotFoundError} when no authoritative copy exists.\n */\nexport async function exportLocalDatabase(\n\tdbName: string,\n\toptions: ExportLocalDatabaseOptions,\n): Promise<Uint8Array> {\n\tconst record = await readManifestRecord(dbName)\n\tif (!record) throw new LocalDatabaseNotFoundError(dbName)\n\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 = (request: WorkerRequest): Promise<WorkerResponse> => worker.send(request)\n\t\ttry {\n\t\t\tif (record.backend === 'opfs') {\n\t\t\t\tconst opened = await send({\n\t\t\t\t\tid: 0,\n\t\t\t\t\ttype: 'open',\n\t\t\t\t\tddlStatements: [],\n\t\t\t\t\tdbName,\n\t\t\t\t\tmustExist: true,\n\t\t\t\t})\n\t\t\t\tif (opened.type === 'error') {\n\t\t\t\t\tif (opened.code === 'NOT_FOUND') throw new LocalDatabaseNotFoundError(dbName)\n\t\t\t\t\tthrow new AdapterError(`Could not open \"${dbName}\" for export: ${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} else {\n\t\t\t\tconst dump = await loadDumpFromIndexedDB<DatabaseDump>(dbName)\n\t\t\t\tif (!dump) throw new LocalDatabaseNotFoundError(dbName)\n\t\t\t\tconst opened = await send({\n\t\t\t\t\tid: 0,\n\t\t\t\t\ttype: 'open',\n\t\t\t\t\tddlStatements: [],\n\t\t\t\t\tdbName,\n\t\t\t\t\tstorage: 'memory',\n\t\t\t\t})\n\t\t\t\tif (opened.type === 'error') {\n\t\t\t\t\tthrow new AdapterError(`Could not prepare \"${dbName}\" for export: ${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\tfor (const statement of restoreDumpStatements(dump, true)) {\n\t\t\t\t\tconst restored = await send({\n\t\t\t\t\t\tid: 0,\n\t\t\t\t\t\ttype: 'execute',\n\t\t\t\t\t\tsql: statement.sql,\n\t\t\t\t\t\tparams: statement.params,\n\t\t\t\t\t})\n\t\t\t\t\tif (restored.type === 'error') {\n\t\t\t\t\t\tthrow new AdapterError(\n\t\t\t\t\t\t\t`Could not restore \"${dbName}\" for export: ${restored.message}`,\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tdbName,\n\t\t\t\t\t\t\t\tcode: restored.code,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst exported = await worker.send({ id: 0, type: 'export' }, undefined, {\n\t\t\t\ttimeoutMs: Number.POSITIVE_INFINITY,\n\t\t\t})\n\t\t\tif (exported.type === 'error' || !(exported.data instanceof Uint8Array)) {\n\t\t\t\tthrow new AdapterError(\n\t\t\t\t\t`Could not export \"${dbName}\": ${exported.type === 'error' ? exported.message : 'worker returned invalid bytes'}`,\n\t\t\t\t\t{ dbName, code: exported.type === 'error' ? exported.code : 'INVALID_EXPORT' },\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn exported.data\n\t\t} finally {\n\t\t\tawait send({ id: 0, type: 'close' }).catch(() => undefined)\n\t\t\tworker.terminate()\n\t\t}\n\t})\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\t// An explicit deletion is not an eviction: the evidence goes first, so a database\n\t\t\t// removed on purpose never reads as a vanished one (offline-readiness design 6.2).\n\t\t\tclearStorageMarker(dbName)\n\t\t\tawait deleteManifestRecord(dbName)\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\treturn exists || record !== null\n\t\t} finally {\n\t\t\tworker.terminate()\n\t\t}\n\t})\n}\n"]}