@aztec/kv-store 0.0.1-commit.9ef841308 → 0.0.1-commit.a4600f49

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 (131) 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/deprecated/indexeddb/index.d.ts +13 -0
  8. package/dest/deprecated/indexeddb/index.d.ts.map +1 -0
  9. package/dest/{indexeddb → deprecated/indexeddb}/index.js +8 -4
  10. package/dest/deprecated/indexeddb/map.d.ts +38 -0
  11. package/dest/deprecated/indexeddb/map.d.ts.map +1 -0
  12. package/dest/deprecated/indexeddb/multi_map.d.ts +14 -0
  13. package/dest/deprecated/indexeddb/multi_map.d.ts.map +1 -0
  14. package/dest/deprecated/indexeddb/set.d.ts +17 -0
  15. package/dest/deprecated/indexeddb/set.d.ts.map +1 -0
  16. package/dest/deprecated/indexeddb/singleton.d.ts +17 -0
  17. package/dest/deprecated/indexeddb/singleton.d.ts.map +1 -0
  18. package/dest/deprecated/indexeddb/store.d.ts +98 -0
  19. package/dest/deprecated/indexeddb/store.d.ts.map +1 -0
  20. package/dest/{indexeddb → deprecated/indexeddb}/store.js +2 -0
  21. package/dest/interfaces/array_test_suite.d.ts +1 -1
  22. package/dest/interfaces/array_test_suite.d.ts.map +1 -1
  23. package/dest/interfaces/array_test_suite.js +33 -34
  24. package/dest/interfaces/index.d.ts +2 -2
  25. package/dest/interfaces/index.d.ts.map +1 -1
  26. package/dest/interfaces/map_test_suite.d.ts +1 -1
  27. package/dest/interfaces/map_test_suite.d.ts.map +1 -1
  28. package/dest/interfaces/map_test_suite.js +32 -33
  29. package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
  30. package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
  31. package/dest/interfaces/multi_map_test_suite.js +68 -69
  32. package/dest/interfaces/set_test_suite.d.ts +1 -1
  33. package/dest/interfaces/set_test_suite.d.ts.map +1 -1
  34. package/dest/interfaces/set_test_suite.js +13 -14
  35. package/dest/interfaces/singleton_test_suite.d.ts +1 -1
  36. package/dest/interfaces/singleton_test_suite.d.ts.map +1 -1
  37. package/dest/interfaces/singleton_test_suite.js +6 -7
  38. package/dest/lmdb/index.js +2 -2
  39. package/dest/lmdb-v2/factory.d.ts +29 -3
  40. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  41. package/dest/lmdb-v2/factory.js +62 -10
  42. package/dest/lmdb-v2/read_transaction.js +21 -19
  43. package/dest/lmdb-v2/store.d.ts +2 -2
  44. package/dest/lmdb-v2/store.d.ts.map +1 -1
  45. package/dest/lmdb-v2/store.js +4 -4
  46. package/dest/sqlite-opfs/array.d.ts +21 -0
  47. package/dest/sqlite-opfs/array.d.ts.map +1 -0
  48. package/dest/sqlite-opfs/array.js +128 -0
  49. package/dest/sqlite-opfs/errors.d.ts +27 -0
  50. package/dest/sqlite-opfs/errors.d.ts.map +1 -0
  51. package/dest/sqlite-opfs/errors.js +34 -0
  52. package/dest/sqlite-opfs/index.d.ts +16 -0
  53. package/dest/sqlite-opfs/index.d.ts.map +1 -0
  54. package/dest/sqlite-opfs/index.js +22 -0
  55. package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts +32 -0
  56. package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts.map +1 -0
  57. package/dest/sqlite-opfs/internal/ordered-binary-browser.js +448 -0
  58. package/dest/sqlite-opfs/map.d.ts +35 -0
  59. package/dest/sqlite-opfs/map.d.ts.map +1 -0
  60. package/dest/sqlite-opfs/map.js +163 -0
  61. package/dest/sqlite-opfs/messages.d.ts +66 -0
  62. package/dest/sqlite-opfs/messages.d.ts.map +1 -0
  63. package/dest/sqlite-opfs/messages.js +5 -0
  64. package/dest/sqlite-opfs/multi_map.d.ts +16 -0
  65. package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
  66. package/dest/sqlite-opfs/multi_map.js +67 -0
  67. package/dest/sqlite-opfs/set.d.ts +13 -0
  68. package/dest/sqlite-opfs/set.d.ts.map +1 -0
  69. package/dest/sqlite-opfs/set.js +19 -0
  70. package/dest/sqlite-opfs/singleton.d.ts +13 -0
  71. package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
  72. package/dest/sqlite-opfs/singleton.js +48 -0
  73. package/dest/sqlite-opfs/store.d.ts +79 -0
  74. package/dest/sqlite-opfs/store.d.ts.map +1 -0
  75. package/dest/sqlite-opfs/store.js +281 -0
  76. package/dest/sqlite-opfs/worker.d.ts +2 -0
  77. package/dest/sqlite-opfs/worker.d.ts.map +1 -0
  78. package/dest/sqlite-opfs/worker.js +248 -0
  79. package/dest/stores/l2_tips_store.d.ts +7 -13
  80. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  81. package/dest/stores/l2_tips_store.js +24 -43
  82. package/package.json +23 -20
  83. package/src/bench/shared_map_bench.ts +111 -0
  84. package/src/{indexeddb → deprecated/indexeddb}/array.ts +2 -2
  85. package/src/{indexeddb → deprecated/indexeddb}/index.ts +8 -2
  86. package/src/{indexeddb → deprecated/indexeddb}/map.ts +2 -2
  87. package/src/{indexeddb → deprecated/indexeddb}/multi_map.ts +2 -2
  88. package/src/{indexeddb → deprecated/indexeddb}/set.ts +2 -2
  89. package/src/{indexeddb → deprecated/indexeddb}/singleton.ts +2 -2
  90. package/src/{indexeddb → deprecated/indexeddb}/store.ts +10 -9
  91. package/src/interfaces/array_test_suite.ts +33 -35
  92. package/src/interfaces/index.ts +1 -1
  93. package/src/interfaces/map_test_suite.ts +32 -34
  94. package/src/interfaces/multi_map_test_suite.ts +65 -67
  95. package/src/interfaces/set_test_suite.ts +13 -15
  96. package/src/interfaces/singleton_test_suite.ts +6 -8
  97. package/src/lmdb/index.ts +2 -2
  98. package/src/lmdb-v2/factory.ts +85 -9
  99. package/src/lmdb-v2/read_transaction.ts +23 -23
  100. package/src/lmdb-v2/store.ts +4 -2
  101. package/src/sqlite-opfs/array.ts +124 -0
  102. package/src/sqlite-opfs/errors.ts +44 -0
  103. package/src/sqlite-opfs/index.ts +39 -0
  104. package/src/sqlite-opfs/internal/ordered-binary-browser.js +465 -0
  105. package/src/sqlite-opfs/map.ts +163 -0
  106. package/src/sqlite-opfs/messages.ts +39 -0
  107. package/src/sqlite-opfs/multi_map.ts +74 -0
  108. package/src/sqlite-opfs/set.ts +29 -0
  109. package/src/sqlite-opfs/singleton.ts +48 -0
  110. package/src/sqlite-opfs/store.ts +296 -0
  111. package/src/sqlite-opfs/worker.ts +230 -0
  112. package/src/stores/l2_tips_store.ts +20 -41
  113. package/dest/indexeddb/array.d.ts +0 -22
  114. package/dest/indexeddb/array.d.ts.map +0 -1
  115. package/dest/indexeddb/index.d.ts +0 -7
  116. package/dest/indexeddb/index.d.ts.map +0 -1
  117. package/dest/indexeddb/map.d.ts +0 -38
  118. package/dest/indexeddb/map.d.ts.map +0 -1
  119. package/dest/indexeddb/multi_map.d.ts +0 -14
  120. package/dest/indexeddb/multi_map.d.ts.map +0 -1
  121. package/dest/indexeddb/set.d.ts +0 -17
  122. package/dest/indexeddb/set.d.ts.map +0 -1
  123. package/dest/indexeddb/singleton.d.ts +0 -17
  124. package/dest/indexeddb/singleton.d.ts.map +0 -1
  125. package/dest/indexeddb/store.d.ts +0 -96
  126. package/dest/indexeddb/store.d.ts.map +0 -1
  127. /package/dest/{indexeddb → deprecated/indexeddb}/array.js +0 -0
  128. /package/dest/{indexeddb → deprecated/indexeddb}/map.js +0 -0
  129. /package/dest/{indexeddb → deprecated/indexeddb}/multi_map.js +0 -0
  130. /package/dest/{indexeddb → deprecated/indexeddb}/set.js +0 -0
  131. /package/dest/{indexeddb → deprecated/indexeddb}/singleton.js +0 -0
@@ -0,0 +1,74 @@
1
+ import { hash } from 'ohash';
2
+
3
+ import type { Key, Value } from '../interfaces/common.js';
4
+ import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
5
+ import { SQLiteOPFSAztecMap } from './map.js';
6
+
7
+ /**
8
+ * Multi-map backed by SQLite. Extends the base map with always-incrementing
9
+ * `key_count` per-key so the same slot is never reused across deletions — this
10
+ * matches the IndexedDB backend's sparse-multi-map semantics.
11
+ */
12
+ export class SQLiteOPFSAztecMultiMap<K extends Key, V extends Value>
13
+ extends SQLiteOPFSAztecMap<K, V>
14
+ implements AztecAsyncMultiMap<K, V>
15
+ {
16
+ override async set(key: K, val: V): Promise<void> {
17
+ const valueHash = hash(val);
18
+ await this.store.transactionAsync(async () => {
19
+ const exists = await this.store.allAsync(
20
+ 'SELECT 1 FROM data WHERE container = ? AND key = ? AND hash = ? LIMIT 1',
21
+ [this.container, this.encodedKey(key), valueHash],
22
+ );
23
+ if (exists.length > 0) {
24
+ return;
25
+ }
26
+ const maxRow = await this.store.allAsync('SELECT MAX(key_count) FROM data WHERE container = ? AND key = ?', [
27
+ this.container,
28
+ this.encodedKey(key),
29
+ ]);
30
+ const count = Number(maxRow[0]?.[0] ?? 0);
31
+ await this.store.runAsync(
32
+ `INSERT INTO data (slot, container, key, key_count, hash, value)
33
+ VALUES (?, ?, ?, ?, ?, ?)`,
34
+ [this.slot(key, count), this.container, this.encodedKey(key), count + 1, valueHash, this.encoder.pack(val)],
35
+ );
36
+ });
37
+ }
38
+
39
+ async *getValuesAsync(key: K): AsyncIterableIterator<V> {
40
+ const rows = await this.store.allAsync(
41
+ 'SELECT value FROM data WHERE container = ? AND key = ? ORDER BY key_count ASC',
42
+ [this.container, this.encodedKey(key)],
43
+ );
44
+ for (const row of rows) {
45
+ const raw = row[0];
46
+ if (raw instanceof Uint8Array) {
47
+ yield this.decodeValue(raw);
48
+ }
49
+ }
50
+ }
51
+
52
+ async getValueCountAsync(key: K): Promise<number> {
53
+ const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ? AND key = ?', [
54
+ this.container,
55
+ this.encodedKey(key),
56
+ ]);
57
+ return Number(rows[0]?.[0] ?? 0);
58
+ }
59
+
60
+ async deleteValue(key: K, val: V): Promise<void> {
61
+ await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ? AND hash = ?', [
62
+ this.container,
63
+ this.encodedKey(key),
64
+ hash(val),
65
+ ]);
66
+ }
67
+
68
+ override async delete(key: K): Promise<void> {
69
+ await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ?', [
70
+ this.container,
71
+ this.encodedKey(key),
72
+ ]);
73
+ }
74
+ }
@@ -0,0 +1,29 @@
1
+ import type { Key, Range } from '../interfaces/common.js';
2
+ import type { AztecAsyncSet } from '../interfaces/set.js';
3
+ import { SQLiteOPFSAztecMap } from './map.js';
4
+ import type { AztecSQLiteOPFSStore } from './store.js';
5
+
6
+ /** Set backed by SQLite. Composes a Map<K, true>. */
7
+ export class SQLiteOPFSAztecSet<K extends Key> implements AztecAsyncSet<K> {
8
+ readonly #map: SQLiteOPFSAztecMap<K, boolean>;
9
+
10
+ constructor(store: AztecSQLiteOPFSStore, name: string) {
11
+ this.#map = new SQLiteOPFSAztecMap<K, boolean>(store, name);
12
+ }
13
+
14
+ hasAsync(key: K): Promise<boolean> {
15
+ return this.#map.hasAsync(key);
16
+ }
17
+
18
+ add(key: K): Promise<void> {
19
+ return this.#map.set(key, true);
20
+ }
21
+
22
+ delete(key: K): Promise<void> {
23
+ return this.#map.delete(key);
24
+ }
25
+
26
+ async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
27
+ yield* this.#map.keysAsync(range);
28
+ }
29
+ }
@@ -0,0 +1,48 @@
1
+ import { Encoder } from '#msgpackr';
2
+ import { toBufferKey } from '#ordered-binary';
3
+ import { hash } from 'ohash';
4
+
5
+ import type { Value } from '../interfaces/common.js';
6
+ import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
7
+ import type { AztecSQLiteOPFSStore } from './store.js';
8
+
9
+ /** Stores a single value identified by `name`. */
10
+ export class SQLiteOPFSAztecSingleton<T extends Value> implements AztecAsyncSingleton<T> {
11
+ readonly #container: string;
12
+ readonly #slot: string;
13
+ readonly #encoder = new Encoder();
14
+
15
+ constructor(
16
+ private readonly store: AztecSQLiteOPFSStore,
17
+ name: string,
18
+ ) {
19
+ this.#container = `singleton:${name}`;
20
+ this.#slot = `singleton:${name}:value`;
21
+ }
22
+
23
+ async getAsync(): Promise<T | undefined> {
24
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.#slot]);
25
+ if (rows.length === 0) {
26
+ return undefined;
27
+ }
28
+ const raw = rows[0][0];
29
+ if (raw instanceof Uint8Array) {
30
+ return this.#encoder.unpack(raw) as T;
31
+ }
32
+ return undefined;
33
+ }
34
+
35
+ async set(val: T): Promise<boolean> {
36
+ const { changes } = await this.store.runAsync(
37
+ `INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
38
+ VALUES (?, ?, ?, ?, ?, ?)`,
39
+ [this.#slot, this.#container, toBufferKey([this.#slot]), 1, hash(val), this.#encoder.pack(val)],
40
+ );
41
+ return changes > 0;
42
+ }
43
+
44
+ async delete(): Promise<boolean> {
45
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [this.#slot]);
46
+ return true;
47
+ }
48
+ }
@@ -0,0 +1,296 @@
1
+ import type { Logger } from '@aztec/foundation/log';
2
+ import { SerialQueue } from '@aztec/foundation/queue';
3
+
4
+ import type { AztecAsyncArray } from '../interfaces/array.js';
5
+ import type { Key, StoreSize, Value } from '../interfaces/common.js';
6
+ import type { AztecAsyncCounter } from '../interfaces/counter.js';
7
+ import type { AztecAsyncMap } from '../interfaces/map.js';
8
+ import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
9
+ import type { AztecAsyncSet } from '../interfaces/set.js';
10
+ import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
11
+ import type { AztecAsyncKVStore } from '../interfaces/store.js';
12
+ import { SQLiteOPFSAztecArray } from './array.js';
13
+ import { SqliteEncryptionError } from './errors.js';
14
+ import { SQLiteOPFSAztecMap } from './map.js';
15
+ import type { ResultRow, SqlValue, WorkerRequest, WorkerResponse } from './messages.js';
16
+ import { SQLiteOPFSAztecMultiMap } from './multi_map.js';
17
+ import { SQLiteOPFSAztecSet } from './set.js';
18
+ import { SQLiteOPFSAztecSingleton } from './singleton.js';
19
+
20
+ /**
21
+ * Main-thread handle for a SQLite database persisted to OPFS via the `opfs-sahpool`
22
+ * VFS. Owns a dedicated Web Worker (the SAH Pool VFS requires Worker context) and
23
+ * routes every SQL op through it via typed postMessage RPC.
24
+ *
25
+ * Transaction ordering is guaranteed by a `SerialQueue` on the main thread combined
26
+ * with an `#inTx` flag: outside a `transactionAsync` block, each op acquires the
27
+ * queue for its own auto-commit; inside a block, the outer call holds the queue and
28
+ * nested ops bypass it to avoid deadlock.
29
+ */
30
+ export class AztecSQLiteOPFSStore implements AztecAsyncKVStore {
31
+ readonly #worker: Worker;
32
+ readonly #pending = new Map<number, { resolve: (r: WorkerResponse) => void; reject: (err: Error) => void }>();
33
+ readonly #txQueue = new SerialQueue();
34
+ readonly #name: string;
35
+ readonly #log: Logger;
36
+ #nextId = 0;
37
+ #inTx = false;
38
+ #closed = false;
39
+
40
+ private constructor(
41
+ worker: Worker,
42
+ name: string,
43
+ log: Logger,
44
+ public readonly isEphemeral: boolean,
45
+ ) {
46
+ this.#worker = worker;
47
+ this.#name = name;
48
+ this.#log = log;
49
+ this.#worker.onmessage = (ev: MessageEvent<WorkerResponse>) => {
50
+ const { id } = ev.data;
51
+ const handler = this.#pending.get(id);
52
+ if (!handler) {
53
+ this.#log.warn(`SQLite worker: no pending handler for id ${id}`);
54
+ return;
55
+ }
56
+ this.#pending.delete(id);
57
+ handler.resolve(ev.data);
58
+ };
59
+ this.#worker.onerror = ev => {
60
+ this.#log.error(`SQLite worker crashed: ${ev.message}`);
61
+ this.#rejectPending(`SQLite worker crashed: ${ev.message}`);
62
+ };
63
+ this.#txQueue.start();
64
+ }
65
+
66
+ /**
67
+ * Opens (or creates) a SQLite database stored in the OPFS SAH Pool. When `ephemeral`
68
+ * is true the database lives only in memory and is lost when the worker terminates.
69
+ * Pass `poolDirectory` to place the SAH Pool in a non-default OPFS subdirectory —
70
+ * required when multiple stores coexist in the same tab, because the SAH Pool holds
71
+ * an exclusive lock on its directory.
72
+ *
73
+ * Pass `encryptionKey` (exactly 32 bytes) to enable at-rest encryption via sqlite3mc's
74
+ * ChaCha20 page cipher. The key buffer is **transferred** to the worker — its
75
+ * ArrayBuffer detaches on the caller side after `postMessage`. This is intentional:
76
+ * the API encodes a one-key-one-owner invariant. A caller that wants to use the same
77
+ * key for multiple stores must explicitly clone it per call (e.g.
78
+ * `new Uint8Array(savedKey)`), making the duplication a visible, deliberate decision
79
+ * rather than a silent structured-clone operation. The default path (one `.open()`,
80
+ * one consumption of the key) leaves zero key bytes on the main thread after the call.
81
+ */
82
+ static async open(
83
+ log: Logger,
84
+ name?: string,
85
+ ephemeral: boolean = false,
86
+ poolDirectory?: string,
87
+ encryptionKey?: Uint8Array,
88
+ ): Promise<AztecSQLiteOPFSStore> {
89
+ if (encryptionKey !== undefined && encryptionKey.length !== 32) {
90
+ throw new SqliteEncryptionError(
91
+ 'invalid_key_length',
92
+ `encryptionKey must be 32 bytes (got ${encryptionKey.length})`,
93
+ );
94
+ }
95
+ if (encryptionKey !== undefined && ephemeral) {
96
+ throw new SqliteEncryptionError(
97
+ 'encryption_not_supported_for_ephemeral',
98
+ 'encryptionKey is not supported for ephemeral (:memory:) stores',
99
+ );
100
+ }
101
+ const dbName = name && !ephemeral ? name : `tmp-${globalThis.crypto.getRandomValues(new Uint8Array(8)).join('')}`;
102
+ log.debug(
103
+ `Opening SQLite-OPFS ${ephemeral ? 'ephemeral ' : ''}${encryptionKey ? 'encrypted ' : ''}database ${dbName}`,
104
+ );
105
+ const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
106
+ const store = new AztecSQLiteOPFSStore(worker, dbName, log, ephemeral);
107
+ // Transfer (not clone) the key buffer to the worker so we don't leave a
108
+ // second copy on the main thread. Caveat: this detaches the caller's
109
+ // encryptionKey.buffer — subsequent reads from the same Uint8Array are empty.
110
+ const transfer = encryptionKey ? [encryptionKey.buffer as ArrayBuffer] : undefined;
111
+ try {
112
+ await store.#sendRequest(
113
+ { type: 'init', id: store.#allocId(), dbName, ephemeral, poolDirectory, encryptionKey },
114
+ transfer,
115
+ );
116
+ } catch (err) {
117
+ worker.terminate();
118
+ throw err;
119
+ }
120
+ return store;
121
+ }
122
+
123
+ openMap<K extends Key, V extends Value>(name: string): AztecAsyncMap<K, V> {
124
+ return new SQLiteOPFSAztecMap<K, V>(this, name);
125
+ }
126
+
127
+ openSet<K extends Key>(name: string): AztecAsyncSet<K> {
128
+ return new SQLiteOPFSAztecSet<K>(this, name);
129
+ }
130
+
131
+ openMultiMap<K extends Key, V extends Value>(name: string): AztecAsyncMultiMap<K, V> {
132
+ return new SQLiteOPFSAztecMultiMap<K, V>(this, name);
133
+ }
134
+
135
+ openCounter<K extends Key>(_name: string): AztecAsyncCounter<K> {
136
+ throw new Error('Method not implemented.');
137
+ }
138
+
139
+ openArray<T extends Value>(name: string): AztecAsyncArray<T> {
140
+ return new SQLiteOPFSAztecArray<T>(this, name);
141
+ }
142
+
143
+ openSingleton<T extends Value>(name: string): AztecAsyncSingleton<T> {
144
+ return new SQLiteOPFSAztecSingleton<T>(this, name);
145
+ }
146
+
147
+ transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
148
+ // Nested calls join the outer transaction — SQLite does not support nested BEGIN,
149
+ // and re-acquiring the SerialQueue while the outer call holds it would deadlock.
150
+ // Errors in the nested callback propagate to the outer catch, which rolls back the
151
+ // whole thing (the standard "nested tx = savepoint-free join" semantic).
152
+ if (this.#inTx) {
153
+ return callback();
154
+ }
155
+ return this.#txQueue.put(async () => {
156
+ this.#inTx = true;
157
+ await this.#sendRequest({ type: 'begin', id: this.#allocId() });
158
+ try {
159
+ const result = await callback();
160
+ await this.#sendRequest({ type: 'commit', id: this.#allocId() });
161
+ return result;
162
+ } catch (err) {
163
+ await this.#sendRequest({ type: 'rollback', id: this.#allocId() }).catch(rollbackErr =>
164
+ this.#log.warn(`SQLite ROLLBACK failed: ${rollbackErr instanceof Error ? rollbackErr.message : rollbackErr}`),
165
+ );
166
+ throw err;
167
+ } finally {
168
+ this.#inTx = false;
169
+ }
170
+ });
171
+ }
172
+
173
+ async clear(): Promise<void> {
174
+ await this.runAsync('DELETE FROM data');
175
+ }
176
+
177
+ async delete(): Promise<void> {
178
+ if (this.#closed) {
179
+ return;
180
+ }
181
+ this.#closed = true;
182
+ await this.#txQueue.end();
183
+ await this.#sendRequest({ type: 'deleteDb', id: this.#allocId(), dbName: this.#name }).catch(err =>
184
+ this.#log.warn(`SQLite deleteDb failed: ${err instanceof Error ? err.message : err}`),
185
+ );
186
+ this.#worker.terminate();
187
+ this.#rejectPending('SQLite store deleted');
188
+ }
189
+
190
+ /**
191
+ * Placeholder — returns zeros to mirror the IndexedDB backend. SQLite exposes real
192
+ * numbers cheaply via `PRAGMA page_count` / `page_size` / `freelist_count` and
193
+ * `SELECT COUNT(*) FROM data`, which would populate `physicalFileSize`, `actualSize`,
194
+ * and `numItems` meaningfully (`mappingSize` stays 0 — it's an LMDB mmap concept).
195
+ * Upgrade when any caller actually consumes these values; all current consumers
196
+ * tolerate zeros.
197
+ */
198
+ estimateSize(): Promise<StoreSize> {
199
+ return Promise.resolve({ mappingSize: 0, physicalFileSize: 0, actualSize: 0, numItems: 0 });
200
+ }
201
+
202
+ async close(): Promise<void> {
203
+ if (this.#closed) {
204
+ return;
205
+ }
206
+ this.#closed = true;
207
+ await this.#txQueue.end();
208
+ await this.#sendRequest({ type: 'close', id: this.#allocId() }).catch(() => {});
209
+ this.#worker.terminate();
210
+ this.#rejectPending('SQLite store closed');
211
+ }
212
+
213
+ backupTo(_dstPath: string, _compact?: boolean): Promise<void> {
214
+ throw new Error('Method not implemented.');
215
+ }
216
+
217
+ /**
218
+ * Returns a raw SQLite image (bytes suitable for writing as a `.sqlite` file and
219
+ * opening in any SQLite tool). Works only for non-ephemeral DBs because the OPFS
220
+ * SAH Pool has to be initialized. Useful for inspection/debugging.
221
+ */
222
+ async exportDb(): Promise<Uint8Array> {
223
+ const resp = await this.#sendRequest({ type: 'export', id: this.#allocId() });
224
+ if (!('bytes' in resp) || !resp.bytes) {
225
+ throw new Error('exportDb: worker returned no bytes');
226
+ }
227
+ return resp.bytes;
228
+ }
229
+
230
+ /**
231
+ * Runs a write statement (INSERT/UPDATE/DELETE/DDL). If called inside a
232
+ * `transactionAsync` block, bypasses the queue; otherwise acquires it so the
233
+ * op runs in its own auto-commit.
234
+ */
235
+ runAsync(sql: string, bind?: SqlValue[]): Promise<{ changes: number }> {
236
+ const send = () =>
237
+ this.#sendRequest({ type: 'run', id: this.#allocId(), sql, bind }).then(r => ({
238
+ changes: 'changes' in r ? (r.changes ?? 0) : 0,
239
+ }));
240
+ return this.#inTx ? send() : this.#txQueue.put(send);
241
+ }
242
+
243
+ /** Runs a SELECT statement and returns rows in array row-mode. */
244
+ allAsync(sql: string, bind?: SqlValue[]): Promise<ResultRow[]> {
245
+ const send = () =>
246
+ this.#sendRequest({ type: 'all', id: this.#allocId(), sql, bind }).then(r => ('rows' in r ? (r.rows ?? []) : []));
247
+ return this.#inTx ? send() : this.#txQueue.put(send);
248
+ }
249
+
250
+ #allocId(): number {
251
+ return ++this.#nextId;
252
+ }
253
+
254
+ /**
255
+ * Reject any in-flight requests with `reason`, so callers awaiting a response to a
256
+ * request sent to a now-terminated worker don't hang forever. Called from
257
+ * close()/delete() and from the worker.onerror handler.
258
+ */
259
+ #rejectPending(reason: string): void {
260
+ if (this.#pending.size === 0) {
261
+ return;
262
+ }
263
+ const err = new Error(reason);
264
+ for (const { reject } of this.#pending.values()) {
265
+ reject(err);
266
+ }
267
+ this.#pending.clear();
268
+ }
269
+
270
+ #sendRequest(req: WorkerRequest, transfer?: Transferable[]): Promise<WorkerResponse> {
271
+ return new Promise<WorkerResponse>((resolve, reject) => {
272
+ this.#pending.set(req.id, {
273
+ resolve: resp => {
274
+ if (resp.type === 'err') {
275
+ // Re-hydrate encryption-shaped errors as the typed class so consumers
276
+ // can pattern-match on `instanceof SqliteEncryptionError`. Plain
277
+ // errors stay plain — the wire protocol only tags encryption paths.
278
+ if (resp.encryptionCode !== undefined) {
279
+ reject(new SqliteEncryptionError(resp.encryptionCode, resp.message));
280
+ } else {
281
+ reject(new Error(resp.message));
282
+ }
283
+ } else {
284
+ resolve(resp);
285
+ }
286
+ },
287
+ reject,
288
+ });
289
+ if (transfer && transfer.length > 0) {
290
+ this.#worker.postMessage(req, transfer);
291
+ } else {
292
+ this.#worker.postMessage(req);
293
+ }
294
+ });
295
+ }
296
+ }
@@ -0,0 +1,230 @@
1
+ /// <reference lib="webworker" />
2
+ import sqlite3InitModule, { type Database, type SAHPoolUtil, type Sqlite3Static } from '@aztec/sqlite3mc-wasm';
3
+
4
+ import { SqliteEncryptionError, type SqliteEncryptionErrorCode, isDecryptFailureMessage } from './errors.js';
5
+ import type { ResultRow, SqlValue, WorkerRequest, WorkerResponse } from './messages.js';
6
+
7
+ const SCHEMA_SQL = `
8
+ CREATE TABLE IF NOT EXISTS data (
9
+ slot TEXT NOT NULL PRIMARY KEY,
10
+ container TEXT NOT NULL,
11
+ key BLOB NOT NULL,
12
+ key_count INTEGER NOT NULL,
13
+ hash TEXT NOT NULL,
14
+ value BLOB
15
+ ) WITHOUT ROWID;
16
+
17
+ CREATE INDEX IF NOT EXISTS idx_container_key ON data(container, key);
18
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_container_key_count ON data(container, key, key_count);
19
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_container_key_hash ON data(container, key, hash);
20
+ `;
21
+
22
+ const DEFAULT_SAH_POOL_DIRECTORY = '.aztec-kv';
23
+ const SAH_POOL_VFS_NAME = 'aztec-kv-opfs';
24
+ const MC_SAH_POOL_VFS_NAME = `multipleciphers-${SAH_POOL_VFS_NAME}`;
25
+
26
+ let sqlite3: Sqlite3Static | undefined;
27
+ let pool: SAHPoolUtil | undefined;
28
+ let db: Database | undefined;
29
+ let dbPath: string | undefined;
30
+
31
+ async function ensurePool(directory: string): Promise<SAHPoolUtil> {
32
+ const s = (sqlite3 ??= await sqlite3InitModule());
33
+ if (!pool) {
34
+ pool = await s.installOpfsSAHPoolVfs({
35
+ name: SAH_POOL_VFS_NAME,
36
+ directory,
37
+ initialCapacity: 8,
38
+ });
39
+ // Register a sqlite3mc-wrapped VFS pointing at our SAH Pool VFS.
40
+ // Encrypted DBs must be opened through this wrapper so sqlite3mc can
41
+ // intercept file I/O; plain DBs continue using the SAH Pool VFS directly.
42
+ // The wrapper name is `multipleciphers-<underlying>`.
43
+ (s.capi as unknown as { sqlite3mc_vfs_create(name: string, makeDefault: number): number }).sqlite3mc_vfs_create(
44
+ SAH_POOL_VFS_NAME,
45
+ 0,
46
+ );
47
+ }
48
+ return pool!;
49
+ }
50
+
51
+ /**
52
+ * Applies sqlite3mc's ChaCha20 page cipher using a pre-derived 32-byte key.
53
+ * The PRAGMAs must run before any schema DDL so sqlite3mc can decrypt existing
54
+ * pages and encrypt new ones. Zeroes the caller-held key array after the PRAGMA
55
+ * completes to minimize residency of the raw key bytes outside sqlite3mc's heap.
56
+ */
57
+ function applyEncryptionKey(conn: Database, key: Uint8Array): void {
58
+ const hex = Array.from(key, b => b.toString(16).padStart(2, '0')).join('');
59
+ conn.exec(`PRAGMA cipher = 'chacha20'`);
60
+ conn.exec(`PRAGMA key = "x'${hex}'"`);
61
+ key.fill(0);
62
+ }
63
+
64
+ async function handleInit(
65
+ dbName: string,
66
+ ephemeral: boolean,
67
+ directory?: string,
68
+ encryptionKey?: Uint8Array,
69
+ ): Promise<void> {
70
+ const s = (sqlite3 ??= await sqlite3InitModule());
71
+ if (encryptionKey !== undefined && ephemeral) {
72
+ throw new SqliteEncryptionError(
73
+ 'encryption_not_supported_for_ephemeral',
74
+ 'encryptionKey is not supported for ephemeral (:memory:) stores',
75
+ );
76
+ }
77
+ if (ephemeral) {
78
+ db = new s.oo1.DB(':memory:', 'c');
79
+ } else {
80
+ const activePool = await ensurePool(directory ?? DEFAULT_SAH_POOL_DIRECTORY);
81
+ dbPath = normalizeDbPath(dbName);
82
+ if (encryptionKey !== undefined) {
83
+ const conn = new s.oo1.DB({ filename: dbPath, flags: 'c', vfs: MC_SAH_POOL_VFS_NAME });
84
+ db = conn;
85
+ applyEncryptionKey(conn, encryptionKey);
86
+ } else {
87
+ db = new activePool.OpfsSAHPoolDb(dbPath);
88
+ }
89
+ }
90
+ runSql(SCHEMA_SQL);
91
+ }
92
+
93
+ function handleClose(): void {
94
+ db?.close();
95
+ db = undefined;
96
+ dbPath = undefined;
97
+ }
98
+
99
+ async function handleExport(): Promise<Uint8Array> {
100
+ if (!db || !dbPath) {
101
+ throw new Error('SQLite worker: no database open to export');
102
+ }
103
+ if (!pool) {
104
+ throw new Error('SQLite worker: no SAH Pool available (ephemeral DBs cannot be exported)');
105
+ }
106
+ return await pool.exportFile(dbPath);
107
+ }
108
+
109
+ function handleDeleteDb(dbName: string): void {
110
+ const path = normalizeDbPath(dbName);
111
+ if (db && dbPath === path) {
112
+ db.close();
113
+ db = undefined;
114
+ dbPath = undefined;
115
+ }
116
+ // Ephemeral :memory: DBs never back a file — skip installing a pool just to unlink
117
+ // nothing. installOpfsSAHPoolVfs acquires an exclusive lock on the OPFS SAH
118
+ // directory, and under heavy test churn that can contend with workers from
119
+ // previous tests whose OPFS handles Chromium hasn't yet released, hanging the RPC
120
+ // and then the whole test run.
121
+ if (!pool) {
122
+ return;
123
+ }
124
+ try {
125
+ pool.unlink(path);
126
+ } catch {
127
+ // File may not exist; ignore.
128
+ }
129
+ }
130
+
131
+ function requireDb(): Database {
132
+ if (!db) {
133
+ throw new Error('SQLite worker: no database open');
134
+ }
135
+ return db;
136
+ }
137
+
138
+ function runSql(sql: string, bind?: SqlValue[]): { changes: number } {
139
+ const conn = requireDb();
140
+ conn.exec({ sql, bind });
141
+ return { changes: conn.changes() };
142
+ }
143
+
144
+ function selectAll(sql: string, bind?: SqlValue[]): ResultRow[] {
145
+ const conn = requireDb();
146
+ const rows: ResultRow[] = [];
147
+ conn.exec({ sql, bind, rowMode: 'array', resultRows: rows });
148
+ return rows;
149
+ }
150
+
151
+ function normalizeDbPath(dbName: string): string {
152
+ return dbName.startsWith('/') ? dbName : `/${dbName}`;
153
+ }
154
+
155
+ function respond(msg: WorkerResponse): void {
156
+ (self as DedicatedWorkerGlobalScope).postMessage(msg);
157
+ }
158
+
159
+ (self as DedicatedWorkerGlobalScope).onmessage = async (ev: MessageEvent<WorkerRequest>) => {
160
+ const req = ev.data;
161
+ try {
162
+ switch (req.type) {
163
+ case 'init':
164
+ await handleInit(req.dbName, req.ephemeral, req.poolDirectory, req.encryptionKey);
165
+ return respond({ type: 'ok', id: req.id });
166
+ case 'close':
167
+ handleClose();
168
+ return respond({ type: 'ok', id: req.id });
169
+ case 'deleteDb':
170
+ handleDeleteDb(req.dbName);
171
+ return respond({ type: 'ok', id: req.id });
172
+ case 'run': {
173
+ const { changes } = runSql(req.sql, req.bind);
174
+ return respond({ type: 'ok', id: req.id, changes });
175
+ }
176
+ case 'all': {
177
+ const rows = selectAll(req.sql, req.bind);
178
+ return respond({ type: 'ok', id: req.id, rows });
179
+ }
180
+ case 'export': {
181
+ const bytes = await handleExport();
182
+ return respond({ type: 'ok', id: req.id, bytes });
183
+ }
184
+ case 'begin':
185
+ runSql('BEGIN');
186
+ return respond({ type: 'ok', id: req.id });
187
+ case 'commit':
188
+ runSql('COMMIT');
189
+ return respond({ type: 'ok', id: req.id });
190
+ case 'rollback':
191
+ runSql('ROLLBACK');
192
+ return respond({ type: 'ok', id: req.id });
193
+ default: {
194
+ const _exhaustive: never = req;
195
+ throw new Error(`Unknown request: ${JSON.stringify(_exhaustive)}`);
196
+ }
197
+ }
198
+ } catch (err) {
199
+ const message = err instanceof Error ? err.message : String(err);
200
+ respond({ type: 'err', id: req.id, message, encryptionCode: detectEncryptionCode(req, err, message) });
201
+ }
202
+ };
203
+
204
+ /**
205
+ * Maps a thrown error during request handling to a typed encryption code, so the
206
+ * main thread can re-hydrate it as a {@link SqliteEncryptionError}. Returns
207
+ * `undefined` for non-encryption errors (preserves the existing untyped path).
208
+ *
209
+ * Two sources:
210
+ * - The error was already a `SqliteEncryptionError` (pre-flight throws inside
211
+ * this worker — e.g. ephemeral + encryptionKey). Forward its code as-is.
212
+ * - The error came from SQLite/sqlite3mc with a known decrypt-failure message
213
+ * during an `init` request. Both "wrong key supplied" and "no key supplied
214
+ * to an encrypted DB" surface as SQLITE_NOTADB ("file is not a database") —
215
+ * we don't constrain on `req.encryptionKey` because the no-key-on-encrypted-DB
216
+ * case is exactly when the caller most needs the typed signal.
217
+ */
218
+ function detectEncryptionCode(
219
+ req: WorkerRequest,
220
+ err: unknown,
221
+ message: string,
222
+ ): SqliteEncryptionErrorCode | undefined {
223
+ if (err instanceof SqliteEncryptionError) {
224
+ return err.code;
225
+ }
226
+ if (req.type === 'init' && isDecryptFailureMessage(message)) {
227
+ return 'decrypt_failed';
228
+ }
229
+ return undefined;
230
+ }