@wovin/core 0.0.6 → 0.0.8

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 (68) hide show
  1. package/dist/applog/applog-helpers.d.ts +5 -6
  2. package/dist/applog/applog-helpers.d.ts.map +1 -1
  3. package/dist/applog/applog-utils.d.ts +2 -1
  4. package/dist/applog/applog-utils.d.ts.map +1 -1
  5. package/dist/applog/datom-types.d.ts +34 -2
  6. package/dist/applog/datom-types.d.ts.map +1 -1
  7. package/dist/applog.min.js +90 -1
  8. package/dist/chunk-2J7BWXZ3.min.js +4373 -0
  9. package/dist/{chunk-G3GOAFHU.min.js.map → chunk-2J7BWXZ3.min.js.map} +1 -1
  10. package/dist/chunk-5MMGBK2U.min.js +1 -0
  11. package/dist/chunk-5MMGBK2U.min.js.map +1 -0
  12. package/dist/chunk-6BFNER6K.min.js +120 -0
  13. package/dist/chunk-6BFNER6K.min.js.map +1 -0
  14. package/dist/chunk-KRQZ6V4Y.min.js +36 -0
  15. package/dist/chunk-KRQZ6V4Y.min.js.map +1 -0
  16. package/dist/chunk-N4QMZPZF.min.js +10844 -0
  17. package/dist/chunk-N4QMZPZF.min.js.map +1 -0
  18. package/dist/chunk-ORHJN4G2.min.js +1425 -0
  19. package/dist/chunk-ORHJN4G2.min.js.map +1 -0
  20. package/dist/chunk-PTGUFZ3Q.min.js +1555 -0
  21. package/dist/chunk-PTGUFZ3Q.min.js.map +1 -0
  22. package/dist/chunk-YOCP7FGE.min.js +66 -0
  23. package/dist/chunk-YOCP7FGE.min.js.map +1 -0
  24. package/dist/index.d.ts +0 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.min.js +252 -1
  27. package/dist/index.min.js.map +1 -1
  28. package/dist/ipfs/car.d.ts +2 -0
  29. package/dist/ipfs/car.d.ts.map +1 -1
  30. package/dist/ipfs.min.js +32 -1
  31. package/dist/mobx/mobx-utils.d.ts +19 -11
  32. package/dist/mobx/mobx-utils.d.ts.map +1 -1
  33. package/dist/pubsub.min.js +21 -1
  34. package/dist/query/basic.d.ts +16 -31
  35. package/dist/query/basic.d.ts.map +1 -1
  36. package/dist/query/divergences.d.ts +2 -2
  37. package/dist/query/divergences.d.ts.map +1 -1
  38. package/dist/query/query-steps.d.ts +4 -0
  39. package/dist/query/query-steps.d.ts.map +1 -0
  40. package/dist/query/types.d.ts +29 -0
  41. package/dist/query/types.d.ts.map +1 -0
  42. package/dist/query.d.ts +1 -0
  43. package/dist/query.d.ts.map +1 -1
  44. package/dist/query.min.js +71 -1
  45. package/dist/thread/basic.d.ts +12 -6
  46. package/dist/thread/basic.d.ts.map +1 -1
  47. package/dist/thread/filters.d.ts +10 -6
  48. package/dist/thread/filters.d.ts.map +1 -1
  49. package/dist/thread/mapped.d.ts +3 -4
  50. package/dist/thread/mapped.d.ts.map +1 -1
  51. package/dist/thread/writeable.d.ts +3 -2
  52. package/dist/thread/writeable.d.ts.map +1 -1
  53. package/dist/thread.min.js +37 -1
  54. package/dist/types/typescript-utils.d.ts +6 -2
  55. package/dist/types/typescript-utils.d.ts.map +1 -1
  56. package/dist/types.min.js +30 -1
  57. package/package.json +83 -83
  58. package/dist/chunk-5Q6PAAMY.min.js +0 -2
  59. package/dist/chunk-5Q6PAAMY.min.js.map +0 -1
  60. package/dist/chunk-BR5DELY4.min.js +0 -2
  61. package/dist/chunk-BR5DELY4.min.js.map +0 -1
  62. package/dist/chunk-D6REGRIL.min.js +0 -2
  63. package/dist/chunk-D6REGRIL.min.js.map +0 -1
  64. package/dist/chunk-G3GOAFHU.min.js +0 -8
  65. package/dist/chunk-IVEP7DZW.min.js +0 -45
  66. package/dist/chunk-IVEP7DZW.min.js.map +0 -1
  67. package/dist/chunk-J5PGGKKW.min.js +0 -2
  68. package/dist/chunk-J5PGGKKW.min.js.map +0 -1
@@ -0,0 +1,1425 @@
1
+ import {
2
+ CID,
3
+ Logger,
4
+ Token,
5
+ Type,
6
+ decode,
7
+ decode2,
8
+ decode3,
9
+ encode,
10
+ makeCborEncoders
11
+ } from "./chunk-2J7BWXZ3.min.js";
12
+ import {
13
+ __commonJS,
14
+ __toESM
15
+ } from "./chunk-KRQZ6V4Y.min.js";
16
+
17
+ // ../../../node_modules/.pnpm/varint@6.0.0/node_modules/varint/encode.js
18
+ var require_encode = __commonJS({
19
+ "../../../node_modules/.pnpm/varint@6.0.0/node_modules/varint/encode.js"(exports, module) {
20
+ "use strict";
21
+ module.exports = encode3;
22
+ var MSB = 128;
23
+ var REST = 127;
24
+ var MSBALL = ~REST;
25
+ var INT = Math.pow(2, 31);
26
+ function encode3(num, out, offset) {
27
+ if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
28
+ encode3.bytes = 0;
29
+ throw new RangeError("Could not encode varint");
30
+ }
31
+ out = out || [];
32
+ offset = offset || 0;
33
+ var oldOffset = offset;
34
+ while (num >= INT) {
35
+ out[offset++] = num & 255 | MSB;
36
+ num /= 128;
37
+ }
38
+ while (num & MSBALL) {
39
+ out[offset++] = num & 255 | MSB;
40
+ num >>>= 7;
41
+ }
42
+ out[offset] = num | 0;
43
+ encode3.bytes = offset - oldOffset + 1;
44
+ return out;
45
+ }
46
+ }
47
+ });
48
+
49
+ // ../../../node_modules/.pnpm/varint@6.0.0/node_modules/varint/decode.js
50
+ var require_decode = __commonJS({
51
+ "../../../node_modules/.pnpm/varint@6.0.0/node_modules/varint/decode.js"(exports, module) {
52
+ "use strict";
53
+ module.exports = read;
54
+ var MSB = 128;
55
+ var REST = 127;
56
+ function read(buf, offset) {
57
+ var res = 0, offset = offset || 0, shift = 0, counter = offset, b, l = buf.length;
58
+ do {
59
+ if (counter >= l || shift > 49) {
60
+ read.bytes = 0;
61
+ throw new RangeError("Could not decode varint");
62
+ }
63
+ b = buf[counter++];
64
+ res += shift < 28 ? (b & REST) << shift : (b & REST) * Math.pow(2, shift);
65
+ shift += 7;
66
+ } while (b >= MSB);
67
+ read.bytes = counter - offset;
68
+ return res;
69
+ }
70
+ }
71
+ });
72
+
73
+ // ../../../node_modules/.pnpm/varint@6.0.0/node_modules/varint/length.js
74
+ var require_length = __commonJS({
75
+ "../../../node_modules/.pnpm/varint@6.0.0/node_modules/varint/length.js"(exports, module) {
76
+ "use strict";
77
+ var N1 = Math.pow(2, 7);
78
+ var N2 = Math.pow(2, 14);
79
+ var N3 = Math.pow(2, 21);
80
+ var N4 = Math.pow(2, 28);
81
+ var N5 = Math.pow(2, 35);
82
+ var N6 = Math.pow(2, 42);
83
+ var N7 = Math.pow(2, 49);
84
+ var N8 = Math.pow(2, 56);
85
+ var N9 = Math.pow(2, 63);
86
+ module.exports = function(value) {
87
+ 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;
88
+ };
89
+ }
90
+ });
91
+
92
+ // ../../../node_modules/.pnpm/varint@6.0.0/node_modules/varint/index.js
93
+ var require_varint = __commonJS({
94
+ "../../../node_modules/.pnpm/varint@6.0.0/node_modules/varint/index.js"(exports, module) {
95
+ "use strict";
96
+ module.exports = {
97
+ encode: require_encode(),
98
+ decode: require_decode(),
99
+ encodingLength: require_length()
100
+ };
101
+ }
102
+ });
103
+
104
+ // ../../../node_modules/.pnpm/@ipld+dag-cbor@9.0.6/node_modules/@ipld/dag-cbor/src/index.js
105
+ var CID_CBOR_TAG = 42;
106
+ function cidEncoder(obj) {
107
+ if (obj.asCID !== obj && obj["/"] !== obj.bytes) {
108
+ return null;
109
+ }
110
+ const cid = CID.asCID(obj);
111
+ if (!cid) {
112
+ return null;
113
+ }
114
+ const bytes = new Uint8Array(cid.bytes.byteLength + 1);
115
+ bytes.set(cid.bytes, 1);
116
+ return [
117
+ new Token(Type.tag, CID_CBOR_TAG),
118
+ new Token(Type.bytes, bytes)
119
+ ];
120
+ }
121
+ function undefinedEncoder() {
122
+ throw new Error("`undefined` is not supported by the IPLD Data Model and cannot be encoded");
123
+ }
124
+ function numberEncoder(num) {
125
+ if (Number.isNaN(num)) {
126
+ throw new Error("`NaN` is not supported by the IPLD Data Model and cannot be encoded");
127
+ }
128
+ if (num === Infinity || num === -Infinity) {
129
+ throw new Error("`Infinity` and `-Infinity` is not supported by the IPLD Data Model and cannot be encoded");
130
+ }
131
+ return null;
132
+ }
133
+ var encodeOptions = {
134
+ float64: true,
135
+ typeEncoders: {
136
+ Object: cidEncoder,
137
+ undefined: undefinedEncoder,
138
+ number: numberEncoder
139
+ }
140
+ };
141
+ function cidDecoder(bytes) {
142
+ if (bytes[0] !== 0) {
143
+ throw new Error("Invalid CID for CBOR tag 42; expected leading 0x00");
144
+ }
145
+ return CID.decode(bytes.subarray(1));
146
+ }
147
+ var decodeOptions = {
148
+ allowIndefinite: false,
149
+ coerceUndefinedToNull: true,
150
+ allowNaN: false,
151
+ allowInfinity: false,
152
+ allowBigInt: true,
153
+ // this will lead to BigInt for ints outside of
154
+ // safe-integer range, which may surprise users
155
+ strict: true,
156
+ useMaps: false,
157
+ rejectDuplicateMapKeys: true,
158
+ /** @type {import('cborg').TagDecoder[]} */
159
+ tags: []
160
+ };
161
+ decodeOptions.tags[CID_CBOR_TAG] = cidDecoder;
162
+ var encode2 = (node) => encode(node, encodeOptions);
163
+ var decode4 = (data) => decode(data, decodeOptions);
164
+
165
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/decoder-common.js
166
+ var import_varint = __toESM(require_varint(), 1);
167
+ var CIDV0_BYTES = {
168
+ SHA2_256: 18,
169
+ LENGTH: 32,
170
+ DAG_PB: 112
171
+ };
172
+ var V2_HEADER_LENGTH = (
173
+ /* characteristics */
174
+ 16 + 8 + 8 + 8
175
+ );
176
+ function decodeVarint(bytes, seeker) {
177
+ if (!bytes.length) {
178
+ throw new Error("Unexpected end of data");
179
+ }
180
+ const i = import_varint.default.decode(bytes);
181
+ seeker.seek(
182
+ /** @type {number} */
183
+ import_varint.default.decode.bytes
184
+ );
185
+ return i;
186
+ }
187
+ function decodeV2Header(bytes) {
188
+ const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
189
+ let offset = 0;
190
+ const header = {
191
+ version: 2,
192
+ /** @type {[bigint, bigint]} */
193
+ characteristics: [
194
+ dv.getBigUint64(offset, true),
195
+ dv.getBigUint64(offset += 8, true)
196
+ ],
197
+ dataOffset: Number(dv.getBigUint64(offset += 8, true)),
198
+ dataSize: Number(dv.getBigUint64(offset += 8, true)),
199
+ indexOffset: Number(dv.getBigUint64(offset += 8, true))
200
+ };
201
+ return header;
202
+ }
203
+ function getMultihashLength(bytes) {
204
+ import_varint.default.decode(bytes);
205
+ const codeLength = (
206
+ /** @type {number} */
207
+ import_varint.default.decode.bytes
208
+ );
209
+ const length = import_varint.default.decode(bytes.subarray(import_varint.default.decode.bytes));
210
+ const lengthLength = (
211
+ /** @type {number} */
212
+ import_varint.default.decode.bytes
213
+ );
214
+ const mhLength = codeLength + lengthLength + length;
215
+ return mhLength;
216
+ }
217
+
218
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/header-validator.js
219
+ var Kinds = {
220
+ Null: (
221
+ /** @returns {undefined|null} */
222
+ (obj) => obj === null ? obj : void 0
223
+ ),
224
+ Int: (
225
+ /** @returns {undefined|number} */
226
+ (obj) => Number.isInteger(obj) ? obj : void 0
227
+ ),
228
+ Float: (
229
+ /** @returns {undefined|number} */
230
+ (obj) => typeof obj === "number" && Number.isFinite(obj) ? obj : void 0
231
+ ),
232
+ String: (
233
+ /** @returns {undefined|string} */
234
+ (obj) => typeof obj === "string" ? obj : void 0
235
+ ),
236
+ Bool: (
237
+ /** @returns {undefined|boolean} */
238
+ (obj) => typeof obj === "boolean" ? obj : void 0
239
+ ),
240
+ Bytes: (
241
+ /** @returns {undefined|Uint8Array} */
242
+ (obj) => obj instanceof Uint8Array ? obj : void 0
243
+ ),
244
+ Link: (
245
+ /** @returns {undefined|object} */
246
+ (obj) => obj !== null && typeof obj === "object" && obj.asCID === obj ? obj : void 0
247
+ ),
248
+ List: (
249
+ /** @returns {undefined|Array<any>} */
250
+ (obj) => Array.isArray(obj) ? obj : void 0
251
+ ),
252
+ Map: (
253
+ /** @returns {undefined|object} */
254
+ (obj) => obj !== null && typeof obj === "object" && obj.asCID !== obj && !Array.isArray(obj) && !(obj instanceof Uint8Array) ? obj : void 0
255
+ )
256
+ };
257
+ var Types = {
258
+ "CarV1HeaderOrV2Pragma > roots (anon) > valueType (anon)": Kinds.Link,
259
+ "CarV1HeaderOrV2Pragma > roots (anon)": (
260
+ /** @returns {undefined|any} */
261
+ (obj) => {
262
+ if (Kinds.List(obj) === void 0) {
263
+ return void 0;
264
+ }
265
+ for (let i = 0; i < obj.length; i++) {
266
+ let v = obj[i];
267
+ v = Types["CarV1HeaderOrV2Pragma > roots (anon) > valueType (anon)"](v);
268
+ if (v === void 0) {
269
+ return void 0;
270
+ }
271
+ if (v !== obj[i]) {
272
+ const ret = obj.slice(0, i);
273
+ for (let j = i; j < obj.length; j++) {
274
+ let v2 = obj[j];
275
+ v2 = Types["CarV1HeaderOrV2Pragma > roots (anon) > valueType (anon)"](v2);
276
+ if (v2 === void 0) {
277
+ return void 0;
278
+ }
279
+ ret.push(v2);
280
+ }
281
+ return ret;
282
+ }
283
+ }
284
+ return obj;
285
+ }
286
+ ),
287
+ Int: Kinds.Int,
288
+ CarV1HeaderOrV2Pragma: (
289
+ /** @returns {undefined|any} */
290
+ (obj) => {
291
+ if (Kinds.Map(obj) === void 0) {
292
+ return void 0;
293
+ }
294
+ const entries = Object.entries(obj);
295
+ let ret = obj;
296
+ let requiredCount = 1;
297
+ for (let i = 0; i < entries.length; i++) {
298
+ const [key, value] = entries[i];
299
+ switch (key) {
300
+ case "roots":
301
+ {
302
+ const v = Types["CarV1HeaderOrV2Pragma > roots (anon)"](obj[key]);
303
+ if (v === void 0) {
304
+ return void 0;
305
+ }
306
+ if (v !== value || ret !== obj) {
307
+ if (ret === obj) {
308
+ ret = {};
309
+ for (let j = 0; j < i; j++) {
310
+ ret[entries[j][0]] = entries[j][1];
311
+ }
312
+ }
313
+ ret.roots = v;
314
+ }
315
+ }
316
+ break;
317
+ case "version":
318
+ {
319
+ requiredCount--;
320
+ const v = Types.Int(obj[key]);
321
+ if (v === void 0) {
322
+ return void 0;
323
+ }
324
+ if (v !== value || ret !== obj) {
325
+ if (ret === obj) {
326
+ ret = {};
327
+ for (let j = 0; j < i; j++) {
328
+ ret[entries[j][0]] = entries[j][1];
329
+ }
330
+ }
331
+ ret.version = v;
332
+ }
333
+ }
334
+ break;
335
+ default:
336
+ return void 0;
337
+ }
338
+ }
339
+ if (requiredCount > 0) {
340
+ return void 0;
341
+ }
342
+ return ret;
343
+ }
344
+ )
345
+ };
346
+ var Reprs = {
347
+ "CarV1HeaderOrV2Pragma > roots (anon) > valueType (anon)": Kinds.Link,
348
+ "CarV1HeaderOrV2Pragma > roots (anon)": (
349
+ /** @returns {undefined|any} */
350
+ (obj) => {
351
+ if (Kinds.List(obj) === void 0) {
352
+ return void 0;
353
+ }
354
+ for (let i = 0; i < obj.length; i++) {
355
+ let v = obj[i];
356
+ v = Reprs["CarV1HeaderOrV2Pragma > roots (anon) > valueType (anon)"](v);
357
+ if (v === void 0) {
358
+ return void 0;
359
+ }
360
+ if (v !== obj[i]) {
361
+ const ret = obj.slice(0, i);
362
+ for (let j = i; j < obj.length; j++) {
363
+ let v2 = obj[j];
364
+ v2 = Reprs["CarV1HeaderOrV2Pragma > roots (anon) > valueType (anon)"](v2);
365
+ if (v2 === void 0) {
366
+ return void 0;
367
+ }
368
+ ret.push(v2);
369
+ }
370
+ return ret;
371
+ }
372
+ }
373
+ return obj;
374
+ }
375
+ ),
376
+ Int: Kinds.Int,
377
+ CarV1HeaderOrV2Pragma: (
378
+ /** @returns {undefined|any} */
379
+ (obj) => {
380
+ if (Kinds.Map(obj) === void 0) {
381
+ return void 0;
382
+ }
383
+ const entries = Object.entries(obj);
384
+ let ret = obj;
385
+ let requiredCount = 1;
386
+ for (let i = 0; i < entries.length; i++) {
387
+ const [key, value] = entries[i];
388
+ switch (key) {
389
+ case "roots":
390
+ {
391
+ const v = Reprs["CarV1HeaderOrV2Pragma > roots (anon)"](value);
392
+ if (v === void 0) {
393
+ return void 0;
394
+ }
395
+ if (v !== value || ret !== obj) {
396
+ if (ret === obj) {
397
+ ret = {};
398
+ for (let j = 0; j < i; j++) {
399
+ ret[entries[j][0]] = entries[j][1];
400
+ }
401
+ }
402
+ ret.roots = v;
403
+ }
404
+ }
405
+ break;
406
+ case "version":
407
+ {
408
+ requiredCount--;
409
+ const v = Reprs.Int(value);
410
+ if (v === void 0) {
411
+ return void 0;
412
+ }
413
+ if (v !== value || ret !== obj) {
414
+ if (ret === obj) {
415
+ ret = {};
416
+ for (let j = 0; j < i; j++) {
417
+ ret[entries[j][0]] = entries[j][1];
418
+ }
419
+ }
420
+ ret.version = v;
421
+ }
422
+ }
423
+ break;
424
+ default:
425
+ return void 0;
426
+ }
427
+ }
428
+ if (requiredCount > 0) {
429
+ return void 0;
430
+ }
431
+ return ret;
432
+ }
433
+ )
434
+ };
435
+ var CarV1HeaderOrV2Pragma = {
436
+ toTyped: Types.CarV1HeaderOrV2Pragma,
437
+ toRepresentation: Reprs.CarV1HeaderOrV2Pragma
438
+ };
439
+
440
+ // ../../../node_modules/.pnpm/cborg@4.0.5/node_modules/cborg/lib/length.js
441
+ var cborEncoders = makeCborEncoders();
442
+
443
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/buffer-writer.js
444
+ var import_varint2 = __toESM(require_varint(), 1);
445
+ var headerPreludeTokens = [
446
+ new Token(Type.map, 2),
447
+ new Token(Type.string, "version"),
448
+ new Token(Type.uint, 1),
449
+ new Token(Type.string, "roots")
450
+ ];
451
+ var CID_TAG = new Token(Type.tag, 42);
452
+
453
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/decoder.js
454
+ async function readHeader(reader, strictVersion) {
455
+ const length = decodeVarint(await reader.upTo(8), reader);
456
+ if (length === 0) {
457
+ throw new Error("Invalid CAR header (zero length)");
458
+ }
459
+ const header = await reader.exactly(length, true);
460
+ const block = decode4(header);
461
+ if (CarV1HeaderOrV2Pragma.toTyped(block) === void 0) {
462
+ throw new Error("Invalid CAR header format");
463
+ }
464
+ if (block.version !== 1 && block.version !== 2 || strictVersion !== void 0 && block.version !== strictVersion) {
465
+ throw new Error(`Invalid CAR version: ${block.version}${strictVersion !== void 0 ? ` (expected ${strictVersion})` : ""}`);
466
+ }
467
+ if (block.version === 1) {
468
+ if (!Array.isArray(block.roots)) {
469
+ throw new Error("Invalid CAR header format");
470
+ }
471
+ return block;
472
+ }
473
+ if (block.roots !== void 0) {
474
+ throw new Error("Invalid CAR header format");
475
+ }
476
+ const v2Header = decodeV2Header(await reader.exactly(V2_HEADER_LENGTH, true));
477
+ reader.seek(v2Header.dataOffset - reader.pos);
478
+ const v1Header = await readHeader(reader, 1);
479
+ return Object.assign(v1Header, v2Header);
480
+ }
481
+ async function readCid(reader) {
482
+ const first = await reader.exactly(2, false);
483
+ if (first[0] === CIDV0_BYTES.SHA2_256 && first[1] === CIDV0_BYTES.LENGTH) {
484
+ const bytes2 = await reader.exactly(34, true);
485
+ const multihash2 = decode2(bytes2);
486
+ return CID.create(0, CIDV0_BYTES.DAG_PB, multihash2);
487
+ }
488
+ const version = decodeVarint(await reader.upTo(8), reader);
489
+ if (version !== 1) {
490
+ throw new Error(`Unexpected CID version (${version})`);
491
+ }
492
+ const codec = decodeVarint(await reader.upTo(8), reader);
493
+ const bytes = await reader.exactly(getMultihashLength(await reader.upTo(8)), true);
494
+ const multihash = decode2(bytes);
495
+ return CID.create(version, codec, multihash);
496
+ }
497
+ async function readBlockHead(reader) {
498
+ const start = reader.pos;
499
+ let length = decodeVarint(await reader.upTo(8), reader);
500
+ if (length === 0) {
501
+ throw new Error("Invalid CAR section (zero length)");
502
+ }
503
+ length += reader.pos - start;
504
+ const cid = await readCid(reader);
505
+ const blockLength = length - Number(reader.pos - start);
506
+ return { cid, length, blockLength };
507
+ }
508
+ async function readBlock(reader) {
509
+ const { cid, blockLength } = await readBlockHead(reader);
510
+ const bytes = await reader.exactly(blockLength, true);
511
+ return { bytes, cid };
512
+ }
513
+ async function readBlockIndex(reader) {
514
+ const offset = reader.pos;
515
+ const { cid, length, blockLength } = await readBlockHead(reader);
516
+ const index = { cid, length, blockLength, offset, blockOffset: reader.pos };
517
+ reader.seek(index.blockLength);
518
+ return index;
519
+ }
520
+ function createDecoder(reader) {
521
+ const headerPromise = (async () => {
522
+ const header = await readHeader(reader);
523
+ if (header.version === 2) {
524
+ const v1length = reader.pos - header.dataOffset;
525
+ reader = limitReader(reader, header.dataSize - v1length);
526
+ }
527
+ return header;
528
+ })();
529
+ return {
530
+ header: () => headerPromise,
531
+ async *blocks() {
532
+ await headerPromise;
533
+ while ((await reader.upTo(8)).length > 0) {
534
+ yield await readBlock(reader);
535
+ }
536
+ },
537
+ async *blocksIndex() {
538
+ await headerPromise;
539
+ while ((await reader.upTo(8)).length > 0) {
540
+ yield await readBlockIndex(reader);
541
+ }
542
+ }
543
+ };
544
+ }
545
+ function bytesReader(bytes) {
546
+ let pos = 0;
547
+ return {
548
+ async upTo(length) {
549
+ const out = bytes.subarray(pos, pos + Math.min(length, bytes.length - pos));
550
+ return out;
551
+ },
552
+ async exactly(length, seek = false) {
553
+ if (length > bytes.length - pos) {
554
+ throw new Error("Unexpected end of data");
555
+ }
556
+ const out = bytes.subarray(pos, pos + length);
557
+ if (seek) {
558
+ pos += length;
559
+ }
560
+ return out;
561
+ },
562
+ seek(length) {
563
+ pos += length;
564
+ },
565
+ get pos() {
566
+ return pos;
567
+ }
568
+ };
569
+ }
570
+ function chunkReader(readChunk) {
571
+ let pos = 0;
572
+ let have = 0;
573
+ let offset = 0;
574
+ let currentChunk = new Uint8Array(0);
575
+ const read = async (length) => {
576
+ have = currentChunk.length - offset;
577
+ const bufa = [currentChunk.subarray(offset)];
578
+ while (have < length) {
579
+ const chunk = await readChunk();
580
+ if (chunk == null) {
581
+ break;
582
+ }
583
+ if (have < 0) {
584
+ if (chunk.length > have) {
585
+ bufa.push(chunk.subarray(-have));
586
+ }
587
+ } else {
588
+ bufa.push(chunk);
589
+ }
590
+ have += chunk.length;
591
+ }
592
+ currentChunk = new Uint8Array(bufa.reduce((p, c) => p + c.length, 0));
593
+ let off = 0;
594
+ for (const b of bufa) {
595
+ currentChunk.set(b, off);
596
+ off += b.length;
597
+ }
598
+ offset = 0;
599
+ };
600
+ return {
601
+ async upTo(length) {
602
+ if (currentChunk.length - offset < length) {
603
+ await read(length);
604
+ }
605
+ return currentChunk.subarray(offset, offset + Math.min(currentChunk.length - offset, length));
606
+ },
607
+ async exactly(length, seek = false) {
608
+ if (currentChunk.length - offset < length) {
609
+ await read(length);
610
+ }
611
+ if (currentChunk.length - offset < length) {
612
+ throw new Error("Unexpected end of data");
613
+ }
614
+ const out = currentChunk.subarray(offset, offset + length);
615
+ if (seek) {
616
+ pos += length;
617
+ offset += length;
618
+ }
619
+ return out;
620
+ },
621
+ seek(length) {
622
+ pos += length;
623
+ offset += length;
624
+ },
625
+ get pos() {
626
+ return pos;
627
+ }
628
+ };
629
+ }
630
+ function asyncIterableReader(asyncIterable) {
631
+ const iterator = asyncIterable[Symbol.asyncIterator]();
632
+ async function readChunk() {
633
+ const next = await iterator.next();
634
+ if (next.done) {
635
+ return null;
636
+ }
637
+ return next.value;
638
+ }
639
+ return chunkReader(readChunk);
640
+ }
641
+ function limitReader(reader, byteLimit) {
642
+ let bytesRead = 0;
643
+ return {
644
+ async upTo(length) {
645
+ let bytes = await reader.upTo(length);
646
+ if (bytes.length + bytesRead > byteLimit) {
647
+ bytes = bytes.subarray(0, byteLimit - bytesRead);
648
+ }
649
+ return bytes;
650
+ },
651
+ async exactly(length, seek = false) {
652
+ const bytes = await reader.exactly(length, seek);
653
+ if (bytes.length + bytesRead > byteLimit) {
654
+ throw new Error("Unexpected end of data");
655
+ }
656
+ if (seek) {
657
+ bytesRead += length;
658
+ }
659
+ return bytes;
660
+ },
661
+ seek(length) {
662
+ bytesRead += length;
663
+ reader.seek(length);
664
+ },
665
+ get pos() {
666
+ return reader.pos;
667
+ }
668
+ };
669
+ }
670
+
671
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/indexer.js
672
+ var CarIndexer = class {
673
+ /**
674
+ * @param {number} version
675
+ * @param {CID[]} roots
676
+ * @param {AsyncGenerator<BlockIndex>} iterator
677
+ */
678
+ constructor(version, roots, iterator) {
679
+ this._version = version;
680
+ this._roots = roots;
681
+ this._iterator = iterator;
682
+ }
683
+ get version() {
684
+ return this._version;
685
+ }
686
+ /**
687
+ * Get the list of roots defined by the CAR referenced by this indexer. May be
688
+ * zero or more `CID`s.
689
+ *
690
+ * @function
691
+ * @memberof CarIndexer
692
+ * @instance
693
+ * @async
694
+ * @returns {Promise<CID[]>}
695
+ */
696
+ async getRoots() {
697
+ return this._roots;
698
+ }
699
+ /**
700
+ * @returns {AsyncIterator<BlockIndex>}
701
+ */
702
+ [Symbol.asyncIterator]() {
703
+ return this._iterator;
704
+ }
705
+ /**
706
+ * Instantiate a {@link CarIndexer} from a `Uint8Array` blob. Only the header
707
+ * is decoded initially, the remainder is processed and emitted via the
708
+ * iterator as it is consumed.
709
+ *
710
+ * @async
711
+ * @static
712
+ * @memberof CarIndexer
713
+ * @param {Uint8Array} bytes
714
+ * @returns {Promise<CarIndexer>}
715
+ */
716
+ static async fromBytes(bytes) {
717
+ if (!(bytes instanceof Uint8Array)) {
718
+ throw new TypeError("fromBytes() requires a Uint8Array");
719
+ }
720
+ return decodeIndexerComplete(bytesReader(bytes));
721
+ }
722
+ /**
723
+ * Instantiate a {@link CarIndexer} from a `AsyncIterable<Uint8Array>`,
724
+ * such as a [modern Node.js stream](https://nodejs.org/api/stream.html#stream_streams_compatibility_with_async_generators_and_async_iterators).
725
+ * is decoded initially, the remainder is processed and emitted via the
726
+ * iterator as it is consumed.
727
+ *
728
+ * @async
729
+ * @static
730
+ * @memberof CarIndexer
731
+ * @param {AsyncIterable<Uint8Array>} asyncIterable
732
+ * @returns {Promise<CarIndexer>}
733
+ */
734
+ static async fromIterable(asyncIterable) {
735
+ if (!asyncIterable || !(typeof asyncIterable[Symbol.asyncIterator] === "function")) {
736
+ throw new TypeError("fromIterable() requires an async iterable");
737
+ }
738
+ return decodeIndexerComplete(asyncIterableReader(asyncIterable));
739
+ }
740
+ };
741
+ async function decodeIndexerComplete(reader) {
742
+ const decoder = createDecoder(reader);
743
+ const { version, roots } = await decoder.header();
744
+ return new CarIndexer(version, roots, decoder.blocksIndex());
745
+ }
746
+
747
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/reader-browser.js
748
+ var CarReader = class {
749
+ /**
750
+ * @constructs CarReader
751
+ * @param {CarHeader|CarV2Header} header
752
+ * @param {Block[]} blocks
753
+ */
754
+ constructor(header, blocks) {
755
+ this._header = header;
756
+ this._blocks = blocks;
757
+ this._keys = blocks.map((b) => b.cid.toString());
758
+ }
759
+ /**
760
+ * @property
761
+ * @memberof CarReader
762
+ * @instance
763
+ */
764
+ get version() {
765
+ return this._header.version;
766
+ }
767
+ /**
768
+ * Get the list of roots defined by the CAR referenced by this reader. May be
769
+ * zero or more `CID`s.
770
+ *
771
+ * @function
772
+ * @memberof CarReader
773
+ * @instance
774
+ * @async
775
+ * @returns {Promise<CID[]>}
776
+ */
777
+ async getRoots() {
778
+ return this._header.roots;
779
+ }
780
+ /**
781
+ * Check whether a given `CID` exists within the CAR referenced by this
782
+ * reader.
783
+ *
784
+ * @function
785
+ * @memberof CarReader
786
+ * @instance
787
+ * @async
788
+ * @param {CID} key
789
+ * @returns {Promise<boolean>}
790
+ */
791
+ async has(key) {
792
+ return this._keys.indexOf(key.toString()) > -1;
793
+ }
794
+ /**
795
+ * Fetch a `Block` (a `{ cid:CID, bytes:Uint8Array }` pair) from the CAR
796
+ * referenced by this reader matching the provided `CID`. In the case where
797
+ * the provided `CID` doesn't exist within the CAR, `undefined` will be
798
+ * returned.
799
+ *
800
+ * @function
801
+ * @memberof CarReader
802
+ * @instance
803
+ * @async
804
+ * @param {CID} key
805
+ * @returns {Promise<Block | undefined>}
806
+ */
807
+ async get(key) {
808
+ const index = this._keys.indexOf(key.toString());
809
+ return index > -1 ? this._blocks[index] : void 0;
810
+ }
811
+ /**
812
+ * Returns a `BlockIterator` (`AsyncIterable<Block>`) that iterates over all
813
+ * of the `Block`s (`{ cid:CID, bytes:Uint8Array }` pairs) contained within
814
+ * the CAR referenced by this reader.
815
+ *
816
+ * @function
817
+ * @memberof CarReader
818
+ * @instance
819
+ * @async
820
+ * @generator
821
+ * @returns {AsyncGenerator<Block>}
822
+ */
823
+ async *blocks() {
824
+ for (const block of this._blocks) {
825
+ yield block;
826
+ }
827
+ }
828
+ /**
829
+ * Returns a `CIDIterator` (`AsyncIterable<CID>`) that iterates over all of
830
+ * the `CID`s contained within the CAR referenced by this reader.
831
+ *
832
+ * @function
833
+ * @memberof CarReader
834
+ * @instance
835
+ * @async
836
+ * @generator
837
+ * @returns {AsyncGenerator<CID>}
838
+ */
839
+ async *cids() {
840
+ for (const block of this._blocks) {
841
+ yield block.cid;
842
+ }
843
+ }
844
+ /**
845
+ * Instantiate a {@link CarReader} from a `Uint8Array` blob. This performs a
846
+ * decode fully in memory and maintains the decoded state in memory for full
847
+ * access to the data via the `CarReader` API.
848
+ *
849
+ * @async
850
+ * @static
851
+ * @memberof CarReader
852
+ * @param {Uint8Array} bytes
853
+ * @returns {Promise<CarReader>}
854
+ */
855
+ static async fromBytes(bytes) {
856
+ if (!(bytes instanceof Uint8Array)) {
857
+ throw new TypeError("fromBytes() requires a Uint8Array");
858
+ }
859
+ return decodeReaderComplete(bytesReader(bytes));
860
+ }
861
+ /**
862
+ * Instantiate a {@link CarReader} from a `AsyncIterable<Uint8Array>`, such as
863
+ * a [modern Node.js stream](https://nodejs.org/api/stream.html#stream_streams_compatibility_with_async_generators_and_async_iterators).
864
+ * This performs a decode fully in memory and maintains the decoded state in
865
+ * memory for full access to the data via the `CarReader` API.
866
+ *
867
+ * Care should be taken for large archives; this API may not be appropriate
868
+ * where memory is a concern or the archive is potentially larger than the
869
+ * amount of memory that the runtime can handle.
870
+ *
871
+ * @async
872
+ * @static
873
+ * @memberof CarReader
874
+ * @param {AsyncIterable<Uint8Array>} asyncIterable
875
+ * @returns {Promise<CarReader>}
876
+ */
877
+ static async fromIterable(asyncIterable) {
878
+ if (!asyncIterable || !(typeof asyncIterable[Symbol.asyncIterator] === "function")) {
879
+ throw new TypeError("fromIterable() requires an async iterable");
880
+ }
881
+ return decodeReaderComplete(asyncIterableReader(asyncIterable));
882
+ }
883
+ };
884
+ async function decodeReaderComplete(reader) {
885
+ const decoder = createDecoder(reader);
886
+ const header = await decoder.header();
887
+ const blocks = [];
888
+ for await (const block of decoder.blocks()) {
889
+ blocks.push(block);
890
+ }
891
+ return new CarReader(header, blocks);
892
+ }
893
+
894
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/iterator.js
895
+ var CarIteratorBase = class {
896
+ /**
897
+ * @param {number} version
898
+ * @param {CID[]} roots
899
+ * @param {AsyncIterable<Block>|void} iterable
900
+ */
901
+ constructor(version, roots, iterable) {
902
+ this._version = version;
903
+ this._roots = roots;
904
+ this._iterable = iterable;
905
+ this._decoded = false;
906
+ }
907
+ get version() {
908
+ return this._version;
909
+ }
910
+ /**
911
+ * @returns {Promise<CID[]>}
912
+ */
913
+ async getRoots() {
914
+ return this._roots;
915
+ }
916
+ };
917
+ var CarBlockIterator = class _CarBlockIterator extends CarIteratorBase {
918
+ // inherited method
919
+ /**
920
+ * Get the list of roots defined by the CAR referenced by this iterator. May be
921
+ * zero or more `CID`s.
922
+ *
923
+ * @function getRoots
924
+ * @memberof CarBlockIterator
925
+ * @instance
926
+ * @async
927
+ * @returns {Promise<CID[]>}
928
+ */
929
+ /**
930
+ * @returns {AsyncIterator<Block>}
931
+ */
932
+ [Symbol.asyncIterator]() {
933
+ if (this._decoded) {
934
+ throw new Error("Cannot decode more than once");
935
+ }
936
+ if (!this._iterable) {
937
+ throw new Error("Block iterable not found");
938
+ }
939
+ this._decoded = true;
940
+ return this._iterable[Symbol.asyncIterator]();
941
+ }
942
+ /**
943
+ * Instantiate a {@link CarBlockIterator} from a `Uint8Array` blob. Rather
944
+ * than decoding the entire byte array prior to returning the iterator, as in
945
+ * {@link CarReader.fromBytes}, only the header is decoded and the remainder
946
+ * of the CAR is parsed as the `Block`s as yielded.
947
+ *
948
+ * @async
949
+ * @static
950
+ * @memberof CarBlockIterator
951
+ * @param {Uint8Array} bytes
952
+ * @returns {Promise<CarBlockIterator>}
953
+ */
954
+ static async fromBytes(bytes) {
955
+ const { version, roots, iterator } = await fromBytes2(bytes);
956
+ return new _CarBlockIterator(version, roots, iterator);
957
+ }
958
+ /**
959
+ * Instantiate a {@link CarBlockIterator} from a `AsyncIterable<Uint8Array>`,
960
+ * such as a [modern Node.js stream](https://nodejs.org/api/stream.html#stream_streams_compatibility_with_async_generators_and_async_iterators).
961
+ * Rather than decoding the entire byte array prior to returning the iterator,
962
+ * as in {@link CarReader.fromIterable}, only the header is decoded and the
963
+ * remainder of the CAR is parsed as the `Block`s as yielded.
964
+ *
965
+ * @async
966
+ * @static
967
+ * @param {AsyncIterable<Uint8Array>} asyncIterable
968
+ * @returns {Promise<CarBlockIterator>}
969
+ */
970
+ static async fromIterable(asyncIterable) {
971
+ const { version, roots, iterator } = await fromIterable(asyncIterable);
972
+ return new _CarBlockIterator(version, roots, iterator);
973
+ }
974
+ };
975
+ var CarCIDIterator = class _CarCIDIterator extends CarIteratorBase {
976
+ // inherited method
977
+ /**
978
+ * Get the list of roots defined by the CAR referenced by this iterator. May be
979
+ * zero or more `CID`s.
980
+ *
981
+ * @function getRoots
982
+ * @memberof CarCIDIterator
983
+ * @instance
984
+ * @async
985
+ * @returns {Promise<CID[]>}
986
+ */
987
+ /**
988
+ * @returns {AsyncIterator<CID>}
989
+ */
990
+ [Symbol.asyncIterator]() {
991
+ if (this._decoded) {
992
+ throw new Error("Cannot decode more than once");
993
+ }
994
+ if (!this._iterable) {
995
+ throw new Error("Block iterable not found");
996
+ }
997
+ this._decoded = true;
998
+ const iterable = this._iterable[Symbol.asyncIterator]();
999
+ return {
1000
+ async next() {
1001
+ const next = await iterable.next();
1002
+ if (next.done) {
1003
+ return next;
1004
+ }
1005
+ return { done: false, value: next.value.cid };
1006
+ }
1007
+ };
1008
+ }
1009
+ /**
1010
+ * Instantiate a {@link CarCIDIterator} from a `Uint8Array` blob. Rather
1011
+ * than decoding the entire byte array prior to returning the iterator, as in
1012
+ * {@link CarReader.fromBytes}, only the header is decoded and the remainder
1013
+ * of the CAR is parsed as the `CID`s as yielded.
1014
+ *
1015
+ * @async
1016
+ * @static
1017
+ * @memberof CarCIDIterator
1018
+ * @param {Uint8Array} bytes
1019
+ * @returns {Promise<CarCIDIterator>}
1020
+ */
1021
+ static async fromBytes(bytes) {
1022
+ const { version, roots, iterator } = await fromBytes2(bytes);
1023
+ return new _CarCIDIterator(version, roots, iterator);
1024
+ }
1025
+ /**
1026
+ * Instantiate a {@link CarCIDIterator} from a `AsyncIterable<Uint8Array>`,
1027
+ * such as a [modern Node.js stream](https://nodejs.org/api/stream.html#stream_streams_compatibility_with_async_generators_and_async_iterators).
1028
+ * Rather than decoding the entire byte array prior to returning the iterator,
1029
+ * as in {@link CarReader.fromIterable}, only the header is decoded and the
1030
+ * remainder of the CAR is parsed as the `CID`s as yielded.
1031
+ *
1032
+ * @async
1033
+ * @static
1034
+ * @memberof CarCIDIterator
1035
+ * @param {AsyncIterable<Uint8Array>} asyncIterable
1036
+ * @returns {Promise<CarCIDIterator>}
1037
+ */
1038
+ static async fromIterable(asyncIterable) {
1039
+ const { version, roots, iterator } = await fromIterable(asyncIterable);
1040
+ return new _CarCIDIterator(version, roots, iterator);
1041
+ }
1042
+ };
1043
+ async function fromBytes2(bytes) {
1044
+ if (!(bytes instanceof Uint8Array)) {
1045
+ throw new TypeError("fromBytes() requires a Uint8Array");
1046
+ }
1047
+ return decodeIterator(bytesReader(bytes));
1048
+ }
1049
+ async function fromIterable(asyncIterable) {
1050
+ if (!asyncIterable || !(typeof asyncIterable[Symbol.asyncIterator] === "function")) {
1051
+ throw new TypeError("fromIterable() requires an async iterable");
1052
+ }
1053
+ return decodeIterator(asyncIterableReader(asyncIterable));
1054
+ }
1055
+ async function decodeIterator(reader) {
1056
+ const decoder = createDecoder(reader);
1057
+ const { version, roots } = await decoder.header();
1058
+ return { version, roots, iterator: decoder.blocks() };
1059
+ }
1060
+
1061
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/encoder.js
1062
+ var import_varint3 = __toESM(require_varint(), 1);
1063
+ function createHeader(roots) {
1064
+ const headerBytes = encode2({ version: 1, roots });
1065
+ const varintBytes = import_varint3.default.encode(headerBytes.length);
1066
+ const header = new Uint8Array(varintBytes.length + headerBytes.length);
1067
+ header.set(varintBytes, 0);
1068
+ header.set(headerBytes, varintBytes.length);
1069
+ return header;
1070
+ }
1071
+ function createEncoder(writer) {
1072
+ return {
1073
+ /**
1074
+ * @param {CID[]} roots
1075
+ * @returns {Promise<void>}
1076
+ */
1077
+ async setRoots(roots) {
1078
+ const bytes = createHeader(roots);
1079
+ await writer.write(bytes);
1080
+ },
1081
+ /**
1082
+ * @param {Block} block
1083
+ * @returns {Promise<void>}
1084
+ */
1085
+ async writeBlock(block) {
1086
+ const { cid, bytes } = block;
1087
+ await writer.write(new Uint8Array(import_varint3.default.encode(cid.bytes.length + bytes.length)));
1088
+ await writer.write(cid.bytes);
1089
+ if (bytes.length) {
1090
+ await writer.write(bytes);
1091
+ }
1092
+ },
1093
+ /**
1094
+ * @returns {Promise<void>}
1095
+ */
1096
+ async close() {
1097
+ await writer.end();
1098
+ }
1099
+ };
1100
+ }
1101
+
1102
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/iterator-channel.js
1103
+ function noop() {
1104
+ }
1105
+ function create() {
1106
+ const chunkQueue = [];
1107
+ let drainer = null;
1108
+ let drainerResolver = noop;
1109
+ let ended = false;
1110
+ let outWait = null;
1111
+ let outWaitResolver = noop;
1112
+ const makeDrainer = () => {
1113
+ if (!drainer) {
1114
+ drainer = new Promise((resolve) => {
1115
+ drainerResolver = () => {
1116
+ drainer = null;
1117
+ drainerResolver = noop;
1118
+ resolve();
1119
+ };
1120
+ });
1121
+ }
1122
+ return drainer;
1123
+ };
1124
+ const writer = {
1125
+ /**
1126
+ * @param {T} chunk
1127
+ * @returns {Promise<void>}
1128
+ */
1129
+ write(chunk) {
1130
+ chunkQueue.push(chunk);
1131
+ const drainer2 = makeDrainer();
1132
+ outWaitResolver();
1133
+ return drainer2;
1134
+ },
1135
+ async end() {
1136
+ ended = true;
1137
+ const drainer2 = makeDrainer();
1138
+ outWaitResolver();
1139
+ await drainer2;
1140
+ }
1141
+ };
1142
+ const iterator = {
1143
+ /** @returns {Promise<IteratorResult<T>>} */
1144
+ async next() {
1145
+ const chunk = chunkQueue.shift();
1146
+ if (chunk) {
1147
+ if (chunkQueue.length === 0) {
1148
+ drainerResolver();
1149
+ }
1150
+ return { done: false, value: chunk };
1151
+ }
1152
+ if (ended) {
1153
+ drainerResolver();
1154
+ return { done: true, value: void 0 };
1155
+ }
1156
+ if (!outWait) {
1157
+ outWait = new Promise((resolve) => {
1158
+ outWaitResolver = () => {
1159
+ outWait = null;
1160
+ outWaitResolver = noop;
1161
+ return resolve(iterator.next());
1162
+ };
1163
+ });
1164
+ }
1165
+ return outWait;
1166
+ }
1167
+ };
1168
+ return { writer, iterator };
1169
+ }
1170
+
1171
+ // ../../../node_modules/.pnpm/@ipld+car@5.2.4/node_modules/@ipld/car/src/writer-browser.js
1172
+ var CarWriter = class _CarWriter {
1173
+ /**
1174
+ * @param {CID[]} roots
1175
+ * @param {CarEncoder} encoder
1176
+ */
1177
+ constructor(roots, encoder) {
1178
+ this._encoder = encoder;
1179
+ this._mutex = encoder.setRoots(roots);
1180
+ this._ended = false;
1181
+ }
1182
+ /**
1183
+ * Write a `Block` (a `{ cid:CID, bytes:Uint8Array }` pair) to the archive.
1184
+ *
1185
+ * @function
1186
+ * @memberof CarWriter
1187
+ * @instance
1188
+ * @async
1189
+ * @param {Block} block - A `{ cid:CID, bytes:Uint8Array }` pair.
1190
+ * @returns {Promise<void>} The returned promise will only resolve once the
1191
+ * bytes this block generates are written to the `out` iterable.
1192
+ */
1193
+ async put(block) {
1194
+ if (!(block.bytes instanceof Uint8Array) || !block.cid) {
1195
+ throw new TypeError("Can only write {cid, bytes} objects");
1196
+ }
1197
+ if (this._ended) {
1198
+ throw new Error("Already closed");
1199
+ }
1200
+ const cid = CID.asCID(block.cid);
1201
+ if (!cid) {
1202
+ throw new TypeError("Can only write {cid, bytes} objects");
1203
+ }
1204
+ this._mutex = this._mutex.then(() => this._encoder.writeBlock({ cid, bytes: block.bytes }));
1205
+ return this._mutex;
1206
+ }
1207
+ /**
1208
+ * Finalise the CAR archive and signal that the `out` iterable should end once
1209
+ * any remaining bytes are written.
1210
+ *
1211
+ * @function
1212
+ * @memberof CarWriter
1213
+ * @instance
1214
+ * @async
1215
+ * @returns {Promise<void>}
1216
+ */
1217
+ async close() {
1218
+ if (this._ended) {
1219
+ throw new Error("Already closed");
1220
+ }
1221
+ await this._mutex;
1222
+ this._ended = true;
1223
+ return this._encoder.close();
1224
+ }
1225
+ /**
1226
+ * Create a new CAR writer "channel" which consists of a
1227
+ * `{ writer:CarWriter, out:AsyncIterable<Uint8Array> }` pair.
1228
+ *
1229
+ * @async
1230
+ * @static
1231
+ * @memberof CarWriter
1232
+ * @param {CID[] | CID | void} roots
1233
+ * @returns {WriterChannel} The channel takes the form of
1234
+ * `{ writer:CarWriter, out:AsyncIterable<Uint8Array> }`.
1235
+ */
1236
+ static create(roots) {
1237
+ roots = toRoots(roots);
1238
+ const { encoder, iterator } = encodeWriter();
1239
+ const writer = new _CarWriter(roots, encoder);
1240
+ const out = new CarWriterOut(iterator);
1241
+ return { writer, out };
1242
+ }
1243
+ /**
1244
+ * Create a new CAR appender "channel" which consists of a
1245
+ * `{ writer:CarWriter, out:AsyncIterable<Uint8Array> }` pair.
1246
+ * This appender does not consider roots and does not produce a CAR header.
1247
+ * It is designed to append blocks to an _existing_ CAR archive. It is
1248
+ * expected that `out` will be concatenated onto the end of an existing
1249
+ * archive that already has a properly formatted header.
1250
+ *
1251
+ * @async
1252
+ * @static
1253
+ * @memberof CarWriter
1254
+ * @returns {WriterChannel} The channel takes the form of
1255
+ * `{ writer:CarWriter, out:AsyncIterable<Uint8Array> }`.
1256
+ */
1257
+ static createAppender() {
1258
+ const { encoder, iterator } = encodeWriter();
1259
+ encoder.setRoots = () => Promise.resolve();
1260
+ const writer = new _CarWriter([], encoder);
1261
+ const out = new CarWriterOut(iterator);
1262
+ return { writer, out };
1263
+ }
1264
+ /**
1265
+ * Update the list of roots in the header of an existing CAR as represented
1266
+ * in a Uint8Array.
1267
+ *
1268
+ * This operation is an _overwrite_, the total length of the CAR will not be
1269
+ * modified. A rejection will occur if the new header will not be the same
1270
+ * length as the existing header, in which case the CAR will not be modified.
1271
+ * It is the responsibility of the user to ensure that the roots being
1272
+ * replaced encode as the same length as the new roots.
1273
+ *
1274
+ * The byte array passed in an argument will be modified and also returned
1275
+ * upon successful modification.
1276
+ *
1277
+ * @async
1278
+ * @static
1279
+ * @memberof CarWriter
1280
+ * @param {Uint8Array} bytes
1281
+ * @param {CID[]} roots - A new list of roots to replace the existing list in
1282
+ * the CAR header. The new header must take up the same number of bytes as the
1283
+ * existing header, so the roots should collectively be the same byte length
1284
+ * as the existing roots.
1285
+ * @returns {Promise<Uint8Array>}
1286
+ */
1287
+ static async updateRootsInBytes(bytes, roots) {
1288
+ const reader = bytesReader(bytes);
1289
+ await readHeader(reader);
1290
+ const newHeader = createHeader(roots);
1291
+ if (Number(reader.pos) !== newHeader.length) {
1292
+ throw new Error(`updateRoots() can only overwrite a header of the same length (old header is ${reader.pos} bytes, new header is ${newHeader.length} bytes)`);
1293
+ }
1294
+ bytes.set(newHeader, 0);
1295
+ return bytes;
1296
+ }
1297
+ };
1298
+ var CarWriterOut = class {
1299
+ /**
1300
+ * @param {AsyncIterator<Uint8Array>} iterator
1301
+ */
1302
+ constructor(iterator) {
1303
+ this._iterator = iterator;
1304
+ }
1305
+ [Symbol.asyncIterator]() {
1306
+ if (this._iterating) {
1307
+ throw new Error("Multiple iterator not supported");
1308
+ }
1309
+ this._iterating = true;
1310
+ return this._iterator;
1311
+ }
1312
+ };
1313
+ function encodeWriter() {
1314
+ const iw = create();
1315
+ const { writer, iterator } = iw;
1316
+ const encoder = createEncoder(writer);
1317
+ return { encoder, iterator };
1318
+ }
1319
+ function toRoots(roots) {
1320
+ if (roots === void 0) {
1321
+ return [];
1322
+ }
1323
+ if (!Array.isArray(roots)) {
1324
+ const cid = CID.asCID(roots);
1325
+ if (!cid) {
1326
+ throw new TypeError("roots must be a single CID or an array of CIDs");
1327
+ }
1328
+ return [cid];
1329
+ }
1330
+ const _roots = [];
1331
+ for (const root of roots) {
1332
+ const _root = CID.asCID(root);
1333
+ if (!_root) {
1334
+ throw new TypeError("roots must be a single CID or an array of CIDs");
1335
+ }
1336
+ _roots.push(_root);
1337
+ }
1338
+ return _roots;
1339
+ }
1340
+
1341
+ // src/ipfs/car.ts
1342
+ var { WARN, LOG, DEBUG, VERBOSE, ERROR } = Logger.setup(Logger.INFO);
1343
+ async function decodeCarToApplogs(car) {
1344
+ const { roots, blocks } = await decodeCar(car);
1345
+ if (!roots || !blocks) {
1346
+ throw ERROR("decodeCar is funky", { roots, blocks });
1347
+ }
1348
+ if (roots.length !== 1) {
1349
+ WARN("Unexpected roots count:", roots);
1350
+ }
1351
+ const root = blocks.get(roots[0]);
1352
+ let applogArr;
1353
+ DEBUG(`root:`, root, { blocks });
1354
+ if (root.info) {
1355
+ applogArr = blocks.get(root.applogs.toString()).logs;
1356
+ DEBUG(`new format - infoLogs`, blocks.get(root.info.toString()));
1357
+ } else {
1358
+ applogArr = root.applogs;
1359
+ }
1360
+ let applogs;
1361
+ applogs = applogArr.map((cidOrLink) => {
1362
+ const cid = cidOrLink.toV1 ? cidOrLink.toV1().toString() : cidOrLink["/"];
1363
+ const applog = blocks.get(cid);
1364
+ if (!applog) {
1365
+ ERROR(`Could not find applog CID in Car file:`, cid, { root, blocks });
1366
+ throw new Error(`Could not find applog CID in Car file: ${cid.toString()}`);
1367
+ }
1368
+ if (applog.pv instanceof CID)
1369
+ applog.pv = applog.pv.toV1().toString();
1370
+ return {
1371
+ ...applog,
1372
+ cid
1373
+ };
1374
+ });
1375
+ DEBUG("Decoded car logs:", root, { roots, blocks, applogs });
1376
+ return { applogs };
1377
+ }
1378
+ async function decodeCar(car) {
1379
+ const roots = (await car.getRoots()).map((c) => c.toV1().toString());
1380
+ const blocks = /* @__PURE__ */ new Map();
1381
+ for await (const { cid, bytes } of car.blocks()) {
1382
+ blocks.set(cid.toV1().toString(), decode3(bytes));
1383
+ }
1384
+ return { roots, blocks };
1385
+ }
1386
+ async function makeCarOut(roots, blocks) {
1387
+ const { writer, out } = CarWriter.create(Array.isArray(roots) ? roots : [roots]);
1388
+ VERBOSE(`Writing ${blocks.length} blocks to CAR`, { roots, blocks });
1389
+ blocks.forEach((b) => writer.put(b));
1390
+ writer.close();
1391
+ return out;
1392
+ }
1393
+ async function makeCarBlob(roots, blocks) {
1394
+ const carOut = await makeCarOut(roots, blocks);
1395
+ const chunks = [];
1396
+ for await (const chunk of carOut) {
1397
+ chunks.push(chunk);
1398
+ }
1399
+ const blob = new Blob(chunks);
1400
+ return blob;
1401
+ }
1402
+ async function carFromBlob(blob) {
1403
+ return CarReader.fromBytes(new Uint8Array(await blob.arrayBuffer()));
1404
+ }
1405
+ function streamReaderToIterable(bodyReader) {
1406
+ return async function* () {
1407
+ while (true) {
1408
+ const { done, value } = await bodyReader.read();
1409
+ if (done) {
1410
+ break;
1411
+ }
1412
+ yield value;
1413
+ }
1414
+ }();
1415
+ }
1416
+
1417
+ export {
1418
+ decodeCarToApplogs,
1419
+ decodeCar,
1420
+ makeCarOut,
1421
+ makeCarBlob,
1422
+ carFromBlob,
1423
+ streamReaderToIterable
1424
+ };
1425
+ //# sourceMappingURL=chunk-ORHJN4G2.min.js.map