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

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 (91) hide show
  1. package/README.md +8 -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/indexeddb/index.js +1 -1
  6. package/dest/interfaces/array_test_suite.d.ts +1 -1
  7. package/dest/interfaces/array_test_suite.d.ts.map +1 -1
  8. package/dest/interfaces/array_test_suite.js +33 -34
  9. package/dest/interfaces/index.d.ts +2 -2
  10. package/dest/interfaces/index.d.ts.map +1 -1
  11. package/dest/interfaces/map_test_suite.d.ts +1 -1
  12. package/dest/interfaces/map_test_suite.d.ts.map +1 -1
  13. package/dest/interfaces/map_test_suite.js +32 -33
  14. package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
  15. package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
  16. package/dest/interfaces/multi_map_test_suite.js +68 -69
  17. package/dest/interfaces/set_test_suite.d.ts +1 -1
  18. package/dest/interfaces/set_test_suite.d.ts.map +1 -1
  19. package/dest/interfaces/set_test_suite.js +13 -14
  20. package/dest/interfaces/singleton_test_suite.d.ts +1 -1
  21. package/dest/interfaces/singleton_test_suite.d.ts.map +1 -1
  22. package/dest/interfaces/singleton_test_suite.js +6 -7
  23. package/dest/lmdb/index.js +2 -2
  24. package/dest/lmdb-v2/factory.d.ts +28 -3
  25. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  26. package/dest/lmdb-v2/factory.js +61 -10
  27. package/dest/lmdb-v2/read_transaction.js +21 -19
  28. package/dest/lmdb-v2/store.d.ts +2 -2
  29. package/dest/lmdb-v2/store.d.ts.map +1 -1
  30. package/dest/lmdb-v2/store.js +4 -4
  31. package/dest/sqlite-opfs/array.d.ts +21 -0
  32. package/dest/sqlite-opfs/array.d.ts.map +1 -0
  33. package/dest/sqlite-opfs/array.js +128 -0
  34. package/dest/sqlite-opfs/errors.d.ts +27 -0
  35. package/dest/sqlite-opfs/errors.d.ts.map +1 -0
  36. package/dest/sqlite-opfs/errors.js +34 -0
  37. package/dest/sqlite-opfs/index.d.ts +16 -0
  38. package/dest/sqlite-opfs/index.d.ts.map +1 -0
  39. package/dest/sqlite-opfs/index.js +22 -0
  40. package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts +32 -0
  41. package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts.map +1 -0
  42. package/dest/sqlite-opfs/internal/ordered-binary-browser.js +448 -0
  43. package/dest/sqlite-opfs/map.d.ts +35 -0
  44. package/dest/sqlite-opfs/map.d.ts.map +1 -0
  45. package/dest/sqlite-opfs/map.js +163 -0
  46. package/dest/sqlite-opfs/messages.d.ts +66 -0
  47. package/dest/sqlite-opfs/messages.d.ts.map +1 -0
  48. package/dest/sqlite-opfs/messages.js +5 -0
  49. package/dest/sqlite-opfs/multi_map.d.ts +16 -0
  50. package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
  51. package/dest/sqlite-opfs/multi_map.js +67 -0
  52. package/dest/sqlite-opfs/set.d.ts +13 -0
  53. package/dest/sqlite-opfs/set.d.ts.map +1 -0
  54. package/dest/sqlite-opfs/set.js +19 -0
  55. package/dest/sqlite-opfs/singleton.d.ts +13 -0
  56. package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
  57. package/dest/sqlite-opfs/singleton.js +48 -0
  58. package/dest/sqlite-opfs/store.d.ts +79 -0
  59. package/dest/sqlite-opfs/store.d.ts.map +1 -0
  60. package/dest/sqlite-opfs/store.js +281 -0
  61. package/dest/sqlite-opfs/worker.d.ts +2 -0
  62. package/dest/sqlite-opfs/worker.d.ts.map +1 -0
  63. package/dest/sqlite-opfs/worker.js +249 -0
  64. package/dest/stores/l2_tips_store.d.ts +3 -3
  65. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  66. package/dest/stores/l2_tips_store.js +2 -2
  67. package/package.json +22 -19
  68. package/src/bench/shared_map_bench.ts +111 -0
  69. package/src/indexeddb/index.ts +1 -1
  70. package/src/interfaces/array_test_suite.ts +33 -35
  71. package/src/interfaces/index.ts +1 -1
  72. package/src/interfaces/map_test_suite.ts +32 -34
  73. package/src/interfaces/multi_map_test_suite.ts +65 -67
  74. package/src/interfaces/set_test_suite.ts +13 -15
  75. package/src/interfaces/singleton_test_suite.ts +6 -8
  76. package/src/lmdb/index.ts +2 -2
  77. package/src/lmdb-v2/factory.ts +79 -9
  78. package/src/lmdb-v2/read_transaction.ts +23 -23
  79. package/src/lmdb-v2/store.ts +4 -2
  80. package/src/sqlite-opfs/array.ts +124 -0
  81. package/src/sqlite-opfs/errors.ts +44 -0
  82. package/src/sqlite-opfs/index.ts +39 -0
  83. package/src/sqlite-opfs/internal/ordered-binary-browser.js +465 -0
  84. package/src/sqlite-opfs/map.ts +163 -0
  85. package/src/sqlite-opfs/messages.ts +39 -0
  86. package/src/sqlite-opfs/multi_map.ts +74 -0
  87. package/src/sqlite-opfs/set.ts +29 -0
  88. package/src/sqlite-opfs/singleton.ts +48 -0
  89. package/src/sqlite-opfs/store.ts +296 -0
  90. package/src/sqlite-opfs/worker.ts +231 -0
  91. package/src/stores/l2_tips_store.ts +3 -2
@@ -0,0 +1,448 @@
1
+ /* eslint-disable */ /**
2
+ * Vendored from `ordered-binary@1.5.3/index.js` with one targeted edit:
3
+ * the `readString` definition (originally a dynamically-generated
4
+ * unrolled-loop function) is replaced with a hand-written interpreted
5
+ * equivalent that does the same work without code-generation-from-strings.
6
+ *
7
+ * Why vendor: upstream has no CSP-safe build, and the codegen is a
8
+ * module-init-time string-passed-to-evaluator that trips MV3 CSP
9
+ * (`script-src 'self' 'wasm-unsafe-eval'`). The codegen is a perf
10
+ * optimization on the string-decoding path only — the interpreted
11
+ * version is functionally identical, just slower per call.
12
+ *
13
+ * Parity with upstream is enforced by `ordered-binary-browser.test.ts`,
14
+ * which compares byte-exact output for a representative key set on every
15
+ * Node-mode test run. If `ordered-binary` is bumped, re-vendor by
16
+ * recopying upstream and reapplying the patch in the section marked
17
+ * `// CSP PATCH BEGIN` ... `// CSP PATCH END`.
18
+ */ /*
19
+ control character types:
20
+ 1 - metadata
21
+ 2 - symbols
22
+ 6 - false
23
+ 7 - true
24
+ 8- 16 - negative doubles
25
+ 16-24 positive doubles
26
+ 27 - String starts with a character 27 or less or is an empty string
27
+ 0 - multipart separator
28
+ > 27 normal string characters
29
+ */ /*
30
+ * Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
31
+ */ const float64Array = new Float64Array(2);
32
+ const int32Array = new Int32Array(float64Array.buffer, 0, 4);
33
+ let nullTerminate = false;
34
+ let textEncoder;
35
+ try {
36
+ textEncoder = new TextEncoder();
37
+ } catch (error) {}
38
+ /*
39
+ * Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
40
+ */ export function writeKey(key, target, position, inSequence) {
41
+ let targetView = target.dataView;
42
+ if (!targetView) {
43
+ targetView = target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength + 3 >> 2 << 2);
44
+ }
45
+ switch(typeof key){
46
+ case 'string':
47
+ let strLength = key.length;
48
+ let c1 = key.charCodeAt(0);
49
+ if (!(c1 >= 28)) {
50
+ // escape character
51
+ target[position++] = 27;
52
+ }
53
+ if (strLength < 0x40) {
54
+ let i, c2;
55
+ for(i = 0; i < strLength; i++){
56
+ c1 = key.charCodeAt(i);
57
+ if (c1 <= 4) {
58
+ target[position++] = 4;
59
+ target[position++] = c1;
60
+ } else if (c1 < 0x80) {
61
+ target[position++] = c1;
62
+ } else if (c1 < 0x800) {
63
+ target[position++] = c1 >> 6 | 0xc0;
64
+ target[position++] = c1 & 0x3f | 0x80;
65
+ } else if ((c1 & 0xfc00) === 0xd800 && ((c2 = key.charCodeAt(i + 1)) & 0xfc00) === 0xdc00) {
66
+ c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
67
+ i++;
68
+ target[position++] = c1 >> 18 | 0xf0;
69
+ target[position++] = c1 >> 12 & 0x3f | 0x80;
70
+ target[position++] = c1 >> 6 & 0x3f | 0x80;
71
+ target[position++] = c1 & 0x3f | 0x80;
72
+ } else {
73
+ target[position++] = c1 >> 12 | 0xe0;
74
+ target[position++] = c1 >> 6 & 0x3f | 0x80;
75
+ target[position++] = c1 & 0x3f | 0x80;
76
+ }
77
+ }
78
+ } else {
79
+ if (target.utf8Write) {
80
+ position += target.utf8Write(key, position, target.byteLength - position);
81
+ } else {
82
+ position += textEncoder.encodeInto(key, target.subarray(position)).written;
83
+ }
84
+ if (position > target.length - 4) {
85
+ throw new RangeError('String does not fit in target buffer');
86
+ }
87
+ }
88
+ break;
89
+ case 'number':
90
+ float64Array[0] = key;
91
+ let lowInt = int32Array[0];
92
+ let highInt = int32Array[1];
93
+ let length;
94
+ if (key < 0) {
95
+ targetView.setInt32(position + 4, ~(lowInt >>> 4 | highInt << 28));
96
+ targetView.setInt32(position + 0, (highInt ^ 0x7fffffff) >>> 4);
97
+ targetView.setInt32(position + 8, (lowInt & 0xf ^ 0xf) << 4, true); // just always do the null termination here
98
+ return position + 9;
99
+ } else if (lowInt & 0xf || inSequence) {
100
+ length = 9;
101
+ } else if (lowInt & 0xfffff) {
102
+ length = 8;
103
+ } else if (lowInt || highInt & 0xf) {
104
+ length = 6;
105
+ } else {
106
+ length = 4;
107
+ }
108
+ // switching order to go to little endian
109
+ targetView.setInt32(position + 0, highInt >>> 4 | 0x10000000);
110
+ targetView.setInt32(position + 4, lowInt >>> 4 | highInt << 28);
111
+ // if (length == 9 || nullTerminate)
112
+ targetView.setInt32(position + 8, (lowInt & 0xf) << 4, true);
113
+ return position + length;
114
+ case 'object':
115
+ if (key) {
116
+ if (Array.isArray(key)) {
117
+ for(let i = 0, l = key.length; i < l; i++){
118
+ if (i > 0) {
119
+ target[position++] = 0;
120
+ }
121
+ position = writeKey(key[i], target, position, true);
122
+ }
123
+ break;
124
+ } else if (key instanceof Uint8Array) {
125
+ target.set(key, position);
126
+ position += key.length;
127
+ break;
128
+ } else {
129
+ throw new Error('Unable to serialize object as a key: ' + JSON.stringify(key));
130
+ }
131
+ } else {
132
+ target[position++] = 0;
133
+ }
134
+ break;
135
+ case 'boolean':
136
+ targetView.setUint32(position++, key ? 7 : 6, true);
137
+ return position;
138
+ case 'bigint':
139
+ let asFloat = Number(key);
140
+ if (BigInt(asFloat) > key) {
141
+ float64Array[0] = asFloat;
142
+ if (asFloat > 0) {
143
+ if (int32Array[0]) {
144
+ int32Array[0]--;
145
+ } else {
146
+ int32Array[1]--;
147
+ int32Array[0] = 0xffffffff;
148
+ }
149
+ } else {
150
+ if (int32Array[0] < 0xffffffff) {
151
+ int32Array[0]++;
152
+ } else {
153
+ int32Array[1]++;
154
+ int32Array[0] = 0;
155
+ }
156
+ }
157
+ asFloat = float64Array[0];
158
+ }
159
+ let difference = key - BigInt(asFloat);
160
+ if (difference === 0n) {
161
+ return writeKey(asFloat, target, position, inSequence);
162
+ }
163
+ writeKey(asFloat, target, position, inSequence);
164
+ position += 9; // always increment by 9 if we are adding fractional bits
165
+ let exponent = BigInt((int32Array[1] >> 20 & 0x7ff) - 1079);
166
+ let nextByte = difference >> exponent;
167
+ target[position - 1] |= Number(nextByte);
168
+ difference -= nextByte << exponent;
169
+ let first = true;
170
+ while(difference || first){
171
+ first = false;
172
+ exponent -= 7n;
173
+ let nextByte = difference >> exponent;
174
+ target[position++] = Number(nextByte) | 0x80;
175
+ difference -= nextByte << exponent;
176
+ }
177
+ return position;
178
+ case 'undefined':
179
+ return position;
180
+ // undefined is interpreted as the absence of a key, signified by zero length
181
+ case 'symbol':
182
+ target[position++] = 2;
183
+ return writeKey(key.description, target, position, inSequence);
184
+ default:
185
+ throw new Error('Can not serialize key of type ' + typeof key);
186
+ }
187
+ if (nullTerminate && !inSequence) {
188
+ targetView.setUint32(position, 0);
189
+ }
190
+ return position;
191
+ }
192
+ let position;
193
+ export function readKey(buffer, start, end, inSequence) {
194
+ position = start;
195
+ let controlByte = buffer[position];
196
+ let value;
197
+ if (controlByte < 24) {
198
+ if (controlByte < 8) {
199
+ position++;
200
+ if (controlByte == 6) {
201
+ value = false;
202
+ } else if (controlByte == 7) {
203
+ value = true;
204
+ } else if (controlByte == 0) {
205
+ value = null;
206
+ } else if (controlByte == 2) {
207
+ value = Symbol.for(readStringSafely(buffer, end));
208
+ } else {
209
+ return Uint8Array.prototype.slice.call(buffer, start, end);
210
+ }
211
+ } else {
212
+ let dataView;
213
+ try {
214
+ dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength + 3 >> 2 << 2));
215
+ } catch (error) {
216
+ // if it is write at the end of the ArrayBuffer, we may need to retry with the exact remaining bytes
217
+ dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.buffer.byteLength - buffer.byteOffset));
218
+ }
219
+ let highInt = dataView.getInt32(position) << 4;
220
+ let size = end - position;
221
+ let lowInt;
222
+ if (size > 4) {
223
+ lowInt = dataView.getInt32(position + 4);
224
+ highInt |= lowInt >>> 28;
225
+ if (size <= 6) {
226
+ // clear the last bits
227
+ lowInt &= -0x10000;
228
+ }
229
+ lowInt = lowInt << 4;
230
+ if (size > 8) {
231
+ lowInt = lowInt | buffer[position + 8] >> 4;
232
+ }
233
+ } else {
234
+ lowInt = 0;
235
+ }
236
+ if (controlByte < 16) {
237
+ // negative gets negated
238
+ highInt = highInt ^ 0x7fffffff;
239
+ lowInt = ~lowInt;
240
+ }
241
+ int32Array[1] = highInt;
242
+ int32Array[0] = lowInt;
243
+ value = float64Array[0];
244
+ position += 9;
245
+ if (size > 9 && buffer[position] > 0) {
246
+ // convert the float to bigint, and then we will add precision as we enumerate through the
247
+ // extra bytes
248
+ value = BigInt(value);
249
+ let exponent = highInt >> 20 & 0x7ff;
250
+ let next_byte = buffer[position - 1] & 0xf;
251
+ value += BigInt(next_byte) << BigInt(exponent - 1079);
252
+ while((next_byte = buffer[position]) > 0 && position++ < end){
253
+ value += BigInt(next_byte & 0x7f) << BigInt((start - position) * 7 + exponent - 1016);
254
+ }
255
+ }
256
+ }
257
+ } else {
258
+ if (controlByte == 27) {
259
+ position++;
260
+ }
261
+ value = readStringSafely(buffer, end);
262
+ }
263
+ while(position < end){
264
+ if (buffer[position] === 0) {
265
+ position++;
266
+ }
267
+ if (inSequence) {
268
+ encoder.position = position;
269
+ return value;
270
+ }
271
+ let nextValue = readKey(buffer, position, end, true);
272
+ if (value instanceof Array) {
273
+ value.push(nextValue);
274
+ } else {
275
+ value = [
276
+ value,
277
+ nextValue
278
+ ];
279
+ }
280
+ }
281
+ return value;
282
+ }
283
+ export const enableNullTermination = ()=>nullTerminate = true;
284
+ export const encoder = {
285
+ writeKey,
286
+ readKey,
287
+ enableNullTermination
288
+ };
289
+ let targetBuffer = [];
290
+ let targetPosition = 0;
291
+ const hasNodeBuffer = typeof Buffer !== 'undefined';
292
+ const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
293
+ export const toBufferKey = (key)=>{
294
+ let newBuffer;
295
+ if (targetPosition + 100 > targetBuffer.length) {
296
+ targetBuffer = new ByteArrayAllocate(8192);
297
+ targetPosition = 0;
298
+ newBuffer = true;
299
+ }
300
+ try {
301
+ let result = targetBuffer.slice(targetPosition, targetPosition = writeKey(key, targetBuffer, targetPosition));
302
+ if (targetPosition > targetBuffer.length) {
303
+ if (newBuffer) {
304
+ throw new Error('Key is too large');
305
+ }
306
+ return toBufferKey(key);
307
+ }
308
+ return result;
309
+ } catch (error) {
310
+ if (newBuffer) {
311
+ throw error;
312
+ }
313
+ targetPosition = targetBuffer.length;
314
+ return toBufferKey(key);
315
+ }
316
+ };
317
+ export const fromBufferKey = (sourceBuffer)=>{
318
+ return readKey(sourceBuffer, 0, sourceBuffer.length);
319
+ };
320
+ const fromCharCode = String.fromCharCode;
321
+ let pendingSurrogate;
322
+ function finishUtf8(byte1, src) {
323
+ if ((byte1 & 0xe0) === 0xc0) {
324
+ // 2 bytes
325
+ const byte2 = src[position++] & 0x3f;
326
+ return (byte1 & 0x1f) << 6 | byte2;
327
+ } else if ((byte1 & 0xf0) === 0xe0) {
328
+ // 3 bytes
329
+ const byte2 = src[position++] & 0x3f;
330
+ const byte3 = src[position++] & 0x3f;
331
+ return (byte1 & 0x1f) << 12 | byte2 << 6 | byte3;
332
+ } else if ((byte1 & 0xf8) === 0xf0) {
333
+ // 4 bytes
334
+ if (pendingSurrogate) {
335
+ byte1 = pendingSurrogate;
336
+ pendingSurrogate = null;
337
+ position += 3;
338
+ return byte1;
339
+ }
340
+ const byte2 = src[position++] & 0x3f;
341
+ const byte3 = src[position++] & 0x3f;
342
+ const byte4 = src[position++] & 0x3f;
343
+ let unit = (byte1 & 0x07) << 0x12 | byte2 << 0x0c | byte3 << 0x06 | byte4;
344
+ if (unit > 0xffff) {
345
+ pendingSurrogate = 0xdc00 | unit & 0x3ff;
346
+ unit = unit - 0x10000 >>> 10 & 0x3ff | 0xd800;
347
+ position -= 4; // reset so we can return the next part of the surrogate pair
348
+ }
349
+ return unit;
350
+ } else {
351
+ return byte1;
352
+ }
353
+ }
354
+ // CSP PATCH BEGIN — replaces upstream's module-init dynamic-codegen with
355
+ // an interpreted equivalent. The original `makeStringBuilder()` produces
356
+ // a function string that unrolls 0x30 iterations of the loop below for
357
+ // perf; this version does the same work as a real loop, slower per call
358
+ // but CSP-safe (no module-init code generation from strings).
359
+ const readString = function makeReadString() {
360
+ function readStr(source) {
361
+ const codes = [];
362
+ while(codes.length < 0x30){
363
+ let v = source[position++];
364
+ if (v > 4) {
365
+ if (v >= 0x80) {
366
+ v = finishUtf8(v, source);
367
+ }
368
+ } else if (v === 4) {
369
+ v = source[position++];
370
+ } else {
371
+ return fromCharCode.apply(null, codes);
372
+ }
373
+ codes.push(v);
374
+ }
375
+ return fromCharCode.apply(null, codes) + readStr(source);
376
+ }
377
+ return readStr;
378
+ }();
379
+ // CSP PATCH END
380
+ function readStringSafely(source, end) {
381
+ if (source[end] > 0) {
382
+ let previous = source[end];
383
+ try {
384
+ // read string expects a null terminator, that is a 0 or undefined from reading past the end of the buffer, so we
385
+ // have to ensure that, but do so safely, restoring the buffer to its original state
386
+ source[end] = 0;
387
+ return readString(source);
388
+ } finally{
389
+ source[end] = previous;
390
+ }
391
+ } else {
392
+ return readString(source);
393
+ }
394
+ }
395
+ export function compareKeys(a, b) {
396
+ // compare with type consistency that matches binary comparison
397
+ if (typeof a == 'object') {
398
+ if (!a) {
399
+ return b == null ? 0 : -1;
400
+ }
401
+ if (a.compare) {
402
+ if (b == null) {
403
+ return 1;
404
+ } else if (b.compare) {
405
+ return a.compare(b);
406
+ } else {
407
+ return -1;
408
+ }
409
+ }
410
+ let arrayComparison;
411
+ if (b instanceof Array) {
412
+ let i = 0;
413
+ while((arrayComparison = compareKeys(a[i], b[i])) == 0 && i <= a.length){
414
+ i++;
415
+ }
416
+ return arrayComparison;
417
+ }
418
+ arrayComparison = compareKeys(a[0], b);
419
+ if (arrayComparison == 0 && a.length > 1) {
420
+ return 1;
421
+ }
422
+ return arrayComparison;
423
+ } else if (typeof a == typeof b) {
424
+ if (typeof a === 'symbol') {
425
+ a = Symbol.keyFor(a);
426
+ b = Symbol.keyFor(b);
427
+ }
428
+ return a < b ? -1 : a === b ? 0 : 1;
429
+ } else if (typeof b == 'object') {
430
+ if (b instanceof Array) {
431
+ return -compareKeys(b, a);
432
+ }
433
+ return 1;
434
+ } else {
435
+ return typeOrder[typeof a] < typeOrder[typeof b] ? -1 : 1;
436
+ }
437
+ }
438
+ const typeOrder = {
439
+ symbol: 0,
440
+ undefined: 1,
441
+ boolean: 2,
442
+ number: 3,
443
+ string: 4
444
+ };
445
+ export const MINIMUM_KEY = null;
446
+ export const MAXIMUM_KEY = new Uint8Array([
447
+ 0xff
448
+ ]);
@@ -0,0 +1,35 @@
1
+ import { Encoder } from '#msgpackr';
2
+ import { Buffer } from 'buffer';
3
+ import type { Key, Range, Value } from '../interfaces/common.js';
4
+ import type { AztecAsyncMap } from '../interfaces/map.js';
5
+ import type { SqlValue } from './messages.js';
6
+ import type { AztecSQLiteOPFSStore } from './store.js';
7
+ /** A map backed by SQLite in OPFS. Mirrors `IndexedDBAztecMap`. */
8
+ export declare class SQLiteOPFSAztecMap<K extends Key, V extends Value> implements AztecAsyncMap<K, V> {
9
+ protected readonly store: AztecSQLiteOPFSStore;
10
+ protected readonly name: string;
11
+ protected readonly container: string;
12
+ protected readonly encoder: Encoder;
13
+ constructor(store: AztecSQLiteOPFSStore, mapName: string);
14
+ getAsync(key: K): Promise<V | undefined>;
15
+ hasAsync(key: K): Promise<boolean>;
16
+ sizeAsync(): Promise<number>;
17
+ set(key: K, val: V): Promise<void>;
18
+ setMany(entries: {
19
+ key: K;
20
+ value: V;
21
+ }[]): Promise<void>;
22
+ swap(_key: K, _fn: (val: V | undefined) => V): Promise<void>;
23
+ setIfNotExists(key: K, val: V): Promise<boolean>;
24
+ delete(key: K): Promise<void>;
25
+ entriesAsync(range?: Range<K>): AsyncIterableIterator<[K, V]>;
26
+ valuesAsync(range?: Range<K>): AsyncIterableIterator<V>;
27
+ keysAsync(range?: Range<K>): AsyncIterableIterator<K>;
28
+ protected rangeQuery(range: Range<K>): Promise<Array<[string, Uint8Array, Uint8Array | null]>>;
29
+ protected decodeValue(val: SqlValue): V;
30
+ protected decodeKey(raw: Uint8Array): K;
31
+ protected encodedKey(key: K): Buffer;
32
+ protected normalizeKey(key: K): (string | number | Uint8Array)[];
33
+ protected slot(key: K, index?: number): string;
34
+ }
35
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFwLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3FsaXRlLW9wZnMvbWFwLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxPQUFPLEVBQUUsTUFBTSxXQUFX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AEM,SAAS,CAAC,KAAK,GAAE,KAAK,CAAC,CAAC,CAAM,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAI/D;IAED,UAAgB,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,CAuBnG;IAED,SAAS,CAAC,WAAW,CAAC,GAAG,EAAE,QAAQ,GAAG,CAAC,CAatC;IAED,SAAS,CAAC,SAAS,CAAC,GAAG,EAAE,UAAU,GAAG,CAAC,CAMtC;IAED,SAAS,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,CAEnC;IAED,SAAS,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,UAAU,CAAC,EAAE,CAE/D;IAED,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,GAAE,MAAU,GAAG,MAAM,CAEhD;CACF"}
@@ -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
+ }