@powersync/web 0.0.0-dev-20251201150812 → 0.0.0-dev-20251203144301
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.umd.js +2415 -58
- package/dist/index.umd.js.map +1 -1
- package/dist/worker/SharedSyncImplementation.umd.js +1884 -39
- package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
- package/dist/worker/WASQLiteDB.umd.js +1809 -8
- package/dist/worker/WASQLiteDB.umd.js.map +1 -1
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js +0 -1203
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js.map +1 -1
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js +0 -1203
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js.map +1 -1
- package/lib/package.json +2 -2
- package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.d.ts +4 -1
- package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.js +52 -28
- package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.js +3 -3
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +1 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +2 -2
- package/lib/src/worker/db/WASQLiteDB.worker.js +0 -1
- package/lib/src/worker/db/opfs.d.ts +96 -0
- package/lib/src/worker/db/opfs.js +582 -0
- package/lib/src/worker/sync/SharedSyncImplementation.js +23 -4
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/db/adapters/LockedAsyncDatabaseAdapter.ts +71 -48
- package/src/db/adapters/wa-sqlite/WASQLiteConnection.ts +3 -4
- package/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +3 -3
- package/src/worker/db/WASQLiteDB.worker.ts +0 -2
- package/src/worker/db/opfs.ts +623 -0
- package/src/worker/sync/SharedSyncImplementation.ts +29 -8
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js +0 -1813
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js.map +0 -1
- package/lib/src/worker/sync/MockSyncService.d.ts +0 -2
- package/lib/src/worker/sync/MockSyncService.js +0 -3
- package/lib/src/worker/sync/MockSyncServiceTypes.d.ts +0 -101
- package/lib/src/worker/sync/MockSyncServiceTypes.js +0 -1
- package/lib/src/worker/sync/MockSyncServiceWorker.d.ts +0 -56
- package/lib/src/worker/sync/MockSyncServiceWorker.js +0 -369
- package/src/worker/sync/MockSyncService.ts +0 -3
- package/src/worker/sync/MockSyncServiceTypes.ts +0 -71
- package/src/worker/sync/MockSyncServiceWorker.ts +0 -406
|
@@ -0,0 +1,623 @@
|
|
|
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
|
+
}
|
|
@@ -525,12 +525,16 @@ export class SharedSyncImplementation extends BaseObserver<SharedSyncImplementat
|
|
|
525
525
|
|
|
526
526
|
client.closeListeners.push(closeListener);
|
|
527
527
|
|
|
528
|
-
const workerPort = await withAbort(
|
|
529
|
-
(
|
|
530
|
-
|
|
531
|
-
|
|
528
|
+
const workerPort = await withAbort({
|
|
529
|
+
action: () => client.clientProvider.getDBWorkerPort(),
|
|
530
|
+
signal: abortController.signal,
|
|
531
|
+
cleanupOnAbort: (port) => {
|
|
532
|
+
port.close();
|
|
532
533
|
}
|
|
533
|
-
)
|
|
534
|
+
}).catch((ex) => {
|
|
535
|
+
removeCloseListener();
|
|
536
|
+
throw ex;
|
|
537
|
+
});
|
|
534
538
|
|
|
535
539
|
const remote = Comlink.wrap<OpenAsyncDatabaseConnection>(workerPort);
|
|
536
540
|
const identifier = this.syncParams!.dbParams.dbFilename;
|
|
@@ -541,7 +545,13 @@ export class SharedSyncImplementation extends BaseObserver<SharedSyncImplementat
|
|
|
541
545
|
* We typically execute the closeListeners using the portMutex in a different context.
|
|
542
546
|
* We can't rely on the closeListeners to abort the operation if the tab is closed.
|
|
543
547
|
*/
|
|
544
|
-
const db = await withAbort(
|
|
548
|
+
const db = await withAbort({
|
|
549
|
+
action: () => remote(this.syncParams!.dbParams),
|
|
550
|
+
signal: abortController.signal,
|
|
551
|
+
cleanupOnAbort: (db) => {
|
|
552
|
+
db.close();
|
|
553
|
+
}
|
|
554
|
+
}).finally(() => {
|
|
545
555
|
// We can remove the close listener here since we no longer need it past this point.
|
|
546
556
|
removeCloseListener();
|
|
547
557
|
});
|
|
@@ -588,7 +598,12 @@ export class SharedSyncImplementation extends BaseObserver<SharedSyncImplementat
|
|
|
588
598
|
/**
|
|
589
599
|
* Runs the action with an abort controller.
|
|
590
600
|
*/
|
|
591
|
-
function withAbort<T>(
|
|
601
|
+
function withAbort<T>(options: {
|
|
602
|
+
action: () => Promise<T>;
|
|
603
|
+
signal: AbortSignal;
|
|
604
|
+
cleanupOnAbort?: (result: T) => void;
|
|
605
|
+
}): Promise<T> {
|
|
606
|
+
const { action, signal, cleanupOnAbort } = options;
|
|
592
607
|
return new Promise((resolve, reject) => {
|
|
593
608
|
if (signal.aborted) {
|
|
594
609
|
reject(new AbortOperation('Operation aborted by abort controller'));
|
|
@@ -608,7 +623,13 @@ function withAbort<T>(action: () => Promise<T>, signal: AbortSignal): Promise<T>
|
|
|
608
623
|
}
|
|
609
624
|
|
|
610
625
|
action()
|
|
611
|
-
.then((data) =>
|
|
626
|
+
.then((data) => {
|
|
627
|
+
// We already rejected due to the abort, allow for cleanup
|
|
628
|
+
if (signal.aborted) {
|
|
629
|
+
return completePromise(() => cleanupOnAbort?.(data));
|
|
630
|
+
}
|
|
631
|
+
completePromise(() => resolve(data));
|
|
632
|
+
})
|
|
612
633
|
.catch((e) => completePromise(() => reject(e)));
|
|
613
634
|
});
|
|
614
635
|
}
|