@powersync/web 0.0.0-dev-20260216124709 → 0.0.0-dev-20260226160112
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/dist/26d61ca9f5694d064635.wasm +0 -0
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js +70 -70
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js.map +1 -1
- package/dist/{_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530150.index.umd.js → _journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-97ebe9.index.umd.js} +77 -77
- package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-97ebe9.index.umd.js.map +1 -0
- package/dist/b4c6283dc473b6b3fd24.wasm +0 -0
- package/dist/c78985091a0b22aaef03.wasm +0 -0
- package/dist/ca59e199e1138b553fad.wasm +0 -0
- package/dist/index.umd.js +4177 -202
- package/dist/index.umd.js.map +1 -1
- package/dist/worker/SharedSyncImplementation.umd.js +18308 -18254
- package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
- package/dist/worker/WASQLiteDB.umd.js +16978 -16924
- package/dist/worker/WASQLiteDB.umd.js.map +1 -1
- package/dist/worker/node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs.umd.js.map +1 -1
- package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-1d4e74.umd.js → node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--0e69f0.umd.js} +20 -20
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--0e69f0.umd.js.map +1 -0
- package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-0d2437.umd.js → node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--1787c2.umd.js} +24 -24
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--1787c2.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--24f702.umd.js +3494 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--24f702.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--4e15a2.umd.js +31 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--4e15a2.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--7ba318.umd.js +31 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--7ba318.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--919198.umd.js +31 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--919198.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--997c14.umd.js +31 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--997c14.umd.js.map +1 -0
- package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-3622cf.umd.js → node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--a6ce73.umd.js} +18 -18
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--a6ce73.umd.js.map +1 -0
- package/lib/package.json +3 -3
- package/lib/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.js +4 -2
- package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.d.ts +10 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.js +36 -21
- package/lib/src/worker/db/open-worker-database.js +2 -2
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.ts +4 -3
- package/src/db/adapters/wa-sqlite/WASQLiteConnection.ts +41 -19
- package/src/worker/db/open-worker-database.ts +2 -2
- package/dist/0b19af1befc07ce338dd.wasm +0 -0
- package/dist/2632c3bda9473da74fd5.wasm +0 -0
- package/dist/64f5351ba3784bfe2f3e.wasm +0 -0
- package/dist/9318ca94aac4d0fe0135.wasm +0 -0
- package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530150.index.umd.js.map +0 -1
- package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530151.index.umd.js +0 -555
- package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530151.index.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-3a94cf.umd.js +0 -44
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-3a94cf.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-868779.umd.js +0 -44
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-868779.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-f60d0d.umd.js +0 -44
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-f60d0d.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +0 -44
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-0d2437.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-1d4e74.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-3622cf.umd.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker/node_modules_pnpm_journeyapps_wa-sqlite_0_0_0-dev-20260226145021_node_modules_journeyapps_wa--24f702.umd.js","mappings":";;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC/sBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;AC7NA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;ACzFA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;;AClEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;AC1hCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","sources":["webpack://sdk_web/../../node_modules/.pnpm/@journeyapps+wa-sqlite@0.0.0-dev-20260226145021/node_modules/@journeyapps/wa-sqlite/src/FacadeVFS.js","webpack://sdk_web/../../node_modules/.pnpm/@journeyapps+wa-sqlite@0.0.0-dev-20260226145021/node_modules/@journeyapps/wa-sqlite/src/VFS.js","webpack://sdk_web/../../node_modules/.pnpm/@journeyapps+wa-sqlite@0.0.0-dev-20260226145021/node_modules/@journeyapps/wa-sqlite/src/examples/LazyLock.js","webpack://sdk_web/../../node_modules/.pnpm/@journeyapps+wa-sqlite@0.0.0-dev-20260226145021/node_modules/@journeyapps/wa-sqlite/src/examples/Lock.js","webpack://sdk_web/../../node_modules/.pnpm/@journeyapps+wa-sqlite@0.0.0-dev-20260226145021/node_modules/@journeyapps/wa-sqlite/src/examples/OPFSWriteAheadVFS.js","webpack://sdk_web/../../node_modules/.pnpm/@journeyapps+wa-sqlite@0.0.0-dev-20260226145021/node_modules/@journeyapps/wa-sqlite/src/examples/WriteAhead.js"],"sourcesContent":["// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport * as VFS from './VFS.js';\n\nconst AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;\n\n// Milliseconds since Julian epoch as a BigInt.\n// https://github.com/sqlite/sqlite/blob/e57527c14f7b7cfa6e32eeab5c549d50c4fa3674/src/os_unix.c#L6872-L6882\nconst UNIX_EPOCH = 24405875n * 8640000n;\n\n// Convenience base class for a JavaScript VFS.\n// The raw xOpen, xRead, etc. function signatures receive only C primitives\n// which aren't easy to work with. This class provides corresponding calls\n// like jOpen, jRead, etc., which receive JavaScript-friendlier arguments\n// such as string, Uint8Array, and DataView.\nexport class FacadeVFS extends VFS.Base {\n /**\n * @param {string} name \n * @param {object} module \n */\n constructor(name, module) {\n super(name, module);\n }\n\n /**\n * Override to indicate which methods are asynchronous.\n * @param {string} methodName \n * @returns {boolean}\n */\n hasAsyncMethod(methodName) {\n // The input argument is a string like \"xOpen\", so convert to \"jOpen\".\n // Then check if the method exists and is async.\n const jMethodName = `j${methodName.slice(1)}`;\n return this[jMethodName] instanceof AsyncFunction;\n }\n \n /**\n * Return the filename for a file id for use by mixins.\n * @param {number} pFile \n * @returns {string}\n */\n getFilename(pFile) {\n throw new Error('unimplemented');\n }\n\n /**\n * @param {string?} filename \n * @param {number} pFile \n * @param {number} flags \n * @param {DataView} pOutFlags \n * @returns {number|Promise<number>}\n */\n jOpen(filename, pFile, flags, pOutFlags) {\n return VFS.SQLITE_CANTOPEN;\n }\n\n /**\n * @param {string} filename \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n jDelete(filename, syncDir) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {string} filename \n * @param {number} flags \n * @param {DataView} pResOut \n * @returns {number|Promise<number>}\n */\n jAccess(filename, flags, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {string} filename \n * @param {Uint8Array} zOut \n * @returns {number|Promise<number>}\n */\n jFullPathname(filename, zOut) {\n // Copy the filename to the output buffer.\n const { read, written } = new TextEncoder().encodeInto(filename, zOut);\n if (read < filename.length) return VFS.SQLITE_IOERR;\n if (written >= zOut.length) return VFS.SQLITE_IOERR;\n zOut[written] = 0;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {Uint8Array} zBuf \n * @returns {number|Promise<number>}\n */\n jGetLastError(zBuf) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n jClose(pFile) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {Uint8Array} pData \n * @param {number} iOffset \n * @returns {number|Promise<number>}\n */\n jRead(pFile, pData, iOffset) {\n pData.fill(0);\n return VFS.SQLITE_IOERR_SHORT_READ;\n }\n\n /**\n * @param {number} pFile \n * @param {Uint8Array} pData \n * @param {number} iOffset \n * @returns {number|Promise<number>}\n */\n jWrite(pFile, pData, iOffset) {\n return VFS.SQLITE_IOERR_WRITE;\n }\n\n /**\n * @param {number} pFile \n * @param {number} size \n * @returns {number|Promise<number>}\n */\n jTruncate(pFile, size) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n jSync(pFile, flags) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {DataView} pSize\n * @returns {number|Promise<number>}\n */\n jFileSize(pFile, pSize) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n jLock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n jUnlock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {DataView} pResOut \n * @returns {number|Promise<number>}\n */\n jCheckReservedLock(pFile, pResOut) {\n pResOut.setInt32(0, 0, true);\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile\n * @param {number} op\n * @param {DataView} pArg\n * @returns {number|Promise<number>}\n */\n jFileControl(pFile, op, pArg) {\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {number} pFile\n * @returns {number|Promise<number>}\n */\n jSectorSize(pFile) {\n return super.xSectorSize(pFile);\n }\n\n /**\n * @param {number} pFile\n * @returns {number|Promise<number>}\n */\n jDeviceCharacteristics(pFile) {\n return 0;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} pFile \n * @param {number} flags \n * @param {number} pOutFlags \n * @returns {number|Promise<number>}\n */\n xOpen(pVfs, zName, pFile, flags, pOutFlags) {\n const filename = this.#decodeFilename(zName, flags);\n const pOutFlagsView = this.#makeTypedDataView('Int32', pOutFlags);\n this['log']?.('jOpen', filename, pFile, '0x' + flags.toString(16));\n return this.jOpen(filename, pFile, flags, pOutFlagsView);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nByte \n * @param {number} pCharOut\n * @returns {number|Promise<number>}\n */\n xRandomness(pVfs, nByte, pCharOut) {\n const randomArray = new Uint8Array(nByte);\n crypto.getRandomValues(randomArray);\n // Copy randomArray to the WebAssembly memory\n const buffer = pCharOut; // Pointer to memory in WebAssembly\n this._module.HEAPU8.set(randomArray, buffer); // Copy randomArray into memory starting at buffer\n return nByte;\n }\n\n /**\n * Gets the current time as milliseconds since Unix epoch\n * @param {number} pVfs pointer to the VFS\n * @param {number} pTime pointer to write the time value\n * @returns {number} SQLite error code\n */\n xCurrentTimeInt64(pVfs, pTime) {\n // Create a DataView to write the current time\n const timeView = this.#makeTypedDataView('BigInt64', pTime);\n \n const currentTime = BigInt(Date.now());\n // Convert the current time to milliseconds since Unix epoch\n const value = UNIX_EPOCH + currentTime;\n \n // Write the time value to the pointer location\n timeView.setBigInt64(0, value, true);\n \n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n xDelete(pVfs, zName, syncDir) {\n const filename = this._module.UTF8ToString(zName);\n this['log']?.('jDelete', filename, syncDir);\n return this.jDelete(filename, syncDir);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} flags \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xAccess(pVfs, zName, flags, pResOut) {\n const filename = this._module.UTF8ToString(zName);\n const pResOutView = this.#makeTypedDataView('Int32', pResOut);\n this['log']?.('jAccess', filename, flags);\n return this.jAccess(filename, flags, pResOutView);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} nOut \n * @param {number} zOut \n * @returns {number|Promise<number>}\n */\n xFullPathname(pVfs, zName, nOut, zOut) {\n const filename = this._module.UTF8ToString(zName);\n const zOutArray = this._module.HEAPU8.subarray(zOut, zOut + nOut);\n this['log']?.('jFullPathname', filename, nOut);\n return this.jFullPathname(filename, zOutArray);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nBuf \n * @param {number} zBuf \n * @returns {number|Promise<number>}\n */\n xGetLastError(pVfs, nBuf, zBuf) {\n const zBufArray = this._module.HEAPU8.subarray(zBuf, zBuf + nBuf);\n this['log']?.('jGetLastError', nBuf);\n return this.jGetLastError(zBufArray);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xClose(pFile) {\n this['log']?.('jClose', pFile);\n return this.jClose(pFile);\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xRead(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n const pDataArray = this.#makeDataArray(pData, iAmt);\n const iOffset = delegalize(iOffsetLo, iOffsetHi);\n this['log']?.('jRead', pFile, iAmt, iOffset);\n return this.jRead(pFile, pDataArray, iOffset);\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xWrite(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n const pDataArray = this.#makeDataArray(pData, iAmt);\n const iOffset = delegalize(iOffsetLo, iOffsetHi);\n this['log']?.('jWrite', pFile, pDataArray, iOffset);\n return this.jWrite(pFile, pDataArray, iOffset);\n }\n\n /**\n * @param {number} pFile \n * @param {number} sizeLo \n * @param {number} sizeHi \n * @returns {number|Promise<number>}\n */\n xTruncate(pFile, sizeLo, sizeHi) {\n const size = delegalize(sizeLo, sizeHi);\n this['log']?.('jTruncate', pFile, size);\n return this.jTruncate(pFile, size);\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n xSync(pFile, flags) {\n this['log']?.('jSync', pFile, flags);\n return this.jSync(pFile, flags);\n }\n\n /**\n * \n * @param {number} pFile \n * @param {number} pSize \n * @returns {number|Promise<number>}\n */\n xFileSize(pFile, pSize) {\n const pSizeView = this.#makeTypedDataView('BigInt64', pSize);\n this['log']?.('jFileSize', pFile);\n return this.jFileSize(pFile, pSizeView);\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xLock(pFile, lockType) {\n this['log']?.('jLock', pFile, lockType);\n return this.jLock(pFile, lockType);\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xUnlock(pFile, lockType) {\n this['log']?.('jUnlock', pFile, lockType);\n return this.jUnlock(pFile, lockType);\n } \n\n /**\n * @param {number} pFile \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xCheckReservedLock(pFile, pResOut) {\n const pResOutView = this.#makeTypedDataView('Int32', pResOut);\n this['log']?.('jCheckReservedLock', pFile);\n return this.jCheckReservedLock(pFile, pResOutView);\n }\n\n /**\n * @param {number} pFile \n * @param {number} op \n * @param {number} pArg \n * @returns {number|Promise<number>}\n */\n xFileControl(pFile, op, pArg) {\n const pArgView = new DataView(\n this._module.HEAPU8.buffer,\n this._module.HEAPU8.byteOffset + pArg);\n this['log']?.('jFileControl', pFile, op, pArgView);\n return this.jFileControl(pFile, op, pArgView);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xSectorSize(pFile) {\n this['log']?.('jSectorSize', pFile);\n return this.jSectorSize(pFile);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xDeviceCharacteristics(pFile) {\n this['log']?.('jDeviceCharacteristics', pFile);\n return this.jDeviceCharacteristics(pFile);\n }\n\n /**\n * Wrapped DataView for pointer arguments.\n * Pointers to a single value are passed using a DataView-like class.\n * This wrapper class prevents use of incorrect type or endianness, and\n * reacquires the underlying buffer when the WebAssembly memory is resized.\n * @param {'Int32'|'BigInt64'} type \n * @param {number} byteOffset \n * @returns {DataView}\n */\n #makeTypedDataView(type, byteOffset) {\n // @ts-ignore\n return new DataViewProxy(this._module, byteOffset, type);\n }\n\n /**\n * Wrapped Uint8Array for buffer arguments.\n * Memory blocks are passed as a Uint8Array-like class. This wrapper\n * class reacquires the underlying buffer when the WebAssembly memory\n * is resized.\n * @param {number} byteOffset \n * @param {number} byteLength \n * @returns {Uint8Array}\n */\n #makeDataArray(byteOffset, byteLength) {\n // @ts-ignore\n return new Uint8ArrayProxy(this._module, byteOffset, byteLength);\n }\n\n #decodeFilename(zName, flags) {\n if (flags & VFS.SQLITE_OPEN_URI) {\n // The first null-terminated string is the URI path. Subsequent\n // strings are query parameter keys and values.\n // https://www.sqlite.org/c3ref/open.html#urifilenamesinsqlite3open\n let pName = zName;\n let state = 1;\n const charCodes = [];\n while (state) {\n const charCode = this._module.HEAPU8[pName++];\n if (charCode) {\n charCodes.push(charCode);\n } else {\n if (!this._module.HEAPU8[pName]) state = null;\n switch (state) {\n case 1: // path\n charCodes.push('?'.charCodeAt(0));\n state = 2;\n break;\n case 2: // key\n charCodes.push('='.charCodeAt(0));\n state = 3;\n break;\n case 3: // value\n charCodes.push('&'.charCodeAt(0));\n state = 2;\n break;\n }\n }\n }\n return new TextDecoder().decode(new Uint8Array(charCodes));\n }\n return zName ? this._module.UTF8ToString(zName) : null;\n }\n}\n\n// Emscripten \"legalizes\" 64-bit integer arguments by passing them as\n// two 32-bit signed integers.\nfunction delegalize(lo32, hi32) {\n return (hi32 * 0x100000000) + lo32 + (lo32 < 0 ? 2**32 : 0);\n}\n\n// This class provides a Uint8Array-like interface for a WebAssembly memory\n// buffer. It is used to access memory blocks passed as arguments to\n// xRead, xWrite, etc. The class reacquires the underlying buffer when the\n// WebAssembly memory is resized, which can happen when the memory is\n// detached and resized by the WebAssembly module.\n//\n// Note that although this class implements the same methods as Uint8Array,\n// it is not a real Uint8Array and passing it to functions that expect\n// a Uint8Array may not work. Use subarray() to get a real Uint8Array\n// if needed.\nclass Uint8ArrayProxy {\n #module;\n\n #_array = new Uint8Array()\n get #array() {\n if (this.#_array.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer so re-create the\n // array with the new buffer.\n this.#_array = this.#module.HEAPU8.subarray(\n this.byteOffset,\n this.byteOffset + this.byteLength);\n }\n return this.#_array;\n }\n\n /**\n * @param {*} module\n * @param {number} byteOffset \n * @param {number} byteLength \n */\n constructor(module, byteOffset, byteLength) {\n this.#module = module;\n this.byteOffset = byteOffset;\n this.length = this.byteLength = byteLength;\n }\n\n get buffer() {\n return this.#array.buffer;\n }\n\n at(index) {\n return this.#array.at(index);\n }\n copyWithin(target, start, end) {\n this.#array.copyWithin(target, start, end);\n }\n entries() {\n return this.#array.entries();\n }\n every(predicate) {\n return this.#array.every(predicate);\n }\n fill(value, start, end) {\n this.#array.fill(value, start, end);\n }\n filter(predicate) {\n return this.#array.filter(predicate);\n }\n find(predicate) {\n return this.#array.find(predicate);\n }\n findIndex(predicate) {\n return this.#array.findIndex(predicate);\n }\n findLast(predicate) {\n return this.#array.findLast(predicate);\n }\n findLastIndex(predicate) {\n return this.#array.findLastIndex(predicate);\n }\n forEach(callback) {\n this.#array.forEach(callback);\n }\n includes(value, start) {\n return this.#array.includes(value, start);\n }\n indexOf(value, start) {\n return this.#array.indexOf(value, start);\n }\n join(separator) {\n return this.#array.join(separator);\n }\n keys() {\n return this.#array.keys();\n }\n lastIndexOf(value, start) {\n return this.#array.lastIndexOf(value, start);\n }\n map(callback) {\n return this.#array.map(callback);\n }\n reduce(callback, initialValue) {\n return this.#array.reduce(callback, initialValue);\n }\n reduceRight(callback, initialValue) {\n return this.#array.reduceRight(callback, initialValue);\n }\n reverse() {\n this.#array.reverse();\n }\n set(array, offset) {\n this.#array.set(array, offset);\n }\n slice(start, end) {\n return this.#array.slice(start, end);\n }\n some(predicate) {\n return this.#array.some(predicate);\n }\n sort(compareFn) {\n this.#array.sort(compareFn);\n }\n subarray(begin, end) {\n return this.#array.subarray(begin, end);\n }\n toLocaleString(locales, options) {\n // @ts-ignore\n return this.#array.toLocaleString(locales, options);\n }\n toReversed() {\n return this.#array.toReversed();\n }\n toSorted(compareFn) {\n return this.#array.toSorted(compareFn);\n }\n toString() {\n return this.#array.toString();\n }\n values() {\n return this.#array.values();\n }\n with(index, value) {\n return this.#array.with(index, value);\n }\n [Symbol.iterator]() {\n return this.#array[Symbol.iterator]();\n }\n}\n\n// This class provides a DataView-like interface for a WebAssembly memory\n// buffer, restricted to either Int32 or BigInt64 types. It also reacquires\n// the underlying buffer when the WebAssembly memory is resized, which can\n// happen when the memory is detached and resized by the WebAssembly module.\nclass DataViewProxy {\n #module;\n #type;\n\n #_view = new DataView(new ArrayBuffer(0));\n get #view() {\n if (this.#_view.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer so re-create the\n // view with the new buffer.\n this.#_view = new DataView(\n this.#module.HEAPU8.buffer,\n this.#module.HEAPU8.byteOffset + this.byteOffset);\n }\n return this.#_view;\n }\n\n /**\n * @param {*} module\n * @param {number} byteOffset \n * @param {'Int32'|'BigInt64'} type\n */\n constructor(module, byteOffset, type) {\n this.#module = module;\n this.byteOffset = byteOffset;\n this.#type = type;\n }\n\n get buffer() {\n return this.#view.buffer;\n }\n get byteLength() {\n return this.#type === 'Int32' ? 4 : 8;\n }\n\n getInt32(byteOffset, littleEndian) {\n if (this.#type !== 'Int32') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n return this.#view.getInt32(byteOffset, littleEndian);\n }\n setInt32(byteOffset, value, littleEndian) {\n if (this.#type !== 'Int32') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n this.#view.setInt32(byteOffset, value, littleEndian);\n }\n getBigInt64(byteOffset, littleEndian) {\n if (this.#type !== 'BigInt64') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n return this.#view.getBigInt64(byteOffset, littleEndian);\n }\n setBigInt64(byteOffset, value, littleEndian) {\n if (this.#type !== 'BigInt64') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n this.#view.setBigInt64(byteOffset, value, littleEndian);\n }\n}","// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport * as VFS from './sqlite-constants.js';\nexport * from './sqlite-constants.js';\n\nconst DEFAULT_SECTOR_SIZE = 512;\n\n// Base class for a VFS.\nexport class Base {\n name;\n mxPathname = 64;\n _module;\n\n /**\n * @param {string} name \n * @param {object} module \n */\n constructor(name, module) {\n this.name = name;\n this._module = module;\n }\n\n /**\n * @returns {void|Promise<void>} \n */\n close() {\n }\n\n /**\n * @returns {boolean|Promise<boolean>}\n */\n isReady() {\n return true;\n }\n\n /**\n * Overload in subclasses to indicate which methods are asynchronous.\n * @param {string} methodName \n * @returns {boolean}\n */\n hasAsyncMethod(methodName) {\n return false;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} pFile \n * @param {number} flags \n * @param {number} pOutFlags \n * @returns {number|Promise<number>}\n */\n xOpen(pVfs, zName, pFile, flags, pOutFlags) {\n return VFS.SQLITE_CANTOPEN;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n xDelete(pVfs, zName, syncDir) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} flags \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xAccess(pVfs, zName, flags, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} nOut \n * @param {number} zOut \n * @returns {number|Promise<number>}\n */\n xFullPathname(pVfs, zName, nOut, zOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nBuf \n * @param {number} zBuf \n * @returns {number|Promise<number>}\n */\n xGetLastError(pVfs, nBuf, zBuf) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xClose(pFile) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xRead(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xWrite(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} sizeLo \n * @param {number} sizeHi \n * @returns {number|Promise<number>}\n */\n xTruncate(pFile, sizeLo, sizeHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n xSync(pFile, flags) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * \n * @param {number} pFile \n * @param {number} pSize \n * @returns {number|Promise<number>}\n */\n xFileSize(pFile, pSize) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xLock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xUnlock(pFile, lockType) {\n return VFS.SQLITE_OK;\n } \n\n /**\n * @param {number} pFile \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xCheckReservedLock(pFile, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} op \n * @param {number} pArg \n * @returns {number|Promise<number>}\n */\n xFileControl(pFile, op, pArg) {\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xSectorSize(pFile) {\n return DEFAULT_SECTOR_SIZE;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xDeviceCharacteristics(pFile) {\n return 0;\n }\n}\n\nexport const FILE_TYPE_MASK = [\n VFS.SQLITE_OPEN_MAIN_DB,\n VFS.SQLITE_OPEN_MAIN_JOURNAL,\n VFS.SQLITE_OPEN_TEMP_DB,\n VFS.SQLITE_OPEN_TEMP_JOURNAL,\n VFS.SQLITE_OPEN_TRANSIENT_DB,\n VFS.SQLITE_OPEN_SUBJOURNAL,\n VFS.SQLITE_OPEN_SUPER_JOURNAL,\n VFS.SQLITE_OPEN_WAL\n].reduce((mask, element) => mask | element);","import { Lock } from './Lock.js';\n\nexport class LazyLock extends Lock {\n #channel;\n #isBusy = false;\n #hasReleaseRequest = false;\n\n /**\n * @param {string} name \n */\n constructor(name) {\n super(name);\n this.#channel = new BroadcastChannel(name);\n this.#channel.onmessage = (event) => {\n if (this.#isBusy) {\n // We're using the lock so postpone the release.\n this.#hasReleaseRequest = true;\n } else {\n this.release();\n }\n }\n }\n\n close() {\n super.close();\n this.#channel.onmessage = null;\n this.#channel.close();\n }\n\n /**\n * @param {LockMode} mode \n * @param {number} timeout \n * @returns {Promise<boolean>}\n */\n async acquire(mode, timeout = -1) {\n this.#isBusy = true;\n try {\n if (mode === this.mode) {\n // We never had to release the lock.\n return true;\n }\n\n if (this.mode) {\n // Release the lock to acquire it in a different mode.\n super.release();\n } else {\n // Poll for the lock. This isn't necessary but if it works it avoids\n // the BroadcastChannel traffic.\n if (await super.acquire(mode, 0)) {\n return true;\n }\n }\n\n // Request the lock.\n const pResult = super.acquire(mode, timeout)\n this.#channel.postMessage({});\n\n return await pResult;\n } catch (e) {\n this.release();\n throw e;\n }\n }\n\n /**\n * @param {LockMode} mode \n * @returns {boolean}\n */\n acquireIfHeld(mode) {\n if (mode === this.mode) {\n this.#isBusy = true;\n return true;\n }\n return false;\n }\n\n release() {\n super.release();\n this.#isBusy = false;\n this.#hasReleaseRequest = false;\n }\n\n releaseLazy() {\n // Release the lock only if someone else wants it.\n this.#isBusy = false;\n if (this.#hasReleaseRequest) {\n this.release();\n }\n }\n}","// This is a convenience wrapper for the Web Locks API.\nexport class Lock {\n #name;\n /** @type {LockMode?} */ #mode = null;\n #releaser = null;\n\n /**\n * @param {string} name \n */\n constructor(name) {\n this.#name = name;\n }\n\n get name() { return this.#name; }\n get mode() { return this.#mode; }\n\n close() {\n this.release();\n }\n \n /**\n * @param {'shared'|'exclusive'} mode \n * @param {number} timeout -1 for infinite, 0 for poll, >0 for milliseconds\n * @return {Promise<boolean>} true if lock acquired, false on failed poll\n */\n async acquire(mode, timeout = -1) {\n if (this.#releaser) {\n throw new Error(`Lock ${this.#name} is already acquired`);\n }\n return new Promise((resolve, reject) => {\n /** @type {LockOptions} */\n const options = { mode, ifAvailable: timeout === 0 };\n let timeoutId;\n if (timeout > 0) {\n const abortController = new AbortController();\n timeoutId = self.setTimeout(() => {\n abortController.abort();\n }, timeout);\n options.signal = abortController.signal;\n }\n\n navigator.locks.request(this.#name, options, lock => {\n if (timeoutId) clearTimeout(timeoutId);\n if (lock === null) {\n // Polling (with timeout = 0) did not acquire the lock.\n return resolve(false);\n }\n\n // Lock acquired. The lock is released when this returned\n // Promise is resolved.\n this.#mode = mode;\n return new Promise(releaser => {\n this.#releaser = releaser;\n resolve(true);\n })\n }).catch(e => {\n return reject(e);\n });\n });\n }\n\n release() {\n this.#releaser?.();\n this.#releaser = null;\n this.#mode = null;\n }\n}\n","import { FacadeVFS } from \"../FacadeVFS.js\";\nimport * as VFS from '../VFS.js';\nimport { LazyLock } from \"./LazyLock.js\";\nimport { WriteAhead } from \"./WriteAhead.js\";\n\nconst LIBRARY_FILES_ROOT = '.wa-sqlite';\nconst DEFAULT_TEMP_FILES = 6;\n\nconst finalizationRegistry = new FinalizationRegistry((/** @type {() => void} */ f) => f());\n\n/**\n * @typedef FileEntry\n * @property {string} zName\n * @property {number} flags\n * @property {FileSystemSyncAccessHandle} [accessHandle]\n\n * Main database file properties:\n * @property {*} [retryResult]\n * @property {FileSystemSyncAccessHandle} [waHandle]\n * @property {FileSystemSyncAccessHandle} [journalHandle]\n * \n * @property {boolean} [useWriteAhead]\n * @property {'reserved'|'exclusive'} [writeHint]\n * @property {'normal'|'exclusive'|null} [lockingMode]\n * @property {number} [lockState] SQLITE_LOCK_*\n * @property {LazyLock} [readLock]\n * @property {LazyLock} [writeLock]\n * @property {'none'|'read'|'write'|'readwrite'} [useLazyLock]\n * @property {number} [timeout]\n * @property {0|1|2|3} [synchronous]\n * \n * @property {WriteAhead} [writeAhead]\n */\n\n/**\n * @typedef OPFSWriteAheadOptions\n * @property {number} [nTmpFiles]\n * @property {number} [autoCheckpointPages]\n * @property {number} [heartbeatInterval]\n * @property {number} [heartbeatActionDelay]\n */\n\nexport class OPFSWriteAheadVFS extends FacadeVFS {\n lastError = null;\n log = null;\n \n /** @type {Map<number, FileEntry>} */ mapIdToFile = new Map();\n /** @type {Map<string, FileEntry>} */ mapPathToFile = new Map();\n\n /** @type {Map<string, FileSystemSyncAccessHandle>} */ boundTempFiles = new Map();\n /** @type {Set<FileSystemSyncAccessHandle>} */ unboundTempFiles = new Set();\n /** @type {OPFSWriteAheadOptions} */ options = {\n nTmpFiles: DEFAULT_TEMP_FILES\n };\n\n _ready;\n\n static async create(name, module, options) {\n const vfs = new OPFSWriteAheadVFS(name, module);\n Object.assign(vfs.options, options);\n await vfs.isReady();\n return vfs;\n }\n\n constructor(name, module) {\n super(name, module);\n this._ready = (async () => {\n // Ensure the library files root directory exists.\n let dirHandle = await navigator.storage.getDirectory();\n dirHandle = await dirHandle.getDirectoryHandle(LIBRARY_FILES_ROOT, { create: true });\n\n // Clean up any stale session directories.\n // @ts-ignore\n for await (const name of dirHandle.keys()) {\n if (name.startsWith('.session-')) {\n // Acquire a lock on the session directory to ensure it is not in use.\n await navigator.locks.request(name, { ifAvailable: true }, async lock => {\n if (lock) {\n // This directory is not in use.\n try {\n await dirHandle.removeEntry(name, { recursive: true });\n } catch (e) {\n // Ignore errors, will try again next time.\n }\n }\n });\n }\n }\n\n // Create our session directory.\n const dirName = `.session-${Math.random().toString(16).slice(2)}`;\n await new Promise(resolve => {\n navigator.locks.request(dirName, () => {\n // @ts-ignore\n resolve();\n return new Promise(release => {\n // @ts-ignore\n finalizationRegistry.register(this, release);\n });\n });\n });\n dirHandle = await dirHandle.getDirectoryHandle(dirName, { create: true });\n\n // Create temporary files.\n for (let i = 0; i < this.options.nTmpFiles; i++) {\n const fileHandle= await dirHandle.getFileHandle(i.toString(), { create: true });\n const accessHandle = await fileHandle.createSyncAccessHandle();\n finalizationRegistry.register(this, () => accessHandle.close());\n this.unboundTempFiles.add(accessHandle);\n }\n })();\n }\n\n isReady() {\n return Promise.all([super.isReady(), this._ready]).then(() => true);\n }\n\n /**\n * @param {string?} zName \n * @param {number} fileId \n * @param {number} flags \n * @param {DataView} pOutFlags \n * @returns {number}\n */\n jOpen(zName, fileId, flags, pOutFlags) {\n try {\n if (zName === null) {\n // Generate a temporary filename. This will only be used as a\n // key to map to a pre-opened temporary file access handle.\n zName = Math.random().toString(16).slice(2);\n }\n\n const file = this.mapPathToFile.get(zName) ?? {\n zName,\n flags,\n retryResult: null,\n };\n this.mapPathToFile.set(zName, file);\n\n if (flags & VFS.SQLITE_OPEN_MAIN_DB) {\n // Open database and journal files with a retry operation.\n if (file.retryResult === null) {\n // This is the initial open attempt. Start the asynchronous task\n // and return SQLITE_BUSY to force a retry.\n this._module.retryOps.push(this.#retryOpen(zName, flags, fileId, pOutFlags));\n return VFS.SQLITE_BUSY;\n } else if (file.retryResult instanceof Error) {\n throw file.retryResult;\n }\n\n // Initialize database file state.\n file.accessHandle = file.retryResult.accessHandle;\n file.journalHandle = file.retryResult.journalHandle;\n file.waHandle = file.retryResult.waHandle;\n file.writeAhead = file.retryResult.writeAhead;\n file.retryResult = null;\n\n file.lockState = VFS.SQLITE_LOCK_NONE;\n file.lockingMode = null;\n file.readLock = new LazyLock(`${zName}#read`);\n file.writeLock = new LazyLock(`${zName}#write`);\n file.useLazyLock = 'readwrite';\n file.timeout = -1;\n file.synchronous = 1; // NORMAL\n file.useWriteAhead = true;\n file.writeHint = null;\n } else if (flags & VFS.SQLITE_OPEN_MAIN_JOURNAL) {\n // A journal file is managed with its main database so look that up.\n const dbFile = this.#getDbFileFromJournalName(zName);\n if (!dbFile) {\n throw new Error(`database file not found for journal ${zName}`);\n }\n\n // Determine whether to use the public journal (which is hot or\n // could become hot) or a private journal from the session directory.\n // This is necessary because read connections can test a journal\n // for hotness while a write-ahead transaction is in progress.\n if (!dbFile.useWriteAhead || dbFile.journalHandle.getSize() > 0) { \n // Either write-ahead is not being used, or a hot journal is being\n // opened for recovery. Use the public journal.\n file.accessHandle = dbFile.journalHandle;\n } else if (dbFile.useWriteAhead && dbFile.lockState > VFS.SQLITE_LOCK_SHARED) {\n // The journal is being opened for a write-ahead transaction.\n // This journal can never be hot so don't expose it to other\n // connections.\n file.accessHandle = this.#openTemporaryFile(zName);\n } else {\n throw new Error('unexpected journal file conditions');\n }\n } else if (flags & (VFS.SQLITE_OPEN_WAL | VFS.SQLITE_OPEN_SUPER_JOURNAL)) {\n throw new Error('WAL and super-journal files are not supported');\n } else {\n // This is a temporary file. Use an unbound pre-opened accessHandle.\n file.accessHandle = this.#openTemporaryFile(zName);\n }\n\n this.mapIdToFile.set(fileId, file);\n pOutFlags.setInt32(0, flags, true);\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_CANTOPEN;\n }\n }\n\n /**\n * @param {string} zName \n * @param {number} syncDir \n * @returns {number}\n */\n jDelete(zName, syncDir) {\n try {\n if (this.boundTempFiles.has(zName)) {\n const file = this.mapPathToFile.get(zName);\n this.#deleteTemporaryFile(file);\n } else if (zName.endsWith('-journal')) {\n const dbFile = this.#getDbFileFromJournalName(zName);\n dbFile?.journalHandle.truncate(0);\n dbFile?.journalHandle.flush();\n this.mapPathToFile.delete(zName);\n } else {\n throw new Error(`unexpected file deletion: ${zName}`);\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_DELETE;\n }\n }\n\n /**\n * @param {string} zName \n * @param {number} flags \n * @param {DataView} pResOut \n * @returns {number}\n */\n jAccess(zName, flags, pResOut) {\n try {\n // Special case main journal files.\n if (zName.endsWith('-journal')) {\n const dbFile = this.#getDbFileFromJournalName(zName);\n if (dbFile) {\n if (dbFile.lockState <= VFS.SQLITE_LOCK_SHARED) {\n // SQLite is testing for a hot journal. Journals created in the\n // session directory for write-ahead transactions are never hot,\n // i.e. they are used only for rollback. So here we look for\n // the status of the public journal.\n if (dbFile.journalHandle.getSize() === 0) {\n // Treat an empty journal file as non-existent.\n pResOut.setInt32(0, 0, true);\n } else {\n pResOut.setInt32(0, 1, true);\n }\n }\n return VFS.SQLITE_OK;\n }\n }\n\n const file = this.mapPathToFile.get(zName);\n pResOut.setInt32(0, file ? 1 : 0, true);\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_ACCESS;\n }\n }\n\n /**\n * @param {number} fileId \n * @returns {number}\n */\n jClose(fileId) {\n try {\n const file = this.mapIdToFile.get(fileId);\n if (file?.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n file.writeAhead.close();\n file.accessHandle.close();\n file.waHandle.close();\n this.mapPathToFile.delete(file?.zName);\n\n file.journalHandle.close();\n const journalPath = this.#getJournalNameFromDbName(file.zName);\n this.mapPathToFile.delete(journalPath);\n\n file.readLock.close();\n file.writeLock.close();\n } else if (file?.flags & VFS.SQLITE_OPEN_MAIN_JOURNAL) {\n // The actual OPFS journal file is managed with the main database\n // file, so don't close the access handle here.\n } else if (file?.flags & VFS.SQLITE_OPEN_DELETEONCLOSE) {\n this.#deleteTemporaryFile(file);\n }\n\n // Disassociate fileId from file entry.\n this.mapIdToFile.delete(fileId);\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_CLOSE;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {Uint8Array} pData \n * @param {number} iOffset\n * @returns {number}\n */\n jRead(fileId, pData, iOffset) {\n try {\n const file = this.mapIdToFile.get(fileId);\n\n let bytesRead = null;\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n // Try reading from the write-ahead overlays first. A read on the\n // database file is always a complete page, except when reading\n // from the 100-byte header.\n const pageOffset = iOffset < 100 ? iOffset : 0;\n const page = file.writeAhead.read(iOffset - pageOffset);\n if (page) {\n const readData = page.subarray(pageOffset, pageOffset + pData.byteLength);\n pData.set(readData);\n bytesRead = readData.byteLength;\n }\n }\n\n if (bytesRead === null) {\n // Read directly from the OPFS file.\n\n // On Chrome (at least), passing pData to accessHandle.read() is\n // an error because pData is a Proxy of a Uint8Array. Calling\n // subarray() produces a real Uint8Array and that works.\n bytesRead = file.accessHandle.read(pData.subarray(), { at: iOffset });\n }\n\n if (bytesRead < pData.byteLength) {\n pData.fill(0, bytesRead);\n return VFS.SQLITE_IOERR_SHORT_READ;\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_READ;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {Uint8Array} pData \n * @param {number} iOffset\n * @returns {number}\n */\n jWrite(fileId, pData, iOffset) {\n try {\n const file = this.mapIdToFile.get(fileId);\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n if (file.useWriteAhead) {\n // Write to the write-ahead overlay.\n file.writeAhead.write(iOffset, pData);\n return VFS.SQLITE_OK;\n }\n }\n\n // On Chrome (at least), passing pData to accessHandle.write() is\n // an error because pData is a Proxy of a Uint8Array. Calling\n // subarray() produces a real Uint8Array and that works.\n file.accessHandle.write(pData.subarray(), { at: iOffset });\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_WRITE;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} iSize \n * @returns {number}\n */\n jTruncate(fileId, iSize) {\n try {\n const file = this.mapIdToFile.get(fileId);\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n if (file.useWriteAhead) {\n file.writeAhead.truncate(iSize);\n return VFS.SQLITE_OK;\n }\n }\n file.accessHandle.truncate(iSize);\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_TRUNCATE;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} flags \n * @returns {number}\n */\n jSync(fileId, flags) {\n try {\n const file = this.mapIdToFile.get(fileId);\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n if (file.useWriteAhead) {\n const durability = file.synchronous > 1 ? 'strict' : 'relaxed';\n file.writeAhead.sync({ durability });\n } else {\n file.accessHandle.flush();\n }\n } else if (!this.boundTempFiles.has(file.zName)) {\n // Persistent journal file requires sync.\n file.accessHandle.flush();\n } else {\n // This is a temporary file so sync is not needed.\n // Temporary journals are only used for rollback by the\n // connection that created them, not for recovery.\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_FSYNC;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {DataView} pSize64 \n * @returns {number}\n */\n jFileSize(fileId, pSize64) {\n try {\n const file = this.mapIdToFile.get(fileId);\n\n let size;\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n size = file.writeAhead.getFileSize() || file.accessHandle.getSize();\n } else {\n size = file.accessHandle.getSize();\n }\n pSize64.setBigInt64(0, BigInt(size), true);\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_FSTAT;\n }\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n jLock(pFile, lockType) {\n try {\n const file = this.mapIdToFile.get(pFile);\n if (file.lockState === VFS.SQLITE_LOCK_NONE && lockType === VFS.SQLITE_LOCK_SHARED) {\n // We do all our locking work in this transition.\n if (file.retryResult === null) {\n if (file.lockingMode === 'exclusive') {\n // SQLite exclusive locking mode really means that no unlocking\n // is done, not that the locking state is immediately EXCLUSIVE.\n // This is a problem if the first transaction after setting\n // exclusive locking mode does not come with a write hint, so\n // we force the write hint here.\n file.writeHint = 'exclusive';\n file.useWriteAhead = false;\n }\n\n // There are three distinct cases. In each case if the required\n // lock is already held then we can proceed synchronously.\n // Otherwise we need to acquire state asynchronously and retry.\n if (!file.writeHint) {\n // Case 1: Read transaction with write-ahead logging.\n if (!file.readLock.acquireIfHeld('shared')) {\n file.retryResult = {};\n this._module.retryOps.push(this.#retryLockRead(file));\n return VFS.SQLITE_BUSY;\n } else {\n file.writeAhead.isolateForRead();\n }\n } else {\n if (file.useWriteAhead) {\n // Case 2: Write transaction with write-ahead logging.\n if (!file.writeLock.acquireIfHeld('exclusive')) {\n file.retryResult = {};\n this._module.retryOps.push(this.#retryLockWrite(file));\n return VFS.SQLITE_BUSY;\n } else {\n file.writeAhead.isolateForWrite();\n }\n } else {\n // Case 3: Transaction without write-ahead logging.\n file.retryResult = {};\n this._module.retryOps.push(this.#retryLockExclusive(file));\n return VFS.SQLITE_BUSY;\n }\n }\n } else if (file.retryResult instanceof Error) {\n throw file.retryResult;\n }\n\n // We have acquired the needed locks, either synchronously or\n // via retry.\n file.retryResult = null;\n } else if (lockType >= VFS.SQLITE_LOCK_RESERVED && !file.writeLock.mode) {\n // This is a write transaction but we don't already have the write\n // lock. This happens when the write hint was not used, which this\n // VFS treats as an error.\n throw new Error('Multi-statement write transaction cannot use BEGIN DEFERRED');\n }\n file.lockState = lockType;\n return VFS.SQLITE_OK;\n } catch (e) {\n if (e.name === 'AbortError') {\n // Timeout expired while waiting for the lock.\n return VFS.SQLITE_BUSY;\n }\n\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_LOCK;\n }\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number}\n */\n jUnlock(pFile, lockType) {\n try {\n const file = this.mapIdToFile.get(pFile);\n\n // If retryResult is non-null, an asynchronous lock operation is in\n // progress. In that case, don't change any locks.\n if (!file.retryResult && lockType === VFS.SQLITE_LOCK_NONE) {\n // In this VFS, this is the only unlock transition that matters.\n if (file.useWriteAhead) {\n // Exit write-ahead isolation.\n file.writeAhead.rejoin();\n }\n\n // Release any locks.\n switch (file.useLazyLock) {\n case 'none':\n file.writeLock.release();\n file.readLock.release();\n break;\n case 'read':\n file.writeLock.release();\n file.readLock.releaseLazy();\n break;\n case 'write':\n file.writeLock.releaseLazy();\n file.readLock.release();\n break;\n case 'readwrite':\n file.writeLock.releaseLazy();\n file.readLock.releaseLazy();\n break;\n }\n\n // Reset state for the next transaction.\n file.writeHint = null;\n if (file.lockingMode === 'normal') {\n file.useWriteAhead = true;\n }\n }\n file.lockState = lockType;\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR_UNLOCK;\n }\n }\n\n /**\n * @param {number} pFile \n * @param {DataView} pResOut \n * @returns {number}\n */\n jCheckReservedLock(pFile, pResOut) {\n // This function is only called in the SHARED lock state, and when\n // a potentially hot journal file exists. Such a journal can only\n // be created without using write-ahead, and such a connection\n // cannot co-exist with this connection in SHARED.\n pResOut.setInt32(0, 0, true);\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile\n * @param {number} op\n * @param {DataView} pArg\n * @returns {number}\n */\n jFileControl(pFile, op, pArg) {\n try {\n const file = this.mapIdToFile.get(pFile);\n switch (op) {\n case VFS.SQLITE_FCNTL_PRAGMA:\n const key = this._module.UTF8ToString(pArg.getUint32(4, true));\n const valueAddress = pArg.getUint32(8, true);\n const value = valueAddress ? this._module.UTF8ToString(valueAddress) : null;\n this.log?.(`PRAGMA ${key} ${value}`);\n switch (key.toLowerCase()) {\n case 'experimental_pragma_20251114':\n // After entering the SHARED locking state on the next\n // transaction, SQLite intends to immediately (barring a hot\n // journal) transition to RESERVED if value is '1', or\n // EXCLUSIVE if value is '2'.\n switch (value) {\n case '1':\n file.writeHint = 'reserved';\n break;\n case '2':\n file.writeHint = 'exclusive';\n break;\n default:\n throw new Error(`unexpected write hint value: ${value}`);\n }\n break;\n case 'busy_timeout':\n // Override SQLite's handling of busy timeouts with our\n // blocking lock timeouts.\n if (value !== null) {\n file.timeout = parseInt(value);\n } else {\n // Return current timeout.\n const s = file.timeout.toString();\n const ptr = this._module._sqlite3_malloc64(s.length + 1);\n this._module.stringToUTF8(s, ptr, s.length + 1);\n pArg.setUint32(0, ptr, true);\n }\n return VFS.SQLITE_OK;\n case 'locking_mode':\n // Track SQLite locking mode. We will use write-ahead only in\n // normal locking mode.\n switch (value?.toLowerCase()) {\n case 'normal':\n file.lockingMode = 'normal';\n break;\n case 'exclusive':\n file.lockingMode = 'exclusive';\n break;\n }\n break;\n case 'synchronous':\n if (value !== null) {\n switch (value.toLowerCase()) {\n case 'off':\n case '0':\n file.synchronous = 0;\n break;\n case 'normal':\n case '1':\n file.synchronous = 1;\n break;\n case 'full':\n case '2':\n file.synchronous = 2;\n break;\n case 'extra':\n case '3':\n file.synchronous = 3;\n break;\n default:\n throw new Error(`unexpected synchronous value: ${value}`);\n }\n }\n break;\n case 'vfs_trace':\n // This is a trace feature for debugging only.\n if (value !== null) {\n this.log = parseInt(value) !== 0 ? console.debug : null;\n file.writeAhead.log = this.log;\n }\n return VFS.SQLITE_OK;\n case 'wal_autocheckpoint':\n if (value !== null) {\n const nPages = parseInt(value);\n file.writeAhead.options.autoCheckpointPages = Math.max(nPages, 0);\n }\n break;\n case 'wal_checkpoint':\n const checkpointMode = (value ?? 'passive').toLowerCase();\n switch (checkpointMode) {\n case 'passive':\n case 'full':\n case 'restart':\n case 'truncate':\n if (file.lockState !== VFS.SQLITE_LOCK_NONE) {\n throw new Error('invalid while database is locked');\n }\n \n this._module.pendingOps.push(this.#pendingCheckpoint(file, checkpointMode));\n break;\n case 'noop':\n break;\n default:\n throw new Error(`unexpected wal_checkpoint mode: ${value}`);\n }\n\n // Return the approximate size of the write-ahead data (which\n // may be smaller than the actual file size). SQLite returns\n // different information, but that is not feasible from a VFS.\n {\n const s = file.writeAhead.getWriteAheadSize().toString();\n const ptr = this._module._sqlite3_malloc64(s.length + 1);\n this._module.stringToUTF8(s, ptr, s.length + 1);\n pArg.setUint32(0, ptr, true);\n }\n return VFS.SQLITE_OK;\n case 'lazy_lock':\n if (value !== null) {\n const useLazyLock = value.toLowerCase();\n switch (useLazyLock) {\n case 'read':\n case 'write':\n case 'readwrite':\n case 'none':\n file.useLazyLock = useLazyLock;\n break;\n default:\n throw new Error(`unexpected value for lazy_lock: ${value}`);\n }\n }\n {\n const s = file.useLazyLock;\n const ptr = this._module._sqlite3_malloc64(s.length + 1);\n this._module.stringToUTF8(s, ptr, s.length + 1);\n pArg.setUint32(0, ptr, true);\n }\n return VFS.SQLITE_OK;\n case 'write_ahead':\n // For testing purposes only: enable or disable write-ahead mode.\n if (value !== null) {\n if (file.lockState !== VFS.SQLITE_LOCK_NONE) {\n throw new Error('cannot change write_ahead mode while database is locked');\n }\n file.useWriteAhead = parseInt(value) !== 0;\n } else {\n const s = (file.useWriteAhead ? '1' : '0');\n const ptr = this._module._sqlite3_malloc64(s.length + 1);\n this._module.stringToUTF8(s, ptr, s.length + 1);\n pArg.setUint32(0, ptr, true);\n }\n return VFS.SQLITE_OK;\n }\n break;\n\n // Support SQLite batch atomic write transactions.\n case VFS.SQLITE_FCNTL_BEGIN_ATOMIC_WRITE:\n case VFS.SQLITE_FCNTL_COMMIT_ATOMIC_WRITE:\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n return VFS.SQLITE_OK;\n }\n break;\n case VFS.SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE:\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n file.writeAhead.rollback();\n return VFS.SQLITE_OK;\n }\n break;\n\n case VFS.SQLITE_FCNTL_SYNC:\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n if (file.useWriteAhead) {\n file.writeAhead.commit();\n }\n }\n break;\n }\n } catch (e) {\n console.error(e.stack);\n this.lastError = e;\n return VFS.SQLITE_IOERR;\n }\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {number} pFile\n * @returns {number}\n */\n jDeviceCharacteristics(pFile) {\n let result = VFS.SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;\n\n const file = this.mapIdToFile.get(pFile);\n if (file.useWriteAhead) {\n // When write-ahead is in use, we can do batch atomic writes.\n result |= VFS.SQLITE_IOCAP_BATCH_ATOMIC;\n }\n return result;\n }\n\n /**\n * @param {Uint8Array} zBuf \n * @returns {number}\n */\n jGetLastError(zBuf) {\n if (this.lastError) {\n console.error(this.lastError);\n const outputArray = zBuf.subarray(0, zBuf.byteLength - 1);\n const { written } = new TextEncoder().encodeInto(this.lastError.message, outputArray);\n zBuf[written] = 0;\n }\n return VFS.SQLITE_OK\n }\n\n /**\n * @param {string} zName \n * @returns {FileSystemSyncAccessHandle}\n */\n #openTemporaryFile(zName) {\n if (this.unboundTempFiles.size === 0) {\n throw new Error('no temporary files available');\n }\n\n // Bind an access handle from the temporary pool.\n const accessHandle = this.unboundTempFiles.values().next().value;\n this.unboundTempFiles.delete(accessHandle);\n this.boundTempFiles.set(zName, accessHandle);\n return accessHandle;\n }\n\n /**\n * @param {FileEntry} file \n */\n #deleteTemporaryFile(file) {\n file.accessHandle.truncate(0);\n\n // Temporary files are not actually deleted, just returned to the pool.\n this.mapPathToFile.delete(file.zName);\n this.unboundTempFiles.add(file.accessHandle);\n this.boundTempFiles.delete(file.zName);\n }\n\n /**\n * @param {string} dbName \n * @returns {string}\n */\n #getJournalNameFromDbName(dbName) {\n return `${dbName}-journal`;\n }\n\n #getWriteAheadNameFromDbName(dbName) {\n // Our WAL file is not compatible with SQLite WAL, so use a distinct name.\n return `${dbName}-waf`;\n }\n\n /**\n * @param {string} journalName \n * @returns {FileEntry}\n */\n #getDbFileFromJournalName(journalName) {\n const dbFilename = journalName.slice(0, -'-journal'.length);\n return this.mapPathToFile.get(dbFilename);\n }\n\n /**\n * Asynchronous PRAGMA operation to checkpoint the write-ahead log.\n * @param {FileEntry} file \n * @param {'passive'|'full'|'restart'|'truncate'} mode \n */\n async #pendingCheckpoint(file, mode) {\n try {\n if (mode !== 'passive') {\n await file.writeLock.acquire('exclusive');\n }\n \n await file.writeAhead.checkpoint(mode);\n } catch (e) {\n if (e.name === 'AbortError') {\n e.code = VFS.SQLITE_BUSY;\n }\n throw e;\n } finally {\n file.writeLock.release();\n }\n }\n\n /**\n * @param {FileEntry} file \n */\n async #retryLockRead(file) {\n try {\n await file.readLock.acquire('shared', file.timeout);\n file.writeAhead.isolateForRead();\n file.retryResult = {};\n } catch (e) {\n if (file.readLock.mode) {\n file.readLock.release();\n }\n file.retryResult = e;\n }\n }\n\n /**\n * @param {FileEntry} file \n */\n async #retryLockWrite(file) {\n try {\n // Write-ahead transactions only need writeLock, not readLock.\n await file.writeLock.acquire('exclusive', file.timeout);\n file.writeAhead.isolateForWrite();\n file.retryResult = {};\n } catch (e) {\n if (file.writeLock.mode) {\n file.writeLock.release();\n }\n file.retryResult = e;\n }\n }\n\n /**\n * @param {FileEntry} file \n */\n async #retryLockExclusive(file) {\n try {\n // This transaction will write directly to the database,\n // i.e. not using write-ahead. Get exclusive access.\n await file.readLock.acquire('exclusive', file.timeout);\n await file.writeLock.acquire('exclusive', file.timeout);\n\n // Transfer everything in write-ahead to the OPFS file.\n await file.writeAhead.checkpoint('restart');\n file.retryResult = {};\n } catch (e) {\n if (file.writeLock.mode) {\n file.writeLock.release();\n }\n if (file.readLock.mode) {\n file.readLock.release();\n }\n file.retryResult = e;\n }\n }\n\n /**\n * Handle asynchronous jOpen() tasks.\n * @param {string} zName \n * @param {number} flags \n * @param {number} fileId \n * @param {DataView} pOutFlags \n * @returns {Promise<void>}\n */\n async #retryOpen(zName, flags, fileId, pOutFlags) {\n /** @type {(() => void)[]} */ const onError = [];\n const file = this.mapPathToFile.get(zName);\n try {\n await navigator.locks.request(`${zName}#open`, async lock => {\n // Parse the path components.\n const directoryNames = zName.split('/').filter(d => d);\n const dbName = directoryNames.pop();\n\n // Get the OPFS directory handle.\n let dirHandle = await navigator.storage.getDirectory();\n const create = !!(flags & VFS.SQLITE_OPEN_CREATE);\n for (const directoryName of directoryNames) {\n dirHandle = await dirHandle.getDirectoryHandle(directoryName, { create });\n }\n\n // Open the main database OPFS file. We need to know whether the file\n // was created or not so we know whether to remove any existing\n // IndexedDB database with the same name. This will not be necessary\n // if the write-ahead data is moved to OPFS entirely.\n let created = false;\n /** @type {FileSystemSyncAccessHandle} */ let accessHandle;\n try {\n const fileHandle = await dirHandle.getFileHandle(dbName);\n // @ts-ignore\n accessHandle = await fileHandle.createSyncAccessHandle({\n mode: 'readwrite-unsafe'\n });\n } catch (e) {\n if (e.name === 'NotFoundError' && create) {\n const fileHandle = await dirHandle.getFileHandle(dbName, { create });\n // @ts-ignore\n accessHandle = await fileHandle.createSyncAccessHandle({\n mode: 'readwrite-unsafe'\n });\n created = true;\n } else {\n throw e;\n }\n }\n onError.push(() => {\n accessHandle.close();\n if (created) {\n dirHandle.removeEntry(dbName);\n }\n });\n\n // Pre-open the journal OPFS file here.\n const journalName = this.#getJournalNameFromDbName(dbName);\n const fileHandle = await dirHandle.getFileHandle(journalName, { create: true });\n // @ts-ignore\n const journalHandle = await fileHandle.createSyncAccessHandle({\n mode: 'readwrite-unsafe'\n });\n onError.push(() => {\n journalHandle.close();\n if (created) {\n dirHandle.removeEntry(journalName);\n }\n });\n\n // Open the WAL file.\n const waName = this.#getWriteAheadNameFromDbName(dbName);\n const waFileHandle = await dirHandle.getFileHandle(waName, { create: true });\n // @ts-ignore\n const waHandle = await waFileHandle.createSyncAccessHandle({\n mode: 'readwrite-unsafe'\n });\n onError.push(() => {\n waHandle.close();\n if (created) {\n dirHandle.removeEntry(waName);\n }\n });\n \n // Create the write-ahead manager.\n const writeAhead = new WriteAhead(\n zName,\n accessHandle,\n waHandle,\n Object.assign({ create: created }, this.options));\n await writeAhead.ready();\n\n file.retryResult = { accessHandle, waHandle, journalHandle, writeAhead };\n });\n } catch (e) {\n while (onError.length) {\n onError.pop()();\n }\n file.retryResult = e;\n }\n }\n}\n","import { Lock } from './Lock.js';\n\nconst DEFAULT_AUTOCHECKPOINT_PAGES = 1000;\nconst DEFAULT_BACKSTOP_INTERVAL = 30_000;\n\nconst SECTOR_SIZE = 4096;\nconst WAL_FILE_HEADER_SIZE = 32;\nconst WAL_FRAME_BASE = SECTOR_SIZE + WAL_FILE_HEADER_SIZE; // first frame starts at this offset\n\n/**\n * @typedef PageEntry\n * @property {number} waOffset location in WAL file\n * @property {number} pageSize\n * @property {Uint8Array} [pageData]\n */\n\n/**\n * @typedef Transaction\n * @property {number} id\n * @property {Map<number, PageEntry>} pages address to page data mapping\n * @property {number} dbFileSize\n * @property {number} dbPageSize\n * @property {number} waOffsetEnd\n */\n\n/**\n * @typedef WriteAheadOptions\n * @property {boolean} [create=false] true if database is being created\n * @property {number} [autoCheckpointPages]\n * @property {number} [backstopInterval]\n */\n\nexport class WriteAhead {\n log = null;\n /** @type {WriteAheadOptions} */ options = {\n create: false,\n autoCheckpointPages: DEFAULT_AUTOCHECKPOINT_PAGES,\n backstopInterval: DEFAULT_BACKSTOP_INTERVAL,\n };\n\n #zName;\n #dbHandle;\n #waFile;\n\n #dbFileSize = 0;\n\n /** @type {Promise<any>} */ #ready;\n /** @type {'read'|'write'} */ #isolationState = null\n\n /** @type {Lock} */ #txIdLock = null;\n /** @type {Transaction} */ #txActive = null;\n \n /** @type {Map<number, PageEntry>} */ #waOverlay = new Map();\n /** @type {Map<number, Transaction>} */ #mapIdToTx = new Map();\n /** @type {Map<number, Transaction>} */ #pendingTx = new Map();\n #mapIdToTxPageCount = 0;\n\n #broadcastChannel;\n /** @type {number} */ #backstopTimer;\n\n /**\n * @param {string} zName \n * @param {FileSystemSyncAccessHandle} dbHandle\n * @param {FileSystemSyncAccessHandle} waHandle\n * @param {WriteAheadOptions} options \n */\n constructor(zName, dbHandle, waHandle, options) {\n this.#zName = zName;\n this.#dbHandle = dbHandle;\n this.#waFile = new WriteAheadFile(waHandle);\n this.options = Object.assign(this.options, options);\n\n // All the asynchronous initialization is done here.\n this.#ready = (async () => {\n // Disable checkpointing by other connections until we're ready.\n await navigator.locks.request(`${this.#zName}-ckpt`, async () => {\n await this.#updateTxIdLock();\n });\n\n // Load all the transactions from the WAL file.\n if (this.options.create) {\n this.#waFile.create();\n }\n\n this.#waFile.open();\n for (const tx of this.#waFile.readAllTx()) {\n this.#activateTx(tx);\n }\n this.#updateTxIdLock(); // doesn't need await\n\n // Listen for transactions and checkpoints from other connections.\n this.#broadcastChannel = new BroadcastChannel(`${zName}#wa`);\n this.#broadcastChannel.onmessage = (event) => {\n this.#handleMessage(event);\n };\n\n // Schedule backstop. The backstop is a guard against a crash in\n // another context between persisting a transaction and broadcasting\n // it.\n this.#backstop();\n })();\n }\n\n /**\n * @returns {Promise<void>}\n */\n ready() {\n return this.#ready;\n }\n\n close() {\n // Stop asynchronous maintenance.\n this.#broadcastChannel.onmessage = null;\n clearTimeout(this.#backstopTimer);\n\n // Wait for any pending commit to complete.\n this.#txIdLock?.release();\n this.#broadcastChannel.close();\n }\n\n /**\n * Freeze our view of the database.\n * The view includes the transactions received so far but is not\n * guaranteed to be completely up to date (this allows this method\n * to be synchronous). Unfreeze the view with rejoin().\n */\n isolateForRead() {\n if (this.#isolationState !== null) {\n throw new Error('Already in isolated state');\n }\n this.#isolationState = 'read';\n\n if (this.#waFile.checkReset()) {\n // The WAL file has been restarted after a full checkpoint. Our\n // view must be at the final transaction before the checkpoint for\n // that to have happened. The previous overlay is now stale. In\n // case we haven't received the checkpoint broadcast, make sure\n // the overlay is cleared.\n this.#handleCheckpoint(this.#waFile.txId);\n }\n }\n\n /**\n * Freeze our view of the database for writing.\n * The view includes all transactions. Unfreeze the view with rejoin().\n */\n isolateForWrite() {\n if (this.#isolationState !== null) {\n throw new Error('Already in isolated state');\n }\n this.#isolationState = 'write';\n\n // Backstop is not needed while writing because we will be current.\n clearTimeout(this.#backstopTimer);\n this.#backstopTimer = null;\n\n if (this.#waFile.checkReset()) {\n // The WAL file has been restarted after a full checkpoint. Our\n // view must be at the final transaction before the checkpoint for\n // that to have happened. The previous overlay is now stale. In\n // case we haven't received the checkpoint broadcast, make sure\n // the overlay is cleared.\n this.#handleCheckpoint(this.#waFile.txId);\n }\n\n // A writer needs all previous transactions assimilated.\n this.#advanceTxId({ readToCurrent: true });\n }\n\n rejoin() {\n if (this.#isolationState === 'write') {\n // Resume backstop after write isolation.\n this.#backstop();\n\n // We need a place for a connection that only does write transactions\n // to auto-checkpoint. This the best place because writing is\n // complete.\n this.#autoCheckpoint();\n } else {\n // Catch up on new transactions that arrived while isolated.\n this.#advanceTxId({ autoCheckpoint: true });\n }\n this.#isolationState = null;\n }\n\n /**\n * @param {number} offset \n * @return {Uint8Array?}\n */\n read(offset) {\n // First look for the page in any write transaction in progress.\n // If the page is not found in the transaction overlay, look in the\n // write-ahead overlay.\n const pageEntry = this.#txActive?.pages.get(offset) ?? this.#waOverlay.get(offset);\n if (pageEntry) {\n this.log?.(`%cread page at offset ${offset} from WAL ${pageEntry.waOffset}`, 'background-color: gold;');\n if (pageEntry.pageData) {\n // Page data is cached.\n return pageEntry.pageData;\n }\n\n // Read the page from the WAL file.\n const buffer = new Uint8Array(pageEntry.pageSize);\n this.#waFile.accessHandle.read(buffer, { at: pageEntry.waOffset });\n return buffer;\n }\n return null;\n }\n\n /**\n * @param {number} offset \n * @param {Uint8Array} data \n */\n write(offset, data) {\n if (this.#isolationState !== 'write') {\n throw new Error('Not in write isolated state');\n }\n\n if (!this.#txActive) {\n // There is no active transaction so create one.\n this.#txActive = this.#waFile.beginTx();\n }\n const waOffset = this.#waFile.writePage(offset, data.slice());\n this.log?.(`%c#write page at ${offset} to WAL ${waOffset}`, 'background-color: lightskyblue;');\n }\n\n /**\n * @param {number} newSize \n */\n truncate(newSize) {\n // Remove any pages past the truncation point. We don't need to save\n // the file size because that will be extracted from page 1.\n for (const offset of this.#txActive.pages.keys()) {\n if (offset >= newSize) {\n this.#txActive.pages.delete(offset);\n }\n }\n }\n\n getFileSize() {\n // If the overlay is empty, the last file size may no longer be valid\n // if direct changes were made to the main database file.\n return this.#waOverlay.size ? this.#dbFileSize : null;\n }\n\n commit() {\n // Persist the final pending transaction page with the database size.\n this.#waFile.commitTx();\n\n // Incorporate the transaction locally.\n this.#activateTx(this.#txActive);\n this.#updateTxIdLock();\n\n // Send the transaction to other connections.\n const payload = { type: 'tx', tx: this.#txActive };\n this.#broadcastChannel.postMessage(payload);\n this.#txActive = null;\n\n }\n\n rollback() {\n // Discard transaction pages.\n this.#waFile.abortTx();\n this.#txActive = null;\n }\n \n /**\n * @param {{durability: 'strict'|'relaxed'}} options \n */\n sync(options) {\n if (options.durability === 'strict') {\n this.#waFile.accessHandle.flush();\n }\n }\n\n /**\n * Flush all write-ahead transactions to the main database file.\n * There must be no other connections reading or writing.\n * @param {'passive'|'full'|'restart'|'truncate'} mode\n */\n async checkpoint(mode) {\n if (mode !== 'passive') {\n this.isolateForWrite();\n }\n try {\n const options = {\n isRestart: mode !== 'passive',\n isRequired: mode !== 'passive'\n };\n await this.#checkpoint(options);\n\n if (mode === 'truncate') {\n this.#waFile.accessHandle.truncate(this.#waFile.currentOffset);\n this.log?.(`%ccheckpoint restart WAL file`, 'background-color: lightgreen;');\n }\n } finally {\n if (mode !== 'passive') {\n this.rejoin();\n }\n }\n }\n\n /**\n * Return the known usage size of the write-ahead file. Note that the\n * actual file size may be larger than reported if this connection is\n * not current or if the file has obsolete content past the current point.\n * @returns {number}\n */\n getWriteAheadSize() {\n return this.#waFile.currentOffset;\n }\n\n /**\n * Incorporate a transaction into our view of the database.\n * @param {Transaction} tx \n */\n #activateTx(tx) {\n // Transfer to the active collection of transactions.\n this.#mapIdToTx.set(tx.id, tx);\n this.#mapIdToTxPageCount += tx.pages.size;\n \n // Add transaction pages to the write-ahead overlay.\n for (const [offset, pageEntry] of tx.pages) {\n this.#waOverlay.set(offset, pageEntry);\n }\n this.#dbFileSize = tx.dbFileSize;\n }\n\n /**\n * Advance the local view of the database. By default, advance to the\n * last broadcast transaction. Optionally, also advance through any\n * additional transactions in the WAL file to be fully current.\n * \n * @param {{readToCurrent?: boolean, autoCheckpoint?: boolean}} options\n */\n #advanceTxId(options = {}) {\n let didAdvance = false;\n while (this.#pendingTx.size) {\n // Fetch the next transaction in sequence. Usually this will come\n // from pendingTx, but if it is missing then read it from the file.\n const nextTxId = this.#waFile.txId + 1;\n let tx;\n if (this.#pendingTx.has(nextTxId)) {\n tx = this.#pendingTx.get(nextTxId);\n this.#pendingTx.delete(tx.id);\n this.#waFile.skipTx(tx.id, tx.waOffsetEnd);\n } else {\n tx = this.#waFile.readTx();\n }\n\n this.#activateTx(tx);\n didAdvance = true;\n }\n\n if (options.readToCurrent) {\n // Read all additional transactions from the WAL file.\n for (const tx of this.#waFile.readAllTx()) {\n this.#activateTx(tx);\n didAdvance = true;\n }\n }\n\n if (didAdvance) {\n // Publish our new view txId.\n this.#updateTxIdLock();\n\n if (options.autoCheckpoint) {\n this.#autoCheckpoint();\n }\n }\n }\n\n #autoCheckpoint() {\n // Perform an automatic checkpoint if enabled and needed. Automatic\n // checkpoints are passive, so this will not change the WAL file\n // usage or size.\n if (this.options.autoCheckpointPages > 0 &&\n this.#mapIdToTxPageCount >= this.options.autoCheckpointPages) {\n this.log?.(`%cauto-checkpoint`, 'background-color: lightgreen;');\n this.checkpoint('passive');\n }\n }\n\n /**\n * Move pages from write-ahead to main database file.\n * \n * @param {{isRestart?: boolean, isRequired?: boolean}} options\n */\n async #checkpoint(options = {}) {\n // By default, checkpointing is abandoned if another connection is\n // already checkpointing. With the isRequired option, a checkpoint\n // is always performed. This is necessary for leaving write-ahead\n // mode or a user-requested checkpoint.\n const lockOptions = {\n ifAvailable: !options.isRequired,\n };\n\n await navigator.locks.request(`${this.#zName}-ckpt`, lockOptions, async lock => {\n if (!lock) return null;\n\n /** @type {number} */ let ckptId;\n if (options.isRestart) {\n // Full checkpoint, use the current WAL file txId.\n ckptId = this.#waFile.txId;\n\n // Wait for all connections to reach this txId.\n await this.#waitForTxIdLocks(value => value.maxTxId >= ckptId);\n this.log?.(`%c#checkpoint full txId ${ckptId}`, 'background-color: lightgreen;');\n } else {\n // Not a full checkpoint, so find the lowest txId in use by any\n // connection.\n ckptId = (await this.#getTxIdLocks())\n .reduce((min, value) => Math.min(min, value.maxTxId), this.#waFile.txId);\n this.log?.(`%c#checkpoint partial txId ${ckptId}`, 'background-color: lightgreen;');\n }\n\n // Sync the WAL file. This ensures that if there is a crash after\n // part of the WAL has been copied, the uncopied part will still be\n // available afterwards.\n this.#waFile.accessHandle.flush();\n\n // Starting at ckptId and going backwards (earlier), write transaction\n // pages to the main database file. Do not overwrite a page written\n // by a later transaction.\n const writtenOffsets = new Set();\n let dbFileSize = 0;\n let tx = /** @type {Transaction} */ ({ id: ckptId + 1 });\n while (tx = this.#mapIdToTx.get(tx.id - 1)) {\n if (tx.id === ckptId) {\n // Set the file size from the latest transaction. This may be\n // unnecessary as SQLite is not known to reduce the database size\n // except with VACUUM.\n dbFileSize = tx.dbFileSize;\n this.#dbHandle.truncate(dbFileSize);\n }\n\n for (const [offset, pageEntry] of tx.pages) {\n if (offset < dbFileSize && !writtenOffsets.has(offset)) {\n let pageData = pageEntry.pageData;\n if (!pageData) {\n // Page data was not cached, so read it from the WAL file.\n pageData = new Uint8Array(pageEntry.pageSize);\n const nRead = this.#waFile.accessHandle.read(\n pageData,\n { at: pageEntry.waOffset });\n if (nRead !== pageData.byteLength) {\n throw new Error('Checkpoint read failed');\n }\n }\n\n // Write the page to the database file.\n const nWritten = this.#dbHandle.write(pageData, { at: offset });\n if (nWritten !== pageData.byteLength) {\n throw new Error('Checkpoint write failed');\n }\n writtenOffsets.add(offset);\n this.log?.(`%c#checkpoint wrote txId ${tx.id} page at ${offset} to database`, 'background-color: lightgreen;');\n }\n }\n }\n\n if (writtenOffsets.size > 0) {\n if (ckptId == this.#waFile.txId) {\n // Ensure data is safely in the file.\n this.log?.(`%c#checkpoint flush database file`, 'background-color: lightgreen;');\n this.#dbHandle.flush();\n }\n\n // Notify other connections and ourselves of the checkpoint.\n this.#broadcastChannel.postMessage({\n type: 'ckpt',\n ckptId,\n });\n this.#handleCheckpoint(ckptId);\n }\n\n if (options.isRestart) {\n // Wait for all connections to clear their overlay.\n await this.#waitForTxIdLocks(value => value.minTxId > ckptId);\n\n this.#waFile.reset();\n }\n });\n }\n\n /**\n * After a checkpoint, remove checkpointed pages from write-ahead.\n * The checkpoint may be been done locally or by another connection.\n * @param {number} ckptId \n */\n #handleCheckpoint(ckptId) {\n this.log?.(`%c#handleCheckpoint to txId ${ckptId}`, 'background-color: lightgreen;');\n\n // Loop backwards from ckptId.\n let tx = /** @type {Transaction} */ ({ id: ckptId + 1 });\n while (tx = this.#mapIdToTx.get(tx.id - 1)) {\n // Remove pages from write-ahead overlay.\n for (const [offset, page] of tx.pages.entries()) {\n // Be sure not to remove a newer version of the page.\n const overlayPage = this.#waOverlay.get(offset);\n if (overlayPage === page) {\n this.log?.(`%cremove txId ${tx.id} page at offset ${offset}`, 'background-color: lightgreen;');\n this.#waOverlay.delete(offset);\n }\n }\n\n // Remove transaction.\n this.#mapIdToTx.delete(tx.id);\n this.#mapIdToTxPageCount -= tx.pages.size;\n }\n this.#updateTxIdLock();\n }\n\n /**\n * @param {MessageEvent} event \n */\n #handleMessage(event) {\n if (event.data.type === 'tx') {\n // New transaction from another connection. Don't use it if we\n // already have it.\n /** @type {Transaction} */ const tx = event.data.tx;\n if (tx.id > this.#waFile.txId) {\n this.#pendingTx.set(tx.id, tx);\n if (this.#isolationState === null) {\n // Not in an isolated state, so advance our view of the database.\n this.#advanceTxId({ autoCheckpoint: true });\n }\n }\n } else if (event.data.type === 'ckpt') {\n // Checkpoint notification from another connection.\n /** @type {number} */ const ckptId = event.data.ckptId;\n this.#handleCheckpoint(ckptId);\n }\n }\n\n /**\n * Periodic check for recovering from lost transaction broadcasts.\n */\n async #backstop() {\n try {\n if (this.#backstopTimer) { \n if (this.#isolationState === null) {\n // Not in an isolated state, so advance our view of the database.\n const oldTxId = this.#waFile.txId;\n this.#advanceTxId({ readToCurrent: true });\n if (this.#waFile.txId > oldTxId) {\n this.log?.(`%cbackstop txId ${oldTxId} -> ${this.#waFile.txId}`, 'background-color: lightyellow;');\n }\n }\n }\n } catch (e) {\n console.error('Backstop failed', e);\n }\n\n // Schedule next backstop. Add a bit of jitter to decorrelate\n // backstops across multiple connections.\n const delay = this.options.backstopInterval * (0.9 + 0.2 * Math.random());\n this.#backstopTimer = self.setTimeout(() => {\n this.#backstop();\n }, delay);\n }\n \n /**\n * Update the lock that publishes our current txId.\n */\n async #updateTxIdLock() {\n // Our view of the database, i.e. the txId, is encoded into the name\n // of a lock so other connections can see it. When our txId changes,\n // we acquire a new lock and release the old one. We must not release\n // the old lock until the new one is in place.\n const oldLock = this.#txIdLock;\n const newLockName = this.#encodeTxIdLockName();\n if (oldLock?.name !== newLockName) {\n this.#txIdLock = new Lock(newLockName);\n await this.#txIdLock.acquire('shared').then(() => {\n // The new lock is acquired.\n oldLock?.release();\n });\n\n if (this.log) {\n const { minTxId, maxTxId } = this.#decodeTxIdLockName(newLockName);\n this.log?.(`%ctxId to ${minTxId}:${maxTxId}`, 'background-color: pink;');\n }\n }\n }\n\n /**\n * Get all txId locks for this database.\n * @returns {Promise<{name: string, minTxId: number, maxTxId: number}[]>}\n */\n async #getTxIdLocks() {\n const { held } = await navigator.locks.query();\n return held\n .map(lock => this.#decodeTxIdLockName(lock.name))\n .filter(value => value !== null)\n }\n\n /**\n * @returns {string}\n */\n #encodeTxIdLockName() {\n // The maxTxId is our current view of the database. The minTxId is\n // the lowest txId we get pages from the WAL for, which is the lowest\n // key in mapIdToTx. If mapIdToTx is empty then we aren't reading\n // from the WAL at all - in this case we arbitrarily set minTxId to\n // invalid value maxTxId + 1.\n const maxTxId = this.#waFile.txId;\n const minTxId = this.#mapIdToTx.keys().next().value ?? (maxTxId + 1);\n return `${this.#zName}-txId<${minTxId}:${maxTxId}>`;\n }\n\n /**\n * @param {string} lockName \n * @returns {{name: string, minTxId: number, maxTxId: number}}\n */\n #decodeTxIdLockName(lockName) {\n const match = lockName.match(/^(.*)-txId<(\\d+):(\\d+)>$/);\n if (match?.[1] === this.#zName) {\n // This txId lock is for this database.\n return {\n name: match[1],\n minTxId: parseInt(match[2]),\n maxTxId: parseInt(match[3])\n };\n }\n return null;\n }\n\n /**\n * Wait for all txId locks that fail the provided predicate.\n * @param {(lock: {name: string, minTxId: number, maxTxId: number}) => boolean} predicate \n */\n async #waitForTxIdLocks(predicate) {\n /** @type {string[]} */ let failingLockNames = [];\n do {\n // Wait for all connections that fail the predicate.\n await Promise.all(\n failingLockNames.map(name => navigator.locks.request(name, async () => {}))\n );\n\n // Refresh the list of failing locks.\n failingLockNames = (await this.#getTxIdLocks())\n .filter(value => !predicate(value))\n .map(value => value.name);\n } while (failingLockNames.length > 0);\n }\n}\n\nclass WriteAheadFile {\n static MAGIC = 0x377f0684;\n static FILE_HEADER_SIZE = 32;\n static FRAME_HEADER_SIZE = 32;\n static FRAME_TYPE_PAGE = 0;\n static FRAME_TYPE_COMMIT = 1;\n\n /** @type {FileSystemSyncAccessHandle} */ accessHandle;\n /** @type {number} */ currentOffset;\n \n txId = 0;\n salt1 = 0;\n salt2 = 0;\n\n /** @type {Transaction} */ txInProgress = null;\n\n constructor(accessHandle) {\n this.accessHandle = accessHandle;\n }\n\n create() {\n this.accessHandle.truncate(0);\n this.#writeFileHeader();\n }\n\n open() {\n // Read file headers from both header slots and use the one with the\n // higher nextTxId.\n const fileHeader = [0, SECTOR_SIZE]\n .map(offset => this.#readFileHeader(offset))\n .filter(h => h)\n .sort((a, b) => b.nextTxId - a.nextTxId)[0];\n \n this.currentOffset = WAL_FRAME_BASE;\n this.txId = fileHeader.nextTxId - 1;\n this.salt1 = fileHeader.salt1;\n this.salt2 = fileHeader.salt2;\n }\n\n reset(options = { truncate: true }) {\n const fileHeader = this.#writeFileHeader();\n if (options.truncate) {\n this.accessHandle.truncate(WAL_FRAME_BASE);\n }\n\n this.currentOffset = WAL_FRAME_BASE;\n this.salt1 = fileHeader.salt1;\n this.salt2 = fileHeader.salt2;\n }\n\n checkReset() {\n // Check for a new header. There are two header slots in the file.\n // Look in the slot that is not current, which will be at offset 0\n // or SECTOR_SIZE depending on the current salt1 value (salt1 is\n // incremented on each new header).\n const headerOffset = (this.salt1 & 0x1) ? 0 : SECTOR_SIZE;\n const fileHeader = this.#readFileHeader(headerOffset);\n if (fileHeader?.nextTxId > this.txId &&\n fileHeader.salt1 === ((this.salt1 + 1) | 0)) {\n // The WAL file has been reset.\n this.currentOffset = WAL_FRAME_BASE;\n this.salt1 = fileHeader.salt1;\n this.salt2 = fileHeader.salt2;\n return true;\n }\n return false;\n }\n\n *readAllTx() {\n while (true) {\n const tx = this.readTx();\n if (!tx) break;\n yield tx;\n }\n }\n\n /**\n * @returns {Transaction?}\n */\n readTx() {\n // Read the next complete transaction or return null.\n let offset = this.currentOffset;\n /** @type {Transaction?} */ let tx = null;\n while (true) {\n const frame = this.#readFrame(offset);\n if (!frame) return null;\n\n if (frame.frameType === WriteAheadFile.FRAME_TYPE_COMMIT) {\n // Update the instance state.\n this.txId += 1;\n this.currentOffset = offset + frame.byteLength;\n \n tx.id = this.txId;\n tx.dbFileSize = frame.dbFileSize;\n tx.waOffsetEnd = this.currentOffset;\n return tx;\n }\n\n // frameType === WriteAheadFile.FRAME_TYPE_PAGE\n if (!tx) {\n tx = {\n id: 0, // placeholder\n pages: new Map(),\n dbFileSize: 0, // placeholder\n dbPageSize: frame.pageData.byteLength,\n waOffsetEnd: 0 // placeholder\n };\n }\n\n tx.pages.set(\n frame.pageOffset,\n {\n pageSize: frame.pageData.byteLength,\n waOffset: offset + WriteAheadFile.FRAME_HEADER_SIZE\n });\n\n offset += frame.byteLength;\n }\n }\n\n /**\n * This method is called when transaction(s) have been received by other\n * means than readTx(), e.g. via BroadcastChannel.\n * \n * @param {number} txId \n * @param {number} offset \n */\n skipTx(txId, offset) {\n this.txId = txId;\n this.currentOffset = offset;\n }\n\n /**\n * \n * @param {{overwrite?: boolean}} options \n * @returns {Transaction}\n */\n beginTx(options = {}) {\n this.txInProgress = {\n id: this.txId + 1,\n pages: new Map(),\n dbFileSize: 0,\n dbPageSize: 0,\n waOffsetEnd: this.currentOffset\n };\n return this.txInProgress;\n }\n\n /**\n * Write a page frame to the WAL file.\n * \n * @param {number} pageOffset \n * @param {Uint8Array} pageData \n */\n writePage(pageOffset, pageData) {\n const headerView = new DataView(new ArrayBuffer(WriteAheadFile.FRAME_HEADER_SIZE));\n headerView.setUint8(0, WriteAheadFile.FRAME_TYPE_PAGE);\n headerView.setUint16(2, pageData.byteLength === 65536 ? 1 : pageData.byteLength);\n headerView.setBigUint64(8, BigInt(pageOffset));\n headerView.setUint32(16, this.salt1);\n headerView.setUint32(20, this.salt2);\n\n const checksum = new Checksum();\n checksum.update(new Uint8Array(headerView.buffer, 0, WriteAheadFile.FRAME_HEADER_SIZE - 8));\n checksum.update(pageData);\n headerView.setUint32(24, checksum.s0);\n headerView.setUint32(28, checksum.s1);\n\n const bytesWritten =\n this.accessHandle.write(headerView, { at: this.txInProgress.waOffsetEnd }) +\n this.accessHandle.write(pageData, {\n at: this.txInProgress.waOffsetEnd + WriteAheadFile.FRAME_HEADER_SIZE\n });\n if (bytesWritten !== headerView.byteLength + pageData.byteLength) {\n throw new Error('write failed');\n }\n\n const pageEntry = {\n pageSize: pageData.byteLength,\n waOffset: this.txInProgress.waOffsetEnd + WriteAheadFile.FRAME_HEADER_SIZE\n };\n if (pageOffset === 0) {\n // This is page 1, which contains the database header.\n const dataView = new DataView(pageData.buffer, pageData.byteOffset, pageData.byteLength);\n const pageCount = dataView.getUint32(28);\n this.txInProgress.dbFileSize = pageCount * pageData.byteLength;\n this.txInProgress.dbPageSize = pageData.byteLength;\n\n // Cache page 1 as a performance optimization and to exercise the\n // cache code path.\n pageEntry.pageData = pageData;\n }\n\n this.txInProgress.pages.set(pageOffset, pageEntry);\n this.txInProgress.waOffsetEnd += bytesWritten;\n\n return pageEntry.waOffset;\n }\n\n /**\n * @returns {Transaction}\n */\n commitTx() {\n const headerView = new DataView(new ArrayBuffer(WriteAheadFile.FRAME_HEADER_SIZE));\n headerView.setUint8(0, WriteAheadFile.FRAME_TYPE_COMMIT);\n headerView.setBigUint64(8, BigInt(this.txInProgress.dbFileSize));\n headerView.setUint32(16, this.salt1);\n headerView.setUint32(20, this.salt2);\n\n const checksum = new Checksum();\n checksum.update(new Uint8Array(headerView.buffer, 0, WriteAheadFile.FRAME_HEADER_SIZE - 8));\n headerView.setUint32(24, checksum.s0);\n headerView.setUint32(28, checksum.s1);\n\n const bytesWritten = this.accessHandle.write(headerView, {\n at: this.txInProgress.waOffsetEnd\n });\n if (bytesWritten !== headerView.byteLength) {\n throw new Error('write failed');\n }\n this.txInProgress.waOffsetEnd += bytesWritten;\n\n const tx = this.txInProgress;\n this.txInProgress = null;\n this.currentOffset = tx.waOffsetEnd;\n this.txId = tx.id;\n return tx;\n }\n\n abortTx() {\n this.txInProgress = null;\n }\n\n #readFileHeader(offset) {\n const headerView = new DataView(new ArrayBuffer(WriteAheadFile.FILE_HEADER_SIZE));\n if (this.accessHandle.read(headerView, { at: offset }) !== headerView.byteLength) {\n return null;\n }\n\n if (headerView.getUint32(0) !== WriteAheadFile.MAGIC) return null;\n\n const checksum = new Checksum();\n checksum.update(new Uint8Array(headerView.buffer, 0, WriteAheadFile.FILE_HEADER_SIZE - 8));\n if (!checksum.matches(headerView.getUint32(24), headerView.getUint32(28))) {\n return null;\n }\n\n return {\n nextTxId: Number(headerView.getBigUint64(8)),\n salt1: headerView.getUint32(16),\n salt2: headerView.getUint32(20)\n }\n }\n\n #readFrame(offset) {\n const headerView = new DataView(new ArrayBuffer(WriteAheadFile.FRAME_HEADER_SIZE));\n if (this.accessHandle.read(headerView, { at: offset }) !== headerView.byteLength) {\n // EOF, not an error.\n return null;\n }\n\n // Verify the frame header salt values match the file header.\n const frameSalt1 = headerView.getUint32(16);\n const frameSalt2 = headerView.getUint32(20);\n if (frameSalt1 !== this.salt1 || frameSalt2 !== this.salt2) {\n // Not necessarily an error, could be from a restart without\n // truncation.\n return null;\n }\n\n const payloadSize = (size => size === 1 ? 65536 : size)(headerView.getUint16(2));\n /** @type {Uint8Array} */ let payloadData;\n if (payloadSize) {\n payloadData = new Uint8Array(payloadSize);\n const payloadBytesRead = this.accessHandle.read(\n payloadData,\n { at: offset + WriteAheadFile.FRAME_HEADER_SIZE });\n if (payloadBytesRead !== payloadSize ) return null;\n }\n\n const checksum = new Checksum();\n checksum.update(new Uint8Array(headerView.buffer, 0, WriteAheadFile.FRAME_HEADER_SIZE - 8));\n if (payloadData) {\n checksum.update(payloadData);\n }\n if (!checksum.matches(headerView.getUint32(24), headerView.getUint32(28))) {\n // Not necessarily an error, could be from a restart without\n // truncation.\n return null;\n }\n\n const frameType = headerView.getUint8(0);\n if (frameType === WriteAheadFile.FRAME_TYPE_PAGE) {\n return {\n frameType,\n byteLength: WriteAheadFile.FRAME_HEADER_SIZE + payloadSize,\n pageOffset: Number(headerView.getBigUint64(8)),\n pageData: payloadData\n }\n } else if (frameType === WriteAheadFile.FRAME_TYPE_COMMIT) {\n // Flags byte is currently unused. A possible future use would be\n // to indicate an overwrite commit to trigger clearing the overlay.\n // This might allow a page size change by VACUUM.\n const flags = headerView.getUint8(1);\n return {\n frameType,\n byteLength: WriteAheadFile.FRAME_HEADER_SIZE,\n dbFileSize: Number(headerView.getBigUint64(8)),\n isOverwrite: !!(flags & 0x1)\n }\n }\n throw new Error(`Invalid frame type: ${frameType}`);\n }\n\n #writeFileHeader() {\n // Derive new values from the previous values. Note that salt1 always\n // flips between even and odd so successive headers are written to\n // alternating slots. If the write fails, the file remains in a valid\n // state.\n const nextTxId = this.txId + 1;\n const salt1 = (this.salt1 + 1) | 0;\n const salt2 = Math.floor(Math.random() * 0xffffffff);\n const headerView = new DataView(new ArrayBuffer(WriteAheadFile.FILE_HEADER_SIZE));\n headerView.setUint32(0, WriteAheadFile.MAGIC);\n headerView.setBigUint64(8, BigInt(nextTxId));\n headerView.setUint32(16, salt1);\n headerView.setUint32(20, salt2);\n\n const checksum = new Checksum();\n checksum.update(new Uint8Array(headerView.buffer, 0, WriteAheadFile.FILE_HEADER_SIZE - 8));\n headerView.setUint32(24, checksum.s0);\n headerView.setUint32(28, checksum.s1);\n\n // A header with an even salt1 is written at offset 0, and with an\n // odd salt1 at SECTOR_SIZE.\n const headerOffset = (salt1 & 0x1) ? SECTOR_SIZE : 0;\n const bytesWritten = this.accessHandle.write(headerView, { at: headerOffset });\n if (bytesWritten !== headerView.byteLength) {\n throw new Error('write failed');\n }\n\n this.accessHandle.flush();\n return { nextTxId, salt1, salt2 };\n }\n}\n\n// https://www.sqlite.org/fileformat.html#checksum_algorithm\nclass Checksum {\n /** @type {number} */ s0 = 0;\n /** @type {number} */ s1 = 0;\n\n /**\n * @param {ArrayBuffer|ArrayBufferView} data \n */\n update(data) {\n if ((data.byteLength % 8) !== 0) throw new Error('Data must be a multiple of 8 bytes');\n const words = ArrayBuffer.isView(data) ?\n new Uint32Array(data.buffer, data.byteOffset, data.byteLength / 4) :\n new Uint32Array(data);\n for (let i = 0; i < words.length; i += 2) {\n this.s0 = (this.s0 + words[i] + this.s1) >>> 0;\n this.s1 = (this.s1 + words[i + 1] + this.s0) >>> 0;\n }\n }\n\n matches(s0, s1) {\n return this.s0 === s0 && this.s1 === s1;\n }\n}\n"],"names":[],"sourceRoot":""}
|