@livestore/wa-sqlite 1.0.5-dev.2 → 1.0.8-dev.2

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.
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livestore/wa-sqlite",
3
- "version": "1.0.5-dev.2",
3
+ "version": "1.0.8-dev.2",
4
4
  "type": "module",
5
5
  "main": "src/sqlite-api.js",
6
6
  "types": "src/types/index.d.ts",
@@ -18,8 +18,8 @@
18
18
  "devDependencies": {
19
19
  "@types/jasmine": "^5.1.4",
20
20
  "@web/dev-server": "^0.4.6",
21
- "@web/test-runner": "^0.18.2",
22
- "@web/test-runner-core": "^0.13.3",
21
+ "@web/test-runner": "^0.20.0",
22
+ "@web/test-runner-core": "^0.13.4",
23
23
  "comlink": "^4.4.1",
24
24
  "jasmine-core": "^4.5.0",
25
25
  "monaco-editor": "^0.34.1",
package/src/FacadeVFS.js CHANGED
@@ -405,64 +405,30 @@ export class FacadeVFS extends VFS.Base {
405
405
 
406
406
  /**
407
407
  * Wrapped DataView for pointer arguments.
408
- * Pointers to a single value are passed using DataView. A Proxy
409
- * wrapper prevents use of incorrect type or endianness.
408
+ * Pointers to a single value are passed using a DataView-like class.
409
+ * This wrapper class prevents use of incorrect type or endianness, and
410
+ * reacquires the underlying buffer when the WebAssembly memory is resized.
410
411
  * @param {'Int32'|'BigInt64'} type
411
412
  * @param {number} byteOffset
412
413
  * @returns {DataView}
413
414
  */
414
415
  #makeTypedDataView(type, byteOffset) {
415
- const byteLength = type === 'Int32' ? 4 : 8;
416
- const getter = `get${type}`;
417
- const setter = `set${type}`;
418
- const makeDataView = () => new DataView(
419
- this._module.HEAPU8.buffer,
420
- this._module.HEAPU8.byteOffset + byteOffset,
421
- byteLength);
422
- let dataView = makeDataView();
423
- return new Proxy(dataView, {
424
- get(_, prop) {
425
- if (dataView.buffer.byteLength === 0) {
426
- // WebAssembly memory resize detached the buffer.
427
- dataView = makeDataView();
428
- }
429
- if (prop === getter) {
430
- return function(byteOffset, littleEndian) {
431
- if (!littleEndian) throw new Error('must be little endian');
432
- return dataView[prop](byteOffset, littleEndian);
433
- }
434
- }
435
- if (prop === setter) {
436
- return function(byteOffset, value, littleEndian) {
437
- if (!littleEndian) throw new Error('must be little endian');
438
- return dataView[prop](byteOffset, value, littleEndian);
439
- }
440
- }
441
- if (typeof prop === 'string' && (prop.match(/^(get)|(set)/))) {
442
- throw new Error('invalid type');
443
- }
444
- const result = dataView[prop];
445
- return typeof result === 'function' ? result.bind(dataView) : result;
446
- }
447
- });
416
+ // @ts-ignore
417
+ return new DataViewProxy(this._module, byteOffset, type);
448
418
  }
449
419
 
450
420
  /**
421
+ * Wrapped Uint8Array for buffer arguments.
422
+ * Memory blocks are passed as a Uint8Array-like class. This wrapper
423
+ * class reacquires the underlying buffer when the WebAssembly memory
424
+ * is resized.
451
425
  * @param {number} byteOffset
452
426
  * @param {number} byteLength
427
+ * @returns {Uint8Array}
453
428
  */
454
429
  #makeDataArray(byteOffset, byteLength) {
455
- let target = this._module.HEAPU8.subarray(byteOffset, byteOffset + byteLength);
456
- return new Proxy(target, {
457
- get: (_, prop, receiver) => {
458
- if (target.buffer.byteLength === 0) {
459
- // WebAssembly memory resize detached the buffer.
460
- target = this._module.HEAPU8.subarray(byteOffset, byteOffset + byteLength);
461
- }
462
- const result = target[prop];
463
- return typeof result === 'function' ? result.bind(target) : result;
464
- }
465
- });
430
+ // @ts-ignore
431
+ return new Uint8ArrayProxy(this._module, byteOffset, byteLength);
466
432
  }
467
433
 
468
434
  #decodeFilename(zName, flags) {
@@ -506,3 +472,210 @@ export class FacadeVFS extends VFS.Base {
506
472
  function delegalize(lo32, hi32) {
507
473
  return (hi32 * 0x100000000) + lo32 + (lo32 < 0 ? 2**32 : 0);
508
474
  }
475
+
476
+ // This class provides a Uint8Array-like interface for a WebAssembly memory
477
+ // buffer. It is used to access memory blocks passed as arguments to
478
+ // xRead, xWrite, etc. The class reacquires the underlying buffer when the
479
+ // WebAssembly memory is resized, which can happen when the memory is
480
+ // detached and resized by the WebAssembly module.
481
+ //
482
+ // Note that although this class implements the same methods as Uint8Array,
483
+ // it is not a real Uint8Array and passing it to functions that expect
484
+ // a Uint8Array may not work. Use subarray() to get a real Uint8Array
485
+ // if needed.
486
+ class Uint8ArrayProxy {
487
+ #module;
488
+
489
+ #_array = new Uint8Array()
490
+ get #array() {
491
+ if (this.#_array.buffer.byteLength === 0) {
492
+ // WebAssembly memory resize detached the buffer so re-create the
493
+ // array with the new buffer.
494
+ this.#_array = this.#module.HEAPU8.subarray(
495
+ this.byteOffset,
496
+ this.byteOffset + this.byteLength);
497
+ }
498
+ return this.#_array;
499
+ }
500
+
501
+ /**
502
+ * @param {*} module
503
+ * @param {number} byteOffset
504
+ * @param {number} byteLength
505
+ */
506
+ constructor(module, byteOffset, byteLength) {
507
+ this.#module = module;
508
+ this.byteOffset = byteOffset;
509
+ this.length = this.byteLength = byteLength;
510
+ }
511
+
512
+ get buffer() {
513
+ return this.#array.buffer;
514
+ }
515
+
516
+ at(index) {
517
+ return this.#array.at(index);
518
+ }
519
+ copyWithin(target, start, end) {
520
+ this.#array.copyWithin(target, start, end);
521
+ }
522
+ entries() {
523
+ return this.#array.entries();
524
+ }
525
+ every(predicate) {
526
+ return this.#array.every(predicate);
527
+ }
528
+ fill(value, start, end) {
529
+ this.#array.fill(value, start, end);
530
+ }
531
+ filter(predicate) {
532
+ return this.#array.filter(predicate);
533
+ }
534
+ find(predicate) {
535
+ return this.#array.find(predicate);
536
+ }
537
+ findIndex(predicate) {
538
+ return this.#array.findIndex(predicate);
539
+ }
540
+ findLast(predicate) {
541
+ return this.#array.findLast(predicate);
542
+ }
543
+ findLastIndex(predicate) {
544
+ return this.#array.findLastIndex(predicate);
545
+ }
546
+ forEach(callback) {
547
+ this.#array.forEach(callback);
548
+ }
549
+ includes(value, start) {
550
+ return this.#array.includes(value, start);
551
+ }
552
+ indexOf(value, start) {
553
+ return this.#array.indexOf(value, start);
554
+ }
555
+ join(separator) {
556
+ return this.#array.join(separator);
557
+ }
558
+ keys() {
559
+ return this.#array.keys();
560
+ }
561
+ lastIndexOf(value, start) {
562
+ return this.#array.lastIndexOf(value, start);
563
+ }
564
+ map(callback) {
565
+ return this.#array.map(callback);
566
+ }
567
+ reduce(callback, initialValue) {
568
+ return this.#array.reduce(callback, initialValue);
569
+ }
570
+ reduceRight(callback, initialValue) {
571
+ return this.#array.reduceRight(callback, initialValue);
572
+ }
573
+ reverse() {
574
+ this.#array.reverse();
575
+ }
576
+ set(array, offset) {
577
+ this.#array.set(array, offset);
578
+ }
579
+ slice(start, end) {
580
+ return this.#array.slice(start, end);
581
+ }
582
+ some(predicate) {
583
+ return this.#array.some(predicate);
584
+ }
585
+ sort(compareFn) {
586
+ this.#array.sort(compareFn);
587
+ }
588
+ subarray(begin, end) {
589
+ return this.#array.subarray(begin, end);
590
+ }
591
+ toLocaleString(locales, options) {
592
+ // @ts-ignore
593
+ return this.#array.toLocaleString(locales, options);
594
+ }
595
+ toReversed() {
596
+ return this.#array.toReversed();
597
+ }
598
+ toSorted(compareFn) {
599
+ return this.#array.toSorted(compareFn);
600
+ }
601
+ toString() {
602
+ return this.#array.toString();
603
+ }
604
+ values() {
605
+ return this.#array.values();
606
+ }
607
+ with(index, value) {
608
+ return this.#array.with(index, value);
609
+ }
610
+ [Symbol.iterator]() {
611
+ return this.#array[Symbol.iterator]();
612
+ }
613
+ }
614
+
615
+ // This class provides a DataView-like interface for a WebAssembly memory
616
+ // buffer, restricted to either Int32 or BigInt64 types. It also reacquires
617
+ // the underlying buffer when the WebAssembly memory is resized, which can
618
+ // happen when the memory is detached and resized by the WebAssembly module.
619
+ class DataViewProxy {
620
+ #module;
621
+ #type;
622
+
623
+ #_view = new DataView(new ArrayBuffer(0));
624
+ get #view() {
625
+ if (this.#_view.buffer.byteLength === 0) {
626
+ // WebAssembly memory resize detached the buffer so re-create the
627
+ // view with the new buffer.
628
+ this.#_view = new DataView(
629
+ this.#module.HEAPU8.buffer,
630
+ this.#module.HEAPU8.byteOffset + this.byteOffset);
631
+ }
632
+ return this.#_view;
633
+ }
634
+
635
+ /**
636
+ * @param {*} module
637
+ * @param {number} byteOffset
638
+ * @param {'Int32'|'BigInt64'} type
639
+ */
640
+ constructor(module, byteOffset, type) {
641
+ this.#module = module;
642
+ this.byteOffset = byteOffset;
643
+ this.#type = type;
644
+ }
645
+
646
+ get buffer() {
647
+ return this.#view.buffer;
648
+ }
649
+ get byteLength() {
650
+ return this.#type === 'Int32' ? 4 : 8;
651
+ }
652
+
653
+ getInt32(byteOffset, littleEndian) {
654
+ if (this.#type !== 'Int32') {
655
+ throw new Error('invalid type');
656
+ }
657
+ if (!littleEndian) throw new Error('must be little endian');
658
+ return this.#view.getInt32(byteOffset, littleEndian);
659
+ }
660
+ setInt32(byteOffset, value, littleEndian) {
661
+ if (this.#type !== 'Int32') {
662
+ throw new Error('invalid type');
663
+ }
664
+ if (!littleEndian) throw new Error('must be little endian');
665
+ this.#view.setInt32(byteOffset, value, littleEndian);
666
+ }
667
+ getBigInt64(byteOffset, littleEndian) {
668
+ if (this.#type !== 'BigInt64') {
669
+ throw new Error('invalid type');
670
+ }
671
+ if (!littleEndian) throw new Error('must be little endian');
672
+ return this.#view.getBigInt64(byteOffset, littleEndian);
673
+ }
674
+ setBigInt64(byteOffset, value, littleEndian) {
675
+ if (this.#type !== 'BigInt64') {
676
+ throw new Error('invalid type');
677
+ }
678
+ if (!littleEndian) throw new Error('must be little endian');
679
+ this.#view.setBigInt64(byteOffset, value, littleEndian);
680
+ }
681
+ }
@@ -3,6 +3,11 @@ import { FacadeVFS } from '../FacadeVFS.js';
3
3
  import * as VFS from '../VFS.js';
4
4
  import { WebLocksMixin } from '../WebLocksMixin.js';
5
5
 
6
+ const RETRYABLE_ERRORS = new Set([
7
+ 'TransactionInactiveError',
8
+ 'InvalidStateError'
9
+ ]);
10
+
6
11
  /**
7
12
  * @typedef Metadata
8
13
  * @property {string} name
@@ -482,25 +487,27 @@ export class IDBBatchAtomicVFS extends WebLocksMixin(FacadeVFS) {
482
487
  break;
483
488
  case VFS.SQLITE_FCNTL_SYNC:
484
489
  this.log?.('xFileControl', file.path, 'SYNC');
485
- const commitMetadata = Object.assign({}, file.metadata);
486
- const prevFileSize = file.rollback.fileSize
487
- this.#idb.q(({ metadata, blocks }) => {
488
- metadata.put(commitMetadata);
489
-
490
- // Remove old page versions.
491
- for (const offset of file.changedPages) {
492
- if (offset < prevFileSize) {
493
- const range = IDBKeyRange.bound(
494
- [file.path, -offset, commitMetadata.version],
495
- [file.path, -offset, Infinity],
496
- true);
497
- blocks.delete(range);
490
+ if (file.rollback) {
491
+ const commitMetadata = Object.assign({}, file.metadata);
492
+ const prevFileSize = file.rollback.fileSize
493
+ this.#idb.q(({ metadata, blocks }) => {
494
+ metadata.put(commitMetadata);
495
+
496
+ // Remove old page versions.
497
+ for (const offset of file.changedPages) {
498
+ if (offset < prevFileSize) {
499
+ const range = IDBKeyRange.bound(
500
+ [file.path, -offset, commitMetadata.version],
501
+ [file.path, -offset, Infinity],
502
+ true);
503
+ blocks.delete(range);
504
+ }
498
505
  }
499
- }
500
- file.changedPages.clear();
501
- }, 'rw', file.txOptions);
502
- file.needsMetadataSync = false;
503
- file.rollback = null;
506
+ file.changedPages.clear();
507
+ }, 'rw', file.txOptions);
508
+ file.needsMetadataSync = false;
509
+ file.rollback = null;
510
+ }
504
511
  break;
505
512
  case VFS.SQLITE_FCNTL_BEGIN_ATOMIC_WRITE:
506
513
  // Every write transaction is atomic, so this is a no-op.
@@ -717,21 +724,21 @@ export class IDBContext {
717
724
  });
718
725
  }
719
726
 
720
- // @ts-ignore
721
- // Create object store proxies.
722
- const objectStores = [...tx.objectStoreNames].map(name => {
723
- return [name, this.proxyStoreOrIndex(tx.objectStore(name))];
724
- });
725
-
726
727
  try {
728
+ // @ts-ignore
729
+ // Create object store proxies.
730
+ const objectStores = [...tx.objectStoreNames].map(name => {
731
+ return [name, this.proxyStoreOrIndex(tx.objectStore(name))];
732
+ });
733
+
727
734
  // Execute the function.
728
735
  return await f(Object.fromEntries(objectStores));
729
736
  } catch (e) {
730
737
  // Use a new transaction if this one was inactive. This will
731
738
  // happen if the last request in the transaction completed
732
739
  // in a previous task but the transaction has not yet committed.
733
- if (!i && e.name === 'TransactionInactiveError') {
734
- this.log?.('TransactionInactiveError, retrying');
740
+ if (!i && RETRYABLE_ERRORS.has(e.name)) {
741
+ this.log?.(`${e.name}, retrying`);
735
742
  tx = null;
736
743
  continue;
737
744
  }
@@ -333,6 +333,12 @@ export class IDBMirrorVFS extends FacadeVFS {
333
333
 
334
334
  if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {
335
335
  this.#requireTxActive(file);
336
+ // SQLite is not necessarily written sequentially, so fill in the
337
+ // unwritten blocks here.
338
+ for (let fillOffset = file.txActive.fileSize;
339
+ fillOffset < iOffset; fillOffset += pData.byteLength) {
340
+ file.txActive.blocks.set(fillOffset, new Uint8Array(pData.byteLength));
341
+ }
336
342
  file.txActive.blocks.set(iOffset, pData.slice());
337
343
  file.txActive.fileSize = Math.max(file.txActive.fileSize, iOffset + pData.byteLength);
338
344
  file.blockSize = pData.byteLength;
@@ -116,7 +116,7 @@ export class MemoryVFS extends FacadeVFS {
116
116
  }
117
117
 
118
118
  // Copy data.
119
- new Uint8Array(file.data, iOffset, pData.byteLength).set(pData);
119
+ new Uint8Array(file.data, iOffset, pData.byteLength).set(pData.subarray());
120
120
  file.size = Math.max(file.size, iOffset + pData.byteLength);
121
121
  return VFS.SQLITE_OK;
122
122
  }
@@ -437,7 +437,7 @@ export class OPFSCoopSyncVFS extends FacadeVFS {
437
437
  if (file.persistentFile.isHandleRequested) {
438
438
  // Another connection wants the access handle.
439
439
  this.#releaseAccessHandle(file);
440
- this.isHandleRequested = false;
440
+ file.persistentFile.isHandleRequested = false;
441
441
  }
442
442
  file.persistentFile.isFileLocked = false;
443
443
  }
package/src/sqlite-api.js CHANGED
@@ -35,11 +35,12 @@ export function Factory(Module) {
35
35
  const tmp = Module._malloc(8);
36
36
  const tmpPtr = [tmp, tmp + 4];
37
37
 
38
+ const textEncoder = new TextEncoder();
38
39
  // Convert a JS string to a C string. sqlite3_malloc is used to allocate
39
40
  // memory (use sqlite3_free to deallocate).
40
41
  function createUTF8(s) {
41
42
  if (typeof s !== 'string') return 0;
42
- const utf8 = new TextEncoder().encode(s);
43
+ const utf8 = textEncoder.encode(s);
43
44
  const zts = Module._sqlite3_malloc(utf8.byteLength + 1);
44
45
  Module.HEAPU8.set(utf8, zts);
45
46
  Module.HEAPU8[zts + utf8.byteLength] = 0;
@@ -119,6 +120,8 @@ export function Factory(Module) {
119
120
  }
120
121
  case 'string':
121
122
  return sqlite3.bind_text(stmt, i, value);
123
+ case "boolean":
124
+ return sqlite3.bind_int(stmt, i, value ? 1 : 0);
122
125
  default:
123
126
  if (value instanceof Uint8Array || Array.isArray(value)) {
124
127
  return sqlite3.bind_blob(stmt, i, value);
@@ -499,7 +502,6 @@ export function Factory(Module) {
499
502
  columns = columns ?? sqlite3.column_names(stmt);
500
503
  const row = sqlite3.row(stmt);
501
504
  callback(row, columns);
502
- callback(row, columns);
503
505
  }
504
506
  }
505
507
  }
@@ -769,7 +771,7 @@ export function Factory(Module) {
769
771
  const onFinally = [];
770
772
  // try {
771
773
  // Encode SQL string to UTF-8.
772
- const utf8 = new TextEncoder().encode(sql);
774
+ const utf8 = textEncoder.encode(sql);
773
775
 
774
776
  // Copy encoded string to WebAssembly memory. The SQLite docs say
775
777
  // zero-termination is a minor optimization so add room for that.
@@ -11,7 +11,7 @@ declare function setValue(ptr: number, value: number, type: string): number;
11
11
  declare function mergeInto(library: object, methods: object): void;
12
12
 
13
13
  declare var HEAPU8: Uint8Array;
14
- declare var HEAPU32: Uint32Array;
14
+ declare var HEAP32: Int32Array;
15
15
  declare var LibraryManager;
16
16
  declare var Module;
17
17
  declare var _vfsAccess;
@@ -238,6 +238,27 @@ export function api_statements(context) {
238
238
  }
239
239
  });
240
240
 
241
+ it('should bind boolean', async function() {
242
+ let rc;
243
+ const sql = 'SELECT ?';
244
+ const storeValue = true;
245
+ const expectedRetrievedValue = 1;
246
+
247
+ for await (const stmt of i(sqlite3.statements(db, sql))) {
248
+ // Comlink intercepts the 'bind' property so use an alias.
249
+ rc = await sqlite3.bind$(stmt, 1, storeValue);
250
+ expect(rc).toEqual(SQLite.SQLITE_OK);
251
+
252
+ while ((rc = await sqlite3.step(stmt)) !== SQLite.SQLITE_DONE) {
253
+ expect(rc).toEqual(SQLite.SQLITE_ROW);
254
+
255
+ expect(await sqlite3.column_count(stmt)).toEqual(1);
256
+ expect(await sqlite3.column_type(stmt, 0)).toEqual(SQLite.SQLITE_INTEGER);
257
+ expect(await sqlite3.column_int(stmt, 0)).toEqual(expectedRetrievedValue);
258
+ }
259
+ }
260
+ });
261
+
241
262
  it('should bind collection array', async function() {
242
263
  let rc;
243
264
  const sql = 'VALUES (?, ?, ?, ?, ?)';