@livestore/wa-sqlite 1.0.1-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -0
  3. package/dist/wa-sqlite-async.mjs +16 -0
  4. package/dist/wa-sqlite-async.wasm +0 -0
  5. package/dist/wa-sqlite-jspi.mjs +16 -0
  6. package/dist/wa-sqlite-jspi.wasm +0 -0
  7. package/dist/wa-sqlite.mjs +16 -0
  8. package/dist/wa-sqlite.wasm +0 -0
  9. package/package.json +45 -0
  10. package/src/FacadeVFS.js +508 -0
  11. package/src/VFS.js +222 -0
  12. package/src/WebLocksMixin.js +412 -0
  13. package/src/examples/AccessHandlePoolVFS.js +458 -0
  14. package/src/examples/IDBBatchAtomicVFS.js +820 -0
  15. package/src/examples/IDBMirrorVFS.js +875 -0
  16. package/src/examples/MemoryAsyncVFS.js +100 -0
  17. package/src/examples/MemoryVFS.js +176 -0
  18. package/src/examples/OPFSAdaptiveVFS.js +437 -0
  19. package/src/examples/OPFSAnyContextVFS.js +300 -0
  20. package/src/examples/OPFSCoopSyncVFS.js +590 -0
  21. package/src/examples/OPFSPermutedVFS.js +1214 -0
  22. package/src/examples/README.md +89 -0
  23. package/src/examples/tag.js +82 -0
  24. package/src/sqlite-api.js +914 -0
  25. package/src/sqlite-constants.js +275 -0
  26. package/src/types/globals.d.ts +60 -0
  27. package/src/types/index.d.ts +1302 -0
  28. package/src/types/tsconfig.json +6 -0
  29. package/test/AccessHandlePoolVFS.test.js +27 -0
  30. package/test/IDBBatchAtomicVFS.test.js +97 -0
  31. package/test/IDBMirrorVFS.test.js +27 -0
  32. package/test/MemoryAsyncVFS.test.js +27 -0
  33. package/test/MemoryVFS.test.js +27 -0
  34. package/test/OPFSAdaptiveVFS.test.js +27 -0
  35. package/test/OPFSAnyContextVFS.test.js +27 -0
  36. package/test/OPFSCoopSyncVFS.test.js +27 -0
  37. package/test/OPFSPermutedVFS.test.js +27 -0
  38. package/test/TestContext.js +96 -0
  39. package/test/WebLocksMixin.test.js +521 -0
  40. package/test/api.test.js +49 -0
  41. package/test/api_exec.js +89 -0
  42. package/test/api_misc.js +63 -0
  43. package/test/api_statements.js +426 -0
  44. package/test/callbacks.test.js +373 -0
  45. package/test/sql.test.js +64 -0
  46. package/test/sql_0001.js +49 -0
  47. package/test/sql_0002.js +52 -0
  48. package/test/sql_0003.js +83 -0
  49. package/test/sql_0004.js +81 -0
  50. package/test/sql_0005.js +76 -0
  51. package/test/test-worker.js +204 -0
  52. package/test/vfs_xAccess.js +2 -0
  53. package/test/vfs_xClose.js +52 -0
  54. package/test/vfs_xOpen.js +91 -0
  55. package/test/vfs_xRead.js +38 -0
  56. package/test/vfs_xWrite.js +36 -0
@@ -0,0 +1,458 @@
1
+ // Copyright 2023 Roy T. Hashimoto. All Rights Reserved.
2
+ import { FacadeVFS } from '../FacadeVFS.js';
3
+ import * as VFS from '../VFS.js';
4
+
5
+ const SECTOR_SIZE = 4096;
6
+
7
+ // Each OPFS file begins with a fixed-size header with metadata. The
8
+ // contents of the file follow immediately after the header.
9
+ const HEADER_MAX_PATH_SIZE = 512;
10
+ const HEADER_FLAGS_SIZE = 4;
11
+ const HEADER_DIGEST_SIZE = 8;
12
+ const HEADER_CORPUS_SIZE = HEADER_MAX_PATH_SIZE + HEADER_FLAGS_SIZE;
13
+ const HEADER_OFFSET_FLAGS = HEADER_MAX_PATH_SIZE;
14
+ const HEADER_OFFSET_DIGEST = HEADER_CORPUS_SIZE;
15
+ const HEADER_OFFSET_DATA = SECTOR_SIZE;
16
+
17
+ // These file types are expected to persist in the file system outside
18
+ // a session. Other files will be removed on VFS start.
19
+ const PERSISTENT_FILE_TYPES =
20
+ VFS.SQLITE_OPEN_MAIN_DB |
21
+ VFS.SQLITE_OPEN_MAIN_JOURNAL |
22
+ VFS.SQLITE_OPEN_SUPER_JOURNAL |
23
+ VFS.SQLITE_OPEN_WAL;
24
+
25
+ const DEFAULT_CAPACITY = 6;
26
+
27
+ /**
28
+ * This VFS uses the updated Access Handle API with all synchronous methods
29
+ * on FileSystemSyncAccessHandle (instead of just read and write). It will
30
+ * work with the regular SQLite WebAssembly build, i.e. the one without
31
+ * Asyncify.
32
+ */
33
+ export class AccessHandlePoolVFS extends FacadeVFS {
34
+ log = null; //function(...args) { console.log(`[${contextName}]`, ...args) };
35
+
36
+ // All the OPFS files the VFS uses are contained in one flat directory
37
+ // specified in the constructor. No other files should be written here.
38
+ #directoryPath;
39
+ #directoryHandle;
40
+
41
+ // The OPFS files all have randomly-generated names that do not match
42
+ // the SQLite files whose data they contain. This map links those names
43
+ // with their respective OPFS access handles.
44
+ #mapAccessHandleToName = new Map();
45
+
46
+ // When a SQLite file is associated with an OPFS file, that association
47
+ // is kept in #mapPathToAccessHandle. Each access handle is in exactly
48
+ // one of #mapPathToAccessHandle or #availableAccessHandles.
49
+ #mapPathToAccessHandle = new Map();
50
+ #availableAccessHandles = new Set();
51
+
52
+ #mapIdToFile = new Map();
53
+
54
+ static async create(name, module) {
55
+ const vfs = new AccessHandlePoolVFS(name, module);
56
+ await vfs.isReady();
57
+ return vfs;
58
+ }
59
+
60
+ constructor(name, module) {
61
+ super(name, module);
62
+ this.#directoryPath = name;
63
+ }
64
+
65
+ /**
66
+ * @param {string?} zName
67
+ * @param {number} fileId
68
+ * @param {number} flags
69
+ * @param {DataView} pOutFlags
70
+ * @returns {number}
71
+ */
72
+ jOpen(zName, fileId, flags, pOutFlags) {
73
+ try {
74
+ // First try to open a path that already exists in the file system.
75
+ const path = zName ? this.#getPath(zName) : Math.random().toString(36);
76
+ let accessHandle = this.#mapPathToAccessHandle.get(path);
77
+ if (!accessHandle && (flags & VFS.SQLITE_OPEN_CREATE)) {
78
+ // File not found so try to create it.
79
+ if (this.getSize() < this.getCapacity()) {
80
+ // Choose an unassociated OPFS file from the pool.
81
+ ([accessHandle] = this.#availableAccessHandles.keys());
82
+ this.#setAssociatedPath(accessHandle, path, flags);
83
+ } else {
84
+ // Out of unassociated files. This can be fixed by calling
85
+ // addCapacity() from the application.
86
+ throw new Error('cannot create file');
87
+ }
88
+ }
89
+ if (!accessHandle) {
90
+ throw new Error('file not found');
91
+ }
92
+ // Subsequent methods are only passed the fileId, so make sure we have
93
+ // a way to get the file resources.
94
+ const file = { path, flags, accessHandle };
95
+ this.#mapIdToFile.set(fileId, file);
96
+
97
+ pOutFlags.setInt32(0, flags, true);
98
+ return VFS.SQLITE_OK;
99
+ } catch (e) {
100
+ console.error(e.message);
101
+ return VFS.SQLITE_CANTOPEN;
102
+ }
103
+ }
104
+
105
+ /**
106
+ * @param {number} fileId
107
+ * @returns {number}
108
+ */
109
+ jClose(fileId) {
110
+ const file = this.#mapIdToFile.get(fileId);
111
+ if (file) {
112
+ file.accessHandle.flush();
113
+ this.#mapIdToFile.delete(fileId);
114
+ if (file.flags & VFS.SQLITE_OPEN_DELETEONCLOSE) {
115
+ this.#deletePath(file.path);
116
+ }
117
+ }
118
+ return VFS.SQLITE_OK;
119
+ }
120
+
121
+ /**
122
+ * @param {number} fileId
123
+ * @param {Uint8Array} pData
124
+ * @param {number} iOffset
125
+ * @returns {number}
126
+ */
127
+ jRead(fileId, pData, iOffset) {
128
+ const file = this.#mapIdToFile.get(fileId);
129
+ const nBytes = file.accessHandle.read(
130
+ pData.subarray(),
131
+ { at: HEADER_OFFSET_DATA + iOffset });
132
+ if (nBytes < pData.byteLength) {
133
+ pData.fill(0, nBytes, pData.byteLength);
134
+ return VFS.SQLITE_IOERR_SHORT_READ;
135
+ }
136
+ return VFS.SQLITE_OK;
137
+ }
138
+
139
+ /**
140
+ * @param {number} fileId
141
+ * @param {Uint8Array} pData
142
+ * @param {number} iOffset
143
+ * @returns {number}
144
+ */
145
+ jWrite(fileId, pData, iOffset) {
146
+ const file = this.#mapIdToFile.get(fileId);
147
+ const nBytes = file.accessHandle.write(
148
+ pData.subarray(),
149
+ { at: HEADER_OFFSET_DATA + iOffset });
150
+ return nBytes === pData.byteLength ? VFS.SQLITE_OK : VFS.SQLITE_IOERR;
151
+ }
152
+
153
+ /**
154
+ * @param {number} fileId
155
+ * @param {number} iSize
156
+ * @returns {number}
157
+ */
158
+ jTruncate(fileId, iSize) {
159
+ const file = this.#mapIdToFile.get(fileId);
160
+ file.accessHandle.truncate(HEADER_OFFSET_DATA + iSize);
161
+ return VFS.SQLITE_OK;
162
+ }
163
+
164
+ /**
165
+ * @param {number} fileId
166
+ * @param {number} flags
167
+ * @returns {number}
168
+ */
169
+ jSync(fileId, flags) {
170
+ const file = this.#mapIdToFile.get(fileId);
171
+ file.accessHandle.flush();
172
+ return VFS.SQLITE_OK;
173
+ }
174
+
175
+ /**
176
+ * @param {number} fileId
177
+ * @param {DataView} pSize64
178
+ * @returns {number}
179
+ */
180
+ jFileSize(fileId, pSize64) {
181
+ const file = this.#mapIdToFile.get(fileId);
182
+ const size = file.accessHandle.getSize() - HEADER_OFFSET_DATA;
183
+ pSize64.setBigInt64(0, BigInt(size), true);
184
+ return VFS.SQLITE_OK;
185
+ }
186
+
187
+ jSectorSize(fileId) {
188
+ return SECTOR_SIZE;
189
+ }
190
+
191
+ jDeviceCharacteristics(fileId) {
192
+ return VFS.SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
193
+ }
194
+
195
+ /**
196
+ * @param {string} zName
197
+ * @param {number} flags
198
+ * @param {DataView} pResOut
199
+ * @returns {number}
200
+ */
201
+ jAccess(zName, flags, pResOut) {
202
+ const path = this.#getPath(zName);
203
+ pResOut.setInt32(0, this.#mapPathToAccessHandle.has(path) ? 1 : 0, true);
204
+ return VFS.SQLITE_OK;
205
+ }
206
+
207
+ /**
208
+ * @param {string} zName
209
+ * @param {number} syncDir
210
+ * @returns {number}
211
+ */
212
+ jDelete(zName, syncDir) {
213
+ const path = this.#getPath(zName);
214
+ this.#deletePath(path);
215
+ return VFS.SQLITE_OK;
216
+ }
217
+
218
+ async close() {
219
+ await this.#releaseAccessHandles();
220
+ }
221
+
222
+ async isReady() {
223
+ if (!this.#directoryHandle) {
224
+ // All files are stored in a single directory.
225
+ let handle = await navigator.storage.getDirectory();
226
+ for (const d of this.#directoryPath.split('/')) {
227
+ if (d) {
228
+ handle = await handle.getDirectoryHandle(d, { create: true });
229
+ }
230
+ }
231
+ this.#directoryHandle = handle;
232
+
233
+ await this.#acquireAccessHandles();
234
+ if (this.getCapacity() === 0) {
235
+ await this.addCapacity(DEFAULT_CAPACITY);
236
+ }
237
+ }
238
+ return true;
239
+ }
240
+
241
+ /**
242
+ * Returns the number of SQLite files in the file system.
243
+ * @returns {number}
244
+ */
245
+ getSize() {
246
+ return this.#mapPathToAccessHandle.size;
247
+ }
248
+
249
+ /**
250
+ * Returns the maximum number of SQLite files the file system can hold.
251
+ * @returns {number}
252
+ */
253
+ getCapacity() {
254
+ return this.#mapAccessHandleToName.size;
255
+ }
256
+
257
+ /**
258
+ * Increase the capacity of the file system by n.
259
+ * @param {number} n
260
+ * @returns {Promise<number>}
261
+ */
262
+ async addCapacity(n) {
263
+ for (let i = 0; i < n; ++i) {
264
+ const name = Math.random().toString(36).replace('0.', '');
265
+ const handle = await this.#directoryHandle.getFileHandle(name, { create: true });
266
+ const accessHandle = await handle.createSyncAccessHandle();
267
+ this.#mapAccessHandleToName.set(accessHandle, name);
268
+
269
+ this.#setAssociatedPath(accessHandle, '', 0);
270
+ }
271
+ return n;
272
+ }
273
+
274
+ /**
275
+ * Decrease the capacity of the file system by n. The capacity cannot be
276
+ * decreased to fewer than the current number of SQLite files in the
277
+ * file system.
278
+ * @param {number} n
279
+ * @returns {Promise<number>}
280
+ */
281
+ async removeCapacity(n) {
282
+ let nRemoved = 0;
283
+ for (const accessHandle of Array.from(this.#availableAccessHandles)) {
284
+ if (nRemoved == n || this.getSize() === this.getCapacity()) return nRemoved;
285
+
286
+ const name = this.#mapAccessHandleToName.get(accessHandle);
287
+ await accessHandle.close();
288
+ await this.#directoryHandle.removeEntry(name);
289
+ this.#mapAccessHandleToName.delete(accessHandle);
290
+ this.#availableAccessHandles.delete(accessHandle);
291
+ ++nRemoved;
292
+ }
293
+ return nRemoved;
294
+ }
295
+
296
+ async #acquireAccessHandles() {
297
+ // Enumerate all the files in the directory.
298
+ const files = [];
299
+ for await (const [name, handle] of this.#directoryHandle) {
300
+ if (handle.kind === 'file') {
301
+ files.push([name, handle]);
302
+ }
303
+ }
304
+
305
+ // Open access handles in parallel, separating associated and unassociated.
306
+ await Promise.all(files.map(async ([name, handle]) => {
307
+ const accessHandle = await handle.createSyncAccessHandle();
308
+ this.#mapAccessHandleToName.set(accessHandle, name);
309
+ const path = this.#getAssociatedPath(accessHandle);
310
+ if (path) {
311
+ this.#mapPathToAccessHandle.set(path, accessHandle);
312
+ } else {
313
+ this.#availableAccessHandles.add(accessHandle);
314
+ }
315
+ }));
316
+ }
317
+
318
+ #releaseAccessHandles() {
319
+ for (const accessHandle of this.#mapAccessHandleToName.keys()) {
320
+ accessHandle.close();
321
+ }
322
+ this.#mapAccessHandleToName.clear();
323
+ this.#mapPathToAccessHandle.clear();
324
+ this.#availableAccessHandles.clear();
325
+ }
326
+
327
+ /**
328
+ * Read and return the associated path from an OPFS file header.
329
+ * Empty string is returned for an unassociated OPFS file.
330
+ * @param accessHandle FileSystemSyncAccessHandle
331
+ * @returns {string} path or empty string
332
+ */
333
+ #getAssociatedPath(accessHandle) {
334
+ // Read the path and digest of the path from the file.
335
+ const corpus = new Uint8Array(HEADER_CORPUS_SIZE);
336
+ accessHandle.read(corpus, { at: 0 })
337
+
338
+ // Delete files not expected to be present.
339
+ const dataView = new DataView(corpus.buffer, corpus.byteOffset);
340
+ const flags = dataView.getUint32(HEADER_OFFSET_FLAGS);
341
+ if (corpus[0] &&
342
+ ((flags & VFS.SQLITE_OPEN_DELETEONCLOSE) ||
343
+ (flags & PERSISTENT_FILE_TYPES) === 0)) {
344
+ console.warn(`Remove file with unexpected flags ${flags.toString(16)}`);
345
+ this.#setAssociatedPath(accessHandle, '', 0);
346
+ return '';
347
+ }
348
+
349
+ const fileDigest = new Uint32Array(HEADER_DIGEST_SIZE / 4);
350
+ accessHandle.read(fileDigest, { at: HEADER_OFFSET_DIGEST });
351
+
352
+ // Verify the digest.
353
+ const computedDigest = this.#computeDigest(corpus);
354
+ if (fileDigest.every((value, i) => value === computedDigest[i])) {
355
+ // Good digest. Decode the null-terminated path string.
356
+ const pathBytes = corpus.findIndex(value => value === 0);
357
+ if (pathBytes === 0) {
358
+ // Ensure that unassociated files are empty. Unassociated files are
359
+ // truncated in #setAssociatedPath after the header is written. If
360
+ // an interruption occurs right before the truncation then garbage
361
+ // may remain in the file.
362
+ accessHandle.truncate(HEADER_OFFSET_DATA);
363
+ }
364
+ return new TextDecoder().decode(corpus.subarray(0, pathBytes));
365
+ } else {
366
+ // Bad digest. Repair this header.
367
+ console.warn('Disassociating file with bad digest.');
368
+ this.#setAssociatedPath(accessHandle, '', 0);
369
+ return '';
370
+ }
371
+ }
372
+
373
+ /**
374
+ * Set the path on an OPFS file header.
375
+ * @param accessHandle FileSystemSyncAccessHandle
376
+ * @param {string} path
377
+ * @param {number} flags
378
+ */
379
+ #setAssociatedPath(accessHandle, path, flags) {
380
+ // Convert the path string to UTF-8.
381
+ const corpus = new Uint8Array(HEADER_CORPUS_SIZE);
382
+ const encodedResult = new TextEncoder().encodeInto(path, corpus);
383
+ if (encodedResult.written >= HEADER_MAX_PATH_SIZE) {
384
+ throw new Error('path too long');
385
+ }
386
+
387
+ // Add the creation flags.
388
+ const dataView = new DataView(corpus.buffer, corpus.byteOffset);
389
+ dataView.setUint32(HEADER_OFFSET_FLAGS, flags);
390
+
391
+ // Write the OPFS file header, including the digest.
392
+ const digest = this.#computeDigest(corpus);
393
+ accessHandle.write(corpus, { at: 0 });
394
+ accessHandle.write(digest, { at: HEADER_OFFSET_DIGEST });
395
+ accessHandle.flush();
396
+
397
+ if (path) {
398
+ this.#mapPathToAccessHandle.set(path, accessHandle);
399
+ this.#availableAccessHandles.delete(accessHandle);
400
+ } else {
401
+ // This OPFS file doesn't represent any SQLite file so it doesn't
402
+ // need to keep any data.
403
+ accessHandle.truncate(HEADER_OFFSET_DATA);
404
+ this.#availableAccessHandles.add(accessHandle);
405
+ }
406
+ }
407
+
408
+ /**
409
+ * We need a synchronous digest function so can't use WebCrypto.
410
+ * Adapted from https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
411
+ * @param {Uint8Array} corpus
412
+ * @returns {ArrayBuffer} 64-bit digest
413
+ */
414
+ #computeDigest(corpus) {
415
+ if (!corpus[0]) {
416
+ // Optimization for deleted file.
417
+ return new Uint32Array([0xfecc5f80, 0xaccec037]);
418
+ }
419
+
420
+ let h1 = 0xdeadbeef;
421
+ let h2 = 0x41c6ce57;
422
+
423
+ for (const value of corpus) {
424
+ h1 = Math.imul(h1 ^ value, 2654435761);
425
+ h2 = Math.imul(h2 ^ value, 1597334677);
426
+ }
427
+
428
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
429
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
430
+
431
+ return new Uint32Array([h1 >>> 0, h2 >>> 0]);
432
+ };
433
+
434
+ /**
435
+ * Convert a bare filename, path, or URL to a UNIX-style path.
436
+ * @param {string|URL} nameOrURL
437
+ * @returns {string} path
438
+ */
439
+ #getPath(nameOrURL) {
440
+ const url = typeof nameOrURL === 'string' ?
441
+ new URL(nameOrURL, 'file://localhost/') :
442
+ nameOrURL;
443
+ return url.pathname;
444
+ }
445
+
446
+ /**
447
+ * Remove the association between a path and an OPFS file.
448
+ * @param {string} path
449
+ */
450
+ #deletePath(path) {
451
+ const accessHandle = this.#mapPathToAccessHandle.get(path);
452
+ if (accessHandle) {
453
+ // Un-associate the SQLite path from the OPFS file.
454
+ this.#mapPathToAccessHandle.delete(path);
455
+ this.#setAssociatedPath(accessHandle, '', 0);
456
+ }
457
+ }
458
+ }