@fireproof/core 0.10.0-dev → 0.10.2-dev

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. package/dist/fireproof.browser.js +2 -3
  2. package/dist/fireproof.browser.js.map +2 -2
  3. package/dist/fireproof.cjs.js +2 -3
  4. package/dist/fireproof.cjs.js.map +2 -2
  5. package/dist/fireproof.esm.js +2 -3
  6. package/dist/fireproof.esm.js.map +2 -2
  7. package/package.json +5 -1
  8. package/.eslintrc.cjs +0 -37
  9. package/.vscode/launch.json +0 -12
  10. package/dist/crdt-helpers.browser.js +0 -4961
  11. package/dist/crdt-helpers.browser.js.map +0 -7
  12. package/dist/crdt-helpers.cjs.js +0 -4823
  13. package/dist/crdt-helpers.cjs.js.map +0 -7
  14. package/dist/crdt-helpers.esm.js +0 -4788
  15. package/dist/crdt-helpers.esm.js.map +0 -7
  16. package/dist/crdt.browser.js +0 -18214
  17. package/dist/crdt.browser.js.map +0 -7
  18. package/dist/crdt.cjs.js +0 -8692
  19. package/dist/crdt.cjs.js.map +0 -7
  20. package/dist/crdt.esm.js +0 -8682
  21. package/dist/crdt.esm.js.map +0 -7
  22. package/dist/database.browser.js +0 -18867
  23. package/dist/database.browser.js.map +0 -7
  24. package/dist/database.cjs.js +0 -9296
  25. package/dist/database.cjs.js.map +0 -7
  26. package/dist/database.esm.js +0 -9288
  27. package/dist/database.esm.js.map +0 -7
  28. package/dist/loader-helpers.browser.js +0 -6943
  29. package/dist/loader-helpers.browser.js.map +0 -7
  30. package/dist/loader-helpers.cjs.js +0 -4419
  31. package/dist/loader-helpers.cjs.js.map +0 -7
  32. package/dist/loader-helpers.esm.js +0 -4408
  33. package/dist/loader-helpers.esm.js.map +0 -7
  34. package/dist/loader.browser.js +0 -15968
  35. package/dist/loader.browser.js.map +0 -7
  36. package/dist/loader.cjs.js +0 -6667
  37. package/dist/loader.cjs.js.map +0 -7
  38. package/dist/loader.esm.js +0 -6657
  39. package/dist/loader.esm.js.map +0 -7
  40. package/dist/store-browser.browser.js +0 -1414
  41. package/dist/store-browser.browser.js.map +0 -7
  42. package/dist/store-browser.cjs.js +0 -1387
  43. package/dist/store-browser.cjs.js.map +0 -7
  44. package/dist/store-browser.esm.js +0 -1358
  45. package/dist/store-browser.esm.js.map +0 -7
  46. package/dist/store-fs.browser.js +0 -16142
  47. package/dist/store-fs.browser.js.map +0 -7
  48. package/dist/store-fs.cjs.js +0 -1171
  49. package/dist/store-fs.cjs.js.map +0 -7
  50. package/dist/store-fs.esm.js +0 -1143
  51. package/dist/store-fs.esm.js.map +0 -7
  52. package/dist/store.browser.js +0 -1113
  53. package/dist/store.browser.js.map +0 -7
  54. package/dist/store.cjs.js +0 -1126
  55. package/dist/store.cjs.js.map +0 -7
  56. package/dist/store.esm.js +0 -1097
  57. package/dist/store.esm.js.map +0 -7
  58. package/dist/transaction.browser.js +0 -17241
  59. package/dist/transaction.browser.js.map +0 -7
  60. package/dist/transaction.cjs.js +0 -7842
  61. package/dist/transaction.cjs.js.map +0 -7
  62. package/dist/transaction.esm.js +0 -7831
  63. package/dist/transaction.esm.js.map +0 -7
  64. package/dist/types.d.browser.js +0 -4
  65. package/dist/types.d.browser.js.map +0 -7
  66. package/dist/types.d.cjs.js +0 -19
  67. package/dist/types.d.cjs.js.map +0 -7
  68. package/dist/types.d.esm.js +0 -1
  69. package/dist/types.d.esm.js.map +0 -7
  70. package/scripts/analyze.js +0 -31
  71. package/scripts/build.js +0 -20
  72. package/scripts/serve.js +0 -20
  73. package/scripts/settings.js +0 -65
  74. package/scripts/test.js +0 -14
  75. package/src/crdt-helpers.ts +0 -89
  76. package/src/crdt.ts +0 -45
  77. package/src/database.ts +0 -61
  78. package/src/fireproof.ts +0 -6
  79. package/src/loader-helpers.ts +0 -53
  80. package/src/loader.ts +0 -66
  81. package/src/store-browser.ts +0 -76
  82. package/src/store-fs.ts +0 -51
  83. package/src/store.ts +0 -32
  84. package/src/transaction.ts +0 -68
  85. package/src/types.d.ts +0 -38
  86. package/test/crdt.test.js +0 -142
  87. package/test/database.test.js +0 -144
  88. package/test/fireproof.test.js +0 -50
  89. package/test/globals.d.ts +0 -4
  90. package/test/hello.test.js +0 -9
  91. package/test/helpers.js +0 -34
  92. package/test/loader.test.js +0 -112
  93. package/test/store-fs.test.js +0 -105
  94. package/test/transaction.test.js +0 -90
  95. package/tsconfig.json +0 -18
  96. package/webpack.config.cjs +0 -17
@@ -1,1358 +0,0 @@
1
- // node_modules/idb/build/wrap-idb-value.js
2
- var instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);
3
- var idbProxyableTypes;
4
- var cursorAdvanceMethods;
5
- function getIdbProxyableTypes() {
6
- return idbProxyableTypes || (idbProxyableTypes = [
7
- IDBDatabase,
8
- IDBObjectStore,
9
- IDBIndex,
10
- IDBCursor,
11
- IDBTransaction
12
- ]);
13
- }
14
- function getCursorAdvanceMethods() {
15
- return cursorAdvanceMethods || (cursorAdvanceMethods = [
16
- IDBCursor.prototype.advance,
17
- IDBCursor.prototype.continue,
18
- IDBCursor.prototype.continuePrimaryKey
19
- ]);
20
- }
21
- var cursorRequestMap = /* @__PURE__ */ new WeakMap();
22
- var transactionDoneMap = /* @__PURE__ */ new WeakMap();
23
- var transactionStoreNamesMap = /* @__PURE__ */ new WeakMap();
24
- var transformCache = /* @__PURE__ */ new WeakMap();
25
- var reverseTransformCache = /* @__PURE__ */ new WeakMap();
26
- function promisifyRequest(request) {
27
- const promise = new Promise((resolve, reject) => {
28
- const unlisten = () => {
29
- request.removeEventListener("success", success);
30
- request.removeEventListener("error", error);
31
- };
32
- const success = () => {
33
- resolve(wrap(request.result));
34
- unlisten();
35
- };
36
- const error = () => {
37
- reject(request.error);
38
- unlisten();
39
- };
40
- request.addEventListener("success", success);
41
- request.addEventListener("error", error);
42
- });
43
- promise.then((value) => {
44
- if (value instanceof IDBCursor) {
45
- cursorRequestMap.set(value, request);
46
- }
47
- }).catch(() => {
48
- });
49
- reverseTransformCache.set(promise, request);
50
- return promise;
51
- }
52
- function cacheDonePromiseForTransaction(tx) {
53
- if (transactionDoneMap.has(tx))
54
- return;
55
- const done = new Promise((resolve, reject) => {
56
- const unlisten = () => {
57
- tx.removeEventListener("complete", complete);
58
- tx.removeEventListener("error", error);
59
- tx.removeEventListener("abort", error);
60
- };
61
- const complete = () => {
62
- resolve();
63
- unlisten();
64
- };
65
- const error = () => {
66
- reject(tx.error || new DOMException("AbortError", "AbortError"));
67
- unlisten();
68
- };
69
- tx.addEventListener("complete", complete);
70
- tx.addEventListener("error", error);
71
- tx.addEventListener("abort", error);
72
- });
73
- transactionDoneMap.set(tx, done);
74
- }
75
- var idbProxyTraps = {
76
- get(target, prop, receiver) {
77
- if (target instanceof IDBTransaction) {
78
- if (prop === "done")
79
- return transactionDoneMap.get(target);
80
- if (prop === "objectStoreNames") {
81
- return target.objectStoreNames || transactionStoreNamesMap.get(target);
82
- }
83
- if (prop === "store") {
84
- return receiver.objectStoreNames[1] ? void 0 : receiver.objectStore(receiver.objectStoreNames[0]);
85
- }
86
- }
87
- return wrap(target[prop]);
88
- },
89
- set(target, prop, value) {
90
- target[prop] = value;
91
- return true;
92
- },
93
- has(target, prop) {
94
- if (target instanceof IDBTransaction && (prop === "done" || prop === "store")) {
95
- return true;
96
- }
97
- return prop in target;
98
- }
99
- };
100
- function replaceTraps(callback) {
101
- idbProxyTraps = callback(idbProxyTraps);
102
- }
103
- function wrapFunction(func) {
104
- if (func === IDBDatabase.prototype.transaction && !("objectStoreNames" in IDBTransaction.prototype)) {
105
- return function(storeNames, ...args) {
106
- const tx = func.call(unwrap(this), storeNames, ...args);
107
- transactionStoreNamesMap.set(tx, storeNames.sort ? storeNames.sort() : [storeNames]);
108
- return wrap(tx);
109
- };
110
- }
111
- if (getCursorAdvanceMethods().includes(func)) {
112
- return function(...args) {
113
- func.apply(unwrap(this), args);
114
- return wrap(cursorRequestMap.get(this));
115
- };
116
- }
117
- return function(...args) {
118
- return wrap(func.apply(unwrap(this), args));
119
- };
120
- }
121
- function transformCachableValue(value) {
122
- if (typeof value === "function")
123
- return wrapFunction(value);
124
- if (value instanceof IDBTransaction)
125
- cacheDonePromiseForTransaction(value);
126
- if (instanceOfAny(value, getIdbProxyableTypes()))
127
- return new Proxy(value, idbProxyTraps);
128
- return value;
129
- }
130
- function wrap(value) {
131
- if (value instanceof IDBRequest)
132
- return promisifyRequest(value);
133
- if (transformCache.has(value))
134
- return transformCache.get(value);
135
- const newValue = transformCachableValue(value);
136
- if (newValue !== value) {
137
- transformCache.set(value, newValue);
138
- reverseTransformCache.set(newValue, value);
139
- }
140
- return newValue;
141
- }
142
- var unwrap = (value) => reverseTransformCache.get(value);
143
-
144
- // node_modules/idb/build/index.js
145
- function openDB(name, version, { blocked, upgrade, blocking, terminated } = {}) {
146
- const request = indexedDB.open(name, version);
147
- const openPromise = wrap(request);
148
- if (upgrade) {
149
- request.addEventListener("upgradeneeded", (event) => {
150
- upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction), event);
151
- });
152
- }
153
- if (blocked) {
154
- request.addEventListener("blocked", (event) => blocked(
155
- // Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
156
- event.oldVersion,
157
- event.newVersion,
158
- event
159
- ));
160
- }
161
- openPromise.then((db) => {
162
- if (terminated)
163
- db.addEventListener("close", () => terminated());
164
- if (blocking) {
165
- db.addEventListener("versionchange", (event) => blocking(event.oldVersion, event.newVersion, event));
166
- }
167
- }).catch(() => {
168
- });
169
- return openPromise;
170
- }
171
- var readMethods = ["get", "getKey", "getAll", "getAllKeys", "count"];
172
- var writeMethods = ["put", "add", "delete", "clear"];
173
- var cachedMethods = /* @__PURE__ */ new Map();
174
- function getMethod(target, prop) {
175
- if (!(target instanceof IDBDatabase && !(prop in target) && typeof prop === "string")) {
176
- return;
177
- }
178
- if (cachedMethods.get(prop))
179
- return cachedMethods.get(prop);
180
- const targetFuncName = prop.replace(/FromIndex$/, "");
181
- const useIndex = prop !== targetFuncName;
182
- const isWrite = writeMethods.includes(targetFuncName);
183
- if (
184
- // Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
185
- !(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) || !(isWrite || readMethods.includes(targetFuncName))
186
- ) {
187
- return;
188
- }
189
- const method = async function(storeName, ...args) {
190
- const tx = this.transaction(storeName, isWrite ? "readwrite" : "readonly");
191
- let target2 = tx.store;
192
- if (useIndex)
193
- target2 = target2.index(args.shift());
194
- return (await Promise.all([
195
- target2[targetFuncName](...args),
196
- isWrite && tx.done
197
- ]))[0];
198
- };
199
- cachedMethods.set(prop, method);
200
- return method;
201
- }
202
- replaceTraps((oldTraps) => ({
203
- ...oldTraps,
204
- get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get(target, prop, receiver),
205
- has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop)
206
- }));
207
-
208
- // node_modules/multiformats/vendor/varint.js
209
- var encode_1 = encode;
210
- var MSB = 128;
211
- var REST = 127;
212
- var MSBALL = ~REST;
213
- var INT = Math.pow(2, 31);
214
- function encode(num, out, offset) {
215
- out = out || [];
216
- offset = offset || 0;
217
- var oldOffset = offset;
218
- while (num >= INT) {
219
- out[offset++] = num & 255 | MSB;
220
- num /= 128;
221
- }
222
- while (num & MSBALL) {
223
- out[offset++] = num & 255 | MSB;
224
- num >>>= 7;
225
- }
226
- out[offset] = num | 0;
227
- encode.bytes = offset - oldOffset + 1;
228
- return out;
229
- }
230
- var decode = read;
231
- var MSB$1 = 128;
232
- var REST$1 = 127;
233
- function read(buf, offset) {
234
- var res = 0, offset = offset || 0, shift = 0, counter = offset, b, l = buf.length;
235
- do {
236
- if (counter >= l) {
237
- read.bytes = 0;
238
- throw new RangeError("Could not decode varint");
239
- }
240
- b = buf[counter++];
241
- res += shift < 28 ? (b & REST$1) << shift : (b & REST$1) * Math.pow(2, shift);
242
- shift += 7;
243
- } while (b >= MSB$1);
244
- read.bytes = counter - offset;
245
- return res;
246
- }
247
- var N1 = Math.pow(2, 7);
248
- var N2 = Math.pow(2, 14);
249
- var N3 = Math.pow(2, 21);
250
- var N4 = Math.pow(2, 28);
251
- var N5 = Math.pow(2, 35);
252
- var N6 = Math.pow(2, 42);
253
- var N7 = Math.pow(2, 49);
254
- var N8 = Math.pow(2, 56);
255
- var N9 = Math.pow(2, 63);
256
- var length = function(value) {
257
- return value < N1 ? 1 : value < N2 ? 2 : value < N3 ? 3 : value < N4 ? 4 : value < N5 ? 5 : value < N6 ? 6 : value < N7 ? 7 : value < N8 ? 8 : value < N9 ? 9 : 10;
258
- };
259
- var varint = {
260
- encode: encode_1,
261
- decode,
262
- encodingLength: length
263
- };
264
- var _brrp_varint = varint;
265
- var varint_default = _brrp_varint;
266
-
267
- // node_modules/multiformats/src/varint.js
268
- var decode2 = (data, offset = 0) => {
269
- const code = varint_default.decode(data, offset);
270
- return [code, varint_default.decode.bytes];
271
- };
272
- var encodeTo = (int, target, offset = 0) => {
273
- varint_default.encode(int, target, offset);
274
- return target;
275
- };
276
- var encodingLength = (int) => {
277
- return varint_default.encodingLength(int);
278
- };
279
-
280
- // node_modules/multiformats/src/bytes.js
281
- var empty = new Uint8Array(0);
282
- var equals = (aa, bb) => {
283
- if (aa === bb)
284
- return true;
285
- if (aa.byteLength !== bb.byteLength) {
286
- return false;
287
- }
288
- for (let ii = 0; ii < aa.byteLength; ii++) {
289
- if (aa[ii] !== bb[ii]) {
290
- return false;
291
- }
292
- }
293
- return true;
294
- };
295
- var coerce = (o) => {
296
- if (o instanceof Uint8Array && o.constructor.name === "Uint8Array")
297
- return o;
298
- if (o instanceof ArrayBuffer)
299
- return new Uint8Array(o);
300
- if (ArrayBuffer.isView(o)) {
301
- return new Uint8Array(o.buffer, o.byteOffset, o.byteLength);
302
- }
303
- throw new Error("Unknown type, must be binary type");
304
- };
305
-
306
- // node_modules/multiformats/src/hashes/digest.js
307
- var create = (code, digest) => {
308
- const size = digest.byteLength;
309
- const sizeOffset = encodingLength(code);
310
- const digestOffset = sizeOffset + encodingLength(size);
311
- const bytes = new Uint8Array(digestOffset + size);
312
- encodeTo(code, bytes, 0);
313
- encodeTo(size, bytes, sizeOffset);
314
- bytes.set(digest, digestOffset);
315
- return new Digest(code, size, digest, bytes);
316
- };
317
- var decode3 = (multihash) => {
318
- const bytes = coerce(multihash);
319
- const [code, sizeOffset] = decode2(bytes);
320
- const [size, digestOffset] = decode2(bytes.subarray(sizeOffset));
321
- const digest = bytes.subarray(sizeOffset + digestOffset);
322
- if (digest.byteLength !== size) {
323
- throw new Error("Incorrect length");
324
- }
325
- return new Digest(code, size, digest, bytes);
326
- };
327
- var equals2 = (a, b) => {
328
- if (a === b) {
329
- return true;
330
- } else {
331
- const data = (
332
- /** @type {{code?:unknown, size?:unknown, bytes?:unknown}} */
333
- b
334
- );
335
- return a.code === data.code && a.size === data.size && data.bytes instanceof Uint8Array && equals(a.bytes, data.bytes);
336
- }
337
- };
338
- var Digest = class {
339
- /**
340
- * Creates a multihash digest.
341
- *
342
- * @param {Code} code
343
- * @param {Size} size
344
- * @param {Uint8Array} digest
345
- * @param {Uint8Array} bytes
346
- */
347
- constructor(code, size, digest, bytes) {
348
- this.code = code;
349
- this.size = size;
350
- this.digest = digest;
351
- this.bytes = bytes;
352
- }
353
- };
354
-
355
- // node_modules/multiformats/vendor/base-x.js
356
- function base(ALPHABET, name) {
357
- if (ALPHABET.length >= 255) {
358
- throw new TypeError("Alphabet too long");
359
- }
360
- var BASE_MAP = new Uint8Array(256);
361
- for (var j = 0; j < BASE_MAP.length; j++) {
362
- BASE_MAP[j] = 255;
363
- }
364
- for (var i = 0; i < ALPHABET.length; i++) {
365
- var x = ALPHABET.charAt(i);
366
- var xc = x.charCodeAt(0);
367
- if (BASE_MAP[xc] !== 255) {
368
- throw new TypeError(x + " is ambiguous");
369
- }
370
- BASE_MAP[xc] = i;
371
- }
372
- var BASE = ALPHABET.length;
373
- var LEADER = ALPHABET.charAt(0);
374
- var FACTOR = Math.log(BASE) / Math.log(256);
375
- var iFACTOR = Math.log(256) / Math.log(BASE);
376
- function encode3(source) {
377
- if (source instanceof Uint8Array)
378
- ;
379
- else if (ArrayBuffer.isView(source)) {
380
- source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
381
- } else if (Array.isArray(source)) {
382
- source = Uint8Array.from(source);
383
- }
384
- if (!(source instanceof Uint8Array)) {
385
- throw new TypeError("Expected Uint8Array");
386
- }
387
- if (source.length === 0) {
388
- return "";
389
- }
390
- var zeroes = 0;
391
- var length2 = 0;
392
- var pbegin = 0;
393
- var pend = source.length;
394
- while (pbegin !== pend && source[pbegin] === 0) {
395
- pbegin++;
396
- zeroes++;
397
- }
398
- var size = (pend - pbegin) * iFACTOR + 1 >>> 0;
399
- var b58 = new Uint8Array(size);
400
- while (pbegin !== pend) {
401
- var carry = source[pbegin];
402
- var i2 = 0;
403
- for (var it1 = size - 1; (carry !== 0 || i2 < length2) && it1 !== -1; it1--, i2++) {
404
- carry += 256 * b58[it1] >>> 0;
405
- b58[it1] = carry % BASE >>> 0;
406
- carry = carry / BASE >>> 0;
407
- }
408
- if (carry !== 0) {
409
- throw new Error("Non-zero carry");
410
- }
411
- length2 = i2;
412
- pbegin++;
413
- }
414
- var it2 = size - length2;
415
- while (it2 !== size && b58[it2] === 0) {
416
- it2++;
417
- }
418
- var str = LEADER.repeat(zeroes);
419
- for (; it2 < size; ++it2) {
420
- str += ALPHABET.charAt(b58[it2]);
421
- }
422
- return str;
423
- }
424
- function decodeUnsafe(source) {
425
- if (typeof source !== "string") {
426
- throw new TypeError("Expected String");
427
- }
428
- if (source.length === 0) {
429
- return new Uint8Array();
430
- }
431
- var psz = 0;
432
- if (source[psz] === " ") {
433
- return;
434
- }
435
- var zeroes = 0;
436
- var length2 = 0;
437
- while (source[psz] === LEADER) {
438
- zeroes++;
439
- psz++;
440
- }
441
- var size = (source.length - psz) * FACTOR + 1 >>> 0;
442
- var b256 = new Uint8Array(size);
443
- while (source[psz]) {
444
- var carry = BASE_MAP[source.charCodeAt(psz)];
445
- if (carry === 255) {
446
- return;
447
- }
448
- var i2 = 0;
449
- for (var it3 = size - 1; (carry !== 0 || i2 < length2) && it3 !== -1; it3--, i2++) {
450
- carry += BASE * b256[it3] >>> 0;
451
- b256[it3] = carry % 256 >>> 0;
452
- carry = carry / 256 >>> 0;
453
- }
454
- if (carry !== 0) {
455
- throw new Error("Non-zero carry");
456
- }
457
- length2 = i2;
458
- psz++;
459
- }
460
- if (source[psz] === " ") {
461
- return;
462
- }
463
- var it4 = size - length2;
464
- while (it4 !== size && b256[it4] === 0) {
465
- it4++;
466
- }
467
- var vch = new Uint8Array(zeroes + (size - it4));
468
- var j2 = zeroes;
469
- while (it4 !== size) {
470
- vch[j2++] = b256[it4++];
471
- }
472
- return vch;
473
- }
474
- function decode5(string) {
475
- var buffer = decodeUnsafe(string);
476
- if (buffer) {
477
- return buffer;
478
- }
479
- throw new Error(`Non-${name} character`);
480
- }
481
- return {
482
- encode: encode3,
483
- decodeUnsafe,
484
- decode: decode5
485
- };
486
- }
487
- var src = base;
488
- var _brrp__multiformats_scope_baseX = src;
489
- var base_x_default = _brrp__multiformats_scope_baseX;
490
-
491
- // node_modules/multiformats/src/bases/base.js
492
- var Encoder = class {
493
- /**
494
- * @param {Base} name
495
- * @param {Prefix} prefix
496
- * @param {(bytes:Uint8Array) => string} baseEncode
497
- */
498
- constructor(name, prefix, baseEncode) {
499
- this.name = name;
500
- this.prefix = prefix;
501
- this.baseEncode = baseEncode;
502
- }
503
- /**
504
- * @param {Uint8Array} bytes
505
- * @returns {API.Multibase<Prefix>}
506
- */
507
- encode(bytes) {
508
- if (bytes instanceof Uint8Array) {
509
- return `${this.prefix}${this.baseEncode(bytes)}`;
510
- } else {
511
- throw Error("Unknown type, must be binary type");
512
- }
513
- }
514
- };
515
- var Decoder = class {
516
- /**
517
- * @param {Base} name
518
- * @param {Prefix} prefix
519
- * @param {(text:string) => Uint8Array} baseDecode
520
- */
521
- constructor(name, prefix, baseDecode) {
522
- this.name = name;
523
- this.prefix = prefix;
524
- if (prefix.codePointAt(0) === void 0) {
525
- throw new Error("Invalid prefix character");
526
- }
527
- this.prefixCodePoint = /** @type {number} */
528
- prefix.codePointAt(0);
529
- this.baseDecode = baseDecode;
530
- }
531
- /**
532
- * @param {string} text
533
- */
534
- decode(text) {
535
- if (typeof text === "string") {
536
- if (text.codePointAt(0) !== this.prefixCodePoint) {
537
- throw Error(`Unable to decode multibase string ${JSON.stringify(text)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);
538
- }
539
- return this.baseDecode(text.slice(this.prefix.length));
540
- } else {
541
- throw Error("Can only multibase decode strings");
542
- }
543
- }
544
- /**
545
- * @template {string} OtherPrefix
546
- * @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
547
- * @returns {ComposedDecoder<Prefix|OtherPrefix>}
548
- */
549
- or(decoder) {
550
- return or(this, decoder);
551
- }
552
- };
553
- var ComposedDecoder = class {
554
- /**
555
- * @param {Decoders<Prefix>} decoders
556
- */
557
- constructor(decoders) {
558
- this.decoders = decoders;
559
- }
560
- /**
561
- * @template {string} OtherPrefix
562
- * @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
563
- * @returns {ComposedDecoder<Prefix|OtherPrefix>}
564
- */
565
- or(decoder) {
566
- return or(this, decoder);
567
- }
568
- /**
569
- * @param {string} input
570
- * @returns {Uint8Array}
571
- */
572
- decode(input) {
573
- const prefix = (
574
- /** @type {Prefix} */
575
- input[0]
576
- );
577
- const decoder = this.decoders[prefix];
578
- if (decoder) {
579
- return decoder.decode(input);
580
- } else {
581
- throw RangeError(`Unable to decode multibase string ${JSON.stringify(input)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`);
582
- }
583
- }
584
- };
585
- var or = (left, right) => new ComposedDecoder(
586
- /** @type {Decoders<L|R>} */
587
- {
588
- ...left.decoders || { [
589
- /** @type API.UnibaseDecoder<L> */
590
- left.prefix
591
- ]: left },
592
- ...right.decoders || { [
593
- /** @type API.UnibaseDecoder<R> */
594
- right.prefix
595
- ]: right }
596
- }
597
- );
598
- var Codec = class {
599
- /**
600
- * @param {Base} name
601
- * @param {Prefix} prefix
602
- * @param {(bytes:Uint8Array) => string} baseEncode
603
- * @param {(text:string) => Uint8Array} baseDecode
604
- */
605
- constructor(name, prefix, baseEncode, baseDecode) {
606
- this.name = name;
607
- this.prefix = prefix;
608
- this.baseEncode = baseEncode;
609
- this.baseDecode = baseDecode;
610
- this.encoder = new Encoder(name, prefix, baseEncode);
611
- this.decoder = new Decoder(name, prefix, baseDecode);
612
- }
613
- /**
614
- * @param {Uint8Array} input
615
- */
616
- encode(input) {
617
- return this.encoder.encode(input);
618
- }
619
- /**
620
- * @param {string} input
621
- */
622
- decode(input) {
623
- return this.decoder.decode(input);
624
- }
625
- };
626
- var from = ({ name, prefix, encode: encode3, decode: decode5 }) => new Codec(name, prefix, encode3, decode5);
627
- var baseX = ({ prefix, name, alphabet }) => {
628
- const { encode: encode3, decode: decode5 } = base_x_default(alphabet, name);
629
- return from({
630
- prefix,
631
- name,
632
- encode: encode3,
633
- /**
634
- * @param {string} text
635
- */
636
- decode: (text) => coerce(decode5(text))
637
- });
638
- };
639
- var decode4 = (string, alphabet, bitsPerChar, name) => {
640
- const codes = {};
641
- for (let i = 0; i < alphabet.length; ++i) {
642
- codes[alphabet[i]] = i;
643
- }
644
- let end = string.length;
645
- while (string[end - 1] === "=") {
646
- --end;
647
- }
648
- const out = new Uint8Array(end * bitsPerChar / 8 | 0);
649
- let bits = 0;
650
- let buffer = 0;
651
- let written = 0;
652
- for (let i = 0; i < end; ++i) {
653
- const value = codes[string[i]];
654
- if (value === void 0) {
655
- throw new SyntaxError(`Non-${name} character`);
656
- }
657
- buffer = buffer << bitsPerChar | value;
658
- bits += bitsPerChar;
659
- if (bits >= 8) {
660
- bits -= 8;
661
- out[written++] = 255 & buffer >> bits;
662
- }
663
- }
664
- if (bits >= bitsPerChar || 255 & buffer << 8 - bits) {
665
- throw new SyntaxError("Unexpected end of data");
666
- }
667
- return out;
668
- };
669
- var encode2 = (data, alphabet, bitsPerChar) => {
670
- const pad = alphabet[alphabet.length - 1] === "=";
671
- const mask = (1 << bitsPerChar) - 1;
672
- let out = "";
673
- let bits = 0;
674
- let buffer = 0;
675
- for (let i = 0; i < data.length; ++i) {
676
- buffer = buffer << 8 | data[i];
677
- bits += 8;
678
- while (bits > bitsPerChar) {
679
- bits -= bitsPerChar;
680
- out += alphabet[mask & buffer >> bits];
681
- }
682
- }
683
- if (bits) {
684
- out += alphabet[mask & buffer << bitsPerChar - bits];
685
- }
686
- if (pad) {
687
- while (out.length * bitsPerChar & 7) {
688
- out += "=";
689
- }
690
- }
691
- return out;
692
- };
693
- var rfc4648 = ({ name, prefix, bitsPerChar, alphabet }) => {
694
- return from({
695
- prefix,
696
- name,
697
- encode(input) {
698
- return encode2(input, alphabet, bitsPerChar);
699
- },
700
- decode(input) {
701
- return decode4(input, alphabet, bitsPerChar, name);
702
- }
703
- });
704
- };
705
-
706
- // node_modules/multiformats/src/bases/base58.js
707
- var base58btc = baseX({
708
- name: "base58btc",
709
- prefix: "z",
710
- alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
711
- });
712
- var base58flickr = baseX({
713
- name: "base58flickr",
714
- prefix: "Z",
715
- alphabet: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
716
- });
717
-
718
- // node_modules/multiformats/src/bases/base32.js
719
- var base32 = rfc4648({
720
- prefix: "b",
721
- name: "base32",
722
- alphabet: "abcdefghijklmnopqrstuvwxyz234567",
723
- bitsPerChar: 5
724
- });
725
- var base32upper = rfc4648({
726
- prefix: "B",
727
- name: "base32upper",
728
- alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
729
- bitsPerChar: 5
730
- });
731
- var base32pad = rfc4648({
732
- prefix: "c",
733
- name: "base32pad",
734
- alphabet: "abcdefghijklmnopqrstuvwxyz234567=",
735
- bitsPerChar: 5
736
- });
737
- var base32padupper = rfc4648({
738
- prefix: "C",
739
- name: "base32padupper",
740
- alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",
741
- bitsPerChar: 5
742
- });
743
- var base32hex = rfc4648({
744
- prefix: "v",
745
- name: "base32hex",
746
- alphabet: "0123456789abcdefghijklmnopqrstuv",
747
- bitsPerChar: 5
748
- });
749
- var base32hexupper = rfc4648({
750
- prefix: "V",
751
- name: "base32hexupper",
752
- alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV",
753
- bitsPerChar: 5
754
- });
755
- var base32hexpad = rfc4648({
756
- prefix: "t",
757
- name: "base32hexpad",
758
- alphabet: "0123456789abcdefghijklmnopqrstuv=",
759
- bitsPerChar: 5
760
- });
761
- var base32hexpadupper = rfc4648({
762
- prefix: "T",
763
- name: "base32hexpadupper",
764
- alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV=",
765
- bitsPerChar: 5
766
- });
767
- var base32z = rfc4648({
768
- prefix: "h",
769
- name: "base32z",
770
- alphabet: "ybndrfg8ejkmcpqxot1uwisza345h769",
771
- bitsPerChar: 5
772
- });
773
-
774
- // node_modules/multiformats/src/cid.js
775
- var format = (link, base2) => {
776
- const { bytes, version } = link;
777
- switch (version) {
778
- case 0:
779
- return toStringV0(
780
- bytes,
781
- baseCache(link),
782
- /** @type {API.MultibaseEncoder<"z">} */
783
- base2 || base58btc.encoder
784
- );
785
- default:
786
- return toStringV1(
787
- bytes,
788
- baseCache(link),
789
- /** @type {API.MultibaseEncoder<Prefix>} */
790
- base2 || base32.encoder
791
- );
792
- }
793
- };
794
- var cache = /* @__PURE__ */ new WeakMap();
795
- var baseCache = (cid) => {
796
- const baseCache2 = cache.get(cid);
797
- if (baseCache2 == null) {
798
- const baseCache3 = /* @__PURE__ */ new Map();
799
- cache.set(cid, baseCache3);
800
- return baseCache3;
801
- }
802
- return baseCache2;
803
- };
804
- var CID = class _CID {
805
- /**
806
- * @param {Version} version - Version of the CID
807
- * @param {Format} code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
808
- * @param {API.MultihashDigest<Alg>} multihash - (Multi)hash of the of the content.
809
- * @param {Uint8Array} bytes
810
- *
811
- */
812
- constructor(version, code, multihash, bytes) {
813
- this.code = code;
814
- this.version = version;
815
- this.multihash = multihash;
816
- this.bytes = bytes;
817
- this["/"] = bytes;
818
- }
819
- /**
820
- * Signalling `cid.asCID === cid` has been replaced with `cid['/'] === cid.bytes`
821
- * please either use `CID.asCID(cid)` or switch to new signalling mechanism
822
- *
823
- * @deprecated
824
- */
825
- get asCID() {
826
- return this;
827
- }
828
- // ArrayBufferView
829
- get byteOffset() {
830
- return this.bytes.byteOffset;
831
- }
832
- // ArrayBufferView
833
- get byteLength() {
834
- return this.bytes.byteLength;
835
- }
836
- /**
837
- * @returns {CID<Data, API.DAG_PB, API.SHA_256, 0>}
838
- */
839
- toV0() {
840
- switch (this.version) {
841
- case 0: {
842
- return (
843
- /** @type {CID<Data, API.DAG_PB, API.SHA_256, 0>} */
844
- this
845
- );
846
- }
847
- case 1: {
848
- const { code, multihash } = this;
849
- if (code !== DAG_PB_CODE) {
850
- throw new Error("Cannot convert a non dag-pb CID to CIDv0");
851
- }
852
- if (multihash.code !== SHA_256_CODE) {
853
- throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");
854
- }
855
- return (
856
- /** @type {CID<Data, API.DAG_PB, API.SHA_256, 0>} */
857
- _CID.createV0(
858
- /** @type {API.MultihashDigest<API.SHA_256>} */
859
- multihash
860
- )
861
- );
862
- }
863
- default: {
864
- throw Error(
865
- `Can not convert CID version ${this.version} to version 0. This is a bug please report`
866
- );
867
- }
868
- }
869
- }
870
- /**
871
- * @returns {CID<Data, Format, Alg, 1>}
872
- */
873
- toV1() {
874
- switch (this.version) {
875
- case 0: {
876
- const { code, digest } = this.multihash;
877
- const multihash = create(code, digest);
878
- return (
879
- /** @type {CID<Data, Format, Alg, 1>} */
880
- _CID.createV1(this.code, multihash)
881
- );
882
- }
883
- case 1: {
884
- return (
885
- /** @type {CID<Data, Format, Alg, 1>} */
886
- this
887
- );
888
- }
889
- default: {
890
- throw Error(
891
- `Can not convert CID version ${this.version} to version 1. This is a bug please report`
892
- );
893
- }
894
- }
895
- }
896
- /**
897
- * @param {unknown} other
898
- * @returns {other is CID<Data, Format, Alg, Version>}
899
- */
900
- equals(other) {
901
- return _CID.equals(this, other);
902
- }
903
- /**
904
- * @template {unknown} Data
905
- * @template {number} Format
906
- * @template {number} Alg
907
- * @template {API.Version} Version
908
- * @param {API.Link<Data, Format, Alg, Version>} self
909
- * @param {unknown} other
910
- * @returns {other is CID}
911
- */
912
- static equals(self, other) {
913
- const unknown = (
914
- /** @type {{code?:unknown, version?:unknown, multihash?:unknown}} */
915
- other
916
- );
917
- return unknown && self.code === unknown.code && self.version === unknown.version && equals2(self.multihash, unknown.multihash);
918
- }
919
- /**
920
- * @param {API.MultibaseEncoder<string>} [base]
921
- * @returns {string}
922
- */
923
- toString(base2) {
924
- return format(this, base2);
925
- }
926
- toJSON() {
927
- return { "/": format(this) };
928
- }
929
- link() {
930
- return this;
931
- }
932
- get [Symbol.toStringTag]() {
933
- return "CID";
934
- }
935
- // Legacy
936
- [Symbol.for("nodejs.util.inspect.custom")]() {
937
- return `CID(${this.toString()})`;
938
- }
939
- /**
940
- * Takes any input `value` and returns a `CID` instance if it was
941
- * a `CID` otherwise returns `null`. If `value` is instanceof `CID`
942
- * it will return value back. If `value` is not instance of this CID
943
- * class, but is compatible CID it will return new instance of this
944
- * `CID` class. Otherwise returns null.
945
- *
946
- * This allows two different incompatible versions of CID library to
947
- * co-exist and interop as long as binary interface is compatible.
948
- *
949
- * @template {unknown} Data
950
- * @template {number} Format
951
- * @template {number} Alg
952
- * @template {API.Version} Version
953
- * @template {unknown} U
954
- * @param {API.Link<Data, Format, Alg, Version>|U} input
955
- * @returns {CID<Data, Format, Alg, Version>|null}
956
- */
957
- static asCID(input) {
958
- if (input == null) {
959
- return null;
960
- }
961
- const value = (
962
- /** @type {any} */
963
- input
964
- );
965
- if (value instanceof _CID) {
966
- return value;
967
- } else if (value["/"] != null && value["/"] === value.bytes || value.asCID === value) {
968
- const { version, code, multihash, bytes } = value;
969
- return new _CID(
970
- version,
971
- code,
972
- /** @type {API.MultihashDigest<Alg>} */
973
- multihash,
974
- bytes || encodeCID(version, code, multihash.bytes)
975
- );
976
- } else if (value[cidSymbol] === true) {
977
- const { version, multihash, code } = value;
978
- const digest = (
979
- /** @type {API.MultihashDigest<Alg>} */
980
- decode3(multihash)
981
- );
982
- return _CID.create(version, code, digest);
983
- } else {
984
- return null;
985
- }
986
- }
987
- /**
988
- *
989
- * @template {unknown} Data
990
- * @template {number} Format
991
- * @template {number} Alg
992
- * @template {API.Version} Version
993
- * @param {Version} version - Version of the CID
994
- * @param {Format} code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
995
- * @param {API.MultihashDigest<Alg>} digest - (Multi)hash of the of the content.
996
- * @returns {CID<Data, Format, Alg, Version>}
997
- */
998
- static create(version, code, digest) {
999
- if (typeof code !== "number") {
1000
- throw new Error("String codecs are no longer supported");
1001
- }
1002
- if (!(digest.bytes instanceof Uint8Array)) {
1003
- throw new Error("Invalid digest");
1004
- }
1005
- switch (version) {
1006
- case 0: {
1007
- if (code !== DAG_PB_CODE) {
1008
- throw new Error(
1009
- `Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`
1010
- );
1011
- } else {
1012
- return new _CID(version, code, digest, digest.bytes);
1013
- }
1014
- }
1015
- case 1: {
1016
- const bytes = encodeCID(version, code, digest.bytes);
1017
- return new _CID(version, code, digest, bytes);
1018
- }
1019
- default: {
1020
- throw new Error("Invalid version");
1021
- }
1022
- }
1023
- }
1024
- /**
1025
- * Simplified version of `create` for CIDv0.
1026
- *
1027
- * @template {unknown} [T=unknown]
1028
- * @param {API.MultihashDigest<typeof SHA_256_CODE>} digest - Multihash.
1029
- * @returns {CID<T, typeof DAG_PB_CODE, typeof SHA_256_CODE, 0>}
1030
- */
1031
- static createV0(digest) {
1032
- return _CID.create(0, DAG_PB_CODE, digest);
1033
- }
1034
- /**
1035
- * Simplified version of `create` for CIDv1.
1036
- *
1037
- * @template {unknown} Data
1038
- * @template {number} Code
1039
- * @template {number} Alg
1040
- * @param {Code} code - Content encoding format code.
1041
- * @param {API.MultihashDigest<Alg>} digest - Miltihash of the content.
1042
- * @returns {CID<Data, Code, Alg, 1>}
1043
- */
1044
- static createV1(code, digest) {
1045
- return _CID.create(1, code, digest);
1046
- }
1047
- /**
1048
- * Decoded a CID from its binary representation. The byte array must contain
1049
- * only the CID with no additional bytes.
1050
- *
1051
- * An error will be thrown if the bytes provided do not contain a valid
1052
- * binary representation of a CID.
1053
- *
1054
- * @template {unknown} Data
1055
- * @template {number} Code
1056
- * @template {number} Alg
1057
- * @template {API.Version} Ver
1058
- * @param {API.ByteView<API.Link<Data, Code, Alg, Ver>>} bytes
1059
- * @returns {CID<Data, Code, Alg, Ver>}
1060
- */
1061
- static decode(bytes) {
1062
- const [cid, remainder] = _CID.decodeFirst(bytes);
1063
- if (remainder.length) {
1064
- throw new Error("Incorrect length");
1065
- }
1066
- return cid;
1067
- }
1068
- /**
1069
- * Decoded a CID from its binary representation at the beginning of a byte
1070
- * array.
1071
- *
1072
- * Returns an array with the first element containing the CID and the second
1073
- * element containing the remainder of the original byte array. The remainder
1074
- * will be a zero-length byte array if the provided bytes only contained a
1075
- * binary CID representation.
1076
- *
1077
- * @template {unknown} T
1078
- * @template {number} C
1079
- * @template {number} A
1080
- * @template {API.Version} V
1081
- * @param {API.ByteView<API.Link<T, C, A, V>>} bytes
1082
- * @returns {[CID<T, C, A, V>, Uint8Array]}
1083
- */
1084
- static decodeFirst(bytes) {
1085
- const specs = _CID.inspectBytes(bytes);
1086
- const prefixSize = specs.size - specs.multihashSize;
1087
- const multihashBytes = coerce(
1088
- bytes.subarray(prefixSize, prefixSize + specs.multihashSize)
1089
- );
1090
- if (multihashBytes.byteLength !== specs.multihashSize) {
1091
- throw new Error("Incorrect length");
1092
- }
1093
- const digestBytes = multihashBytes.subarray(
1094
- specs.multihashSize - specs.digestSize
1095
- );
1096
- const digest = new Digest(
1097
- specs.multihashCode,
1098
- specs.digestSize,
1099
- digestBytes,
1100
- multihashBytes
1101
- );
1102
- const cid = specs.version === 0 ? _CID.createV0(
1103
- /** @type {API.MultihashDigest<API.SHA_256>} */
1104
- digest
1105
- ) : _CID.createV1(specs.codec, digest);
1106
- return [
1107
- /** @type {CID<T, C, A, V>} */
1108
- cid,
1109
- bytes.subarray(specs.size)
1110
- ];
1111
- }
1112
- /**
1113
- * Inspect the initial bytes of a CID to determine its properties.
1114
- *
1115
- * Involves decoding up to 4 varints. Typically this will require only 4 to 6
1116
- * bytes but for larger multicodec code values and larger multihash digest
1117
- * lengths these varints can be quite large. It is recommended that at least
1118
- * 10 bytes be made available in the `initialBytes` argument for a complete
1119
- * inspection.
1120
- *
1121
- * @template {unknown} T
1122
- * @template {number} C
1123
- * @template {number} A
1124
- * @template {API.Version} V
1125
- * @param {API.ByteView<API.Link<T, C, A, V>>} initialBytes
1126
- * @returns {{ version:V, codec:C, multihashCode:A, digestSize:number, multihashSize:number, size:number }}
1127
- */
1128
- static inspectBytes(initialBytes) {
1129
- let offset = 0;
1130
- const next = () => {
1131
- const [i, length2] = decode2(initialBytes.subarray(offset));
1132
- offset += length2;
1133
- return i;
1134
- };
1135
- let version = (
1136
- /** @type {V} */
1137
- next()
1138
- );
1139
- let codec = (
1140
- /** @type {C} */
1141
- DAG_PB_CODE
1142
- );
1143
- if (
1144
- /** @type {number} */
1145
- version === 18
1146
- ) {
1147
- version = /** @type {V} */
1148
- 0;
1149
- offset = 0;
1150
- } else {
1151
- codec = /** @type {C} */
1152
- next();
1153
- }
1154
- if (version !== 0 && version !== 1) {
1155
- throw new RangeError(`Invalid CID version ${version}`);
1156
- }
1157
- const prefixSize = offset;
1158
- const multihashCode = (
1159
- /** @type {A} */
1160
- next()
1161
- );
1162
- const digestSize = next();
1163
- const size = offset + digestSize;
1164
- const multihashSize = size - prefixSize;
1165
- return { version, codec, multihashCode, digestSize, multihashSize, size };
1166
- }
1167
- /**
1168
- * Takes cid in a string representation and creates an instance. If `base`
1169
- * decoder is not provided will use a default from the configuration. It will
1170
- * throw an error if encoding of the CID is not compatible with supplied (or
1171
- * a default decoder).
1172
- *
1173
- * @template {string} Prefix
1174
- * @template {unknown} Data
1175
- * @template {number} Code
1176
- * @template {number} Alg
1177
- * @template {API.Version} Ver
1178
- * @param {API.ToString<API.Link<Data, Code, Alg, Ver>, Prefix>} source
1179
- * @param {API.MultibaseDecoder<Prefix>} [base]
1180
- * @returns {CID<Data, Code, Alg, Ver>}
1181
- */
1182
- static parse(source, base2) {
1183
- const [prefix, bytes] = parseCIDtoBytes(source, base2);
1184
- const cid = _CID.decode(bytes);
1185
- if (cid.version === 0 && source[0] !== "Q") {
1186
- throw Error("Version 0 CID string must not include multibase prefix");
1187
- }
1188
- baseCache(cid).set(prefix, source);
1189
- return cid;
1190
- }
1191
- };
1192
- var parseCIDtoBytes = (source, base2) => {
1193
- switch (source[0]) {
1194
- case "Q": {
1195
- const decoder = base2 || base58btc;
1196
- return [
1197
- /** @type {Prefix} */
1198
- base58btc.prefix,
1199
- decoder.decode(`${base58btc.prefix}${source}`)
1200
- ];
1201
- }
1202
- case base58btc.prefix: {
1203
- const decoder = base2 || base58btc;
1204
- return [
1205
- /** @type {Prefix} */
1206
- base58btc.prefix,
1207
- decoder.decode(source)
1208
- ];
1209
- }
1210
- case base32.prefix: {
1211
- const decoder = base2 || base32;
1212
- return [
1213
- /** @type {Prefix} */
1214
- base32.prefix,
1215
- decoder.decode(source)
1216
- ];
1217
- }
1218
- default: {
1219
- if (base2 == null) {
1220
- throw Error(
1221
- "To parse non base32 or base58btc encoded CID multibase decoder must be provided"
1222
- );
1223
- }
1224
- return [
1225
- /** @type {Prefix} */
1226
- source[0],
1227
- base2.decode(source)
1228
- ];
1229
- }
1230
- }
1231
- };
1232
- var toStringV0 = (bytes, cache2, base2) => {
1233
- const { prefix } = base2;
1234
- if (prefix !== base58btc.prefix) {
1235
- throw Error(`Cannot string encode V0 in ${base2.name} encoding`);
1236
- }
1237
- const cid = cache2.get(prefix);
1238
- if (cid == null) {
1239
- const cid2 = base2.encode(bytes).slice(1);
1240
- cache2.set(prefix, cid2);
1241
- return cid2;
1242
- } else {
1243
- return cid;
1244
- }
1245
- };
1246
- var toStringV1 = (bytes, cache2, base2) => {
1247
- const { prefix } = base2;
1248
- const cid = cache2.get(prefix);
1249
- if (cid == null) {
1250
- const cid2 = base2.encode(bytes);
1251
- cache2.set(prefix, cid2);
1252
- return cid2;
1253
- } else {
1254
- return cid;
1255
- }
1256
- };
1257
- var DAG_PB_CODE = 112;
1258
- var SHA_256_CODE = 18;
1259
- var encodeCID = (version, code, multihash) => {
1260
- const codeOffset = encodingLength(version);
1261
- const hashOffset = codeOffset + encodingLength(code);
1262
- const bytes = new Uint8Array(hashOffset + multihash.byteLength);
1263
- encodeTo(version, bytes, 0);
1264
- encodeTo(code, bytes, codeOffset);
1265
- bytes.set(multihash, hashOffset);
1266
- return bytes;
1267
- };
1268
- var cidSymbol = Symbol.for("@ipld/js-cid/CID");
1269
-
1270
- // node_modules/multiformats/src/link.js
1271
- var parse = (source, base2) => CID.parse(source, base2);
1272
-
1273
- // src/store.ts
1274
- var StoredHeader = class {
1275
- car;
1276
- constructor(jsonHeader) {
1277
- this.car = parse(jsonHeader.car);
1278
- }
1279
- };
1280
- var HeaderStore = class {
1281
- name;
1282
- constructor(name) {
1283
- this.name = name;
1284
- }
1285
- makeHeader(car) {
1286
- return JSON.stringify({ car: car.toString() });
1287
- }
1288
- parseHeader(headerData) {
1289
- const header = JSON.parse(headerData);
1290
- return new StoredHeader(header);
1291
- }
1292
- };
1293
- var CarStore = class {
1294
- name;
1295
- constructor(name) {
1296
- this.name = name;
1297
- }
1298
- };
1299
-
1300
- // src/store-browser.ts
1301
- var FORMAT = "0.9";
1302
- var CarStoreIDB = class extends CarStore {
1303
- keyId = "public";
1304
- // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
1305
- idb = null;
1306
- name = "default";
1307
- async withDB(dbWorkFun) {
1308
- if (!this.idb) {
1309
- const dbName = `fp.${FORMAT}.${this.keyId}.${this.name}.valet`;
1310
- const options = {
1311
- upgrade(db) {
1312
- db.createObjectStore("cars");
1313
- }
1314
- };
1315
- this.idb = await openDB(dbName, 0, options);
1316
- }
1317
- return await dbWorkFun(this.idb);
1318
- }
1319
- async load(cid) {
1320
- return await this.withDB(async (db) => {
1321
- const tx = db.transaction(["cars"], "readonly");
1322
- const bytes = await tx.objectStore("cars").get(cid.toString());
1323
- if (!bytes)
1324
- throw new Error(`missing block ${cid.toString()}`);
1325
- return { cid, bytes };
1326
- });
1327
- }
1328
- async save(car) {
1329
- return await this.withDB(async (db) => {
1330
- const tx = db.transaction(["cars"], "readwrite");
1331
- await tx.objectStore("cars").put(car.bytes, car.cid.toString());
1332
- return await tx.done;
1333
- });
1334
- }
1335
- };
1336
- var HeaderStoreLS = class extends HeaderStore {
1337
- keyId = "public";
1338
- name = "default";
1339
- headerKey(branch) {
1340
- return `fp.${FORMAT}.${this.keyId}.${this.name}.${branch}`;
1341
- }
1342
- // eslint-disable-next-line @typescript-eslint/require-await
1343
- async load(branch = "main") {
1344
- const bytes = localStorage.getItem(this.headerKey(branch));
1345
- return bytes ? this.parseHeader(bytes.toString()) : null;
1346
- }
1347
- // eslint-disable-next-line @typescript-eslint/require-await
1348
- async save(carCid, branch = "main") {
1349
- const headerKey = this.headerKey(branch);
1350
- return localStorage.setItem(headerKey, this.makeHeader(carCid));
1351
- }
1352
- };
1353
- export {
1354
- CarStoreIDB,
1355
- FORMAT,
1356
- HeaderStoreLS
1357
- };
1358
- //# sourceMappingURL=store-browser.esm.js.map