@aztec/kv-store 0.0.1-commit.a072138 → 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 (151) 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/{indexeddb → deprecated/indexeddb}/array.js +18 -6
  8. package/dest/deprecated/indexeddb/index.d.ts +7 -0
  9. package/dest/deprecated/indexeddb/index.d.ts.map +1 -0
  10. package/dest/deprecated/indexeddb/index.js +8 -0
  11. package/dest/deprecated/indexeddb/map.d.ts +38 -0
  12. package/dest/deprecated/indexeddb/map.d.ts.map +1 -0
  13. package/dest/{indexeddb → deprecated/indexeddb}/map.js +23 -13
  14. package/dest/deprecated/indexeddb/multi_map.d.ts +14 -0
  15. package/dest/deprecated/indexeddb/multi_map.d.ts.map +1 -0
  16. package/dest/{indexeddb → deprecated/indexeddb}/multi_map.js +16 -1
  17. package/dest/deprecated/indexeddb/set.d.ts +17 -0
  18. package/dest/deprecated/indexeddb/set.d.ts.map +1 -0
  19. package/dest/deprecated/indexeddb/singleton.d.ts +17 -0
  20. package/dest/deprecated/indexeddb/singleton.d.ts.map +1 -0
  21. package/dest/{indexeddb → deprecated/indexeddb}/singleton.js +3 -1
  22. package/dest/deprecated/indexeddb/store.d.ts +98 -0
  23. package/dest/deprecated/indexeddb/store.d.ts.map +1 -0
  24. package/dest/{indexeddb → deprecated/indexeddb}/store.js +8 -4
  25. package/dest/interfaces/array_test_suite.d.ts +1 -1
  26. package/dest/interfaces/array_test_suite.d.ts.map +1 -1
  27. package/dest/interfaces/array_test_suite.js +33 -34
  28. package/dest/interfaces/index.d.ts +2 -2
  29. package/dest/interfaces/index.d.ts.map +1 -1
  30. package/dest/interfaces/map_test_suite.d.ts +1 -1
  31. package/dest/interfaces/map_test_suite.d.ts.map +1 -1
  32. package/dest/interfaces/map_test_suite.js +74 -29
  33. package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
  34. package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
  35. package/dest/interfaces/multi_map_test_suite.js +84 -60
  36. package/dest/interfaces/set_test_suite.d.ts +1 -1
  37. package/dest/interfaces/set_test_suite.d.ts.map +1 -1
  38. package/dest/interfaces/set_test_suite.js +13 -14
  39. package/dest/interfaces/singleton_test_suite.d.ts +1 -1
  40. package/dest/interfaces/singleton_test_suite.d.ts.map +1 -1
  41. package/dest/interfaces/singleton_test_suite.js +6 -7
  42. package/dest/lmdb/index.d.ts +2 -2
  43. package/dest/lmdb/index.d.ts.map +1 -1
  44. package/dest/lmdb/index.js +2 -2
  45. package/dest/lmdb/store.d.ts +3 -3
  46. package/dest/lmdb/store.d.ts.map +1 -1
  47. package/dest/lmdb/store.js +12 -8
  48. package/dest/lmdb-v2/factory.d.ts +30 -4
  49. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  50. package/dest/lmdb-v2/factory.js +62 -10
  51. package/dest/lmdb-v2/read_transaction.js +21 -19
  52. package/dest/lmdb-v2/store.d.ts +2 -2
  53. package/dest/lmdb-v2/store.d.ts.map +1 -1
  54. package/dest/lmdb-v2/store.js +4 -4
  55. package/dest/sqlite-opfs/array.d.ts +21 -0
  56. package/dest/sqlite-opfs/array.d.ts.map +1 -0
  57. package/dest/sqlite-opfs/array.js +128 -0
  58. package/dest/sqlite-opfs/errors.d.ts +27 -0
  59. package/dest/sqlite-opfs/errors.d.ts.map +1 -0
  60. package/dest/sqlite-opfs/errors.js +34 -0
  61. package/dest/sqlite-opfs/index.d.ts +14 -0
  62. package/dest/sqlite-opfs/index.d.ts.map +1 -0
  63. package/dest/sqlite-opfs/index.js +16 -0
  64. package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts +32 -0
  65. package/dest/sqlite-opfs/internal/ordered-binary-browser.d.ts.map +1 -0
  66. package/dest/sqlite-opfs/internal/ordered-binary-browser.js +448 -0
  67. package/dest/sqlite-opfs/manage.d.ts +20 -0
  68. package/dest/sqlite-opfs/manage.d.ts.map +1 -0
  69. package/dest/sqlite-opfs/manage.js +31 -0
  70. package/dest/sqlite-opfs/map.d.ts +35 -0
  71. package/dest/sqlite-opfs/map.d.ts.map +1 -0
  72. package/dest/sqlite-opfs/map.js +163 -0
  73. package/dest/sqlite-opfs/messages.d.ts +66 -0
  74. package/dest/sqlite-opfs/messages.d.ts.map +1 -0
  75. package/dest/sqlite-opfs/messages.js +5 -0
  76. package/dest/sqlite-opfs/multi_map.d.ts +16 -0
  77. package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
  78. package/dest/sqlite-opfs/multi_map.js +67 -0
  79. package/dest/sqlite-opfs/set.d.ts +13 -0
  80. package/dest/sqlite-opfs/set.d.ts.map +1 -0
  81. package/dest/sqlite-opfs/set.js +19 -0
  82. package/dest/sqlite-opfs/singleton.d.ts +13 -0
  83. package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
  84. package/dest/sqlite-opfs/singleton.js +48 -0
  85. package/dest/sqlite-opfs/store.d.ts +79 -0
  86. package/dest/sqlite-opfs/store.d.ts.map +1 -0
  87. package/dest/sqlite-opfs/store.js +281 -0
  88. package/dest/sqlite-opfs/worker.d.ts +2 -0
  89. package/dest/sqlite-opfs/worker.d.ts.map +1 -0
  90. package/dest/sqlite-opfs/worker.js +264 -0
  91. package/dest/stores/l2_tips_store.d.ts +7 -13
  92. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  93. package/dest/stores/l2_tips_store.js +24 -43
  94. package/dest/utils.d.ts +1 -1
  95. package/dest/utils.d.ts.map +1 -1
  96. package/dest/utils.js +7 -1
  97. package/package.json +30 -26
  98. package/src/bench/shared_map_bench.ts +111 -0
  99. package/src/{indexeddb → deprecated/indexeddb}/array.ts +6 -6
  100. package/src/deprecated/indexeddb/index.ts +12 -0
  101. package/src/{indexeddb → deprecated/indexeddb}/map.ts +26 -13
  102. package/src/{indexeddb → deprecated/indexeddb}/multi_map.ts +17 -3
  103. package/src/{indexeddb → deprecated/indexeddb}/set.ts +2 -2
  104. package/src/{indexeddb → deprecated/indexeddb}/singleton.ts +3 -3
  105. package/src/{indexeddb → deprecated/indexeddb}/store.ts +23 -15
  106. package/src/interfaces/array_test_suite.ts +33 -35
  107. package/src/interfaces/index.ts +1 -1
  108. package/src/interfaces/map_test_suite.ts +56 -30
  109. package/src/interfaces/multi_map_test_suite.ts +88 -58
  110. package/src/interfaces/set_test_suite.ts +13 -15
  111. package/src/interfaces/singleton_test_suite.ts +6 -8
  112. package/src/lmdb/index.ts +3 -3
  113. package/src/lmdb/store.ts +12 -8
  114. package/src/lmdb-v2/factory.ts +86 -10
  115. package/src/lmdb-v2/read_transaction.ts +23 -23
  116. package/src/lmdb-v2/store.ts +4 -2
  117. package/src/sqlite-opfs/array.ts +124 -0
  118. package/src/sqlite-opfs/errors.ts +44 -0
  119. package/src/sqlite-opfs/index.ts +22 -0
  120. package/src/sqlite-opfs/internal/ordered-binary-browser.js +465 -0
  121. package/src/sqlite-opfs/manage.ts +36 -0
  122. package/src/sqlite-opfs/map.ts +163 -0
  123. package/src/sqlite-opfs/messages.ts +39 -0
  124. package/src/sqlite-opfs/multi_map.ts +74 -0
  125. package/src/sqlite-opfs/set.ts +29 -0
  126. package/src/sqlite-opfs/singleton.ts +48 -0
  127. package/src/sqlite-opfs/store.ts +296 -0
  128. package/src/sqlite-opfs/worker.ts +248 -0
  129. package/src/stores/l2_tips_store.ts +20 -41
  130. package/src/utils.ts +10 -0
  131. package/dest/config.d.ts +0 -17
  132. package/dest/config.d.ts.map +0 -1
  133. package/dest/config.js +0 -26
  134. package/dest/indexeddb/array.d.ts +0 -22
  135. package/dest/indexeddb/array.d.ts.map +0 -1
  136. package/dest/indexeddb/index.d.ts +0 -7
  137. package/dest/indexeddb/index.d.ts.map +0 -1
  138. package/dest/indexeddb/index.js +0 -16
  139. package/dest/indexeddb/map.d.ts +0 -32
  140. package/dest/indexeddb/map.d.ts.map +0 -1
  141. package/dest/indexeddb/multi_map.d.ts +0 -13
  142. package/dest/indexeddb/multi_map.d.ts.map +0 -1
  143. package/dest/indexeddb/set.d.ts +0 -17
  144. package/dest/indexeddb/set.d.ts.map +0 -1
  145. package/dest/indexeddb/singleton.d.ts +0 -17
  146. package/dest/indexeddb/singleton.d.ts.map +0 -1
  147. package/dest/indexeddb/store.d.ts +0 -96
  148. package/dest/indexeddb/store.d.ts.map +0 -1
  149. package/src/config.ts +0 -36
  150. package/src/indexeddb/index.ts +0 -31
  151. /package/dest/{indexeddb → deprecated/indexeddb}/set.js +0 -0
@@ -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,20 @@
1
+ /** Prefix for the per-store OPFS SAH pool directories owned by this package. */
2
+ export declare const OPFS_POOL_DIR_PREFIX = ".aztec-kv-";
3
+ /**
4
+ * OPFS directory holding a store's SAH pool. One directory per store: the SAH-pool VFS allows only one
5
+ * concurrent instance per directory and its capacity does not grow automatically, so sharing a pool across
6
+ * stores would make concurrently opened stores contend for locks and orphaned stores exhaust pool slots.
7
+ */
8
+ export declare function storePoolDirectory(effectiveName: string): string;
9
+ /**
10
+ * Lists the names of every persistent sqlite-opfs store in this origin, by enumerating the per-store pool
11
+ * directories. Includes stores other than the current one, so wallets can surface and clean up data for
12
+ * networks/versions no longer in use.
13
+ */
14
+ export declare function listStores(): Promise<string[]>;
15
+ /**
16
+ * Permanently deletes a store by effective name (as returned by {@link listStores}). The store must be closed:
17
+ * an open store's SAH pool holds locks on the directory and the removal will reject.
18
+ */
19
+ export declare function deleteStore(effectiveName: string): Promise<void>;
20
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFuYWdlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3FsaXRlLW9wZnMvbWFuYWdlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGdGQUFnRjtBQUNoRixlQUFPLE1BQU0sb0JBQW9CLGVBQWUsQ0FBQztBQUVqRDs7OztHQUlHO0FBQ0gsd0JBQWdCLGtCQUFrQixDQUFDLGFBQWEsRUFBRSxNQUFNLEdBQUcsTUFBTSxDQUVoRTtBQUVEOzs7O0dBSUc7QUFDSCx3QkFBc0IsVUFBVSxJQUFJLE9BQU8sQ0FBQyxNQUFNLEVBQUUsQ0FBQyxDQVNwRDtBQUVEOzs7R0FHRztBQUNILHdCQUFzQixXQUFXLENBQUMsYUFBYSxFQUFFLE1BQU0sR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLENBR3RFIn0=
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manage.d.ts","sourceRoot":"","sources":["../../src/sqlite-opfs/manage.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,eAAO,MAAM,oBAAoB,eAAe,CAAC;AAEjD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED;;;;GAIG;AACH,wBAAsB,UAAU,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CASpD;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGtE"}
@@ -0,0 +1,31 @@
1
+ /** Prefix for the per-store OPFS SAH pool directories owned by this package. */ export const OPFS_POOL_DIR_PREFIX = '.aztec-kv-';
2
+ /**
3
+ * OPFS directory holding a store's SAH pool. One directory per store: the SAH-pool VFS allows only one
4
+ * concurrent instance per directory and its capacity does not grow automatically, so sharing a pool across
5
+ * stores would make concurrently opened stores contend for locks and orphaned stores exhaust pool slots.
6
+ */ export function storePoolDirectory(effectiveName) {
7
+ return `${OPFS_POOL_DIR_PREFIX}${effectiveName}`;
8
+ }
9
+ /**
10
+ * Lists the names of every persistent sqlite-opfs store in this origin, by enumerating the per-store pool
11
+ * directories. Includes stores other than the current one, so wallets can surface and clean up data for
12
+ * networks/versions no longer in use.
13
+ */ export async function listStores() {
14
+ const root = await navigator.storage.getDirectory();
15
+ const names = [];
16
+ for await (const [entryName, handle] of root.entries()){
17
+ if (handle.kind === 'directory' && entryName.startsWith(OPFS_POOL_DIR_PREFIX)) {
18
+ names.push(entryName.slice(OPFS_POOL_DIR_PREFIX.length));
19
+ }
20
+ }
21
+ return names;
22
+ }
23
+ /**
24
+ * Permanently deletes a store by effective name (as returned by {@link listStores}). The store must be closed:
25
+ * an open store's SAH pool holds locks on the directory and the removal will reject.
26
+ */ export async function deleteStore(effectiveName) {
27
+ const root = await navigator.storage.getDirectory();
28
+ await root.removeEntry(storePoolDirectory(effectiveName), {
29
+ recursive: true
30
+ });
31
+ }
@@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFwLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3FsaXRlLW9wZnMvbWFwLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxPQUFPLEVBQUUsTUFBTSxXQUFXLENBQUM7QUFFcEMsT0FBTyxFQUFFLE1BQU0sRUFBRSxNQUFNLFFBQVEsQ0FBQztBQUdoQyxPQUFPLEtBQUssRUFBRSxHQUFHLEVBQUUsS0FBSyxFQUFFLEtBQUssRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBQ2pFLE9BQU8sS0FBSyxFQUFFLGFBQWEsRUFBRSxNQUFNLHNCQUFzQixDQUFDO0FBQzFELE9BQU8sS0FBSyxFQUFFLFFBQVEsRUFBRSxNQUFNLGV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