@powersync/web 1.12.2 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -0
- package/dist/3cb48be086dd9edd02ff.wasm +0 -0
- package/dist/{_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d0.index.umd.js → _journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa20.index.umd.js} +18 -8
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa20.index.umd.js.map +1 -0
- package/dist/{_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d1.index.umd.js → _journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa21.index.umd.js} +18 -8
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa21.index.umd.js.map +1 -0
- package/dist/df958358cadf945bd0fe.wasm +0 -0
- package/dist/f9c8ada26c59f5bf4339.wasm +0 -0
- package/dist/fe5693c7678cf12e05ac.wasm +0 -0
- package/dist/index.umd.js +3709 -499
- package/dist/index.umd.js.map +1 -1
- package/dist/worker/SharedSyncImplementation.umd.js +247 -2172
- package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
- package/dist/worker/WASQLiteDB.umd.js +767 -196
- package/dist/worker/WASQLiteDB.umd.js.map +1 -1
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js +45 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js.map +1 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js +45 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js.map +1 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js +2 -2
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js.map +1 -1
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +45 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +1 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js +1509 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js.map +1 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js +39 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js.map +1 -1
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js +1641 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js.map +1 -0
- package/lib/package.json +6 -6
- package/lib/src/db/PowerSyncDatabase.d.ts +10 -2
- package/lib/src/db/PowerSyncDatabase.js +20 -4
- package/lib/src/db/adapters/AbstractWebSQLOpenFactory.d.ts +2 -0
- package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js +3 -0
- package/lib/src/db/adapters/AsyncDatabaseConnection.d.ts +26 -0
- package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.d.ts +82 -0
- package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.js +239 -0
- package/lib/src/db/adapters/WebDBAdapter.d.ts +17 -0
- package/lib/src/db/adapters/WebDBAdapter.js +1 -0
- package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.d.ts +39 -0
- package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.js +46 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.d.ts +127 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.js +343 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.d.ts +10 -42
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +36 -212
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +12 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +81 -4
- package/lib/src/db/adapters/web-sql-flags.d.ts +17 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +9 -2
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +16 -10
- package/lib/src/db/sync/WebStreamingSyncImplementation.d.ts +0 -5
- package/lib/src/index.d.ts +8 -7
- package/lib/src/index.js +8 -7
- package/lib/src/worker/db/WASQLiteDB.worker.js +38 -20
- package/lib/src/worker/db/open-worker-database.d.ts +5 -4
- package/lib/src/worker/db/open-worker-database.js +5 -3
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.d.ts +1 -0
- package/lib/src/worker/sync/SharedSyncImplementation.d.ts +20 -3
- package/lib/src/worker/sync/SharedSyncImplementation.js +40 -11
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/dist/24cd027f23123a1360de.wasm +0 -0
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d0.index.umd.js.map +0 -1
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d1.index.umd.js.map +0 -1
- package/lib/src/shared/open-db.d.ts +0 -5
- package/lib/src/shared/open-db.js +0 -193
- package/lib/src/shared/types.d.ts +0 -22
- /package/lib/src/{shared/types.js → db/adapters/AsyncDatabaseConnection.js} +0 -0
package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACliBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;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;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;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/@journeyapps/wa-sqlite/src/FacadeVFS.js","webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/VFS.js","webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.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 DataView. A Proxy\n * wrapper prevents use of incorrect type or endianness.\n * @param {'Int32'|'BigInt64'} type \n * @param {number} byteOffset \n * @returns {DataView}\n */\n #makeTypedDataView(type, byteOffset) {\n const byteLength = type === 'Int32' ? 4 : 8;\n const getter = `get${type}`;\n const setter = `set${type}`;\n const makeDataView = () => new DataView(\n this._module.HEAPU8.buffer,\n this._module.HEAPU8.byteOffset + byteOffset,\n byteLength);\n let dataView = makeDataView();\n return new Proxy(dataView, {\n get(_, prop) {\n if (dataView.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer.\n dataView = makeDataView();\n }\n if (prop === getter) {\n return function(byteOffset, littleEndian) {\n if (!littleEndian) throw new Error('must be little endian');\n return dataView[prop](byteOffset, littleEndian);\n }\n }\n if (prop === setter) {\n return function(byteOffset, value, littleEndian) {\n if (!littleEndian) throw new Error('must be little endian');\n return dataView[prop](byteOffset, value, littleEndian);\n }\n }\n if (typeof prop === 'string' && (prop.match(/^(get)|(set)/))) {\n throw new Error('invalid type');\n }\n const result = dataView[prop];\n return typeof result === 'function' ? result.bind(dataView) : result;\n }\n });\n }\n\n /**\n * @param {number} byteOffset \n * @param {number} byteLength \n */\n #makeDataArray(byteOffset, byteLength) {\n let target = this._module.HEAPU8.subarray(byteOffset, byteOffset + byteLength);\n return new Proxy(target, {\n get: (_, prop, receiver) => {\n if (target.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer.\n target = this._module.HEAPU8.subarray(byteOffset, byteOffset + byteLength);\n }\n const result = target[prop];\n return typeof result === 'function' ? result.bind(target) : result;\n }\n });\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","// 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);","// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport { FacadeVFS } from '../FacadeVFS.js';\nimport * as VFS from '../VFS.js';\n\nconst DEFAULT_TEMPORARY_FILES = 10;\nconst LOCK_NOTIFY_INTERVAL = 1000;\n\nconst DB_RELATED_FILE_SUFFIXES = ['', '-journal', '-wal'];\n\nconst finalizationRegistry = new FinalizationRegistry(releaser => releaser());\n\nclass File {\n /** @type {string} */ path\n /** @type {number} */ flags;\n /** @type {FileSystemSyncAccessHandle} */ accessHandle;\n\n /** @type {PersistentFile?} */ persistentFile;\n\n constructor(path, flags) {\n this.path = path;\n this.flags = flags;\n }\n}\n\nclass PersistentFile {\n /** @type {FileSystemFileHandle} */ fileHandle\n /** @type {FileSystemSyncAccessHandle} */ accessHandle = null\n\n // The following properties are for main database files.\n\n /** @type {boolean} */ isLockBusy = false;\n /** @type {boolean} */ isFileLocked = false;\n /** @type {boolean} */ isRequestInProgress = false;\n /** @type {function} */ handleLockReleaser = null;\n\n /** @type {BroadcastChannel} */ handleRequestChannel;\n /** @type {boolean} */ isHandleRequested = false;\n\n constructor(fileHandle) {\n this.fileHandle = fileHandle;\n }\n}\n\nexport class OPFSCoopSyncVFS extends FacadeVFS {\n /** @type {Map<number, File>} */ mapIdToFile = new Map();\n\n lastError = null;\n log = null; //function(...args) { console.log(`[${contextName}]`, ...args) };\n \n /** @type {Map<string, PersistentFile>} */ persistentFiles = new Map();\n /** @type {Map<string, FileSystemSyncAccessHandle>} */ boundAccessHandles = new Map();\n /** @type {Set<FileSystemSyncAccessHandle>} */ unboundAccessHandles = new Set();\n /** @type {Set<string>} */ accessiblePaths = new Set();\n releaser = null;\n\n static async create(name, module) {\n const vfs = new OPFSCoopSyncVFS(name, module);\n await Promise.all([\n vfs.isReady(),\n vfs.#initialize(DEFAULT_TEMPORARY_FILES),\n ]);\n return vfs;\n }\n\n constructor(name, module) {\n super(name, module);\n }\n\n async #initialize(nTemporaryFiles) {\n // Delete temporary directories no longer in use.\n const root = await navigator.storage.getDirectory();\n // @ts-ignore\n for await (const entry of root.values()) {\n if (entry.kind === 'directory' && entry.name.startsWith('.ahp-')) {\n // A lock with the same name as the directory protects it from\n // being deleted.\n await navigator.locks.request(entry.name, { ifAvailable: true }, async lock => {\n if (lock) {\n this.log?.(`Deleting temporary directory ${entry.name}`);\n await root.removeEntry(entry.name, { recursive: true });\n } else {\n this.log?.(`Temporary directory ${entry.name} is in use`);\n }\n });\n }\n }\n\n // Create our temporary directory.\n const tmpDirName = `.ahp-${Math.random().toString(36).slice(2)}`;\n this.releaser = await new Promise(resolve => {\n navigator.locks.request(tmpDirName, () => {\n return new Promise(release => {\n resolve(release);\n });\n });\n });\n finalizationRegistry.register(this, this.releaser);\n const tmpDir = await root.getDirectoryHandle(tmpDirName, { create: true });\n\n // Populate temporary directory.\n for (let i = 0; i < nTemporaryFiles; i++) {\n const tmpFile = await tmpDir.getFileHandle(`${i}.tmp`, { create: true });\n const tmpAccessHandle = await tmpFile.createSyncAccessHandle();\n this.unboundAccessHandles.add(tmpAccessHandle);\n }\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 const url = new URL(zName || Math.random().toString(36).slice(2), 'file://');\n const path = url.pathname;\n\n if (flags & VFS.SQLITE_OPEN_MAIN_DB) {\n const persistentFile = this.persistentFiles.get(path);\n if (persistentFile?.isRequestInProgress) {\n // Should not reach here unless SQLite itself retries an open.\n // Otherwise, asynchronous operations started on a previous\n // open try should have completed.\n return VFS.SQLITE_BUSY;\n } else if (!persistentFile) {\n // This is the usual starting point for opening a database.\n // Register a Promise that resolves when the database and related\n // files are ready to be used.\n this.log?.(`creating persistent file for ${path}`);\n const create = !!(flags & VFS.SQLITE_OPEN_CREATE);\n this._module.retryOps.push((async () => {\n try {\n // Get the path directory handle.\n let dirHandle = await navigator.storage.getDirectory();\n const directories = path.split('/').filter(d => d);\n const filename = directories.pop();\n for (const directory of directories) {\n dirHandle = await dirHandle.getDirectoryHandle(directory, { create });\n }\n\n // Get file handles for the database and related files,\n // and create persistent file instances.\n for (const suffix of DB_RELATED_FILE_SUFFIXES) {\n const fileHandle = await dirHandle.getFileHandle(filename + suffix, { create });\n await this.#createPersistentFile(fileHandle);\n }\n\n // Get access handles for the files.\n const file = new File(path, flags);\n file.persistentFile = this.persistentFiles.get(path);\n await this.#requestAccessHandle(file);\n } catch (e) {\n // Use an invalid persistent file to signal this error\n // for the retried open.\n const persistentFile = new PersistentFile(null);\n this.persistentFiles.set(path, persistentFile);\n console.error(e);\n }\n })());\n return VFS.SQLITE_BUSY;\n } else if (!persistentFile.fileHandle) {\n // The asynchronous open operation failed.\n this.persistentFiles.delete(path);\n return VFS.SQLITE_CANTOPEN;\n } else if (!persistentFile.accessHandle) {\n // This branch is reached if the database was previously opened\n // and closed.\n this._module.retryOps.push((async () => {\n const file = new File(path, flags);\n file.persistentFile = this.persistentFiles.get(path);\n await this.#requestAccessHandle(file);\n })());\n return VFS.SQLITE_BUSY;\n }\n }\n\n if (!this.accessiblePaths.has(path) &&\n !(flags & VFS.SQLITE_OPEN_CREATE)) {\n throw new Error(`File ${path} not found`);\n }\n\n const file = new File(path, flags);\n this.mapIdToFile.set(fileId, file);\n\n if (this.persistentFiles.has(path)) {\n file.persistentFile = this.persistentFiles.get(path);\n } else if (this.boundAccessHandles.has(path)) {\n // This temporary file was previously created and closed. Reopen\n // the same access handle.\n file.accessHandle = this.boundAccessHandles.get(path);\n } else if (this.unboundAccessHandles.size) {\n // Associate an unbound access handle to this file.\n file.accessHandle = this.unboundAccessHandles.values().next().value;\n file.accessHandle.truncate(0);\n this.unboundAccessHandles.delete(file.accessHandle);\n this.boundAccessHandles.set(path, file.accessHandle);\n }\n this.accessiblePaths.add(path);\n \n pOutFlags.setInt32(0, flags, true);\n return VFS.SQLITE_OK;\n } catch (e) {\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 const url = new URL(zName, 'file://');\n const path = url.pathname;\n if (this.persistentFiles.has(path)) {\n const persistentFile = this.persistentFiles.get(path);\n persistentFile.accessHandle.truncate(0);\n } else {\n this.boundAccessHandles.get(path)?.truncate(0);\n }\n this.accessiblePaths.delete(path);\n return VFS.SQLITE_OK;\n } catch (e) {\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 const url = new URL(zName, 'file://');\n const path = url.pathname;\n pResOut.setInt32(0, this.accessiblePaths.has(path) ? 1 : 0, true);\n return VFS.SQLITE_OK;\n } catch (e) {\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 this.mapIdToFile.delete(fileId);\n\n if (file?.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n if (file.persistentFile?.handleLockReleaser) {\n this.#releaseAccessHandle(file);\n }\n } else if (file?.flags & VFS.SQLITE_OPEN_DELETEONCLOSE) {\n file.accessHandle.truncate(0);\n this.accessiblePaths.delete(file.path);\n if (!this.persistentFiles.has(file.path)) {\n this.boundAccessHandles.delete(file.path);\n this.unboundAccessHandles.add(file.accessHandle);\n }\n }\n return VFS.SQLITE_OK;\n } catch (e) {\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 // 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 const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n const bytesRead = accessHandle.read(pData.subarray(), { at: iOffset });\n\n // Opening a database file performs one read without a xLock call.\n if ((file.flags & VFS.SQLITE_OPEN_MAIN_DB) && !file.persistentFile.isFileLocked) {\n this.#releaseAccessHandle(file);\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 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\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 const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n const nBytes = accessHandle.write(pData.subarray(), { at: iOffset });\n if (nBytes !== pData.byteLength) throw new Error('short write');\n return VFS.SQLITE_OK;\n } catch (e) {\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 const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n accessHandle.truncate(iSize);\n return VFS.SQLITE_OK;\n } catch (e) {\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 const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n accessHandle.flush();\n return VFS.SQLITE_OK;\n } catch (e) {\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 const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n const size = accessHandle.getSize();\n pSize64.setBigInt64(0, BigInt(size), true);\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_FSTAT;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} lockType \n * @returns {number}\n */\n jLock(fileId, lockType) {\n const file = this.mapIdToFile.get(fileId);\n if (file.persistentFile.isRequestInProgress) {\n file.persistentFile.isLockBusy = true;\n return VFS.SQLITE_BUSY;\n }\n\n file.persistentFile.isFileLocked = true;\n if (!file.persistentFile.handleLockReleaser) {\n // Start listening for notifications from other connections.\n // This is before we actually get access handles, but waiting to\n // listen until then allows a race condition where notifications\n // are missed. \n file.persistentFile.handleRequestChannel.onmessage = () => {\n this.log?.(`received notification for ${file.path}`);\n if (file.persistentFile.isFileLocked) {\n // We're still using the access handle, so mark it to be\n // released when we're done.\n file.persistentFile.isHandleRequested = true;\n } else {\n // Release the access handles immediately.\n this.#releaseAccessHandle(file);\n }\n file.persistentFile.handleRequestChannel.onmessage = null;\n };\n\n this.#requestAccessHandle(file);\n this.log?.('returning SQLITE_BUSY');\n file.persistentFile.isLockBusy = true;\n return VFS.SQLITE_BUSY;\n }\n file.persistentFile.isLockBusy = false;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} fileId \n * @param {number} lockType \n * @returns {number}\n */\n jUnlock(fileId, lockType) {\n const file = this.mapIdToFile.get(fileId);\n if (lockType === VFS.SQLITE_LOCK_NONE) {\n // Don't change any state if this unlock is because xLock returned\n // SQLITE_BUSY.\n if (!file.persistentFile.isLockBusy) {\n if (file.persistentFile.isHandleRequested) {\n // Another connection wants the access handle.\n this.#releaseAccessHandle(file);\n this.isHandleRequested = false;\n }\n file.persistentFile.isFileLocked = false;\n }\n }\n return VFS.SQLITE_OK;\n }\n \n /**\n * @param {number} fileId\n * @param {number} op\n * @param {DataView} pArg\n * @returns {number|Promise<number>}\n */\n jFileControl(fileId, op, pArg) {\n try {\n const file = this.mapIdToFile.get(fileId);\n switch (op) {\n case VFS.SQLITE_FCNTL_PRAGMA:\n const key = extractString(pArg, 4);\n const value = extractString(pArg, 8);\n this.log?.('xFileControl', file.path, 'PRAGMA', key, value);\n switch (key.toLowerCase()) {\n case 'journal_mode':\n if (value &&\n !['off', 'memory', 'delete', 'wal'].includes(value.toLowerCase())) {\n throw new Error('journal_mode must be \"off\", \"memory\", \"delete\", or \"wal\"');\n }\n break;\n }\n break;\n }\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR;\n }\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {Uint8Array} zBuf \n * @returns \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 {FileSystemFileHandle} fileHandle \n * @returns {Promise<PersistentFile>}\n */\n async #createPersistentFile(fileHandle) {\n const persistentFile = new PersistentFile(fileHandle);\n const root = await navigator.storage.getDirectory();\n const relativePath = await root.resolve(fileHandle);\n const path = `/${relativePath.join('/')}`;\n persistentFile.handleRequestChannel = new BroadcastChannel(`ahp:${path}`);\n this.persistentFiles.set(path, persistentFile);\n\n const f = await fileHandle.getFile();\n if (f.size) {\n this.accessiblePaths.add(path);\n }\n return persistentFile;\n }\n\n /**\n * @param {File} file \n */\n #requestAccessHandle(file) {\n console.assert(!file.persistentFile.handleLockReleaser);\n if (!file.persistentFile.isRequestInProgress) {\n file.persistentFile.isRequestInProgress = true;\n this._module.retryOps.push((async () => {\n // Acquire the Web Lock.\n file.persistentFile.handleLockReleaser = await this.#acquireLock(file.persistentFile);\n\n // Get access handles for the database and releated files in parallel.\n this.log?.(`creating access handles for ${file.path}`)\n await Promise.all(DB_RELATED_FILE_SUFFIXES.map(async suffix => {\n const persistentFile = this.persistentFiles.get(file.path + suffix);\n if (persistentFile) {\n persistentFile.accessHandle =\n await persistentFile.fileHandle.createSyncAccessHandle();\n }\n }));\n file.persistentFile.isRequestInProgress = false;\n })());\n return this._module.retryOps.at(-1);\n }\n return Promise.resolve();\n }\n\n /**\n * @param {File} file \n */\n async #releaseAccessHandle(file) {\n DB_RELATED_FILE_SUFFIXES.forEach(async suffix => {\n const persistentFile = this.persistentFiles.get(file.path + suffix);\n if (persistentFile) {\n persistentFile.accessHandle?.close();\n persistentFile.accessHandle = null;\n }\n });\n this.log?.(`access handles closed for ${file.path}`)\n\n file.persistentFile.handleLockReleaser?.();\n file.persistentFile.handleLockReleaser = null;\n this.log?.(`lock released for ${file.path}`)\n }\n\n /**\n * @param {PersistentFile} persistentFile \n * @returns {Promise<function>} lock releaser\n */\n #acquireLock(persistentFile) {\n return new Promise(resolve => {\n // Tell other connections we want the access handle.\n const lockName = persistentFile.handleRequestChannel.name;\n const notify = () => {\n this.log?.(`notifying for ${lockName}`);\n persistentFile.handleRequestChannel.postMessage(null);\n }\n const notifyId = setInterval(notify, LOCK_NOTIFY_INTERVAL);\n setTimeout(notify);\n\n this.log?.(`lock requested: ${lockName}`)\n navigator.locks.request(lockName, lock => {\n // We have the lock. Stop asking other connections for it.\n this.log?.(`lock acquired: ${lockName}`, lock);\n clearInterval(notifyId);\n return new Promise(resolve);\n });\n });\n }\n}\n\nfunction extractString(dataView, offset) {\n const p = dataView.getUint32(offset, true);\n if (p) {\n const chars = new Uint8Array(dataView.buffer, p);\n return new TextDecoder().decode(chars.subarray(0, chars.indexOf(0)));\n }\n return null;\n}"],"names":[],"sourceRoot":""}
|
package/lib/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/web",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"description": "A Web SDK for JourneyApps PowerSync",
|
|
5
5
|
"main": "lib/src/index.js",
|
|
6
6
|
"types": "lib/src/index.d.ts",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"build:webpack-workers": "webpack --config webpack.workers.config.js",
|
|
47
47
|
"build": "pnpm run build:tsc && pnpm run build:webpack-main && pnpm run build:webpack-workers",
|
|
48
48
|
"build:prod": "pnpm run build:tsc --sourceMap false && pnpm run build:webpack-main && pnpm run build:webpack-workers",
|
|
49
|
-
"clean": "rm -rf lib dist tsconfig.tsbuildinfo",
|
|
49
|
+
"clean": "rm -rf lib dist tsconfig.tsbuildinfo node_modules",
|
|
50
50
|
"watch": "tsc --build -w",
|
|
51
51
|
"test": "pnpm build && vitest"
|
|
52
52
|
},
|
|
@@ -60,19 +60,19 @@
|
|
|
60
60
|
"author": "JOURNEYAPPS",
|
|
61
61
|
"license": "Apache-2.0",
|
|
62
62
|
"peerDependencies": {
|
|
63
|
-
"@journeyapps/wa-sqlite": "^1.
|
|
64
|
-
"@powersync/common": "workspace:^1.22.
|
|
63
|
+
"@journeyapps/wa-sqlite": "^1.2.0",
|
|
64
|
+
"@powersync/common": "workspace:^1.22.2"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
67
|
"@powersync/common": "workspace:*",
|
|
68
68
|
"async-mutex": "^0.4.0",
|
|
69
69
|
"bson": "^6.6.0",
|
|
70
|
-
"comlink": "^4.4.
|
|
70
|
+
"comlink": "^4.4.2",
|
|
71
71
|
"commander": "^12.1.0",
|
|
72
72
|
"js-logger": "^1.6.1"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
|
-
"@journeyapps/wa-sqlite": "^1.
|
|
75
|
+
"@journeyapps/wa-sqlite": "^1.2.0",
|
|
76
76
|
"@types/uuid": "^9.0.6",
|
|
77
77
|
"@vitest/browser": "^2.1.4",
|
|
78
78
|
"crypto-browserify": "^3.12.0",
|
|
@@ -24,12 +24,20 @@ export interface WebSyncOptions {
|
|
|
24
24
|
type WithWebSyncOptions<Base> = Base & {
|
|
25
25
|
sync?: WebSyncOptions;
|
|
26
26
|
};
|
|
27
|
+
export interface WebEncryptionOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Encryption key for the database.
|
|
30
|
+
* If set, the database will be encrypted using Multiple Ciphers.
|
|
31
|
+
*/
|
|
32
|
+
encryptionKey?: string;
|
|
33
|
+
}
|
|
34
|
+
type WithWebEncryptionOptions<Base> = Base & WebEncryptionOptions;
|
|
27
35
|
export type WebPowerSyncDatabaseOptionsWithAdapter = WithWebSyncOptions<WithWebFlags<PowerSyncDatabaseOptionsWithDBAdapter>>;
|
|
28
36
|
export type WebPowerSyncDatabaseOptionsWithOpenFactory = WithWebSyncOptions<WithWebFlags<PowerSyncDatabaseOptionsWithOpenFactory>>;
|
|
29
|
-
export type WebPowerSyncDatabaseOptionsWithSettings = WithWebSyncOptions<WithWebFlags<PowerSyncDatabaseOptionsWithSettings
|
|
37
|
+
export type WebPowerSyncDatabaseOptionsWithSettings = WithWebSyncOptions<WithWebFlags<WithWebEncryptionOptions<PowerSyncDatabaseOptionsWithSettings>>>;
|
|
30
38
|
export type WebPowerSyncDatabaseOptions = WithWebSyncOptions<WithWebFlags<PowerSyncDatabaseOptions>>;
|
|
31
39
|
export declare const DEFAULT_POWERSYNC_FLAGS: Required<WebPowerSyncFlags>;
|
|
32
|
-
export declare const resolveWebPowerSyncFlags: (flags?: WebPowerSyncFlags) => WebPowerSyncFlags
|
|
40
|
+
export declare const resolveWebPowerSyncFlags: (flags?: WebPowerSyncFlags) => Required<WebPowerSyncFlags>;
|
|
33
41
|
/**
|
|
34
42
|
* A PowerSync database which provides SQLite functionality
|
|
35
43
|
* which is automatically synced.
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { AbstractPowerSyncDatabase, DEFAULT_POWERSYNC_CLOSE_OPTIONS, SqliteBucketStorage } from '@powersync/common';
|
|
1
|
+
import { AbstractPowerSyncDatabase, DEFAULT_POWERSYNC_CLOSE_OPTIONS, isDBAdapter, isSQLOpenFactory, SqliteBucketStorage } from '@powersync/common';
|
|
2
2
|
import { Mutex } from 'async-mutex';
|
|
3
|
+
import { getNavigatorLocks } from '../shared/navigator';
|
|
3
4
|
import { WASQLiteOpenFactory } from './adapters/wa-sqlite/WASQLiteOpenFactory';
|
|
4
5
|
import { DEFAULT_WEB_SQL_FLAGS, resolveWebSQLFlags } from './adapters/web-sql-flags';
|
|
5
6
|
import { SharedWebStreamingSyncImplementation } from './sync/SharedWebStreamingSyncImplementation';
|
|
6
7
|
import { SSRStreamingSyncImplementation } from './sync/SSRWebStreamingSyncImplementation';
|
|
7
8
|
import { WebRemote } from './sync/WebRemote';
|
|
8
9
|
import { WebStreamingSyncImplementation } from './sync/WebStreamingSyncImplementation';
|
|
9
|
-
import { getNavigatorLocks } from '../shared/navigator';
|
|
10
10
|
export const DEFAULT_POWERSYNC_FLAGS = {
|
|
11
11
|
...DEFAULT_WEB_SQL_FLAGS,
|
|
12
12
|
externallyUnload: false
|
|
@@ -18,6 +18,17 @@ export const resolveWebPowerSyncFlags = (flags) => {
|
|
|
18
18
|
...resolveWebSQLFlags(flags)
|
|
19
19
|
};
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Asserts that the database options are valid for custom database constructors.
|
|
23
|
+
*/
|
|
24
|
+
function assertValidDatabaseOptions(options) {
|
|
25
|
+
if ('database' in options && 'encryptionKey' in options) {
|
|
26
|
+
const { database } = options;
|
|
27
|
+
if (isSQLOpenFactory(database) || isDBAdapter(database)) {
|
|
28
|
+
throw new Error(`Invalid configuration: 'encryptionKey' should only be included inside the database object when using a custom ${isSQLOpenFactory(database) ? 'WASQLiteOpenFactory' : 'WASQLiteDBAdapter'} constructor.`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
21
32
|
/**
|
|
22
33
|
* A PowerSync database which provides SQLite functionality
|
|
23
34
|
* which is automatically synced.
|
|
@@ -40,6 +51,7 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
|
|
|
40
51
|
constructor(options) {
|
|
41
52
|
super(options);
|
|
42
53
|
this.options = options;
|
|
54
|
+
assertValidDatabaseOptions(options);
|
|
43
55
|
this.resolvedFlags = resolveWebPowerSyncFlags(options.flags);
|
|
44
56
|
if (this.resolvedFlags.enableMultiTabs && !this.resolvedFlags.externallyUnload) {
|
|
45
57
|
this.unloadListener = () => this.close({ disconnect: false });
|
|
@@ -50,7 +62,8 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
|
|
|
50
62
|
openDBAdapter(options) {
|
|
51
63
|
const defaultFactory = new WASQLiteOpenFactory({
|
|
52
64
|
...options.database,
|
|
53
|
-
flags: resolveWebPowerSyncFlags(options.flags)
|
|
65
|
+
flags: resolveWebPowerSyncFlags(options.flags),
|
|
66
|
+
encryptionKey: options.encryptionKey
|
|
54
67
|
});
|
|
55
68
|
return defaultFactory.openDB();
|
|
56
69
|
}
|
|
@@ -113,7 +126,10 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
|
|
|
113
126
|
const logger = this.options.logger;
|
|
114
127
|
logger ? logger.warn(warning) : console.warn(warning);
|
|
115
128
|
}
|
|
116
|
-
return new SharedWebStreamingSyncImplementation(
|
|
129
|
+
return new SharedWebStreamingSyncImplementation({
|
|
130
|
+
...syncOptions,
|
|
131
|
+
db: this.database // This should always be the case
|
|
132
|
+
});
|
|
117
133
|
default:
|
|
118
134
|
return new WebStreamingSyncImplementation(syncOptions);
|
|
119
135
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { DBAdapter, SQLOpenFactory } from '@powersync/common';
|
|
2
|
+
import { ILogger } from 'js-logger';
|
|
2
3
|
import { ResolvedWebSQLFlags, WebSQLOpenFactoryOptions } from './web-sql-flags';
|
|
3
4
|
export declare abstract class AbstractWebSQLOpenFactory implements SQLOpenFactory {
|
|
4
5
|
protected options: WebSQLOpenFactoryOptions;
|
|
5
6
|
protected resolvedFlags: ResolvedWebSQLFlags;
|
|
7
|
+
protected logger: ILogger;
|
|
6
8
|
constructor(options: WebSQLOpenFactoryOptions);
|
|
7
9
|
/**
|
|
8
10
|
* Opens a DBAdapter if not in SSR mode
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import Logger from 'js-logger';
|
|
1
2
|
import { SSRDBAdapter } from './SSRDBAdapter';
|
|
2
3
|
import { isServerSide, resolveWebSQLFlags } from './web-sql-flags';
|
|
3
4
|
export class AbstractWebSQLOpenFactory {
|
|
4
5
|
options;
|
|
5
6
|
resolvedFlags;
|
|
7
|
+
logger;
|
|
6
8
|
constructor(options) {
|
|
7
9
|
this.options = options;
|
|
8
10
|
this.resolvedFlags = resolveWebSQLFlags(options.flags);
|
|
11
|
+
this.logger = options.logger ?? Logger.get(`AbstractWebSQLOpenFactory - ${this.options.dbFilename}`);
|
|
9
12
|
}
|
|
10
13
|
/**
|
|
11
14
|
* Opens a {@link DBAdapter} using resolved flags.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BatchedUpdateNotification, QueryResult } from '@powersync/common';
|
|
2
|
+
import { ResolvedWebSQLOpenOptions } from './web-sql-flags';
|
|
3
|
+
/**
|
|
4
|
+
* Proxied query result does not contain a function for accessing row values
|
|
5
|
+
*/
|
|
6
|
+
export type ProxiedQueryResult = Omit<QueryResult, 'rows'> & {
|
|
7
|
+
rows: {
|
|
8
|
+
_array: any[];
|
|
9
|
+
length: number;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export type OnTableChangeCallback = (event: BatchedUpdateNotification) => void;
|
|
13
|
+
/**
|
|
14
|
+
* @internal
|
|
15
|
+
* An async Database connection which provides basic async SQL methods.
|
|
16
|
+
* This is usually a proxied through a web worker.
|
|
17
|
+
*/
|
|
18
|
+
export interface AsyncDatabaseConnection<Config extends ResolvedWebSQLOpenOptions = ResolvedWebSQLOpenOptions> {
|
|
19
|
+
init(): Promise<void>;
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
execute(sql: string, params?: any[]): Promise<ProxiedQueryResult>;
|
|
22
|
+
executeBatch(sql: string, params?: any[]): Promise<ProxiedQueryResult>;
|
|
23
|
+
registerOnTableChange(callback: OnTableChangeCallback): Promise<() => void>;
|
|
24
|
+
getConfig(): Promise<Config>;
|
|
25
|
+
}
|
|
26
|
+
export type OpenAsyncDatabaseConnection<Config extends ResolvedWebSQLOpenOptions = ResolvedWebSQLOpenOptions> = (config: Config) => AsyncDatabaseConnection;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { BaseObserver, DBAdapterListener, DBLockOptions, LockContext, QueryResult, Transaction } from '@powersync/common';
|
|
2
|
+
import { ILogger } from 'js-logger';
|
|
3
|
+
import { AsyncDatabaseConnection } from './AsyncDatabaseConnection';
|
|
4
|
+
import { SharedConnectionWorker, WebDBAdapter } from './WebDBAdapter';
|
|
5
|
+
import { ResolvedWebSQLOpenOptions } from './web-sql-flags';
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export interface LockedAsyncDatabaseAdapterOptions {
|
|
10
|
+
name: string;
|
|
11
|
+
openConnection: () => Promise<AsyncDatabaseConnection>;
|
|
12
|
+
debugMode?: boolean;
|
|
13
|
+
logger?: ILogger;
|
|
14
|
+
}
|
|
15
|
+
export type LockedAsyncDatabaseAdapterListener = DBAdapterListener & {
|
|
16
|
+
initialized?: () => void;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* @internal
|
|
20
|
+
* Wraps a {@link AsyncDatabaseConnection} and provides exclusive locking functions in
|
|
21
|
+
* order to implement {@link DBAdapter}.
|
|
22
|
+
*/
|
|
23
|
+
export declare class LockedAsyncDatabaseAdapter extends BaseObserver<LockedAsyncDatabaseAdapterListener> implements WebDBAdapter {
|
|
24
|
+
protected options: LockedAsyncDatabaseAdapterOptions;
|
|
25
|
+
private logger;
|
|
26
|
+
private dbGetHelpers;
|
|
27
|
+
private debugMode;
|
|
28
|
+
private _dbIdentifier;
|
|
29
|
+
protected initPromise: Promise<void>;
|
|
30
|
+
private _db;
|
|
31
|
+
protected _disposeTableChangeListener: (() => void) | null;
|
|
32
|
+
private _config;
|
|
33
|
+
constructor(options: LockedAsyncDatabaseAdapterOptions);
|
|
34
|
+
protected get baseDB(): AsyncDatabaseConnection<ResolvedWebSQLOpenOptions>;
|
|
35
|
+
get name(): string;
|
|
36
|
+
/**
|
|
37
|
+
* Init is automatic, this helps catch errors or explicitly await initialization
|
|
38
|
+
*/
|
|
39
|
+
init(): Promise<void>;
|
|
40
|
+
protected _init(): Promise<void>;
|
|
41
|
+
getConfiguration(): ResolvedWebSQLOpenOptions;
|
|
42
|
+
protected waitForInitialized(): Promise<void>;
|
|
43
|
+
shareConnection(): Promise<SharedConnectionWorker>;
|
|
44
|
+
/**
|
|
45
|
+
* Registers a table change notification callback with the base database.
|
|
46
|
+
* This can be extended by custom implementations in order to handle proxy events.
|
|
47
|
+
*/
|
|
48
|
+
protected registerOnChangeListener(db: AsyncDatabaseConnection): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* This is currently a no-op on web
|
|
51
|
+
*/
|
|
52
|
+
refreshSchema(): Promise<void>;
|
|
53
|
+
execute(query: string, params?: any[] | undefined): Promise<QueryResult>;
|
|
54
|
+
executeBatch(query: string, params?: any[][]): Promise<QueryResult>;
|
|
55
|
+
/**
|
|
56
|
+
* Attempts to close the connection.
|
|
57
|
+
* Shared workers might not actually close the connection if other
|
|
58
|
+
* tabs are still using it.
|
|
59
|
+
*/
|
|
60
|
+
close(): void;
|
|
61
|
+
getAll<T>(sql: string, parameters?: any[] | undefined): Promise<T[]>;
|
|
62
|
+
getOptional<T>(sql: string, parameters?: any[] | undefined): Promise<T | null>;
|
|
63
|
+
get<T>(sql: string, parameters?: any[] | undefined): Promise<T>;
|
|
64
|
+
readLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions | undefined): Promise<T>;
|
|
65
|
+
writeLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions | undefined): Promise<T>;
|
|
66
|
+
protected acquireLock(callback: () => Promise<any>): Promise<any>;
|
|
67
|
+
readTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions | undefined): Promise<T>;
|
|
68
|
+
writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions | undefined): Promise<T>;
|
|
69
|
+
private generateDBHelpers;
|
|
70
|
+
/**
|
|
71
|
+
* Wraps a lock context into a transaction context
|
|
72
|
+
*/
|
|
73
|
+
private wrapTransaction;
|
|
74
|
+
/**
|
|
75
|
+
* Wraps the worker execute function, awaiting for it to be available
|
|
76
|
+
*/
|
|
77
|
+
private _execute;
|
|
78
|
+
/**
|
|
79
|
+
* Wraps the worker executeBatch function, awaiting for it to be available
|
|
80
|
+
*/
|
|
81
|
+
private _executeBatch;
|
|
82
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { BaseObserver } from '@powersync/common';
|
|
2
|
+
import Logger from 'js-logger';
|
|
3
|
+
import { getNavigatorLocks } from '../..//shared/navigator';
|
|
4
|
+
import { WorkerWrappedAsyncDatabaseConnection } from './WorkerWrappedAsyncDatabaseConnection';
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
* Wraps a {@link AsyncDatabaseConnection} and provides exclusive locking functions in
|
|
8
|
+
* order to implement {@link DBAdapter}.
|
|
9
|
+
*/
|
|
10
|
+
export class LockedAsyncDatabaseAdapter extends BaseObserver {
|
|
11
|
+
options;
|
|
12
|
+
logger;
|
|
13
|
+
dbGetHelpers;
|
|
14
|
+
debugMode;
|
|
15
|
+
_dbIdentifier;
|
|
16
|
+
initPromise;
|
|
17
|
+
_db = null;
|
|
18
|
+
_disposeTableChangeListener = null;
|
|
19
|
+
_config = null;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
super();
|
|
22
|
+
this.options = options;
|
|
23
|
+
this._dbIdentifier = options.name;
|
|
24
|
+
this.logger = options.logger ?? Logger.get(`LockedAsyncDatabaseAdapter - ${this._dbIdentifier}`);
|
|
25
|
+
// Set the name if provided. We can query for the name if not available yet
|
|
26
|
+
this.debugMode = options.debugMode ?? false;
|
|
27
|
+
if (this.debugMode) {
|
|
28
|
+
const originalExecute = this._execute.bind(this);
|
|
29
|
+
this._execute = async (sql, bindings) => {
|
|
30
|
+
const start = performance.now();
|
|
31
|
+
try {
|
|
32
|
+
const r = await originalExecute(sql, bindings);
|
|
33
|
+
performance.measure(`[SQL] ${sql}`, { start });
|
|
34
|
+
return r;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
performance.measure(`[SQL] [ERROR: ${e.message}] ${sql}`, { start });
|
|
38
|
+
throw e;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
this.dbGetHelpers = this.generateDBHelpers({
|
|
43
|
+
execute: (query, params) => this.acquireLock(() => this._execute(query, params))
|
|
44
|
+
});
|
|
45
|
+
this.initPromise = this._init();
|
|
46
|
+
}
|
|
47
|
+
get baseDB() {
|
|
48
|
+
if (!this._db) {
|
|
49
|
+
throw new Error(`Initialization has not completed yet. Cannot access base db`);
|
|
50
|
+
}
|
|
51
|
+
return this._db;
|
|
52
|
+
}
|
|
53
|
+
get name() {
|
|
54
|
+
return this._dbIdentifier;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Init is automatic, this helps catch errors or explicitly await initialization
|
|
58
|
+
*/
|
|
59
|
+
async init() {
|
|
60
|
+
return this.initPromise;
|
|
61
|
+
}
|
|
62
|
+
async _init() {
|
|
63
|
+
this._db = await this.options.openConnection();
|
|
64
|
+
await this._db.init();
|
|
65
|
+
this._config = await this._db.getConfig();
|
|
66
|
+
await this.registerOnChangeListener(this._db);
|
|
67
|
+
this.iterateListeners((cb) => cb.initialized?.());
|
|
68
|
+
}
|
|
69
|
+
getConfiguration() {
|
|
70
|
+
if (!this._config) {
|
|
71
|
+
throw new Error(`Cannot get config before initialization is completed`);
|
|
72
|
+
}
|
|
73
|
+
return this._config;
|
|
74
|
+
}
|
|
75
|
+
async waitForInitialized() {
|
|
76
|
+
// Awaiting this will expose errors on function calls like .execute etc
|
|
77
|
+
await this.initPromise;
|
|
78
|
+
}
|
|
79
|
+
async shareConnection() {
|
|
80
|
+
if (false == this._db instanceof WorkerWrappedAsyncDatabaseConnection) {
|
|
81
|
+
throw new Error(`Only worker connections can be shared`);
|
|
82
|
+
}
|
|
83
|
+
return this._db.shareConnection();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Registers a table change notification callback with the base database.
|
|
87
|
+
* This can be extended by custom implementations in order to handle proxy events.
|
|
88
|
+
*/
|
|
89
|
+
async registerOnChangeListener(db) {
|
|
90
|
+
this._disposeTableChangeListener = await db.registerOnTableChange((event) => {
|
|
91
|
+
this.iterateListeners((cb) => cb.tablesUpdated?.(event));
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* This is currently a no-op on web
|
|
96
|
+
*/
|
|
97
|
+
async refreshSchema() { }
|
|
98
|
+
async execute(query, params) {
|
|
99
|
+
return this.writeLock((ctx) => ctx.execute(query, params));
|
|
100
|
+
}
|
|
101
|
+
async executeBatch(query, params) {
|
|
102
|
+
return this.writeLock((ctx) => this._executeBatch(query, params));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Attempts to close the connection.
|
|
106
|
+
* Shared workers might not actually close the connection if other
|
|
107
|
+
* tabs are still using it.
|
|
108
|
+
*/
|
|
109
|
+
close() {
|
|
110
|
+
this._disposeTableChangeListener?.();
|
|
111
|
+
this.baseDB?.close?.();
|
|
112
|
+
}
|
|
113
|
+
async getAll(sql, parameters) {
|
|
114
|
+
await this.waitForInitialized();
|
|
115
|
+
return this.dbGetHelpers.getAll(sql, parameters);
|
|
116
|
+
}
|
|
117
|
+
async getOptional(sql, parameters) {
|
|
118
|
+
await this.waitForInitialized();
|
|
119
|
+
return this.dbGetHelpers.getOptional(sql, parameters);
|
|
120
|
+
}
|
|
121
|
+
async get(sql, parameters) {
|
|
122
|
+
await this.waitForInitialized();
|
|
123
|
+
return this.dbGetHelpers.get(sql, parameters);
|
|
124
|
+
}
|
|
125
|
+
async readLock(fn, options) {
|
|
126
|
+
await this.waitForInitialized();
|
|
127
|
+
return this.acquireLock(async () => fn(this.generateDBHelpers({ execute: this._execute })));
|
|
128
|
+
}
|
|
129
|
+
async writeLock(fn, options) {
|
|
130
|
+
await this.waitForInitialized();
|
|
131
|
+
return this.acquireLock(async () => fn(this.generateDBHelpers({ execute: this._execute })));
|
|
132
|
+
}
|
|
133
|
+
acquireLock(callback) {
|
|
134
|
+
return getNavigatorLocks().request(`db-lock-${this._dbIdentifier}`, callback);
|
|
135
|
+
}
|
|
136
|
+
async readTransaction(fn, options) {
|
|
137
|
+
return this.readLock(this.wrapTransaction(fn));
|
|
138
|
+
}
|
|
139
|
+
writeTransaction(fn, options) {
|
|
140
|
+
return this.writeLock(this.wrapTransaction(fn));
|
|
141
|
+
}
|
|
142
|
+
generateDBHelpers(tx) {
|
|
143
|
+
return {
|
|
144
|
+
...tx,
|
|
145
|
+
/**
|
|
146
|
+
* Execute a read-only query and return results
|
|
147
|
+
*/
|
|
148
|
+
async getAll(sql, parameters) {
|
|
149
|
+
const res = await tx.execute(sql, parameters);
|
|
150
|
+
return res.rows?._array ?? [];
|
|
151
|
+
},
|
|
152
|
+
/**
|
|
153
|
+
* Execute a read-only query and return the first result, or null if the ResultSet is empty.
|
|
154
|
+
*/
|
|
155
|
+
async getOptional(sql, parameters) {
|
|
156
|
+
const res = await tx.execute(sql, parameters);
|
|
157
|
+
return res.rows?.item(0) ?? null;
|
|
158
|
+
},
|
|
159
|
+
/**
|
|
160
|
+
* Execute a read-only query and return the first result, error if the ResultSet is empty.
|
|
161
|
+
*/
|
|
162
|
+
async get(sql, parameters) {
|
|
163
|
+
const res = await tx.execute(sql, parameters);
|
|
164
|
+
const first = res.rows?.item(0);
|
|
165
|
+
if (!first) {
|
|
166
|
+
throw new Error('Result set is empty');
|
|
167
|
+
}
|
|
168
|
+
return first;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Wraps a lock context into a transaction context
|
|
174
|
+
*/
|
|
175
|
+
wrapTransaction(cb) {
|
|
176
|
+
return async (tx) => {
|
|
177
|
+
await this._execute('BEGIN TRANSACTION');
|
|
178
|
+
let finalized = false;
|
|
179
|
+
const commit = async () => {
|
|
180
|
+
if (finalized) {
|
|
181
|
+
return { rowsAffected: 0 };
|
|
182
|
+
}
|
|
183
|
+
finalized = true;
|
|
184
|
+
return this._execute('COMMIT');
|
|
185
|
+
};
|
|
186
|
+
const rollback = () => {
|
|
187
|
+
finalized = true;
|
|
188
|
+
return this._execute('ROLLBACK');
|
|
189
|
+
};
|
|
190
|
+
try {
|
|
191
|
+
const result = await cb({
|
|
192
|
+
...tx,
|
|
193
|
+
commit,
|
|
194
|
+
rollback
|
|
195
|
+
});
|
|
196
|
+
if (!finalized) {
|
|
197
|
+
await commit();
|
|
198
|
+
}
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
catch (ex) {
|
|
202
|
+
this.logger.debug('Caught ex in transaction', ex);
|
|
203
|
+
try {
|
|
204
|
+
await rollback();
|
|
205
|
+
}
|
|
206
|
+
catch (ex2) {
|
|
207
|
+
// In rare cases, a rollback may fail.
|
|
208
|
+
// Safe to ignore.
|
|
209
|
+
}
|
|
210
|
+
throw ex;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Wraps the worker execute function, awaiting for it to be available
|
|
216
|
+
*/
|
|
217
|
+
_execute = async (sql, bindings) => {
|
|
218
|
+
await this.waitForInitialized();
|
|
219
|
+
const result = await this.baseDB.execute(sql, bindings);
|
|
220
|
+
return {
|
|
221
|
+
...result,
|
|
222
|
+
rows: {
|
|
223
|
+
...result.rows,
|
|
224
|
+
item: (idx) => result.rows._array[idx]
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Wraps the worker executeBatch function, awaiting for it to be available
|
|
230
|
+
*/
|
|
231
|
+
_executeBatch = async (query, params) => {
|
|
232
|
+
await this.waitForInitialized();
|
|
233
|
+
const result = await this.baseDB.executeBatch(query, params);
|
|
234
|
+
return {
|
|
235
|
+
...result,
|
|
236
|
+
rows: undefined
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DBAdapter } from '@powersync/common';
|
|
2
|
+
import { ResolvedWebSQLOpenOptions } from './web-sql-flags';
|
|
3
|
+
export type SharedConnectionWorker = {
|
|
4
|
+
identifier: string;
|
|
5
|
+
port: MessagePort;
|
|
6
|
+
};
|
|
7
|
+
export interface WebDBAdapter extends DBAdapter {
|
|
8
|
+
/**
|
|
9
|
+
* Get a MessagePort which can be used to share the internals of this connection.
|
|
10
|
+
*/
|
|
11
|
+
shareConnection(): Promise<SharedConnectionWorker>;
|
|
12
|
+
/**
|
|
13
|
+
* Get the config options used to open this connection.
|
|
14
|
+
* This is useful for sharing connections.
|
|
15
|
+
*/
|
|
16
|
+
getConfiguration(): ResolvedWebSQLOpenOptions;
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|