@powersync/web 0.0.0-dev-20251203144301 → 0.0.0-dev-20260112083235

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.
Files changed (107) hide show
  1. package/README.md +1 -1
  2. package/dist/0b19af1befc07ce338dd.wasm +0 -0
  3. package/dist/2632c3bda9473da74fd5.wasm +0 -0
  4. package/dist/64f5351ba3784bfe2f3e.wasm +0 -0
  5. package/dist/9318ca94aac4d0fe0135.wasm +0 -0
  6. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js +1875 -0
  7. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js.map +1 -0
  8. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-685f6c0.index.umd.js +567 -0
  9. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-685f6c0.index.umd.js.map +1 -0
  10. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-685f6c1.index.umd.js +567 -0
  11. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-685f6c1.index.umd.js.map +1 -0
  12. package/dist/index.umd.js +7107 -4249
  13. package/dist/index.umd.js.map +1 -1
  14. package/dist/worker/SharedSyncImplementation.umd.js +337 -2101
  15. package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
  16. package/dist/worker/WASQLiteDB.umd.js +182 -1931
  17. package/dist/worker/WASQLiteDB.umd.js.map +1 -1
  18. package/dist/worker/node_modules_bson_lib_bson_mjs.umd.js +3 -3
  19. package/dist/worker/node_modules_bson_lib_bson_mjs.umd.js.map +1 -1
  20. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js +22 -9
  21. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js.map +1 -1
  22. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js +22 -9
  23. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js.map +1 -1
  24. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js +22 -9
  25. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js.map +1 -1
  26. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +22 -9
  27. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +1 -1
  28. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js +1206 -3
  29. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js.map +1 -1
  30. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js +1209 -6
  31. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js.map +1 -1
  32. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js +1820 -0
  33. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js.map +1 -0
  34. package/lib/package.json +20 -15
  35. package/lib/src/db/PowerSyncDatabase.d.ts +3 -1
  36. package/lib/src/db/PowerSyncDatabase.js +19 -7
  37. package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.d.ts +1 -1
  38. package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js +1 -1
  39. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.d.ts +2 -2
  40. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js +2 -2
  41. package/lib/src/db/adapters/AsyncDatabaseConnection.d.ts +1 -9
  42. package/lib/src/db/adapters/AsyncDatabaseConnection.js +1 -11
  43. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.d.ts +3 -3
  44. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.js +25 -12
  45. package/lib/src/db/adapters/WebDBAdapter.d.ts +1 -1
  46. package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.d.ts +2 -2
  47. package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.js +1 -2
  48. package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.d.ts +2 -2
  49. package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.js +12 -3
  50. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.d.ts +3 -3
  51. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +5 -5
  52. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +4 -4
  53. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +6 -6
  54. package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.d.ts +1 -1
  55. package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js +3 -3
  56. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +4 -4
  57. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +5 -5
  58. package/lib/src/db/sync/WebRemote.js +1 -1
  59. package/lib/src/db/sync/WebStreamingSyncImplementation.d.ts +1 -1
  60. package/lib/src/db/sync/WebStreamingSyncImplementation.js +1 -1
  61. package/lib/src/index.d.ts +12 -12
  62. package/lib/src/index.js +12 -12
  63. package/lib/src/worker/db/SharedWASQLiteConnection.d.ts +2 -2
  64. package/lib/src/worker/db/WASQLiteDB.worker.js +4 -3
  65. package/lib/src/worker/db/WorkerWASQLiteConnection.d.ts +2 -2
  66. package/lib/src/worker/db/WorkerWASQLiteConnection.js +1 -1
  67. package/lib/src/worker/db/open-worker-database.d.ts +2 -2
  68. package/lib/src/worker/db/open-worker-database.js +1 -1
  69. package/lib/src/worker/sync/BroadcastLogger.d.ts +1 -1
  70. package/lib/src/worker/sync/SharedSyncImplementation.d.ts +4 -4
  71. package/lib/src/worker/sync/SharedSyncImplementation.js +84 -83
  72. package/lib/src/worker/sync/SharedSyncImplementation.worker.js +2 -2
  73. package/lib/src/worker/sync/WorkerClient.d.ts +1 -1
  74. package/lib/src/worker/sync/WorkerClient.js +2 -2
  75. package/lib/tsconfig.tsbuildinfo +1 -1
  76. package/package.json +21 -16
  77. package/src/db/PowerSyncDatabase.ts +23 -8
  78. package/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.ts +1 -1
  79. package/src/db/adapters/AbstractWebSQLOpenFactory.ts +3 -3
  80. package/src/db/adapters/AsyncDatabaseConnection.ts +1 -13
  81. package/src/db/adapters/LockedAsyncDatabaseAdapter.ts +30 -14
  82. package/src/db/adapters/WebDBAdapter.ts +1 -1
  83. package/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.ts +3 -4
  84. package/src/db/adapters/wa-sqlite/WASQLiteConnection.ts +14 -5
  85. package/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.ts +7 -7
  86. package/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +7 -7
  87. package/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.ts +3 -3
  88. package/src/db/sync/SharedWebStreamingSyncImplementation.ts +7 -7
  89. package/src/db/sync/WebRemote.ts +1 -1
  90. package/src/db/sync/WebStreamingSyncImplementation.ts +2 -2
  91. package/src/index.ts +12 -12
  92. package/src/worker/db/SharedWASQLiteConnection.ts +2 -2
  93. package/src/worker/db/WASQLiteDB.worker.ts +6 -5
  94. package/src/worker/db/WorkerWASQLiteConnection.ts +2 -2
  95. package/src/worker/db/open-worker-database.ts +2 -2
  96. package/src/worker/sync/BroadcastLogger.ts +1 -1
  97. package/src/worker/sync/SharedSyncImplementation.ts +92 -89
  98. package/src/worker/sync/SharedSyncImplementation.worker.ts +2 -2
  99. package/src/worker/sync/WorkerClient.ts +2 -2
  100. package/dist/1807036ae51c10ee4d23.wasm +0 -0
  101. package/dist/307d8ce2280e3bae09d5.wasm +0 -0
  102. package/dist/cd8b9e8f4c87bf81c169.wasm +0 -0
  103. package/dist/e797080f5ed0b5324166.wasm +0 -0
  104. package/lib/src/worker/db/opfs.d.ts +0 -96
  105. package/lib/src/worker/db/opfs.js +0 -582
  106. package/src/worker/db/opfs.ts +0 -623
  107. /package/bin/{powersync.js → powersync.cjs} +0 -0
@@ -1,623 +0,0 @@
1
- // Copyright 2024 Roy T. Hashimoto. All Rights Reserved.
2
- // @ts-nocheck
3
- import { FacadeVFS } from '@journeyapps/wa-sqlite/src/FacadeVFS.js';
4
- import * as VFS from '@journeyapps/wa-sqlite/src/VFS.js';
5
-
6
- const DEFAULT_TEMPORARY_FILES = 10;
7
- const LOCK_NOTIFY_INTERVAL = 1000;
8
-
9
- const DB_RELATED_FILE_SUFFIXES = ['', '-journal', '-wal'];
10
-
11
- const finalizationRegistry = new FinalizationRegistry((releaser) => releaser());
12
-
13
- class File {
14
- /** @type {string} */ path;
15
- /** @type {number} */ flags;
16
- /** @type {FileSystemSyncAccessHandle} */ accessHandle;
17
-
18
- /** @type {PersistentFile?} */ persistentFile;
19
-
20
- constructor(path, flags) {
21
- this.path = path;
22
- this.flags = flags;
23
- }
24
- }
25
-
26
- class PersistentFile {
27
- /** @type {FileSystemFileHandle} */ fileHandle;
28
- /** @type {FileSystemSyncAccessHandle} */ accessHandle = null;
29
-
30
- // The following properties are for main database files.
31
-
32
- /** @type {boolean} */ isLockBusy = false;
33
- /** @type {boolean} */ isFileLocked = false;
34
- /** @type {boolean} */ isRequestInProgress = false;
35
- /** @type {function} */ handleLockReleaser = null;
36
-
37
- /** @type {BroadcastChannel} */ handleRequestChannel;
38
- /** @type {boolean} */ isHandleRequested = false;
39
-
40
- constructor(fileHandle) {
41
- this.fileHandle = fileHandle;
42
- }
43
- }
44
-
45
- export class OPFSCoopSyncVFS extends FacadeVFS {
46
- /** @type {Map<number, File>} */ mapIdToFile = new Map();
47
-
48
- lastError = null;
49
- log = null; //function(...args) { console.log(`[${contextName}]`, ...args) };
50
-
51
- /** @type {Map<string, PersistentFile>} */ persistentFiles = new Map();
52
- /** @type {Map<string, FileSystemSyncAccessHandle>} */ boundAccessHandles = new Map();
53
- /** @type {Set<FileSystemSyncAccessHandle>} */ unboundAccessHandles = new Set();
54
- /** @type {Set<string>} */ accessiblePaths = new Set();
55
- releaser = null;
56
-
57
- static async create(name, module) {
58
- const vfs = new OPFSCoopSyncVFS(name, module);
59
- await Promise.all([vfs.isReady(), vfs.#initialize(DEFAULT_TEMPORARY_FILES)]);
60
- return vfs;
61
- }
62
-
63
- constructor(name, module) {
64
- super(name, module);
65
- }
66
-
67
- async #initialize(nTemporaryFiles) {
68
- // Delete temporary directories no longer in use.
69
- const root = await navigator.storage.getDirectory();
70
- // @ts-ignore
71
- for await (const entry of root.values()) {
72
- if (entry.kind === 'directory' && entry.name.startsWith('.ahp-')) {
73
- // A lock with the same name as the directory protects it from
74
- // being deleted.
75
- await navigator.locks.request(entry.name, { ifAvailable: true }, async (lock) => {
76
- if (lock) {
77
- this.log?.(`Deleting temporary directory ${entry.name}`);
78
- await root.removeEntry(entry.name, { recursive: true });
79
- } else {
80
- this.log?.(`Temporary directory ${entry.name} is in use`);
81
- }
82
- });
83
- }
84
- }
85
-
86
- // Create our temporary directory.
87
- const tmpDirName = `.ahp-${Math.random().toString(36).slice(2)}`;
88
- this.releaser = await new Promise((resolve) => {
89
- navigator.locks.request(tmpDirName, () => {
90
- return new Promise((release) => {
91
- resolve(release);
92
- });
93
- });
94
- });
95
-
96
- const releaseHandle = async () => {
97
- await Promise.all(
98
- this.persistentFiles.values().map(async (file) => {
99
- try {
100
- await this.#releaseAccessHandle(file);
101
- } catch (e) {
102
- this.log?.('error releasing access handle', e);
103
- } finally {
104
- release();
105
- }
106
- })
107
- );
108
- };
109
-
110
- finalizationRegistry.register(this, releaseHandle);
111
- finalizationRegistry.register(this, this.releaser);
112
- const tmpDir = await root.getDirectoryHandle(tmpDirName, { create: true });
113
-
114
- // Populate temporary directory.
115
- for (let i = 0; i < nTemporaryFiles; i++) {
116
- const tmpFile = await tmpDir.getFileHandle(`${i}.tmp`, { create: true });
117
- const tmpAccessHandle = await tmpFile.createSyncAccessHandle();
118
- this.unboundAccessHandles.add(tmpAccessHandle);
119
- }
120
- }
121
-
122
- /**
123
- * @param {string?} zName
124
- * @param {number} fileId
125
- * @param {number} flags
126
- * @param {DataView} pOutFlags
127
- * @returns {number}
128
- */
129
- jOpen(zName, fileId, flags, pOutFlags) {
130
- try {
131
- const url = new URL(zName || Math.random().toString(36).slice(2), 'file://');
132
- const path = url.pathname;
133
-
134
- if (flags & VFS.SQLITE_OPEN_MAIN_DB) {
135
- const persistentFile = this.persistentFiles.get(path);
136
- if (persistentFile?.isRequestInProgress) {
137
- // Should not reach here unless SQLite itself retries an open.
138
- // Otherwise, asynchronous operations started on a previous
139
- // open try should have completed.
140
- return VFS.SQLITE_BUSY;
141
- } else if (!persistentFile) {
142
- // This is the usual starting point for opening a database.
143
- // Register a Promise that resolves when the database and related
144
- // files are ready to be used.
145
- this.log?.(`creating persistent file for ${path}`);
146
- const create = !!(flags & VFS.SQLITE_OPEN_CREATE);
147
- this._module.retryOps.push(
148
- (async () => {
149
- try {
150
- // Get the path directory handle.
151
- let dirHandle = await navigator.storage.getDirectory();
152
- const directories = path.split('/').filter((d) => d);
153
- const filename = directories.pop();
154
- for (const directory of directories) {
155
- dirHandle = await dirHandle.getDirectoryHandle(directory, { create });
156
- }
157
-
158
- // Get file handles for the database and related files,
159
- // and create persistent file instances.
160
- for (const suffix of DB_RELATED_FILE_SUFFIXES) {
161
- const fileHandle = await dirHandle.getFileHandle(filename + suffix, { create });
162
- await this.#createPersistentFile(fileHandle);
163
- }
164
-
165
- // Get access handles for the files.
166
- const file = new File(path, flags);
167
- file.persistentFile = this.persistentFiles.get(path);
168
- await this.#requestAccessHandle(file);
169
- } catch (e) {
170
- // Use an invalid persistent file to signal this error
171
- // for the retried open.
172
- const persistentFile = new PersistentFile(null);
173
- this.persistentFiles.set(path, persistentFile);
174
- console.error(e);
175
- }
176
- })()
177
- );
178
- return VFS.SQLITE_BUSY;
179
- } else if (!persistentFile.fileHandle) {
180
- // The asynchronous open operation failed.
181
- this.persistentFiles.delete(path);
182
- return VFS.SQLITE_CANTOPEN;
183
- } else if (!persistentFile.accessHandle) {
184
- // This branch is reached if the database was previously opened
185
- // and closed.
186
- this._module.retryOps.push(
187
- (async () => {
188
- const file = new File(path, flags);
189
- file.persistentFile = this.persistentFiles.get(path);
190
- await this.#requestAccessHandle(file);
191
- })()
192
- );
193
- return VFS.SQLITE_BUSY;
194
- }
195
- }
196
-
197
- if (!this.accessiblePaths.has(path) && !(flags & VFS.SQLITE_OPEN_CREATE)) {
198
- throw new Error(`File ${path} not found`);
199
- }
200
-
201
- const file = new File(path, flags);
202
- this.mapIdToFile.set(fileId, file);
203
-
204
- if (this.persistentFiles.has(path)) {
205
- file.persistentFile = this.persistentFiles.get(path);
206
- } else if (this.boundAccessHandles.has(path)) {
207
- // This temporary file was previously created and closed. Reopen
208
- // the same access handle.
209
- file.accessHandle = this.boundAccessHandles.get(path);
210
- } else if (this.unboundAccessHandles.size) {
211
- // Associate an unbound access handle to this file.
212
- file.accessHandle = this.unboundAccessHandles.values().next().value;
213
- file.accessHandle.truncate(0);
214
- this.unboundAccessHandles.delete(file.accessHandle);
215
- this.boundAccessHandles.set(path, file.accessHandle);
216
- }
217
- this.accessiblePaths.add(path);
218
-
219
- pOutFlags.setInt32(0, flags, true);
220
- return VFS.SQLITE_OK;
221
- } catch (e) {
222
- this.lastError = e;
223
- return VFS.SQLITE_CANTOPEN;
224
- }
225
- }
226
-
227
- /**
228
- * @param {string} zName
229
- * @param {number} syncDir
230
- * @returns {number}
231
- */
232
- jDelete(zName, syncDir) {
233
- try {
234
- const url = new URL(zName, 'file://');
235
- const path = url.pathname;
236
- if (this.persistentFiles.has(path)) {
237
- const persistentFile = this.persistentFiles.get(path);
238
- persistentFile.accessHandle.truncate(0);
239
- } else {
240
- this.boundAccessHandles.get(path)?.truncate(0);
241
- }
242
- this.accessiblePaths.delete(path);
243
- return VFS.SQLITE_OK;
244
- } catch (e) {
245
- this.lastError = e;
246
- return VFS.SQLITE_IOERR_DELETE;
247
- }
248
- }
249
-
250
- /**
251
- * @param {string} zName
252
- * @param {number} flags
253
- * @param {DataView} pResOut
254
- * @returns {number}
255
- */
256
- jAccess(zName, flags, pResOut) {
257
- try {
258
- const url = new URL(zName, 'file://');
259
- const path = url.pathname;
260
- pResOut.setInt32(0, this.accessiblePaths.has(path) ? 1 : 0, true);
261
- return VFS.SQLITE_OK;
262
- } catch (e) {
263
- this.lastError = e;
264
- return VFS.SQLITE_IOERR_ACCESS;
265
- }
266
- }
267
-
268
- /**
269
- * @param {number} fileId
270
- * @returns {number}
271
- */
272
- jClose(fileId) {
273
- try {
274
- const file = this.mapIdToFile.get(fileId);
275
- this.mapIdToFile.delete(fileId);
276
-
277
- if (file?.flags & VFS.SQLITE_OPEN_MAIN_DB) {
278
- if (file.persistentFile?.accessHandle) {
279
- this.#releaseAccessHandle(file);
280
- }
281
- } else if (file?.flags & VFS.SQLITE_OPEN_DELETEONCLOSE) {
282
- file.accessHandle.truncate(0);
283
- this.accessiblePaths.delete(file.path);
284
- if (!this.persistentFiles.has(file.path)) {
285
- this.boundAccessHandles.delete(file.path);
286
- this.unboundAccessHandles.add(file.accessHandle);
287
- }
288
- }
289
- return VFS.SQLITE_OK;
290
- } catch (e) {
291
- this.lastError = e;
292
- return VFS.SQLITE_IOERR_CLOSE;
293
- }
294
- }
295
-
296
- /**
297
- * @param {number} fileId
298
- * @param {Uint8Array} pData
299
- * @param {number} iOffset
300
- * @returns {number}
301
- */
302
- jRead(fileId, pData, iOffset) {
303
- try {
304
- const file = this.mapIdToFile.get(fileId);
305
-
306
- // On Chrome (at least), passing pData to accessHandle.read() is
307
- // an error because pData is a Proxy of a Uint8Array. Calling
308
- // subarray() produces a real Uint8Array and that works.
309
- const accessHandle = file.accessHandle || file.persistentFile.accessHandle;
310
- const bytesRead = accessHandle.read(pData.subarray(), { at: iOffset });
311
-
312
- // Opening a database file performs one read without a xLock call.
313
- if (file.flags & VFS.SQLITE_OPEN_MAIN_DB && !file.persistentFile.isFileLocked) {
314
- this.#releaseAccessHandle(file);
315
- }
316
-
317
- if (bytesRead < pData.byteLength) {
318
- pData.fill(0, bytesRead);
319
- return VFS.SQLITE_IOERR_SHORT_READ;
320
- }
321
- return VFS.SQLITE_OK;
322
- } catch (e) {
323
- this.lastError = e;
324
- return VFS.SQLITE_IOERR_READ;
325
- }
326
- }
327
-
328
- /**
329
- * @param {number} fileId
330
- * @param {Uint8Array} pData
331
- * @param {number} iOffset
332
- * @returns {number}
333
- */
334
- jWrite(fileId, pData, iOffset) {
335
- try {
336
- const file = this.mapIdToFile.get(fileId);
337
-
338
- // On Chrome (at least), passing pData to accessHandle.write() is
339
- // an error because pData is a Proxy of a Uint8Array. Calling
340
- // subarray() produces a real Uint8Array and that works.
341
- const accessHandle = file.accessHandle || file.persistentFile.accessHandle;
342
- const nBytes = accessHandle.write(pData.subarray(), { at: iOffset });
343
- if (nBytes !== pData.byteLength) throw new Error('short write');
344
- return VFS.SQLITE_OK;
345
- } catch (e) {
346
- this.lastError = e;
347
- return VFS.SQLITE_IOERR_WRITE;
348
- }
349
- }
350
-
351
- /**
352
- * @param {number} fileId
353
- * @param {number} iSize
354
- * @returns {number}
355
- */
356
- jTruncate(fileId, iSize) {
357
- try {
358
- const file = this.mapIdToFile.get(fileId);
359
- const accessHandle = file.accessHandle || file.persistentFile.accessHandle;
360
- accessHandle.truncate(iSize);
361
- return VFS.SQLITE_OK;
362
- } catch (e) {
363
- this.lastError = e;
364
- return VFS.SQLITE_IOERR_TRUNCATE;
365
- }
366
- }
367
-
368
- /**
369
- * @param {number} fileId
370
- * @param {number} flags
371
- * @returns {number}
372
- */
373
- jSync(fileId, flags) {
374
- try {
375
- const file = this.mapIdToFile.get(fileId);
376
- const accessHandle = file.accessHandle || file.persistentFile.accessHandle;
377
- accessHandle.flush();
378
- return VFS.SQLITE_OK;
379
- } catch (e) {
380
- this.lastError = e;
381
- return VFS.SQLITE_IOERR_FSYNC;
382
- }
383
- }
384
-
385
- /**
386
- * @param {number} fileId
387
- * @param {DataView} pSize64
388
- * @returns {number}
389
- */
390
- jFileSize(fileId, pSize64) {
391
- try {
392
- const file = this.mapIdToFile.get(fileId);
393
- const accessHandle = file.accessHandle || file.persistentFile.accessHandle;
394
- const size = accessHandle.getSize();
395
- pSize64.setBigInt64(0, BigInt(size), true);
396
- return VFS.SQLITE_OK;
397
- } catch (e) {
398
- this.lastError = e;
399
- return VFS.SQLITE_IOERR_FSTAT;
400
- }
401
- }
402
-
403
- /**
404
- * @param {number} fileId
405
- * @param {number} lockType
406
- * @returns {number}
407
- */
408
- jLock(fileId, lockType) {
409
- const file = this.mapIdToFile.get(fileId);
410
- if (file.persistentFile.isRequestInProgress) {
411
- file.persistentFile.isLockBusy = true;
412
- return VFS.SQLITE_BUSY;
413
- }
414
-
415
- file.persistentFile.isFileLocked = true;
416
- if (!file.persistentFile.handleLockReleaser) {
417
- // Start listening for notifications from other connections.
418
- // This is before we actually get access handles, but waiting to
419
- // listen until then allows a race condition where notifications
420
- // are missed.
421
- file.persistentFile.handleRequestChannel.onmessage = () => {
422
- this.log?.(`received notification for ${file.path}`);
423
- if (file.persistentFile.isFileLocked) {
424
- // We're still using the access handle, so mark it to be
425
- // released when we're done.
426
- file.persistentFile.isHandleRequested = true;
427
- } else {
428
- // Release the access handles immediately.
429
- this.#releaseAccessHandle(file);
430
- }
431
- file.persistentFile.handleRequestChannel.onmessage = null;
432
- };
433
-
434
- this.#requestAccessHandle(file);
435
- this.log?.('returning SQLITE_BUSY');
436
- file.persistentFile.isLockBusy = true;
437
- return VFS.SQLITE_BUSY;
438
- }
439
- file.persistentFile.isLockBusy = false;
440
- return VFS.SQLITE_OK;
441
- }
442
-
443
- /**
444
- * @param {number} fileId
445
- * @param {number} lockType
446
- * @returns {number}
447
- */
448
- jUnlock(fileId, lockType) {
449
- const file = this.mapIdToFile.get(fileId);
450
- if (lockType === VFS.SQLITE_LOCK_NONE) {
451
- // Don't change any state if this unlock is because xLock returned
452
- // SQLITE_BUSY.
453
- if (!file.persistentFile.isLockBusy) {
454
- if (file.persistentFile.isHandleRequested) {
455
- // Another connection wants the access handle.
456
- this.#releaseAccessHandle(file);
457
- file.persistentFile.isHandleRequested = false;
458
- }
459
- file.persistentFile.isFileLocked = false;
460
- }
461
- }
462
- return VFS.SQLITE_OK;
463
- }
464
-
465
- /**
466
- * @param {number} fileId
467
- * @param {number} op
468
- * @param {DataView} pArg
469
- * @returns {number|Promise<number>}
470
- */
471
- jFileControl(fileId, op, pArg) {
472
- try {
473
- const file = this.mapIdToFile.get(fileId);
474
- switch (op) {
475
- case VFS.SQLITE_FCNTL_PRAGMA:
476
- const key = extractString(pArg, 4);
477
- const value = extractString(pArg, 8);
478
- this.log?.('xFileControl', file.path, 'PRAGMA', key, value);
479
- switch (key.toLowerCase()) {
480
- case 'journal_mode':
481
- if (value && !['off', 'memory', 'delete', 'wal'].includes(value.toLowerCase())) {
482
- throw new Error('journal_mode must be "off", "memory", "delete", or "wal"');
483
- }
484
- break;
485
- }
486
- break;
487
- }
488
- } catch (e) {
489
- this.lastError = e;
490
- return VFS.SQLITE_IOERR;
491
- }
492
- return VFS.SQLITE_NOTFOUND;
493
- }
494
-
495
- /**
496
- * @param {Uint8Array} zBuf
497
- * @returns
498
- */
499
- jGetLastError(zBuf) {
500
- if (this.lastError) {
501
- console.error(this.lastError);
502
- const outputArray = zBuf.subarray(0, zBuf.byteLength - 1);
503
- const { written } = new TextEncoder().encodeInto(this.lastError.message, outputArray);
504
- zBuf[written] = 0;
505
- }
506
- return VFS.SQLITE_OK;
507
- }
508
-
509
- /**
510
- * @param {FileSystemFileHandle} fileHandle
511
- * @returns {Promise<PersistentFile>}
512
- */
513
- async #createPersistentFile(fileHandle) {
514
- const persistentFile = new PersistentFile(fileHandle);
515
- const root = await navigator.storage.getDirectory();
516
- const relativePath = await root.resolve(fileHandle);
517
- const path = `/${relativePath.join('/')}`;
518
- persistentFile.handleRequestChannel = new BroadcastChannel(`ahp:${path}`);
519
- this.persistentFiles.set(path, persistentFile);
520
-
521
- const f = await fileHandle.getFile();
522
- if (f.size) {
523
- this.accessiblePaths.add(path);
524
- }
525
- return persistentFile;
526
- }
527
-
528
- /**
529
- * @param {File} file
530
- */
531
- #requestAccessHandle(file) {
532
- console.assert(!file.persistentFile.handleLockReleaser);
533
- if (!file.persistentFile.isRequestInProgress) {
534
- file.persistentFile.isRequestInProgress = true;
535
- this._module.retryOps.push(
536
- (async () => {
537
- // Acquire the Web Lock.
538
- file.persistentFile.handleLockReleaser = await this.#acquireLock(file.persistentFile);
539
- try {
540
- // Get access handles for the database and releated files in parallel.
541
- this.log?.(`creating access handles for ${file.path}`);
542
- await Promise.all(
543
- DB_RELATED_FILE_SUFFIXES.map(async (suffix) => {
544
- const persistentFile = this.persistentFiles.get(file.path + suffix);
545
- if (persistentFile) {
546
- persistentFile.accessHandle = await persistentFile.fileHandle.createSyncAccessHandle();
547
- }
548
- })
549
- );
550
- } catch (e) {
551
- this.log?.(`failed to create access handles for ${file.path}`, e);
552
- // Close any of the potentially opened access handles
553
- DB_RELATED_FILE_SUFFIXES.forEach(async (suffix) => {
554
- const persistentFile = this.persistentFiles.get(file.path + suffix);
555
- if (persistentFile) {
556
- persistentFile.accessHandle?.close();
557
- }
558
- });
559
- // Release the lock, if we failed here, we'd need to obtain the lock later in order to retry
560
- file.persistentFile.handleLockReleaser();
561
- throw e;
562
- } finally {
563
- file.persistentFile.isRequestInProgress = false;
564
- }
565
- })()
566
- );
567
- return this._module.retryOps.at(-1);
568
- }
569
- return Promise.resolve();
570
- }
571
-
572
- /**
573
- * @param {File} file
574
- */
575
- async #releaseAccessHandle(file) {
576
- DB_RELATED_FILE_SUFFIXES.forEach(async (suffix) => {
577
- const persistentFile = this.persistentFiles.get(file.path + suffix);
578
- if (persistentFile) {
579
- persistentFile.accessHandle?.close();
580
- persistentFile.accessHandle = null;
581
- }
582
- });
583
- this.log?.(`access handles closed for ${file.path}`);
584
-
585
- file.persistentFile.handleLockReleaser?.();
586
- file.persistentFile.handleLockReleaser = null;
587
- this.log?.(`lock released for ${file.path}`);
588
- }
589
-
590
- /**
591
- * @param {PersistentFile} persistentFile
592
- * @returns {Promise<function>} lock releaser
593
- */
594
- #acquireLock(persistentFile) {
595
- return new Promise((resolve) => {
596
- // Tell other connections we want the access handle.
597
- const lockName = persistentFile.handleRequestChannel.name;
598
- const notify = () => {
599
- this.log?.(`notifying for ${lockName}`);
600
- persistentFile.handleRequestChannel.postMessage(null);
601
- };
602
- const notifyId = setInterval(notify, LOCK_NOTIFY_INTERVAL);
603
- setTimeout(notify);
604
-
605
- this.log?.(`lock requested: ${lockName}`);
606
- navigator.locks.request(lockName, (lock) => {
607
- // We have the lock. Stop asking other connections for it.
608
- this.log?.(`lock acquired: ${lockName}`, lock);
609
- clearInterval(notifyId);
610
- return new Promise(resolve);
611
- });
612
- });
613
- }
614
- }
615
-
616
- function extractString(dataView, offset) {
617
- const p = dataView.getUint32(offset, true);
618
- if (p) {
619
- const chars = new Uint8Array(dataView.buffer, p);
620
- return new TextDecoder().decode(chars.subarray(0, chars.indexOf(0)));
621
- }
622
- return null;
623
- }
File without changes