@aztec/kv-store 0.0.1-commit.b9865e97 → 0.0.1-commit.be03c316

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 (80) hide show
  1. package/README.md +10 -8
  2. package/dest/deprecated/indexeddb/array.d.ts +22 -0
  3. package/dest/deprecated/indexeddb/array.d.ts.map +1 -0
  4. package/dest/deprecated/indexeddb/index.d.ts +7 -0
  5. package/dest/deprecated/indexeddb/index.d.ts.map +1 -0
  6. package/dest/deprecated/indexeddb/index.js +8 -0
  7. package/dest/deprecated/indexeddb/map.d.ts +38 -0
  8. package/dest/deprecated/indexeddb/map.d.ts.map +1 -0
  9. package/dest/deprecated/indexeddb/multi_map.d.ts +14 -0
  10. package/dest/deprecated/indexeddb/multi_map.d.ts.map +1 -0
  11. package/dest/deprecated/indexeddb/set.d.ts +17 -0
  12. package/dest/deprecated/indexeddb/set.d.ts.map +1 -0
  13. package/dest/deprecated/indexeddb/singleton.d.ts +17 -0
  14. package/dest/deprecated/indexeddb/singleton.d.ts.map +1 -0
  15. package/dest/deprecated/indexeddb/store.d.ts +98 -0
  16. package/dest/deprecated/indexeddb/store.d.ts.map +1 -0
  17. package/dest/{indexeddb → deprecated/indexeddb}/store.js +2 -0
  18. package/dest/lmdb-v2/factory.d.ts +3 -2
  19. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  20. package/dest/lmdb-v2/factory.js +2 -1
  21. package/dest/sqlite-opfs/errors.d.ts +28 -1
  22. package/dest/sqlite-opfs/errors.d.ts.map +1 -1
  23. package/dest/sqlite-opfs/errors.js +43 -0
  24. package/dest/sqlite-opfs/index.d.ts +3 -5
  25. package/dest/sqlite-opfs/index.d.ts.map +1 -1
  26. package/dest/sqlite-opfs/index.js +2 -8
  27. package/dest/sqlite-opfs/manage.d.ts +22 -0
  28. package/dest/sqlite-opfs/manage.d.ts.map +1 -0
  29. package/dest/sqlite-opfs/manage.js +38 -0
  30. package/dest/sqlite-opfs/pool_integrity.d.ts +20 -0
  31. package/dest/sqlite-opfs/pool_integrity.d.ts.map +1 -0
  32. package/dest/sqlite-opfs/pool_integrity.js +282 -0
  33. package/dest/sqlite-opfs/pool_lock.d.ts +8 -0
  34. package/dest/sqlite-opfs/pool_lock.d.ts.map +1 -0
  35. package/dest/sqlite-opfs/pool_lock.js +47 -0
  36. package/dest/sqlite-opfs/store.d.ts +6 -1
  37. package/dest/sqlite-opfs/store.d.ts.map +1 -1
  38. package/dest/sqlite-opfs/store.js +69 -33
  39. package/dest/sqlite-opfs/worker.js +39 -13
  40. package/dest/stores/l2_tips_store.d.ts +6 -12
  41. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  42. package/dest/stores/l2_tips_store.js +22 -41
  43. package/package.json +8 -8
  44. package/src/{indexeddb → deprecated/indexeddb}/array.ts +2 -2
  45. package/src/deprecated/indexeddb/index.ts +12 -0
  46. package/src/{indexeddb → deprecated/indexeddb}/map.ts +2 -2
  47. package/src/{indexeddb → deprecated/indexeddb}/multi_map.ts +2 -2
  48. package/src/{indexeddb → deprecated/indexeddb}/set.ts +2 -2
  49. package/src/{indexeddb → deprecated/indexeddb}/singleton.ts +2 -2
  50. package/src/{indexeddb → deprecated/indexeddb}/store.ts +10 -9
  51. package/src/lmdb-v2/factory.ts +7 -1
  52. package/src/sqlite-opfs/errors.ts +50 -0
  53. package/src/sqlite-opfs/index.ts +8 -20
  54. package/src/sqlite-opfs/manage.ts +46 -0
  55. package/src/sqlite-opfs/pool_integrity.ts +327 -0
  56. package/src/sqlite-opfs/pool_lock.ts +62 -0
  57. package/src/sqlite-opfs/store.ts +57 -23
  58. package/src/sqlite-opfs/worker.ts +41 -13
  59. package/src/stores/l2_tips_store.ts +18 -40
  60. package/dest/indexeddb/array.d.ts +0 -22
  61. package/dest/indexeddb/array.d.ts.map +0 -1
  62. package/dest/indexeddb/index.d.ts +0 -7
  63. package/dest/indexeddb/index.d.ts.map +0 -1
  64. package/dest/indexeddb/index.js +0 -16
  65. package/dest/indexeddb/map.d.ts +0 -38
  66. package/dest/indexeddb/map.d.ts.map +0 -1
  67. package/dest/indexeddb/multi_map.d.ts +0 -14
  68. package/dest/indexeddb/multi_map.d.ts.map +0 -1
  69. package/dest/indexeddb/set.d.ts +0 -17
  70. package/dest/indexeddb/set.d.ts.map +0 -1
  71. package/dest/indexeddb/singleton.d.ts +0 -17
  72. package/dest/indexeddb/singleton.d.ts.map +0 -1
  73. package/dest/indexeddb/store.d.ts +0 -96
  74. package/dest/indexeddb/store.d.ts.map +0 -1
  75. package/src/indexeddb/index.ts +0 -31
  76. /package/dest/{indexeddb → deprecated/indexeddb}/array.js +0 -0
  77. /package/dest/{indexeddb → deprecated/indexeddb}/map.js +0 -0
  78. /package/dest/{indexeddb → deprecated/indexeddb}/multi_map.js +0 -0
  79. /package/dest/{indexeddb → deprecated/indexeddb}/set.js +0 -0
  80. /package/dest/{indexeddb → deprecated/indexeddb}/singleton.js +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pool_integrity.d.ts","sourceRoot":"","sources":["../../src/sqlite-opfs/pool_integrity.ts"],"names":[],"mappings":"AAyCA,eAAO,MAAM,8BAA8B,6BAA6B,CAAC;AAEzE,MAAM,WAAW,wBAAwB;IACvC,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,EAAE,CAAC;CAC3B;AAED,MAAM,WAAW,sBAAsB;IACrC,aAAa,EAAE,CAAC,CAAC;IACjB,qBAAqB,EAAE,MAAM,CAAC;IAC9B,aAAa,EAAE,MAAM,CAAC;IACtB,qBAAqB,EAAE,wBAAwB,EAAE,CAAC;CACnD;AAED,MAAM,WAAW,oBAAqB,SAAQ,sBAAsB;IAClE,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAED;;;GAGG;AACH,wBAAsB,uBAAuB,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,GAAG,SAAS,CAAC,CA6C9G"}
@@ -0,0 +1,282 @@
1
+ import { normalizePoolDirectory } from './pool_lock.js';
2
+ // The constants below mirror the opaque-file header format of the pinned opfs-sahpool VFS
3
+ // (`yarn-project/sqlite3mc-wasm/vendor/jswasm/sqlite3.mjs`, class `OpfsSAHPool`). The pool names its files randomly
4
+ // under `.opaque/` and prepends a header that maps each one back to its logical SQLite path:
5
+ //
6
+ // bytes [0, 512) logical path, NUL-terminated UTF-8 (HEADER_MAX_PATH_SIZE)
7
+ // bytes [512, 516) SQLite open-flags of the file, big-endian uint32 (HEADER_FLAGS_SIZE)
8
+ // bytes [516, 524) digest over the preceding 516 bytes, two uint32 words (HEADER_DIGEST_SIZE)
9
+ //
10
+ // Database content starts at byte 4096 (the pool's SECTOR_SIZE). We re-read the header ourselves rather than asking
11
+ // the VFS because the upstream pool "repairs" anything it cannot validate by disassociating the file — destroying
12
+ // exactly the evidence this module exists to quarantine. The browser regression test writes a real pool through the
13
+ // pinned VFS before duplicating an opaque file, so a vendor upgrade that changes the layout breaks detection loudly
14
+ // rather than silently.
15
+ const OPAQUE_DIRECTORY = '.opaque';
16
+ const HEADER_MAX_PATH_SIZE = 512;
17
+ const HEADER_FLAGS_SIZE = 4;
18
+ const HEADER_DIGEST_SIZE = 8;
19
+ const HEADER_CORPUS_SIZE = HEADER_MAX_PATH_SIZE + HEADER_FLAGS_SIZE;
20
+ const HEADER_SIZE = HEADER_CORPUS_SIZE + HEADER_DIGEST_SIZE;
21
+ // Standard SQLite open-flag bit values (sqlite3.h). Restated as literals because the header stores them numerically
22
+ // and this module runs on the main thread, without the sqlite3 WASM bundle that defines `capi.SQLITE_OPEN_*`.
23
+ const SQLITE_OPEN_DELETEONCLOSE = 0x00000008;
24
+ const SQLITE_OPEN_MEMORY = 0x00000080;
25
+ const SQLITE_OPEN_MAIN_DB = 0x00000100;
26
+ const SQLITE_OPEN_MAIN_JOURNAL = 0x00000800;
27
+ const SQLITE_OPEN_SUPER_JOURNAL = 0x00004000;
28
+ const SQLITE_OPEN_WAL = 0x00080000;
29
+ // A live association must name one of the file types the pool persists; transient types (temp DBs, statement
30
+ // journals) never survive in a valid header.
31
+ const PERSISTENT_FILE_TYPES = SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_SUPER_JOURNAL | SQLITE_OPEN_WAL;
32
+ // The upstream VFS repurposes SQLITE_OPEN_MEMORY — meaningless for a file that exists on disk — as a header version
33
+ // marker: headers written with it set carry a real digest, while legacy headers leave it unset and store all-zero
34
+ // digest words.
35
+ const FLAG_COMPUTE_DIGEST_V2 = SQLITE_OPEN_MEMORY;
36
+ export const OPFS_QUARANTINE_ROOT_DIRECTORY = '.aztec-sqlite-quarantine';
37
+ /**
38
+ * Detects duplicate SAH logical-file associations and, if found, copies the complete pool into quarantine before
39
+ * removing the original. The caller must hold the pool's exclusive Web Lock for the whole operation.
40
+ */ export async function quarantineDuplicatePool(poolDirectory) {
41
+ poolDirectory = normalizePoolDirectory(poolDirectory);
42
+ const root = await navigator.storage.getDirectory();
43
+ const source = await getDirectory(root, poolDirectory);
44
+ if (!source) {
45
+ return undefined;
46
+ }
47
+ const opaque = await getChildDirectory(source, OPAQUE_DIRECTORY);
48
+ if (!opaque) {
49
+ return undefined;
50
+ }
51
+ const duplicateAssociations = await findDuplicateAssociations(opaque);
52
+ if (duplicateAssociations.length === 0) {
53
+ return undefined;
54
+ }
55
+ const quarantineRoot = await root.getDirectoryHandle(OPFS_QUARANTINE_ROOT_DIRECTORY, {
56
+ create: true
57
+ });
58
+ const quarantineName = createQuarantineName();
59
+ const destination = await quarantineRoot.getDirectoryHandle(quarantineName, {
60
+ create: true
61
+ });
62
+ const metadata = {
63
+ formatVersion: 1,
64
+ originalPoolDirectory: poolDirectory,
65
+ quarantinedAt: new Date().toISOString(),
66
+ duplicateAssociations
67
+ };
68
+ let quarantineComplete = false;
69
+ try {
70
+ await copyDirectory(source, destination);
71
+ await verifyDirectoryCopy(source, destination);
72
+ await writeJson(destination, 'quarantine.json', metadata);
73
+ quarantineComplete = true;
74
+ await removeDirectory(root, poolDirectory);
75
+ } catch (err) {
76
+ if (!quarantineComplete) {
77
+ await quarantineRoot.removeEntry(quarantineName, {
78
+ recursive: true
79
+ }).catch(()=>{});
80
+ }
81
+ throw err;
82
+ }
83
+ return {
84
+ ...metadata,
85
+ quarantineDirectory: `${OPFS_QUARANTINE_ROOT_DIRECTORY}/${quarantineName}`
86
+ };
87
+ }
88
+ async function findDuplicateAssociations(opaque) {
89
+ const associations = new Map();
90
+ for await (const [opaqueName, handle] of opaque.entries()){
91
+ if (handle.kind !== 'file') {
92
+ continue;
93
+ }
94
+ const logicalPath = await readAssociatedPath(handle);
95
+ if (logicalPath) {
96
+ const names = associations.get(logicalPath) ?? [];
97
+ names.push(opaqueName);
98
+ associations.set(logicalPath, names);
99
+ }
100
+ }
101
+ return [
102
+ ...associations.entries()
103
+ ].filter(([, names])=>names.length > 1).map(([logicalPath, opaqueFileNames])=>({
104
+ logicalPath,
105
+ opaqueFileNames: opaqueFileNames.sort()
106
+ })).sort((a, b)=>a.logicalPath.localeCompare(b.logicalPath));
107
+ }
108
+ /**
109
+ * Returns the logical SQLite path an opaque SAH file is associated with, or undefined if the file is not a live,
110
+ * valid association. Applies the same checks as the vendored pool's `getAssociatedPath`: a non-empty NUL-terminated
111
+ * path, open-flags naming a persistent file type without DELETEONCLOSE, and a matching header digest. Files failing
112
+ * any check are the pool's free-list or garbage entries — the VFS itself would disassociate them on open — so they
113
+ * cannot participate in a duplicate mapping.
114
+ */ async function readAssociatedPath(handle) {
115
+ const file = await handle.getFile();
116
+ if (file.size < HEADER_SIZE) {
117
+ return undefined;
118
+ }
119
+ const header = new Uint8Array(await file.slice(0, HEADER_SIZE).arrayBuffer());
120
+ const pathEnd = header.subarray(0, HEADER_MAX_PATH_SIZE).indexOf(0);
121
+ if (pathEnd <= 0) {
122
+ return undefined;
123
+ }
124
+ const dataView = new DataView(header.buffer, header.byteOffset, header.byteLength);
125
+ const flags = dataView.getUint32(HEADER_MAX_PATH_SIZE);
126
+ if ((flags & SQLITE_OPEN_DELETEONCLOSE) !== 0 || (flags & PERSISTENT_FILE_TYPES) === 0) {
127
+ return undefined;
128
+ }
129
+ if (!hasValidDigest(header, flags)) {
130
+ return undefined;
131
+ }
132
+ try {
133
+ return new TextDecoder('utf-8', {
134
+ fatal: true
135
+ }).decode(header.subarray(0, pathEnd));
136
+ } catch {
137
+ return undefined;
138
+ }
139
+ }
140
+ /**
141
+ * Byte-for-byte port of the vendored pool's `computeDigest`, checked against the digest words stored in the header.
142
+ */ function hasValidDigest(header, flags) {
143
+ let expected0 = 0;
144
+ let expected1 = 0;
145
+ if ((flags & FLAG_COMPUTE_DIGEST_V2) !== 0) {
146
+ // These seeds (0xdeadbeef, 0x41c6ce57) and odd multipliers (2654435761, 104729) are the upstream author's choices
147
+ // (a cyrb53-hash variant) and carry no meaning here beyond having to match the vendored implementation bit for
148
+ // bit.
149
+ expected0 = 0xdeadbeef;
150
+ expected1 = 0x41c6ce57;
151
+ for (const value of header.subarray(0, HEADER_CORPUS_SIZE)){
152
+ expected0 = Math.imul(expected0 ^ value, 2654435761);
153
+ expected1 = Math.imul(expected1 ^ value, 104729);
154
+ }
155
+ expected0 >>>= 0;
156
+ expected1 >>>= 0;
157
+ }
158
+ const dataView = new DataView(header.buffer, header.byteOffset, header.byteLength);
159
+ return dataView.getUint32(HEADER_CORPUS_SIZE, true) === expected0 && dataView.getUint32(HEADER_CORPUS_SIZE + 4, true) === expected1;
160
+ }
161
+ async function getDirectory(root, path) {
162
+ let current = root;
163
+ try {
164
+ for (const segment of path.split('/')){
165
+ current = await current.getDirectoryHandle(segment);
166
+ }
167
+ return current;
168
+ } catch (err) {
169
+ if (err instanceof DOMException && err.name === 'NotFoundError') {
170
+ return undefined;
171
+ }
172
+ throw err;
173
+ }
174
+ }
175
+ async function getChildDirectory(parent, name) {
176
+ try {
177
+ return await parent.getDirectoryHandle(name);
178
+ } catch (err) {
179
+ if (err instanceof DOMException && err.name === 'NotFoundError') {
180
+ return undefined;
181
+ }
182
+ throw err;
183
+ }
184
+ }
185
+ async function removeDirectory(root, path) {
186
+ const segments = path.split('/');
187
+ const name = segments.pop();
188
+ let parent = root;
189
+ for (const segment of segments){
190
+ parent = await parent.getDirectoryHandle(segment);
191
+ }
192
+ await parent.removeEntry(name, {
193
+ recursive: true
194
+ });
195
+ }
196
+ async function copyDirectory(source, destination) {
197
+ for await (const [name, handle] of source.entries()){
198
+ if (handle.kind === 'directory') {
199
+ const childDestination = await destination.getDirectoryHandle(name, {
200
+ create: true
201
+ });
202
+ await copyDirectory(handle, childDestination);
203
+ continue;
204
+ }
205
+ const sourceFile = await handle.getFile();
206
+ const destinationHandle = await destination.getFileHandle(name, {
207
+ create: true
208
+ });
209
+ const writable = await destinationHandle.createWritable();
210
+ try {
211
+ await writable.write(sourceFile);
212
+ await writable.close();
213
+ } catch (err) {
214
+ await writable.abort().catch(()=>{});
215
+ throw err;
216
+ }
217
+ }
218
+ }
219
+ async function verifyDirectoryCopy(source, destination) {
220
+ const sourceEntries = await getSortedEntries(source);
221
+ const destinationEntries = await getSortedEntries(destination);
222
+ if (sourceEntries.length !== destinationEntries.length || sourceEntries.some(([name, handle], index)=>{
223
+ const destinationEntry = destinationEntries[index];
224
+ return name !== destinationEntry[0] || handle.kind !== destinationEntry[1].kind;
225
+ })) {
226
+ throw new Error('Failed to verify quarantined OPFS directory structure');
227
+ }
228
+ for(let i = 0; i < sourceEntries.length; i++){
229
+ const [, sourceHandle] = sourceEntries[i];
230
+ const [, destinationHandle] = destinationEntries[i];
231
+ if (sourceHandle.kind === 'directory') {
232
+ await verifyDirectoryCopy(sourceHandle, destinationHandle);
233
+ } else {
234
+ await verifyFilesEqual(sourceHandle, destinationHandle);
235
+ }
236
+ }
237
+ }
238
+ async function getSortedEntries(directory) {
239
+ const entries = [];
240
+ for await (const entry of directory.entries()){
241
+ entries.push(entry);
242
+ }
243
+ return entries.sort(([a], [b])=>a.localeCompare(b));
244
+ }
245
+ async function verifyFilesEqual(sourceHandle, destinationHandle) {
246
+ const source = await sourceHandle.getFile();
247
+ const destination = await destinationHandle.getFile();
248
+ if (source.size !== destination.size) {
249
+ throw new Error(`Failed to verify quarantined OPFS file "${source.name}"`);
250
+ }
251
+ const chunkSize = 1024 * 1024;
252
+ for(let offset = 0; offset < source.size; offset += chunkSize){
253
+ const [sourceChunk, destinationChunk] = await Promise.all([
254
+ source.slice(offset, offset + chunkSize).arrayBuffer(),
255
+ destination.slice(offset, offset + chunkSize).arrayBuffer()
256
+ ]);
257
+ const sourceBytes = new Uint8Array(sourceChunk);
258
+ const destinationBytes = new Uint8Array(destinationChunk);
259
+ if (sourceBytes.some((value, index)=>value !== destinationBytes[index])) {
260
+ throw new Error(`Failed to verify quarantined OPFS file "${source.name}"`);
261
+ }
262
+ }
263
+ }
264
+ async function writeJson(directory, name, value) {
265
+ const handle = await directory.getFileHandle(name, {
266
+ create: true
267
+ });
268
+ const writable = await handle.createWritable();
269
+ try {
270
+ await writable.write(JSON.stringify(value, undefined, 2));
271
+ await writable.close();
272
+ } catch (err) {
273
+ await writable.abort().catch(()=>{});
274
+ throw err;
275
+ }
276
+ }
277
+ function createQuarantineName() {
278
+ const random = globalThis.crypto.getRandomValues(new Uint8Array(8));
279
+ return `${Date.now()}-${[
280
+ ...random
281
+ ].map((byte)=>byte.toString(16).padStart(2, '0')).join('')}`;
282
+ }
@@ -0,0 +1,8 @@
1
+ export declare const DEFAULT_SAH_POOL_DIRECTORY = ".aztec-kv";
2
+ export interface PoolLockLease {
3
+ release(): Promise<void>;
4
+ }
5
+ export declare function normalizePoolDirectory(poolDirectory?: string): string;
6
+ export declare function acquirePoolLock(poolDirectory?: string): Promise<PoolLockLease>;
7
+ export declare function withPoolLock<T>(poolDirectory: string, callback: () => Promise<T>): Promise<T>;
8
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9vbF9sb2NrLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8u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navailableError } 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
+ }
@@ -21,6 +21,7 @@ import type { ResultRow, SqlValue } from './messages.js';
21
21
  export declare class AztecSQLiteOPFSStore implements AztecAsyncKVStore {
22
22
  #private;
23
23
  readonly isEphemeral: boolean;
24
+ private readonly poolLock?;
24
25
  private constructor();
25
26
  /**
26
27
  * Opens (or creates) a SQLite database stored in the OPFS SAH Pool. When `ephemeral`
@@ -29,6 +30,10 @@ export declare class AztecSQLiteOPFSStore implements AztecAsyncKVStore {
29
30
  * required when multiple stores coexist in the same tab, because the SAH Pool holds
30
31
  * an exclusive lock on its directory.
31
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
+ *
32
37
  * Pass `encryptionKey` (exactly 32 bytes) to enable at-rest encryption via sqlite3mc's
33
38
  * ChaCha20 page cipher. The key buffer is **transferred** to the worker — its
34
39
  * ArrayBuffer detaches on the caller side after `postMessage`. This is intentional:
@@ -76,4 +81,4 @@ export declare class AztecSQLiteOPFSStore implements AztecAsyncKVStore {
76
81
  /** Runs a SELECT statement and returns rows in array row-mode. */
77
82
  allAsync(sql: string, bind?: SqlValue[]): Promise<ResultRow[]>;
78
83
  }
79
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3RvcmUuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zcWxpdGUtb3Bmcy9zdG9yZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxNQUFNLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUdwRCxPQUFPLEtBQUssRUFBRSxlQUFlLEVBQUUsTUFBTSx3QkFBd0IsQ0FBQztBQUM5RCxPQUFPLEtBQUssRUFBRSxHQUFHLEVBQUUsU0FBUyxFQUFFLEtBQUssRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBQ3JFLE9BQU8sS0FBSyxFQUFFLGlCQUFpQixFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFDbEUsT0FBTyxLQUFLLEVBQUUsYUFBYSxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFDMUQsT0FBTyxLQUFLLEVBQUUsa0JBQWtCLEVBQUUsTUFBTSw0QkFBNEIsQ0FBQztBQUNyRSxPQUFPLEtBQUssRUFBRSxhQUFhLEVBQUUsTUFBTSxzQkFBc0IsQ0FBQztBQUMxRCxPQUFPLEtBQUssRU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ueue } from '@aztec/foundation/queue';
2
2
  import { SQLiteOPFSAztecArray } from './array.js';
3
- import { SqliteEncryptionError } from './errors.js';
3
+ import { SqliteCorruptionError, SqliteEncryptionError, isCorruptionMessage } from './errors.js';
4
4
  import { SQLiteOPFSAztecMap } from './map.js';
5
5
  import { SQLiteOPFSAztecMultiMap } from './multi_map.js';
6
+ import { quarantineDuplicatePool } from './pool_integrity.js';
7
+ import { acquirePoolLock, normalizePoolDirectory } from './pool_lock.js';
6
8
  import { SQLiteOPFSAztecSet } from './set.js';
7
9
  import { SQLiteOPFSAztecSingleton } from './singleton.js';
8
10
  /**
@@ -16,6 +18,7 @@ import { SQLiteOPFSAztecSingleton } from './singleton.js';
16
18
  * nested ops bypass it to avoid deadlock.
17
19
  */ export class AztecSQLiteOPFSStore {
18
20
  isEphemeral;
21
+ poolLock;
19
22
  #worker;
20
23
  #pending;
21
24
  #txQueue;
@@ -24,13 +27,16 @@ import { SQLiteOPFSAztecSingleton } from './singleton.js';
24
27
  #nextId;
25
28
  #inTx;
26
29
  #closed;
27
- constructor(worker, name, log, isEphemeral){
30
+ #workerFailed;
31
+ constructor(worker, name, log, isEphemeral, poolLock){
28
32
  this.isEphemeral = isEphemeral;
33
+ this.poolLock = poolLock;
29
34
  this.#pending = new Map();
30
35
  this.#txQueue = new SerialQueue();
31
36
  this.#nextId = 0;
32
37
  this.#inTx = false;
33
38
  this.#closed = false;
39
+ this.#workerFailed = false;
34
40
  this.#worker = worker;
35
41
  this.#name = name;
36
42
  this.#log = log;
@@ -45,6 +51,7 @@ import { SQLiteOPFSAztecSingleton } from './singleton.js';
45
51
  handler.resolve(ev.data);
46
52
  };
47
53
  this.#worker.onerror = (ev)=>{
54
+ this.#workerFailed = true;
48
55
  this.#log.error(`SQLite worker crashed: ${ev.message}`);
49
56
  this.#rejectPending(`SQLite worker crashed: ${ev.message}`);
50
57
  };
@@ -57,6 +64,10 @@ import { SQLiteOPFSAztecSingleton } from './singleton.js';
57
64
  * required when multiple stores coexist in the same tab, because the SAH Pool holds
58
65
  * an exclusive lock on its directory.
59
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
+ *
60
71
  * Pass `encryptionKey` (exactly 32 bytes) to enable at-rest encryption via sqlite3mc's
61
72
  * ChaCha20 page cipher. The key buffer is **transferred** to the worker — its
62
73
  * ArrayBuffer detaches on the caller side after `postMessage`. This is intentional:
@@ -74,30 +85,40 @@ import { SQLiteOPFSAztecSingleton } from './singleton.js';
74
85
  }
75
86
  const dbName = name && !ephemeral ? name : `tmp-${globalThis.crypto.getRandomValues(new Uint8Array(8)).join('')}`;
76
87
  log.debug(`Opening SQLite-OPFS ${ephemeral ? 'ephemeral ' : ''}${encryptionKey ? 'encrypted ' : ''}database ${dbName}`);
77
- const worker = new Worker(new URL('./worker.js', import.meta.url), {
78
- type: 'module'
79
- });
80
- const store = new AztecSQLiteOPFSStore(worker, dbName, log, ephemeral);
81
- // Transfer (not clone) the key buffer to the worker so we don't leave a
82
- // second copy on the main thread. Caveat: this detaches the caller's
83
- // encryptionKey.buffer — subsequent reads from the same Uint8Array are empty.
84
- const transfer = encryptionKey ? [
85
- encryptionKey.buffer
86
- ] : undefined;
88
+ const effectivePoolDirectory = ephemeral ? undefined : normalizePoolDirectory(poolDirectory);
89
+ const poolLock = effectivePoolDirectory ? await acquirePoolLock(effectivePoolDirectory) : undefined;
90
+ let worker;
87
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;
88
108
  await store.#sendRequest({
89
109
  type: 'init',
90
110
  id: store.#allocId(),
91
111
  dbName,
92
112
  ephemeral,
93
- poolDirectory,
113
+ poolDirectory: effectivePoolDirectory,
94
114
  encryptionKey
95
115
  }, transfer);
116
+ return store;
96
117
  } catch (err) {
97
- worker.terminate();
118
+ worker?.terminate();
119
+ await poolLock?.release();
98
120
  throw err;
99
121
  }
100
- return store;
101
122
  }
102
123
  openMap(name) {
103
124
  return new SQLiteOPFSAztecMap(this, name);
@@ -157,14 +178,18 @@ import { SQLiteOPFSAztecSingleton } from './singleton.js';
157
178
  return;
158
179
  }
159
180
  this.#closed = true;
160
- await this.#txQueue.end();
161
- await this.#sendRequest({
162
- type: 'deleteDb',
163
- id: this.#allocId(),
164
- dbName: this.#name
165
- }).catch((err)=>this.#log.warn(`SQLite deleteDb failed: ${err instanceof Error ? err.message : err}`));
166
- this.#worker.terminate();
167
- this.#rejectPending('SQLite store deleted');
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
+ }
168
193
  }
169
194
  /**
170
195
  * Placeholder — returns zeros to mirror the IndexedDB backend. SQLite exposes real
@@ -186,13 +211,17 @@ import { SQLiteOPFSAztecSingleton } from './singleton.js';
186
211
  return;
187
212
  }
188
213
  this.#closed = true;
189
- await this.#txQueue.end();
190
- await this.#sendRequest({
191
- type: 'close',
192
- id: this.#allocId()
193
- }).catch(()=>{});
194
- this.#worker.terminate();
195
- this.#rejectPending('SQLite store closed');
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
+ }
196
225
  }
197
226
  backupTo(_dstPath, _compact) {
198
227
  throw new Error('Method not implemented.');
@@ -253,15 +282,22 @@ import { SQLiteOPFSAztecSingleton } from './singleton.js';
253
282
  this.#pending.clear();
254
283
  }
255
284
  #sendRequest(req, transfer) {
285
+ if (this.#workerFailed) {
286
+ return Promise.reject(new Error('SQLite worker has crashed'));
287
+ }
256
288
  return new Promise((resolve, reject)=>{
257
289
  this.#pending.set(req.id, {
258
290
  resolve: (resp)=>{
259
291
  if (resp.type === 'err') {
260
- // Re-hydrate encryption-shaped errors as the typed class so consumers
261
- // can pattern-match on `instanceof SqliteEncryptionError`. Plain
262
- // errors stay plain the wire protocol only tags encryption paths.
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.
263
297
  if (resp.encryptionCode !== undefined) {
264
298
  reject(new SqliteEncryptionError(resp.encryptionCode, resp.message));
299
+ } else if (isCorruptionMessage(resp.message)) {
300
+ reject(new SqliteCorruptionError(resp.message));
265
301
  } else {
266
302
  reject(new Error(resp.message));
267
303
  }