@pkcprotocol/pkc-js 0.0.61 → 0.0.63
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.
- package/dist/browser/clients/base-client-manager.js +1 -1
- package/dist/browser/clients/base-client-manager.js.map +1 -1
- package/dist/browser/community/community-client-manager.js +1 -1
- package/dist/browser/community/community-client-manager.js.map +1 -1
- package/dist/browser/generated-version.d.ts +1 -1
- package/dist/browser/generated-version.js +1 -1
- package/dist/browser/helia/helia-for-pkc.js +22 -0
- package/dist/browser/helia/helia-for-pkc.js.map +1 -1
- package/dist/browser/helia/util.js +10 -1
- package/dist/browser/helia/util.js.map +1 -1
- package/dist/browser/pkc/pkc.js +12 -6
- package/dist/browser/pkc/pkc.js.map +1 -1
- package/dist/browser/publications/comment/comment.js +1 -1
- package/dist/browser/publications/comment/comment.js.map +1 -1
- package/dist/browser/rpc/src/index.js +42 -3
- package/dist/browser/rpc/src/index.js.map +1 -1
- package/dist/browser/runtime/browser/util.d.ts +1 -1
- package/dist/browser/runtime/browser/util.js +4 -2
- package/dist/browser/runtime/browser/util.js.map +1 -1
- package/dist/browser/runtime/node/test/mock-http-router.d.ts +15 -0
- package/dist/browser/runtime/node/test/mock-http-router.js +80 -2
- package/dist/browser/runtime/node/test/mock-http-router.js.map +1 -1
- package/dist/browser/runtime/node/util.d.ts +1 -1
- package/dist/browser/runtime/node/util.js +4 -2
- package/dist/browser/runtime/node/util.js.map +1 -1
- package/dist/browser/schema/schema.js +1 -1
- package/dist/browser/schema/schema.js.map +1 -1
- package/dist/browser/types.d.ts +1 -1
- package/dist/browser/util.d.ts +2 -2
- package/dist/browser/util.js +6 -5
- package/dist/browser/util.js.map +1 -1
- package/dist/bundled/bundle-manifest.json +378 -319
- package/dist/bundled/chunks/chunk-2rV9d50f.js +35 -0
- package/dist/bundled/chunks/{helia-for-pkc-De6c8bno.js → helia-for-pkc-C6vMsrJ-.js} +15 -6
- package/dist/bundled/chunks/{local-community-Cjt9uNmf.js → local-community-BQmiwsV4.js} +4 -3
- package/dist/bundled/chunks/{local-community-BmuSx3YO.js → local-community-BoIoFZHk.js} +1 -1
- package/dist/bundled/chunks/{node-ChsjNuK-.js → node-G40jxRL2.js} +15 -11
- package/dist/bundled/chunks/{rpc-local-community-CShi3R2S.js → rpc-local-community-D4-NEPJb.js} +23 -20
- package/dist/bundled/chunks/{schema-9inbPhRV.js → schema-SPGmy_14.js} +12670 -27579
- package/dist/bundled/chunks/src-0J9-Tmgd.js +4147 -0
- package/dist/bundled/chunks/src-4u0f92eE.js +9899 -0
- package/dist/bundled/chunks/src-BOzgM7Fp.js +800 -0
- package/dist/bundled/chunks/src-CYIMjhjK.js +48 -0
- package/dist/bundled/chunks/{util-CTdPyu-3.js → util-CJ6nXIW9.js} +7 -2
- package/dist/bundled/index.js +1 -1
- package/dist/bundled/rpc/src/index.js +31 -8
- package/dist/node/clients/base-client-manager.js +1 -1
- package/dist/node/clients/base-client-manager.js.map +1 -1
- package/dist/node/community/community-client-manager.js +1 -1
- package/dist/node/community/community-client-manager.js.map +1 -1
- package/dist/node/generated-version.d.ts +1 -1
- package/dist/node/generated-version.js +1 -1
- package/dist/node/helia/helia-for-pkc.js +22 -0
- package/dist/node/helia/helia-for-pkc.js.map +1 -1
- package/dist/node/helia/util.js +10 -1
- package/dist/node/helia/util.js.map +1 -1
- package/dist/node/pkc/pkc.js +12 -6
- package/dist/node/pkc/pkc.js.map +1 -1
- package/dist/node/publications/comment/comment.js +1 -1
- package/dist/node/publications/comment/comment.js.map +1 -1
- package/dist/node/rpc/src/index.js +42 -3
- package/dist/node/rpc/src/index.js.map +1 -1
- package/dist/node/runtime/browser/util.d.ts +1 -1
- package/dist/node/runtime/browser/util.js +4 -2
- package/dist/node/runtime/browser/util.js.map +1 -1
- package/dist/node/runtime/node/test/mock-http-router.d.ts +15 -0
- package/dist/node/runtime/node/test/mock-http-router.js +80 -2
- package/dist/node/runtime/node/test/mock-http-router.js.map +1 -1
- package/dist/node/runtime/node/util.d.ts +1 -1
- package/dist/node/runtime/node/util.js +4 -2
- package/dist/node/runtime/node/util.js.map +1 -1
- package/dist/node/schema/schema.js +1 -1
- package/dist/node/schema/schema.js.map +1 -1
- package/dist/node/types.d.ts +1 -1
- package/dist/node/util.d.ts +2 -2
- package/dist/node/util.js +6 -5
- package/dist/node/util.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,4147 @@
|
|
|
1
|
+
import { i as __toESM, t as __commonJSMin } from "./chunk-2rV9d50f.js";
|
|
2
|
+
import { i as first, n as src_exports, r as prepare, t as encode } from "./src-BOzgM7Fp.js";
|
|
3
|
+
import { CID } from "multiformats/cid";
|
|
4
|
+
import { fromString } from "uint8arrays/from-string";
|
|
5
|
+
import { concat } from "uint8arrays/concat";
|
|
6
|
+
import { alloc, allocUnsafe } from "uint8arrays/alloc";
|
|
7
|
+
import { equals } from "uint8arrays/equals";
|
|
8
|
+
import { withArrayBuffer } from "uint8arrays/with-array-buffer";
|
|
9
|
+
import * as raw from "multiformats/codecs/raw";
|
|
10
|
+
import { sha256 } from "multiformats/hashes/sha2";
|
|
11
|
+
import { murmur3128 } from "@multiformats/murmur3";
|
|
12
|
+
import { BlackHoleBlockstore } from "blockstore-core/black-hole";
|
|
13
|
+
//#region node_modules/it-batch/dist/src/index.js
|
|
14
|
+
/**
|
|
15
|
+
* @packageDocumentation
|
|
16
|
+
*
|
|
17
|
+
* The final batch may be smaller than the max.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
*
|
|
21
|
+
* ```javascript
|
|
22
|
+
* import batch from 'it-batch'
|
|
23
|
+
* import all from 'it-all'
|
|
24
|
+
*
|
|
25
|
+
* // This can also be an iterator, generator, etc
|
|
26
|
+
* const values = [0, 1, 2, 3, 4]
|
|
27
|
+
* const batchSize = 2
|
|
28
|
+
*
|
|
29
|
+
* const result = all(batch(values, batchSize))
|
|
30
|
+
*
|
|
31
|
+
* console.info(result) // [0, 1], [2, 3], [4]
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* Async sources must be awaited:
|
|
35
|
+
*
|
|
36
|
+
* ```javascript
|
|
37
|
+
* import batch from 'it-batch'
|
|
38
|
+
* import all from 'it-all'
|
|
39
|
+
*
|
|
40
|
+
* const values = async function * () {
|
|
41
|
+
* yield * [0, 1, 2, 3, 4]
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* const batchSize = 2
|
|
45
|
+
* const result = await all(batch(values(), batchSize))
|
|
46
|
+
*
|
|
47
|
+
* console.info(result) // [0, 1], [2, 3], [4]
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
function isAsyncIterable$1(thing) {
|
|
51
|
+
return thing[Symbol.asyncIterator] != null;
|
|
52
|
+
}
|
|
53
|
+
function batch(source, size = 1) {
|
|
54
|
+
size = Number(size);
|
|
55
|
+
if (isAsyncIterable$1(source)) return async function* () {
|
|
56
|
+
let things = [];
|
|
57
|
+
if (size < 1) size = 1;
|
|
58
|
+
if (size !== Math.round(size)) throw new Error("Batch size must be an integer");
|
|
59
|
+
for await (const thing of source) {
|
|
60
|
+
things.push(thing);
|
|
61
|
+
while (things.length >= size) {
|
|
62
|
+
yield things.slice(0, size);
|
|
63
|
+
things = things.slice(size);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
while (things.length > 0) {
|
|
67
|
+
yield things.slice(0, size);
|
|
68
|
+
things = things.slice(size);
|
|
69
|
+
}
|
|
70
|
+
}();
|
|
71
|
+
return function* () {
|
|
72
|
+
let things = [];
|
|
73
|
+
if (size < 1) size = 1;
|
|
74
|
+
if (size !== Math.round(size)) throw new Error("Batch size must be an integer");
|
|
75
|
+
for (const thing of source) {
|
|
76
|
+
things.push(thing);
|
|
77
|
+
while (things.length >= size) {
|
|
78
|
+
yield things.slice(0, size);
|
|
79
|
+
things = things.slice(size);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
while (things.length > 0) {
|
|
83
|
+
yield things.slice(0, size);
|
|
84
|
+
things = things.slice(size);
|
|
85
|
+
}
|
|
86
|
+
}();
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region node_modules/it-parallel-batch/dist/src/index.js
|
|
90
|
+
/**
|
|
91
|
+
* @packageDocumentation
|
|
92
|
+
*
|
|
93
|
+
* Takes an async iterator that emits promise-returning functions, invokes them in parallel and emits the results in the same order as the input.
|
|
94
|
+
*
|
|
95
|
+
* The final batch may be smaller than the batch size.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
*
|
|
99
|
+
* ```javascript
|
|
100
|
+
* import parallelBatch from 'it-parallel-batch'
|
|
101
|
+
* import all from 'it-all'
|
|
102
|
+
* import delay from 'delay'
|
|
103
|
+
*
|
|
104
|
+
* // This can also be an iterator, async iterator, generator, etc
|
|
105
|
+
* const input = [
|
|
106
|
+
* async () => {
|
|
107
|
+
* await delay(500)
|
|
108
|
+
*
|
|
109
|
+
* return 1
|
|
110
|
+
* },
|
|
111
|
+
* async () => {
|
|
112
|
+
* await delay(200)
|
|
113
|
+
*
|
|
114
|
+
* return 2
|
|
115
|
+
* },
|
|
116
|
+
* async () => {
|
|
117
|
+
* await delay(100)
|
|
118
|
+
*
|
|
119
|
+
* return 3
|
|
120
|
+
* }
|
|
121
|
+
* ]
|
|
122
|
+
*
|
|
123
|
+
* const batchSize = 2
|
|
124
|
+
*
|
|
125
|
+
* const result = await all(parallelBatch(input, batchSize))
|
|
126
|
+
*
|
|
127
|
+
* console.info(result) // [1, 2, 3]
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
/**
|
|
131
|
+
* Takes an (async) iterator that emits promise-returning functions,
|
|
132
|
+
* invokes them in parallel and emits the results as they become available but
|
|
133
|
+
* in the same order as the input
|
|
134
|
+
*/
|
|
135
|
+
async function* parallelBatch(source, size = 1) {
|
|
136
|
+
for await (const tasks of batch(source, size)) {
|
|
137
|
+
const things = tasks.map(async (p) => {
|
|
138
|
+
return p().then((value) => ({
|
|
139
|
+
ok: true,
|
|
140
|
+
value
|
|
141
|
+
}), (err) => ({
|
|
142
|
+
ok: false,
|
|
143
|
+
err
|
|
144
|
+
}));
|
|
145
|
+
});
|
|
146
|
+
for (let i = 0; i < things.length; i++) {
|
|
147
|
+
const result = await things[i];
|
|
148
|
+
if (result.ok) yield result.value;
|
|
149
|
+
else throw result.err;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/uint8arraylist/dist/src/index.js
|
|
155
|
+
/**
|
|
156
|
+
* @packageDocumentation
|
|
157
|
+
*
|
|
158
|
+
* A class that lets you do operations over a list of Uint8Arrays without
|
|
159
|
+
* copying them.
|
|
160
|
+
*
|
|
161
|
+
* ```js
|
|
162
|
+
* import { Uint8ArrayList } from 'uint8arraylist'
|
|
163
|
+
*
|
|
164
|
+
* const list = new Uint8ArrayList()
|
|
165
|
+
* list.append(Uint8Array.from([0, 1, 2]))
|
|
166
|
+
* list.append(Uint8Array.from([3, 4, 5]))
|
|
167
|
+
*
|
|
168
|
+
* list.subarray()
|
|
169
|
+
* // -> Uint8Array([0, 1, 2, 3, 4, 5])
|
|
170
|
+
*
|
|
171
|
+
* list.consume(3)
|
|
172
|
+
* list.subarray()
|
|
173
|
+
* // -> Uint8Array([3, 4, 5])
|
|
174
|
+
*
|
|
175
|
+
* // you can also iterate over the list
|
|
176
|
+
* for (const buf of list) {
|
|
177
|
+
* // ..do something with `buf`
|
|
178
|
+
* }
|
|
179
|
+
*
|
|
180
|
+
* list.subarray(0, 1)
|
|
181
|
+
* // -> Uint8Array([0])
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* ## Converting Uint8ArrayLists to Uint8Arrays
|
|
185
|
+
*
|
|
186
|
+
* There are two ways to turn a `Uint8ArrayList` into a `Uint8Array` - `.slice` and `.subarray` and one way to turn a `Uint8ArrayList` into a `Uint8ArrayList` with different contents - `.sublist`.
|
|
187
|
+
*
|
|
188
|
+
* ### slice
|
|
189
|
+
*
|
|
190
|
+
* Slice follows the same semantics as [Uint8Array.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice) in that it creates a new `Uint8Array` and copies bytes into it using an optional offset & length.
|
|
191
|
+
*
|
|
192
|
+
* ```js
|
|
193
|
+
* const list = new Uint8ArrayList()
|
|
194
|
+
* list.append(Uint8Array.from([0, 1, 2]))
|
|
195
|
+
* list.append(Uint8Array.from([3, 4, 5]))
|
|
196
|
+
*
|
|
197
|
+
* list.slice(0, 1)
|
|
198
|
+
* // -> Uint8Array([0])
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* ### subarray
|
|
202
|
+
*
|
|
203
|
+
* Subarray attempts to follow the same semantics as [Uint8Array.subarray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray) with one important different - this is a no-copy operation, unless the requested bytes span two internal buffers in which case it is a copy operation.
|
|
204
|
+
*
|
|
205
|
+
* ```js
|
|
206
|
+
* const list = new Uint8ArrayList()
|
|
207
|
+
* list.append(Uint8Array.from([0, 1, 2]))
|
|
208
|
+
* list.append(Uint8Array.from([3, 4, 5]))
|
|
209
|
+
*
|
|
210
|
+
* list.subarray(0, 1)
|
|
211
|
+
* // -> Uint8Array([0]) - no-copy
|
|
212
|
+
*
|
|
213
|
+
* list.subarray(2, 5)
|
|
214
|
+
* // -> Uint8Array([2, 3, 4]) - copy
|
|
215
|
+
* ```
|
|
216
|
+
*
|
|
217
|
+
* ### sublist
|
|
218
|
+
*
|
|
219
|
+
* Sublist creates and returns a new `Uint8ArrayList` that shares the underlying buffers with the original so is always a no-copy operation.
|
|
220
|
+
*
|
|
221
|
+
* ```js
|
|
222
|
+
* const list = new Uint8ArrayList()
|
|
223
|
+
* list.append(Uint8Array.from([0, 1, 2]))
|
|
224
|
+
* list.append(Uint8Array.from([3, 4, 5]))
|
|
225
|
+
*
|
|
226
|
+
* list.sublist(0, 1)
|
|
227
|
+
* // -> Uint8ArrayList([0]) - no-copy
|
|
228
|
+
*
|
|
229
|
+
* list.sublist(2, 5)
|
|
230
|
+
* // -> Uint8ArrayList([2], [3, 4]) - no-copy
|
|
231
|
+
* ```
|
|
232
|
+
*
|
|
233
|
+
* ## Inspiration
|
|
234
|
+
*
|
|
235
|
+
* Borrows liberally from [bl](https://www.npmjs.com/package/bl) but only uses native JS types.
|
|
236
|
+
*/
|
|
237
|
+
const symbol = Symbol.for("@achingbrain/uint8arraylist");
|
|
238
|
+
function findBufAndOffset(bufs, index) {
|
|
239
|
+
if (index == null || index < 0) throw new RangeError("index is out of bounds");
|
|
240
|
+
let offset = 0;
|
|
241
|
+
for (const buf of bufs) {
|
|
242
|
+
const bufEnd = offset + buf.byteLength;
|
|
243
|
+
if (index < bufEnd) return {
|
|
244
|
+
buf,
|
|
245
|
+
index: index - offset
|
|
246
|
+
};
|
|
247
|
+
offset = bufEnd;
|
|
248
|
+
}
|
|
249
|
+
throw new RangeError("index is out of bounds");
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Check if object is a CID instance
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
*
|
|
256
|
+
* ```js
|
|
257
|
+
* import { isUint8ArrayList, Uint8ArrayList } from 'uint8arraylist'
|
|
258
|
+
*
|
|
259
|
+
* isUint8ArrayList(true) // false
|
|
260
|
+
* isUint8ArrayList([]) // false
|
|
261
|
+
* isUint8ArrayList(new Uint8ArrayList()) // true
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
function isUint8ArrayList(value) {
|
|
265
|
+
return Boolean(value?.[symbol]);
|
|
266
|
+
}
|
|
267
|
+
var Uint8ArrayList = class Uint8ArrayList {
|
|
268
|
+
bufs;
|
|
269
|
+
length;
|
|
270
|
+
[symbol] = true;
|
|
271
|
+
constructor(...data) {
|
|
272
|
+
this.bufs = [];
|
|
273
|
+
this.length = 0;
|
|
274
|
+
if (data.length > 0) this.appendAll(data);
|
|
275
|
+
}
|
|
276
|
+
*[Symbol.iterator]() {
|
|
277
|
+
yield* this.bufs;
|
|
278
|
+
}
|
|
279
|
+
get byteLength() {
|
|
280
|
+
return this.length;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Add one or more `bufs` to the end of this Uint8ArrayList
|
|
284
|
+
*/
|
|
285
|
+
append(...bufs) {
|
|
286
|
+
this.appendAll(bufs);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Add all `bufs` to the end of this Uint8ArrayList
|
|
290
|
+
*/
|
|
291
|
+
appendAll(bufs) {
|
|
292
|
+
let length = 0;
|
|
293
|
+
for (const buf of bufs) if (buf instanceof Uint8Array) {
|
|
294
|
+
length += buf.byteLength;
|
|
295
|
+
this.bufs.push(buf);
|
|
296
|
+
} else if (isUint8ArrayList(buf)) {
|
|
297
|
+
length += buf.byteLength;
|
|
298
|
+
for (const chunk of buf.bufs) this.bufs.push(chunk);
|
|
299
|
+
} else throw new Error("Could not append value, must be an Uint8Array or a Uint8ArrayList");
|
|
300
|
+
this.length += length;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Add one or more `bufs` to the start of this Uint8ArrayList
|
|
304
|
+
*/
|
|
305
|
+
prepend(...bufs) {
|
|
306
|
+
this.prependAll(bufs);
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Add all `bufs` to the start of this Uint8ArrayList
|
|
310
|
+
*/
|
|
311
|
+
prependAll(bufs) {
|
|
312
|
+
let length = 0;
|
|
313
|
+
for (const buf of bufs.reverse()) if (buf instanceof Uint8Array) {
|
|
314
|
+
length += buf.byteLength;
|
|
315
|
+
this.bufs.unshift(buf);
|
|
316
|
+
} else if (isUint8ArrayList(buf)) {
|
|
317
|
+
length += buf.byteLength;
|
|
318
|
+
this.bufs.unshift(...buf.bufs);
|
|
319
|
+
} else throw new Error("Could not prepend value, must be an Uint8Array or a Uint8ArrayList");
|
|
320
|
+
this.length += length;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Read the value at `index`
|
|
324
|
+
*/
|
|
325
|
+
get(index) {
|
|
326
|
+
const res = findBufAndOffset(this.bufs, index);
|
|
327
|
+
return res.buf[res.index];
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Set the value at `index` to `value`
|
|
331
|
+
*/
|
|
332
|
+
set(index, value) {
|
|
333
|
+
const res = findBufAndOffset(this.bufs, index);
|
|
334
|
+
res.buf[res.index] = value;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Copy bytes from `buf` to the index specified by `offset`
|
|
338
|
+
*/
|
|
339
|
+
write(buf, offset = 0) {
|
|
340
|
+
if (buf instanceof Uint8Array) for (let i = 0; i < buf.length; i++) this.set(offset + i, buf[i]);
|
|
341
|
+
else if (isUint8ArrayList(buf)) for (let i = 0; i < buf.length; i++) this.set(offset + i, buf.get(i));
|
|
342
|
+
else throw new Error("Could not write value, must be an Uint8Array or a Uint8ArrayList");
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Remove bytes from the front of the pool
|
|
346
|
+
*/
|
|
347
|
+
consume(bytes) {
|
|
348
|
+
bytes = Math.trunc(bytes);
|
|
349
|
+
if (Number.isNaN(bytes) || bytes <= 0) return;
|
|
350
|
+
if (bytes === this.byteLength) {
|
|
351
|
+
this.bufs = [];
|
|
352
|
+
this.length = 0;
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
while (this.bufs.length > 0) if (bytes >= this.bufs[0].byteLength) {
|
|
356
|
+
bytes -= this.bufs[0].byteLength;
|
|
357
|
+
this.length -= this.bufs[0].byteLength;
|
|
358
|
+
this.bufs.shift();
|
|
359
|
+
} else {
|
|
360
|
+
this.bufs[0] = this.bufs[0].subarray(bytes);
|
|
361
|
+
this.length -= bytes;
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Extracts a section of an array and returns a new array.
|
|
367
|
+
*
|
|
368
|
+
* This is a copy operation as it is with Uint8Arrays and Arrays
|
|
369
|
+
* - note this is different to the behaviour of Node Buffers.
|
|
370
|
+
*/
|
|
371
|
+
slice(beginInclusive, endExclusive) {
|
|
372
|
+
const { bufs, length } = this._subList(beginInclusive, endExclusive);
|
|
373
|
+
return concat(bufs, length);
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Returns a section of the array from the given start and end element index.
|
|
377
|
+
*
|
|
378
|
+
* This is a no-copy operation where the data extracted data comes from a
|
|
379
|
+
* single Uint8Array internally, otherwise it is a copy operation.
|
|
380
|
+
*/
|
|
381
|
+
subarray(beginInclusive, endExclusive) {
|
|
382
|
+
const { bufs, length } = this._subList(beginInclusive, endExclusive);
|
|
383
|
+
if (bufs.length === 1) return bufs[0];
|
|
384
|
+
return concat(bufs, length);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Returns a new Uint8ArrayList from the given start and end element index.
|
|
388
|
+
*
|
|
389
|
+
* This is a no-copy operation.
|
|
390
|
+
*/
|
|
391
|
+
sublist(beginInclusive, endExclusive) {
|
|
392
|
+
const { bufs, length } = this._subList(beginInclusive, endExclusive);
|
|
393
|
+
const list = new Uint8ArrayList();
|
|
394
|
+
list.length = length;
|
|
395
|
+
list.bufs = bufs;
|
|
396
|
+
return list;
|
|
397
|
+
}
|
|
398
|
+
_subList(beginInclusive, endExclusive) {
|
|
399
|
+
beginInclusive = beginInclusive ?? 0;
|
|
400
|
+
endExclusive = endExclusive ?? this.length;
|
|
401
|
+
if (beginInclusive < 0) beginInclusive = this.length + beginInclusive;
|
|
402
|
+
if (endExclusive < 0) endExclusive = this.length + endExclusive;
|
|
403
|
+
if (beginInclusive < 0 || endExclusive > this.length) throw new RangeError("index is out of bounds");
|
|
404
|
+
if (beginInclusive === endExclusive) return {
|
|
405
|
+
bufs: [],
|
|
406
|
+
length: 0
|
|
407
|
+
};
|
|
408
|
+
if (beginInclusive === 0 && endExclusive === this.length) return {
|
|
409
|
+
bufs: [...this.bufs],
|
|
410
|
+
length: this.length
|
|
411
|
+
};
|
|
412
|
+
const bufs = [];
|
|
413
|
+
let offset = 0;
|
|
414
|
+
for (let i = 0; i < this.bufs.length; i++) {
|
|
415
|
+
const buf = this.bufs[i];
|
|
416
|
+
const bufStart = offset;
|
|
417
|
+
const bufEnd = bufStart + buf.byteLength;
|
|
418
|
+
offset = bufEnd;
|
|
419
|
+
if (beginInclusive >= bufEnd) continue;
|
|
420
|
+
const sliceStartInBuf = beginInclusive >= bufStart && beginInclusive < bufEnd;
|
|
421
|
+
const sliceEndsInBuf = endExclusive > bufStart && endExclusive <= bufEnd;
|
|
422
|
+
if (sliceStartInBuf && sliceEndsInBuf) {
|
|
423
|
+
if (beginInclusive === bufStart && endExclusive === bufEnd) {
|
|
424
|
+
bufs.push(buf);
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
const start = beginInclusive - bufStart;
|
|
428
|
+
bufs.push(buf.subarray(start, start + (endExclusive - beginInclusive)));
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
if (sliceStartInBuf) {
|
|
432
|
+
if (beginInclusive === 0) {
|
|
433
|
+
bufs.push(buf);
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
bufs.push(buf.subarray(beginInclusive - bufStart));
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (sliceEndsInBuf) {
|
|
440
|
+
if (endExclusive === bufEnd) {
|
|
441
|
+
bufs.push(buf);
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
bufs.push(buf.subarray(0, endExclusive - bufStart));
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
bufs.push(buf);
|
|
448
|
+
}
|
|
449
|
+
return {
|
|
450
|
+
bufs,
|
|
451
|
+
length: endExclusive - beginInclusive
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
indexOf(search, offset = 0) {
|
|
455
|
+
if (!isUint8ArrayList(search) && !(search instanceof Uint8Array)) throw new TypeError("The \"value\" argument must be a Uint8ArrayList or Uint8Array");
|
|
456
|
+
const needle = search instanceof Uint8Array ? search : search.subarray();
|
|
457
|
+
offset = Number(offset ?? 0);
|
|
458
|
+
if (isNaN(offset)) offset = 0;
|
|
459
|
+
if (offset < 0) offset = this.length + offset;
|
|
460
|
+
if (offset < 0) offset = 0;
|
|
461
|
+
if (search.length === 0) return offset > this.length ? this.length : offset;
|
|
462
|
+
const M = needle.byteLength;
|
|
463
|
+
if (M === 0) throw new TypeError("search must be at least 1 byte long");
|
|
464
|
+
const radix = 256;
|
|
465
|
+
const rightmostPositions = new Int32Array(radix);
|
|
466
|
+
for (let c = 0; c < radix; c++) rightmostPositions[c] = -1;
|
|
467
|
+
for (let j = 0; j < M; j++) rightmostPositions[needle[j]] = j;
|
|
468
|
+
const right = rightmostPositions;
|
|
469
|
+
const lastIndex = this.byteLength - needle.byteLength;
|
|
470
|
+
const lastPatIndex = needle.byteLength - 1;
|
|
471
|
+
let skip;
|
|
472
|
+
for (let i = offset; i <= lastIndex; i += skip) {
|
|
473
|
+
skip = 0;
|
|
474
|
+
for (let j = lastPatIndex; j >= 0; j--) {
|
|
475
|
+
const char = this.get(i + j);
|
|
476
|
+
if (needle[j] !== char) {
|
|
477
|
+
skip = Math.max(1, j - right[char]);
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
if (skip === 0) return i;
|
|
482
|
+
}
|
|
483
|
+
return -1;
|
|
484
|
+
}
|
|
485
|
+
getInt8(byteOffset) {
|
|
486
|
+
const buf = this.subarray(byteOffset, byteOffset + 1);
|
|
487
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getInt8(0);
|
|
488
|
+
}
|
|
489
|
+
setInt8(byteOffset, value) {
|
|
490
|
+
const buf = allocUnsafe(1);
|
|
491
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setInt8(0, value);
|
|
492
|
+
this.write(buf, byteOffset);
|
|
493
|
+
}
|
|
494
|
+
getInt16(byteOffset, littleEndian) {
|
|
495
|
+
const buf = this.subarray(byteOffset, byteOffset + 2);
|
|
496
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getInt16(0, littleEndian);
|
|
497
|
+
}
|
|
498
|
+
setInt16(byteOffset, value, littleEndian) {
|
|
499
|
+
const buf = alloc(2);
|
|
500
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setInt16(0, value, littleEndian);
|
|
501
|
+
this.write(buf, byteOffset);
|
|
502
|
+
}
|
|
503
|
+
getInt32(byteOffset, littleEndian) {
|
|
504
|
+
const buf = this.subarray(byteOffset, byteOffset + 4);
|
|
505
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getInt32(0, littleEndian);
|
|
506
|
+
}
|
|
507
|
+
setInt32(byteOffset, value, littleEndian) {
|
|
508
|
+
const buf = alloc(4);
|
|
509
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setInt32(0, value, littleEndian);
|
|
510
|
+
this.write(buf, byteOffset);
|
|
511
|
+
}
|
|
512
|
+
getBigInt64(byteOffset, littleEndian) {
|
|
513
|
+
const buf = this.subarray(byteOffset, byteOffset + 8);
|
|
514
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getBigInt64(0, littleEndian);
|
|
515
|
+
}
|
|
516
|
+
setBigInt64(byteOffset, value, littleEndian) {
|
|
517
|
+
const buf = alloc(8);
|
|
518
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setBigInt64(0, value, littleEndian);
|
|
519
|
+
this.write(buf, byteOffset);
|
|
520
|
+
}
|
|
521
|
+
getUint8(byteOffset) {
|
|
522
|
+
const buf = this.subarray(byteOffset, byteOffset + 1);
|
|
523
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getUint8(0);
|
|
524
|
+
}
|
|
525
|
+
setUint8(byteOffset, value) {
|
|
526
|
+
const buf = allocUnsafe(1);
|
|
527
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setUint8(0, value);
|
|
528
|
+
this.write(buf, byteOffset);
|
|
529
|
+
}
|
|
530
|
+
getUint16(byteOffset, littleEndian) {
|
|
531
|
+
const buf = this.subarray(byteOffset, byteOffset + 2);
|
|
532
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getUint16(0, littleEndian);
|
|
533
|
+
}
|
|
534
|
+
setUint16(byteOffset, value, littleEndian) {
|
|
535
|
+
const buf = alloc(2);
|
|
536
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setUint16(0, value, littleEndian);
|
|
537
|
+
this.write(buf, byteOffset);
|
|
538
|
+
}
|
|
539
|
+
getUint32(byteOffset, littleEndian) {
|
|
540
|
+
const buf = this.subarray(byteOffset, byteOffset + 4);
|
|
541
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getUint32(0, littleEndian);
|
|
542
|
+
}
|
|
543
|
+
setUint32(byteOffset, value, littleEndian) {
|
|
544
|
+
const buf = alloc(4);
|
|
545
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setUint32(0, value, littleEndian);
|
|
546
|
+
this.write(buf, byteOffset);
|
|
547
|
+
}
|
|
548
|
+
getBigUint64(byteOffset, littleEndian) {
|
|
549
|
+
const buf = this.subarray(byteOffset, byteOffset + 8);
|
|
550
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getBigUint64(0, littleEndian);
|
|
551
|
+
}
|
|
552
|
+
setBigUint64(byteOffset, value, littleEndian) {
|
|
553
|
+
const buf = alloc(8);
|
|
554
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setBigUint64(0, value, littleEndian);
|
|
555
|
+
this.write(buf, byteOffset);
|
|
556
|
+
}
|
|
557
|
+
getFloat32(byteOffset, littleEndian) {
|
|
558
|
+
const buf = this.subarray(byteOffset, byteOffset + 4);
|
|
559
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getFloat32(0, littleEndian);
|
|
560
|
+
}
|
|
561
|
+
setFloat32(byteOffset, value, littleEndian) {
|
|
562
|
+
const buf = alloc(4);
|
|
563
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setFloat32(0, value, littleEndian);
|
|
564
|
+
this.write(buf, byteOffset);
|
|
565
|
+
}
|
|
566
|
+
getFloat64(byteOffset, littleEndian) {
|
|
567
|
+
const buf = this.subarray(byteOffset, byteOffset + 8);
|
|
568
|
+
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength).getFloat64(0, littleEndian);
|
|
569
|
+
}
|
|
570
|
+
setFloat64(byteOffset, value, littleEndian) {
|
|
571
|
+
const buf = alloc(8);
|
|
572
|
+
new DataView(buf.buffer, buf.byteOffset, buf.byteLength).setFloat64(0, value, littleEndian);
|
|
573
|
+
this.write(buf, byteOffset);
|
|
574
|
+
}
|
|
575
|
+
equals(other) {
|
|
576
|
+
if (other == null) return false;
|
|
577
|
+
if (!(other instanceof Uint8ArrayList)) return false;
|
|
578
|
+
if (other.bufs.length !== this.bufs.length) return false;
|
|
579
|
+
for (let i = 0; i < this.bufs.length; i++) if (!equals(this.bufs[i], other.bufs[i])) return false;
|
|
580
|
+
return true;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Create a Uint8ArrayList from a pre-existing list of Uint8Arrays. Use this
|
|
584
|
+
* method if you know the total size of all the Uint8Arrays ahead of time.
|
|
585
|
+
*/
|
|
586
|
+
static fromUint8Arrays(bufs, length) {
|
|
587
|
+
const list = new Uint8ArrayList();
|
|
588
|
+
list.bufs = bufs;
|
|
589
|
+
if (length == null) length = bufs.reduce((acc, curr) => acc + curr.byteLength, 0);
|
|
590
|
+
list.length = length;
|
|
591
|
+
return list;
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
//#endregion
|
|
595
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/constants.js
|
|
596
|
+
const DEFAULT_SHARD_HASH_CODE = BigInt(34);
|
|
597
|
+
const DEFAULT_CHUNK_SIZE_256KIB = 262144;
|
|
598
|
+
const DEFAULT_CHUNK_SIZE_1MIB = 1048576;
|
|
599
|
+
//#endregion
|
|
600
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/chunker/fixed-size.js
|
|
601
|
+
const fixedSize = (options = {}) => {
|
|
602
|
+
const chunkSize = options.chunkSize ?? 262144;
|
|
603
|
+
return async function* fixedSizeChunker(source) {
|
|
604
|
+
let list = new Uint8ArrayList();
|
|
605
|
+
let currentLength = 0;
|
|
606
|
+
let emitted = false;
|
|
607
|
+
for await (const buffer of source) {
|
|
608
|
+
list.append(buffer);
|
|
609
|
+
currentLength += buffer.length;
|
|
610
|
+
while (currentLength >= chunkSize) {
|
|
611
|
+
yield list.subarray(0, chunkSize);
|
|
612
|
+
emitted = true;
|
|
613
|
+
if (chunkSize === list.length) {
|
|
614
|
+
list = new Uint8ArrayList();
|
|
615
|
+
currentLength = 0;
|
|
616
|
+
} else {
|
|
617
|
+
const newBl = new Uint8ArrayList();
|
|
618
|
+
newBl.append(list.sublist(chunkSize));
|
|
619
|
+
list = newBl;
|
|
620
|
+
currentLength -= chunkSize;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
if (!emitted || currentLength > 0) yield list.subarray(0, currentLength);
|
|
625
|
+
};
|
|
626
|
+
};
|
|
627
|
+
//#endregion
|
|
628
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/ipfs-unixfs/dist/src/errors.js
|
|
629
|
+
var InvalidTypeError = class InvalidTypeError extends Error {
|
|
630
|
+
static name = "InvalidTypeError";
|
|
631
|
+
static code = "ERR_INVALID_TYPE";
|
|
632
|
+
name = InvalidTypeError.name;
|
|
633
|
+
code = InvalidTypeError.code;
|
|
634
|
+
constructor(message = "Invalid type") {
|
|
635
|
+
super(message);
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
//#endregion
|
|
639
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/uint8-varint/dist/src/index.js
|
|
640
|
+
const N1 = Math.pow(2, 7);
|
|
641
|
+
const N2 = Math.pow(2, 14);
|
|
642
|
+
const N3 = Math.pow(2, 21);
|
|
643
|
+
const N4 = Math.pow(2, 28);
|
|
644
|
+
const N5 = Math.pow(2, 35);
|
|
645
|
+
const N6 = Math.pow(2, 42);
|
|
646
|
+
const N7 = Math.pow(2, 49);
|
|
647
|
+
/** Most significant bit of a byte */
|
|
648
|
+
const MSB = 128;
|
|
649
|
+
/** Rest of the bits in a byte */
|
|
650
|
+
const REST = 127;
|
|
651
|
+
function encodingLength(value) {
|
|
652
|
+
if (value < N1) return 1;
|
|
653
|
+
if (value < N2) return 2;
|
|
654
|
+
if (value < N3) return 3;
|
|
655
|
+
if (value < N4) return 4;
|
|
656
|
+
if (value < N5) return 5;
|
|
657
|
+
if (value < N6) return 6;
|
|
658
|
+
if (value < N7) return 7;
|
|
659
|
+
if (Number.MAX_SAFE_INTEGER != null && value > Number.MAX_SAFE_INTEGER) throw new RangeError("Could not encode varint");
|
|
660
|
+
return 8;
|
|
661
|
+
}
|
|
662
|
+
function encodeUint8Array(value, buf, offset = 0) {
|
|
663
|
+
switch (encodingLength(value)) {
|
|
664
|
+
case 8:
|
|
665
|
+
buf[offset++] = value & 255 | MSB;
|
|
666
|
+
value /= 128;
|
|
667
|
+
case 7:
|
|
668
|
+
buf[offset++] = value & 255 | MSB;
|
|
669
|
+
value /= 128;
|
|
670
|
+
case 6:
|
|
671
|
+
buf[offset++] = value & 255 | MSB;
|
|
672
|
+
value /= 128;
|
|
673
|
+
case 5:
|
|
674
|
+
buf[offset++] = value & 255 | MSB;
|
|
675
|
+
value /= 128;
|
|
676
|
+
case 4:
|
|
677
|
+
buf[offset++] = value & 255 | MSB;
|
|
678
|
+
value >>>= 7;
|
|
679
|
+
case 3:
|
|
680
|
+
buf[offset++] = value & 255 | MSB;
|
|
681
|
+
value >>>= 7;
|
|
682
|
+
case 2:
|
|
683
|
+
buf[offset++] = value & 255 | MSB;
|
|
684
|
+
value >>>= 7;
|
|
685
|
+
case 1:
|
|
686
|
+
buf[offset++] = value & 255;
|
|
687
|
+
value >>>= 7;
|
|
688
|
+
break;
|
|
689
|
+
default: throw new Error("unreachable");
|
|
690
|
+
}
|
|
691
|
+
return buf;
|
|
692
|
+
}
|
|
693
|
+
function decodeUint8Array(buf, offset) {
|
|
694
|
+
let b = buf[offset];
|
|
695
|
+
let res = 0;
|
|
696
|
+
res += b & REST;
|
|
697
|
+
if (b < MSB) return res;
|
|
698
|
+
b = buf[offset + 1];
|
|
699
|
+
res += (b & REST) << 7;
|
|
700
|
+
if (b < MSB) return res;
|
|
701
|
+
b = buf[offset + 2];
|
|
702
|
+
res += (b & REST) << 14;
|
|
703
|
+
if (b < MSB) return res;
|
|
704
|
+
b = buf[offset + 3];
|
|
705
|
+
res += (b & REST) << 21;
|
|
706
|
+
if (b < MSB) return res;
|
|
707
|
+
b = buf[offset + 4];
|
|
708
|
+
res += (b & REST) * N4;
|
|
709
|
+
if (b < MSB) return res;
|
|
710
|
+
b = buf[offset + 5];
|
|
711
|
+
res += (b & REST) * N5;
|
|
712
|
+
if (b < MSB) return res;
|
|
713
|
+
b = buf[offset + 6];
|
|
714
|
+
res += (b & REST) * N6;
|
|
715
|
+
if (b < MSB) return res;
|
|
716
|
+
b = buf[offset + 7];
|
|
717
|
+
res += (b & REST) * N7;
|
|
718
|
+
if (b < MSB) return res;
|
|
719
|
+
throw new RangeError("Could not decode varint");
|
|
720
|
+
}
|
|
721
|
+
//#endregion
|
|
722
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/utils/float.js
|
|
723
|
+
const f32 = new Float32Array([-0]);
|
|
724
|
+
const f8b = new Uint8Array(f32.buffer);
|
|
725
|
+
/**
|
|
726
|
+
* Writes a 32 bit float to a buffer using little endian byte order
|
|
727
|
+
*/
|
|
728
|
+
function writeFloatLE(val, buf, pos) {
|
|
729
|
+
f32[0] = val;
|
|
730
|
+
buf[pos] = f8b[0];
|
|
731
|
+
buf[pos + 1] = f8b[1];
|
|
732
|
+
buf[pos + 2] = f8b[2];
|
|
733
|
+
buf[pos + 3] = f8b[3];
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* Reads a 32 bit float from a buffer using little endian byte order
|
|
737
|
+
*/
|
|
738
|
+
function readFloatLE(buf, pos) {
|
|
739
|
+
f8b[0] = buf[pos];
|
|
740
|
+
f8b[1] = buf[pos + 1];
|
|
741
|
+
f8b[2] = buf[pos + 2];
|
|
742
|
+
f8b[3] = buf[pos + 3];
|
|
743
|
+
return f32[0];
|
|
744
|
+
}
|
|
745
|
+
const f64 = new Float64Array([-0]);
|
|
746
|
+
const d8b = new Uint8Array(f64.buffer);
|
|
747
|
+
/**
|
|
748
|
+
* Writes a 64 bit double to a buffer using little endian byte order
|
|
749
|
+
*/
|
|
750
|
+
function writeDoubleLE(val, buf, pos) {
|
|
751
|
+
f64[0] = val;
|
|
752
|
+
buf[pos] = d8b[0];
|
|
753
|
+
buf[pos + 1] = d8b[1];
|
|
754
|
+
buf[pos + 2] = d8b[2];
|
|
755
|
+
buf[pos + 3] = d8b[3];
|
|
756
|
+
buf[pos + 4] = d8b[4];
|
|
757
|
+
buf[pos + 5] = d8b[5];
|
|
758
|
+
buf[pos + 6] = d8b[6];
|
|
759
|
+
buf[pos + 7] = d8b[7];
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Reads a 64 bit double from a buffer using little endian byte order
|
|
763
|
+
*/
|
|
764
|
+
function readDoubleLE(buf, pos) {
|
|
765
|
+
d8b[0] = buf[pos];
|
|
766
|
+
d8b[1] = buf[pos + 1];
|
|
767
|
+
d8b[2] = buf[pos + 2];
|
|
768
|
+
d8b[3] = buf[pos + 3];
|
|
769
|
+
d8b[4] = buf[pos + 4];
|
|
770
|
+
d8b[5] = buf[pos + 5];
|
|
771
|
+
d8b[6] = buf[pos + 6];
|
|
772
|
+
d8b[7] = buf[pos + 7];
|
|
773
|
+
return f64[0];
|
|
774
|
+
}
|
|
775
|
+
//#endregion
|
|
776
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/utils/longbits.js
|
|
777
|
+
const MAX_SAFE_NUMBER_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
|
|
778
|
+
const MIN_SAFE_NUMBER_INTEGER = BigInt(Number.MIN_SAFE_INTEGER);
|
|
779
|
+
/**
|
|
780
|
+
* Constructs new long bits.
|
|
781
|
+
*
|
|
782
|
+
* @classdesc Helper class for working with the low and high bits of a 64 bit value.
|
|
783
|
+
* @memberof util
|
|
784
|
+
* @function Object() { [native code] }
|
|
785
|
+
* @param {number} lo - Low 32 bits, unsigned
|
|
786
|
+
* @param {number} hi - High 32 bits, unsigned
|
|
787
|
+
*/
|
|
788
|
+
var LongBits = class LongBits {
|
|
789
|
+
lo;
|
|
790
|
+
hi;
|
|
791
|
+
constructor(lo, hi) {
|
|
792
|
+
/**
|
|
793
|
+
* Low bits
|
|
794
|
+
*/
|
|
795
|
+
this.lo = lo | 0;
|
|
796
|
+
/**
|
|
797
|
+
* High bits
|
|
798
|
+
*/
|
|
799
|
+
this.hi = hi | 0;
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Converts this long bits to a possibly unsafe JavaScript number
|
|
803
|
+
*/
|
|
804
|
+
toNumber(unsigned = false) {
|
|
805
|
+
if (!unsigned && this.hi >>> 31 > 0) {
|
|
806
|
+
const lo = ~this.lo + 1 >>> 0;
|
|
807
|
+
let hi = ~this.hi >>> 0;
|
|
808
|
+
if (lo === 0) hi = hi + 1 >>> 0;
|
|
809
|
+
return -(lo + hi * 4294967296);
|
|
810
|
+
}
|
|
811
|
+
return this.lo + this.hi * 4294967296;
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Converts this long bits to a bigint
|
|
815
|
+
*/
|
|
816
|
+
toBigInt(unsigned = false) {
|
|
817
|
+
if (unsigned) return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n);
|
|
818
|
+
if (this.hi >>> 31 !== 0) {
|
|
819
|
+
const lo = ~this.lo + 1 >>> 0;
|
|
820
|
+
let hi = ~this.hi >>> 0;
|
|
821
|
+
if (lo === 0) hi = hi + 1 >>> 0;
|
|
822
|
+
return -(BigInt(lo) + (BigInt(hi) << 32n));
|
|
823
|
+
}
|
|
824
|
+
return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n);
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* Converts this long bits to a string
|
|
828
|
+
*/
|
|
829
|
+
toString(unsigned = false) {
|
|
830
|
+
return this.toBigInt(unsigned).toString();
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Zig-zag encodes this long bits
|
|
834
|
+
*/
|
|
835
|
+
zzEncode() {
|
|
836
|
+
const mask = this.hi >> 31;
|
|
837
|
+
this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
|
|
838
|
+
this.lo = (this.lo << 1 ^ mask) >>> 0;
|
|
839
|
+
return this;
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Zig-zag decodes this long bits
|
|
843
|
+
*/
|
|
844
|
+
zzDecode() {
|
|
845
|
+
const mask = -(this.lo & 1);
|
|
846
|
+
this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
|
|
847
|
+
this.hi = (this.hi >>> 1 ^ mask) >>> 0;
|
|
848
|
+
return this;
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Calculates the length of this longbits when encoded as a varint.
|
|
852
|
+
*/
|
|
853
|
+
length() {
|
|
854
|
+
const part0 = this.lo;
|
|
855
|
+
const part1 = (this.lo >>> 28 | this.hi << 4) >>> 0;
|
|
856
|
+
const part2 = this.hi >>> 24;
|
|
857
|
+
return part2 === 0 ? part1 === 0 ? part0 < 16384 ? part0 < 128 ? 1 : 2 : part0 < 2097152 ? 3 : 4 : part1 < 16384 ? part1 < 128 ? 5 : 6 : part1 < 2097152 ? 7 : 8 : part2 < 128 ? 9 : 10;
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Constructs new long bits from the specified number
|
|
861
|
+
*/
|
|
862
|
+
static fromBigInt(value) {
|
|
863
|
+
if (value === 0n) return zero;
|
|
864
|
+
if (value < MAX_SAFE_NUMBER_INTEGER && value > MIN_SAFE_NUMBER_INTEGER) return this.fromNumber(Number(value));
|
|
865
|
+
const negative = value < 0n;
|
|
866
|
+
if (negative) value = -value;
|
|
867
|
+
let hi = value >> 32n;
|
|
868
|
+
let lo = value - (hi << 32n);
|
|
869
|
+
if (negative) {
|
|
870
|
+
hi = ~hi | 0n;
|
|
871
|
+
lo = ~lo | 0n;
|
|
872
|
+
if (++lo > TWO_32) {
|
|
873
|
+
lo = 0n;
|
|
874
|
+
if (++hi > TWO_32) hi = 0n;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
return new LongBits(Number(lo), Number(hi));
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* Constructs new long bits from the specified number
|
|
881
|
+
*/
|
|
882
|
+
static fromNumber(value) {
|
|
883
|
+
if (value === 0) return zero;
|
|
884
|
+
const sign = value < 0;
|
|
885
|
+
if (sign) value = -value;
|
|
886
|
+
let lo = value >>> 0;
|
|
887
|
+
let hi = (value - lo) / 4294967296 >>> 0;
|
|
888
|
+
if (sign) {
|
|
889
|
+
hi = ~hi >>> 0;
|
|
890
|
+
lo = ~lo >>> 0;
|
|
891
|
+
if (++lo > 4294967295) {
|
|
892
|
+
lo = 0;
|
|
893
|
+
if (++hi > 4294967295) hi = 0;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
return new LongBits(lo, hi);
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Constructs new long bits from a number, long or string
|
|
900
|
+
*/
|
|
901
|
+
static from(value) {
|
|
902
|
+
if (typeof value === "number") return LongBits.fromNumber(value);
|
|
903
|
+
if (typeof value === "bigint") return LongBits.fromBigInt(value);
|
|
904
|
+
if (typeof value === "string") return LongBits.fromBigInt(BigInt(value));
|
|
905
|
+
return value.low != null || value.high != null ? new LongBits(value.low >>> 0, value.high >>> 0) : zero;
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
const zero = new LongBits(0, 0);
|
|
909
|
+
zero.toBigInt = function() {
|
|
910
|
+
return 0n;
|
|
911
|
+
};
|
|
912
|
+
zero.zzEncode = zero.zzDecode = function() {
|
|
913
|
+
return this;
|
|
914
|
+
};
|
|
915
|
+
zero.length = function() {
|
|
916
|
+
return 1;
|
|
917
|
+
};
|
|
918
|
+
const TWO_32 = 4294967296n;
|
|
919
|
+
//#endregion
|
|
920
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/utils/utf8.js
|
|
921
|
+
/**
|
|
922
|
+
* Calculates the UTF8 byte length of a string
|
|
923
|
+
*/
|
|
924
|
+
function length(string) {
|
|
925
|
+
let len = 0;
|
|
926
|
+
let c = 0;
|
|
927
|
+
for (let i = 0; i < string.length; ++i) {
|
|
928
|
+
c = string.charCodeAt(i);
|
|
929
|
+
if (c < 128) len += 1;
|
|
930
|
+
else if (c < 2048) len += 2;
|
|
931
|
+
else if ((c & 64512) === 55296 && (string.charCodeAt(i + 1) & 64512) === 56320) {
|
|
932
|
+
++i;
|
|
933
|
+
len += 4;
|
|
934
|
+
} else len += 3;
|
|
935
|
+
}
|
|
936
|
+
return len;
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Reads UTF8 bytes as a string
|
|
940
|
+
*/
|
|
941
|
+
function read(buffer, start, end) {
|
|
942
|
+
if (end - start < 1) return "";
|
|
943
|
+
let parts;
|
|
944
|
+
const chunk = [];
|
|
945
|
+
let i = 0;
|
|
946
|
+
let t;
|
|
947
|
+
while (start < end) {
|
|
948
|
+
t = buffer[start++];
|
|
949
|
+
if (t < 128) chunk[i++] = t;
|
|
950
|
+
else if (t > 191 && t < 224) chunk[i++] = (t & 31) << 6 | buffer[start++] & 63;
|
|
951
|
+
else if (t > 239 && t < 365) {
|
|
952
|
+
t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 65536;
|
|
953
|
+
chunk[i++] = 55296 + (t >> 10);
|
|
954
|
+
chunk[i++] = 56320 + (t & 1023);
|
|
955
|
+
} else chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
|
|
956
|
+
if (i > 8191) {
|
|
957
|
+
(parts ?? (parts = [])).push(String.fromCharCode.apply(String, chunk));
|
|
958
|
+
i = 0;
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
if (parts != null) {
|
|
962
|
+
if (i > 0) parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
|
|
963
|
+
return parts.join("");
|
|
964
|
+
}
|
|
965
|
+
return String.fromCharCode.apply(String, chunk.slice(0, i));
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Writes a string as UTF8 bytes
|
|
969
|
+
*/
|
|
970
|
+
function write(string, buffer, offset) {
|
|
971
|
+
const start = offset;
|
|
972
|
+
let c1;
|
|
973
|
+
let c2;
|
|
974
|
+
for (let i = 0; i < string.length; ++i) {
|
|
975
|
+
c1 = string.charCodeAt(i);
|
|
976
|
+
if (c1 < 128) buffer[offset++] = c1;
|
|
977
|
+
else if (c1 < 2048) {
|
|
978
|
+
buffer[offset++] = c1 >> 6 | 192;
|
|
979
|
+
buffer[offset++] = c1 & 63 | 128;
|
|
980
|
+
} else if ((c1 & 64512) === 55296 && ((c2 = string.charCodeAt(i + 1)) & 64512) === 56320) {
|
|
981
|
+
c1 = 65536 + ((c1 & 1023) << 10) + (c2 & 1023);
|
|
982
|
+
++i;
|
|
983
|
+
buffer[offset++] = c1 >> 18 | 240;
|
|
984
|
+
buffer[offset++] = c1 >> 12 & 63 | 128;
|
|
985
|
+
buffer[offset++] = c1 >> 6 & 63 | 128;
|
|
986
|
+
buffer[offset++] = c1 & 63 | 128;
|
|
987
|
+
} else {
|
|
988
|
+
buffer[offset++] = c1 >> 12 | 224;
|
|
989
|
+
buffer[offset++] = c1 >> 6 & 63 | 128;
|
|
990
|
+
buffer[offset++] = c1 & 63 | 128;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
return offset - start;
|
|
994
|
+
}
|
|
995
|
+
//#endregion
|
|
996
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/utils/reader.js
|
|
997
|
+
/* istanbul ignore next */
|
|
998
|
+
function indexOutOfRange(reader, writeLength) {
|
|
999
|
+
return RangeError(`index out of range: ${reader.pos} + ${writeLength ?? 1} > ${reader.len}`);
|
|
1000
|
+
}
|
|
1001
|
+
function readFixed32End(buf, end) {
|
|
1002
|
+
return (buf[end - 4] | buf[end - 3] << 8 | buf[end - 2] << 16 | buf[end - 1] << 24) >>> 0;
|
|
1003
|
+
}
|
|
1004
|
+
/**
|
|
1005
|
+
* Constructs a new reader instance using the specified buffer.
|
|
1006
|
+
*/
|
|
1007
|
+
var Uint8ArrayReader = class {
|
|
1008
|
+
buf;
|
|
1009
|
+
pos;
|
|
1010
|
+
len;
|
|
1011
|
+
_slice = Uint8Array.prototype.subarray;
|
|
1012
|
+
constructor(buffer) {
|
|
1013
|
+
/**
|
|
1014
|
+
* Read buffer
|
|
1015
|
+
*/
|
|
1016
|
+
this.buf = withArrayBuffer(buffer);
|
|
1017
|
+
/**
|
|
1018
|
+
* Read buffer position
|
|
1019
|
+
*/
|
|
1020
|
+
this.pos = 0;
|
|
1021
|
+
/**
|
|
1022
|
+
* Read buffer length
|
|
1023
|
+
*/
|
|
1024
|
+
this.len = buffer.length;
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Reads a varint as an unsigned 32 bit value
|
|
1028
|
+
*/
|
|
1029
|
+
uint32() {
|
|
1030
|
+
let value = 4294967295;
|
|
1031
|
+
value = (this.buf[this.pos] & 127) >>> 0;
|
|
1032
|
+
if (this.buf[this.pos++] < 128) return value;
|
|
1033
|
+
value = (value | (this.buf[this.pos] & 127) << 7) >>> 0;
|
|
1034
|
+
if (this.buf[this.pos++] < 128) return value;
|
|
1035
|
+
value = (value | (this.buf[this.pos] & 127) << 14) >>> 0;
|
|
1036
|
+
if (this.buf[this.pos++] < 128) return value;
|
|
1037
|
+
value = (value | (this.buf[this.pos] & 127) << 21) >>> 0;
|
|
1038
|
+
if (this.buf[this.pos++] < 128) return value;
|
|
1039
|
+
value = (value | (this.buf[this.pos] & 15) << 28) >>> 0;
|
|
1040
|
+
if (this.buf[this.pos++] < 128) return value;
|
|
1041
|
+
if ((this.pos += 5) > this.len) {
|
|
1042
|
+
this.pos = this.len;
|
|
1043
|
+
throw indexOutOfRange(this, 10);
|
|
1044
|
+
}
|
|
1045
|
+
return value;
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* Reads a varint as a signed 32 bit value
|
|
1049
|
+
*/
|
|
1050
|
+
int32() {
|
|
1051
|
+
return this.uint32() | 0;
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Reads a zig-zag encoded varint as a signed 32 bit value
|
|
1055
|
+
*/
|
|
1056
|
+
sint32() {
|
|
1057
|
+
const value = this.uint32();
|
|
1058
|
+
return value >>> 1 ^ -(value & 1) | 0;
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Reads a varint as a boolean
|
|
1062
|
+
*/
|
|
1063
|
+
bool() {
|
|
1064
|
+
return this.uint32() !== 0;
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* Reads fixed 32 bits as an unsigned 32 bit integer
|
|
1068
|
+
*/
|
|
1069
|
+
fixed32() {
|
|
1070
|
+
if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4);
|
|
1071
|
+
return readFixed32End(this.buf, this.pos += 4);
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Reads fixed 32 bits as a signed 32 bit integer
|
|
1075
|
+
*/
|
|
1076
|
+
sfixed32() {
|
|
1077
|
+
if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4);
|
|
1078
|
+
return readFixed32End(this.buf, this.pos += 4) | 0;
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* Reads a float (32 bit) as a number
|
|
1082
|
+
*/
|
|
1083
|
+
float() {
|
|
1084
|
+
if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4);
|
|
1085
|
+
const value = readFloatLE(this.buf, this.pos);
|
|
1086
|
+
this.pos += 4;
|
|
1087
|
+
return value;
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Reads a double (64 bit float) as a number
|
|
1091
|
+
*/
|
|
1092
|
+
double() {
|
|
1093
|
+
/* istanbul ignore if */
|
|
1094
|
+
if (this.pos + 8 > this.len) throw indexOutOfRange(this, 4);
|
|
1095
|
+
const value = readDoubleLE(this.buf, this.pos);
|
|
1096
|
+
this.pos += 8;
|
|
1097
|
+
return value;
|
|
1098
|
+
}
|
|
1099
|
+
/**
|
|
1100
|
+
* Reads a sequence of bytes preceded by its length as a varint
|
|
1101
|
+
*/
|
|
1102
|
+
bytes() {
|
|
1103
|
+
const length = this.uint32();
|
|
1104
|
+
const start = this.pos;
|
|
1105
|
+
const end = this.pos + length;
|
|
1106
|
+
/* istanbul ignore if */
|
|
1107
|
+
if (end > this.len) throw indexOutOfRange(this, length);
|
|
1108
|
+
this.pos += length;
|
|
1109
|
+
return start === end ? new Uint8Array(0) : this.buf.subarray(start, end);
|
|
1110
|
+
}
|
|
1111
|
+
/**
|
|
1112
|
+
* Reads a string preceded by its byte length as a varint
|
|
1113
|
+
*/
|
|
1114
|
+
string() {
|
|
1115
|
+
const bytes = this.bytes();
|
|
1116
|
+
return read(bytes, 0, bytes.length);
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Skips the specified number of bytes if specified, otherwise skips a varint
|
|
1120
|
+
*/
|
|
1121
|
+
skip(length) {
|
|
1122
|
+
if (typeof length === "number") {
|
|
1123
|
+
/* istanbul ignore if */
|
|
1124
|
+
if (this.pos + length > this.len) throw indexOutOfRange(this, length);
|
|
1125
|
+
this.pos += length;
|
|
1126
|
+
} else do
|
|
1127
|
+
/* istanbul ignore if */
|
|
1128
|
+
if (this.pos >= this.len) throw indexOutOfRange(this);
|
|
1129
|
+
while ((this.buf[this.pos++] & 128) !== 0);
|
|
1130
|
+
return this;
|
|
1131
|
+
}
|
|
1132
|
+
/**
|
|
1133
|
+
* Skips the next element of the specified wire type
|
|
1134
|
+
*/
|
|
1135
|
+
skipType(wireType) {
|
|
1136
|
+
switch (wireType) {
|
|
1137
|
+
case 0:
|
|
1138
|
+
this.skip();
|
|
1139
|
+
break;
|
|
1140
|
+
case 1:
|
|
1141
|
+
this.skip(8);
|
|
1142
|
+
break;
|
|
1143
|
+
case 2:
|
|
1144
|
+
this.skip(this.uint32());
|
|
1145
|
+
break;
|
|
1146
|
+
case 3:
|
|
1147
|
+
while ((wireType = this.uint32() & 7) !== 4) this.skipType(wireType);
|
|
1148
|
+
break;
|
|
1149
|
+
case 5:
|
|
1150
|
+
this.skip(4);
|
|
1151
|
+
break;
|
|
1152
|
+
/* istanbul ignore next */
|
|
1153
|
+
default: throw Error(`invalid wire type ${wireType} at offset ${this.pos}`);
|
|
1154
|
+
}
|
|
1155
|
+
return this;
|
|
1156
|
+
}
|
|
1157
|
+
readLongVarint() {
|
|
1158
|
+
const bits = new LongBits(0, 0);
|
|
1159
|
+
let i = 0;
|
|
1160
|
+
if (this.len - this.pos > 4) {
|
|
1161
|
+
for (; i < 4; ++i) {
|
|
1162
|
+
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
|
|
1163
|
+
if (this.buf[this.pos++] < 128) return bits;
|
|
1164
|
+
}
|
|
1165
|
+
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0;
|
|
1166
|
+
bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0;
|
|
1167
|
+
if (this.buf[this.pos++] < 128) return bits;
|
|
1168
|
+
i = 0;
|
|
1169
|
+
} else {
|
|
1170
|
+
for (; i < 3; ++i) {
|
|
1171
|
+
/* istanbul ignore if */
|
|
1172
|
+
if (this.pos >= this.len) throw indexOutOfRange(this);
|
|
1173
|
+
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
|
|
1174
|
+
if (this.buf[this.pos++] < 128) return bits;
|
|
1175
|
+
}
|
|
1176
|
+
bits.lo = (bits.lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0;
|
|
1177
|
+
return bits;
|
|
1178
|
+
}
|
|
1179
|
+
if (this.len - this.pos > 4) for (; i < 5; ++i) {
|
|
1180
|
+
bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
|
|
1181
|
+
if (this.buf[this.pos++] < 128) return bits;
|
|
1182
|
+
}
|
|
1183
|
+
else for (; i < 5; ++i) {
|
|
1184
|
+
if (this.pos >= this.len) throw indexOutOfRange(this);
|
|
1185
|
+
bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
|
|
1186
|
+
if (this.buf[this.pos++] < 128) return bits;
|
|
1187
|
+
}
|
|
1188
|
+
throw Error("invalid varint encoding");
|
|
1189
|
+
}
|
|
1190
|
+
readFixed64() {
|
|
1191
|
+
if (this.pos + 8 > this.len) throw indexOutOfRange(this, 8);
|
|
1192
|
+
return new LongBits(readFixed32End(this.buf, this.pos += 4), readFixed32End(this.buf, this.pos += 4));
|
|
1193
|
+
}
|
|
1194
|
+
/**
|
|
1195
|
+
* Reads a varint as a signed 64 bit value
|
|
1196
|
+
*/
|
|
1197
|
+
int64() {
|
|
1198
|
+
return this.readLongVarint().toBigInt();
|
|
1199
|
+
}
|
|
1200
|
+
/**
|
|
1201
|
+
* Reads a varint as a signed 64 bit value returned as a possibly unsafe
|
|
1202
|
+
* JavaScript number
|
|
1203
|
+
*/
|
|
1204
|
+
int64Number() {
|
|
1205
|
+
return this.readLongVarint().toNumber();
|
|
1206
|
+
}
|
|
1207
|
+
/**
|
|
1208
|
+
* Reads a varint as a signed 64 bit value returned as a string
|
|
1209
|
+
*/
|
|
1210
|
+
int64String() {
|
|
1211
|
+
return this.readLongVarint().toString();
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Reads a varint as an unsigned 64 bit value
|
|
1215
|
+
*/
|
|
1216
|
+
uint64() {
|
|
1217
|
+
return this.readLongVarint().toBigInt(true);
|
|
1218
|
+
}
|
|
1219
|
+
/**
|
|
1220
|
+
* Reads a varint as an unsigned 64 bit value returned as a possibly unsafe
|
|
1221
|
+
* JavaScript number
|
|
1222
|
+
*/
|
|
1223
|
+
uint64Number() {
|
|
1224
|
+
const value = decodeUint8Array(this.buf, this.pos);
|
|
1225
|
+
this.pos += encodingLength(value);
|
|
1226
|
+
return value;
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Reads a varint as an unsigned 64 bit value returned as a string
|
|
1230
|
+
*/
|
|
1231
|
+
uint64String() {
|
|
1232
|
+
return this.readLongVarint().toString(true);
|
|
1233
|
+
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Reads a zig-zag encoded varint as a signed 64 bit value
|
|
1236
|
+
*/
|
|
1237
|
+
sint64() {
|
|
1238
|
+
return this.readLongVarint().zzDecode().toBigInt();
|
|
1239
|
+
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Reads a zig-zag encoded varint as a signed 64 bit value returned as a
|
|
1242
|
+
* possibly unsafe JavaScript number
|
|
1243
|
+
*/
|
|
1244
|
+
sint64Number() {
|
|
1245
|
+
return this.readLongVarint().zzDecode().toNumber();
|
|
1246
|
+
}
|
|
1247
|
+
/**
|
|
1248
|
+
* Reads a zig-zag encoded varint as a signed 64 bit value returned as a
|
|
1249
|
+
* string
|
|
1250
|
+
*/
|
|
1251
|
+
sint64String() {
|
|
1252
|
+
return this.readLongVarint().zzDecode().toString();
|
|
1253
|
+
}
|
|
1254
|
+
/**
|
|
1255
|
+
* Reads fixed 64 bits
|
|
1256
|
+
*/
|
|
1257
|
+
fixed64() {
|
|
1258
|
+
return this.readFixed64().toBigInt();
|
|
1259
|
+
}
|
|
1260
|
+
/**
|
|
1261
|
+
* Reads fixed 64 bits returned as a possibly unsafe JavaScript number
|
|
1262
|
+
*/
|
|
1263
|
+
fixed64Number() {
|
|
1264
|
+
return this.readFixed64().toNumber();
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
* Reads fixed 64 bits returned as a string
|
|
1268
|
+
*/
|
|
1269
|
+
fixed64String() {
|
|
1270
|
+
return this.readFixed64().toString();
|
|
1271
|
+
}
|
|
1272
|
+
/**
|
|
1273
|
+
* Reads zig-zag encoded fixed 64 bits
|
|
1274
|
+
*/
|
|
1275
|
+
sfixed64() {
|
|
1276
|
+
return this.readFixed64().toBigInt();
|
|
1277
|
+
}
|
|
1278
|
+
/**
|
|
1279
|
+
* Reads zig-zag encoded fixed 64 bits returned as a possibly unsafe
|
|
1280
|
+
* JavaScript number
|
|
1281
|
+
*/
|
|
1282
|
+
sfixed64Number() {
|
|
1283
|
+
return this.readFixed64().toNumber();
|
|
1284
|
+
}
|
|
1285
|
+
/**
|
|
1286
|
+
* Reads zig-zag encoded fixed 64 bits returned as a string
|
|
1287
|
+
*/
|
|
1288
|
+
sfixed64String() {
|
|
1289
|
+
return this.readFixed64().toString();
|
|
1290
|
+
}
|
|
1291
|
+
};
|
|
1292
|
+
function createReader(buf) {
|
|
1293
|
+
return new Uint8ArrayReader(buf instanceof Uint8Array ? buf : buf.subarray());
|
|
1294
|
+
}
|
|
1295
|
+
//#endregion
|
|
1296
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/decode.js
|
|
1297
|
+
function decodeMessage(buf, codec, opts) {
|
|
1298
|
+
const reader = createReader(buf);
|
|
1299
|
+
return codec.decode(reader, void 0, opts);
|
|
1300
|
+
}
|
|
1301
|
+
//#endregion
|
|
1302
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/utils/pool.js
|
|
1303
|
+
/**
|
|
1304
|
+
* A general purpose buffer pool
|
|
1305
|
+
*/
|
|
1306
|
+
function pool(size) {
|
|
1307
|
+
const SIZE = size ?? 8192;
|
|
1308
|
+
const MAX = SIZE >>> 1;
|
|
1309
|
+
let slab;
|
|
1310
|
+
let offset = SIZE;
|
|
1311
|
+
return function poolAlloc(size) {
|
|
1312
|
+
if (size < 1 || size > MAX) return allocUnsafe(size);
|
|
1313
|
+
if (offset + size > SIZE) {
|
|
1314
|
+
slab = allocUnsafe(SIZE);
|
|
1315
|
+
offset = 0;
|
|
1316
|
+
}
|
|
1317
|
+
const buf = slab.subarray(offset, offset += size);
|
|
1318
|
+
if ((offset & 7) !== 0) offset = (offset | 7) + 1;
|
|
1319
|
+
return buf;
|
|
1320
|
+
};
|
|
1321
|
+
}
|
|
1322
|
+
//#endregion
|
|
1323
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/utils/writer.js
|
|
1324
|
+
/**
|
|
1325
|
+
* Constructs a new writer operation instance.
|
|
1326
|
+
*
|
|
1327
|
+
* @classdesc Scheduled writer operation
|
|
1328
|
+
*/
|
|
1329
|
+
var Op = class {
|
|
1330
|
+
/**
|
|
1331
|
+
* Function to call
|
|
1332
|
+
*/
|
|
1333
|
+
fn;
|
|
1334
|
+
/**
|
|
1335
|
+
* Value byte length
|
|
1336
|
+
*/
|
|
1337
|
+
len;
|
|
1338
|
+
/**
|
|
1339
|
+
* Next operation
|
|
1340
|
+
*/
|
|
1341
|
+
next;
|
|
1342
|
+
/**
|
|
1343
|
+
* Value to write
|
|
1344
|
+
*/
|
|
1345
|
+
val;
|
|
1346
|
+
constructor(fn, len, val) {
|
|
1347
|
+
this.fn = fn;
|
|
1348
|
+
this.len = len;
|
|
1349
|
+
this.next = void 0;
|
|
1350
|
+
this.val = val;
|
|
1351
|
+
}
|
|
1352
|
+
};
|
|
1353
|
+
/* istanbul ignore next */
|
|
1354
|
+
function noop() {}
|
|
1355
|
+
/**
|
|
1356
|
+
* Constructs a new writer state instance
|
|
1357
|
+
*/
|
|
1358
|
+
var State = class {
|
|
1359
|
+
/**
|
|
1360
|
+
* Current head
|
|
1361
|
+
*/
|
|
1362
|
+
head;
|
|
1363
|
+
/**
|
|
1364
|
+
* Current tail
|
|
1365
|
+
*/
|
|
1366
|
+
tail;
|
|
1367
|
+
/**
|
|
1368
|
+
* Current buffer length
|
|
1369
|
+
*/
|
|
1370
|
+
len;
|
|
1371
|
+
/**
|
|
1372
|
+
* Next state
|
|
1373
|
+
*/
|
|
1374
|
+
next;
|
|
1375
|
+
constructor(writer) {
|
|
1376
|
+
this.head = writer.head;
|
|
1377
|
+
this.tail = writer.tail;
|
|
1378
|
+
this.len = writer.len;
|
|
1379
|
+
this.next = writer.states;
|
|
1380
|
+
}
|
|
1381
|
+
};
|
|
1382
|
+
const bufferPool = pool();
|
|
1383
|
+
/**
|
|
1384
|
+
* Allocates a buffer of the specified size
|
|
1385
|
+
*/
|
|
1386
|
+
function alloc$1(size) {
|
|
1387
|
+
if (globalThis.Buffer != null) return allocUnsafe(size);
|
|
1388
|
+
return bufferPool(size);
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* When a value is written, the writer calculates its byte length and puts it into a linked
|
|
1392
|
+
* list of operations to perform when finish() is called. This both allows us to allocate
|
|
1393
|
+
* buffers of the exact required size and reduces the amount of work we have to do compared
|
|
1394
|
+
* to first calculating over objects and then encoding over objects. In our case, the encoding
|
|
1395
|
+
* part is just a linked list walk calling operations with already prepared values.
|
|
1396
|
+
*/
|
|
1397
|
+
var Uint8ArrayWriter = class {
|
|
1398
|
+
/**
|
|
1399
|
+
* Current length
|
|
1400
|
+
*/
|
|
1401
|
+
len;
|
|
1402
|
+
/**
|
|
1403
|
+
* Operations head
|
|
1404
|
+
*/
|
|
1405
|
+
head;
|
|
1406
|
+
/**
|
|
1407
|
+
* Operations tail
|
|
1408
|
+
*/
|
|
1409
|
+
tail;
|
|
1410
|
+
/**
|
|
1411
|
+
* Linked forked states
|
|
1412
|
+
*/
|
|
1413
|
+
states;
|
|
1414
|
+
constructor() {
|
|
1415
|
+
this.len = 0;
|
|
1416
|
+
this.head = new Op(noop, 0, 0);
|
|
1417
|
+
this.tail = this.head;
|
|
1418
|
+
this.states = null;
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Pushes a new operation to the queue
|
|
1422
|
+
*/
|
|
1423
|
+
_push(fn, len, val) {
|
|
1424
|
+
this.tail = this.tail.next = new Op(fn, len, val);
|
|
1425
|
+
this.len += len;
|
|
1426
|
+
return this;
|
|
1427
|
+
}
|
|
1428
|
+
/**
|
|
1429
|
+
* Writes an unsigned 32 bit value as a varint
|
|
1430
|
+
*/
|
|
1431
|
+
uint32(value) {
|
|
1432
|
+
this.len += (this.tail = this.tail.next = new VarintOp((value = value >>> 0) < 128 ? 1 : value < 16384 ? 2 : value < 2097152 ? 3 : value < 268435456 ? 4 : 5, value)).len;
|
|
1433
|
+
return this;
|
|
1434
|
+
}
|
|
1435
|
+
/**
|
|
1436
|
+
* Writes a signed 32 bit value as a varint`
|
|
1437
|
+
*/
|
|
1438
|
+
int32(value) {
|
|
1439
|
+
return value < 0 ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) : this.uint32(value);
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* Writes a 32 bit value as a varint, zig-zag encoded
|
|
1443
|
+
*/
|
|
1444
|
+
sint32(value) {
|
|
1445
|
+
return this.uint32((value << 1 ^ value >> 31) >>> 0);
|
|
1446
|
+
}
|
|
1447
|
+
/**
|
|
1448
|
+
* Writes an unsigned 64 bit value as a varint
|
|
1449
|
+
*/
|
|
1450
|
+
uint64(value) {
|
|
1451
|
+
const bits = LongBits.fromBigInt(value);
|
|
1452
|
+
return this._push(writeVarint64, bits.length(), bits);
|
|
1453
|
+
}
|
|
1454
|
+
/**
|
|
1455
|
+
* Writes an unsigned 64 bit value as a varint
|
|
1456
|
+
*/
|
|
1457
|
+
uint64Number(value) {
|
|
1458
|
+
return this._push(encodeUint8Array, encodingLength(value), value);
|
|
1459
|
+
}
|
|
1460
|
+
/**
|
|
1461
|
+
* Writes an unsigned 64 bit value as a varint
|
|
1462
|
+
*/
|
|
1463
|
+
uint64String(value) {
|
|
1464
|
+
return this.uint64(BigInt(value));
|
|
1465
|
+
}
|
|
1466
|
+
/**
|
|
1467
|
+
* Writes a signed 64 bit value as a varint
|
|
1468
|
+
*/
|
|
1469
|
+
int64(value) {
|
|
1470
|
+
return this.uint64(value);
|
|
1471
|
+
}
|
|
1472
|
+
/**
|
|
1473
|
+
* Writes a signed 64 bit value as a varint
|
|
1474
|
+
*/
|
|
1475
|
+
int64Number(value) {
|
|
1476
|
+
return this.uint64Number(value);
|
|
1477
|
+
}
|
|
1478
|
+
/**
|
|
1479
|
+
* Writes a signed 64 bit value as a varint
|
|
1480
|
+
*/
|
|
1481
|
+
int64String(value) {
|
|
1482
|
+
return this.uint64String(value);
|
|
1483
|
+
}
|
|
1484
|
+
/**
|
|
1485
|
+
* Writes a signed 64 bit value as a varint, zig-zag encoded
|
|
1486
|
+
*/
|
|
1487
|
+
sint64(value) {
|
|
1488
|
+
const bits = LongBits.fromBigInt(value).zzEncode();
|
|
1489
|
+
return this._push(writeVarint64, bits.length(), bits);
|
|
1490
|
+
}
|
|
1491
|
+
/**
|
|
1492
|
+
* Writes a signed 64 bit value as a varint, zig-zag encoded
|
|
1493
|
+
*/
|
|
1494
|
+
sint64Number(value) {
|
|
1495
|
+
const bits = LongBits.fromNumber(value).zzEncode();
|
|
1496
|
+
return this._push(writeVarint64, bits.length(), bits);
|
|
1497
|
+
}
|
|
1498
|
+
/**
|
|
1499
|
+
* Writes a signed 64 bit value as a varint, zig-zag encoded
|
|
1500
|
+
*/
|
|
1501
|
+
sint64String(value) {
|
|
1502
|
+
return this.sint64(BigInt(value));
|
|
1503
|
+
}
|
|
1504
|
+
/**
|
|
1505
|
+
* Writes a boolish value as a varint
|
|
1506
|
+
*/
|
|
1507
|
+
bool(value) {
|
|
1508
|
+
return this._push(writeByte, 1, value ? 1 : 0);
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Writes an unsigned 32 bit value as fixed 32 bits
|
|
1512
|
+
*/
|
|
1513
|
+
fixed32(value) {
|
|
1514
|
+
return this._push(writeFixed32, 4, value >>> 0);
|
|
1515
|
+
}
|
|
1516
|
+
/**
|
|
1517
|
+
* Writes a signed 32 bit value as fixed 32 bits
|
|
1518
|
+
*/
|
|
1519
|
+
sfixed32(value) {
|
|
1520
|
+
return this.fixed32(value);
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* Writes an unsigned 64 bit value as fixed 64 bits
|
|
1524
|
+
*/
|
|
1525
|
+
fixed64(value) {
|
|
1526
|
+
const bits = LongBits.fromBigInt(value);
|
|
1527
|
+
return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* Writes an unsigned 64 bit value as fixed 64 bits
|
|
1531
|
+
*/
|
|
1532
|
+
fixed64Number(value) {
|
|
1533
|
+
const bits = LongBits.fromNumber(value);
|
|
1534
|
+
return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
|
|
1535
|
+
}
|
|
1536
|
+
/**
|
|
1537
|
+
* Writes an unsigned 64 bit value as fixed 64 bits
|
|
1538
|
+
*/
|
|
1539
|
+
fixed64String(value) {
|
|
1540
|
+
return this.fixed64(BigInt(value));
|
|
1541
|
+
}
|
|
1542
|
+
/**
|
|
1543
|
+
* Writes a signed 64 bit value as fixed 64 bits
|
|
1544
|
+
*/
|
|
1545
|
+
sfixed64(value) {
|
|
1546
|
+
return this.fixed64(value);
|
|
1547
|
+
}
|
|
1548
|
+
/**
|
|
1549
|
+
* Writes a signed 64 bit value as fixed 64 bits
|
|
1550
|
+
*/
|
|
1551
|
+
sfixed64Number(value) {
|
|
1552
|
+
return this.fixed64Number(value);
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* Writes a signed 64 bit value as fixed 64 bits
|
|
1556
|
+
*/
|
|
1557
|
+
sfixed64String(value) {
|
|
1558
|
+
return this.fixed64String(value);
|
|
1559
|
+
}
|
|
1560
|
+
/**
|
|
1561
|
+
* Writes a float (32 bit)
|
|
1562
|
+
*/
|
|
1563
|
+
float(value) {
|
|
1564
|
+
return this._push(writeFloatLE, 4, value);
|
|
1565
|
+
}
|
|
1566
|
+
/**
|
|
1567
|
+
* Writes a double (64 bit float).
|
|
1568
|
+
*
|
|
1569
|
+
* @function
|
|
1570
|
+
* @param {number} value - Value to write
|
|
1571
|
+
* @returns {Writer} `this`
|
|
1572
|
+
*/
|
|
1573
|
+
double(value) {
|
|
1574
|
+
return this._push(writeDoubleLE, 8, value);
|
|
1575
|
+
}
|
|
1576
|
+
/**
|
|
1577
|
+
* Writes a sequence of bytes
|
|
1578
|
+
*/
|
|
1579
|
+
bytes(value) {
|
|
1580
|
+
const len = value.length >>> 0;
|
|
1581
|
+
if (len === 0) return this._push(writeByte, 1, 0);
|
|
1582
|
+
return this.uint32(len)._push(writeBytes, len, value);
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* Writes a string
|
|
1586
|
+
*/
|
|
1587
|
+
string(value) {
|
|
1588
|
+
const len = length(value);
|
|
1589
|
+
return len !== 0 ? this.uint32(len)._push(write, len, value) : this._push(writeByte, 1, 0);
|
|
1590
|
+
}
|
|
1591
|
+
/**
|
|
1592
|
+
* Forks this writer's state by pushing it to a stack.
|
|
1593
|
+
* Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
|
|
1594
|
+
*/
|
|
1595
|
+
fork() {
|
|
1596
|
+
this.states = new State(this);
|
|
1597
|
+
this.head = this.tail = new Op(noop, 0, 0);
|
|
1598
|
+
this.len = 0;
|
|
1599
|
+
return this;
|
|
1600
|
+
}
|
|
1601
|
+
/**
|
|
1602
|
+
* Resets this instance to the last state
|
|
1603
|
+
*/
|
|
1604
|
+
reset() {
|
|
1605
|
+
if (this.states != null) {
|
|
1606
|
+
this.head = this.states.head;
|
|
1607
|
+
this.tail = this.states.tail;
|
|
1608
|
+
this.len = this.states.len;
|
|
1609
|
+
this.states = this.states.next;
|
|
1610
|
+
} else {
|
|
1611
|
+
this.head = this.tail = new Op(noop, 0, 0);
|
|
1612
|
+
this.len = 0;
|
|
1613
|
+
}
|
|
1614
|
+
return this;
|
|
1615
|
+
}
|
|
1616
|
+
/**
|
|
1617
|
+
* Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
|
|
1618
|
+
*/
|
|
1619
|
+
ldelim() {
|
|
1620
|
+
const head = this.head;
|
|
1621
|
+
const tail = this.tail;
|
|
1622
|
+
const len = this.len;
|
|
1623
|
+
this.reset().uint32(len);
|
|
1624
|
+
if (len !== 0) {
|
|
1625
|
+
this.tail.next = head.next;
|
|
1626
|
+
this.tail = tail;
|
|
1627
|
+
this.len += len;
|
|
1628
|
+
}
|
|
1629
|
+
return this;
|
|
1630
|
+
}
|
|
1631
|
+
/**
|
|
1632
|
+
* Finishes the write operation
|
|
1633
|
+
*/
|
|
1634
|
+
finish() {
|
|
1635
|
+
let head = this.head.next;
|
|
1636
|
+
const buf = alloc$1(this.len);
|
|
1637
|
+
let pos = 0;
|
|
1638
|
+
while (head != null) {
|
|
1639
|
+
head.fn(head.val, buf, pos);
|
|
1640
|
+
pos += head.len;
|
|
1641
|
+
head = head.next;
|
|
1642
|
+
}
|
|
1643
|
+
return buf;
|
|
1644
|
+
}
|
|
1645
|
+
};
|
|
1646
|
+
function writeByte(val, buf, pos) {
|
|
1647
|
+
buf[pos] = val & 255;
|
|
1648
|
+
}
|
|
1649
|
+
function writeVarint32(val, buf, pos) {
|
|
1650
|
+
while (val > 127) {
|
|
1651
|
+
buf[pos++] = val & 127 | 128;
|
|
1652
|
+
val >>>= 7;
|
|
1653
|
+
}
|
|
1654
|
+
buf[pos] = val;
|
|
1655
|
+
}
|
|
1656
|
+
/**
|
|
1657
|
+
* Constructs a new varint writer operation instance.
|
|
1658
|
+
*
|
|
1659
|
+
* @classdesc Scheduled varint writer operation
|
|
1660
|
+
*/
|
|
1661
|
+
var VarintOp = class extends Op {
|
|
1662
|
+
next;
|
|
1663
|
+
constructor(len, val) {
|
|
1664
|
+
super(writeVarint32, len, val);
|
|
1665
|
+
this.next = void 0;
|
|
1666
|
+
}
|
|
1667
|
+
};
|
|
1668
|
+
function writeVarint64(val, buf, pos) {
|
|
1669
|
+
while (val.hi !== 0) {
|
|
1670
|
+
buf[pos++] = val.lo & 127 | 128;
|
|
1671
|
+
val.lo = (val.lo >>> 7 | val.hi << 25) >>> 0;
|
|
1672
|
+
val.hi >>>= 7;
|
|
1673
|
+
}
|
|
1674
|
+
while (val.lo > 127) {
|
|
1675
|
+
buf[pos++] = val.lo & 127 | 128;
|
|
1676
|
+
val.lo = val.lo >>> 7;
|
|
1677
|
+
}
|
|
1678
|
+
buf[pos++] = val.lo;
|
|
1679
|
+
}
|
|
1680
|
+
function writeFixed32(val, buf, pos) {
|
|
1681
|
+
buf[pos] = val & 255;
|
|
1682
|
+
buf[pos + 1] = val >>> 8 & 255;
|
|
1683
|
+
buf[pos + 2] = val >>> 16 & 255;
|
|
1684
|
+
buf[pos + 3] = val >>> 24;
|
|
1685
|
+
}
|
|
1686
|
+
function writeBytes(val, buf, pos) {
|
|
1687
|
+
buf.set(val, pos);
|
|
1688
|
+
}
|
|
1689
|
+
if (globalThis.Buffer != null) {
|
|
1690
|
+
Uint8ArrayWriter.prototype.bytes = function(value) {
|
|
1691
|
+
const len = value.length >>> 0;
|
|
1692
|
+
this.uint32(len);
|
|
1693
|
+
if (len > 0) this._push(writeBytesBuffer, len, value);
|
|
1694
|
+
return this;
|
|
1695
|
+
};
|
|
1696
|
+
Uint8ArrayWriter.prototype.string = function(value) {
|
|
1697
|
+
const len = globalThis.Buffer.byteLength(value);
|
|
1698
|
+
this.uint32(len);
|
|
1699
|
+
if (len > 0) this._push(writeStringBuffer, len, value);
|
|
1700
|
+
return this;
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1703
|
+
function writeBytesBuffer(val, buf, pos) {
|
|
1704
|
+
buf.set(val, pos);
|
|
1705
|
+
}
|
|
1706
|
+
function writeStringBuffer(val, buf, pos) {
|
|
1707
|
+
if (val.length < 40) write(val, buf, pos);
|
|
1708
|
+
else if (buf.utf8Write != null) buf.utf8Write(val, pos);
|
|
1709
|
+
else buf.set(fromString(val), pos);
|
|
1710
|
+
}
|
|
1711
|
+
/**
|
|
1712
|
+
* Creates a new writer
|
|
1713
|
+
*/
|
|
1714
|
+
function createWriter() {
|
|
1715
|
+
return new Uint8ArrayWriter();
|
|
1716
|
+
}
|
|
1717
|
+
//#endregion
|
|
1718
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/encode.js
|
|
1719
|
+
function encodeMessage(message, codec) {
|
|
1720
|
+
const w = createWriter();
|
|
1721
|
+
codec.encode(message, w, { lengthDelimited: false });
|
|
1722
|
+
return w.finish();
|
|
1723
|
+
}
|
|
1724
|
+
//#endregion
|
|
1725
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/stream.js
|
|
1726
|
+
function* streamMessage(buf, codec, opts) {
|
|
1727
|
+
const reader = createReader(buf);
|
|
1728
|
+
yield* codec.stream(reader, void 0, "$", opts);
|
|
1729
|
+
}
|
|
1730
|
+
//#endregion
|
|
1731
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/codec.js
|
|
1732
|
+
const CODEC_TYPES = {
|
|
1733
|
+
VARINT: 0,
|
|
1734
|
+
BIT64: 1,
|
|
1735
|
+
LENGTH_DELIMITED: 2,
|
|
1736
|
+
START_GROUP: 3,
|
|
1737
|
+
END_GROUP: 4,
|
|
1738
|
+
BIT32: 5
|
|
1739
|
+
};
|
|
1740
|
+
function createCodec(name, type, encode, decode, stream) {
|
|
1741
|
+
return {
|
|
1742
|
+
name,
|
|
1743
|
+
type,
|
|
1744
|
+
encode,
|
|
1745
|
+
decode,
|
|
1746
|
+
stream
|
|
1747
|
+
};
|
|
1748
|
+
}
|
|
1749
|
+
//#endregion
|
|
1750
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/codecs/enum.js
|
|
1751
|
+
function enumeration(v) {
|
|
1752
|
+
function findValue(val) {
|
|
1753
|
+
if (v[val.toString()] == null) throw new Error("Invalid enum value");
|
|
1754
|
+
return v[val];
|
|
1755
|
+
}
|
|
1756
|
+
return createCodec("enum", CODEC_TYPES.VARINT, function enumEncode(val, writer) {
|
|
1757
|
+
const enumValue = findValue(val);
|
|
1758
|
+
writer.int32(enumValue);
|
|
1759
|
+
}, function enumDecode(reader) {
|
|
1760
|
+
return findValue(reader.int32());
|
|
1761
|
+
}, function* enumStream(reader) {
|
|
1762
|
+
yield findValue(reader.int32());
|
|
1763
|
+
});
|
|
1764
|
+
}
|
|
1765
|
+
//#endregion
|
|
1766
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/codecs/message.js
|
|
1767
|
+
function message(encode, decode, stream) {
|
|
1768
|
+
return createCodec("message", CODEC_TYPES.LENGTH_DELIMITED, encode, decode, stream);
|
|
1769
|
+
}
|
|
1770
|
+
//#endregion
|
|
1771
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/protons-runtime/dist/src/index.js
|
|
1772
|
+
/**
|
|
1773
|
+
* Thrown when a repeated field has too many elements
|
|
1774
|
+
*/
|
|
1775
|
+
var MaxLengthError = class extends Error {
|
|
1776
|
+
/**
|
|
1777
|
+
* This will be removed in a future release
|
|
1778
|
+
*
|
|
1779
|
+
* @deprecated use the `.name` property instead
|
|
1780
|
+
*/
|
|
1781
|
+
code = "ERR_MAX_LENGTH";
|
|
1782
|
+
name = "MaxLengthError";
|
|
1783
|
+
};
|
|
1784
|
+
//#endregion
|
|
1785
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/ipfs-unixfs/dist/src/unixfs.js
|
|
1786
|
+
var Data;
|
|
1787
|
+
(function(Data) {
|
|
1788
|
+
(function(DataType) {
|
|
1789
|
+
DataType["Raw"] = "Raw";
|
|
1790
|
+
DataType["Directory"] = "Directory";
|
|
1791
|
+
DataType["File"] = "File";
|
|
1792
|
+
DataType["Metadata"] = "Metadata";
|
|
1793
|
+
DataType["Symlink"] = "Symlink";
|
|
1794
|
+
DataType["HAMTShard"] = "HAMTShard";
|
|
1795
|
+
})(Data.DataType || (Data.DataType = {}));
|
|
1796
|
+
let __DataTypeValues;
|
|
1797
|
+
(function(__DataTypeValues) {
|
|
1798
|
+
__DataTypeValues[__DataTypeValues["Raw"] = 0] = "Raw";
|
|
1799
|
+
__DataTypeValues[__DataTypeValues["Directory"] = 1] = "Directory";
|
|
1800
|
+
__DataTypeValues[__DataTypeValues["File"] = 2] = "File";
|
|
1801
|
+
__DataTypeValues[__DataTypeValues["Metadata"] = 3] = "Metadata";
|
|
1802
|
+
__DataTypeValues[__DataTypeValues["Symlink"] = 4] = "Symlink";
|
|
1803
|
+
__DataTypeValues[__DataTypeValues["HAMTShard"] = 5] = "HAMTShard";
|
|
1804
|
+
})(__DataTypeValues || (__DataTypeValues = {}));
|
|
1805
|
+
(function(DataType) {
|
|
1806
|
+
DataType.codec = () => {
|
|
1807
|
+
return enumeration(__DataTypeValues);
|
|
1808
|
+
};
|
|
1809
|
+
})(Data.DataType || (Data.DataType = {}));
|
|
1810
|
+
let _codec;
|
|
1811
|
+
Data.codec = () => {
|
|
1812
|
+
if (_codec == null) _codec = message((obj, w, opts = {}) => {
|
|
1813
|
+
if (opts.lengthDelimited !== false) w.fork();
|
|
1814
|
+
if (obj.Type != null) {
|
|
1815
|
+
w.uint32(8);
|
|
1816
|
+
Data.DataType.codec().encode(obj.Type, w);
|
|
1817
|
+
}
|
|
1818
|
+
if (obj.Data != null) {
|
|
1819
|
+
w.uint32(18);
|
|
1820
|
+
w.bytes(obj.Data);
|
|
1821
|
+
}
|
|
1822
|
+
if (obj.filesize != null) {
|
|
1823
|
+
w.uint32(24);
|
|
1824
|
+
w.uint64(obj.filesize);
|
|
1825
|
+
}
|
|
1826
|
+
if (obj.blocksizes != null && obj.blocksizes.length > 0) for (const value of obj.blocksizes) {
|
|
1827
|
+
w.uint32(32);
|
|
1828
|
+
w.uint64(value);
|
|
1829
|
+
}
|
|
1830
|
+
if (obj.hashType != null) {
|
|
1831
|
+
w.uint32(40);
|
|
1832
|
+
w.uint64(obj.hashType);
|
|
1833
|
+
}
|
|
1834
|
+
if (obj.fanout != null) {
|
|
1835
|
+
w.uint32(48);
|
|
1836
|
+
w.uint64(obj.fanout);
|
|
1837
|
+
}
|
|
1838
|
+
if (obj.mode != null) {
|
|
1839
|
+
w.uint32(56);
|
|
1840
|
+
w.uint32(obj.mode);
|
|
1841
|
+
}
|
|
1842
|
+
if (obj.mtime != null) {
|
|
1843
|
+
w.uint32(66);
|
|
1844
|
+
UnixTime.codec().encode(obj.mtime, w);
|
|
1845
|
+
}
|
|
1846
|
+
if (opts.lengthDelimited !== false) w.ldelim();
|
|
1847
|
+
}, (reader, length, opts = {}) => {
|
|
1848
|
+
const obj = { blocksizes: [] };
|
|
1849
|
+
const end = length == null ? reader.len : reader.pos + length;
|
|
1850
|
+
while (reader.pos < end) {
|
|
1851
|
+
const tag = reader.uint32();
|
|
1852
|
+
switch (tag >>> 3) {
|
|
1853
|
+
case 1:
|
|
1854
|
+
obj.Type = Data.DataType.codec().decode(reader);
|
|
1855
|
+
break;
|
|
1856
|
+
case 2:
|
|
1857
|
+
obj.Data = reader.bytes();
|
|
1858
|
+
break;
|
|
1859
|
+
case 3:
|
|
1860
|
+
obj.filesize = reader.uint64();
|
|
1861
|
+
break;
|
|
1862
|
+
case 4:
|
|
1863
|
+
if (opts.limits?.blocksizes != null && obj.blocksizes.length === opts.limits.blocksizes) throw new MaxLengthError("Decode error - repeated field \"blocksizes\" had too many elements");
|
|
1864
|
+
obj.blocksizes.push(reader.uint64());
|
|
1865
|
+
break;
|
|
1866
|
+
case 5:
|
|
1867
|
+
obj.hashType = reader.uint64();
|
|
1868
|
+
break;
|
|
1869
|
+
case 6:
|
|
1870
|
+
obj.fanout = reader.uint64();
|
|
1871
|
+
break;
|
|
1872
|
+
case 7:
|
|
1873
|
+
obj.mode = reader.uint32();
|
|
1874
|
+
break;
|
|
1875
|
+
case 8:
|
|
1876
|
+
obj.mtime = UnixTime.codec().decode(reader, reader.uint32(), { limits: opts.limits?.mtime });
|
|
1877
|
+
break;
|
|
1878
|
+
default:
|
|
1879
|
+
reader.skipType(tag & 7);
|
|
1880
|
+
break;
|
|
1881
|
+
}
|
|
1882
|
+
}
|
|
1883
|
+
return obj;
|
|
1884
|
+
}, function* (reader, length, prefix, opts = {}) {
|
|
1885
|
+
const obj = { blocksizes: 0 };
|
|
1886
|
+
const end = length == null ? reader.len : reader.pos + length;
|
|
1887
|
+
while (reader.pos < end) {
|
|
1888
|
+
const tag = reader.uint32();
|
|
1889
|
+
switch (tag >>> 3) {
|
|
1890
|
+
case 1:
|
|
1891
|
+
yield {
|
|
1892
|
+
field: `${prefix}.Type`,
|
|
1893
|
+
value: Data.DataType.codec().decode(reader)
|
|
1894
|
+
};
|
|
1895
|
+
break;
|
|
1896
|
+
case 2:
|
|
1897
|
+
yield {
|
|
1898
|
+
field: `${prefix}.Data`,
|
|
1899
|
+
value: reader.bytes()
|
|
1900
|
+
};
|
|
1901
|
+
break;
|
|
1902
|
+
case 3:
|
|
1903
|
+
yield {
|
|
1904
|
+
field: `${prefix}.filesize`,
|
|
1905
|
+
value: reader.uint64()
|
|
1906
|
+
};
|
|
1907
|
+
break;
|
|
1908
|
+
case 4:
|
|
1909
|
+
if (opts.limits?.blocksizes != null && obj.blocksizes === opts.limits.blocksizes) throw new MaxLengthError("Streaming decode error - repeated field \"blocksizes\" had too many elements");
|
|
1910
|
+
yield {
|
|
1911
|
+
field: `${prefix}.blocksizes[]`,
|
|
1912
|
+
index: obj.blocksizes,
|
|
1913
|
+
value: reader.uint64()
|
|
1914
|
+
};
|
|
1915
|
+
obj.blocksizes++;
|
|
1916
|
+
break;
|
|
1917
|
+
case 5:
|
|
1918
|
+
yield {
|
|
1919
|
+
field: `${prefix}.hashType`,
|
|
1920
|
+
value: reader.uint64()
|
|
1921
|
+
};
|
|
1922
|
+
break;
|
|
1923
|
+
case 6:
|
|
1924
|
+
yield {
|
|
1925
|
+
field: `${prefix}.fanout`,
|
|
1926
|
+
value: reader.uint64()
|
|
1927
|
+
};
|
|
1928
|
+
break;
|
|
1929
|
+
case 7:
|
|
1930
|
+
yield {
|
|
1931
|
+
field: `${prefix}.mode`,
|
|
1932
|
+
value: reader.uint32()
|
|
1933
|
+
};
|
|
1934
|
+
break;
|
|
1935
|
+
case 8:
|
|
1936
|
+
yield* UnixTime.codec().stream(reader, reader.uint32(), `${prefix}.mtime`, { limits: opts.limits?.mtime });
|
|
1937
|
+
break;
|
|
1938
|
+
default:
|
|
1939
|
+
reader.skipType(tag & 7);
|
|
1940
|
+
break;
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
});
|
|
1944
|
+
return _codec;
|
|
1945
|
+
};
|
|
1946
|
+
function encode(obj) {
|
|
1947
|
+
return encodeMessage(obj, Data.codec());
|
|
1948
|
+
}
|
|
1949
|
+
Data.encode = encode;
|
|
1950
|
+
function decode(buf, opts) {
|
|
1951
|
+
return decodeMessage(buf, Data.codec(), opts);
|
|
1952
|
+
}
|
|
1953
|
+
Data.decode = decode;
|
|
1954
|
+
function stream(buf, opts) {
|
|
1955
|
+
return streamMessage(buf, Data.codec(), opts);
|
|
1956
|
+
}
|
|
1957
|
+
Data.stream = stream;
|
|
1958
|
+
})(Data || (Data = {}));
|
|
1959
|
+
var UnixTime;
|
|
1960
|
+
(function(UnixTime) {
|
|
1961
|
+
let _codec;
|
|
1962
|
+
UnixTime.codec = () => {
|
|
1963
|
+
if (_codec == null) _codec = message((obj, w, opts = {}) => {
|
|
1964
|
+
if (opts.lengthDelimited !== false) w.fork();
|
|
1965
|
+
if (obj.Seconds != null) {
|
|
1966
|
+
w.uint32(8);
|
|
1967
|
+
w.int64(obj.Seconds);
|
|
1968
|
+
}
|
|
1969
|
+
if (obj.FractionalNanoseconds != null) {
|
|
1970
|
+
w.uint32(21);
|
|
1971
|
+
w.fixed32(obj.FractionalNanoseconds);
|
|
1972
|
+
}
|
|
1973
|
+
if (opts.lengthDelimited !== false) w.ldelim();
|
|
1974
|
+
}, (reader, length, opts = {}) => {
|
|
1975
|
+
const obj = {};
|
|
1976
|
+
const end = length == null ? reader.len : reader.pos + length;
|
|
1977
|
+
while (reader.pos < end) {
|
|
1978
|
+
const tag = reader.uint32();
|
|
1979
|
+
switch (tag >>> 3) {
|
|
1980
|
+
case 1:
|
|
1981
|
+
obj.Seconds = reader.int64();
|
|
1982
|
+
break;
|
|
1983
|
+
case 2:
|
|
1984
|
+
obj.FractionalNanoseconds = reader.fixed32();
|
|
1985
|
+
break;
|
|
1986
|
+
default:
|
|
1987
|
+
reader.skipType(tag & 7);
|
|
1988
|
+
break;
|
|
1989
|
+
}
|
|
1990
|
+
}
|
|
1991
|
+
return obj;
|
|
1992
|
+
}, function* (reader, length, prefix, opts = {}) {
|
|
1993
|
+
const end = length == null ? reader.len : reader.pos + length;
|
|
1994
|
+
while (reader.pos < end) {
|
|
1995
|
+
const tag = reader.uint32();
|
|
1996
|
+
switch (tag >>> 3) {
|
|
1997
|
+
case 1:
|
|
1998
|
+
yield {
|
|
1999
|
+
field: `${prefix}.Seconds`,
|
|
2000
|
+
value: reader.int64()
|
|
2001
|
+
};
|
|
2002
|
+
break;
|
|
2003
|
+
case 2:
|
|
2004
|
+
yield {
|
|
2005
|
+
field: `${prefix}.FractionalNanoseconds`,
|
|
2006
|
+
value: reader.fixed32()
|
|
2007
|
+
};
|
|
2008
|
+
break;
|
|
2009
|
+
default:
|
|
2010
|
+
reader.skipType(tag & 7);
|
|
2011
|
+
break;
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
});
|
|
2015
|
+
return _codec;
|
|
2016
|
+
};
|
|
2017
|
+
function encode(obj) {
|
|
2018
|
+
return encodeMessage(obj, UnixTime.codec());
|
|
2019
|
+
}
|
|
2020
|
+
UnixTime.encode = encode;
|
|
2021
|
+
function decode(buf, opts) {
|
|
2022
|
+
return decodeMessage(buf, UnixTime.codec(), opts);
|
|
2023
|
+
}
|
|
2024
|
+
UnixTime.decode = decode;
|
|
2025
|
+
function stream(buf, opts) {
|
|
2026
|
+
return streamMessage(buf, UnixTime.codec(), opts);
|
|
2027
|
+
}
|
|
2028
|
+
UnixTime.stream = stream;
|
|
2029
|
+
})(UnixTime || (UnixTime = {}));
|
|
2030
|
+
var Metadata;
|
|
2031
|
+
(function(Metadata) {
|
|
2032
|
+
let _codec;
|
|
2033
|
+
Metadata.codec = () => {
|
|
2034
|
+
if (_codec == null) _codec = message((obj, w, opts = {}) => {
|
|
2035
|
+
if (opts.lengthDelimited !== false) w.fork();
|
|
2036
|
+
if (obj.MimeType != null) {
|
|
2037
|
+
w.uint32(10);
|
|
2038
|
+
w.string(obj.MimeType);
|
|
2039
|
+
}
|
|
2040
|
+
if (opts.lengthDelimited !== false) w.ldelim();
|
|
2041
|
+
}, (reader, length, opts = {}) => {
|
|
2042
|
+
const obj = {};
|
|
2043
|
+
const end = length == null ? reader.len : reader.pos + length;
|
|
2044
|
+
while (reader.pos < end) {
|
|
2045
|
+
const tag = reader.uint32();
|
|
2046
|
+
switch (tag >>> 3) {
|
|
2047
|
+
case 1:
|
|
2048
|
+
obj.MimeType = reader.string();
|
|
2049
|
+
break;
|
|
2050
|
+
default:
|
|
2051
|
+
reader.skipType(tag & 7);
|
|
2052
|
+
break;
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
return obj;
|
|
2056
|
+
}, function* (reader, length, prefix, opts = {}) {
|
|
2057
|
+
const end = length == null ? reader.len : reader.pos + length;
|
|
2058
|
+
while (reader.pos < end) {
|
|
2059
|
+
const tag = reader.uint32();
|
|
2060
|
+
switch (tag >>> 3) {
|
|
2061
|
+
case 1:
|
|
2062
|
+
yield {
|
|
2063
|
+
field: `${prefix}.MimeType`,
|
|
2064
|
+
value: reader.string()
|
|
2065
|
+
};
|
|
2066
|
+
break;
|
|
2067
|
+
default:
|
|
2068
|
+
reader.skipType(tag & 7);
|
|
2069
|
+
break;
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
});
|
|
2073
|
+
return _codec;
|
|
2074
|
+
};
|
|
2075
|
+
function encode(obj) {
|
|
2076
|
+
return encodeMessage(obj, Metadata.codec());
|
|
2077
|
+
}
|
|
2078
|
+
Metadata.encode = encode;
|
|
2079
|
+
function decode(buf, opts) {
|
|
2080
|
+
return decodeMessage(buf, Metadata.codec(), opts);
|
|
2081
|
+
}
|
|
2082
|
+
Metadata.decode = decode;
|
|
2083
|
+
function stream(buf, opts) {
|
|
2084
|
+
return streamMessage(buf, Metadata.codec(), opts);
|
|
2085
|
+
}
|
|
2086
|
+
Metadata.stream = stream;
|
|
2087
|
+
})(Metadata || (Metadata = {}));
|
|
2088
|
+
//#endregion
|
|
2089
|
+
//#region node_modules/ipfs-unixfs-importer/node_modules/ipfs-unixfs/dist/src/index.js
|
|
2090
|
+
/**
|
|
2091
|
+
* @packageDocumentation
|
|
2092
|
+
*
|
|
2093
|
+
* This module contains the protobuf definition of the UnixFS data structure found at the root of all UnixFS DAGs.
|
|
2094
|
+
*
|
|
2095
|
+
* The UnixFS spec can be found in the [ipfs/specs repository](http://github.com/ipfs/specs)
|
|
2096
|
+
*
|
|
2097
|
+
* @example Create a file composed of several blocks
|
|
2098
|
+
*
|
|
2099
|
+
* ```TypeScript
|
|
2100
|
+
* import { UnixFS } from 'ipfs-unixfs'
|
|
2101
|
+
*
|
|
2102
|
+
* const data = new UnixFS({ type: 'file' })
|
|
2103
|
+
* data.addBlockSize(256n) // add the size of each block
|
|
2104
|
+
* data.addBlockSize(256n)
|
|
2105
|
+
* // ...
|
|
2106
|
+
* ```
|
|
2107
|
+
*
|
|
2108
|
+
* @example Create a directory that contains several files
|
|
2109
|
+
*
|
|
2110
|
+
* Creating a directory that contains several files is achieve by creating a unixfs element that identifies a MerkleDAG node as a directory. The links of that MerkleDAG node are the files that are contained in this directory.
|
|
2111
|
+
*
|
|
2112
|
+
* ```TypeScript
|
|
2113
|
+
* import { UnixFS } from 'ipfs-unixfs'
|
|
2114
|
+
*
|
|
2115
|
+
* const data = new UnixFS({ type: 'directory' })
|
|
2116
|
+
* ```
|
|
2117
|
+
*
|
|
2118
|
+
* @example Create an unixfs Data element
|
|
2119
|
+
*
|
|
2120
|
+
* ```TypeScript
|
|
2121
|
+
* import { UnixFS } from 'ipfs-unixfs'
|
|
2122
|
+
*
|
|
2123
|
+
* const data = new UnixFS({
|
|
2124
|
+
* // ...options
|
|
2125
|
+
* })
|
|
2126
|
+
* ```
|
|
2127
|
+
*
|
|
2128
|
+
* `options` is an optional object argument that might include the following keys:
|
|
2129
|
+
*
|
|
2130
|
+
* - type (string, default `file`): The type of UnixFS entry. Can be:
|
|
2131
|
+
* - `raw`
|
|
2132
|
+
* - `directory`
|
|
2133
|
+
* - `file`
|
|
2134
|
+
* - `metadata`
|
|
2135
|
+
* - `symlink`
|
|
2136
|
+
* - `hamt-sharded-directory`
|
|
2137
|
+
* - data (Uint8Array): The optional data field for this node
|
|
2138
|
+
* - blockSizes (Array, default: `[]`): If this is a `file` node that is made up of multiple blocks, `blockSizes` is a list numbers that represent the size of the file chunks stored in each child node. It is used to calculate the total file size.
|
|
2139
|
+
* - mode (Number, default `0644` for files, `0755` for directories/hamt-sharded-directories) file mode
|
|
2140
|
+
* - mtime (`Date`, `{ secs, nsecs }`, `{ Seconds, FractionalNanoseconds }`, `[ secs, nsecs ]`): The modification time of this node
|
|
2141
|
+
*
|
|
2142
|
+
* @example Add and remove a block size to the block size list
|
|
2143
|
+
*
|
|
2144
|
+
* ```TypeScript
|
|
2145
|
+
* import { UnixFS } from 'ipfs-unixfs'
|
|
2146
|
+
*
|
|
2147
|
+
* const data = new UnixFS({ type: 'file' })
|
|
2148
|
+
* const sizeInBytes = 100n
|
|
2149
|
+
* data.addBlockSize(sizeInBytes)
|
|
2150
|
+
* ```
|
|
2151
|
+
*
|
|
2152
|
+
* ```TypeScript
|
|
2153
|
+
* import { UnixFS } from 'ipfs-unixfs'
|
|
2154
|
+
*
|
|
2155
|
+
* const data = new UnixFS({ type: 'file' })
|
|
2156
|
+
*
|
|
2157
|
+
* const index = 0
|
|
2158
|
+
* data.removeBlockSize(index)
|
|
2159
|
+
* ```
|
|
2160
|
+
*
|
|
2161
|
+
* @example Get total fileSize
|
|
2162
|
+
*
|
|
2163
|
+
* ```TypeScript
|
|
2164
|
+
* import { UnixFS } from 'ipfs-unixfs'
|
|
2165
|
+
*
|
|
2166
|
+
* const data = new UnixFS({ type: 'file' })
|
|
2167
|
+
* data.fileSize() // => size in bytes
|
|
2168
|
+
* ```
|
|
2169
|
+
*
|
|
2170
|
+
* @example Marshal and unmarshal
|
|
2171
|
+
*
|
|
2172
|
+
* ```TypeScript
|
|
2173
|
+
* import { UnixFS } from 'ipfs-unixfs'
|
|
2174
|
+
*
|
|
2175
|
+
* const data = new UnixFS({ type: 'file' })
|
|
2176
|
+
* const marshaled = data.marshal()
|
|
2177
|
+
* const unmarshaled = UnixFS.unmarshal(marshaled)
|
|
2178
|
+
* ```
|
|
2179
|
+
*
|
|
2180
|
+
* @example Is this UnixFS entry a directory?
|
|
2181
|
+
*
|
|
2182
|
+
* ```TypeScript
|
|
2183
|
+
* import { UnixFS } from 'ipfs-unixfs'
|
|
2184
|
+
*
|
|
2185
|
+
* const dir = new UnixFS({ type: 'directory' })
|
|
2186
|
+
* dir.isDirectory() // true
|
|
2187
|
+
*
|
|
2188
|
+
* const file = new UnixFS({ type: 'file' })
|
|
2189
|
+
* file.isDirectory() // false
|
|
2190
|
+
* ```
|
|
2191
|
+
*
|
|
2192
|
+
* @example Has an mtime been set?
|
|
2193
|
+
*
|
|
2194
|
+
* If no modification time has been set, no `mtime` property will be present on the `Data` instance:
|
|
2195
|
+
*
|
|
2196
|
+
* ```TypeScript
|
|
2197
|
+
* import { UnixFS } from 'ipfs-unixfs'
|
|
2198
|
+
*
|
|
2199
|
+
* const file = new UnixFS({ type: 'file' })
|
|
2200
|
+
* file.mtime // undefined
|
|
2201
|
+
*
|
|
2202
|
+
* Object.prototype.hasOwnProperty.call(file, 'mtime') // false
|
|
2203
|
+
*
|
|
2204
|
+
* const dir = new UnixFS({ type: 'directory', mtime: { secs: 5n } })
|
|
2205
|
+
* dir.mtime // { secs: Number, nsecs: Number }
|
|
2206
|
+
* ```
|
|
2207
|
+
*/
|
|
2208
|
+
const types = {
|
|
2209
|
+
Raw: "raw",
|
|
2210
|
+
Directory: "directory",
|
|
2211
|
+
File: "file",
|
|
2212
|
+
Metadata: "metadata",
|
|
2213
|
+
Symlink: "symlink",
|
|
2214
|
+
HAMTShard: "hamt-sharded-directory"
|
|
2215
|
+
};
|
|
2216
|
+
const dirTypes = ["directory", "hamt-sharded-directory"];
|
|
2217
|
+
const DEFAULT_FILE_MODE = parseInt("0644", 8);
|
|
2218
|
+
const DEFAULT_DIRECTORY_MODE = parseInt("0755", 8);
|
|
2219
|
+
var UnixFS = class UnixFS {
|
|
2220
|
+
/**
|
|
2221
|
+
* Decode from protobuf https://github.com/ipfs/specs/blob/master/UNIXFS.md
|
|
2222
|
+
*/
|
|
2223
|
+
static unmarshal(marshaled) {
|
|
2224
|
+
const message = Data.decode(marshaled);
|
|
2225
|
+
const data = new UnixFS({
|
|
2226
|
+
type: types[message.Type != null ? message.Type.toString() : "File"],
|
|
2227
|
+
data: message.Data,
|
|
2228
|
+
blockSizes: message.blocksizes,
|
|
2229
|
+
mode: message.mode,
|
|
2230
|
+
mtime: message.mtime != null ? {
|
|
2231
|
+
secs: message.mtime.Seconds ?? 0n,
|
|
2232
|
+
nsecs: message.mtime.FractionalNanoseconds
|
|
2233
|
+
} : void 0,
|
|
2234
|
+
fanout: message.fanout
|
|
2235
|
+
});
|
|
2236
|
+
data._originalMode = message.mode ?? 0;
|
|
2237
|
+
return data;
|
|
2238
|
+
}
|
|
2239
|
+
type;
|
|
2240
|
+
data;
|
|
2241
|
+
blockSizes;
|
|
2242
|
+
hashType;
|
|
2243
|
+
fanout;
|
|
2244
|
+
mtime;
|
|
2245
|
+
_mode;
|
|
2246
|
+
_originalMode;
|
|
2247
|
+
constructor(options = { type: "file" }) {
|
|
2248
|
+
const { type, data, blockSizes, hashType, fanout, mtime, mode } = options;
|
|
2249
|
+
if (type != null && !Object.values(types).includes(type)) throw new InvalidTypeError("Type: " + type + " is not valid");
|
|
2250
|
+
this.type = type ?? "file";
|
|
2251
|
+
this.data = data;
|
|
2252
|
+
this.hashType = hashType;
|
|
2253
|
+
this.fanout = fanout;
|
|
2254
|
+
this.blockSizes = blockSizes ?? [];
|
|
2255
|
+
this._originalMode = 0;
|
|
2256
|
+
this.mode = mode;
|
|
2257
|
+
this.mtime = mtime;
|
|
2258
|
+
}
|
|
2259
|
+
set mode(mode) {
|
|
2260
|
+
if (mode == null) this._mode = this.isDirectory() ? DEFAULT_DIRECTORY_MODE : DEFAULT_FILE_MODE;
|
|
2261
|
+
else this._mode = mode & 4095;
|
|
2262
|
+
}
|
|
2263
|
+
get mode() {
|
|
2264
|
+
return this._mode;
|
|
2265
|
+
}
|
|
2266
|
+
isDirectory() {
|
|
2267
|
+
return dirTypes.includes(this.type);
|
|
2268
|
+
}
|
|
2269
|
+
addBlockSize(size) {
|
|
2270
|
+
this.blockSizes.push(size);
|
|
2271
|
+
}
|
|
2272
|
+
removeBlockSize(index) {
|
|
2273
|
+
this.blockSizes.splice(index, 1);
|
|
2274
|
+
}
|
|
2275
|
+
/**
|
|
2276
|
+
* Returns `0n` for directories or `data.length + sum(blockSizes)` for everything else
|
|
2277
|
+
*/
|
|
2278
|
+
fileSize() {
|
|
2279
|
+
if (this.isDirectory()) return 0n;
|
|
2280
|
+
let sum = 0n;
|
|
2281
|
+
this.blockSizes.forEach((size) => {
|
|
2282
|
+
sum += size;
|
|
2283
|
+
});
|
|
2284
|
+
if (this.data != null) sum += BigInt(this.data.length);
|
|
2285
|
+
return sum;
|
|
2286
|
+
}
|
|
2287
|
+
/**
|
|
2288
|
+
* encode to protobuf Uint8Array
|
|
2289
|
+
*/
|
|
2290
|
+
marshal() {
|
|
2291
|
+
let type;
|
|
2292
|
+
switch (this.type) {
|
|
2293
|
+
case "raw":
|
|
2294
|
+
type = Data.DataType.Raw;
|
|
2295
|
+
break;
|
|
2296
|
+
case "directory":
|
|
2297
|
+
type = Data.DataType.Directory;
|
|
2298
|
+
break;
|
|
2299
|
+
case "file":
|
|
2300
|
+
type = Data.DataType.File;
|
|
2301
|
+
break;
|
|
2302
|
+
case "metadata":
|
|
2303
|
+
type = Data.DataType.Metadata;
|
|
2304
|
+
break;
|
|
2305
|
+
case "symlink":
|
|
2306
|
+
type = Data.DataType.Symlink;
|
|
2307
|
+
break;
|
|
2308
|
+
case "hamt-sharded-directory":
|
|
2309
|
+
type = Data.DataType.HAMTShard;
|
|
2310
|
+
break;
|
|
2311
|
+
default: throw new InvalidTypeError(`Type: ${type} is not valid`);
|
|
2312
|
+
}
|
|
2313
|
+
let data = this.data;
|
|
2314
|
+
if (this.data == null || this.data.length === 0) data = void 0;
|
|
2315
|
+
let mode;
|
|
2316
|
+
if (this.mode != null) {
|
|
2317
|
+
mode = this._originalMode & 4294963200 | (this.mode ?? 0);
|
|
2318
|
+
if (mode === DEFAULT_FILE_MODE && !this.isDirectory()) mode = void 0;
|
|
2319
|
+
if (mode === DEFAULT_DIRECTORY_MODE && this.isDirectory()) mode = void 0;
|
|
2320
|
+
}
|
|
2321
|
+
let mtime;
|
|
2322
|
+
if (this.mtime != null) mtime = {
|
|
2323
|
+
Seconds: this.mtime.secs,
|
|
2324
|
+
FractionalNanoseconds: this.mtime.nsecs
|
|
2325
|
+
};
|
|
2326
|
+
return Data.encode({
|
|
2327
|
+
Type: type,
|
|
2328
|
+
Data: data,
|
|
2329
|
+
filesize: this.isDirectory() ? void 0 : this.fileSize(),
|
|
2330
|
+
blocksizes: this.blockSizes,
|
|
2331
|
+
hashType: this.hashType,
|
|
2332
|
+
fanout: this.fanout,
|
|
2333
|
+
mode,
|
|
2334
|
+
mtime
|
|
2335
|
+
});
|
|
2336
|
+
}
|
|
2337
|
+
};
|
|
2338
|
+
//#endregion
|
|
2339
|
+
//#region node_modules/progress-events/dist/src/index.js
|
|
2340
|
+
/**
|
|
2341
|
+
* An implementation of the ProgressEvent interface, this is essentially
|
|
2342
|
+
* a typed `CustomEvent` with a `type` property that lets us disambiguate
|
|
2343
|
+
* events passed to `progress` callbacks.
|
|
2344
|
+
*/
|
|
2345
|
+
var CustomProgressEvent = class extends Event {
|
|
2346
|
+
type;
|
|
2347
|
+
detail;
|
|
2348
|
+
constructor(type, detail) {
|
|
2349
|
+
super(type);
|
|
2350
|
+
this.type = type;
|
|
2351
|
+
this.detail = detail;
|
|
2352
|
+
}
|
|
2353
|
+
};
|
|
2354
|
+
//#endregion
|
|
2355
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/utils/persist.js
|
|
2356
|
+
const persist = async (buffer, blockstore, options) => {
|
|
2357
|
+
if (options.codec == null) options.codec = src_exports;
|
|
2358
|
+
const multihash = await sha256.digest(buffer);
|
|
2359
|
+
const cid = CID.create(options.cidVersion ?? 1, options.codec.code, multihash);
|
|
2360
|
+
await blockstore.put(cid, buffer, options);
|
|
2361
|
+
return cid;
|
|
2362
|
+
};
|
|
2363
|
+
//#endregion
|
|
2364
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/dag-builder/buffer-importer.js
|
|
2365
|
+
function defaultBufferImporter(options) {
|
|
2366
|
+
return async function* bufferImporter(file, blockstore) {
|
|
2367
|
+
let bytesWritten = 0n;
|
|
2368
|
+
for await (let block of file.content) yield async () => {
|
|
2369
|
+
let unixfs;
|
|
2370
|
+
const opts = {
|
|
2371
|
+
codec: src_exports,
|
|
2372
|
+
cidVersion: options.cidVersion,
|
|
2373
|
+
onProgress: options.onProgress
|
|
2374
|
+
};
|
|
2375
|
+
if (options.rawLeaves) {
|
|
2376
|
+
opts.codec = raw;
|
|
2377
|
+
opts.cidVersion = 1;
|
|
2378
|
+
} else {
|
|
2379
|
+
unixfs = new UnixFS({
|
|
2380
|
+
type: options.leafType,
|
|
2381
|
+
data: block
|
|
2382
|
+
});
|
|
2383
|
+
block = encode({
|
|
2384
|
+
Data: unixfs.marshal(),
|
|
2385
|
+
Links: []
|
|
2386
|
+
});
|
|
2387
|
+
}
|
|
2388
|
+
const cid = await persist(block, blockstore, opts);
|
|
2389
|
+
bytesWritten += BigInt(block.byteLength);
|
|
2390
|
+
options.onProgress?.(new CustomProgressEvent("unixfs:importer:progress:file:write", {
|
|
2391
|
+
bytesWritten,
|
|
2392
|
+
cid,
|
|
2393
|
+
path: file.path
|
|
2394
|
+
}));
|
|
2395
|
+
return {
|
|
2396
|
+
cid,
|
|
2397
|
+
unixfs,
|
|
2398
|
+
size: BigInt(block.length),
|
|
2399
|
+
block
|
|
2400
|
+
};
|
|
2401
|
+
};
|
|
2402
|
+
};
|
|
2403
|
+
}
|
|
2404
|
+
//#endregion
|
|
2405
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/errors.js
|
|
2406
|
+
var InvalidParametersError = class InvalidParametersError extends Error {
|
|
2407
|
+
static name = "InvalidParametersError";
|
|
2408
|
+
static code = "ERR_INVALID_PARAMS";
|
|
2409
|
+
name = InvalidParametersError.name;
|
|
2410
|
+
code = InvalidParametersError.code;
|
|
2411
|
+
constructor(message = "Invalid parameters") {
|
|
2412
|
+
super(message);
|
|
2413
|
+
}
|
|
2414
|
+
};
|
|
2415
|
+
var InvalidContentError = class InvalidContentError extends Error {
|
|
2416
|
+
static name = "InvalidContentError";
|
|
2417
|
+
static code = "ERR_INVALID_CONTENT";
|
|
2418
|
+
name = InvalidContentError.name;
|
|
2419
|
+
code = InvalidContentError.code;
|
|
2420
|
+
constructor(message = "Invalid content") {
|
|
2421
|
+
super(message);
|
|
2422
|
+
}
|
|
2423
|
+
};
|
|
2424
|
+
var InvalidShardingStrategyError = class extends Error {
|
|
2425
|
+
static name = "InvalidShardingStrategyError";
|
|
2426
|
+
static code = "ERR_SHARDING_STRATEGY";
|
|
2427
|
+
name = InvalidContentError.name;
|
|
2428
|
+
code = InvalidContentError.code;
|
|
2429
|
+
};
|
|
2430
|
+
//#endregion
|
|
2431
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/dag-builder/dir.js
|
|
2432
|
+
const defaultDirBuilder = async (dir, blockstore, options) => {
|
|
2433
|
+
const unixfs = new UnixFS({
|
|
2434
|
+
type: "directory",
|
|
2435
|
+
mtime: dir.mtime,
|
|
2436
|
+
mode: dir.mode
|
|
2437
|
+
});
|
|
2438
|
+
const block = encode(prepare({ Data: unixfs.marshal() }));
|
|
2439
|
+
return {
|
|
2440
|
+
cid: await persist(block, blockstore, options),
|
|
2441
|
+
path: dir.path,
|
|
2442
|
+
unixfs,
|
|
2443
|
+
size: BigInt(block.length),
|
|
2444
|
+
originalPath: dir.originalPath,
|
|
2445
|
+
block
|
|
2446
|
+
};
|
|
2447
|
+
};
|
|
2448
|
+
//#endregion
|
|
2449
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/dag-builder/file.js
|
|
2450
|
+
async function* buildFileBatch(file, blockstore, options) {
|
|
2451
|
+
let count = -1;
|
|
2452
|
+
let previous;
|
|
2453
|
+
for await (const entry of parallelBatch(options.bufferImporter(file, blockstore), options.blockWriteConcurrency)) {
|
|
2454
|
+
count++;
|
|
2455
|
+
if (count === 0) {
|
|
2456
|
+
previous = {
|
|
2457
|
+
...entry,
|
|
2458
|
+
single: true
|
|
2459
|
+
};
|
|
2460
|
+
continue;
|
|
2461
|
+
} else if (count === 1 && previous != null) {
|
|
2462
|
+
yield {
|
|
2463
|
+
...previous,
|
|
2464
|
+
block: void 0,
|
|
2465
|
+
single: void 0
|
|
2466
|
+
};
|
|
2467
|
+
previous = void 0;
|
|
2468
|
+
}
|
|
2469
|
+
yield {
|
|
2470
|
+
...entry,
|
|
2471
|
+
block: void 0
|
|
2472
|
+
};
|
|
2473
|
+
}
|
|
2474
|
+
if (previous != null) yield previous;
|
|
2475
|
+
}
|
|
2476
|
+
function isSingleBlockImport(result) {
|
|
2477
|
+
return result.single === true;
|
|
2478
|
+
}
|
|
2479
|
+
const reduce = (file, blockstore, options) => {
|
|
2480
|
+
const reducer = async function(leaves) {
|
|
2481
|
+
if (leaves.length === 1 && isSingleBlockImport(leaves[0]) && options.reduceSingleLeafToSelf) {
|
|
2482
|
+
const leaf = leaves[0];
|
|
2483
|
+
let node = leaf.block;
|
|
2484
|
+
if (isSingleBlockImport(leaf) && (file.mtime !== void 0 || file.mode !== void 0)) {
|
|
2485
|
+
leaf.unixfs = new UnixFS({
|
|
2486
|
+
type: "file",
|
|
2487
|
+
mtime: file.mtime,
|
|
2488
|
+
mode: file.mode,
|
|
2489
|
+
data: leaf.block
|
|
2490
|
+
});
|
|
2491
|
+
node = {
|
|
2492
|
+
Data: leaf.unixfs.marshal(),
|
|
2493
|
+
Links: []
|
|
2494
|
+
};
|
|
2495
|
+
leaf.block = encode(prepare(node));
|
|
2496
|
+
leaf.cid = await persist(leaf.block, blockstore, {
|
|
2497
|
+
...options,
|
|
2498
|
+
cidVersion: options.cidVersion
|
|
2499
|
+
});
|
|
2500
|
+
leaf.size = BigInt(leaf.block.length);
|
|
2501
|
+
}
|
|
2502
|
+
options.onProgress?.(new CustomProgressEvent("unixfs:importer:progress:file:layout", {
|
|
2503
|
+
cid: leaf.cid,
|
|
2504
|
+
path: file.originalPath
|
|
2505
|
+
}));
|
|
2506
|
+
return {
|
|
2507
|
+
cid: leaf.cid,
|
|
2508
|
+
path: file.path,
|
|
2509
|
+
unixfs: leaf.unixfs,
|
|
2510
|
+
size: leaf.size,
|
|
2511
|
+
originalPath: file.originalPath
|
|
2512
|
+
};
|
|
2513
|
+
}
|
|
2514
|
+
const f = new UnixFS({
|
|
2515
|
+
type: "file",
|
|
2516
|
+
mtime: file.mtime,
|
|
2517
|
+
mode: file.mode
|
|
2518
|
+
});
|
|
2519
|
+
const links = leaves.filter((leaf) => {
|
|
2520
|
+
if (leaf.cid.code === raw.code && leaf.size > 0) return true;
|
|
2521
|
+
if (leaf.unixfs != null && leaf.unixfs.data == null && leaf.unixfs.fileSize() > 0n) return true;
|
|
2522
|
+
return Boolean(leaf.unixfs?.data?.length);
|
|
2523
|
+
}).map((leaf) => {
|
|
2524
|
+
if (leaf.cid.code === raw.code) {
|
|
2525
|
+
f.addBlockSize(leaf.size);
|
|
2526
|
+
return {
|
|
2527
|
+
Name: "",
|
|
2528
|
+
Tsize: Number(leaf.size),
|
|
2529
|
+
Hash: leaf.cid
|
|
2530
|
+
};
|
|
2531
|
+
}
|
|
2532
|
+
if (leaf.unixfs?.data == null) f.addBlockSize(leaf.unixfs?.fileSize() ?? 0n);
|
|
2533
|
+
else f.addBlockSize(BigInt(leaf.unixfs.data.length));
|
|
2534
|
+
return {
|
|
2535
|
+
Name: "",
|
|
2536
|
+
Tsize: Number(leaf.size),
|
|
2537
|
+
Hash: leaf.cid
|
|
2538
|
+
};
|
|
2539
|
+
});
|
|
2540
|
+
const node = {
|
|
2541
|
+
Data: f.marshal(),
|
|
2542
|
+
Links: links
|
|
2543
|
+
};
|
|
2544
|
+
const block = encode(prepare(node));
|
|
2545
|
+
const cid = await persist(block, blockstore, options);
|
|
2546
|
+
options.onProgress?.(new CustomProgressEvent("unixfs:importer:progress:file:layout", {
|
|
2547
|
+
cid,
|
|
2548
|
+
path: file.originalPath
|
|
2549
|
+
}));
|
|
2550
|
+
return {
|
|
2551
|
+
cid,
|
|
2552
|
+
path: file.path,
|
|
2553
|
+
unixfs: f,
|
|
2554
|
+
size: BigInt(block.length + node.Links.reduce((acc, curr) => acc + (curr.Tsize ?? 0), 0)),
|
|
2555
|
+
originalPath: file.originalPath,
|
|
2556
|
+
block
|
|
2557
|
+
};
|
|
2558
|
+
};
|
|
2559
|
+
return reducer;
|
|
2560
|
+
};
|
|
2561
|
+
const defaultFileBuilder = async (file, block, options) => {
|
|
2562
|
+
return options.layout(buildFileBatch(file, block, options), reduce(file, block, options));
|
|
2563
|
+
};
|
|
2564
|
+
//#endregion
|
|
2565
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/dag-builder/index.js
|
|
2566
|
+
function isIterable(thing) {
|
|
2567
|
+
return Symbol.iterator in thing;
|
|
2568
|
+
}
|
|
2569
|
+
function isAsyncIterable(thing) {
|
|
2570
|
+
return Symbol.asyncIterator in thing;
|
|
2571
|
+
}
|
|
2572
|
+
function contentAsAsyncIterable(content) {
|
|
2573
|
+
try {
|
|
2574
|
+
if (content instanceof Uint8Array) return (async function* () {
|
|
2575
|
+
yield content;
|
|
2576
|
+
})();
|
|
2577
|
+
else if (isIterable(content)) return (async function* () {
|
|
2578
|
+
yield* content;
|
|
2579
|
+
})();
|
|
2580
|
+
else if (isAsyncIterable(content)) return content;
|
|
2581
|
+
} catch {
|
|
2582
|
+
throw new InvalidContentError("Content was invalid");
|
|
2583
|
+
}
|
|
2584
|
+
throw new InvalidContentError("Content was invalid");
|
|
2585
|
+
}
|
|
2586
|
+
function defaultDagBuilder(options) {
|
|
2587
|
+
return async function* dagBuilder(source, blockstore) {
|
|
2588
|
+
for await (const entry of source) {
|
|
2589
|
+
let originalPath;
|
|
2590
|
+
if (entry.path != null) {
|
|
2591
|
+
originalPath = entry.path;
|
|
2592
|
+
entry.path = entry.path.split("/").filter((path) => path != null && path !== ".").join("/");
|
|
2593
|
+
}
|
|
2594
|
+
if (isFileCandidate(entry)) {
|
|
2595
|
+
const file = {
|
|
2596
|
+
path: entry.path,
|
|
2597
|
+
mtime: entry.mtime,
|
|
2598
|
+
mode: entry.mode,
|
|
2599
|
+
content: (async function* () {
|
|
2600
|
+
let bytesRead = 0n;
|
|
2601
|
+
for await (const chunk of options.chunker(options.chunkValidator(contentAsAsyncIterable(entry.content)))) {
|
|
2602
|
+
const currentChunkSize = BigInt(chunk.byteLength);
|
|
2603
|
+
bytesRead += currentChunkSize;
|
|
2604
|
+
options.onProgress?.(new CustomProgressEvent("unixfs:importer:progress:file:read", {
|
|
2605
|
+
bytesRead,
|
|
2606
|
+
chunkSize: currentChunkSize,
|
|
2607
|
+
path: entry.path
|
|
2608
|
+
}));
|
|
2609
|
+
yield chunk;
|
|
2610
|
+
}
|
|
2611
|
+
})(),
|
|
2612
|
+
originalPath
|
|
2613
|
+
};
|
|
2614
|
+
const fileBuilder = options.fileBuilder ?? defaultFileBuilder;
|
|
2615
|
+
yield async () => fileBuilder(file, blockstore, options);
|
|
2616
|
+
} else if (entry.path != null) {
|
|
2617
|
+
const dir = {
|
|
2618
|
+
path: entry.path,
|
|
2619
|
+
mtime: entry.mtime,
|
|
2620
|
+
mode: entry.mode,
|
|
2621
|
+
originalPath
|
|
2622
|
+
};
|
|
2623
|
+
const dirBuilder = options.dirBuilder ?? defaultDirBuilder;
|
|
2624
|
+
yield async () => dirBuilder(dir, blockstore, options);
|
|
2625
|
+
} else throw new Error("Import candidate must have content or path or both");
|
|
2626
|
+
}
|
|
2627
|
+
};
|
|
2628
|
+
}
|
|
2629
|
+
function isFileCandidate(entry) {
|
|
2630
|
+
return entry.content != null;
|
|
2631
|
+
}
|
|
2632
|
+
//#endregion
|
|
2633
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/dag-builder/validate-chunks.js
|
|
2634
|
+
const defaultChunkValidator = () => {
|
|
2635
|
+
return async function* validateChunks(source) {
|
|
2636
|
+
for await (const content of source) {
|
|
2637
|
+
if (content.length === void 0) throw new InvalidContentError("Content was invalid");
|
|
2638
|
+
if (typeof content === "string" || content instanceof String) yield fromString(content.toString());
|
|
2639
|
+
else if (Array.isArray(content)) yield Uint8Array.from(content);
|
|
2640
|
+
else if (content instanceof Uint8Array) yield content;
|
|
2641
|
+
else throw new InvalidContentError("Content was invalid");
|
|
2642
|
+
}
|
|
2643
|
+
};
|
|
2644
|
+
};
|
|
2645
|
+
//#endregion
|
|
2646
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/layout/balanced.js
|
|
2647
|
+
const DEFAULT_MAX_CHILDREN_PER_NODE = 174;
|
|
2648
|
+
function balanced(options) {
|
|
2649
|
+
const maxChildrenPerNode = options?.maxChildrenPerNode ?? DEFAULT_MAX_CHILDREN_PER_NODE;
|
|
2650
|
+
return async function balancedLayout(source, reduce) {
|
|
2651
|
+
const roots = [];
|
|
2652
|
+
for await (const chunked of batch(source, maxChildrenPerNode)) roots.push(await reduce(chunked));
|
|
2653
|
+
if (roots.length > 1) return balancedLayout(roots, reduce);
|
|
2654
|
+
return roots[0];
|
|
2655
|
+
};
|
|
2656
|
+
}
|
|
2657
|
+
//#endregion
|
|
2658
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/dir.js
|
|
2659
|
+
var Dir = class {
|
|
2660
|
+
options;
|
|
2661
|
+
root;
|
|
2662
|
+
dir;
|
|
2663
|
+
path;
|
|
2664
|
+
dirty;
|
|
2665
|
+
flat;
|
|
2666
|
+
parent;
|
|
2667
|
+
parentKey;
|
|
2668
|
+
unixfs;
|
|
2669
|
+
mode;
|
|
2670
|
+
mtime;
|
|
2671
|
+
cid;
|
|
2672
|
+
size;
|
|
2673
|
+
nodeSize;
|
|
2674
|
+
constructor(props, options) {
|
|
2675
|
+
this.options = options ?? {};
|
|
2676
|
+
this.root = props.root;
|
|
2677
|
+
this.dir = props.dir;
|
|
2678
|
+
this.path = props.path;
|
|
2679
|
+
this.dirty = props.dirty;
|
|
2680
|
+
this.flat = props.flat;
|
|
2681
|
+
this.parent = props.parent;
|
|
2682
|
+
this.parentKey = props.parentKey;
|
|
2683
|
+
this.unixfs = props.unixfs;
|
|
2684
|
+
this.mode = props.mode;
|
|
2685
|
+
this.mtime = props.mtime;
|
|
2686
|
+
}
|
|
2687
|
+
};
|
|
2688
|
+
//#endregion
|
|
2689
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/utils/pb-size.js
|
|
2690
|
+
/**
|
|
2691
|
+
* Protobuf size calculation utilities for DAG-PB nodes.
|
|
2692
|
+
*
|
|
2693
|
+
* Computes exact serialized sizes matching @ipld/dag-pb's encoding
|
|
2694
|
+
* without allocating byte arrays. Used by DirFlat to avoid O(N)
|
|
2695
|
+
* re-serialization on every file insert.
|
|
2696
|
+
*
|
|
2697
|
+
* Ported from @ipld/dag-pb/src/pb-encode.js (sov, len64, sizeLink, sizeNode)
|
|
2698
|
+
* and boxo's directory.go estimatedSize logic.
|
|
2699
|
+
*/
|
|
2700
|
+
const maxInt32 = 2 ** 32;
|
|
2701
|
+
const len8tab = [
|
|
2702
|
+
0,
|
|
2703
|
+
1,
|
|
2704
|
+
2,
|
|
2705
|
+
2,
|
|
2706
|
+
3,
|
|
2707
|
+
3,
|
|
2708
|
+
3,
|
|
2709
|
+
3,
|
|
2710
|
+
4,
|
|
2711
|
+
4,
|
|
2712
|
+
4,
|
|
2713
|
+
4,
|
|
2714
|
+
4,
|
|
2715
|
+
4,
|
|
2716
|
+
4,
|
|
2717
|
+
4,
|
|
2718
|
+
5,
|
|
2719
|
+
5,
|
|
2720
|
+
5,
|
|
2721
|
+
5,
|
|
2722
|
+
5,
|
|
2723
|
+
5,
|
|
2724
|
+
5,
|
|
2725
|
+
5,
|
|
2726
|
+
5,
|
|
2727
|
+
5,
|
|
2728
|
+
5,
|
|
2729
|
+
5,
|
|
2730
|
+
5,
|
|
2731
|
+
5,
|
|
2732
|
+
5,
|
|
2733
|
+
5,
|
|
2734
|
+
6,
|
|
2735
|
+
6,
|
|
2736
|
+
6,
|
|
2737
|
+
6,
|
|
2738
|
+
6,
|
|
2739
|
+
6,
|
|
2740
|
+
6,
|
|
2741
|
+
6,
|
|
2742
|
+
6,
|
|
2743
|
+
6,
|
|
2744
|
+
6,
|
|
2745
|
+
6,
|
|
2746
|
+
6,
|
|
2747
|
+
6,
|
|
2748
|
+
6,
|
|
2749
|
+
6,
|
|
2750
|
+
6,
|
|
2751
|
+
6,
|
|
2752
|
+
6,
|
|
2753
|
+
6,
|
|
2754
|
+
6,
|
|
2755
|
+
6,
|
|
2756
|
+
6,
|
|
2757
|
+
6,
|
|
2758
|
+
6,
|
|
2759
|
+
6,
|
|
2760
|
+
6,
|
|
2761
|
+
6,
|
|
2762
|
+
6,
|
|
2763
|
+
6,
|
|
2764
|
+
6,
|
|
2765
|
+
6,
|
|
2766
|
+
7,
|
|
2767
|
+
7,
|
|
2768
|
+
7,
|
|
2769
|
+
7,
|
|
2770
|
+
7,
|
|
2771
|
+
7,
|
|
2772
|
+
7,
|
|
2773
|
+
7,
|
|
2774
|
+
7,
|
|
2775
|
+
7,
|
|
2776
|
+
7,
|
|
2777
|
+
7,
|
|
2778
|
+
7,
|
|
2779
|
+
7,
|
|
2780
|
+
7,
|
|
2781
|
+
7,
|
|
2782
|
+
7,
|
|
2783
|
+
7,
|
|
2784
|
+
7,
|
|
2785
|
+
7,
|
|
2786
|
+
7,
|
|
2787
|
+
7,
|
|
2788
|
+
7,
|
|
2789
|
+
7,
|
|
2790
|
+
7,
|
|
2791
|
+
7,
|
|
2792
|
+
7,
|
|
2793
|
+
7,
|
|
2794
|
+
7,
|
|
2795
|
+
7,
|
|
2796
|
+
7,
|
|
2797
|
+
7,
|
|
2798
|
+
7,
|
|
2799
|
+
7,
|
|
2800
|
+
7,
|
|
2801
|
+
7,
|
|
2802
|
+
7,
|
|
2803
|
+
7,
|
|
2804
|
+
7,
|
|
2805
|
+
7,
|
|
2806
|
+
7,
|
|
2807
|
+
7,
|
|
2808
|
+
7,
|
|
2809
|
+
7,
|
|
2810
|
+
7,
|
|
2811
|
+
7,
|
|
2812
|
+
7,
|
|
2813
|
+
7,
|
|
2814
|
+
7,
|
|
2815
|
+
7,
|
|
2816
|
+
7,
|
|
2817
|
+
7,
|
|
2818
|
+
7,
|
|
2819
|
+
7,
|
|
2820
|
+
7,
|
|
2821
|
+
7,
|
|
2822
|
+
7,
|
|
2823
|
+
7,
|
|
2824
|
+
7,
|
|
2825
|
+
7,
|
|
2826
|
+
7,
|
|
2827
|
+
7,
|
|
2828
|
+
7,
|
|
2829
|
+
7,
|
|
2830
|
+
8,
|
|
2831
|
+
8,
|
|
2832
|
+
8,
|
|
2833
|
+
8,
|
|
2834
|
+
8,
|
|
2835
|
+
8,
|
|
2836
|
+
8,
|
|
2837
|
+
8,
|
|
2838
|
+
8,
|
|
2839
|
+
8,
|
|
2840
|
+
8,
|
|
2841
|
+
8,
|
|
2842
|
+
8,
|
|
2843
|
+
8,
|
|
2844
|
+
8,
|
|
2845
|
+
8,
|
|
2846
|
+
8,
|
|
2847
|
+
8,
|
|
2848
|
+
8,
|
|
2849
|
+
8,
|
|
2850
|
+
8,
|
|
2851
|
+
8,
|
|
2852
|
+
8,
|
|
2853
|
+
8,
|
|
2854
|
+
8,
|
|
2855
|
+
8,
|
|
2856
|
+
8,
|
|
2857
|
+
8,
|
|
2858
|
+
8,
|
|
2859
|
+
8,
|
|
2860
|
+
8,
|
|
2861
|
+
8,
|
|
2862
|
+
8,
|
|
2863
|
+
8,
|
|
2864
|
+
8,
|
|
2865
|
+
8,
|
|
2866
|
+
8,
|
|
2867
|
+
8,
|
|
2868
|
+
8,
|
|
2869
|
+
8,
|
|
2870
|
+
8,
|
|
2871
|
+
8,
|
|
2872
|
+
8,
|
|
2873
|
+
8,
|
|
2874
|
+
8,
|
|
2875
|
+
8,
|
|
2876
|
+
8,
|
|
2877
|
+
8,
|
|
2878
|
+
8,
|
|
2879
|
+
8,
|
|
2880
|
+
8,
|
|
2881
|
+
8,
|
|
2882
|
+
8,
|
|
2883
|
+
8,
|
|
2884
|
+
8,
|
|
2885
|
+
8,
|
|
2886
|
+
8,
|
|
2887
|
+
8,
|
|
2888
|
+
8,
|
|
2889
|
+
8,
|
|
2890
|
+
8,
|
|
2891
|
+
8,
|
|
2892
|
+
8,
|
|
2893
|
+
8,
|
|
2894
|
+
8,
|
|
2895
|
+
8,
|
|
2896
|
+
8,
|
|
2897
|
+
8,
|
|
2898
|
+
8,
|
|
2899
|
+
8,
|
|
2900
|
+
8,
|
|
2901
|
+
8,
|
|
2902
|
+
8,
|
|
2903
|
+
8,
|
|
2904
|
+
8,
|
|
2905
|
+
8,
|
|
2906
|
+
8,
|
|
2907
|
+
8,
|
|
2908
|
+
8,
|
|
2909
|
+
8,
|
|
2910
|
+
8,
|
|
2911
|
+
8,
|
|
2912
|
+
8,
|
|
2913
|
+
8,
|
|
2914
|
+
8,
|
|
2915
|
+
8,
|
|
2916
|
+
8,
|
|
2917
|
+
8,
|
|
2918
|
+
8,
|
|
2919
|
+
8,
|
|
2920
|
+
8,
|
|
2921
|
+
8,
|
|
2922
|
+
8,
|
|
2923
|
+
8,
|
|
2924
|
+
8,
|
|
2925
|
+
8,
|
|
2926
|
+
8,
|
|
2927
|
+
8,
|
|
2928
|
+
8,
|
|
2929
|
+
8,
|
|
2930
|
+
8,
|
|
2931
|
+
8,
|
|
2932
|
+
8,
|
|
2933
|
+
8,
|
|
2934
|
+
8,
|
|
2935
|
+
8,
|
|
2936
|
+
8,
|
|
2937
|
+
8,
|
|
2938
|
+
8,
|
|
2939
|
+
8,
|
|
2940
|
+
8,
|
|
2941
|
+
8,
|
|
2942
|
+
8,
|
|
2943
|
+
8,
|
|
2944
|
+
8,
|
|
2945
|
+
8,
|
|
2946
|
+
8,
|
|
2947
|
+
8,
|
|
2948
|
+
8,
|
|
2949
|
+
8,
|
|
2950
|
+
8,
|
|
2951
|
+
8,
|
|
2952
|
+
8,
|
|
2953
|
+
8,
|
|
2954
|
+
8,
|
|
2955
|
+
8,
|
|
2956
|
+
8,
|
|
2957
|
+
8
|
|
2958
|
+
];
|
|
2959
|
+
function len64(x) {
|
|
2960
|
+
let n = 0;
|
|
2961
|
+
if (x >= maxInt32) {
|
|
2962
|
+
x = Math.floor(x / maxInt32);
|
|
2963
|
+
n = 32;
|
|
2964
|
+
}
|
|
2965
|
+
if (x >= 65536) {
|
|
2966
|
+
x >>>= 16;
|
|
2967
|
+
n += 16;
|
|
2968
|
+
}
|
|
2969
|
+
if (x >= 256) {
|
|
2970
|
+
x >>>= 8;
|
|
2971
|
+
n += 8;
|
|
2972
|
+
}
|
|
2973
|
+
return n + len8tab[x];
|
|
2974
|
+
}
|
|
2975
|
+
/**
|
|
2976
|
+
* Protobuf varint byte size, matching @ipld/dag-pb's sov().
|
|
2977
|
+
*/
|
|
2978
|
+
function varintLen(x) {
|
|
2979
|
+
if (x % 2 === 0) x++;
|
|
2980
|
+
return Math.floor((len64(x) + 6) / 7);
|
|
2981
|
+
}
|
|
2982
|
+
/**
|
|
2983
|
+
* Compute UTF-8 byte length of a JS string without allocation.
|
|
2984
|
+
*
|
|
2985
|
+
* Safe to assume UTF-8 because @ipld/dag-pb always encodes PBLink.Name
|
|
2986
|
+
* via TextEncoder (UTF-8) and decodes via TextDecoder (UTF-8).
|
|
2987
|
+
* This produces the same result as textEncoder.encode(str).length
|
|
2988
|
+
* without the Uint8Array allocation on every put() call.
|
|
2989
|
+
*/
|
|
2990
|
+
function utf8ByteLength(str) {
|
|
2991
|
+
let len = 0;
|
|
2992
|
+
for (let i = 0; i < str.length; i++) {
|
|
2993
|
+
const c = str.charCodeAt(i);
|
|
2994
|
+
if (c < 128) len++;
|
|
2995
|
+
else if (c < 2048) len += 2;
|
|
2996
|
+
else if (c >= 55296 && c <= 56319 && i + 1 < str.length) {
|
|
2997
|
+
i++;
|
|
2998
|
+
len += 4;
|
|
2999
|
+
} else len += 3;
|
|
3000
|
+
}
|
|
3001
|
+
return len;
|
|
3002
|
+
}
|
|
3003
|
+
/**
|
|
3004
|
+
* Exact bytes a single PBLink adds to the PBNode encoding.
|
|
3005
|
+
*
|
|
3006
|
+
* Matches sizeLink() + its wrapper in sizeNode() from pb-encode.js:
|
|
3007
|
+
* linkLen = Hash(1+sov(cidLen)+cidLen) + Name(1+sov(nameLen)+nameLen) + Tsize(1+sov(tsize))
|
|
3008
|
+
* total = 1 + sov(linkLen) + linkLen
|
|
3009
|
+
*/
|
|
3010
|
+
function linkSerializedSize(nameByteLen, cidByteLength, tsize) {
|
|
3011
|
+
let linkLen = 1 + varintLen(cidByteLength) + cidByteLength;
|
|
3012
|
+
linkLen += 1 + varintLen(nameByteLen) + nameByteLen;
|
|
3013
|
+
linkLen += 1 + varintLen(tsize);
|
|
3014
|
+
return 1 + varintLen(linkLen) + linkLen;
|
|
3015
|
+
}
|
|
3016
|
+
const DIR_DEFAULT_MODE = 493;
|
|
3017
|
+
/**
|
|
3018
|
+
* Exact bytes the PBNode Data field adds for a UnixFS directory.
|
|
3019
|
+
*
|
|
3020
|
+
* Directory-only: the type field is hardcoded to directory (2 bytes) and
|
|
3021
|
+
* the default mode is 0o755. Do not use for file nodes (different type
|
|
3022
|
+
* byte, different default mode 0o644).
|
|
3023
|
+
*
|
|
3024
|
+
* For the common case (no mode, no mtime) this is always 4 bytes:
|
|
3025
|
+
* innerSize=2 [0x08,0x01], wrapper 1+1+2=4.
|
|
3026
|
+
*/
|
|
3027
|
+
function dataFieldSerializedSize(mode, mtime) {
|
|
3028
|
+
let innerSize = 2;
|
|
3029
|
+
if (mode !== void 0 && mode !== DIR_DEFAULT_MODE) innerSize += 1 + varintLen(mode);
|
|
3030
|
+
if (mtime != null) {
|
|
3031
|
+
let mtimeInner = 0;
|
|
3032
|
+
const secs = Number(mtime.secs);
|
|
3033
|
+
if (secs < 0) mtimeInner += 11;
|
|
3034
|
+
else mtimeInner += 1 + varintLen(secs);
|
|
3035
|
+
if (mtime.nsecs != null) mtimeInner += 5;
|
|
3036
|
+
innerSize += 1 + varintLen(mtimeInner) + mtimeInner;
|
|
3037
|
+
}
|
|
3038
|
+
return 1 + varintLen(innerSize) + innerSize;
|
|
3039
|
+
}
|
|
3040
|
+
//#endregion
|
|
3041
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/dir-flat.js
|
|
3042
|
+
function estimateLinkSize(nameBytes, child) {
|
|
3043
|
+
if (child?.cid != null && child?.size != null) return nameBytes + child.cid.byteLength;
|
|
3044
|
+
return 0;
|
|
3045
|
+
}
|
|
3046
|
+
function calculateLinkSize(nameBytes, child) {
|
|
3047
|
+
if (child?.cid != null && child?.size != null) return linkSerializedSize(nameBytes, child.cid.byteLength, Number(child.size));
|
|
3048
|
+
return 0;
|
|
3049
|
+
}
|
|
3050
|
+
var DirFlat = class extends Dir {
|
|
3051
|
+
_children;
|
|
3052
|
+
constructor(props, options) {
|
|
3053
|
+
super(props, options);
|
|
3054
|
+
this._children = /* @__PURE__ */ new Map();
|
|
3055
|
+
}
|
|
3056
|
+
async put(name, value) {
|
|
3057
|
+
if (this.nodeSize !== void 0) {
|
|
3058
|
+
const oldChild = this._children.get(name);
|
|
3059
|
+
const nameBytes = utf8ByteLength(name);
|
|
3060
|
+
const strategy = this.options?.shardSplitStrategy;
|
|
3061
|
+
if (strategy === "links-bytes") {
|
|
3062
|
+
this.nodeSize -= estimateLinkSize(nameBytes, oldChild);
|
|
3063
|
+
this.nodeSize += estimateLinkSize(nameBytes, value);
|
|
3064
|
+
} else if (strategy === "block-bytes") {
|
|
3065
|
+
this.nodeSize -= calculateLinkSize(nameBytes, oldChild);
|
|
3066
|
+
this.nodeSize += calculateLinkSize(nameBytes, value);
|
|
3067
|
+
} else throw new InvalidShardingStrategyError(`Invalid shardSplitStrategy: ${strategy}`);
|
|
3068
|
+
if (this.nodeSize < 0) this.nodeSize = void 0;
|
|
3069
|
+
}
|
|
3070
|
+
this.cid = void 0;
|
|
3071
|
+
this.size = void 0;
|
|
3072
|
+
this._children.set(name, value);
|
|
3073
|
+
}
|
|
3074
|
+
async get(name) {
|
|
3075
|
+
return Promise.resolve(this._children.get(name));
|
|
3076
|
+
}
|
|
3077
|
+
childCount() {
|
|
3078
|
+
return this._children.size;
|
|
3079
|
+
}
|
|
3080
|
+
directChildrenCount() {
|
|
3081
|
+
return this.childCount();
|
|
3082
|
+
}
|
|
3083
|
+
onlyChild() {
|
|
3084
|
+
return this._children.values().next().value;
|
|
3085
|
+
}
|
|
3086
|
+
*eachChildSeries() {
|
|
3087
|
+
for (const [key, child] of this._children.entries()) yield {
|
|
3088
|
+
key,
|
|
3089
|
+
child
|
|
3090
|
+
};
|
|
3091
|
+
}
|
|
3092
|
+
marshal() {
|
|
3093
|
+
const unixfs = new UnixFS({
|
|
3094
|
+
type: "directory",
|
|
3095
|
+
mtime: this.mtime,
|
|
3096
|
+
mode: this.mode
|
|
3097
|
+
});
|
|
3098
|
+
const links = [];
|
|
3099
|
+
for (const [name, child] of this._children.entries()) {
|
|
3100
|
+
if (child.size == null || child.cid == null) continue;
|
|
3101
|
+
if (child.cid == null) throw new Error("Directory contents must be flushed before marshaling");
|
|
3102
|
+
links.push({
|
|
3103
|
+
Hash: child.cid,
|
|
3104
|
+
Name: name,
|
|
3105
|
+
Tsize: child.size == null ? void 0 : Number(child.size)
|
|
3106
|
+
});
|
|
3107
|
+
}
|
|
3108
|
+
return encode(prepare({
|
|
3109
|
+
Data: unixfs.marshal(),
|
|
3110
|
+
Links: links
|
|
3111
|
+
}));
|
|
3112
|
+
}
|
|
3113
|
+
async estimateNodeSize() {
|
|
3114
|
+
if (this.nodeSize !== void 0) return this.nodeSize;
|
|
3115
|
+
const strategy = this.options?.shardSplitStrategy;
|
|
3116
|
+
if (strategy === "links-bytes") {
|
|
3117
|
+
this.nodeSize = 0;
|
|
3118
|
+
for (const [name, child] of this._children.entries()) this.nodeSize += estimateLinkSize(utf8ByteLength(name), child);
|
|
3119
|
+
} else if (strategy === "block-bytes") {
|
|
3120
|
+
this.nodeSize = dataFieldSerializedSize(this.mode, this.mtime);
|
|
3121
|
+
for (const [name, child] of this._children.entries()) this.nodeSize += calculateLinkSize(utf8ByteLength(name), child);
|
|
3122
|
+
} else throw new InvalidShardingStrategyError(`Invalid shardSplitStrategy: ${strategy}`);
|
|
3123
|
+
return this.nodeSize;
|
|
3124
|
+
}
|
|
3125
|
+
async *flush(block) {
|
|
3126
|
+
const links = [];
|
|
3127
|
+
for (const [name, child] of this._children.entries()) {
|
|
3128
|
+
let result = child;
|
|
3129
|
+
if (child instanceof Dir) for await (const entry of child.flush(block)) {
|
|
3130
|
+
result = entry;
|
|
3131
|
+
yield entry;
|
|
3132
|
+
}
|
|
3133
|
+
if (result.size != null && result.cid != null) links.push({
|
|
3134
|
+
Name: name,
|
|
3135
|
+
Tsize: Number(result.size),
|
|
3136
|
+
Hash: result.cid
|
|
3137
|
+
});
|
|
3138
|
+
}
|
|
3139
|
+
const unixfs = new UnixFS({
|
|
3140
|
+
type: "directory",
|
|
3141
|
+
mtime: this.mtime,
|
|
3142
|
+
mode: this.mode
|
|
3143
|
+
});
|
|
3144
|
+
const node = {
|
|
3145
|
+
Data: unixfs.marshal(),
|
|
3146
|
+
Links: links
|
|
3147
|
+
};
|
|
3148
|
+
const buffer = encode(prepare(node));
|
|
3149
|
+
const cid = await persist(buffer, block, this.options);
|
|
3150
|
+
const size = buffer.length + node.Links.reduce(
|
|
3151
|
+
/**
|
|
3152
|
+
* @param {number} acc
|
|
3153
|
+
* @param {PBLink} curr
|
|
3154
|
+
*/
|
|
3155
|
+
(acc, curr) => acc + (curr.Tsize ?? 0),
|
|
3156
|
+
0
|
|
3157
|
+
);
|
|
3158
|
+
this.cid = cid;
|
|
3159
|
+
this.size = size;
|
|
3160
|
+
yield {
|
|
3161
|
+
cid,
|
|
3162
|
+
unixfs,
|
|
3163
|
+
path: this.path,
|
|
3164
|
+
size: BigInt(size)
|
|
3165
|
+
};
|
|
3166
|
+
}
|
|
3167
|
+
};
|
|
3168
|
+
//#endregion
|
|
3169
|
+
//#region node_modules/hamt-sharding/dist/src/bucket.js
|
|
3170
|
+
var import_sparse_array = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
3171
|
+
const BITS_PER_BYTE = 7;
|
|
3172
|
+
module.exports = class SparseArray {
|
|
3173
|
+
constructor() {
|
|
3174
|
+
this._bitArrays = [];
|
|
3175
|
+
this._data = [];
|
|
3176
|
+
this._length = 0;
|
|
3177
|
+
this._changedLength = false;
|
|
3178
|
+
this._changedData = false;
|
|
3179
|
+
}
|
|
3180
|
+
set(index, value) {
|
|
3181
|
+
let pos = this._internalPositionFor(index, false);
|
|
3182
|
+
if (value === void 0) {
|
|
3183
|
+
if (pos !== -1) {
|
|
3184
|
+
this._unsetInternalPos(pos);
|
|
3185
|
+
this._unsetBit(index);
|
|
3186
|
+
this._changedLength = true;
|
|
3187
|
+
this._changedData = true;
|
|
3188
|
+
}
|
|
3189
|
+
} else {
|
|
3190
|
+
let needsSort = false;
|
|
3191
|
+
if (pos === -1) {
|
|
3192
|
+
pos = this._data.length;
|
|
3193
|
+
this._setBit(index);
|
|
3194
|
+
this._changedData = true;
|
|
3195
|
+
} else needsSort = true;
|
|
3196
|
+
this._setInternalPos(pos, index, value, needsSort);
|
|
3197
|
+
this._changedLength = true;
|
|
3198
|
+
}
|
|
3199
|
+
}
|
|
3200
|
+
unset(index) {
|
|
3201
|
+
this.set(index, void 0);
|
|
3202
|
+
}
|
|
3203
|
+
get(index) {
|
|
3204
|
+
this._sortData();
|
|
3205
|
+
const pos = this._internalPositionFor(index, true);
|
|
3206
|
+
if (pos === -1) return;
|
|
3207
|
+
return this._data[pos][1];
|
|
3208
|
+
}
|
|
3209
|
+
push(value) {
|
|
3210
|
+
this.set(this.length, value);
|
|
3211
|
+
return this.length;
|
|
3212
|
+
}
|
|
3213
|
+
get length() {
|
|
3214
|
+
this._sortData();
|
|
3215
|
+
if (this._changedLength) {
|
|
3216
|
+
const last = this._data[this._data.length - 1];
|
|
3217
|
+
this._length = last ? last[0] + 1 : 0;
|
|
3218
|
+
this._changedLength = false;
|
|
3219
|
+
}
|
|
3220
|
+
return this._length;
|
|
3221
|
+
}
|
|
3222
|
+
forEach(iterator) {
|
|
3223
|
+
let i = 0;
|
|
3224
|
+
while (i < this.length) {
|
|
3225
|
+
iterator(this.get(i), i, this);
|
|
3226
|
+
i++;
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
map(iterator) {
|
|
3230
|
+
let i = 0;
|
|
3231
|
+
let mapped = new Array(this.length);
|
|
3232
|
+
while (i < this.length) {
|
|
3233
|
+
mapped[i] = iterator(this.get(i), i, this);
|
|
3234
|
+
i++;
|
|
3235
|
+
}
|
|
3236
|
+
return mapped;
|
|
3237
|
+
}
|
|
3238
|
+
reduce(reducer, initialValue) {
|
|
3239
|
+
let i = 0;
|
|
3240
|
+
let acc = initialValue;
|
|
3241
|
+
while (i < this.length) {
|
|
3242
|
+
const value = this.get(i);
|
|
3243
|
+
acc = reducer(acc, value, i);
|
|
3244
|
+
i++;
|
|
3245
|
+
}
|
|
3246
|
+
return acc;
|
|
3247
|
+
}
|
|
3248
|
+
find(finder) {
|
|
3249
|
+
let i = 0, found, last;
|
|
3250
|
+
while (i < this.length && !found) {
|
|
3251
|
+
last = this.get(i);
|
|
3252
|
+
found = finder(last);
|
|
3253
|
+
i++;
|
|
3254
|
+
}
|
|
3255
|
+
return found ? last : void 0;
|
|
3256
|
+
}
|
|
3257
|
+
_internalPositionFor(index, noCreate) {
|
|
3258
|
+
const bytePos = this._bytePosFor(index, noCreate);
|
|
3259
|
+
if (bytePos >= this._bitArrays.length) return -1;
|
|
3260
|
+
const byte = this._bitArrays[bytePos];
|
|
3261
|
+
const bitPos = index - bytePos * BITS_PER_BYTE;
|
|
3262
|
+
if (!((byte & 1 << bitPos) > 0)) return -1;
|
|
3263
|
+
return this._bitArrays.slice(0, bytePos).reduce(popCountReduce, 0) + popCount(byte & ~(4294967295 << bitPos + 1)) - 1;
|
|
3264
|
+
}
|
|
3265
|
+
_bytePosFor(index, noCreate) {
|
|
3266
|
+
const bytePos = Math.floor(index / BITS_PER_BYTE);
|
|
3267
|
+
const targetLength = bytePos + 1;
|
|
3268
|
+
while (!noCreate && this._bitArrays.length < targetLength) this._bitArrays.push(0);
|
|
3269
|
+
return bytePos;
|
|
3270
|
+
}
|
|
3271
|
+
_setBit(index) {
|
|
3272
|
+
const bytePos = this._bytePosFor(index, false);
|
|
3273
|
+
this._bitArrays[bytePos] |= 1 << index - bytePos * BITS_PER_BYTE;
|
|
3274
|
+
}
|
|
3275
|
+
_unsetBit(index) {
|
|
3276
|
+
const bytePos = this._bytePosFor(index, false);
|
|
3277
|
+
this._bitArrays[bytePos] &= ~(1 << index - bytePos * BITS_PER_BYTE);
|
|
3278
|
+
}
|
|
3279
|
+
_setInternalPos(pos, index, value, needsSort) {
|
|
3280
|
+
const data = this._data;
|
|
3281
|
+
const elem = [index, value];
|
|
3282
|
+
if (needsSort) {
|
|
3283
|
+
this._sortData();
|
|
3284
|
+
data[pos] = elem;
|
|
3285
|
+
} else {
|
|
3286
|
+
if (data.length) if (data[data.length - 1][0] >= index) data.push(elem);
|
|
3287
|
+
else if (data[0][0] <= index) data.unshift(elem);
|
|
3288
|
+
else {
|
|
3289
|
+
const randomIndex = Math.round(data.length / 2);
|
|
3290
|
+
this._data = data.slice(0, randomIndex).concat(elem).concat(data.slice(randomIndex));
|
|
3291
|
+
}
|
|
3292
|
+
else this._data.push(elem);
|
|
3293
|
+
this._changedData = true;
|
|
3294
|
+
this._changedLength = true;
|
|
3295
|
+
}
|
|
3296
|
+
}
|
|
3297
|
+
_unsetInternalPos(pos) {
|
|
3298
|
+
this._data.splice(pos, 1);
|
|
3299
|
+
}
|
|
3300
|
+
_sortData() {
|
|
3301
|
+
if (this._changedData) this._data.sort(sortInternal);
|
|
3302
|
+
this._changedData = false;
|
|
3303
|
+
}
|
|
3304
|
+
bitField() {
|
|
3305
|
+
const bytes = [];
|
|
3306
|
+
let pendingBitsForResultingByte = 8;
|
|
3307
|
+
let pendingBitsForNewByte = 0;
|
|
3308
|
+
let resultingByte = 0;
|
|
3309
|
+
let newByte;
|
|
3310
|
+
const pending = this._bitArrays.slice();
|
|
3311
|
+
while (pending.length || pendingBitsForNewByte) {
|
|
3312
|
+
if (pendingBitsForNewByte === 0) {
|
|
3313
|
+
newByte = pending.shift();
|
|
3314
|
+
pendingBitsForNewByte = 7;
|
|
3315
|
+
}
|
|
3316
|
+
const usingBits = Math.min(pendingBitsForNewByte, pendingBitsForResultingByte);
|
|
3317
|
+
const mask = ~(255 << usingBits);
|
|
3318
|
+
const masked = newByte & mask;
|
|
3319
|
+
resultingByte |= masked << 8 - pendingBitsForResultingByte;
|
|
3320
|
+
newByte = newByte >>> usingBits;
|
|
3321
|
+
pendingBitsForNewByte -= usingBits;
|
|
3322
|
+
pendingBitsForResultingByte -= usingBits;
|
|
3323
|
+
if (!pendingBitsForResultingByte || !pendingBitsForNewByte && !pending.length) {
|
|
3324
|
+
bytes.push(resultingByte);
|
|
3325
|
+
resultingByte = 0;
|
|
3326
|
+
pendingBitsForResultingByte = 8;
|
|
3327
|
+
}
|
|
3328
|
+
}
|
|
3329
|
+
for (var i = bytes.length - 1; i > 0; i--) if (bytes[i] === 0) bytes.pop();
|
|
3330
|
+
else break;
|
|
3331
|
+
return bytes;
|
|
3332
|
+
}
|
|
3333
|
+
compactArray() {
|
|
3334
|
+
this._sortData();
|
|
3335
|
+
return this._data.map(valueOnly);
|
|
3336
|
+
}
|
|
3337
|
+
};
|
|
3338
|
+
function popCountReduce(count, byte) {
|
|
3339
|
+
return count + popCount(byte);
|
|
3340
|
+
}
|
|
3341
|
+
function popCount(_v) {
|
|
3342
|
+
let v = _v;
|
|
3343
|
+
v = v - (v >> 1 & 1431655765);
|
|
3344
|
+
v = (v & 858993459) + (v >> 2 & 858993459);
|
|
3345
|
+
return (v + (v >> 4) & 252645135) * 16843009 >> 24;
|
|
3346
|
+
}
|
|
3347
|
+
function sortInternal(a, b) {
|
|
3348
|
+
return a[0] - b[0];
|
|
3349
|
+
}
|
|
3350
|
+
function valueOnly(elem) {
|
|
3351
|
+
return elem[1];
|
|
3352
|
+
}
|
|
3353
|
+
})))(), 1);
|
|
3354
|
+
var Bucket = class Bucket {
|
|
3355
|
+
_options;
|
|
3356
|
+
_popCount;
|
|
3357
|
+
_parent;
|
|
3358
|
+
_posAtParent;
|
|
3359
|
+
_children;
|
|
3360
|
+
key;
|
|
3361
|
+
constructor(options, parent, posAtParent = 0) {
|
|
3362
|
+
this._options = options;
|
|
3363
|
+
this._popCount = 0;
|
|
3364
|
+
this._parent = parent;
|
|
3365
|
+
this._posAtParent = posAtParent;
|
|
3366
|
+
this._children = new import_sparse_array.default();
|
|
3367
|
+
this.key = null;
|
|
3368
|
+
}
|
|
3369
|
+
async put(key, value) {
|
|
3370
|
+
const place = await this._findNewBucketAndPos(key);
|
|
3371
|
+
place.bucket._putAt(place, key, value);
|
|
3372
|
+
}
|
|
3373
|
+
async get(key) {
|
|
3374
|
+
const child = await this._findChild(key);
|
|
3375
|
+
if (child != null) return child.value;
|
|
3376
|
+
}
|
|
3377
|
+
async del(key) {
|
|
3378
|
+
const place = await this._findPlace(key);
|
|
3379
|
+
const child = place.bucket._at(place.pos);
|
|
3380
|
+
if (child != null && child.key === key) place.bucket._delAt(place.pos);
|
|
3381
|
+
}
|
|
3382
|
+
leafCount() {
|
|
3383
|
+
return this._children.compactArray().reduce((acc, child) => {
|
|
3384
|
+
if (child instanceof Bucket) return acc + child.leafCount();
|
|
3385
|
+
return acc + 1;
|
|
3386
|
+
}, 0);
|
|
3387
|
+
}
|
|
3388
|
+
childrenCount() {
|
|
3389
|
+
return this._children.length;
|
|
3390
|
+
}
|
|
3391
|
+
onlyChild() {
|
|
3392
|
+
return this._children.get(0);
|
|
3393
|
+
}
|
|
3394
|
+
*eachLeafSeries() {
|
|
3395
|
+
const children = this._children.compactArray();
|
|
3396
|
+
for (const child of children) if (child instanceof Bucket) yield* child.eachLeafSeries();
|
|
3397
|
+
else yield child;
|
|
3398
|
+
}
|
|
3399
|
+
serialize(map, reduce) {
|
|
3400
|
+
return reduce(this._children.reduce((acc, child, index) => {
|
|
3401
|
+
if (child != null) if (child instanceof Bucket) acc.push(child.serialize(map, reduce));
|
|
3402
|
+
else acc.push(map(child, index));
|
|
3403
|
+
return acc;
|
|
3404
|
+
}, []));
|
|
3405
|
+
}
|
|
3406
|
+
async asyncTransform(asyncMap, asyncReduce) {
|
|
3407
|
+
return asyncTransformBucket(this, asyncMap, asyncReduce);
|
|
3408
|
+
}
|
|
3409
|
+
toJSON() {
|
|
3410
|
+
return this.serialize(mapNode, reduceNodes);
|
|
3411
|
+
}
|
|
3412
|
+
prettyPrint() {
|
|
3413
|
+
return JSON.stringify(this.toJSON(), null, " ");
|
|
3414
|
+
}
|
|
3415
|
+
tableSize() {
|
|
3416
|
+
return Math.pow(2, this._options.bits);
|
|
3417
|
+
}
|
|
3418
|
+
async _findChild(key) {
|
|
3419
|
+
const result = await this._findPlace(key);
|
|
3420
|
+
const child = result.bucket._at(result.pos);
|
|
3421
|
+
if (child instanceof Bucket) return;
|
|
3422
|
+
if (child != null && child.key === key) return child;
|
|
3423
|
+
}
|
|
3424
|
+
async _findPlace(key) {
|
|
3425
|
+
const hashValue = this._options.hash(typeof key === "string" ? fromString(key) : key);
|
|
3426
|
+
const index = await hashValue.take(this._options.bits);
|
|
3427
|
+
const child = this._children.get(index);
|
|
3428
|
+
if (child instanceof Bucket) return child._findPlace(hashValue);
|
|
3429
|
+
return {
|
|
3430
|
+
bucket: this,
|
|
3431
|
+
pos: index,
|
|
3432
|
+
hash: hashValue,
|
|
3433
|
+
existingChild: child
|
|
3434
|
+
};
|
|
3435
|
+
}
|
|
3436
|
+
async _findNewBucketAndPos(key) {
|
|
3437
|
+
const place = await this._findPlace(key);
|
|
3438
|
+
if (place.existingChild != null && place.existingChild.key !== key) {
|
|
3439
|
+
const bucket = new Bucket(this._options, place.bucket, place.pos);
|
|
3440
|
+
place.bucket._putObjectAt(place.pos, bucket);
|
|
3441
|
+
const newPlace = await bucket._findPlace(place.existingChild.hash);
|
|
3442
|
+
newPlace.bucket._putAt(newPlace, place.existingChild.key, place.existingChild.value);
|
|
3443
|
+
return bucket._findNewBucketAndPos(place.hash);
|
|
3444
|
+
}
|
|
3445
|
+
return place;
|
|
3446
|
+
}
|
|
3447
|
+
_putAt(place, key, value) {
|
|
3448
|
+
this._putObjectAt(place.pos, {
|
|
3449
|
+
key,
|
|
3450
|
+
value,
|
|
3451
|
+
hash: place.hash
|
|
3452
|
+
});
|
|
3453
|
+
}
|
|
3454
|
+
_putObjectAt(pos, object) {
|
|
3455
|
+
if (this._children.get(pos) == null) this._popCount++;
|
|
3456
|
+
this._children.set(pos, object);
|
|
3457
|
+
}
|
|
3458
|
+
_delAt(pos) {
|
|
3459
|
+
if (pos === -1) throw new Error("Invalid position");
|
|
3460
|
+
if (this._children.get(pos) != null) this._popCount--;
|
|
3461
|
+
this._children.unset(pos);
|
|
3462
|
+
this._level();
|
|
3463
|
+
}
|
|
3464
|
+
_level() {
|
|
3465
|
+
if (this._parent != null && this._popCount <= 1) if (this._popCount === 1) {
|
|
3466
|
+
const onlyChild = this._children.find(exists);
|
|
3467
|
+
if (onlyChild != null && !(onlyChild instanceof Bucket)) {
|
|
3468
|
+
const hash = onlyChild.hash;
|
|
3469
|
+
hash.untake(this._options.bits);
|
|
3470
|
+
const place = {
|
|
3471
|
+
pos: this._posAtParent,
|
|
3472
|
+
hash,
|
|
3473
|
+
bucket: this._parent
|
|
3474
|
+
};
|
|
3475
|
+
this._parent._putAt(place, onlyChild.key, onlyChild.value);
|
|
3476
|
+
}
|
|
3477
|
+
} else this._parent._delAt(this._posAtParent);
|
|
3478
|
+
}
|
|
3479
|
+
_at(index) {
|
|
3480
|
+
return this._children.get(index);
|
|
3481
|
+
}
|
|
3482
|
+
};
|
|
3483
|
+
function exists(o) {
|
|
3484
|
+
return Boolean(o);
|
|
3485
|
+
}
|
|
3486
|
+
function mapNode(node, _) {
|
|
3487
|
+
return node.key;
|
|
3488
|
+
}
|
|
3489
|
+
function reduceNodes(nodes) {
|
|
3490
|
+
return nodes;
|
|
3491
|
+
}
|
|
3492
|
+
async function asyncTransformBucket(bucket, asyncMap, asyncReduce) {
|
|
3493
|
+
const output = [];
|
|
3494
|
+
for (const child of bucket._children.compactArray()) if (child instanceof Bucket) await asyncTransformBucket(child, asyncMap, asyncReduce);
|
|
3495
|
+
else {
|
|
3496
|
+
const mappedChildren = await asyncMap(child);
|
|
3497
|
+
output.push({
|
|
3498
|
+
bitField: bucket._children.bitField(),
|
|
3499
|
+
children: mappedChildren
|
|
3500
|
+
});
|
|
3501
|
+
}
|
|
3502
|
+
return asyncReduce(output);
|
|
3503
|
+
}
|
|
3504
|
+
//#endregion
|
|
3505
|
+
//#region node_modules/hamt-sharding/dist/src/consumable-buffer.js
|
|
3506
|
+
const START_MASKS = [
|
|
3507
|
+
255,
|
|
3508
|
+
254,
|
|
3509
|
+
252,
|
|
3510
|
+
248,
|
|
3511
|
+
240,
|
|
3512
|
+
224,
|
|
3513
|
+
192,
|
|
3514
|
+
128
|
|
3515
|
+
];
|
|
3516
|
+
const STOP_MASKS = [
|
|
3517
|
+
1,
|
|
3518
|
+
3,
|
|
3519
|
+
7,
|
|
3520
|
+
15,
|
|
3521
|
+
31,
|
|
3522
|
+
63,
|
|
3523
|
+
127,
|
|
3524
|
+
255
|
|
3525
|
+
];
|
|
3526
|
+
var ConsumableBuffer = class {
|
|
3527
|
+
_value;
|
|
3528
|
+
_currentBytePos;
|
|
3529
|
+
_currentBitPos;
|
|
3530
|
+
constructor(value) {
|
|
3531
|
+
this._value = value;
|
|
3532
|
+
this._currentBytePos = value.length - 1;
|
|
3533
|
+
this._currentBitPos = 7;
|
|
3534
|
+
}
|
|
3535
|
+
availableBits() {
|
|
3536
|
+
return this._currentBitPos + 1 + this._currentBytePos * 8;
|
|
3537
|
+
}
|
|
3538
|
+
totalBits() {
|
|
3539
|
+
return this._value.length * 8;
|
|
3540
|
+
}
|
|
3541
|
+
take(bits) {
|
|
3542
|
+
let pendingBits = bits;
|
|
3543
|
+
let result = 0;
|
|
3544
|
+
while (pendingBits > 0 && this._haveBits()) {
|
|
3545
|
+
const byte = this._value[this._currentBytePos];
|
|
3546
|
+
const availableBits = this._currentBitPos + 1;
|
|
3547
|
+
const taking = Math.min(availableBits, pendingBits);
|
|
3548
|
+
const value = byteBitsToInt(byte, availableBits - taking, taking);
|
|
3549
|
+
result = (result << taking) + value;
|
|
3550
|
+
pendingBits -= taking;
|
|
3551
|
+
this._currentBitPos -= taking;
|
|
3552
|
+
if (this._currentBitPos < 0) {
|
|
3553
|
+
this._currentBitPos = 7;
|
|
3554
|
+
this._currentBytePos--;
|
|
3555
|
+
}
|
|
3556
|
+
}
|
|
3557
|
+
return result;
|
|
3558
|
+
}
|
|
3559
|
+
untake(bits) {
|
|
3560
|
+
this._currentBitPos += bits;
|
|
3561
|
+
while (this._currentBitPos > 7) {
|
|
3562
|
+
this._currentBitPos -= 8;
|
|
3563
|
+
this._currentBytePos += 1;
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
_haveBits() {
|
|
3567
|
+
return this._currentBytePos >= 0;
|
|
3568
|
+
}
|
|
3569
|
+
};
|
|
3570
|
+
function byteBitsToInt(byte, start, length) {
|
|
3571
|
+
return (byte & maskFor(start, length)) >>> start;
|
|
3572
|
+
}
|
|
3573
|
+
function maskFor(start, length) {
|
|
3574
|
+
return START_MASKS[start] & STOP_MASKS[Math.min(length + start - 1, 7)];
|
|
3575
|
+
}
|
|
3576
|
+
//#endregion
|
|
3577
|
+
//#region node_modules/hamt-sharding/dist/src/consumable-hash.js
|
|
3578
|
+
function wrapHash(hashFn) {
|
|
3579
|
+
function hashing(value) {
|
|
3580
|
+
if (value instanceof InfiniteHash) return value;
|
|
3581
|
+
else return new InfiniteHash(value, hashFn);
|
|
3582
|
+
}
|
|
3583
|
+
return hashing;
|
|
3584
|
+
}
|
|
3585
|
+
var InfiniteHash = class {
|
|
3586
|
+
_value;
|
|
3587
|
+
_hashFn;
|
|
3588
|
+
_depth;
|
|
3589
|
+
_availableBits;
|
|
3590
|
+
_currentBufferIndex;
|
|
3591
|
+
_buffers;
|
|
3592
|
+
constructor(value, hashFn) {
|
|
3593
|
+
if (!(value instanceof Uint8Array)) throw new Error("can only hash Uint8Arrays");
|
|
3594
|
+
this._value = value;
|
|
3595
|
+
this._hashFn = hashFn;
|
|
3596
|
+
this._depth = -1;
|
|
3597
|
+
this._availableBits = 0;
|
|
3598
|
+
this._currentBufferIndex = 0;
|
|
3599
|
+
this._buffers = [];
|
|
3600
|
+
}
|
|
3601
|
+
async take(bits) {
|
|
3602
|
+
let pendingBits = bits;
|
|
3603
|
+
while (this._availableBits < pendingBits) await this._produceMoreBits();
|
|
3604
|
+
let result = 0;
|
|
3605
|
+
while (pendingBits > 0) {
|
|
3606
|
+
const hash = this._buffers[this._currentBufferIndex];
|
|
3607
|
+
const available = Math.min(hash.availableBits(), pendingBits);
|
|
3608
|
+
const took = hash.take(available);
|
|
3609
|
+
result = (result << available) + took;
|
|
3610
|
+
pendingBits -= available;
|
|
3611
|
+
this._availableBits -= available;
|
|
3612
|
+
if (hash.availableBits() === 0) this._currentBufferIndex++;
|
|
3613
|
+
}
|
|
3614
|
+
return result;
|
|
3615
|
+
}
|
|
3616
|
+
untake(bits) {
|
|
3617
|
+
let pendingBits = bits;
|
|
3618
|
+
while (pendingBits > 0) {
|
|
3619
|
+
const hash = this._buffers[this._currentBufferIndex];
|
|
3620
|
+
const availableForUntake = Math.min(hash.totalBits() - hash.availableBits(), pendingBits);
|
|
3621
|
+
hash.untake(availableForUntake);
|
|
3622
|
+
pendingBits -= availableForUntake;
|
|
3623
|
+
this._availableBits += availableForUntake;
|
|
3624
|
+
if (this._currentBufferIndex > 0 && hash.totalBits() === hash.availableBits()) {
|
|
3625
|
+
this._depth--;
|
|
3626
|
+
this._currentBufferIndex--;
|
|
3627
|
+
}
|
|
3628
|
+
}
|
|
3629
|
+
}
|
|
3630
|
+
async _produceMoreBits() {
|
|
3631
|
+
this._depth++;
|
|
3632
|
+
const value = this._depth > 0 ? concat([this._value, Uint8Array.from([this._depth])]) : this._value;
|
|
3633
|
+
const buffer = new ConsumableBuffer(await this._hashFn(value));
|
|
3634
|
+
this._buffers.push(buffer);
|
|
3635
|
+
this._availableBits += buffer.availableBits();
|
|
3636
|
+
}
|
|
3637
|
+
};
|
|
3638
|
+
//#endregion
|
|
3639
|
+
//#region node_modules/hamt-sharding/dist/src/index.js
|
|
3640
|
+
/**
|
|
3641
|
+
* @packageDocumentation
|
|
3642
|
+
*
|
|
3643
|
+
* A [Hash Mapped Trie](https://en.wikipedia.org/wiki/Hash_array_mapped_trie) implementation for JavaScript.
|
|
3644
|
+
*
|
|
3645
|
+
* This is used by [@helia/unixfs](https://www.npmjs.com/package/@helia/unixfs) for it's HAMT-sharded directory implementation.
|
|
3646
|
+
*
|
|
3647
|
+
* @example
|
|
3648
|
+
*
|
|
3649
|
+
* ```TypeScript
|
|
3650
|
+
* import { createHAMT } from 'hamt-sharding'
|
|
3651
|
+
* import crypto from 'crypto-promise'
|
|
3652
|
+
*
|
|
3653
|
+
* // decide how to hash buffers made from keys, can return a Promise
|
|
3654
|
+
* const hashFn = async (buf) => {
|
|
3655
|
+
* return crypto
|
|
3656
|
+
* .createHash('sha256')
|
|
3657
|
+
* .update(buf)
|
|
3658
|
+
* .digest()
|
|
3659
|
+
* }
|
|
3660
|
+
*
|
|
3661
|
+
* const bucket = createHAMT({
|
|
3662
|
+
* hashFn: hashFn
|
|
3663
|
+
* })
|
|
3664
|
+
*
|
|
3665
|
+
* await bucket.put('key', 'value')
|
|
3666
|
+
*
|
|
3667
|
+
* const output = await bucket.get('key')
|
|
3668
|
+
* // output === 'value'
|
|
3669
|
+
* ```
|
|
3670
|
+
*/
|
|
3671
|
+
function createHAMT(options) {
|
|
3672
|
+
if (options == null || options.hashFn == null) throw new Error("please define an options.hashFn");
|
|
3673
|
+
return new Bucket({
|
|
3674
|
+
bits: options.bits ?? 8,
|
|
3675
|
+
hash: wrapHash(options.hashFn)
|
|
3676
|
+
});
|
|
3677
|
+
}
|
|
3678
|
+
//#endregion
|
|
3679
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/dir-sharded.js
|
|
3680
|
+
async function hamtHashFn(buf) {
|
|
3681
|
+
return (await murmur3128.encode(buf)).slice(0, 8).reverse();
|
|
3682
|
+
}
|
|
3683
|
+
var DirSharded = class extends Dir {
|
|
3684
|
+
bucket;
|
|
3685
|
+
constructor(props, options) {
|
|
3686
|
+
super(props, options);
|
|
3687
|
+
this.bucket = createHAMT({
|
|
3688
|
+
hashFn: hamtHashFn,
|
|
3689
|
+
bits: options.shardFanoutBits ?? 8
|
|
3690
|
+
});
|
|
3691
|
+
}
|
|
3692
|
+
async put(name, value) {
|
|
3693
|
+
this.cid = void 0;
|
|
3694
|
+
this.size = void 0;
|
|
3695
|
+
this.nodeSize = void 0;
|
|
3696
|
+
await this.bucket.put(name, value);
|
|
3697
|
+
}
|
|
3698
|
+
async get(name) {
|
|
3699
|
+
return this.bucket.get(name);
|
|
3700
|
+
}
|
|
3701
|
+
childCount() {
|
|
3702
|
+
return this.bucket.leafCount();
|
|
3703
|
+
}
|
|
3704
|
+
directChildrenCount() {
|
|
3705
|
+
return this.bucket.childrenCount();
|
|
3706
|
+
}
|
|
3707
|
+
onlyChild() {
|
|
3708
|
+
return this.bucket.onlyChild();
|
|
3709
|
+
}
|
|
3710
|
+
*eachChildSeries() {
|
|
3711
|
+
for (const { key, value } of this.bucket.eachLeafSeries()) yield {
|
|
3712
|
+
key,
|
|
3713
|
+
child: value
|
|
3714
|
+
};
|
|
3715
|
+
}
|
|
3716
|
+
async estimateNodeSize() {
|
|
3717
|
+
if (this.nodeSize !== void 0) return this.nodeSize;
|
|
3718
|
+
const blockstore = new BlackHoleBlockstore();
|
|
3719
|
+
const result = await calculateSize(this.bucket, this, blockstore, this.options);
|
|
3720
|
+
this.nodeSize = result.size;
|
|
3721
|
+
return this.nodeSize;
|
|
3722
|
+
}
|
|
3723
|
+
async *flush(blockstore) {
|
|
3724
|
+
for await (const entry of flush(this.bucket, blockstore, this, this.options)) yield {
|
|
3725
|
+
...entry,
|
|
3726
|
+
path: this.path
|
|
3727
|
+
};
|
|
3728
|
+
}
|
|
3729
|
+
};
|
|
3730
|
+
async function* flush(bucket, blockstore, shardRoot, options) {
|
|
3731
|
+
const children = bucket._children;
|
|
3732
|
+
const padLength = (bucket.tableSize() - 1).toString(16).length;
|
|
3733
|
+
const links = [];
|
|
3734
|
+
let childrenSize = 0n;
|
|
3735
|
+
for (let i = 0; i < children.length; i++) {
|
|
3736
|
+
const child = children.get(i);
|
|
3737
|
+
if (child == null) continue;
|
|
3738
|
+
const labelPrefix = i.toString(16).toUpperCase().padStart(padLength, "0");
|
|
3739
|
+
if (child instanceof Bucket) {
|
|
3740
|
+
let shard;
|
|
3741
|
+
for await (const subShard of flush(child, blockstore, null, options)) shard = subShard;
|
|
3742
|
+
if (shard == null) throw new Error("Could not flush sharded directory, no sub-shard found");
|
|
3743
|
+
links.push({
|
|
3744
|
+
Name: labelPrefix,
|
|
3745
|
+
Tsize: Number(shard.size),
|
|
3746
|
+
Hash: shard.cid
|
|
3747
|
+
});
|
|
3748
|
+
childrenSize += shard.size;
|
|
3749
|
+
} else if (isDir(child.value)) {
|
|
3750
|
+
const dir = child.value;
|
|
3751
|
+
let flushedDir;
|
|
3752
|
+
for await (const entry of dir.flush(blockstore)) {
|
|
3753
|
+
flushedDir = entry;
|
|
3754
|
+
yield flushedDir;
|
|
3755
|
+
}
|
|
3756
|
+
if (flushedDir == null) throw new Error("Did not flush dir");
|
|
3757
|
+
const label = labelPrefix + child.key;
|
|
3758
|
+
links.push({
|
|
3759
|
+
Name: label,
|
|
3760
|
+
Tsize: Number(flushedDir.size),
|
|
3761
|
+
Hash: flushedDir.cid
|
|
3762
|
+
});
|
|
3763
|
+
childrenSize += flushedDir.size;
|
|
3764
|
+
} else {
|
|
3765
|
+
const value = child.value;
|
|
3766
|
+
if (value.cid == null) continue;
|
|
3767
|
+
const label = labelPrefix + child.key;
|
|
3768
|
+
const size = value.size;
|
|
3769
|
+
links.push({
|
|
3770
|
+
Name: label,
|
|
3771
|
+
Tsize: Number(size),
|
|
3772
|
+
Hash: value.cid
|
|
3773
|
+
});
|
|
3774
|
+
childrenSize += BigInt(size ?? 0);
|
|
3775
|
+
}
|
|
3776
|
+
}
|
|
3777
|
+
const dir = new UnixFS({
|
|
3778
|
+
type: "hamt-sharded-directory",
|
|
3779
|
+
data: Uint8Array.from(children.bitField().reverse()),
|
|
3780
|
+
fanout: BigInt(bucket.tableSize()),
|
|
3781
|
+
hashType: DEFAULT_SHARD_HASH_CODE,
|
|
3782
|
+
mtime: shardRoot?.mtime,
|
|
3783
|
+
mode: shardRoot?.mode
|
|
3784
|
+
});
|
|
3785
|
+
const buffer = encode(prepare({
|
|
3786
|
+
Data: dir.marshal(),
|
|
3787
|
+
Links: links
|
|
3788
|
+
}));
|
|
3789
|
+
yield {
|
|
3790
|
+
cid: await persist(buffer, blockstore, options),
|
|
3791
|
+
unixfs: dir,
|
|
3792
|
+
size: BigInt(buffer.byteLength) + childrenSize
|
|
3793
|
+
};
|
|
3794
|
+
}
|
|
3795
|
+
function isDir(obj) {
|
|
3796
|
+
return typeof obj.flush === "function";
|
|
3797
|
+
}
|
|
3798
|
+
async function calculateSize(bucket, shardRoot, blocks, options) {
|
|
3799
|
+
const children = bucket._children;
|
|
3800
|
+
const padLength = (bucket.tableSize() - 1).toString(16).length;
|
|
3801
|
+
const links = [];
|
|
3802
|
+
let sizeEstimate = 0;
|
|
3803
|
+
for (let i = 0; i < children.length; i++) {
|
|
3804
|
+
const child = children.get(i);
|
|
3805
|
+
if (child == null) continue;
|
|
3806
|
+
const labelPrefix = i.toString(16).toUpperCase().padStart(padLength, "0");
|
|
3807
|
+
if (child instanceof Bucket) {
|
|
3808
|
+
const { size, cid } = await calculateSize(child, null, blocks, options);
|
|
3809
|
+
links.push({
|
|
3810
|
+
Name: labelPrefix,
|
|
3811
|
+
Tsize: Number(size),
|
|
3812
|
+
Hash: cid
|
|
3813
|
+
});
|
|
3814
|
+
sizeEstimate += labelPrefix.length + cid.byteLength;
|
|
3815
|
+
} else if (isDir(child.value)) {
|
|
3816
|
+
const dir = child.value;
|
|
3817
|
+
const size = dir.nodeSize;
|
|
3818
|
+
if (dir.cid == null) throw new Error("Child directory has not been persisted");
|
|
3819
|
+
links.push({
|
|
3820
|
+
Name: labelPrefix + child.key,
|
|
3821
|
+
Tsize: Number(size),
|
|
3822
|
+
Hash: dir.cid
|
|
3823
|
+
});
|
|
3824
|
+
sizeEstimate += labelPrefix.length + dir.cid.byteLength;
|
|
3825
|
+
} else {
|
|
3826
|
+
const value = child.value;
|
|
3827
|
+
const label = labelPrefix + child.key;
|
|
3828
|
+
const size = value.size;
|
|
3829
|
+
links.push({
|
|
3830
|
+
Name: label,
|
|
3831
|
+
Tsize: Number(size),
|
|
3832
|
+
Hash: value.cid
|
|
3833
|
+
});
|
|
3834
|
+
sizeEstimate += labelPrefix.length + value.cid.byteLength;
|
|
3835
|
+
}
|
|
3836
|
+
}
|
|
3837
|
+
const buffer = encode(prepare({
|
|
3838
|
+
Data: new UnixFS({
|
|
3839
|
+
type: "hamt-sharded-directory",
|
|
3840
|
+
data: Uint8Array.from(children.bitField().reverse()),
|
|
3841
|
+
fanout: BigInt(bucket.tableSize()),
|
|
3842
|
+
hashType: DEFAULT_SHARD_HASH_CODE,
|
|
3843
|
+
mtime: shardRoot?.mtime,
|
|
3844
|
+
mode: shardRoot?.mode
|
|
3845
|
+
}).marshal(),
|
|
3846
|
+
Links: links
|
|
3847
|
+
}));
|
|
3848
|
+
return {
|
|
3849
|
+
cid: await persist(buffer, blocks, options),
|
|
3850
|
+
size: options.shardSplitStrategy === "links-bytes" ? sizeEstimate : buffer.length
|
|
3851
|
+
};
|
|
3852
|
+
}
|
|
3853
|
+
//#endregion
|
|
3854
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/flat-to-shard.js
|
|
3855
|
+
async function flatToShard(child, dir) {
|
|
3856
|
+
let newDir = dir;
|
|
3857
|
+
const shardSplitThresholdBytes = dir.options.shardSplitThresholdBytes ?? 262144;
|
|
3858
|
+
if (dir instanceof DirFlat && await dir.estimateNodeSize() > shardSplitThresholdBytes) newDir = await convertToShard(dir);
|
|
3859
|
+
const parent = newDir.parent;
|
|
3860
|
+
if (parent != null) {
|
|
3861
|
+
if (newDir !== dir) {
|
|
3862
|
+
if (child != null) child.parent = newDir;
|
|
3863
|
+
if (newDir.parentKey == null) throw new Error("No parent key found");
|
|
3864
|
+
await parent.put(newDir.parentKey, newDir);
|
|
3865
|
+
}
|
|
3866
|
+
return flatToShard(newDir, parent);
|
|
3867
|
+
}
|
|
3868
|
+
return newDir;
|
|
3869
|
+
}
|
|
3870
|
+
async function convertToShard(oldDir) {
|
|
3871
|
+
const newDir = new DirSharded({
|
|
3872
|
+
root: oldDir.root,
|
|
3873
|
+
dir: true,
|
|
3874
|
+
parent: oldDir.parent,
|
|
3875
|
+
parentKey: oldDir.parentKey,
|
|
3876
|
+
path: oldDir.path,
|
|
3877
|
+
dirty: oldDir.dirty,
|
|
3878
|
+
flat: false,
|
|
3879
|
+
mtime: oldDir.mtime,
|
|
3880
|
+
mode: oldDir.mode
|
|
3881
|
+
}, oldDir.options);
|
|
3882
|
+
for (const { key, child } of oldDir.eachChildSeries()) await newDir.put(key, child);
|
|
3883
|
+
return newDir;
|
|
3884
|
+
}
|
|
3885
|
+
//#endregion
|
|
3886
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/utils/to-path-components.js
|
|
3887
|
+
const toPathComponents = (path = "") => {
|
|
3888
|
+
return path.split(/(?<!\\)\//).filter(Boolean);
|
|
3889
|
+
};
|
|
3890
|
+
//#endregion
|
|
3891
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/tree-builder.js
|
|
3892
|
+
async function addToTree(elem, tree, options) {
|
|
3893
|
+
const pathElems = toPathComponents(elem.path ?? "");
|
|
3894
|
+
const lastIndex = pathElems.length - 1;
|
|
3895
|
+
let parent = tree;
|
|
3896
|
+
let currentPath = "";
|
|
3897
|
+
for (let i = 0; i < pathElems.length; i++) {
|
|
3898
|
+
const pathElem = pathElems[i];
|
|
3899
|
+
currentPath += `${currentPath !== "" ? "/" : ""}${pathElem}`;
|
|
3900
|
+
const last = i === lastIndex;
|
|
3901
|
+
parent.dirty = true;
|
|
3902
|
+
parent.cid = void 0;
|
|
3903
|
+
parent.size = void 0;
|
|
3904
|
+
if (last) {
|
|
3905
|
+
await parent.put(pathElem, elem);
|
|
3906
|
+
tree = await flatToShard(null, parent);
|
|
3907
|
+
} else {
|
|
3908
|
+
let dir = await parent.get(pathElem);
|
|
3909
|
+
if (dir == null || !(dir instanceof Dir)) dir = new DirFlat({
|
|
3910
|
+
root: false,
|
|
3911
|
+
dir: true,
|
|
3912
|
+
parent,
|
|
3913
|
+
parentKey: pathElem,
|
|
3914
|
+
path: currentPath,
|
|
3915
|
+
dirty: true,
|
|
3916
|
+
flat: true,
|
|
3917
|
+
mtime: dir?.unixfs?.mtime,
|
|
3918
|
+
mode: dir?.unixfs?.mode
|
|
3919
|
+
}, options);
|
|
3920
|
+
await parent.put(pathElem, dir);
|
|
3921
|
+
parent = dir;
|
|
3922
|
+
}
|
|
3923
|
+
}
|
|
3924
|
+
return tree;
|
|
3925
|
+
}
|
|
3926
|
+
async function* flushAndYield(tree, blockstore) {
|
|
3927
|
+
if (!(tree instanceof Dir)) {
|
|
3928
|
+
if (tree.unixfs?.isDirectory() === true) yield tree;
|
|
3929
|
+
return;
|
|
3930
|
+
}
|
|
3931
|
+
yield* tree.flush(blockstore);
|
|
3932
|
+
}
|
|
3933
|
+
function defaultTreeBuilder(options) {
|
|
3934
|
+
return async function* treeBuilder(source, block) {
|
|
3935
|
+
let tree = new DirFlat({
|
|
3936
|
+
root: true,
|
|
3937
|
+
dir: true,
|
|
3938
|
+
path: "",
|
|
3939
|
+
dirty: true,
|
|
3940
|
+
flat: true
|
|
3941
|
+
}, options);
|
|
3942
|
+
let rootDir;
|
|
3943
|
+
let singleRoot = false;
|
|
3944
|
+
for await (const entry of source) {
|
|
3945
|
+
if (entry == null) continue;
|
|
3946
|
+
const dir = `${entry.originalPath ?? ""}`.split("/")[0];
|
|
3947
|
+
if (dir != null && dir !== "") {
|
|
3948
|
+
if (rootDir == null) {
|
|
3949
|
+
rootDir = dir;
|
|
3950
|
+
singleRoot = true;
|
|
3951
|
+
} else if (rootDir !== dir) singleRoot = false;
|
|
3952
|
+
}
|
|
3953
|
+
tree = await addToTree(entry, tree, options);
|
|
3954
|
+
if (entry.unixfs?.isDirectory() !== true) yield entry;
|
|
3955
|
+
}
|
|
3956
|
+
if (options.wrapWithDirectory || singleRoot && tree.childCount() > 1) yield* flushAndYield(tree, block);
|
|
3957
|
+
else for (const unwrapped of tree.eachChildSeries()) {
|
|
3958
|
+
if (unwrapped == null) continue;
|
|
3959
|
+
yield* flushAndYield(unwrapped.child, block);
|
|
3960
|
+
}
|
|
3961
|
+
};
|
|
3962
|
+
}
|
|
3963
|
+
//#endregion
|
|
3964
|
+
//#region node_modules/ipfs-unixfs-importer/dist/src/index.js
|
|
3965
|
+
/**
|
|
3966
|
+
* @packageDocumentation
|
|
3967
|
+
*
|
|
3968
|
+
* @example
|
|
3969
|
+
*
|
|
3970
|
+
* Let's create a little directory to import:
|
|
3971
|
+
*
|
|
3972
|
+
* ```console
|
|
3973
|
+
* > cd /tmp
|
|
3974
|
+
* > mkdir foo
|
|
3975
|
+
* > echo 'hello' > foo/bar
|
|
3976
|
+
* > echo 'world' > foo/quux
|
|
3977
|
+
* ```
|
|
3978
|
+
*
|
|
3979
|
+
* And write the importing logic:
|
|
3980
|
+
*
|
|
3981
|
+
* ```TypeScript
|
|
3982
|
+
* import { importer } from 'ipfs-unixfs-importer'
|
|
3983
|
+
* import { MemoryBlockstore } from 'blockstore-core/memory'
|
|
3984
|
+
* import * as fs from 'node:fs'
|
|
3985
|
+
*
|
|
3986
|
+
* // Where the blocks will be stored
|
|
3987
|
+
* const blockstore = new MemoryBlockstore()
|
|
3988
|
+
*
|
|
3989
|
+
* // Import path /tmp/foo/
|
|
3990
|
+
* const source = [{
|
|
3991
|
+
* path: '/tmp/foo/bar',
|
|
3992
|
+
* content: fs.createReadStream('/tmp/foo/bar')
|
|
3993
|
+
* }, {
|
|
3994
|
+
* path: '/tmp/foo/quxx',
|
|
3995
|
+
* content: fs.createReadStream('/tmp/foo/quux')
|
|
3996
|
+
* }]
|
|
3997
|
+
*
|
|
3998
|
+
* for await (const entry of importer(source, blockstore)) {
|
|
3999
|
+
* console.info(entry)
|
|
4000
|
+
* }
|
|
4001
|
+
* ```
|
|
4002
|
+
*
|
|
4003
|
+
* When run, metadata about DAGNodes in the created tree is printed until the root:
|
|
4004
|
+
*
|
|
4005
|
+
* ```
|
|
4006
|
+
* {
|
|
4007
|
+
* cid: CID, // see https://github.com/multiformats/js-cid
|
|
4008
|
+
* path: 'tmp/foo/bar',
|
|
4009
|
+
* unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs
|
|
4010
|
+
* }
|
|
4011
|
+
* {
|
|
4012
|
+
* cid: CID, // see https://github.com/multiformats/js-cid
|
|
4013
|
+
* path: 'tmp/foo/quxx',
|
|
4014
|
+
* unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs
|
|
4015
|
+
* }
|
|
4016
|
+
* {
|
|
4017
|
+
* cid: CID, // see https://github.com/multiformats/js-cid
|
|
4018
|
+
* path: 'tmp/foo',
|
|
4019
|
+
* unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs
|
|
4020
|
+
* }
|
|
4021
|
+
* {
|
|
4022
|
+
* cid: CID, // see https://github.com/multiformats/js-cid
|
|
4023
|
+
* path: 'tmp',
|
|
4024
|
+
* unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs
|
|
4025
|
+
* }
|
|
4026
|
+
* ```
|
|
4027
|
+
*/
|
|
4028
|
+
/**
|
|
4029
|
+
* The importer creates UnixFS DAGs and stores the blocks that make
|
|
4030
|
+
* them up in the passed blockstore.
|
|
4031
|
+
*
|
|
4032
|
+
* @example
|
|
4033
|
+
*
|
|
4034
|
+
* ```typescript
|
|
4035
|
+
* import { importer } from 'ipfs-unixfs-importer'
|
|
4036
|
+
* import { MemoryBlockstore } from 'blockstore-core'
|
|
4037
|
+
*
|
|
4038
|
+
* // store blocks in memory, other blockstores are available
|
|
4039
|
+
* const blockstore = new MemoryBlockstore()
|
|
4040
|
+
*
|
|
4041
|
+
* const input = [{
|
|
4042
|
+
* path: './foo.txt',
|
|
4043
|
+
* content: Uint8Array.from([0, 1, 2, 3, 4])
|
|
4044
|
+
* }, {
|
|
4045
|
+
* path: './bar.txt',
|
|
4046
|
+
* content: Uint8Array.from([0, 1, 2, 3, 4])
|
|
4047
|
+
* }]
|
|
4048
|
+
*
|
|
4049
|
+
* for await (const entry of importer(input, blockstore)) {
|
|
4050
|
+
* console.info(entry)
|
|
4051
|
+
* // { cid: CID(), ... }
|
|
4052
|
+
* }
|
|
4053
|
+
* ```
|
|
4054
|
+
*/
|
|
4055
|
+
async function* importer(source, blockstore, options = {}) {
|
|
4056
|
+
let candidates;
|
|
4057
|
+
if (Symbol.asyncIterator in source || Symbol.iterator in source) candidates = source;
|
|
4058
|
+
else candidates = [source];
|
|
4059
|
+
let chunkSize;
|
|
4060
|
+
let maxChildrenPerNode;
|
|
4061
|
+
if (options.profile === "unixfs-v0-2015") {
|
|
4062
|
+
options.shardSplitStrategy = options.shardSplitStrategy ?? "links-bytes";
|
|
4063
|
+
options.cidVersion = options.cidVersion ?? 0;
|
|
4064
|
+
options.rawLeaves = options.rawLeaves ?? false;
|
|
4065
|
+
chunkSize = DEFAULT_CHUNK_SIZE_256KIB;
|
|
4066
|
+
maxChildrenPerNode = 174;
|
|
4067
|
+
} else if (options.profile === "unixfs-v1-2025") {
|
|
4068
|
+
options.shardSplitStrategy = options.shardSplitStrategy ?? "block-bytes";
|
|
4069
|
+
options.cidVersion = options.cidVersion ?? 1;
|
|
4070
|
+
options.rawLeaves = options.rawLeaves ?? true;
|
|
4071
|
+
chunkSize = DEFAULT_CHUNK_SIZE_1MIB;
|
|
4072
|
+
maxChildrenPerNode = 1024;
|
|
4073
|
+
}
|
|
4074
|
+
const wrapWithDirectory = options.wrapWithDirectory ?? false;
|
|
4075
|
+
const shardSplitThresholdBytes = options.shardSplitThresholdBytes ?? 262144;
|
|
4076
|
+
const shardSplitStrategy = options.shardSplitStrategy ?? "links-bytes";
|
|
4077
|
+
const shardFanoutBits = options.shardFanoutBits ?? 8;
|
|
4078
|
+
const cidVersion = options.cidVersion ?? 1;
|
|
4079
|
+
const rawLeaves = options.rawLeaves ?? true;
|
|
4080
|
+
const leafType = options.leafType ?? "file";
|
|
4081
|
+
const fileImportConcurrency = options.fileImportConcurrency ?? 50;
|
|
4082
|
+
const blockWriteConcurrency = options.blockWriteConcurrency ?? 10;
|
|
4083
|
+
const reduceSingleLeafToSelf = options.reduceSingleLeafToSelf ?? true;
|
|
4084
|
+
const chunker = options.chunker ?? fixedSize({ chunkSize });
|
|
4085
|
+
const chunkValidator = options.chunkValidator ?? defaultChunkValidator();
|
|
4086
|
+
const buildDag = options.dagBuilder ?? defaultDagBuilder({
|
|
4087
|
+
chunker,
|
|
4088
|
+
chunkValidator,
|
|
4089
|
+
wrapWithDirectory,
|
|
4090
|
+
layout: options.layout ?? balanced({ maxChildrenPerNode }),
|
|
4091
|
+
bufferImporter: options.bufferImporter ?? defaultBufferImporter({
|
|
4092
|
+
cidVersion,
|
|
4093
|
+
rawLeaves,
|
|
4094
|
+
leafType,
|
|
4095
|
+
onProgress: options.onProgress
|
|
4096
|
+
}),
|
|
4097
|
+
blockWriteConcurrency,
|
|
4098
|
+
reduceSingleLeafToSelf,
|
|
4099
|
+
cidVersion,
|
|
4100
|
+
onProgress: options.onProgress,
|
|
4101
|
+
dirBuilder: options.dirBuilder,
|
|
4102
|
+
fileBuilder: options.fileBuilder
|
|
4103
|
+
});
|
|
4104
|
+
const buildTree = options.treeBuilder ?? defaultTreeBuilder({
|
|
4105
|
+
wrapWithDirectory,
|
|
4106
|
+
shardSplitThresholdBytes,
|
|
4107
|
+
shardSplitStrategy,
|
|
4108
|
+
shardFanoutBits,
|
|
4109
|
+
cidVersion,
|
|
4110
|
+
onProgress: options.onProgress
|
|
4111
|
+
});
|
|
4112
|
+
for await (const entry of buildTree(parallelBatch(buildDag(candidates, blockstore), fileImportConcurrency), blockstore)) yield {
|
|
4113
|
+
cid: entry.cid,
|
|
4114
|
+
path: entry.path,
|
|
4115
|
+
unixfs: entry.unixfs,
|
|
4116
|
+
size: entry.size
|
|
4117
|
+
};
|
|
4118
|
+
}
|
|
4119
|
+
/**
|
|
4120
|
+
* `importFile` is similar to `importer` except it accepts a single
|
|
4121
|
+
* `FileCandidate` and returns a promise of a single `ImportResult`
|
|
4122
|
+
* instead of a stream of results.
|
|
4123
|
+
*
|
|
4124
|
+
* @example
|
|
4125
|
+
*
|
|
4126
|
+
* ```typescript
|
|
4127
|
+
* import { importFile } from 'ipfs-unixfs-importer'
|
|
4128
|
+
* import { MemoryBlockstore } from 'blockstore-core'
|
|
4129
|
+
*
|
|
4130
|
+
* // store blocks in memory, other blockstores are available
|
|
4131
|
+
* const blockstore = new MemoryBlockstore()
|
|
4132
|
+
*
|
|
4133
|
+
* const input: FileCandidate = {
|
|
4134
|
+
* path: './foo.txt',
|
|
4135
|
+
* content: Uint8Array.from([0, 1, 2, 3, 4])
|
|
4136
|
+
* }
|
|
4137
|
+
*
|
|
4138
|
+
* const entry = await importFile(input, blockstore)
|
|
4139
|
+
* ```
|
|
4140
|
+
*/
|
|
4141
|
+
async function importFile(content, blockstore, options = {}) {
|
|
4142
|
+
const result = await first(importer([content], blockstore, options));
|
|
4143
|
+
if (result == null) throw new InvalidParametersError("Nothing imported");
|
|
4144
|
+
return result;
|
|
4145
|
+
}
|
|
4146
|
+
//#endregion
|
|
4147
|
+
export { importFile };
|