@aztec/kv-store 0.0.1-commit.c31f2472 → 0.0.1-commit.c52d6e7

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 (159) hide show
  1. package/README.md +10 -1
  2. package/dest/bench/shared_map_bench.d.ts +19 -0
  3. package/dest/bench/shared_map_bench.d.ts.map +1 -0
  4. package/dest/bench/shared_map_bench.js +91 -0
  5. package/dest/deprecated/indexeddb/array.d.ts +22 -0
  6. package/dest/deprecated/indexeddb/array.d.ts.map +1 -0
  7. package/dest/{indexeddb → deprecated/indexeddb}/array.js +18 -6
  8. package/dest/deprecated/indexeddb/index.d.ts +7 -0
  9. package/dest/deprecated/indexeddb/index.d.ts.map +1 -0
  10. package/dest/deprecated/indexeddb/index.js +8 -0
  11. package/dest/deprecated/indexeddb/map.d.ts +38 -0
  12. package/dest/deprecated/indexeddb/map.d.ts.map +1 -0
  13. package/dest/{indexeddb → deprecated/indexeddb}/map.js +23 -13
  14. package/dest/deprecated/indexeddb/multi_map.d.ts +14 -0
  15. package/dest/deprecated/indexeddb/multi_map.d.ts.map +1 -0
  16. package/dest/{indexeddb → deprecated/indexeddb}/multi_map.js +16 -1
  17. package/dest/deprecated/indexeddb/set.d.ts +17 -0
  18. package/dest/deprecated/indexeddb/set.d.ts.map +1 -0
  19. package/dest/deprecated/indexeddb/singleton.d.ts +17 -0
  20. package/dest/deprecated/indexeddb/singleton.d.ts.map +1 -0
  21. package/dest/{indexeddb → deprecated/indexeddb}/singleton.js +3 -1
  22. package/dest/deprecated/indexeddb/store.d.ts +98 -0
  23. package/dest/deprecated/indexeddb/store.d.ts.map +1 -0
  24. package/dest/{indexeddb → deprecated/indexeddb}/store.js +8 -4
  25. package/dest/interfaces/array_test_suite.d.ts +1 -1
  26. package/dest/interfaces/array_test_suite.d.ts.map +1 -1
  27. package/dest/interfaces/array_test_suite.js +33 -34
  28. package/dest/interfaces/index.d.ts +2 -2
  29. package/dest/interfaces/index.d.ts.map +1 -1
  30. package/dest/interfaces/map_test_suite.d.ts +1 -1
  31. package/dest/interfaces/map_test_suite.d.ts.map +1 -1
  32. package/dest/interfaces/map_test_suite.js +74 -29
  33. package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
  34. package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
  35. package/dest/interfaces/multi_map_test_suite.js +84 -60
  36. package/dest/interfaces/set_test_suite.d.ts +1 -1
  37. package/dest/interfaces/set_test_suite.d.ts.map +1 -1
  38. package/dest/interfaces/set_test_suite.js +13 -14
  39. package/dest/interfaces/singleton_test_suite.d.ts +1 -1
  40. package/dest/interfaces/singleton_test_suite.d.ts.map +1 -1
  41. package/dest/interfaces/singleton_test_suite.js +6 -7
  42. package/dest/lmdb/index.d.ts +3 -3
  43. package/dest/lmdb/index.d.ts.map +1 -1
  44. package/dest/lmdb/index.js +4 -4
  45. package/dest/lmdb/store.d.ts +3 -3
  46. package/dest/lmdb/store.d.ts.map +1 -1
  47. package/dest/lmdb/store.js +12 -8
  48. package/dest/lmdb-v2/factory.d.ts +30 -4
  49. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  50. package/dest/lmdb-v2/factory.js +63 -11
  51. package/dest/lmdb-v2/read_transaction.js +21 -19
  52. package/dest/lmdb-v2/store.d.ts +2 -2
  53. package/dest/lmdb-v2/store.d.ts.map +1 -1
  54. package/dest/lmdb-v2/store.js +4 -4
  55. package/dest/sqlite-opfs/array.d.ts +21 -0
  56. package/dest/sqlite-opfs/array.d.ts.map +1 -0
  57. package/dest/sqlite-opfs/array.js +128 -0
  58. package/dest/sqlite-opfs/errors.d.ts +54 -0
  59. package/dest/sqlite-opfs/errors.d.ts.map +1 -0
  60. package/dest/sqlite-opfs/errors.js +77 -0
  61. package/dest/sqlite-opfs/index.d.ts +14 -0
  62. package/dest/sqlite-opfs/index.d.ts.map +1 -0
  63. package/dest/sqlite-opfs/index.js +16 -0
  64. package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts +32 -0
  65. package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts.map +1 -0
  66. package/dest/sqlite-opfs/internal/ordered-binary-browser.js +448 -0
  67. package/dest/sqlite-opfs/manage.d.ts +22 -0
  68. package/dest/sqlite-opfs/manage.d.ts.map +1 -0
  69. package/dest/sqlite-opfs/manage.js +38 -0
  70. package/dest/sqlite-opfs/map.d.ts +35 -0
  71. package/dest/sqlite-opfs/map.d.ts.map +1 -0
  72. package/dest/sqlite-opfs/map.js +163 -0
  73. package/dest/sqlite-opfs/messages.d.ts +66 -0
  74. package/dest/sqlite-opfs/messages.d.ts.map +1 -0
  75. package/dest/sqlite-opfs/messages.js +5 -0
  76. package/dest/sqlite-opfs/multi_map.d.ts +16 -0
  77. package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
  78. package/dest/sqlite-opfs/multi_map.js +67 -0
  79. package/dest/sqlite-opfs/pool_integrity.d.ts +20 -0
  80. package/dest/sqlite-opfs/pool_integrity.d.ts.map +1 -0
  81. package/dest/sqlite-opfs/pool_integrity.js +282 -0
  82. package/dest/sqlite-opfs/pool_lock.d.ts +8 -0
  83. package/dest/sqlite-opfs/pool_lock.d.ts.map +1 -0
  84. package/dest/sqlite-opfs/pool_lock.js +47 -0
  85. package/dest/sqlite-opfs/set.d.ts +13 -0
  86. package/dest/sqlite-opfs/set.d.ts.map +1 -0
  87. package/dest/sqlite-opfs/set.js +19 -0
  88. package/dest/sqlite-opfs/singleton.d.ts +13 -0
  89. package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
  90. package/dest/sqlite-opfs/singleton.js +48 -0
  91. package/dest/sqlite-opfs/store.d.ts +84 -0
  92. package/dest/sqlite-opfs/store.d.ts.map +1 -0
  93. package/dest/sqlite-opfs/store.js +317 -0
  94. package/dest/sqlite-opfs/worker.d.ts +2 -0
  95. package/dest/sqlite-opfs/worker.d.ts.map +1 -0
  96. package/dest/sqlite-opfs/worker.js +275 -0
  97. package/dest/stores/l2_tips_store.d.ts +7 -13
  98. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  99. package/dest/stores/l2_tips_store.js +24 -43
  100. package/dest/utils.d.ts +9 -6
  101. package/dest/utils.d.ts.map +1 -1
  102. package/dest/utils.js +51 -16
  103. package/package.json +30 -26
  104. package/src/bench/shared_map_bench.ts +111 -0
  105. package/src/{indexeddb → deprecated/indexeddb}/array.ts +6 -6
  106. package/src/deprecated/indexeddb/index.ts +12 -0
  107. package/src/{indexeddb → deprecated/indexeddb}/map.ts +26 -13
  108. package/src/{indexeddb → deprecated/indexeddb}/multi_map.ts +17 -3
  109. package/src/{indexeddb → deprecated/indexeddb}/set.ts +2 -2
  110. package/src/{indexeddb → deprecated/indexeddb}/singleton.ts +3 -3
  111. package/src/{indexeddb → deprecated/indexeddb}/store.ts +23 -15
  112. package/src/interfaces/array_test_suite.ts +33 -35
  113. package/src/interfaces/index.ts +1 -1
  114. package/src/interfaces/map_test_suite.ts +56 -30
  115. package/src/interfaces/multi_map_test_suite.ts +88 -58
  116. package/src/interfaces/set_test_suite.ts +13 -15
  117. package/src/interfaces/singleton_test_suite.ts +6 -8
  118. package/src/lmdb/index.ts +10 -5
  119. package/src/lmdb/store.ts +12 -8
  120. package/src/lmdb-v2/factory.ts +86 -10
  121. package/src/lmdb-v2/read_transaction.ts +23 -23
  122. package/src/lmdb-v2/store.ts +4 -2
  123. package/src/sqlite-opfs/array.ts +124 -0
  124. package/src/sqlite-opfs/errors.ts +94 -0
  125. package/src/sqlite-opfs/index.ts +27 -0
  126. package/src/sqlite-opfs/internal/ordered-binary-browser.js +465 -0
  127. package/src/sqlite-opfs/manage.ts +46 -0
  128. package/src/sqlite-opfs/map.ts +163 -0
  129. package/src/sqlite-opfs/messages.ts +39 -0
  130. package/src/sqlite-opfs/multi_map.ts +74 -0
  131. package/src/sqlite-opfs/pool_integrity.ts +327 -0
  132. package/src/sqlite-opfs/pool_lock.ts +62 -0
  133. package/src/sqlite-opfs/set.ts +29 -0
  134. package/src/sqlite-opfs/singleton.ts +48 -0
  135. package/src/sqlite-opfs/store.ts +330 -0
  136. package/src/sqlite-opfs/worker.ts +259 -0
  137. package/src/stores/l2_tips_store.ts +20 -41
  138. package/src/utils.ts +79 -21
  139. package/dest/config.d.ts +0 -17
  140. package/dest/config.d.ts.map +0 -1
  141. package/dest/config.js +0 -26
  142. package/dest/indexeddb/array.d.ts +0 -22
  143. package/dest/indexeddb/array.d.ts.map +0 -1
  144. package/dest/indexeddb/index.d.ts +0 -7
  145. package/dest/indexeddb/index.d.ts.map +0 -1
  146. package/dest/indexeddb/index.js +0 -19
  147. package/dest/indexeddb/map.d.ts +0 -32
  148. package/dest/indexeddb/map.d.ts.map +0 -1
  149. package/dest/indexeddb/multi_map.d.ts +0 -13
  150. package/dest/indexeddb/multi_map.d.ts.map +0 -1
  151. package/dest/indexeddb/set.d.ts +0 -17
  152. package/dest/indexeddb/set.d.ts.map +0 -1
  153. package/dest/indexeddb/singleton.d.ts +0 -17
  154. package/dest/indexeddb/singleton.d.ts.map +0 -1
  155. package/dest/indexeddb/store.d.ts +0 -96
  156. package/dest/indexeddb/store.d.ts.map +0 -1
  157. package/src/config.ts +0 -36
  158. package/src/indexeddb/index.ts +0 -29
  159. /package/dest/{indexeddb → deprecated/indexeddb}/set.js +0 -0
@@ -0,0 +1,47 @@
1
+ import { SqlitePoolBusyError, SqliteWebLocksUnavailableError } from './errors.js';
2
+ export const DEFAULT_SAH_POOL_DIRECTORY = '.aztec-kv';
3
+ const WEB_LOCK_PREFIX = 'aztec.sqlite-opfs.pool:';
4
+ export function normalizePoolDirectory(poolDirectory) {
5
+ const path = poolDirectory?.split('/').filter((segment)=>segment.length > 0).join('/');
6
+ return path || DEFAULT_SAH_POOL_DIRECTORY;
7
+ }
8
+ export async function acquirePoolLock(poolDirectory) {
9
+ if (!navigator.locks) {
10
+ throw new SqliteWebLocksUnavailableError();
11
+ }
12
+ const normalizedDirectory = normalizePoolDirectory(poolDirectory);
13
+ const acquired = Promise.withResolvers();
14
+ const hold = Promise.withResolvers();
15
+ const request = navigator.locks.request(`${WEB_LOCK_PREFIX}${normalizedDirectory}`, {
16
+ mode: 'exclusive',
17
+ ifAvailable: true
18
+ }, async (lock)=>{
19
+ acquired.resolve(lock);
20
+ if (lock) {
21
+ await hold.promise;
22
+ }
23
+ });
24
+ request.catch(acquired.reject);
25
+ if (!await acquired.promise) {
26
+ await request;
27
+ throw new SqlitePoolBusyError(normalizedDirectory);
28
+ }
29
+ let released = false;
30
+ return {
31
+ release: async ()=>{
32
+ if (!released) {
33
+ released = true;
34
+ hold.resolve();
35
+ }
36
+ await request;
37
+ }
38
+ };
39
+ }
40
+ export async function withPoolLock(poolDirectory, callback) {
41
+ const lease = await acquirePoolLock(poolDirectory);
42
+ try {
43
+ return await callback();
44
+ } finally{
45
+ await lease.release();
46
+ }
47
+ }
@@ -0,0 +1,13 @@
1
+ import type { Key, Range } from '../interfaces/common.js';
2
+ import type { AztecAsyncSet } from '../interfaces/set.js';
3
+ import type { AztecSQLiteOPFSStore } from './store.js';
4
+ /** Set backed by SQLite. Composes a Map<K, true>. */
5
+ export declare class SQLiteOPFSAztecSet<K extends Key> implements AztecAsyncSet<K> {
6
+ #private;
7
+ constructor(store: AztecSQLiteOPFSStore, name: string);
8
+ hasAsync(key: K): Promise<boolean>;
9
+ add(key: K): Promise<void>;
10
+ delete(key: K): Promise<void>;
11
+ entriesAsync(range?: Range<K>): AsyncIterableIterator<K>;
12
+ }
13
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2V0LmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3FsaXRlLW9wZnMvc2V0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxFQUFFLEdBQUcsRUFBRSxLQUFLLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUMxRCxPQUFPLEtBQUssRUFBRSxhQUFhLEVBQUU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CAAC,OAAO,CAAC,CAEjC;IAED,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAEzB;IAED,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAE5B;IAEM,YAAY,CAAC,KAAK,GAAE,KAAK,CAAC,CAAC,CAAM,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAElE;CACF"}
@@ -0,0 +1,19 @@
1
+ import { SQLiteOPFSAztecMap } from './map.js';
2
+ /** Set backed by SQLite. Composes a Map<K, true>. */ export class SQLiteOPFSAztecSet {
3
+ #map;
4
+ constructor(store, name){
5
+ this.#map = new SQLiteOPFSAztecMap(store, name);
6
+ }
7
+ hasAsync(key) {
8
+ return this.#map.hasAsync(key);
9
+ }
10
+ add(key) {
11
+ return this.#map.set(key, true);
12
+ }
13
+ delete(key) {
14
+ return this.#map.delete(key);
15
+ }
16
+ async *entriesAsync(range = {}) {
17
+ yield* this.#map.keysAsync(range);
18
+ }
19
+ }
@@ -0,0 +1,13 @@
1
+ import type { Value } from '../interfaces/common.js';
2
+ import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
3
+ import type { AztecSQLiteOPFSStore } from './store.js';
4
+ /** Stores a single value identified by `name`. */
5
+ export declare class SQLiteOPFSAztecSingleton<T extends Value> implements AztecAsyncSingleton<T> {
6
+ #private;
7
+ private readonly store;
8
+ constructor(store: AztecSQLiteOPFSStore, name: string);
9
+ getAsync(): Promise<T | undefined>;
10
+ set(val: T): Promise<boolean>;
11
+ delete(): Promise<boolean>;
12
+ }
13
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2luZ2xldG9uLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3FsaXRlLW9wZnMvc2luZ2xldG9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlBLE9BQU8sS0FBSyxFQUF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AS,CAAC,CAUvC;IAEK,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAOlC;IAEK,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,CAG/B;CACF"}
@@ -0,0 +1,48 @@
1
+ import { Encoder } from '#msgpackr';
2
+ import { toBufferKey } from '#ordered-binary';
3
+ import { hash } from 'ohash';
4
+ /** Stores a single value identified by `name`. */ export class SQLiteOPFSAztecSingleton {
5
+ store;
6
+ #container;
7
+ #slot;
8
+ #encoder;
9
+ constructor(store, name){
10
+ this.store = store;
11
+ this.#encoder = new Encoder();
12
+ this.#container = `singleton:${name}`;
13
+ this.#slot = `singleton:${name}:value`;
14
+ }
15
+ async getAsync() {
16
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [
17
+ this.#slot
18
+ ]);
19
+ if (rows.length === 0) {
20
+ return undefined;
21
+ }
22
+ const raw = rows[0][0];
23
+ if (raw instanceof Uint8Array) {
24
+ return this.#encoder.unpack(raw);
25
+ }
26
+ return undefined;
27
+ }
28
+ async set(val) {
29
+ const { changes } = await this.store.runAsync(`INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
30
+ VALUES (?, ?, ?, ?, ?, ?)`, [
31
+ this.#slot,
32
+ this.#container,
33
+ toBufferKey([
34
+ this.#slot
35
+ ]),
36
+ 1,
37
+ hash(val),
38
+ this.#encoder.pack(val)
39
+ ]);
40
+ return changes > 0;
41
+ }
42
+ async delete() {
43
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [
44
+ this.#slot
45
+ ]);
46
+ return true;
47
+ }
48
+ }
@@ -0,0 +1,84 @@
1
+ import type { Logger } from '@aztec/foundation/log';
2
+ import type { AztecAsyncArray } from '../interfaces/array.js';
3
+ import type { Key, StoreSize, Value } from '../interfaces/common.js';
4
+ import type { AztecAsyncCounter } from '../interfaces/counter.js';
5
+ import type { AztecAsyncMap } from '../interfaces/map.js';
6
+ import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
7
+ import type { AztecAsyncSet } from '../interfaces/set.js';
8
+ import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
9
+ import type { AztecAsyncKVStore } from '../interfaces/store.js';
10
+ import type { ResultRow, SqlValue } from './messages.js';
11
+ /**
12
+ * Main-thread handle for a SQLite database persisted to OPFS via the `opfs-sahpool`
13
+ * VFS. Owns a dedicated Web Worker (the SAH Pool VFS requires Worker context) and
14
+ * routes every SQL op through it via typed postMessage RPC.
15
+ *
16
+ * Transaction ordering is guaranteed by a `SerialQueue` on the main thread combined
17
+ * with an `#inTx` flag: outside a `transactionAsync` block, each op acquires the
18
+ * queue for its own auto-commit; inside a block, the outer call holds the queue and
19
+ * nested ops bypass it to avoid deadlock.
20
+ */
21
+ export declare class AztecSQLiteOPFSStore implements AztecAsyncKVStore {
22
+ #private;
23
+ readonly isEphemeral: boolean;
24
+ private readonly poolLock?;
25
+ private constructor();
26
+ /**
27
+ * Opens (or creates) a SQLite database stored in the OPFS SAH Pool. When `ephemeral`
28
+ * is true the database lives only in memory and is lost when the worker terminates.
29
+ * Pass `poolDirectory` to place the SAH Pool in a non-default OPFS subdirectory —
30
+ * required when multiple stores coexist in the same tab, because the SAH Pool holds
31
+ * an exclusive lock on its directory.
32
+ *
33
+ * Persistent stores hold an origin-wide Web Lock for the pool directory until close
34
+ * or delete. If another store instance already owns it, open fails immediately with
35
+ * `SqlitePoolBusyError`.
36
+ *
37
+ * Pass `encryptionKey` (exactly 32 bytes) to enable at-rest encryption via sqlite3mc's
38
+ * ChaCha20 page cipher. The key buffer is **transferred** to the worker — its
39
+ * ArrayBuffer detaches on the caller side after `postMessage`. This is intentional:
40
+ * the API encodes a one-key-one-owner invariant. A caller that wants to use the same
41
+ * key for multiple stores must explicitly clone it per call (e.g.
42
+ * `new Uint8Array(savedKey)`), making the duplication a visible, deliberate decision
43
+ * rather than a silent structured-clone operation. The default path (one `.open()`,
44
+ * one consumption of the key) leaves zero key bytes on the main thread after the call.
45
+ */
46
+ static open(log: Logger, name?: string, ephemeral?: boolean, poolDirectory?: string, encryptionKey?: Uint8Array): Promise<AztecSQLiteOPFSStore>;
47
+ openMap<K extends Key, V extends Value>(name: string): AztecAsyncMap<K, V>;
48
+ openSet<K extends Key>(name: string): AztecAsyncSet<K>;
49
+ openMultiMap<K extends Key, V extends Value>(name: string): AztecAsyncMultiMap<K, V>;
50
+ openCounter<K extends Key>(_name: string): AztecAsyncCounter<K>;
51
+ openArray<T extends Value>(name: string): AztecAsyncArray<T>;
52
+ openSingleton<T extends Value>(name: string): AztecAsyncSingleton<T>;
53
+ transactionAsync<T>(callback: () => Promise<T>): Promise<T>;
54
+ clear(): Promise<void>;
55
+ delete(): Promise<void>;
56
+ /**
57
+ * Placeholder — returns zeros to mirror the IndexedDB backend. SQLite exposes real
58
+ * numbers cheaply via `PRAGMA page_count` / `page_size` / `freelist_count` and
59
+ * `SELECT COUNT(*) FROM data`, which would populate `physicalFileSize`, `actualSize`,
60
+ * and `numItems` meaningfully (`mappingSize` stays 0 — it's an LMDB mmap concept).
61
+ * Upgrade when any caller actually consumes these values; all current consumers
62
+ * tolerate zeros.
63
+ */
64
+ estimateSize(): Promise<StoreSize>;
65
+ close(): Promise<void>;
66
+ backupTo(_dstPath: string, _compact?: boolean): Promise<void>;
67
+ /**
68
+ * Returns a raw SQLite image (bytes suitable for writing as a `.sqlite` file and
69
+ * opening in any SQLite tool). Works only for non-ephemeral DBs because the OPFS
70
+ * SAH Pool has to be initialized. Useful for inspection/debugging.
71
+ */
72
+ exportDb(): Promise<Uint8Array>;
73
+ /**
74
+ * Runs a write statement (INSERT/UPDATE/DELETE/DDL). If called inside a
75
+ * `transactionAsync` block, bypasses the queue; otherwise acquires it so the
76
+ * op runs in its own auto-commit.
77
+ */
78
+ runAsync(sql: string, bind?: SqlValue[]): Promise<{
79
+ changes: number;
80
+ }>;
81
+ /** Runs a SELECT statement and returns rows in array row-mode. */
82
+ allAsync(sql: string, bind?: SqlValue[]):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WAAW,CAAC,CAAC,SAAS,GAAG,EAAE,KAAK,EAAE,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAE9D;IAED,SAAS,CAAC,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,eAAe,CAAC,CAAC,CAAC,CAE3D;IAED,aAAa,CAAC,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAEnE;IAED,gBAAgB,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAwB1D;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAE3B;IAEK,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAe5B;IAED;;;;;;;OAOG;IACH,YAAY,IAAI,OAAO,CAAC,SAAS,CAAC,CAEjC;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAa3B;IAED,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAE5D;IAED;;;;OAIG;IACG,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAC,CAMpC;IAED;;;;OAIG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAMrE;IAED,kEAAkE;IAClE,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC,CAI7D;CAuDF"}
@@ -0,0 +1,317 @@
1
+ import { SerialQueue } from '@aztec/foundation/queue';
2
+ import { SQLiteOPFSAztecArray } from './array.js';
3
+ import { SqliteCorruptionError, SqliteEncryptionError, isCorruptionMessage } from './errors.js';
4
+ import { SQLiteOPFSAztecMap } from './map.js';
5
+ import { SQLiteOPFSAztecMultiMap } from './multi_map.js';
6
+ import { quarantineDuplicatePool } from './pool_integrity.js';
7
+ import { acquirePoolLock, normalizePoolDirectory } from './pool_lock.js';
8
+ import { SQLiteOPFSAztecSet } from './set.js';
9
+ import { SQLiteOPFSAztecSingleton } from './singleton.js';
10
+ /**
11
+ * Main-thread handle for a SQLite database persisted to OPFS via the `opfs-sahpool`
12
+ * VFS. Owns a dedicated Web Worker (the SAH Pool VFS requires Worker context) and
13
+ * routes every SQL op through it via typed postMessage RPC.
14
+ *
15
+ * Transaction ordering is guaranteed by a `SerialQueue` on the main thread combined
16
+ * with an `#inTx` flag: outside a `transactionAsync` block, each op acquires the
17
+ * queue for its own auto-commit; inside a block, the outer call holds the queue and
18
+ * nested ops bypass it to avoid deadlock.
19
+ */ export class AztecSQLiteOPFSStore {
20
+ isEphemeral;
21
+ poolLock;
22
+ #worker;
23
+ #pending;
24
+ #txQueue;
25
+ #name;
26
+ #log;
27
+ #nextId;
28
+ #inTx;
29
+ #closed;
30
+ #workerFailed;
31
+ constructor(worker, name, log, isEphemeral, poolLock){
32
+ this.isEphemeral = isEphemeral;
33
+ this.poolLock = poolLock;
34
+ this.#pending = new Map();
35
+ this.#txQueue = new SerialQueue();
36
+ this.#nextId = 0;
37
+ this.#inTx = false;
38
+ this.#closed = false;
39
+ this.#workerFailed = false;
40
+ this.#worker = worker;
41
+ this.#name = name;
42
+ this.#log = log;
43
+ this.#worker.onmessage = (ev)=>{
44
+ const { id } = ev.data;
45
+ const handler = this.#pending.get(id);
46
+ if (!handler) {
47
+ this.#log.warn(`SQLite worker: no pending handler for id ${id}`);
48
+ return;
49
+ }
50
+ this.#pending.delete(id);
51
+ handler.resolve(ev.data);
52
+ };
53
+ this.#worker.onerror = (ev)=>{
54
+ this.#workerFailed = true;
55
+ this.#log.error(`SQLite worker crashed: ${ev.message}`);
56
+ this.#rejectPending(`SQLite worker crashed: ${ev.message}`);
57
+ };
58
+ this.#txQueue.start();
59
+ }
60
+ /**
61
+ * Opens (or creates) a SQLite database stored in the OPFS SAH Pool. When `ephemeral`
62
+ * is true the database lives only in memory and is lost when the worker terminates.
63
+ * Pass `poolDirectory` to place the SAH Pool in a non-default OPFS subdirectory —
64
+ * required when multiple stores coexist in the same tab, because the SAH Pool holds
65
+ * an exclusive lock on its directory.
66
+ *
67
+ * Persistent stores hold an origin-wide Web Lock for the pool directory until close
68
+ * or delete. If another store instance already owns it, open fails immediately with
69
+ * `SqlitePoolBusyError`.
70
+ *
71
+ * Pass `encryptionKey` (exactly 32 bytes) to enable at-rest encryption via sqlite3mc's
72
+ * ChaCha20 page cipher. The key buffer is **transferred** to the worker — its
73
+ * ArrayBuffer detaches on the caller side after `postMessage`. This is intentional:
74
+ * the API encodes a one-key-one-owner invariant. A caller that wants to use the same
75
+ * key for multiple stores must explicitly clone it per call (e.g.
76
+ * `new Uint8Array(savedKey)`), making the duplication a visible, deliberate decision
77
+ * rather than a silent structured-clone operation. The default path (one `.open()`,
78
+ * one consumption of the key) leaves zero key bytes on the main thread after the call.
79
+ */ static async open(log, name, ephemeral = false, poolDirectory, encryptionKey) {
80
+ if (encryptionKey !== undefined && encryptionKey.length !== 32) {
81
+ throw new SqliteEncryptionError('invalid_key_length', `encryptionKey must be 32 bytes (got ${encryptionKey.length})`);
82
+ }
83
+ if (encryptionKey !== undefined && ephemeral) {
84
+ throw new SqliteEncryptionError('encryption_not_supported_for_ephemeral', 'encryptionKey is not supported for ephemeral (:memory:) stores');
85
+ }
86
+ const dbName = name && !ephemeral ? name : `tmp-${globalThis.crypto.getRandomValues(new Uint8Array(8)).join('')}`;
87
+ log.debug(`Opening SQLite-OPFS ${ephemeral ? 'ephemeral ' : ''}${encryptionKey ? 'encrypted ' : ''}database ${dbName}`);
88
+ const effectivePoolDirectory = ephemeral ? undefined : normalizePoolDirectory(poolDirectory);
89
+ const poolLock = effectivePoolDirectory ? await acquirePoolLock(effectivePoolDirectory) : undefined;
90
+ let worker;
91
+ try {
92
+ if (effectivePoolDirectory) {
93
+ const quarantine = await quarantineDuplicatePool(effectivePoolDirectory);
94
+ if (quarantine) {
95
+ log.warn(`Quarantined SQLite-OPFS pool with duplicate logical file mappings`, quarantine);
96
+ }
97
+ }
98
+ worker = new Worker(new URL('./worker.js', import.meta.url), {
99
+ type: 'module'
100
+ });
101
+ const store = new AztecSQLiteOPFSStore(worker, dbName, log, ephemeral, poolLock);
102
+ // Transfer (not clone) the key buffer to the worker so we don't leave a
103
+ // second copy on the main thread. Caveat: this detaches the caller's
104
+ // encryptionKey.buffer — subsequent reads from the same Uint8Array are empty.
105
+ const transfer = encryptionKey ? [
106
+ encryptionKey.buffer
107
+ ] : undefined;
108
+ await store.#sendRequest({
109
+ type: 'init',
110
+ id: store.#allocId(),
111
+ dbName,
112
+ ephemeral,
113
+ poolDirectory: effectivePoolDirectory,
114
+ encryptionKey
115
+ }, transfer);
116
+ return store;
117
+ } catch (err) {
118
+ worker?.terminate();
119
+ await poolLock?.release();
120
+ throw err;
121
+ }
122
+ }
123
+ openMap(name) {
124
+ return new SQLiteOPFSAztecMap(this, name);
125
+ }
126
+ openSet(name) {
127
+ return new SQLiteOPFSAztecSet(this, name);
128
+ }
129
+ openMultiMap(name) {
130
+ return new SQLiteOPFSAztecMultiMap(this, name);
131
+ }
132
+ openCounter(_name) {
133
+ throw new Error('Method not implemented.');
134
+ }
135
+ openArray(name) {
136
+ return new SQLiteOPFSAztecArray(this, name);
137
+ }
138
+ openSingleton(name) {
139
+ return new SQLiteOPFSAztecSingleton(this, name);
140
+ }
141
+ transactionAsync(callback) {
142
+ // Nested calls join the outer transaction — SQLite does not support nested BEGIN,
143
+ // and re-acquiring the SerialQueue while the outer call holds it would deadlock.
144
+ // Errors in the nested callback propagate to the outer catch, which rolls back the
145
+ // whole thing (the standard "nested tx = savepoint-free join" semantic).
146
+ if (this.#inTx) {
147
+ return callback();
148
+ }
149
+ return this.#txQueue.put(async ()=>{
150
+ this.#inTx = true;
151
+ await this.#sendRequest({
152
+ type: 'begin',
153
+ id: this.#allocId()
154
+ });
155
+ try {
156
+ const result = await callback();
157
+ await this.#sendRequest({
158
+ type: 'commit',
159
+ id: this.#allocId()
160
+ });
161
+ return result;
162
+ } catch (err) {
163
+ await this.#sendRequest({
164
+ type: 'rollback',
165
+ id: this.#allocId()
166
+ }).catch((rollbackErr)=>this.#log.warn(`SQLite ROLLBACK failed: ${rollbackErr instanceof Error ? rollbackErr.message : rollbackErr}`));
167
+ throw err;
168
+ } finally{
169
+ this.#inTx = false;
170
+ }
171
+ });
172
+ }
173
+ async clear() {
174
+ await this.runAsync('DELETE FROM data');
175
+ }
176
+ async delete() {
177
+ if (this.#closed) {
178
+ return;
179
+ }
180
+ this.#closed = true;
181
+ try {
182
+ await this.#txQueue.end();
183
+ await this.#sendRequest({
184
+ type: 'deleteDb',
185
+ id: this.#allocId(),
186
+ dbName: this.#name
187
+ }).catch((err)=>this.#log.warn(`SQLite deleteDb failed: ${err instanceof Error ? err.message : err}`));
188
+ } finally{
189
+ this.#worker.terminate();
190
+ this.#rejectPending('SQLite store deleted');
191
+ await this.poolLock?.release();
192
+ }
193
+ }
194
+ /**
195
+ * Placeholder — returns zeros to mirror the IndexedDB backend. SQLite exposes real
196
+ * numbers cheaply via `PRAGMA page_count` / `page_size` / `freelist_count` and
197
+ * `SELECT COUNT(*) FROM data`, which would populate `physicalFileSize`, `actualSize`,
198
+ * and `numItems` meaningfully (`mappingSize` stays 0 — it's an LMDB mmap concept).
199
+ * Upgrade when any caller actually consumes these values; all current consumers
200
+ * tolerate zeros.
201
+ */ estimateSize() {
202
+ return Promise.resolve({
203
+ mappingSize: 0,
204
+ physicalFileSize: 0,
205
+ actualSize: 0,
206
+ numItems: 0
207
+ });
208
+ }
209
+ async close() {
210
+ if (this.#closed) {
211
+ return;
212
+ }
213
+ this.#closed = true;
214
+ try {
215
+ await this.#txQueue.end();
216
+ await this.#sendRequest({
217
+ type: 'close',
218
+ id: this.#allocId()
219
+ }).catch(()=>{});
220
+ } finally{
221
+ this.#worker.terminate();
222
+ this.#rejectPending('SQLite store closed');
223
+ await this.poolLock?.release();
224
+ }
225
+ }
226
+ backupTo(_dstPath, _compact) {
227
+ throw new Error('Method not implemented.');
228
+ }
229
+ /**
230
+ * Returns a raw SQLite image (bytes suitable for writing as a `.sqlite` file and
231
+ * opening in any SQLite tool). Works only for non-ephemeral DBs because the OPFS
232
+ * SAH Pool has to be initialized. Useful for inspection/debugging.
233
+ */ async exportDb() {
234
+ const resp = await this.#sendRequest({
235
+ type: 'export',
236
+ id: this.#allocId()
237
+ });
238
+ if (!('bytes' in resp) || !resp.bytes) {
239
+ throw new Error('exportDb: worker returned no bytes');
240
+ }
241
+ return resp.bytes;
242
+ }
243
+ /**
244
+ * Runs a write statement (INSERT/UPDATE/DELETE/DDL). If called inside a
245
+ * `transactionAsync` block, bypasses the queue; otherwise acquires it so the
246
+ * op runs in its own auto-commit.
247
+ */ runAsync(sql, bind) {
248
+ const send = ()=>this.#sendRequest({
249
+ type: 'run',
250
+ id: this.#allocId(),
251
+ sql,
252
+ bind
253
+ }).then((r)=>({
254
+ changes: 'changes' in r ? r.changes ?? 0 : 0
255
+ }));
256
+ return this.#inTx ? send() : this.#txQueue.put(send);
257
+ }
258
+ /** Runs a SELECT statement and returns rows in array row-mode. */ allAsync(sql, bind) {
259
+ const send = ()=>this.#sendRequest({
260
+ type: 'all',
261
+ id: this.#allocId(),
262
+ sql,
263
+ bind
264
+ }).then((r)=>'rows' in r ? r.rows ?? [] : []);
265
+ return this.#inTx ? send() : this.#txQueue.put(send);
266
+ }
267
+ #allocId() {
268
+ return ++this.#nextId;
269
+ }
270
+ /**
271
+ * Reject any in-flight requests with `reason`, so callers awaiting a response to a
272
+ * request sent to a now-terminated worker don't hang forever. Called from
273
+ * close()/delete() and from the worker.onerror handler.
274
+ */ #rejectPending(reason) {
275
+ if (this.#pending.size === 0) {
276
+ return;
277
+ }
278
+ const err = new Error(reason);
279
+ for (const { reject } of this.#pending.values()){
280
+ reject(err);
281
+ }
282
+ this.#pending.clear();
283
+ }
284
+ #sendRequest(req, transfer) {
285
+ if (this.#workerFailed) {
286
+ return Promise.reject(new Error('SQLite worker has crashed'));
287
+ }
288
+ return new Promise((resolve, reject)=>{
289
+ this.#pending.set(req.id, {
290
+ resolve: (resp)=>{
291
+ if (resp.type === 'err') {
292
+ // Re-hydrate typed errors so consumers can pattern-match on
293
+ // `instanceof`. Encryption is tagged on the wire (some cases are
294
+ // pre-flight throws with no message to match); corruption is a
295
+ // single unambiguous message, so we classify it here rather than
296
+ // adding a redundant wire field. Everything else stays a plain Error.
297
+ if (resp.encryptionCode !== undefined) {
298
+ reject(new SqliteEncryptionError(resp.encryptionCode, resp.message));
299
+ } else if (isCorruptionMessage(resp.message)) {
300
+ reject(new SqliteCorruptionError(resp.message));
301
+ } else {
302
+ reject(new Error(resp.message));
303
+ }
304
+ } else {
305
+ resolve(resp);
306
+ }
307
+ },
308
+ reject
309
+ });
310
+ if (transfer && transfer.length > 0) {
311
+ this.#worker.postMessage(req, transfer);
312
+ } else {
313
+ this.#worker.postMessage(req);
314
+ }
315
+ });
316
+ }
317
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid29ya2VyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3FsaXRlLW9wZnMvd29ya2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiIifQ==
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/sqlite-opfs/worker.ts"],"names":[],"mappings":""}