@aztec/kv-store 0.0.1-commit.a89ec08 → 0.0.1-commit.aa0c64f
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.
- package/README.md +10 -1
- package/dest/bench/shared_map_bench.d.ts +19 -0
- package/dest/bench/shared_map_bench.d.ts.map +1 -0
- package/dest/bench/shared_map_bench.js +91 -0
- package/dest/deprecated/indexeddb/array.d.ts +22 -0
- package/dest/deprecated/indexeddb/array.d.ts.map +1 -0
- package/dest/deprecated/indexeddb/index.d.ts +7 -0
- package/dest/deprecated/indexeddb/index.d.ts.map +1 -0
- package/dest/deprecated/indexeddb/index.js +8 -0
- package/dest/deprecated/indexeddb/map.d.ts +38 -0
- package/dest/deprecated/indexeddb/map.d.ts.map +1 -0
- package/dest/deprecated/indexeddb/multi_map.d.ts +14 -0
- package/dest/deprecated/indexeddb/multi_map.d.ts.map +1 -0
- package/dest/deprecated/indexeddb/set.d.ts +17 -0
- package/dest/deprecated/indexeddb/set.d.ts.map +1 -0
- package/dest/deprecated/indexeddb/singleton.d.ts +17 -0
- package/dest/deprecated/indexeddb/singleton.d.ts.map +1 -0
- package/dest/deprecated/indexeddb/store.d.ts +98 -0
- package/dest/deprecated/indexeddb/store.d.ts.map +1 -0
- package/dest/{indexeddb → deprecated/indexeddb}/store.js +2 -0
- package/dest/interfaces/array_test_suite.d.ts +1 -1
- package/dest/interfaces/array_test_suite.d.ts.map +1 -1
- package/dest/interfaces/array_test_suite.js +33 -34
- package/dest/interfaces/index.d.ts +2 -2
- package/dest/interfaces/index.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.d.ts +1 -1
- package/dest/interfaces/map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.js +32 -33
- package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
- package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/multi_map_test_suite.js +68 -69
- package/dest/interfaces/set_test_suite.d.ts +1 -1
- package/dest/interfaces/set_test_suite.d.ts.map +1 -1
- package/dest/interfaces/set_test_suite.js +13 -14
- package/dest/interfaces/singleton_test_suite.d.ts +1 -1
- package/dest/interfaces/singleton_test_suite.d.ts.map +1 -1
- package/dest/interfaces/singleton_test_suite.js +6 -7
- package/dest/lmdb/index.d.ts +2 -2
- package/dest/lmdb/index.d.ts.map +1 -1
- package/dest/lmdb/index.js +2 -2
- package/dest/lmdb/store.d.ts +3 -3
- package/dest/lmdb/store.d.ts.map +1 -1
- package/dest/lmdb/store.js +12 -8
- package/dest/lmdb-v2/factory.d.ts +30 -4
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +62 -10
- package/dest/lmdb-v2/read_transaction.js +21 -19
- package/dest/lmdb-v2/store.d.ts +2 -2
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +4 -4
- package/dest/sqlite-opfs/array.d.ts +21 -0
- package/dest/sqlite-opfs/array.d.ts.map +1 -0
- package/dest/sqlite-opfs/array.js +128 -0
- package/dest/sqlite-opfs/errors.d.ts +54 -0
- package/dest/sqlite-opfs/errors.d.ts.map +1 -0
- package/dest/sqlite-opfs/errors.js +77 -0
- package/dest/sqlite-opfs/index.d.ts +14 -0
- package/dest/sqlite-opfs/index.d.ts.map +1 -0
- package/dest/sqlite-opfs/index.js +16 -0
- package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts +32 -0
- package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts.map +1 -0
- package/dest/sqlite-opfs/internal/ordered-binary-browser.js +448 -0
- package/dest/sqlite-opfs/manage.d.ts +22 -0
- package/dest/sqlite-opfs/manage.d.ts.map +1 -0
- package/dest/sqlite-opfs/manage.js +38 -0
- package/dest/sqlite-opfs/map.d.ts +35 -0
- package/dest/sqlite-opfs/map.d.ts.map +1 -0
- package/dest/sqlite-opfs/map.js +163 -0
- package/dest/sqlite-opfs/messages.d.ts +66 -0
- package/dest/sqlite-opfs/messages.d.ts.map +1 -0
- package/dest/sqlite-opfs/messages.js +5 -0
- package/dest/sqlite-opfs/multi_map.d.ts +16 -0
- package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
- package/dest/sqlite-opfs/multi_map.js +67 -0
- package/dest/sqlite-opfs/pool_integrity.d.ts +20 -0
- package/dest/sqlite-opfs/pool_integrity.d.ts.map +1 -0
- package/dest/sqlite-opfs/pool_integrity.js +282 -0
- package/dest/sqlite-opfs/pool_lock.d.ts +8 -0
- package/dest/sqlite-opfs/pool_lock.d.ts.map +1 -0
- package/dest/sqlite-opfs/pool_lock.js +47 -0
- package/dest/sqlite-opfs/set.d.ts +13 -0
- package/dest/sqlite-opfs/set.d.ts.map +1 -0
- package/dest/sqlite-opfs/set.js +19 -0
- package/dest/sqlite-opfs/singleton.d.ts +13 -0
- package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
- package/dest/sqlite-opfs/singleton.js +48 -0
- package/dest/sqlite-opfs/store.d.ts +84 -0
- package/dest/sqlite-opfs/store.d.ts.map +1 -0
- package/dest/sqlite-opfs/store.js +317 -0
- package/dest/sqlite-opfs/worker.d.ts +2 -0
- package/dest/sqlite-opfs/worker.d.ts.map +1 -0
- package/dest/sqlite-opfs/worker.js +275 -0
- package/dest/stores/l2_tips_store.d.ts +7 -13
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +24 -43
- package/package.json +24 -22
- package/src/bench/shared_map_bench.ts +111 -0
- package/src/{indexeddb → deprecated/indexeddb}/array.ts +2 -2
- package/src/deprecated/indexeddb/index.ts +12 -0
- package/src/{indexeddb → deprecated/indexeddb}/map.ts +2 -2
- package/src/{indexeddb → deprecated/indexeddb}/multi_map.ts +2 -2
- package/src/{indexeddb → deprecated/indexeddb}/set.ts +2 -2
- package/src/{indexeddb → deprecated/indexeddb}/singleton.ts +2 -2
- package/src/{indexeddb → deprecated/indexeddb}/store.ts +10 -9
- package/src/interfaces/array_test_suite.ts +33 -35
- package/src/interfaces/index.ts +1 -1
- package/src/interfaces/map_test_suite.ts +32 -34
- package/src/interfaces/multi_map_test_suite.ts +65 -67
- package/src/interfaces/set_test_suite.ts +13 -15
- package/src/interfaces/singleton_test_suite.ts +6 -8
- package/src/lmdb/index.ts +3 -3
- package/src/lmdb/store.ts +12 -8
- package/src/lmdb-v2/factory.ts +86 -10
- package/src/lmdb-v2/read_transaction.ts +23 -23
- package/src/lmdb-v2/store.ts +4 -2
- package/src/sqlite-opfs/array.ts +124 -0
- package/src/sqlite-opfs/errors.ts +94 -0
- package/src/sqlite-opfs/index.ts +27 -0
- package/src/sqlite-opfs/internal/ordered-binary-browser.js +465 -0
- package/src/sqlite-opfs/manage.ts +46 -0
- package/src/sqlite-opfs/map.ts +163 -0
- package/src/sqlite-opfs/messages.ts +39 -0
- package/src/sqlite-opfs/multi_map.ts +74 -0
- package/src/sqlite-opfs/pool_integrity.ts +327 -0
- package/src/sqlite-opfs/pool_lock.ts +62 -0
- package/src/sqlite-opfs/set.ts +29 -0
- package/src/sqlite-opfs/singleton.ts +48 -0
- package/src/sqlite-opfs/store.ts +330 -0
- package/src/sqlite-opfs/worker.ts +259 -0
- package/src/stores/l2_tips_store.ts +20 -41
- package/dest/config.d.ts +0 -17
- package/dest/config.d.ts.map +0 -1
- package/dest/config.js +0 -26
- package/dest/indexeddb/array.d.ts +0 -22
- package/dest/indexeddb/array.d.ts.map +0 -1
- package/dest/indexeddb/index.d.ts +0 -7
- package/dest/indexeddb/index.d.ts.map +0 -1
- package/dest/indexeddb/index.js +0 -16
- package/dest/indexeddb/map.d.ts +0 -38
- package/dest/indexeddb/map.d.ts.map +0 -1
- package/dest/indexeddb/multi_map.d.ts +0 -14
- package/dest/indexeddb/multi_map.d.ts.map +0 -1
- package/dest/indexeddb/set.d.ts +0 -17
- package/dest/indexeddb/set.d.ts.map +0 -1
- package/dest/indexeddb/singleton.d.ts +0 -17
- package/dest/indexeddb/singleton.d.ts.map +0 -1
- package/dest/indexeddb/store.d.ts +0 -96
- package/dest/indexeddb/store.d.ts.map +0 -1
- package/src/config.ts +0 -36
- package/src/indexeddb/index.ts +0 -31
- /package/dest/{indexeddb → deprecated/indexeddb}/array.js +0 -0
- /package/dest/{indexeddb → deprecated/indexeddb}/map.js +0 -0
- /package/dest/{indexeddb → deprecated/indexeddb}/multi_map.js +0 -0
- /package/dest/{indexeddb → deprecated/indexeddb}/set.js +0 -0
- /package/dest/{indexeddb → deprecated/indexeddb}/singleton.js +0 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { Encoder } from '#msgpackr';
|
|
2
|
+
import { fromBufferKey, toBufferKey } from '#ordered-binary';
|
|
3
|
+
import { Buffer } from 'buffer';
|
|
4
|
+
import { hash } from 'ohash';
|
|
5
|
+
/** A map backed by SQLite in OPFS. Mirrors `IndexedDBAztecMap`. */ export class SQLiteOPFSAztecMap {
|
|
6
|
+
store;
|
|
7
|
+
name;
|
|
8
|
+
container;
|
|
9
|
+
encoder;
|
|
10
|
+
constructor(store, mapName){
|
|
11
|
+
this.store = store;
|
|
12
|
+
this.encoder = new Encoder();
|
|
13
|
+
this.name = mapName;
|
|
14
|
+
this.container = `map:${mapName}`;
|
|
15
|
+
}
|
|
16
|
+
async getAsync(key) {
|
|
17
|
+
const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [
|
|
18
|
+
this.slot(key)
|
|
19
|
+
]);
|
|
20
|
+
if (rows.length === 0) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const raw = rows[0][0];
|
|
24
|
+
return raw == null ? undefined : this.decodeValue(raw);
|
|
25
|
+
}
|
|
26
|
+
async hasAsync(key) {
|
|
27
|
+
const rows = await this.store.allAsync('SELECT 1 FROM data WHERE slot = ? LIMIT 1', [
|
|
28
|
+
this.slot(key)
|
|
29
|
+
]);
|
|
30
|
+
return rows.length > 0;
|
|
31
|
+
}
|
|
32
|
+
async sizeAsync() {
|
|
33
|
+
const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ?', [
|
|
34
|
+
this.container
|
|
35
|
+
]);
|
|
36
|
+
return Number(rows[0]?.[0] ?? 0);
|
|
37
|
+
}
|
|
38
|
+
async set(key, val) {
|
|
39
|
+
await this.store.runAsync(`INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
|
|
40
|
+
VALUES (?, ?, ?, ?, ?, ?)`, [
|
|
41
|
+
this.slot(key),
|
|
42
|
+
this.container,
|
|
43
|
+
this.encodedKey(key),
|
|
44
|
+
1,
|
|
45
|
+
hash(val),
|
|
46
|
+
this.encoder.pack(val)
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
async setMany(entries) {
|
|
50
|
+
if (entries.length === 0) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await this.store.transactionAsync(async ()=>{
|
|
54
|
+
for (const { key, value } of entries){
|
|
55
|
+
await this.set(key, value);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
swap(_key, _fn) {
|
|
60
|
+
throw new Error('Not implemented');
|
|
61
|
+
}
|
|
62
|
+
async setIfNotExists(key, val) {
|
|
63
|
+
return await this.store.transactionAsync(async ()=>{
|
|
64
|
+
if (await this.hasAsync(key)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
await this.set(key, val);
|
|
68
|
+
return true;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async delete(key) {
|
|
72
|
+
await this.store.runAsync('DELETE FROM data WHERE slot = ?', [
|
|
73
|
+
this.slot(key)
|
|
74
|
+
]);
|
|
75
|
+
}
|
|
76
|
+
async *entriesAsync(range = {}) {
|
|
77
|
+
const rows = await this.rangeQuery(range);
|
|
78
|
+
for (const row of rows){
|
|
79
|
+
const [_slot, keyBlob, value] = row;
|
|
80
|
+
if (keyBlob == null || value == null) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
yield [
|
|
84
|
+
this.decodeKey(keyBlob),
|
|
85
|
+
this.decodeValue(value)
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async *valuesAsync(range = {}) {
|
|
90
|
+
for await (const [, value] of this.entriesAsync(range)){
|
|
91
|
+
yield value;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async *keysAsync(range = {}) {
|
|
95
|
+
for await (const [key] of this.entriesAsync(range)){
|
|
96
|
+
yield key;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async rangeQuery(range) {
|
|
100
|
+
// Inclusivity flips with direction to match the IndexedDB backend:
|
|
101
|
+
// forward: [start, end) reverse: (start, end]
|
|
102
|
+
// That asymmetry is load-bearing — tests pin the exact inclusivity at boundaries.
|
|
103
|
+
const reverse = !!range.reverse;
|
|
104
|
+
const parts = [
|
|
105
|
+
'container = ?'
|
|
106
|
+
];
|
|
107
|
+
const bind = [
|
|
108
|
+
this.container
|
|
109
|
+
];
|
|
110
|
+
if (range.start !== undefined) {
|
|
111
|
+
parts.push(reverse ? 'key > ?' : 'key >= ?');
|
|
112
|
+
bind.push(this.encodedKey(range.start));
|
|
113
|
+
}
|
|
114
|
+
if (range.end !== undefined) {
|
|
115
|
+
parts.push(reverse ? 'key <= ?' : 'key < ?');
|
|
116
|
+
bind.push(this.encodedKey(range.end));
|
|
117
|
+
}
|
|
118
|
+
const order = reverse ? 'DESC' : 'ASC';
|
|
119
|
+
let sql = `SELECT slot, key, value FROM data WHERE ${parts.join(' AND ')} ORDER BY key ${order}, key_count ${order}`;
|
|
120
|
+
if (range.limit !== undefined) {
|
|
121
|
+
sql += ' LIMIT ?';
|
|
122
|
+
bind.push(range.limit);
|
|
123
|
+
}
|
|
124
|
+
const rows = await this.store.allAsync(sql, bind);
|
|
125
|
+
return rows.map((r)=>[
|
|
126
|
+
String(r[0]),
|
|
127
|
+
r[1],
|
|
128
|
+
r[2]
|
|
129
|
+
]);
|
|
130
|
+
}
|
|
131
|
+
decodeValue(val) {
|
|
132
|
+
if (!(val instanceof Uint8Array)) {
|
|
133
|
+
return val;
|
|
134
|
+
}
|
|
135
|
+
const unpacked = this.encoder.unpack(val);
|
|
136
|
+
// msgpackr returns plain Uint8Array in browsers for packed Buffers. Callers that
|
|
137
|
+
// stored Buffers (walletDB uses Buffer.from(...).toString('utf8') round-trips)
|
|
138
|
+
// rely on Buffer-flavored behavior — re-wrap at the storage boundary, mirroring
|
|
139
|
+
// IndexedDBAztecMap.restoreBuffers.
|
|
140
|
+
if (unpacked instanceof Uint8Array && !Buffer.isBuffer(unpacked)) {
|
|
141
|
+
return Buffer.from(unpacked);
|
|
142
|
+
}
|
|
143
|
+
return unpacked;
|
|
144
|
+
}
|
|
145
|
+
decodeKey(raw) {
|
|
146
|
+
const parsed = fromBufferKey(Buffer.from(raw));
|
|
147
|
+
if (Array.isArray(parsed)) {
|
|
148
|
+
return parsed.length > 1 ? parsed : parsed[0];
|
|
149
|
+
}
|
|
150
|
+
return parsed;
|
|
151
|
+
}
|
|
152
|
+
encodedKey(key) {
|
|
153
|
+
return toBufferKey(this.normalizeKey(key));
|
|
154
|
+
}
|
|
155
|
+
normalizeKey(key) {
|
|
156
|
+
return Array.isArray(key) ? key : [
|
|
157
|
+
key
|
|
158
|
+
];
|
|
159
|
+
}
|
|
160
|
+
slot(key, index = 0) {
|
|
161
|
+
return `map:${this.name}:slot:${this.normalizeKey(key)}:${index}`;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RPC protocol between the main thread and the SQLite worker.
|
|
3
|
+
* All requests carry a unique `id`; responses echo the same id so the main thread
|
|
4
|
+
* can resolve the right pending promise.
|
|
5
|
+
*/
|
|
6
|
+
import type { SqliteEncryptionErrorCode } from './errors.js';
|
|
7
|
+
/** Matches `@sqlite.org/sqlite-wasm`'s internal SqlValue type. Boolean is not a native SQLite type. */
|
|
8
|
+
export type SqlValue = string | number | bigint | null | Uint8Array;
|
|
9
|
+
/** A row returned in 'array' rowMode — one value per column, in select order. */
|
|
10
|
+
export type ResultRow = SqlValue[];
|
|
11
|
+
export type WorkerRequest = {
|
|
12
|
+
type: 'init';
|
|
13
|
+
id: number;
|
|
14
|
+
dbName: string;
|
|
15
|
+
ephemeral: boolean;
|
|
16
|
+
poolDirectory?: string;
|
|
17
|
+
encryptionKey?: Uint8Array;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'close';
|
|
20
|
+
id: number;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'deleteDb';
|
|
23
|
+
id: number;
|
|
24
|
+
dbName: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'run';
|
|
27
|
+
id: number;
|
|
28
|
+
sql: string;
|
|
29
|
+
bind?: SqlValue[];
|
|
30
|
+
} | {
|
|
31
|
+
type: 'all';
|
|
32
|
+
id: number;
|
|
33
|
+
sql: string;
|
|
34
|
+
bind?: SqlValue[];
|
|
35
|
+
} | {
|
|
36
|
+
type: 'export';
|
|
37
|
+
id: number;
|
|
38
|
+
} | {
|
|
39
|
+
type: 'begin';
|
|
40
|
+
id: number;
|
|
41
|
+
} | {
|
|
42
|
+
type: 'commit';
|
|
43
|
+
id: number;
|
|
44
|
+
} | {
|
|
45
|
+
type: 'rollback';
|
|
46
|
+
id: number;
|
|
47
|
+
};
|
|
48
|
+
export type WorkerResponse = {
|
|
49
|
+
type: 'ok';
|
|
50
|
+
id: number;
|
|
51
|
+
rows?: ResultRow[];
|
|
52
|
+
changes?: number;
|
|
53
|
+
bytes?: Uint8Array;
|
|
54
|
+
} | {
|
|
55
|
+
type: 'err';
|
|
56
|
+
id: number;
|
|
57
|
+
message: string;
|
|
58
|
+
/**
|
|
59
|
+
* Set when the worker detected an encryption-shaped failure. The main thread
|
|
60
|
+
* uses this to re-throw the error as a {@link SqliteEncryptionError} with the
|
|
61
|
+
* matching code. Absent on non-encryption failures (untyped paths preserved).
|
|
62
|
+
*/
|
|
63
|
+
encryptionCode?: SqliteEncryptionErrorCode;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AM,CAAA;CAAE,GAC7B;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAAC;AAErC,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,SAAS,EAAE,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,UAAU,CAAA;CAAE,GACpF;IACE,IAAI,EAAE,KAAK,CAAC;IACZ,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,cAAc,CAAC,EAAE,yBAAyB,CAAC;CAC5C,CAAC;AAEN,MAAM,MAAM,iBAAiB,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Key, Value } from '../interfaces/common.js';
|
|
2
|
+
import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
|
|
3
|
+
import { SQLiteOPFSAztecMap } from './map.js';
|
|
4
|
+
/**
|
|
5
|
+
* Multi-map backed by SQLite. Extends the base map with always-incrementing
|
|
6
|
+
* `key_count` per-key so the same slot is never reused across deletions — this
|
|
7
|
+
* matches the IndexedDB backend's sparse-multi-map semantics.
|
|
8
|
+
*/
|
|
9
|
+
export declare class SQLiteOPFSAztecMultiMap<K extends Key, V extends Value> extends SQLiteOPFSAztecMap<K, V> implements AztecAsyncMultiMap<K, V> {
|
|
10
|
+
set(key: K, val: V): Promise<void>;
|
|
11
|
+
getValuesAsync(key: K): AsyncIterableIterator<V>;
|
|
12
|
+
getValueCountAsync(key: K): Promise<number>;
|
|
13
|
+
deleteValue(key: K, val: V): Promise<void>;
|
|
14
|
+
delete(key: K): Promise<void>;
|
|
15
|
+
}
|
|
16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0FBQyxDQUszQztDQUNGIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi_map.d.ts","sourceRoot":"","sources":["../../src/sqlite-opfs/multi_map.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE9C;;;;GAIG;AACH,qBAAa,uBAAuB,CAAC,CAAC,SAAS,GAAG,EAAE,CAAC,SAAS,KAAK,CACjE,SAAQ,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAC/B,YAAW,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC;IAEpB,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAqBhD;IAEM,cAAc,CAAC,GAAG,EAAE,CAAC,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAWtD;IAEK,kBAAkB,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAMhD;IAEK,WAAW,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAM/C;IAEc,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAK3C;CACF"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { hash } from 'ohash';
|
|
2
|
+
import { SQLiteOPFSAztecMap } from './map.js';
|
|
3
|
+
/**
|
|
4
|
+
* Multi-map backed by SQLite. Extends the base map with always-incrementing
|
|
5
|
+
* `key_count` per-key so the same slot is never reused across deletions — this
|
|
6
|
+
* matches the IndexedDB backend's sparse-multi-map semantics.
|
|
7
|
+
*/ export class SQLiteOPFSAztecMultiMap extends SQLiteOPFSAztecMap {
|
|
8
|
+
async set(key, val) {
|
|
9
|
+
const valueHash = hash(val);
|
|
10
|
+
await this.store.transactionAsync(async ()=>{
|
|
11
|
+
const exists = await this.store.allAsync('SELECT 1 FROM data WHERE container = ? AND key = ? AND hash = ? LIMIT 1', [
|
|
12
|
+
this.container,
|
|
13
|
+
this.encodedKey(key),
|
|
14
|
+
valueHash
|
|
15
|
+
]);
|
|
16
|
+
if (exists.length > 0) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const maxRow = await this.store.allAsync('SELECT MAX(key_count) FROM data WHERE container = ? AND key = ?', [
|
|
20
|
+
this.container,
|
|
21
|
+
this.encodedKey(key)
|
|
22
|
+
]);
|
|
23
|
+
const count = Number(maxRow[0]?.[0] ?? 0);
|
|
24
|
+
await this.store.runAsync(`INSERT INTO data (slot, container, key, key_count, hash, value)
|
|
25
|
+
VALUES (?, ?, ?, ?, ?, ?)`, [
|
|
26
|
+
this.slot(key, count),
|
|
27
|
+
this.container,
|
|
28
|
+
this.encodedKey(key),
|
|
29
|
+
count + 1,
|
|
30
|
+
valueHash,
|
|
31
|
+
this.encoder.pack(val)
|
|
32
|
+
]);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async *getValuesAsync(key) {
|
|
36
|
+
const rows = await this.store.allAsync('SELECT value FROM data WHERE container = ? AND key = ? ORDER BY key_count ASC', [
|
|
37
|
+
this.container,
|
|
38
|
+
this.encodedKey(key)
|
|
39
|
+
]);
|
|
40
|
+
for (const row of rows){
|
|
41
|
+
const raw = row[0];
|
|
42
|
+
if (raw instanceof Uint8Array) {
|
|
43
|
+
yield this.decodeValue(raw);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async getValueCountAsync(key) {
|
|
48
|
+
const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ? AND key = ?', [
|
|
49
|
+
this.container,
|
|
50
|
+
this.encodedKey(key)
|
|
51
|
+
]);
|
|
52
|
+
return Number(rows[0]?.[0] ?? 0);
|
|
53
|
+
}
|
|
54
|
+
async deleteValue(key, val) {
|
|
55
|
+
await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ? AND hash = ?', [
|
|
56
|
+
this.container,
|
|
57
|
+
this.encodedKey(key),
|
|
58
|
+
hash(val)
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
async delete(key) {
|
|
62
|
+
await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ?', [
|
|
63
|
+
this.container,
|
|
64
|
+
this.encodedKey(key)
|
|
65
|
+
]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const OPFS_QUARANTINE_ROOT_DIRECTORY = ".aztec-sqlite-quarantine";
|
|
2
|
+
export interface DuplicatePoolAssociation {
|
|
3
|
+
logicalPath: string;
|
|
4
|
+
opaqueFileNames: string[];
|
|
5
|
+
}
|
|
6
|
+
export interface PoolQuarantineMetadata {
|
|
7
|
+
formatVersion: 1;
|
|
8
|
+
originalPoolDirectory: string;
|
|
9
|
+
quarantinedAt: string;
|
|
10
|
+
duplicateAssociations: DuplicatePoolAssociation[];
|
|
11
|
+
}
|
|
12
|
+
export interface PoolQuarantineResult extends PoolQuarantineMetadata {
|
|
13
|
+
quarantineDirectory: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Detects duplicate SAH logical-file associations and, if found, copies the complete pool into quarantine before
|
|
17
|
+
* removing the original. The caller must hold the pool's exclusive Web Lock for the whole operation.
|
|
18
|
+
*/
|
|
19
|
+
export declare function quarantineDuplicatePool(poolDirectory: string): Promise<PoolQuarantineResult | undefined>;
|
|
20
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9vbF9pbnRlZ3JpdHkuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zcWxpdGUtb3Bmcy9wb29sX2ludGVnc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AAM,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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9vbF9sb2NrLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3FsaXRlLW9wZnMvcG9vbF9sb2NrLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVBLGVBQU8sTUFBTSwwQkFBMEIsY0FBYyxDQUFDO0FBSXRELE1BQU0sV0FBVyxhQUFhO0lBQzVCLE9BQU8sSUFB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