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

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 (137) 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 +7 -0
  8. package/dest/deprecated/indexeddb/index.d.ts.map +1 -0
  9. package/dest/deprecated/indexeddb/index.js +8 -0
  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 +14 -0
  53. package/dest/sqlite-opfs/index.d.ts.map +1 -0
  54. package/dest/sqlite-opfs/index.js +16 -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/manage.d.ts +20 -0
  59. package/dest/sqlite-opfs/manage.d.ts.map +1 -0
  60. package/dest/sqlite-opfs/manage.js +31 -0
  61. package/dest/sqlite-opfs/map.d.ts +35 -0
  62. package/dest/sqlite-opfs/map.d.ts.map +1 -0
  63. package/dest/sqlite-opfs/map.js +163 -0
  64. package/dest/sqlite-opfs/messages.d.ts +66 -0
  65. package/dest/sqlite-opfs/messages.d.ts.map +1 -0
  66. package/dest/sqlite-opfs/messages.js +5 -0
  67. package/dest/sqlite-opfs/multi_map.d.ts +16 -0
  68. package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
  69. package/dest/sqlite-opfs/multi_map.js +67 -0
  70. package/dest/sqlite-opfs/set.d.ts +13 -0
  71. package/dest/sqlite-opfs/set.d.ts.map +1 -0
  72. package/dest/sqlite-opfs/set.js +19 -0
  73. package/dest/sqlite-opfs/singleton.d.ts +13 -0
  74. package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
  75. package/dest/sqlite-opfs/singleton.js +48 -0
  76. package/dest/sqlite-opfs/store.d.ts +79 -0
  77. package/dest/sqlite-opfs/store.d.ts.map +1 -0
  78. package/dest/sqlite-opfs/store.js +281 -0
  79. package/dest/sqlite-opfs/worker.d.ts +2 -0
  80. package/dest/sqlite-opfs/worker.d.ts.map +1 -0
  81. package/dest/sqlite-opfs/worker.js +264 -0
  82. package/dest/stores/l2_tips_store.d.ts +7 -13
  83. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  84. package/dest/stores/l2_tips_store.js +24 -43
  85. package/package.json +23 -20
  86. package/src/bench/shared_map_bench.ts +111 -0
  87. package/src/{indexeddb → deprecated/indexeddb}/array.ts +2 -2
  88. package/src/deprecated/indexeddb/index.ts +12 -0
  89. package/src/{indexeddb → deprecated/indexeddb}/map.ts +2 -2
  90. package/src/{indexeddb → deprecated/indexeddb}/multi_map.ts +2 -2
  91. package/src/{indexeddb → deprecated/indexeddb}/set.ts +2 -2
  92. package/src/{indexeddb → deprecated/indexeddb}/singleton.ts +2 -2
  93. package/src/{indexeddb → deprecated/indexeddb}/store.ts +10 -9
  94. package/src/interfaces/array_test_suite.ts +33 -35
  95. package/src/interfaces/index.ts +1 -1
  96. package/src/interfaces/map_test_suite.ts +32 -34
  97. package/src/interfaces/multi_map_test_suite.ts +65 -67
  98. package/src/interfaces/set_test_suite.ts +13 -15
  99. package/src/interfaces/singleton_test_suite.ts +6 -8
  100. package/src/lmdb/index.ts +2 -2
  101. package/src/lmdb-v2/factory.ts +85 -9
  102. package/src/lmdb-v2/read_transaction.ts +23 -23
  103. package/src/lmdb-v2/store.ts +4 -2
  104. package/src/sqlite-opfs/array.ts +124 -0
  105. package/src/sqlite-opfs/errors.ts +44 -0
  106. package/src/sqlite-opfs/index.ts +22 -0
  107. package/src/sqlite-opfs/internal/ordered-binary-browser.js +465 -0
  108. package/src/sqlite-opfs/manage.ts +36 -0
  109. package/src/sqlite-opfs/map.ts +163 -0
  110. package/src/sqlite-opfs/messages.ts +39 -0
  111. package/src/sqlite-opfs/multi_map.ts +74 -0
  112. package/src/sqlite-opfs/set.ts +29 -0
  113. package/src/sqlite-opfs/singleton.ts +48 -0
  114. package/src/sqlite-opfs/store.ts +296 -0
  115. package/src/sqlite-opfs/worker.ts +248 -0
  116. package/src/stores/l2_tips_store.ts +20 -41
  117. package/dest/indexeddb/array.d.ts +0 -22
  118. package/dest/indexeddb/array.d.ts.map +0 -1
  119. package/dest/indexeddb/index.d.ts +0 -7
  120. package/dest/indexeddb/index.d.ts.map +0 -1
  121. package/dest/indexeddb/index.js +0 -16
  122. package/dest/indexeddb/map.d.ts +0 -38
  123. package/dest/indexeddb/map.d.ts.map +0 -1
  124. package/dest/indexeddb/multi_map.d.ts +0 -14
  125. package/dest/indexeddb/multi_map.d.ts.map +0 -1
  126. package/dest/indexeddb/set.d.ts +0 -17
  127. package/dest/indexeddb/set.d.ts.map +0 -1
  128. package/dest/indexeddb/singleton.d.ts +0 -17
  129. package/dest/indexeddb/singleton.d.ts.map +0 -1
  130. package/dest/indexeddb/store.d.ts +0 -96
  131. package/dest/indexeddb/store.d.ts.map +0 -1
  132. package/src/indexeddb/index.ts +0 -31
  133. /package/dest/{indexeddb → deprecated/indexeddb}/array.js +0 -0
  134. /package/dest/{indexeddb → deprecated/indexeddb}/map.js +0 -0
  135. /package/dest/{indexeddb → deprecated/indexeddb}/multi_map.js +0 -0
  136. /package/dest/{indexeddb → deprecated/indexeddb}/set.js +0 -0
  137. /package/dest/{indexeddb → deprecated/indexeddb}/singleton.js +0 -0
@@ -0,0 +1,465 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Vendored from `ordered-binary@1.5.3/index.js` with one targeted edit:
4
+ * the `readString` definition (originally a dynamically-generated
5
+ * unrolled-loop function) is replaced with a hand-written interpreted
6
+ * equivalent that does the same work without code-generation-from-strings.
7
+ *
8
+ * Why vendor: upstream has no CSP-safe build, and the codegen is a
9
+ * module-init-time string-passed-to-evaluator that trips MV3 CSP
10
+ * (`script-src 'self' 'wasm-unsafe-eval'`). The codegen is a perf
11
+ * optimization on the string-decoding path only — the interpreted
12
+ * version is functionally identical, just slower per call.
13
+ *
14
+ * Parity with upstream is enforced by `ordered-binary-browser.test.ts`,
15
+ * which compares byte-exact output for a representative key set on every
16
+ * Node-mode test run. If `ordered-binary` is bumped, re-vendor by
17
+ * recopying upstream and reapplying the patch in the section marked
18
+ * `// CSP PATCH BEGIN` ... `// CSP PATCH END`.
19
+ */
20
+ /*
21
+ control character types:
22
+ 1 - metadata
23
+ 2 - symbols
24
+ 6 - false
25
+ 7 - true
26
+ 8- 16 - negative doubles
27
+ 16-24 positive doubles
28
+ 27 - String starts with a character 27 or less or is an empty string
29
+ 0 - multipart separator
30
+ > 27 normal string characters
31
+ */
32
+ /*
33
+ * Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
34
+ */
35
+
36
+ const float64Array = new Float64Array(2);
37
+ const int32Array = new Int32Array(float64Array.buffer, 0, 4);
38
+ let nullTerminate = false;
39
+ let textEncoder;
40
+ try {
41
+ textEncoder = new TextEncoder();
42
+ } catch (error) {}
43
+
44
+ /*
45
+ * Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
46
+ */
47
+ export function writeKey(key, target, position, inSequence) {
48
+ let targetView = target.dataView;
49
+ if (!targetView) {
50
+ targetView = target.dataView = new DataView(target.buffer, target.byteOffset, ((target.byteLength + 3) >> 2) << 2);
51
+ }
52
+ switch (typeof key) {
53
+ case 'string':
54
+ let strLength = key.length;
55
+ let c1 = key.charCodeAt(0);
56
+ if (!(c1 >= 28)) {
57
+ // escape character
58
+ target[position++] = 27;
59
+ }
60
+ if (strLength < 0x40) {
61
+ let i, c2;
62
+ for (i = 0; i < strLength; i++) {
63
+ c1 = key.charCodeAt(i);
64
+ if (c1 <= 4) {
65
+ target[position++] = 4;
66
+ target[position++] = c1;
67
+ } else if (c1 < 0x80) {
68
+ target[position++] = c1;
69
+ } else if (c1 < 0x800) {
70
+ target[position++] = (c1 >> 6) | 0xc0;
71
+ target[position++] = (c1 & 0x3f) | 0x80;
72
+ } else if ((c1 & 0xfc00) === 0xd800 && ((c2 = key.charCodeAt(i + 1)) & 0xfc00) === 0xdc00) {
73
+ c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
74
+ i++;
75
+ target[position++] = (c1 >> 18) | 0xf0;
76
+ target[position++] = ((c1 >> 12) & 0x3f) | 0x80;
77
+ target[position++] = ((c1 >> 6) & 0x3f) | 0x80;
78
+ target[position++] = (c1 & 0x3f) | 0x80;
79
+ } else {
80
+ target[position++] = (c1 >> 12) | 0xe0;
81
+ target[position++] = ((c1 >> 6) & 0x3f) | 0x80;
82
+ target[position++] = (c1 & 0x3f) | 0x80;
83
+ }
84
+ }
85
+ } else {
86
+ if (target.utf8Write) {
87
+ position += target.utf8Write(key, position, target.byteLength - position);
88
+ } else {
89
+ position += textEncoder.encodeInto(key, target.subarray(position)).written;
90
+ }
91
+ if (position > target.length - 4) {
92
+ throw new RangeError('String does not fit in target buffer');
93
+ }
94
+ }
95
+ break;
96
+ case 'number':
97
+ float64Array[0] = key;
98
+ let lowInt = int32Array[0];
99
+ let highInt = int32Array[1];
100
+ let length;
101
+ if (key < 0) {
102
+ targetView.setInt32(position + 4, ~((lowInt >>> 4) | (highInt << 28)));
103
+ targetView.setInt32(position + 0, (highInt ^ 0x7fffffff) >>> 4);
104
+ targetView.setInt32(position + 8, ((lowInt & 0xf) ^ 0xf) << 4, true); // just always do the null termination here
105
+ return position + 9;
106
+ } else if (lowInt & 0xf || inSequence) {
107
+ length = 9;
108
+ } else if (lowInt & 0xfffff) {
109
+ length = 8;
110
+ } else if (lowInt || highInt & 0xf) {
111
+ length = 6;
112
+ } else {
113
+ length = 4;
114
+ }
115
+ // switching order to go to little endian
116
+ targetView.setInt32(position + 0, (highInt >>> 4) | 0x10000000);
117
+ targetView.setInt32(position + 4, (lowInt >>> 4) | (highInt << 28));
118
+ // if (length == 9 || nullTerminate)
119
+ targetView.setInt32(position + 8, (lowInt & 0xf) << 4, true);
120
+ return position + length;
121
+ case 'object':
122
+ if (key) {
123
+ if (Array.isArray(key)) {
124
+ for (let i = 0, l = key.length; i < l; i++) {
125
+ if (i > 0) {
126
+ target[position++] = 0;
127
+ }
128
+ position = writeKey(key[i], target, position, true);
129
+ }
130
+ break;
131
+ } else if (key instanceof Uint8Array) {
132
+ target.set(key, position);
133
+ position += key.length;
134
+ break;
135
+ } else {
136
+ throw new Error('Unable to serialize object as a key: ' + JSON.stringify(key));
137
+ }
138
+ } // null
139
+ else {
140
+ target[position++] = 0;
141
+ }
142
+ break;
143
+ case 'boolean':
144
+ targetView.setUint32(position++, key ? 7 : 6, true);
145
+ return position;
146
+ case 'bigint':
147
+ let asFloat = Number(key);
148
+ if (BigInt(asFloat) > key) {
149
+ float64Array[0] = asFloat;
150
+ if (asFloat > 0) {
151
+ if (int32Array[0]) {
152
+ int32Array[0]--;
153
+ } else {
154
+ int32Array[1]--;
155
+ int32Array[0] = 0xffffffff;
156
+ }
157
+ } else {
158
+ if (int32Array[0] < 0xffffffff) {
159
+ int32Array[0]++;
160
+ } else {
161
+ int32Array[1]++;
162
+ int32Array[0] = 0;
163
+ }
164
+ }
165
+ asFloat = float64Array[0];
166
+ }
167
+ let difference = key - BigInt(asFloat);
168
+ if (difference === 0n) {
169
+ return writeKey(asFloat, target, position, inSequence);
170
+ }
171
+ writeKey(asFloat, target, position, inSequence);
172
+ position += 9; // always increment by 9 if we are adding fractional bits
173
+ let exponent = BigInt(((int32Array[1] >> 20) & 0x7ff) - 1079);
174
+ let nextByte = difference >> exponent;
175
+ target[position - 1] |= Number(nextByte);
176
+ difference -= nextByte << exponent;
177
+ let first = true;
178
+ while (difference || first) {
179
+ first = false;
180
+ exponent -= 7n;
181
+ let nextByte = difference >> exponent;
182
+ target[position++] = Number(nextByte) | 0x80;
183
+ difference -= nextByte << exponent;
184
+ }
185
+ return position;
186
+ case 'undefined':
187
+ return position;
188
+ // undefined is interpreted as the absence of a key, signified by zero length
189
+ case 'symbol':
190
+ target[position++] = 2;
191
+ return writeKey(key.description, target, position, inSequence);
192
+ default:
193
+ throw new Error('Can not serialize key of type ' + typeof key);
194
+ }
195
+ if (nullTerminate && !inSequence) {
196
+ targetView.setUint32(position, 0);
197
+ }
198
+ return position;
199
+ }
200
+
201
+ let position;
202
+ export function readKey(buffer, start, end, inSequence) {
203
+ position = start;
204
+ let controlByte = buffer[position];
205
+ let value;
206
+ if (controlByte < 24) {
207
+ if (controlByte < 8) {
208
+ position++;
209
+ if (controlByte == 6) {
210
+ value = false;
211
+ } else if (controlByte == 7) {
212
+ value = true;
213
+ } else if (controlByte == 0) {
214
+ value = null;
215
+ } else if (controlByte == 2) {
216
+ value = Symbol.for(readStringSafely(buffer, end));
217
+ } else {
218
+ return Uint8Array.prototype.slice.call(buffer, start, end);
219
+ }
220
+ } else {
221
+ let dataView;
222
+ try {
223
+ dataView =
224
+ buffer.dataView ||
225
+ (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, ((buffer.byteLength + 3) >> 2) << 2));
226
+ } catch (error) {
227
+ // if it is write at the end of the ArrayBuffer, we may need to retry with the exact remaining bytes
228
+ dataView =
229
+ buffer.dataView ||
230
+ (buffer.dataView = new DataView(
231
+ buffer.buffer,
232
+ buffer.byteOffset,
233
+ buffer.buffer.byteLength - buffer.byteOffset,
234
+ ));
235
+ }
236
+
237
+ let highInt = dataView.getInt32(position) << 4;
238
+ let size = end - position;
239
+ let lowInt;
240
+ if (size > 4) {
241
+ lowInt = dataView.getInt32(position + 4);
242
+ highInt |= lowInt >>> 28;
243
+ if (size <= 6) {
244
+ // clear the last bits
245
+ lowInt &= -0x10000;
246
+ }
247
+ lowInt = lowInt << 4;
248
+ if (size > 8) {
249
+ lowInt = lowInt | (buffer[position + 8] >> 4);
250
+ }
251
+ } else {
252
+ lowInt = 0;
253
+ }
254
+ if (controlByte < 16) {
255
+ // negative gets negated
256
+ highInt = highInt ^ 0x7fffffff;
257
+ lowInt = ~lowInt;
258
+ }
259
+ int32Array[1] = highInt;
260
+ int32Array[0] = lowInt;
261
+ value = float64Array[0];
262
+ position += 9;
263
+ if (size > 9 && buffer[position] > 0) {
264
+ // convert the float to bigint, and then we will add precision as we enumerate through the
265
+ // extra bytes
266
+ value = BigInt(value);
267
+ let exponent = (highInt >> 20) & 0x7ff;
268
+ let next_byte = buffer[position - 1] & 0xf;
269
+ value += BigInt(next_byte) << BigInt(exponent - 1079);
270
+ while ((next_byte = buffer[position]) > 0 && position++ < end) {
271
+ value += BigInt(next_byte & 0x7f) << BigInt((start - position) * 7 + exponent - 1016);
272
+ }
273
+ }
274
+ }
275
+ } else {
276
+ if (controlByte == 27) {
277
+ position++;
278
+ }
279
+ value = readStringSafely(buffer, end);
280
+ }
281
+ while (position < end) {
282
+ if (buffer[position] === 0) {
283
+ position++;
284
+ }
285
+ if (inSequence) {
286
+ encoder.position = position;
287
+ return value;
288
+ }
289
+ let nextValue = readKey(buffer, position, end, true);
290
+ if (value instanceof Array) {
291
+ value.push(nextValue);
292
+ } else {
293
+ value = [value, nextValue];
294
+ }
295
+ }
296
+ return value;
297
+ }
298
+ export const enableNullTermination = () => (nullTerminate = true);
299
+
300
+ export const encoder = {
301
+ writeKey,
302
+ readKey,
303
+ enableNullTermination,
304
+ };
305
+ let targetBuffer = [];
306
+ let targetPosition = 0;
307
+ const hasNodeBuffer = typeof Buffer !== 'undefined';
308
+ const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
309
+ export const toBufferKey = key => {
310
+ let newBuffer;
311
+ if (targetPosition + 100 > targetBuffer.length) {
312
+ targetBuffer = new ByteArrayAllocate(8192);
313
+ targetPosition = 0;
314
+ newBuffer = true;
315
+ }
316
+ try {
317
+ let result = targetBuffer.slice(targetPosition, (targetPosition = writeKey(key, targetBuffer, targetPosition)));
318
+ if (targetPosition > targetBuffer.length) {
319
+ if (newBuffer) {
320
+ throw new Error('Key is too large');
321
+ }
322
+ return toBufferKey(key);
323
+ }
324
+ return result;
325
+ } catch (error) {
326
+ if (newBuffer) {
327
+ throw error;
328
+ }
329
+ targetPosition = targetBuffer.length;
330
+ return toBufferKey(key);
331
+ }
332
+ };
333
+ export const fromBufferKey = sourceBuffer => {
334
+ return readKey(sourceBuffer, 0, sourceBuffer.length);
335
+ };
336
+ const fromCharCode = String.fromCharCode;
337
+
338
+ let pendingSurrogate;
339
+ function finishUtf8(byte1, src) {
340
+ if ((byte1 & 0xe0) === 0xc0) {
341
+ // 2 bytes
342
+ const byte2 = src[position++] & 0x3f;
343
+ return ((byte1 & 0x1f) << 6) | byte2;
344
+ } else if ((byte1 & 0xf0) === 0xe0) {
345
+ // 3 bytes
346
+ const byte2 = src[position++] & 0x3f;
347
+ const byte3 = src[position++] & 0x3f;
348
+ return ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
349
+ } else if ((byte1 & 0xf8) === 0xf0) {
350
+ // 4 bytes
351
+ if (pendingSurrogate) {
352
+ byte1 = pendingSurrogate;
353
+ pendingSurrogate = null;
354
+ position += 3;
355
+ return byte1;
356
+ }
357
+ const byte2 = src[position++] & 0x3f;
358
+ const byte3 = src[position++] & 0x3f;
359
+ const byte4 = src[position++] & 0x3f;
360
+ let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
361
+ if (unit > 0xffff) {
362
+ pendingSurrogate = 0xdc00 | (unit & 0x3ff);
363
+ unit = (((unit - 0x10000) >>> 10) & 0x3ff) | 0xd800;
364
+ position -= 4; // reset so we can return the next part of the surrogate pair
365
+ }
366
+ return unit;
367
+ } else {
368
+ return byte1;
369
+ }
370
+ }
371
+
372
+ // CSP PATCH BEGIN — replaces upstream's module-init dynamic-codegen with
373
+ // an interpreted equivalent. The original `makeStringBuilder()` produces
374
+ // a function string that unrolls 0x30 iterations of the loop below for
375
+ // perf; this version does the same work as a real loop, slower per call
376
+ // but CSP-safe (no module-init code generation from strings).
377
+ const readString = (function makeReadString() {
378
+ function readStr(source) {
379
+ const codes = [];
380
+ while (codes.length < 0x30) {
381
+ let v = source[position++];
382
+ if (v > 4) {
383
+ if (v >= 0x80) {
384
+ v = finishUtf8(v, source);
385
+ }
386
+ } else if (v === 4) {
387
+ v = source[position++];
388
+ } else {
389
+ return fromCharCode.apply(null, codes);
390
+ }
391
+ codes.push(v);
392
+ }
393
+ return fromCharCode.apply(null, codes) + readStr(source);
394
+ }
395
+ return readStr;
396
+ })();
397
+ // CSP PATCH END
398
+
399
+ function readStringSafely(source, end) {
400
+ if (source[end] > 0) {
401
+ let previous = source[end];
402
+ try {
403
+ // read string expects a null terminator, that is a 0 or undefined from reading past the end of the buffer, so we
404
+ // have to ensure that, but do so safely, restoring the buffer to its original state
405
+ source[end] = 0;
406
+ return readString(source);
407
+ } finally {
408
+ source[end] = previous;
409
+ }
410
+ } else {
411
+ return readString(source);
412
+ }
413
+ }
414
+ export function compareKeys(a, b) {
415
+ // compare with type consistency that matches binary comparison
416
+ if (typeof a == 'object') {
417
+ if (!a) {
418
+ return b == null ? 0 : -1;
419
+ }
420
+ if (a.compare) {
421
+ if (b == null) {
422
+ return 1;
423
+ } else if (b.compare) {
424
+ return a.compare(b);
425
+ } else {
426
+ return -1;
427
+ }
428
+ }
429
+ let arrayComparison;
430
+ if (b instanceof Array) {
431
+ let i = 0;
432
+ while ((arrayComparison = compareKeys(a[i], b[i])) == 0 && i <= a.length) {
433
+ i++;
434
+ }
435
+ return arrayComparison;
436
+ }
437
+ arrayComparison = compareKeys(a[0], b);
438
+ if (arrayComparison == 0 && a.length > 1) {
439
+ return 1;
440
+ }
441
+ return arrayComparison;
442
+ } else if (typeof a == typeof b) {
443
+ if (typeof a === 'symbol') {
444
+ a = Symbol.keyFor(a);
445
+ b = Symbol.keyFor(b);
446
+ }
447
+ return a < b ? -1 : a === b ? 0 : 1;
448
+ } else if (typeof b == 'object') {
449
+ if (b instanceof Array) {
450
+ return -compareKeys(b, a);
451
+ }
452
+ return 1;
453
+ } else {
454
+ return typeOrder[typeof a] < typeOrder[typeof b] ? -1 : 1;
455
+ }
456
+ }
457
+ const typeOrder = {
458
+ symbol: 0,
459
+ undefined: 1,
460
+ boolean: 2,
461
+ number: 3,
462
+ string: 4,
463
+ };
464
+ export const MINIMUM_KEY = null;
465
+ export const MAXIMUM_KEY = new Uint8Array([0xff]);
@@ -0,0 +1,36 @@
1
+ /** Prefix for the per-store OPFS SAH pool directories owned by this package. */
2
+ export const OPFS_POOL_DIR_PREFIX = '.aztec-kv-';
3
+
4
+ /**
5
+ * OPFS directory holding a store's SAH pool. One directory per store: the SAH-pool VFS allows only one
6
+ * concurrent instance per directory and its capacity does not grow automatically, so sharing a pool across
7
+ * stores would make concurrently opened stores contend for locks and orphaned stores exhaust pool slots.
8
+ */
9
+ export function storePoolDirectory(effectiveName: string): string {
10
+ return `${OPFS_POOL_DIR_PREFIX}${effectiveName}`;
11
+ }
12
+
13
+ /**
14
+ * Lists the names of every persistent sqlite-opfs store in this origin, by enumerating the per-store pool
15
+ * directories. Includes stores other than the current one, so wallets can surface and clean up data for
16
+ * networks/versions no longer in use.
17
+ */
18
+ export async function listStores(): Promise<string[]> {
19
+ const root = await navigator.storage.getDirectory();
20
+ const names: string[] = [];
21
+ for await (const [entryName, handle] of root.entries()) {
22
+ if (handle.kind === 'directory' && entryName.startsWith(OPFS_POOL_DIR_PREFIX)) {
23
+ names.push(entryName.slice(OPFS_POOL_DIR_PREFIX.length));
24
+ }
25
+ }
26
+ return names;
27
+ }
28
+
29
+ /**
30
+ * Permanently deletes a store by effective name (as returned by {@link listStores}). The store must be closed:
31
+ * an open store's SAH pool holds locks on the directory and the removal will reject.
32
+ */
33
+ export async function deleteStore(effectiveName: string): Promise<void> {
34
+ const root = await navigator.storage.getDirectory();
35
+ await root.removeEntry(storePoolDirectory(effectiveName), { recursive: true });
36
+ }
@@ -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
+
6
+ import type { Key, Range, Value } from '../interfaces/common.js';
7
+ import type { AztecAsyncMap } from '../interfaces/map.js';
8
+ import type { SqlValue } from './messages.js';
9
+ import type { AztecSQLiteOPFSStore } from './store.js';
10
+
11
+ /** A map backed by SQLite in OPFS. Mirrors `IndexedDBAztecMap`. */
12
+ export class SQLiteOPFSAztecMap<K extends Key, V extends Value> implements AztecAsyncMap<K, V> {
13
+ protected readonly name: string;
14
+ protected readonly container: string;
15
+ protected readonly encoder = new Encoder();
16
+
17
+ constructor(
18
+ protected readonly store: AztecSQLiteOPFSStore,
19
+ mapName: string,
20
+ ) {
21
+ this.name = mapName;
22
+ this.container = `map:${mapName}`;
23
+ }
24
+
25
+ async getAsync(key: K): Promise<V | undefined> {
26
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.slot(key)]);
27
+ if (rows.length === 0) {
28
+ return undefined;
29
+ }
30
+ const raw = rows[0][0];
31
+ return raw == null ? undefined : this.decodeValue(raw);
32
+ }
33
+
34
+ async hasAsync(key: K): Promise<boolean> {
35
+ const rows = await this.store.allAsync('SELECT 1 FROM data WHERE slot = ? LIMIT 1', [this.slot(key)]);
36
+ return rows.length > 0;
37
+ }
38
+
39
+ async sizeAsync(): Promise<number> {
40
+ const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ?', [this.container]);
41
+ return Number(rows[0]?.[0] ?? 0);
42
+ }
43
+
44
+ async set(key: K, val: V): Promise<void> {
45
+ await this.store.runAsync(
46
+ `INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
47
+ VALUES (?, ?, ?, ?, ?, ?)`,
48
+ [this.slot(key), this.container, this.encodedKey(key), 1, hash(val), this.encoder.pack(val)],
49
+ );
50
+ }
51
+
52
+ async setMany(entries: { key: K; value: V }[]): Promise<void> {
53
+ if (entries.length === 0) {
54
+ return;
55
+ }
56
+ await this.store.transactionAsync(async () => {
57
+ for (const { key, value } of entries) {
58
+ await this.set(key, value);
59
+ }
60
+ });
61
+ }
62
+
63
+ swap(_key: K, _fn: (val: V | undefined) => V): Promise<void> {
64
+ throw new Error('Not implemented');
65
+ }
66
+
67
+ async setIfNotExists(key: K, val: V): Promise<boolean> {
68
+ return await this.store.transactionAsync(async () => {
69
+ if (await this.hasAsync(key)) {
70
+ return false;
71
+ }
72
+ await this.set(key, val);
73
+ return true;
74
+ });
75
+ }
76
+
77
+ async delete(key: K): Promise<void> {
78
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [this.slot(key)]);
79
+ }
80
+
81
+ async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<[K, V]> {
82
+ const rows = await this.rangeQuery(range);
83
+ for (const row of rows) {
84
+ const [_slot, keyBlob, value] = row;
85
+ if (keyBlob == null || value == null) {
86
+ continue;
87
+ }
88
+ yield [this.decodeKey(keyBlob), this.decodeValue(value)];
89
+ }
90
+ }
91
+
92
+ async *valuesAsync(range: Range<K> = {}): AsyncIterableIterator<V> {
93
+ for await (const [, value] of this.entriesAsync(range)) {
94
+ yield value;
95
+ }
96
+ }
97
+
98
+ async *keysAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
99
+ for await (const [key] of this.entriesAsync(range)) {
100
+ yield key;
101
+ }
102
+ }
103
+
104
+ protected async rangeQuery(range: Range<K>): Promise<Array<[string, Uint8Array, Uint8Array | null]>> {
105
+ // Inclusivity flips with direction to match the IndexedDB backend:
106
+ // forward: [start, end) reverse: (start, end]
107
+ // That asymmetry is load-bearing — tests pin the exact inclusivity at boundaries.
108
+ const reverse = !!range.reverse;
109
+ const parts: string[] = ['container = ?'];
110
+ const bind: SqlValue[] = [this.container];
111
+ if (range.start !== undefined) {
112
+ parts.push(reverse ? 'key > ?' : 'key >= ?');
113
+ bind.push(this.encodedKey(range.start));
114
+ }
115
+ if (range.end !== undefined) {
116
+ parts.push(reverse ? 'key <= ?' : 'key < ?');
117
+ bind.push(this.encodedKey(range.end));
118
+ }
119
+ const order = reverse ? 'DESC' : 'ASC';
120
+ let sql = `SELECT slot, key, value FROM data WHERE ${parts.join(' AND ')} ORDER BY key ${order}, key_count ${order}`;
121
+ if (range.limit !== undefined) {
122
+ sql += ' LIMIT ?';
123
+ bind.push(range.limit);
124
+ }
125
+ const rows = await this.store.allAsync(sql, bind);
126
+ return rows.map(r => [String(r[0]), r[1] as Uint8Array, r[2] as Uint8Array | null]);
127
+ }
128
+
129
+ protected decodeValue(val: SqlValue): V {
130
+ if (!(val instanceof Uint8Array)) {
131
+ return val as V;
132
+ }
133
+ const unpacked = this.encoder.unpack(val);
134
+ // msgpackr returns plain Uint8Array in browsers for packed Buffers. Callers that
135
+ // stored Buffers (walletDB uses Buffer.from(...).toString('utf8') round-trips)
136
+ // rely on Buffer-flavored behavior — re-wrap at the storage boundary, mirroring
137
+ // IndexedDBAztecMap.restoreBuffers.
138
+ if (unpacked instanceof Uint8Array && !Buffer.isBuffer(unpacked)) {
139
+ return Buffer.from(unpacked) as V;
140
+ }
141
+ return unpacked as V;
142
+ }
143
+
144
+ protected decodeKey(raw: Uint8Array): K {
145
+ const parsed = fromBufferKey(Buffer.from(raw));
146
+ if (Array.isArray(parsed)) {
147
+ return (parsed.length > 1 ? parsed : parsed[0]) as K;
148
+ }
149
+ return parsed as K;
150
+ }
151
+
152
+ protected encodedKey(key: K): Buffer {
153
+ return toBufferKey(this.normalizeKey(key));
154
+ }
155
+
156
+ protected normalizeKey(key: K): (string | number | Uint8Array)[] {
157
+ return Array.isArray(key) ? key : [key];
158
+ }
159
+
160
+ protected slot(key: K, index: number = 0): string {
161
+ return `map:${this.name}:slot:${this.normalizeKey(key)}:${index}`;
162
+ }
163
+ }
@@ -0,0 +1,39 @@
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
+
8
+ /** Matches `@sqlite.org/sqlite-wasm`'s internal SqlValue type. Boolean is not a native SQLite type. */
9
+ export type SqlValue = string | number | bigint | null | Uint8Array;
10
+
11
+ /** A row returned in 'array' rowMode — one value per column, in select order. */
12
+ export type ResultRow = SqlValue[];
13
+
14
+ export type WorkerRequest =
15
+ | { type: 'init'; id: number; dbName: string; ephemeral: boolean; poolDirectory?: string; encryptionKey?: Uint8Array }
16
+ | { type: 'close'; id: number }
17
+ | { type: 'deleteDb'; id: number; dbName: string }
18
+ | { type: 'run'; id: number; sql: string; bind?: SqlValue[] }
19
+ | { type: 'all'; id: number; sql: string; bind?: SqlValue[] }
20
+ | { type: 'export'; id: number }
21
+ | { type: 'begin'; id: number }
22
+ | { type: 'commit'; id: number }
23
+ | { type: 'rollback'; id: number };
24
+
25
+ export type WorkerResponse =
26
+ | { type: 'ok'; id: number; rows?: ResultRow[]; changes?: number; bytes?: Uint8Array }
27
+ | {
28
+ type: 'err';
29
+ id: number;
30
+ message: string;
31
+ /**
32
+ * Set when the worker detected an encryption-shaped failure. The main thread
33
+ * uses this to re-throw the error as a {@link SqliteEncryptionError} with the
34
+ * matching code. Absent on non-encryption failures (untyped paths preserved).
35
+ */
36
+ encryptionCode?: SqliteEncryptionErrorCode;
37
+ };
38
+
39
+ export type WorkerRequestType = WorkerRequest['type'];