@powersync/web 0.0.0-dev-20260708104358 → 0.0.0-dev-20260917074240
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 +1 -11
- package/bin/powersync.cjs +1 -1
- package/dist/index.react_native_web.js +2095 -0
- package/dist/index.react_native_web.js.map +1 -0
- package/dist/worker/AccessHandlePoolVFS-BPUHfZME.js +2 -0
- package/dist/worker/AccessHandlePoolVFS-BPUHfZME.js.map +1 -0
- package/dist/worker/FacadeVFS-d1ZDvud7.js +2 -0
- package/dist/worker/FacadeVFS-d1ZDvud7.js.map +1 -0
- package/dist/worker/IDBBatchAtomicVFS-DbkDb777.js +2 -0
- package/dist/worker/IDBBatchAtomicVFS-DbkDb777.js.map +1 -0
- package/dist/worker/MemoryVFS-DVJL5F8j.js +2 -0
- package/dist/worker/MemoryVFS-DVJL5F8j.js.map +1 -0
- package/dist/worker/OPFSCoopSyncVFS-BgTiWPfa.js +2 -0
- package/dist/worker/OPFSCoopSyncVFS-BgTiWPfa.js.map +1 -0
- package/dist/worker/OPFSWriteAheadVFS-Rt5CGCZP.js +2 -0
- package/dist/worker/OPFSWriteAheadVFS-Rt5CGCZP.js.map +1 -0
- package/dist/worker/assets/mc-wa-sqlite-BHScYYZw.wasm +0 -0
- package/dist/worker/assets/mc-wa-sqlite-async-DdlQmAUV.wasm +0 -0
- package/dist/worker/assets/wa-sqlite-CIxTmRHQ.wasm +0 -0
- package/dist/worker/assets/wa-sqlite-async-zc68fxQq.wasm +0 -0
- package/dist/worker/mc-wa-sqlite-PubB6u8c.js +2 -0
- package/dist/worker/mc-wa-sqlite-PubB6u8c.js.map +1 -0
- package/dist/worker/mc-wa-sqlite-async-DJG_ANKO.js +2 -0
- package/dist/worker/mc-wa-sqlite-async-DJG_ANKO.js.map +1 -0
- package/dist/worker/wa-sqlite-D1_4mGs8.js +2 -0
- package/dist/worker/wa-sqlite-D1_4mGs8.js.map +1 -0
- package/dist/worker/wa-sqlite-async-BxkwNY-2.js +2 -0
- package/dist/worker/wa-sqlite-async-BxkwNY-2.js.map +1 -0
- package/dist/worker/websockets-Q8W_lerF.js +9 -0
- package/dist/worker/websockets-Q8W_lerF.js.map +1 -0
- package/dist/worker/worker.js +7 -0
- package/dist/worker/worker.js.map +1 -0
- package/lib/attachments/IndexDBFileSystemAdapter.js.map +1 -0
- package/lib/{src/db → db}/NavigatorTriggerClaimManager.d.ts +1 -1
- package/lib/db/NavigatorTriggerClaimManager.js.map +1 -0
- package/lib/db/PowerSyncDatabase.d.ts +66 -0
- package/lib/{src/db → db}/PowerSyncDatabase.js +60 -79
- package/lib/db/PowerSyncDatabase.js.map +1 -0
- package/lib/db/adapters/AsyncWebAdapter.d.ts +32 -0
- package/lib/{src/db → db}/adapters/AsyncWebAdapter.js +18 -69
- package/lib/db/adapters/AsyncWebAdapter.js.map +1 -0
- package/lib/db/adapters/SSRDBAdapter.d.ts +17 -0
- package/lib/db/adapters/SSRDBAdapter.js +37 -0
- package/lib/db/adapters/SSRDBAdapter.js.map +1 -0
- package/lib/{src/db → db}/adapters/WebDBAdapter.d.ts +1 -1
- package/lib/db/adapters/WebDBAdapter.js.map +1 -0
- package/lib/db/adapters/acquireFromPool.d.ts +7 -0
- package/lib/db/adapters/acquireFromPool.js +60 -0
- package/lib/db/adapters/acquireFromPool.js.map +1 -0
- package/lib/db/adapters/memory-pool/client.d.ts +70 -0
- package/lib/db/adapters/memory-pool/client.js +269 -0
- package/lib/db/adapters/memory-pool/client.js.map +1 -0
- package/lib/db/adapters/memory-pool/shared.d.ts +51 -0
- package/lib/db/adapters/memory-pool/shared.js +16 -0
- package/lib/db/adapters/memory-pool/shared.js.map +1 -0
- package/lib/db/adapters/memory-pool/vfs.d.ts +19 -0
- package/lib/db/adapters/memory-pool/vfs.js +247 -0
- package/lib/db/adapters/memory-pool/vfs.js.map +1 -0
- package/lib/db/adapters/memory-pool/worker.js +45 -0
- package/lib/db/adapters/memory-pool/worker.js.map +1 -0
- package/lib/db/adapters/options.d.ts +94 -0
- package/lib/db/adapters/options.js +6 -0
- package/lib/db/adapters/options.js.map +1 -0
- package/lib/db/adapters/resolveAndValidateOptions.d.ts +13 -0
- package/lib/db/adapters/resolveAndValidateOptions.js +41 -0
- package/lib/db/adapters/resolveAndValidateOptions.js.map +1 -0
- package/lib/{src/db → db}/adapters/wa-sqlite/ConcurrentConnection.d.ts +3 -4
- package/lib/{src/db → db}/adapters/wa-sqlite/ConcurrentConnection.js +2 -2
- package/lib/db/adapters/wa-sqlite/ConcurrentConnection.js.map +1 -0
- package/lib/{src/db → db}/adapters/wa-sqlite/DatabaseClient.d.ts +4 -4
- package/lib/{src/db → db}/adapters/wa-sqlite/DatabaseClient.js +11 -37
- package/lib/db/adapters/wa-sqlite/DatabaseClient.js.map +1 -0
- package/lib/{src/db → db}/adapters/wa-sqlite/DatabaseServer.d.ts +5 -5
- package/lib/{src/db → db}/adapters/wa-sqlite/DatabaseServer.js +14 -7
- package/lib/db/adapters/wa-sqlite/DatabaseServer.js.map +1 -0
- package/lib/{src/db → db}/adapters/wa-sqlite/RawSqliteConnection.d.ts +30 -12
- package/lib/{src/db → db}/adapters/wa-sqlite/RawSqliteConnection.js +70 -16
- package/lib/db/adapters/wa-sqlite/RawSqliteConnection.js.map +1 -0
- package/lib/db/adapters/wa-sqlite/StatementCache.d.ts +16 -0
- package/lib/db/adapters/wa-sqlite/StatementCache.js +44 -0
- package/lib/db/adapters/wa-sqlite/StatementCache.js.map +1 -0
- package/lib/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +18 -0
- package/lib/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +140 -0
- package/lib/db/adapters/wa-sqlite/WASQLiteOpenFactory.js.map +1 -0
- package/lib/{src/db → db}/adapters/wa-sqlite/vfs.d.ts +9 -2
- package/lib/{src/db → db}/adapters/wa-sqlite/vfs.js +6 -6
- package/lib/db/adapters/wa-sqlite/vfs.js.map +1 -0
- package/lib/{src/db → db}/sync/SSRWebStreamingSyncImplementation.d.ts +5 -16
- package/lib/{src/db → db}/sync/SSRWebStreamingSyncImplementation.js +7 -23
- package/lib/db/sync/SSRWebStreamingSyncImplementation.js.map +1 -0
- package/lib/{src/db → db}/sync/SharedWebStreamingSyncImplementation.d.ts +15 -15
- package/lib/{src/db → db}/sync/SharedWebStreamingSyncImplementation.js +38 -70
- package/lib/db/sync/SharedWebStreamingSyncImplementation.js.map +1 -0
- package/lib/db/sync/TabLocalStreamingSyncImplementation.d.ts +19 -0
- package/lib/db/sync/TabLocalStreamingSyncImplementation.js +70 -0
- package/lib/db/sync/TabLocalStreamingSyncImplementation.js.map +1 -0
- package/lib/db/sync/WebRemote.d.ts +10 -0
- package/lib/db/sync/WebRemote.js +33 -0
- package/lib/db/sync/WebRemote.js.map +1 -0
- package/lib/{src/db → db}/sync/WebStreamingSyncImplementation.d.ts +2 -4
- package/lib/{src/db → db}/sync/WebStreamingSyncImplementation.js +3 -2
- package/lib/db/sync/WebStreamingSyncImplementation.js.map +1 -0
- package/lib/db/sync/userAgent.js.map +1 -0
- package/lib/{src/index.d.ts → index.d.ts} +1 -3
- package/lib/{src/index.js → index.js} +1 -3
- package/lib/index.js.map +1 -0
- package/lib/shared/navigator.js.map +1 -0
- package/lib/shared/tab_close_signal.js.map +1 -0
- package/lib/worker/SharedWorkerConnectionRequest.d.ts +5 -0
- package/lib/worker/SharedWorkerConnectionRequest.js +2 -0
- package/lib/worker/SharedWorkerConnectionRequest.js.map +1 -0
- package/lib/worker/client.d.ts +17 -0
- package/lib/worker/client.js +90 -0
- package/lib/worker/client.js.map +1 -0
- package/lib/worker/db/MultiDatabaseServer.d.ts +21 -0
- package/lib/{src/worker → worker}/db/MultiDatabaseServer.js +45 -24
- package/lib/worker/db/MultiDatabaseServer.js.map +1 -0
- package/lib/worker/sync/AbstractSharedSyncClientProvider.d.ts +14 -0
- package/lib/worker/sync/AbstractSharedSyncClientProvider.js.map +1 -0
- package/lib/worker/sync/BroadcastLogger.d.ts +27 -0
- package/lib/worker/sync/BroadcastLogger.js +65 -0
- package/lib/worker/sync/BroadcastLogger.js.map +1 -0
- package/lib/{src/worker → worker}/sync/SharedSyncImplementation.d.ts +14 -25
- package/lib/{src/worker → worker}/sync/SharedSyncImplementation.js +140 -107
- package/lib/worker/sync/SharedSyncImplementation.js.map +1 -0
- package/lib/{src/worker → worker}/sync/WorkerClient.d.ts +4 -4
- package/lib/{src/worker → worker}/sync/WorkerClient.js +5 -5
- package/lib/worker/sync/WorkerClient.js.map +1 -0
- package/lib/worker/worker.d.ts +1 -0
- package/lib/worker/worker.js +34 -0
- package/lib/worker/worker.js.map +1 -0
- package/package.json +31 -32
- package/src/db/NavigatorTriggerClaimManager.ts +1 -1
- package/src/db/PowerSyncDatabase.ts +96 -144
- package/src/db/adapters/AsyncWebAdapter.ts +29 -86
- package/src/db/adapters/SSRDBAdapter.ts +14 -64
- package/src/db/adapters/WebDBAdapter.ts +1 -1
- package/src/db/adapters/acquireFromPool.ts +73 -0
- package/src/db/adapters/memory-pool/client.ts +367 -0
- package/src/db/adapters/memory-pool/shared.ts +79 -0
- package/src/db/adapters/memory-pool/vfs.ts +293 -0
- package/src/db/adapters/memory-pool/worker.ts +53 -0
- package/src/db/adapters/options.ts +108 -0
- package/src/db/adapters/resolveAndValidateOptions.ts +49 -0
- package/src/db/adapters/wa-sqlite/ConcurrentConnection.ts +4 -5
- package/src/db/adapters/wa-sqlite/DatabaseClient.ts +20 -56
- package/src/db/adapters/wa-sqlite/DatabaseServer.ts +18 -12
- package/src/db/adapters/wa-sqlite/RawSqliteConnection.ts +108 -30
- package/src/db/adapters/wa-sqlite/StatementCache.ts +50 -0
- package/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +82 -125
- package/src/db/adapters/wa-sqlite/vfs.ts +13 -8
- package/src/db/sync/SSRWebStreamingSyncImplementation.ts +8 -36
- package/src/db/sync/SharedWebStreamingSyncImplementation.ts +47 -82
- package/src/db/sync/TabLocalStreamingSyncImplementation.ts +90 -0
- package/src/db/sync/WebRemote.ts +23 -26
- package/src/db/sync/WebStreamingSyncImplementation.ts +4 -5
- package/src/index.ts +1 -3
- package/src/worker/SharedWorkerConnectionRequest.ts +6 -0
- package/src/worker/client.ts +121 -0
- package/src/worker/db/MultiDatabaseServer.ts +59 -33
- package/src/worker/sync/AbstractSharedSyncClientProvider.ts +5 -10
- package/src/worker/sync/BroadcastLogger.ts +35 -103
- package/src/worker/sync/SharedSyncImplementation.ts +197 -138
- package/src/worker/sync/WorkerClient.ts +8 -8
- package/src/worker/worker.ts +38 -0
- package/dist/2075a31bb151adbb9767.wasm +0 -0
- package/dist/3322bc84de986b63c2cd.wasm +0 -0
- package/dist/8e97452e297be23b5e50.wasm +0 -0
- package/dist/fbc178b70d530e8ce02b.wasm +0 -0
- package/dist/index.umd.js +0 -7821
- package/dist/index.umd.js.map +0 -1
- package/dist/worker/SharedSyncImplementation.umd.js +0 -16139
- package/dist/worker/SharedSyncImplementation.umd.js.map +0 -1
- package/dist/worker/WASQLiteDB.umd.js +0 -17434
- package/dist/worker/WASQLiteDB.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-9af0a7.umd.js +0 -31
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-9af0a7.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-bbf5a9.umd.js +0 -31
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-bbf5a9.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-c26e0f.umd.js +0 -31
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-c26e0f.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +0 -31
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-2fb422.umd.js +0 -3562
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-2fb422.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-833585.umd.js +0 -1400
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-833585.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-96fb23.umd.js +0 -2478
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-96fb23.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-c89911.umd.js +0 -1681
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-c89911.umd.js.map +0 -1
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-ec4eb1.umd.js +0 -1820
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-ec4eb1.umd.js.map +0 -1
- package/lib/package.json +0 -91
- package/lib/src/attachments/IndexDBFileSystemAdapter.js.map +0 -1
- package/lib/src/db/NavigatorTriggerClaimManager.js.map +0 -1
- package/lib/src/db/PowerSyncDatabase.d.ts +0 -78
- package/lib/src/db/PowerSyncDatabase.js.map +0 -1
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.d.ts +0 -23
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js +0 -27
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js.map +0 -1
- package/lib/src/db/adapters/AsyncWebAdapter.d.ts +0 -50
- package/lib/src/db/adapters/AsyncWebAdapter.js.map +0 -1
- package/lib/src/db/adapters/SSRDBAdapter.d.ts +0 -30
- package/lib/src/db/adapters/SSRDBAdapter.js +0 -69
- package/lib/src/db/adapters/SSRDBAdapter.js.map +0 -1
- package/lib/src/db/adapters/WebDBAdapter.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/ConcurrentConnection.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/DatabaseClient.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/DatabaseServer.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/RawSqliteConnection.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +0 -45
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +0 -146
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.d.ts +0 -15
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js +0 -22
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/vfs.js.map +0 -1
- package/lib/src/db/adapters/web-sql-flags.d.ts +0 -92
- package/lib/src/db/adapters/web-sql-flags.js +0 -37
- package/lib/src/db/adapters/web-sql-flags.js.map +0 -1
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js.map +0 -1
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js.map +0 -1
- package/lib/src/db/sync/WebRemote.d.ts +0 -7
- package/lib/src/db/sync/WebRemote.js +0 -33
- package/lib/src/db/sync/WebRemote.js.map +0 -1
- package/lib/src/db/sync/WebStreamingSyncImplementation.js.map +0 -1
- package/lib/src/db/sync/userAgent.js.map +0 -1
- package/lib/src/index.js.map +0 -1
- package/lib/src/shared/navigator.js.map +0 -1
- package/lib/src/shared/tab_close_signal.js.map +0 -1
- package/lib/src/worker/db/MultiDatabaseServer.d.ts +0 -17
- package/lib/src/worker/db/MultiDatabaseServer.js.map +0 -1
- package/lib/src/worker/db/WASQLiteDB.worker.d.ts +0 -4
- package/lib/src/worker/db/WASQLiteDB.worker.js +0 -31
- package/lib/src/worker/db/WASQLiteDB.worker.js.map +0 -1
- package/lib/src/worker/db/open-worker-database.d.ts +0 -14
- package/lib/src/worker/db/open-worker-database.js +0 -53
- package/lib/src/worker/db/open-worker-database.js.map +0 -1
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.d.ts +0 -19
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.js.map +0 -1
- package/lib/src/worker/sync/BroadcastLogger.d.ts +0 -47
- package/lib/src/worker/sync/BroadcastLogger.js +0 -129
- package/lib/src/worker/sync/BroadcastLogger.js.map +0 -1
- package/lib/src/worker/sync/SharedSyncImplementation.js.map +0 -1
- package/lib/src/worker/sync/SharedSyncImplementation.worker.js +0 -12
- package/lib/src/worker/sync/SharedSyncImplementation.worker.js.map +0 -1
- package/lib/src/worker/sync/WorkerClient.js.map +0 -1
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.ts +0 -47
- package/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.ts +0 -24
- package/src/db/adapters/web-sql-flags.ts +0 -141
- package/src/worker/db/WASQLiteDB.worker.ts +0 -35
- package/src/worker/db/open-worker-database.ts +0 -62
- package/src/worker/sync/SharedSyncImplementation.worker.ts +0 -14
- /package/lib/{src/attachments → attachments}/IndexDBFileSystemAdapter.d.ts +0 -0
- /package/lib/{src/attachments → attachments}/IndexDBFileSystemAdapter.js +0 -0
- /package/lib/{src/db → db}/NavigatorTriggerClaimManager.js +0 -0
- /package/lib/{src/db → db}/adapters/WebDBAdapter.js +0 -0
- /package/lib/{src/worker/sync/SharedSyncImplementation.worker.d.ts → db/adapters/memory-pool/worker.d.ts} +0 -0
- /package/lib/{src/db → db}/sync/userAgent.d.ts +0 -0
- /package/lib/{src/db → db}/sync/userAgent.js +0 -0
- /package/lib/{src/shared → shared}/navigator.d.ts +0 -0
- /package/lib/{src/shared → shared}/navigator.js +0 -0
- /package/lib/{src/shared → shared}/tab_close_signal.d.ts +0 -0
- /package/lib/{src/shared → shared}/tab_close_signal.js +0 -0
- /package/lib/{src/worker → worker}/sync/AbstractSharedSyncClientProvider.js +0 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import { FacadeVFS } from '@journeyapps/wa-sqlite/src/FacadeVFS.js';
|
|
3
|
+
import * as VFS from '@journeyapps/wa-sqlite/src/VFS.js';
|
|
4
|
+
import { emptyWalState, WalIndexChange, WalOverlayEntry, WriteAheadBuffers, WriteAheadState } from './shared.js';
|
|
5
|
+
|
|
6
|
+
const mainDbSentinel = Symbol();
|
|
7
|
+
|
|
8
|
+
export class InMemoryWriteAheadLog extends FacadeVFS {
|
|
9
|
+
// The main /database file is the only file for which we create a write-ahead log. All other files (including the
|
|
10
|
+
// journal) are local-only.
|
|
11
|
+
#openedFiles = new Map<number, LocalFile | typeof mainDbSentinel>();
|
|
12
|
+
#files = new Map<string, LocalFile | typeof mainDbSentinel>();
|
|
13
|
+
|
|
14
|
+
#tx: WriteAheadTransaction | undefined = undefined;
|
|
15
|
+
#newOverlayPagesToSendToMainTab = new Map<number, WalOverlayEntry>();
|
|
16
|
+
|
|
17
|
+
writeAheadState: WriteAheadState = emptyWalState();
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
module: any,
|
|
21
|
+
readonly buffers: WriteAheadBuffers
|
|
22
|
+
) {
|
|
23
|
+
super('InMemoryWriteAheadLog', module);
|
|
24
|
+
this.#files.set('/database', mainDbSentinel);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
takeChanges(): WalIndexChange {
|
|
28
|
+
const added: (number | WalOverlayEntry)[] = [];
|
|
29
|
+
this.#newOverlayPagesToSendToMainTab.forEach((v, k) => {
|
|
30
|
+
added.push(k);
|
|
31
|
+
added.push(v);
|
|
32
|
+
});
|
|
33
|
+
this.#newOverlayPagesToSendToMainTab.clear();
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
cleared: false,
|
|
37
|
+
walEnd: this.writeAheadState.walEnd,
|
|
38
|
+
fileSize: this.writeAheadState.fileSize,
|
|
39
|
+
added
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
jAccess(zName: string, _pFlags: number, pResOut: DataView) {
|
|
44
|
+
const file = this.#files.get(zName);
|
|
45
|
+
pResOut.setInt32(0, file != null ? 1 : 0, true);
|
|
46
|
+
return VFS.SQLITE_OK;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
jOpen(zName: string | null, fileId: number, flags: number, pOutFlags: DataView): number {
|
|
50
|
+
if (flags & VFS.SQLITE_OPEN_MAIN_DB) {
|
|
51
|
+
this.#openedFiles.set(fileId, mainDbSentinel);
|
|
52
|
+
} else {
|
|
53
|
+
// Treat all other files, including the mostly unused journal as temporary.
|
|
54
|
+
zName ??= `tmp-${crypto.randomUUID()}`;
|
|
55
|
+
|
|
56
|
+
const existing = this.#files.get(zName);
|
|
57
|
+
if (existing != null) {
|
|
58
|
+
this.#openedFiles.set(fileId, existing);
|
|
59
|
+
} else {
|
|
60
|
+
if (!(flags & VFS.SQLITE_OPEN_CREATE)) {
|
|
61
|
+
return VFS.SQLITE_IOERR;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const newFile = new LocalFile(zName);
|
|
65
|
+
newFile.deleteOnClose = (flags & VFS.SQLITE_OPEN_DELETEONCLOSE) != 0;
|
|
66
|
+
this.#openedFiles.set(fileId, newFile);
|
|
67
|
+
this.#files.set(zName, newFile);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
pOutFlags.setInt32(0, flags, true);
|
|
72
|
+
return VFS.SQLITE_OK;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
jClose(fileid: number) {
|
|
76
|
+
const file = this.#openedFiles.get(fileid)!;
|
|
77
|
+
this.#openedFiles.delete(fileid);
|
|
78
|
+
|
|
79
|
+
if (file != mainDbSentinel && file.deleteOnClose) {
|
|
80
|
+
this.#files.delete(file.name);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return VFS.SQLITE_OK;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
jRead(fileId: number, data: Uint8Array, offset: number) {
|
|
87
|
+
const file = this.#openedFiles.get(fileId)!;
|
|
88
|
+
let endOffset: number;
|
|
89
|
+
let readableBytes: number;
|
|
90
|
+
|
|
91
|
+
if (file == mainDbSentinel) {
|
|
92
|
+
const { fileSize, overlay } = this.writeAheadState;
|
|
93
|
+
const startOffset = Math.min(offset, fileSize);
|
|
94
|
+
endOffset = Math.min(fileSize, offset + data.byteLength);
|
|
95
|
+
readableBytes = endOffset - startOffset;
|
|
96
|
+
|
|
97
|
+
// Try reading from the write-ahead overlay first. A read on the database file is always a complete page, except
|
|
98
|
+
// when reading from the 100-byte header.
|
|
99
|
+
const page = overlay.get(offset < 100 ? 0 : offset);
|
|
100
|
+
|
|
101
|
+
if (page != null) {
|
|
102
|
+
const pageOffset = page.logOffset;
|
|
103
|
+
const source = new Uint8Array(
|
|
104
|
+
this.buffers.writeAheadLog,
|
|
105
|
+
offset < 100 ? pageOffset + offset : pageOffset,
|
|
106
|
+
readableBytes
|
|
107
|
+
);
|
|
108
|
+
data.set(source);
|
|
109
|
+
} else {
|
|
110
|
+
// Page is not in WAL overlay, read directly from underlying in-memory buffer.
|
|
111
|
+
data.set(new Uint8Array(this.buffers.database, startOffset, readableBytes));
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
const fileSize = file.size;
|
|
115
|
+
const startOffset = Math.min(offset, fileSize);
|
|
116
|
+
endOffset = Math.min(fileSize, offset + data.byteLength);
|
|
117
|
+
readableBytes = endOffset - startOffset;
|
|
118
|
+
|
|
119
|
+
if (readableBytes > 0) {
|
|
120
|
+
data.set(new Uint8Array(file.buffer, startOffset, readableBytes));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (readableBytes < data.byteLength) {
|
|
125
|
+
data.fill(0, readableBytes); // Fill rest with zeroes.
|
|
126
|
+
return VFS.SQLITE_IOERR_SHORT_READ;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return VFS.SQLITE_OK;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
jWrite(fileId: number, data: Uint8Array, offset: number): number {
|
|
133
|
+
const file = this.#openedFiles.get(fileId)!;
|
|
134
|
+
// wa-sqlite passes a proxy instead of a real Uint8Array.
|
|
135
|
+
const actualBuffer = data.subarray();
|
|
136
|
+
|
|
137
|
+
if (file === mainDbSentinel) {
|
|
138
|
+
// Write to the write-ahead overlay.
|
|
139
|
+
let didWrite = false;
|
|
140
|
+
if (this.#tx != null) {
|
|
141
|
+
didWrite = this.#tx.appendPage(offset, actualBuffer);
|
|
142
|
+
} else {
|
|
143
|
+
const tx = new WriteAheadTransaction(this.writeAheadState.walEnd, this.writeAheadState.fileSize, this.buffers);
|
|
144
|
+
didWrite = tx.appendPage(offset, actualBuffer);
|
|
145
|
+
this.#commit(tx);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return didWrite ? VFS.SQLITE_OK : VFS.SQLITE_FULL;
|
|
149
|
+
} else {
|
|
150
|
+
const writeLength = data.length;
|
|
151
|
+
const endOffset = offset + writeLength;
|
|
152
|
+
if (endOffset > file.buffer.byteLength) {
|
|
153
|
+
const didGrow = file.growBuffer(endOffset);
|
|
154
|
+
if (!didGrow) {
|
|
155
|
+
return VFS.SQLITE_FULL;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
new Uint8Array(file.buffer, offset, writeLength).set(actualBuffer);
|
|
160
|
+
file.size = Math.max(file.size, endOffset);
|
|
161
|
+
return VFS.SQLITE_OK;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
jTruncate(fileid: number, size: number): number {
|
|
166
|
+
const file = this.#openedFiles.get(fileid)!;
|
|
167
|
+
|
|
168
|
+
if (file === mainDbSentinel) {
|
|
169
|
+
this.writeAheadState.fileSize = Math.min(this.writeAheadState.fileSize, size);
|
|
170
|
+
} else {
|
|
171
|
+
file.size = Math.min(size, file.size);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return VFS.SQLITE_OK;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
jFileSize(fileid: number, pSize64: DataView) {
|
|
178
|
+
const file = this.#openedFiles.get(fileid)!;
|
|
179
|
+
|
|
180
|
+
if (file === mainDbSentinel) {
|
|
181
|
+
pSize64.setBigInt64(0, BigInt(this.writeAheadState.fileSize), true);
|
|
182
|
+
} else {
|
|
183
|
+
pSize64.setBigInt64(0, BigInt(file.size), true);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return VFS.SQLITE_OK;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
jDelete(name: string) {
|
|
190
|
+
if (name === '/database') {
|
|
191
|
+
return VFS.SQLITE_IOERR_DELETE;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
this.#files.delete(name);
|
|
195
|
+
return VFS.SQLITE_OK;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
jDeviceCharacteristics(): number {
|
|
199
|
+
return VFS.SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | VFS.SQLITE_IOCAP_BATCH_ATOMIC;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
jFileControl(fileid: number, op: number) {
|
|
203
|
+
const file = this.#openedFiles.get(fileid)!;
|
|
204
|
+
if (file !== mainDbSentinel) return VFS.SQLITE_NOTFOUND;
|
|
205
|
+
|
|
206
|
+
switch (op) {
|
|
207
|
+
case VFS.SQLITE_FCNTL_BEGIN_ATOMIC_WRITE:
|
|
208
|
+
this.#tx = new WriteAheadTransaction(this.writeAheadState.walEnd, this.writeAheadState.fileSize, this.buffers);
|
|
209
|
+
break;
|
|
210
|
+
case VFS.SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE:
|
|
211
|
+
this.#tx = undefined;
|
|
212
|
+
break;
|
|
213
|
+
case VFS.SQLITE_FCNTL_COMMIT_ATOMIC_WRITE:
|
|
214
|
+
this.#commit(this.#tx!);
|
|
215
|
+
this.#tx = undefined;
|
|
216
|
+
break;
|
|
217
|
+
default:
|
|
218
|
+
return VFS.SQLITE_NOTFOUND;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return VFS.SQLITE_OK;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
#commit(tx: WriteAheadTransaction) {
|
|
225
|
+
this.writeAheadState.walEnd = tx.walEndOffset;
|
|
226
|
+
this.writeAheadState.fileSize = tx.fileSize;
|
|
227
|
+
tx.changedPages.forEach((walEntry, databaseOffset) => {
|
|
228
|
+
this.#newOverlayPagesToSendToMainTab.set(databaseOffset, walEntry);
|
|
229
|
+
this.writeAheadState.overlay.set(databaseOffset, walEntry);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
class WriteAheadTransaction {
|
|
235
|
+
walEndOffset: number;
|
|
236
|
+
fileSize: number;
|
|
237
|
+
changedPages = new Map<number, WalOverlayEntry>();
|
|
238
|
+
|
|
239
|
+
constructor(
|
|
240
|
+
endOffset: number,
|
|
241
|
+
fileSize: number,
|
|
242
|
+
readonly buffers: WriteAheadBuffers
|
|
243
|
+
) {
|
|
244
|
+
this.walEndOffset = endOffset;
|
|
245
|
+
this.fileSize = fileSize;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
appendPage(offset: number, data: Uint8Array): boolean {
|
|
249
|
+
const currentEnd = this.walEndOffset;
|
|
250
|
+
const newEnd = currentEnd + data.length;
|
|
251
|
+
|
|
252
|
+
const wal = this.buffers.writeAheadLog;
|
|
253
|
+
if (newEnd > wal.byteLength) {
|
|
254
|
+
if (!this.#growWal(wal, newEnd)) return false;
|
|
255
|
+
}
|
|
256
|
+
this.fileSize = Math.max(this.fileSize, offset + data.length);
|
|
257
|
+
new Uint8Array(wal, currentEnd, data.length).set(data);
|
|
258
|
+
|
|
259
|
+
this.walEndOffset = newEnd;
|
|
260
|
+
this.changedPages.set(offset, { logOffset: currentEnd, size: data.length });
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#growWal(buffer: SharedArrayBuffer, minSize: number) {
|
|
265
|
+
const newSize = Math.max(minSize, Math.min(2 * buffer.byteLength, buffer.maxByteLength));
|
|
266
|
+
if (newSize > buffer.maxByteLength) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
buffer.grow(newSize);
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
class LocalFile {
|
|
276
|
+
deleteOnClose = false;
|
|
277
|
+
name: string;
|
|
278
|
+
buffer = new ArrayBuffer(1024);
|
|
279
|
+
size: number = 0;
|
|
280
|
+
|
|
281
|
+
constructor(name: string) {
|
|
282
|
+
this.name = name;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
growBuffer(minSize: number): boolean {
|
|
286
|
+
const newSize = Math.max(minSize, 2 * this.buffer.byteLength);
|
|
287
|
+
const newBuffer = new ArrayBuffer(newSize);
|
|
288
|
+
|
|
289
|
+
new Uint8Array(newBuffer).set(new Uint8Array(this.buffer));
|
|
290
|
+
this.buffer = newBuffer;
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
import { applyWalChanges, DatabaseServer, WalIndexChange, WriteAheadBuffers } from './shared.js';
|
|
3
|
+
import { RawQueryResult } from '@powersync/common';
|
|
4
|
+
import { InMemoryWriteAheadLog } from './vfs.js';
|
|
5
|
+
import { RawSqliteConnection, RawWaSqliteDatabaseOptions, RawWebResult } from '../wa-sqlite/RawSqliteConnection.js';
|
|
6
|
+
import { WASQLiteVFS } from '../wa-sqlite/vfs.js';
|
|
7
|
+
import { TemporaryStorageOption } from '../options.js';
|
|
8
|
+
|
|
9
|
+
class MemoryDatabaseServer implements DatabaseServer {
|
|
10
|
+
#vfs!: InMemoryWriteAheadLog;
|
|
11
|
+
#connection!: RawSqliteConnection;
|
|
12
|
+
|
|
13
|
+
async open(buffers: WriteAheadBuffers, options: Partial<RawWaSqliteDatabaseOptions>): Promise<void> {
|
|
14
|
+
const { default: factory } = await import('@journeyapps/wa-sqlite/dist/wa-sqlite.mjs');
|
|
15
|
+
const module = await factory();
|
|
16
|
+
const vfs = (this.#vfs = new InMemoryWriteAheadLog(module, buffers));
|
|
17
|
+
|
|
18
|
+
const connection = new RawSqliteConnection({
|
|
19
|
+
encryptionKey: undefined,
|
|
20
|
+
vfs: WASQLiteVFS.InMemoryVfs,
|
|
21
|
+
// Unused, since we register our own VFS. Only needed to satisfy the type.
|
|
22
|
+
temporaryStorage: TemporaryStorageOption.MEMORY,
|
|
23
|
+
cacheSizeKb: 50 * 1024,
|
|
24
|
+
preparedStatementsCache: 0,
|
|
25
|
+
...options,
|
|
26
|
+
// The rest of these options is not overridable: The VFS handles /database reads and writes in a special way and
|
|
27
|
+
// there can only be one database per buffers instance.
|
|
28
|
+
filename: '/database',
|
|
29
|
+
readonly: false
|
|
30
|
+
});
|
|
31
|
+
await connection.initWithModule(module, vfs);
|
|
32
|
+
this.#connection = connection;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async updateWalState(overlay: WalIndexChange): Promise<void> {
|
|
36
|
+
const currentState = this.#vfs.writeAheadState;
|
|
37
|
+
applyWalChanges(currentState, overlay);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async executeRaw(query: string, params?: any[] | undefined): Promise<RawQueryResult> {
|
|
41
|
+
return await this.#connection.execute(query, params);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async executeBatch(query: string, params: any[][]): Promise<RawWebResult[]> {
|
|
45
|
+
return await this.#connection.executeBatch(query, params);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async takeWalChanges(): Promise<WalIndexChange> {
|
|
49
|
+
return this.#vfs.takeChanges();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
Comlink.expose(new MemoryDatabaseServer());
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { LogLevels, SQLOpenOptions } from '@powersync/common';
|
|
2
|
+
import { WASQLiteVFS } from './wa-sqlite/vfs.js';
|
|
3
|
+
|
|
4
|
+
export interface WebSpecificOpenOptions {
|
|
5
|
+
/**
|
|
6
|
+
* SQLite operations are currently not supported in SSR mode.
|
|
7
|
+
* A warning will be logged if attempting to use SQLite in SSR.
|
|
8
|
+
* Setting this to `true` will disabled the warning above.
|
|
9
|
+
*/
|
|
10
|
+
disableSSRWarning: boolean;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Enables multi tab support.
|
|
14
|
+
*
|
|
15
|
+
* Enabling multi-tab support will transparently make PowerSync manage the sync process in a shared worker collecting
|
|
16
|
+
* Sync Streams across tabs. Additionally, it enables a shared worker for IndexedDB databases.
|
|
17
|
+
*
|
|
18
|
+
* It is still valid to open multiple tabs when this option is disabled, but the experience may be degrated as only
|
|
19
|
+
* one tab can sync at the time.
|
|
20
|
+
*
|
|
21
|
+
* This is enabled by default on Desktop browsers if shared workers are enabled, except for Safari.
|
|
22
|
+
*/
|
|
23
|
+
enableMultiTabs: boolean;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The SQLite connection is often executed through a web worker in order to offload computation and because some file
|
|
27
|
+
* system implementations (notably those based on web filesystem APIs like OPFS) are only available in workers.
|
|
28
|
+
*
|
|
29
|
+
* Manually disabling the use of web workers is not recommended, but can be useful for testing or for environments
|
|
30
|
+
* or toolchains where web workers are not supported.
|
|
31
|
+
*/
|
|
32
|
+
useWebWorker: boolean;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Open in SSR placeholder mode. DB operations and Sync operations will be a No-op
|
|
36
|
+
*/
|
|
37
|
+
ssrMode: boolean;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The log level for database workers.
|
|
41
|
+
*
|
|
42
|
+
* Defaults to {@link LogLevels.info}.
|
|
43
|
+
*/
|
|
44
|
+
databaseWorkerLogLevel: number;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Where to store SQLite temporary files. Defaults to 'MEMORY'.
|
|
48
|
+
* Setting this to `FILESYSTEM` can cause issues with larger queries or datasets.
|
|
49
|
+
*/
|
|
50
|
+
temporaryStorage: TemporaryStorageOption;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Maximum SQLite cache size. Defaults to 50MB.
|
|
54
|
+
*
|
|
55
|
+
* For details, see: https://www.sqlite.org/pragma.html#pragma_cache_size
|
|
56
|
+
*/
|
|
57
|
+
cacheSizeKb: number;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Encryption key for the database.
|
|
61
|
+
* If set, the database will be encrypted using ChaCha20.
|
|
62
|
+
*/
|
|
63
|
+
encryptionKey: string | undefined;
|
|
64
|
+
|
|
65
|
+
vfs: WASQLiteVFS;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* If the {@link vfs} supports it, an additional amount of read-only connections to open. Using additional read
|
|
69
|
+
* connections can speed up queries by dispatching them to multiple workers running them concurrently.
|
|
70
|
+
*
|
|
71
|
+
* {@link WASQLiteVFS.OPFSWriteAheadVFS} is the only VFS with support for multiple connections, so this option is
|
|
72
|
+
* ignored for other VFS implementations.
|
|
73
|
+
*
|
|
74
|
+
* Defaults to 1.
|
|
75
|
+
*/
|
|
76
|
+
additionalReaders: number;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Allows you to override the default wasqlite db worker.
|
|
80
|
+
*
|
|
81
|
+
* You can either provide a path to the worker script
|
|
82
|
+
* or a factory method that returns a worker.
|
|
83
|
+
*/
|
|
84
|
+
worker?: string | URL | ((options: ResolvedWebSQLOpenOptions) => Worker | SharedWorker);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Use an existing port to an initialized worker.
|
|
88
|
+
* A worker will be initialized if none is provided
|
|
89
|
+
*/
|
|
90
|
+
workerPort?: MessagePort | undefined;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* If set to a value greater than zero, the worker will cache prepared statements to avoid preparing them every time
|
|
94
|
+
* a query runs.
|
|
95
|
+
*
|
|
96
|
+
* Defaults to 0 (disabling the cache).
|
|
97
|
+
*/
|
|
98
|
+
preparedStatementsCache?: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface ResolvedWebSQLOpenOptions extends SQLOpenOptions, WebSpecificOpenOptions {}
|
|
102
|
+
|
|
103
|
+
export interface WebSQLOpenOptions extends SQLOpenOptions, Partial<WebSpecificOpenOptions> {}
|
|
104
|
+
|
|
105
|
+
export enum TemporaryStorageOption {
|
|
106
|
+
MEMORY = 'memory',
|
|
107
|
+
FILESYSTEM = 'file'
|
|
108
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { LogLevels } from '@powersync/common';
|
|
2
|
+
import { TemporaryStorageOption, WebSpecificOpenOptions } from './options.js';
|
|
3
|
+
import { vfsRequiresDedicatedWorkers, WASQLiteVFS } from './wa-sqlite/vfs.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The maximum length of a db filename we support.
|
|
7
|
+
*
|
|
8
|
+
* We configure the same on WA-SQLite (which otherwise defaults to a maximum length of 64). We don't want to support
|
|
9
|
+
* very long path names as Safari maps OPFS files directly to OS files, and APFS has a 255-byte filename limit. Since
|
|
10
|
+
* some VFS append additional characters for pooled file access handles, we want to stay well below that.
|
|
11
|
+
*/
|
|
12
|
+
export const maxPathNameLength = 128;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export function resolveAndValidateOptions<And = {}>(
|
|
18
|
+
options: Partial<WebSpecificOpenOptions> & And
|
|
19
|
+
): WebSpecificOpenOptions & And {
|
|
20
|
+
const defaults: WebSpecificOpenOptions = {
|
|
21
|
+
disableSSRWarning: false,
|
|
22
|
+
ssrMode: !('window' in globalThis),
|
|
23
|
+
/**
|
|
24
|
+
* Multiple tabs are by default not supported on Android, iOS and Safari.
|
|
25
|
+
* Other platforms will have multiple tabs enabled by default.
|
|
26
|
+
*/
|
|
27
|
+
enableMultiTabs:
|
|
28
|
+
typeof globalThis.navigator !== 'undefined' && // For SSR purposes
|
|
29
|
+
typeof SharedWorker !== 'undefined' &&
|
|
30
|
+
!navigator.userAgent.match(/(Android|iPhone|iPod|iPad)/i) &&
|
|
31
|
+
!(window as any).safari,
|
|
32
|
+
useWebWorker: true,
|
|
33
|
+
databaseWorkerLogLevel: LogLevels.info,
|
|
34
|
+
temporaryStorage: TemporaryStorageOption.MEMORY,
|
|
35
|
+
cacheSizeKb: 50 * 1024,
|
|
36
|
+
encryptionKey: undefined,
|
|
37
|
+
vfs: WASQLiteVFS.IDBBatchAtomicVFS,
|
|
38
|
+
additionalReaders: 1
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const resolved = Object.assign(defaults, options);
|
|
42
|
+
if (vfsRequiresDedicatedWorkers(resolved.vfs) && !resolved.useWebWorker) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`Invalid configuration: The 'useWebWorker' flag must be true when using an OPFS-based VFS (${resolved.vfs}).`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return resolved;
|
|
49
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Mutex, UnlockFn } from '@powersync/
|
|
2
|
-
import { RawSqliteConnection } from './RawSqliteConnection.js';
|
|
3
|
-
import { ResolvedWASQLiteOpenFactoryOptions } from './WASQLiteOpenFactory.js';
|
|
1
|
+
import { Mutex, UnlockFn } from '@powersync/shared-internals';
|
|
2
|
+
import { RawSqliteConnection, RawWaSqliteDatabaseOptions } from './RawSqliteConnection.js';
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* A wrapper around a {@link RawSqliteConnection} allowing multiple tabs to access it.
|
|
@@ -33,7 +32,7 @@ export class ConcurrentSqliteConnection {
|
|
|
33
32
|
this.leaseMutex = needsNavigatorLocks ? null : new Mutex();
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
get options():
|
|
35
|
+
get options(): RawWaSqliteDatabaseOptions {
|
|
37
36
|
return this.inner.options;
|
|
38
37
|
}
|
|
39
38
|
|
|
@@ -46,7 +45,7 @@ export class ConcurrentSqliteConnection {
|
|
|
46
45
|
const options: LockOptions = { signal: abort };
|
|
47
46
|
|
|
48
47
|
navigator.locks
|
|
49
|
-
.request(`db-lock-${this.options.
|
|
48
|
+
.request(`db-lock-${this.options.filename}`, options, (_) => {
|
|
50
49
|
return new Promise<void>((returnLock) => {
|
|
51
50
|
return resolve(() => {
|
|
52
51
|
returnLock();
|
|
@@ -2,23 +2,22 @@ import {
|
|
|
2
2
|
QueryResult,
|
|
3
3
|
LockContext,
|
|
4
4
|
DBLockOptions,
|
|
5
|
-
DBAdapterListener,
|
|
6
|
-
ConnectionPool,
|
|
7
5
|
SqlExecutor,
|
|
8
|
-
DBGetUtilsDefaultMixin,
|
|
9
6
|
BatchedUpdateNotification,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
SQLOpenOptions,
|
|
8
|
+
DBAdapter,
|
|
9
|
+
RawQueryResult,
|
|
10
|
+
queryResultWithoutRows,
|
|
11
|
+
BaseQueryResult
|
|
13
12
|
} from '@powersync/common';
|
|
14
13
|
import { SharedConnectionWorker, WebDBAdapterConfiguration } from '../WebDBAdapter.js';
|
|
15
14
|
import { ClientConnectionView } from './DatabaseServer.js';
|
|
16
|
-
import { RawQueryResult } from './RawSqliteConnection.js';
|
|
17
15
|
import * as Comlink from 'comlink';
|
|
18
|
-
import {
|
|
16
|
+
import type { ConnectToMultiDatabaseServerOptions } from '../../../worker/db/MultiDatabaseServer.js';
|
|
17
|
+
import { ConnectionClosedError } from '@powersync/shared-internals';
|
|
19
18
|
|
|
20
19
|
export interface OpenWorkerConnection {
|
|
21
|
-
connect(config:
|
|
20
|
+
connect(config: ConnectToMultiDatabaseServerOptions): Promise<ClientConnectionView>;
|
|
22
21
|
connectToExisting(options: { identifier: string; lockName: string }): Promise<ClientConnectionView>;
|
|
23
22
|
}
|
|
24
23
|
|
|
@@ -44,10 +43,7 @@ export interface ClientOptions {
|
|
|
44
43
|
/**
|
|
45
44
|
* A single-connection {@link ConnectionPool} implementation based on a worker connection.
|
|
46
45
|
*/
|
|
47
|
-
export class DatabaseClient<Config extends SQLOpenOptions = WebDBAdapterConfiguration>
|
|
48
|
-
extends BaseObserver<DBAdapterListener>
|
|
49
|
-
implements ConnectionPool
|
|
50
|
-
{
|
|
46
|
+
export class DatabaseClient<Config extends SQLOpenOptions = WebDBAdapterConfiguration> extends DBAdapter {
|
|
51
47
|
#connection: ConnectionState;
|
|
52
48
|
#shareConnectionAbortController = new AbortController();
|
|
53
49
|
#receiveTableUpdates: MessagePort;
|
|
@@ -70,9 +66,7 @@ export class DatabaseClient<Config extends SQLOpenOptions = WebDBAdapterConfigur
|
|
|
70
66
|
port2.onmessage = (event) => {
|
|
71
67
|
const tables = event.data as string[];
|
|
72
68
|
const notification: BatchedUpdateNotification = {
|
|
73
|
-
tables
|
|
74
|
-
groupedUpdates: {},
|
|
75
|
-
rawUpdates: []
|
|
69
|
+
tables
|
|
76
70
|
};
|
|
77
71
|
this.iterateListeners((l) => {
|
|
78
72
|
l.tablesUpdated && l.tablesUpdated(notification);
|
|
@@ -185,16 +179,17 @@ export class DatabaseClient<Config extends SQLOpenOptions = WebDBAdapterConfigur
|
|
|
185
179
|
}
|
|
186
180
|
|
|
187
181
|
/**
|
|
188
|
-
* A {@link
|
|
182
|
+
* A {@link LockContext} implemented by sending commands to a worker.
|
|
189
183
|
*
|
|
190
184
|
* While an instance is active, it has exclusive access to the underlying database connection (as represented by its
|
|
191
185
|
* token).
|
|
192
186
|
*/
|
|
193
|
-
class
|
|
187
|
+
class ClientLockContext extends LockContext {
|
|
194
188
|
readonly #connection: ConnectionState;
|
|
195
189
|
readonly #token: string;
|
|
196
190
|
|
|
197
191
|
constructor(connection: ConnectionState, token: string) {
|
|
192
|
+
super();
|
|
198
193
|
this.#connection = connection;
|
|
199
194
|
this.#token = token;
|
|
200
195
|
}
|
|
@@ -223,37 +218,8 @@ class ClientSqlExecutor implements SqlExecutor {
|
|
|
223
218
|
}
|
|
224
219
|
}
|
|
225
220
|
|
|
226
|
-
async
|
|
227
|
-
|
|
228
|
-
let rows: QueryResult['rows'] | undefined;
|
|
229
|
-
if (rs.resultSet) {
|
|
230
|
-
const resultSet = rs.resultSet;
|
|
231
|
-
|
|
232
|
-
function rowToJavaScriptObject(row: any[]): Record<string, any> {
|
|
233
|
-
const obj: Record<string, any> = {};
|
|
234
|
-
resultSet.columns.forEach((key, idx) => (obj[key] = row[idx]));
|
|
235
|
-
return obj;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const mapped = resultSet.rows.map(rowToJavaScriptObject);
|
|
239
|
-
|
|
240
|
-
rows = {
|
|
241
|
-
_array: mapped,
|
|
242
|
-
length: mapped.length,
|
|
243
|
-
item: (idx: number) => mapped[idx]
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return {
|
|
248
|
-
rowsAffected: rs.changes,
|
|
249
|
-
insertId: rs.lastInsertRowId,
|
|
250
|
-
rows
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
async executeRaw(query: string, params?: any[] | undefined): Promise<any[][]> {
|
|
255
|
-
const rs = await this.#executeOnWorker(query, params);
|
|
256
|
-
return rs.resultSet?.rows ?? [];
|
|
221
|
+
async executeRaw(query: string, params?: any[] | undefined): Promise<RawQueryResult> {
|
|
222
|
+
return await this.#executeOnWorker(query, params);
|
|
257
223
|
}
|
|
258
224
|
|
|
259
225
|
async #executeOnWorker(query: string, params: any[] | undefined): Promise<RawQueryResult> {
|
|
@@ -263,23 +229,21 @@ class ClientSqlExecutor implements SqlExecutor {
|
|
|
263
229
|
);
|
|
264
230
|
}
|
|
265
231
|
|
|
266
|
-
async executeBatch(query: string, params: any[][] = []): Promise<QueryResult
|
|
232
|
+
async executeBatch(query: string, params: any[][] = []): Promise<QueryResult<never>> {
|
|
267
233
|
const results = await this.maybeTrace(
|
|
268
234
|
(c) => c.executeBatch(this.#token, query, params),
|
|
269
235
|
() => `${query} (batch of ${params.length})`
|
|
270
236
|
);
|
|
271
|
-
const result:
|
|
237
|
+
const result: BaseQueryResult = { insertId: undefined, rowsAffected: 0 };
|
|
272
238
|
for (const source of results) {
|
|
273
|
-
result.insertId = source.
|
|
274
|
-
result.rowsAffected
|
|
239
|
+
result.insertId = source.insertId;
|
|
240
|
+
result.rowsAffected = (result.rowsAffected ?? 0) + source.rowsAffected;
|
|
275
241
|
}
|
|
276
242
|
|
|
277
|
-
return result;
|
|
243
|
+
return queryResultWithoutRows(result);
|
|
278
244
|
}
|
|
279
245
|
}
|
|
280
246
|
|
|
281
|
-
class ClientLockContext extends DBGetUtilsDefaultMixin(ClientSqlExecutor) implements LockContext {}
|
|
282
|
-
|
|
283
247
|
interface ConnectionState {
|
|
284
248
|
connection: ClientConnectionView;
|
|
285
249
|
notifyRemoteClosed: AbortController | undefined;
|