@aztec/kv-store 0.0.1-commit.f504929 → 0.0.1-commit.f5a9928
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/{indexeddb → deprecated/indexeddb}/multi_map.js +2 -1
- 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 +5 -3
- 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}/set.js +0 -0
- /package/dest/{indexeddb → deprecated/indexeddb}/singleton.js +0 -0
|
@@ -66,20 +66,20 @@ export class ReadTransaction {
|
|
|
66
66
|
): AsyncIterable<[Uint8Array, T]> {
|
|
67
67
|
this.assertIsOpen();
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
key: startKey,
|
|
71
|
-
reverse,
|
|
72
|
-
count: typeof limit === 'number' ? Math.min(limit, CURSOR_PAGE_SIZE) : CURSOR_PAGE_SIZE,
|
|
73
|
-
onePage: typeof limit === 'number' && limit < CURSOR_PAGE_SIZE,
|
|
74
|
-
db,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
const cursor = response.cursor;
|
|
78
|
-
let entries = response.entries;
|
|
79
|
-
let done = typeof cursor !== 'number';
|
|
80
|
-
let count = 0;
|
|
81
|
-
|
|
69
|
+
let cursor: number | undefined;
|
|
82
70
|
try {
|
|
71
|
+
const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
|
|
72
|
+
key: startKey,
|
|
73
|
+
reverse,
|
|
74
|
+
count: typeof limit === 'number' ? Math.min(limit, CURSOR_PAGE_SIZE) : CURSOR_PAGE_SIZE,
|
|
75
|
+
onePage: typeof limit === 'number' && limit < CURSOR_PAGE_SIZE,
|
|
76
|
+
db,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
cursor = response.cursor ?? undefined;
|
|
80
|
+
let entries = response.entries;
|
|
81
|
+
let done = typeof cursor !== 'number';
|
|
82
|
+
let count = 0;
|
|
83
83
|
// emit the first page and any subsequent pages in a while loop
|
|
84
84
|
// NB: end contition is in the middle of the while loop
|
|
85
85
|
while (entries.length > 0) {
|
|
@@ -125,17 +125,17 @@ export class ReadTransaction {
|
|
|
125
125
|
async #countEntries(db: string, startKey: Uint8Array, endKey: Uint8Array, reverse: boolean): Promise<number> {
|
|
126
126
|
this.assertIsOpen();
|
|
127
127
|
|
|
128
|
-
|
|
129
|
-
key: startKey,
|
|
130
|
-
reverse,
|
|
131
|
-
count: 0,
|
|
132
|
-
onePage: false,
|
|
133
|
-
db,
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const cursor = response.cursor;
|
|
137
|
-
|
|
128
|
+
let cursor: number | undefined;
|
|
138
129
|
try {
|
|
130
|
+
const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
|
|
131
|
+
key: startKey,
|
|
132
|
+
reverse,
|
|
133
|
+
count: 0,
|
|
134
|
+
onePage: false,
|
|
135
|
+
db,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
cursor = response.cursor ?? undefined;
|
|
139
139
|
if (!cursor) {
|
|
140
140
|
return 0;
|
|
141
141
|
}
|
package/src/lmdb-v2/store.ts
CHANGED
|
@@ -43,9 +43,10 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
|
43
43
|
maxReaders: number,
|
|
44
44
|
private log: Logger,
|
|
45
45
|
private cleanup?: () => Promise<void>,
|
|
46
|
+
ephemeral: boolean = false,
|
|
46
47
|
) {
|
|
47
48
|
this.log.info(`Starting data store with maxReaders ${maxReaders}`);
|
|
48
|
-
this.channel = new MsgpackChannel(new NativeLMDBStore(dataDir, mapSize, maxReaders));
|
|
49
|
+
this.channel = new MsgpackChannel(new NativeLMDBStore(dataDir, mapSize, maxReaders, ephemeral));
|
|
49
50
|
// leave one reader to always be available for regular, atomic, reads
|
|
50
51
|
this.availableCursors = new Semaphore(maxReaders - 1);
|
|
51
52
|
}
|
|
@@ -76,9 +77,10 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
|
|
|
76
77
|
maxReaders: number = 16,
|
|
77
78
|
cleanup?: () => Promise<void>,
|
|
78
79
|
bindings?: LoggerBindings,
|
|
80
|
+
ephemeral: boolean = false,
|
|
79
81
|
) {
|
|
80
82
|
const log = createLogger('kv-store:lmdb-v2', bindings);
|
|
81
|
-
const db = new AztecLMDBStoreV2(dataDir, dbMapSizeKb, maxReaders, log, cleanup);
|
|
83
|
+
const db = new AztecLMDBStoreV2(dataDir, dbMapSizeKb, maxReaders, log, cleanup, ephemeral);
|
|
82
84
|
await db.start();
|
|
83
85
|
return db;
|
|
84
86
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Encoder } from '#msgpackr';
|
|
2
|
+
import { toBufferKey } from '#ordered-binary';
|
|
3
|
+
import { hash } from 'ohash';
|
|
4
|
+
|
|
5
|
+
import type { AztecAsyncArray } from '../interfaces/array.js';
|
|
6
|
+
import type { Value } from '../interfaces/common.js';
|
|
7
|
+
import type { AztecSQLiteOPFSStore } from './store.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Persistent array backed by SQLite. Entries share a common `key` (the array name)
|
|
11
|
+
* and are ordered by `key_count`, which doubles as the 1-indexed slot number.
|
|
12
|
+
*/
|
|
13
|
+
export class SQLiteOPFSAztecArray<T extends Value> implements AztecAsyncArray<T> {
|
|
14
|
+
readonly #name: string;
|
|
15
|
+
readonly #container: string;
|
|
16
|
+
readonly #encoder = new Encoder();
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
private readonly store: AztecSQLiteOPFSStore,
|
|
20
|
+
name: string,
|
|
21
|
+
) {
|
|
22
|
+
this.#name = name;
|
|
23
|
+
this.#container = `array:${name}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async lengthAsync(): Promise<number> {
|
|
27
|
+
const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ? AND key = ?', [
|
|
28
|
+
this.#container,
|
|
29
|
+
this.#encodedKey(),
|
|
30
|
+
]);
|
|
31
|
+
return Number(rows[0]?.[0] ?? 0);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async push(...vals: T[]): Promise<number> {
|
|
35
|
+
if (vals.length === 0) {
|
|
36
|
+
return this.lengthAsync();
|
|
37
|
+
}
|
|
38
|
+
return await this.store.transactionAsync(async () => {
|
|
39
|
+
let length = await this.lengthAsync();
|
|
40
|
+
for (const val of vals) {
|
|
41
|
+
await this.store.runAsync(
|
|
42
|
+
`INSERT INTO data (slot, container, key, key_count, hash, value)
|
|
43
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
44
|
+
[this.#slot(length), this.#container, this.#encodedKey(), length + 1, hash(val), this.#encoder.pack(val)],
|
|
45
|
+
);
|
|
46
|
+
length += 1;
|
|
47
|
+
}
|
|
48
|
+
return length;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async pop(): Promise<T | undefined> {
|
|
53
|
+
return await this.store.transactionAsync(async () => {
|
|
54
|
+
const length = await this.lengthAsync();
|
|
55
|
+
if (length === 0) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const slot = this.#slot(length - 1);
|
|
59
|
+
const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [slot]);
|
|
60
|
+
await this.store.runAsync('DELETE FROM data WHERE slot = ?', [slot]);
|
|
61
|
+
const raw = rows[0]?.[0];
|
|
62
|
+
return raw instanceof Uint8Array ? (this.#encoder.unpack(raw) as T) : undefined;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async atAsync(index: number): Promise<T | undefined> {
|
|
67
|
+
const length = await this.lengthAsync();
|
|
68
|
+
const resolved = index < 0 ? length + index : index;
|
|
69
|
+
if (resolved < 0 || resolved >= length) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.#slot(resolved)]);
|
|
73
|
+
const raw = rows[0]?.[0];
|
|
74
|
+
return raw instanceof Uint8Array ? (this.#encoder.unpack(raw) as T) : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async setAt(index: number, val: T): Promise<boolean> {
|
|
78
|
+
return await this.store.transactionAsync(async () => {
|
|
79
|
+
const length = await this.lengthAsync();
|
|
80
|
+
const resolved = index < 0 ? length + index : index;
|
|
81
|
+
if (resolved < 0 || resolved >= length) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
await this.store.runAsync(
|
|
85
|
+
`INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
|
|
86
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
87
|
+
[this.#slot(resolved), this.#container, this.#encodedKey(), resolved + 1, hash(val), this.#encoder.pack(val)],
|
|
88
|
+
);
|
|
89
|
+
return true;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async *entriesAsync(): AsyncIterableIterator<[number, T]> {
|
|
94
|
+
const rows = await this.store.allAsync(
|
|
95
|
+
'SELECT key_count, value FROM data WHERE container = ? AND key = ? ORDER BY key_count ASC',
|
|
96
|
+
[this.#container, this.#encodedKey()],
|
|
97
|
+
);
|
|
98
|
+
for (const row of rows) {
|
|
99
|
+
const keyCount = Number(row[0]);
|
|
100
|
+
const raw = row[1];
|
|
101
|
+
if (raw instanceof Uint8Array) {
|
|
102
|
+
yield [keyCount - 1, this.#encoder.unpack(raw) as T];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async *valuesAsync(): AsyncIterableIterator<T> {
|
|
108
|
+
for await (const [, val] of this.entriesAsync()) {
|
|
109
|
+
yield val;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<T> {
|
|
114
|
+
return this.valuesAsync();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
#encodedKey(): Buffer {
|
|
118
|
+
return toBufferKey([this.#name]);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#slot(index: number): string {
|
|
122
|
+
return `array:${this.#name}:slot:${index}`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed error surface for sqlite3mc-backed page-level encryption failures.
|
|
3
|
+
*
|
|
4
|
+
* Three concrete failure modes are surfaced:
|
|
5
|
+
*
|
|
6
|
+
* - `invalid_key_length`: caller-side pre-flight (key not 32 bytes).
|
|
7
|
+
* - `encryption_not_supported_for_ephemeral`: caller-side pre-flight (encryption was requested on an ephemeral
|
|
8
|
+
* `:memory:` store, which sqlite3mc does not support).
|
|
9
|
+
* - `decrypt_failed`: runtime failure raised when sqlite3mc cannot decode page 1 of an existing database. Covers
|
|
10
|
+
* both "wrong key supplied" and "no key supplied to an encrypted DB".
|
|
11
|
+
*/
|
|
12
|
+
export type SqliteEncryptionErrorCode =
|
|
13
|
+
| 'invalid_key_length'
|
|
14
|
+
| 'encryption_not_supported_for_ephemeral'
|
|
15
|
+
| 'decrypt_failed';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Error thrown by sqlite-opfs when an encryption operation fails.
|
|
19
|
+
**/
|
|
20
|
+
export class SqliteEncryptionError extends Error {
|
|
21
|
+
readonly code: SqliteEncryptionErrorCode;
|
|
22
|
+
|
|
23
|
+
constructor(code: SqliteEncryptionErrorCode, message: string, opts?: { cause?: unknown }) {
|
|
24
|
+
super(message, opts?.cause !== undefined ? { cause: opts.cause } : undefined);
|
|
25
|
+
this.name = 'SqliteEncryptionError';
|
|
26
|
+
this.code = code;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Strings raised by sqlite3mc when page 1 cannot be decoded.
|
|
32
|
+
**/
|
|
33
|
+
const SQLITE3MC_DECRYPT_ERROR_PATTERNS: readonly RegExp[] = [
|
|
34
|
+
/file is not a database/i,
|
|
35
|
+
/file is encrypted or is not a database/i,
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns `true` if `message` matches one of the known sqlite3mc decrypt-failure
|
|
40
|
+
* strings.
|
|
41
|
+
**/
|
|
42
|
+
export function isDecryptFailureMessage(message: string): boolean {
|
|
43
|
+
return SQLITE3MC_DECRYPT_ERROR_PATTERNS.some(p => p.test(message));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Error thrown by sqlite-opfs when the on-disk database image is corrupt
|
|
48
|
+
* (`SQLITE_CORRUPT`, result code 11 — "database disk image is malformed").
|
|
49
|
+
*
|
|
50
|
+
* Distinct from {@link SqliteEncryptionError}: no key recovers a corrupt image,
|
|
51
|
+
* so there is nothing to retry. The only escape is to delete the store and start
|
|
52
|
+
* fresh, so consumers pattern-match on `instanceof SqliteCorruptionError` to wipe
|
|
53
|
+
* and reopen rather than surfacing a dead-end error.
|
|
54
|
+
**/
|
|
55
|
+
export class SqliteCorruptionError extends Error {
|
|
56
|
+
constructor(message: string, opts?: { cause?: unknown }) {
|
|
57
|
+
super(message, opts?.cause !== undefined ? { cause: opts.cause } : undefined);
|
|
58
|
+
this.name = 'SqliteCorruptionError';
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Error thrown when another browser context already owns a store's OPFS pool. */
|
|
63
|
+
export class SqlitePoolBusyError extends Error {
|
|
64
|
+
constructor(public readonly poolDirectory: string) {
|
|
65
|
+
super(`SQLite-OPFS pool "${poolDirectory}" is already in use by another store instance`);
|
|
66
|
+
this.name = 'SqlitePoolBusyError';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Error thrown when the browser context does not expose the Web Locks API required by the OPFS SAH pool. */
|
|
71
|
+
export class SqliteWebLocksUnavailableError extends Error {
|
|
72
|
+
constructor() {
|
|
73
|
+
super('SQLite-OPFS requires the Web Locks API, but it is unavailable in this browser context');
|
|
74
|
+
this.name = 'SqliteWebLocksUnavailableError';
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Strings/codes raised by SQLite when a database image is corrupt. Kept disjoint
|
|
80
|
+
* from {@link SQLITE3MC_DECRYPT_ERROR_PATTERNS}: "file is not a database"
|
|
81
|
+
* (SQLITE_NOTADB) is the decrypt signal, whereas a malformed image is the
|
|
82
|
+
* genuinely-unrecoverable SQLITE_CORRUPT.
|
|
83
|
+
**/
|
|
84
|
+
const SQLITE_CORRUPTION_ERROR_PATTERNS: readonly RegExp[] = [
|
|
85
|
+
/database disk image is malformed/i,
|
|
86
|
+
/\bSQLITE_CORRUPT\b/i,
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns `true` if `message` matches one of the known SQLite corruption strings.
|
|
91
|
+
**/
|
|
92
|
+
export function isCorruptionMessage(message: string): boolean {
|
|
93
|
+
return SQLITE_CORRUPTION_ERROR_PATTERNS.some(p => p.test(message));
|
|
94
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
+
|
|
3
|
+
import { AztecSQLiteOPFSStore } from './store.js';
|
|
4
|
+
|
|
5
|
+
export { AztecSQLiteOPFSStore } from './store.js';
|
|
6
|
+
export {
|
|
7
|
+
SqliteCorruptionError,
|
|
8
|
+
SqliteEncryptionError,
|
|
9
|
+
SqlitePoolBusyError,
|
|
10
|
+
SqliteWebLocksUnavailableError,
|
|
11
|
+
} from './errors.js';
|
|
12
|
+
export type { SqliteEncryptionErrorCode } from './errors.js';
|
|
13
|
+
export { OPFS_POOL_DIR_PREFIX, deletePoolDirectory, deleteStore, listStores, storePoolDirectory } from './manage.js';
|
|
14
|
+
|
|
15
|
+
export function openTmpStore(ephemeral: boolean = false): Promise<AztecSQLiteOPFSStore> {
|
|
16
|
+
return AztecSQLiteOPFSStore.open(createLogger('kv-store:sqlite-opfs'), undefined, ephemeral);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Convenience helper for tests and consumers that want an encrypted sqlite-opfs
|
|
21
|
+
* store without dealing with the full `open()` parameter order. Key must be 32
|
|
22
|
+
* bytes. Creates a fresh persistent store (sqlite3mc does not support encryption
|
|
23
|
+
* on ephemeral `:memory:` databases) in an auto-generated OPFS directory.
|
|
24
|
+
*/
|
|
25
|
+
export function openEncryptedStore(encryptionKey: Uint8Array, name?: string, poolDirectory?: string) {
|
|
26
|
+
return AztecSQLiteOPFSStore.open(createLogger('kv-store:sqlite-opfs'), name, false, poolDirectory, encryptionKey);
|
|
27
|
+
}
|