-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathlib.js
More file actions
765 lines (720 loc) · 23.6 KB
/
lib.js
File metadata and controls
765 lines (720 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/**
* A client library for the https://nft.storage/ service. It provides a convenient
* interface for working with the [Raw HTTP API](https://nft.storage/#api-docs)
* from a web browser or [Node.js](https://nodejs.org/) and comes bundled with
* TS for out-of-the box type inference and better IntelliSense.
*
* @example
* ```js
* import { NFTStorage, File, Blob } from "nft.storage"
* const client = new NFTStorage({ token: API_TOKEN })
*
* const cid = await client.storeBlob(new Blob(['hello world']))
* ```
* @module
*/
import { transform } from 'streaming-iterables'
import pRetry, { AbortError } from 'p-retry'
import { TreewalkCarSplitter } from 'carbites/treewalk'
import { pack } from 'ipfs-car/pack'
import { CID } from 'multiformats/cid'
import throttledQueue from 'throttled-queue'
import * as Token from './token.js'
import { fetch, File, Blob, FormData, Blockstore } from './platform.js'
import { toGatewayURL } from './gateway.js'
import { BlockstoreCarReader } from './bs-car-reader.js'
import pipe from 'it-pipe'
const MAX_STORE_RETRIES = 5
const MAX_CONCURRENT_UPLOADS = 3
const MAX_CHUNK_SIZE = 1024 * 1024 * 10 // chunk to ~10MB CARs
const RATE_LIMIT_REQUESTS = 30
const RATE_LIMIT_PERIOD = 10 * 1000
/**
* @typedef {import('./lib/interface.js').Service} Service
* @typedef {import('./lib/interface.js').CIDString} CIDString
* @typedef {import('./lib/interface.js').Deal} Deal
* @typedef {import('./lib/interface.js').FileObject} FileObject
* @typedef {import('./lib/interface.js').FilesSource} FilesSource
* @typedef {import('./lib/interface.js').Pin} Pin
* @typedef {import('./lib/interface.js').CarReader} CarReader
* @typedef {import('ipfs-car/blockstore').Blockstore} BlockstoreI
* @typedef {import('./lib/interface.js').RateLimiter} RateLimiter
*/
/**
* @returns {RateLimiter}
*/
export function createRateLimiter() {
const throttle = throttledQueue(RATE_LIMIT_REQUESTS, RATE_LIMIT_PERIOD)
return () => throttle(() => {})
}
/**
* Rate limiter used by static API if no rate limiter is passed. Note that each
* instance of the NFTStorage class gets it's own limiter if none is passed.
* This is because rate limits are enforced per API token.
*/
const globalRateLimiter = createRateLimiter()
/**
* @template {import('./lib/interface.js').TokenInput} T
* @typedef {import('./lib/interface.js').Token<T>} TokenType
*/
/**
* @implements Service
*/
class NFTStorage {
/**
* Constructs a client bound to the given `options.token` and
* `options.endpoint`.
*
* @example
* ```js
* import { NFTStorage, File, Blob } from "nft.storage"
* const client = new NFTStorage({ token: API_TOKEN })
*
* const cid = await client.storeBlob(new Blob(['hello world']))
* ```
* Optionally you could pass an alternative API endpoint (e.g. for testing)
* @example
* ```js
* import { NFTStorage } from "nft.storage"
* const client = new NFTStorage({
* token: API_TOKEN
* endpoint: new URL('http://localhost:8080/')
* })
* ```
*
* @param {{token: string, endpoint?: URL, rateLimiter?: RateLimiter}} options
*/
constructor({
token,
endpoint = new URL('https://api.nft.storage'),
rateLimiter,
}) {
/**
* Authorization token.
*
* @readonly
*/
this.token = token
/**
* Service API endpoint `URL`.
* @readonly
*/
this.endpoint = endpoint
/**
* @readonly
*/
this.rateLimiter = rateLimiter || createRateLimiter()
}
/**
* @hidden
* @param {string} token
*/
static auth(token) {
if (!token) throw new Error('missing token')
return { Authorization: `Bearer ${token}`, 'X-Client': 'nft.storage/js' }
}
/**
* Stores a single file and returns its CID.
*
* @param {Service} service
* @param {Blob} blob
* @returns {Promise<CIDString>}
*/
static async storeBlob(service, blob) {
const blockstore = new Blockstore()
let cidString
try {
const { cid, car } = await NFTStorage.encodeBlob(blob, { blockstore })
await NFTStorage.storeCar(service, car)
cidString = cid.toString()
} finally {
await blockstore.close()
}
return cidString
}
/**
* Stores a CAR file and returns its root CID.
*
* @param {Service} service
* @param {Blob|CarReader} car
* @param {import('./lib/interface.js').CarStorerOptions} [options]
* @returns {Promise<CIDString>}
*/
static async storeCar(
{ endpoint, token, rateLimiter = globalRateLimiter },
car,
{ onStoredChunk, maxRetries, decoders } = {}
) {
const url = new URL('upload/', endpoint)
const headers = NFTStorage.auth(token)
const targetSize = MAX_CHUNK_SIZE
const splitter =
car instanceof Blob
? await TreewalkCarSplitter.fromBlob(car, targetSize, { decoders })
: new TreewalkCarSplitter(car, targetSize, { decoders })
const upload = transform(
MAX_CONCURRENT_UPLOADS,
async function (/** @type {AsyncIterable<Uint8Array>} */ car) {
const carParts = []
for await (const part of car) {
carParts.push(part)
}
const carFile = new Blob(carParts, { type: 'application/vnd.ipld.car' })
const cid = await pRetry(
async () => {
await rateLimiter()
const response = await fetch(url.toString(), {
method: 'POST',
headers,
body: carFile,
})
/* c8 ignore next 3 */
if (response.status === 429) {
throw new Error('rate limited')
}
const result = await response.json()
if (!result.ok) {
// do not retry if unauthorized - will not succeed
if (response.status === 401) {
throw new AbortError(result.error.message)
}
throw new Error(result.error.message)
}
return result.value.cid
},
{
retries: maxRetries == null ? MAX_STORE_RETRIES : maxRetries,
}
)
onStoredChunk && onStoredChunk(carFile.size)
return cid
}
)
let root
for await (const cid of upload(splitter.cars())) {
root = cid
}
return /** @type {CIDString} */ (root)
}
/**
* Stores a directory of files and returns a CID. Provided files **MUST**
* be within the same directory, otherwise error is raised e.g. `foo/bar.png`,
* `foo/bla/baz.json` is ok but `foo/bar.png`, `bla/baz.json` is not.
*
* @param {Service} service
* @param {FilesSource} filesSource
* @returns {Promise<CIDString>}
*/
static async storeDirectory(service, filesSource) {
const blockstore = new Blockstore()
let cidString
try {
const { cid, car } = await NFTStorage.encodeDirectory(filesSource, {
blockstore,
})
await NFTStorage.storeCar(service, car)
cidString = cid.toString()
} finally {
await blockstore.close()
}
return cidString
}
/**
* Stores the given token and all resources it references (in the form of a
* File or a Blob) along with a metadata JSON as specificed in ERC-1155. The
* `token.image` must be either a `File` or a `Blob` instance, which will be
* stored and the corresponding content address URL will be saved in the
* metadata JSON file under `image` field.
*
* If `token.properties` contains properties with `File` or `Blob` values,
* those also get stored and their URLs will be saved in the metadata JSON
* file in their place.
*
* Note: URLs for `File` objects will retain file names e.g. in case of
* `new File([bytes], 'cat.png', { type: 'image/png' })` will be transformed
* into a URL that looks like `ipfs://bafy...hash/image/cat.png`. For `Blob`
* objects, the URL will not have a file name name or mime type, instead it
* will be transformed into a URL that looks like
* `ipfs://bafy...hash/image/blob`.
*
* @template {import('./lib/interface.js').TokenInput} T
* @param {Service} service
* @param {T} metadata
* @returns {Promise<TokenType<T>>}
*/
static async store(service, metadata) {
const { token, car } = await NFTStorage.encodeNFT(metadata)
await NFTStorage.storeCar(service, car)
return token
}
/**
* Returns current status of the stored NFT by its CID. Note the NFT must
* have previously been stored by this account.
*
* @param {Service} service
* @param {string} cid
* @returns {Promise<import('./lib/interface.js').StatusResult>}
*/
static async status(
{ endpoint, token, rateLimiter = globalRateLimiter },
cid
) {
const url = new URL(`${cid}/`, endpoint)
await rateLimiter()
const response = await fetch(url.toString(), {
method: 'GET',
headers: NFTStorage.auth(token),
})
/* c8 ignore next 3 */
if (response.status === 429) {
throw new Error('rate limited')
}
const result = await response.json()
if (result.ok) {
return {
cid: result.value.cid,
deals: decodeDeals(result.value.deals),
size: result.value.size,
pin: decodePin(result.value.pin),
created: new Date(result.value.created),
}
} else {
throw new Error(result.error.message)
}
}
/**
* Check if a CID of an NFT is being stored by NFT.Storage.
*
* @param {import('./lib/interface.js').PublicService} service
* @param {string} cid
* @returns {Promise<import('./lib/interface.js').CheckResult>}
*/
static async check({ endpoint, rateLimiter = globalRateLimiter }, cid) {
const url = new URL(`check/${cid}/`, endpoint)
await rateLimiter()
const response = await fetch(url.toString())
/* c8 ignore next 3 */
if (response.status === 429) {
throw new Error('rate limited')
}
const result = await response.json()
if (result.ok) {
return {
cid: result.value.cid,
deals: decodeDeals(result.value.deals),
pin: result.value.pin,
}
} else {
throw new Error(result.error.message)
}
}
/**
* Removes stored content by its CID from this account. Please note that
* even if content is removed from the service other nodes that have
* replicated it might still continue providing it.
*
* @param {Service} service
* @param {string} cid
* @returns {Promise<void>}
*/
static async delete(
{ endpoint, token, rateLimiter = globalRateLimiter },
cid
) {
const url = new URL(`${cid}/`, endpoint)
await rateLimiter()
const response = await fetch(url.toString(), {
method: 'DELETE',
headers: NFTStorage.auth(token),
})
/* c8 ignore next 3 */
if (response.status === 429) {
throw new Error('rate limited')
}
const result = await response.json()
if (!result.ok) {
throw new Error(result.error.message)
}
}
/**
* Encodes the given token and all resources it references (in the form of a
* File or a Blob) along with a metadata JSON as specificed in ERC-1155 to a
* CAR file. The `token.image` must be either a `File` or a `Blob` instance,
* which will be stored and the corresponding content address URL will be
* saved in the metadata JSON file under `image` field.
*
* If `token.properties` contains properties with `File` or `Blob` values,
* those also get stored and their URLs will be saved in the metadata JSON
* file in their place.
*
* Note: URLs for `File` objects will retain file names e.g. in case of
* `new File([bytes], 'cat.png', { type: 'image/png' })` will be transformed
* into a URL that looks like `ipfs://bafy...hash/image/cat.png`. For `Blob`
* objects, the URL will not have a file name name or mime type, instead it
* will be transformed into a URL that looks like
* `ipfs://bafy...hash/image/blob`.
*
* @example
* ```js
* const { token, car } = await NFTStorage.encodeNFT({
* name: 'nft.storage store test',
* description: 'Test ERC-1155 compatible metadata.',
* image: new File(['<DATA>'], 'pinpie.jpg', { type: 'image/jpg' }),
* properties: {
* custom: 'Custom data can appear here, files are auto uploaded.',
* file: new File(['<DATA>'], 'README.md', { type: 'text/plain' }),
* }
* })
*
* console.log('IPFS URL for the metadata:', token.url)
* console.log('metadata.json contents:\n', token.data)
* console.log('metadata.json with IPFS gateway URLs:\n', token.embed())
*
* // Now store the CAR file on NFT.Storage
* await client.storeCar(car)
* ```
*
* @template {import('./lib/interface.js').TokenInput} T
* @param {T} input
* @returns {Promise<{ cid: CID, token: TokenType<T>, car: CarReader }>}
*/
static async encodeNFT(input) {
validateERC1155(input)
return Token.Token.encode(input)
}
/**
* Encodes a single file to a CAR file and also returns its root CID.
*
* @example
* ```js
* const content = new Blob(['hello world'])
* const { cid, car } = await NFTStorage.encodeBlob(content)
*
* // Root CID of the file
* console.log(cid.toString())
*
* // Now store the CAR file on NFT.Storage
* await client.storeCar(car)
* ```
*
* @param {Blob} blob
* @param {object} [options]
* @param {BlockstoreI} [options.blockstore]
* @returns {Promise<{ cid: CID, car: CarReader }>}
*/
static async encodeBlob(blob, { blockstore } = {}) {
if (blob.size === 0) {
throw new Error('Content size is 0, make sure to provide some content')
}
return packCar([toImportCandidate('blob', blob)], {
blockstore,
wrapWithDirectory: false,
})
}
/**
* Encodes a directory of files to a CAR file and also returns the root CID.
* Provided files **MUST** be within the same directory, otherwise error is
* raised e.g. `foo/bar.png`, `foo/bla/baz.json` is ok but `foo/bar.png`,
* `bla/baz.json` is not.
*
* @example
* ```js
* const { cid, car } = await NFTStorage.encodeDirectory([
* new File(['hello world'], 'hello.txt'),
* new File([JSON.stringify({'from': 'incognito'}, null, 2)], 'metadata.json')
* ])
*
* // Root CID of the directory
* console.log(cid.toString())
*
* // Now store the CAR file on NFT.Storage
* await client.storeCar(car)
* ```
*
* @param {FilesSource} files
* @param {object} [options]
* @param {BlockstoreI} [options.blockstore]
* @returns {Promise<{ cid: CID, car: CarReader }>}
*/
static async encodeDirectory(files, { blockstore } = {}) {
let size = 0
const input = pipe(files, async function* (files) {
for await (const file of files) {
yield toImportCandidate(file.name, file)
size += file.size
}
})
const packed = await packCar(input, {
blockstore,
wrapWithDirectory: true,
})
if (size === 0) {
throw new Error(
'Total size of files should exceed 0, make sure to provide some content'
)
}
return packed
}
// Just a sugar so you don't have to pass around endpoint and token around.
/**
* Stores a single file and returns the corresponding Content Identifier (CID).
* Takes a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob)
* or a [File](https://developer.mozilla.org/en-US/docs/Web/API/File). Note
* that no file name or file metadata is retained.
*
* @example
* ```js
* const content = new Blob(['hello world'])
* const cid = await client.storeBlob(content)
* cid //> 'zdj7Wn9FQAURCP6MbwcWuzi7u65kAsXCdjNTkhbJcoaXBusq9'
* ```
*
* @param {Blob} blob
*/
storeBlob(blob) {
return NFTStorage.storeBlob(this, blob)
}
/**
* Stores files encoded as a single [Content Addressed Archive
* (CAR)](https://github.com/ipld/specs/blob/master/block-layer/content-addressable-archives.md).
*
* Takes a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob)
* or a [File](https://developer.mozilla.org/en-US/docs/Web/API/File).
*
* Returns the corresponding Content Identifier (CID).
*
* See the [`ipfs-car` docs](https://www.npmjs.com/package/ipfs-car) for more
* details on packing a CAR file.
*
* @example
* ```js
* import { pack } from 'ipfs-car/pack'
* import { CarReader } from '@ipld/car'
* const { out, root } = await pack({
* input: fs.createReadStream('pinpie.pdf')
* })
* const expectedCid = root.toString()
* const carReader = await CarReader.fromIterable(out)
* const cid = await storage.storeCar(carReader)
* console.assert(cid === expectedCid)
* ```
*
* @example
* ```
* import { packToBlob } from 'ipfs-car/pack/blob'
* const data = 'Hello world'
* const { root, car } = await packToBlob({ input: [new TextEncoder().encode(data)] })
* const expectedCid = root.toString()
* const cid = await client.storeCar(car)
* console.assert(cid === expectedCid)
* ```
* @param {Blob|CarReader} car
* @param {import('./lib/interface.js').CarStorerOptions} [options]
*/
storeCar(car, options) {
return NFTStorage.storeCar(this, car, options)
}
/**
* Stores a directory of files and returns a CID for the directory.
*
* @example
* ```js
* const cid = await client.storeDirectory([
* new File(['hello world'], 'hello.txt'),
* new File([JSON.stringify({'from': 'incognito'}, null, 2)], 'metadata.json')
* ])
* cid //>
* ```
*
* Argument can be a [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList)
* instance as well, in which case directory structure will be retained.
*
* @param {FilesSource} files
*/
storeDirectory(files) {
return NFTStorage.storeDirectory(this, files)
}
/**
* Returns current status of the stored NFT by its CID. Note the NFT must
* have previously been stored by this account.
*
* @example
* ```js
* const status = await client.status('zdj7Wn9FQAURCP6MbwcWuzi7u65kAsXCdjNTkhbJcoaXBusq9')
* ```
*
* @param {string} cid
*/
status(cid) {
return NFTStorage.status(this, cid)
}
/**
* Removes stored content by its CID from the service.
*
* > Please note that even if content is removed from the service other nodes
* that have replicated it might still continue providing it.
*
* @example
* ```js
* await client.delete('zdj7Wn9FQAURCP6MbwcWuzi7u65kAsXCdjNTkhbJcoaXBusq9')
* ```
*
* @param {string} cid
*/
delete(cid) {
return NFTStorage.delete(this, cid)
}
/**
* Check if a CID of an NFT is being stored by nft.storage. Throws if the NFT
* was not found.
*
* @example
* ```js
* const status = await client.check('zdj7Wn9FQAURCP6MbwcWuzi7u65kAsXCdjNTkhbJcoaXBusq9')
* ```
*
* @param {string} cid
*/
check(cid) {
return NFTStorage.check(this, cid)
}
/**
* Stores the given token and all resources it references (in the form of a
* File or a Blob) along with a metadata JSON as specificed in
* [ERC-1155](https://eips.ethereum.org/EIPS/eip-1155#metadata). The
* `token.image` must be either a `File` or a `Blob` instance, which will be
* stored and the corresponding content address URL will be saved in the
* metadata JSON file under `image` field.
*
* If `token.properties` contains properties with `File` or `Blob` values,
* those also get stored and their URLs will be saved in the metadata JSON
* file in their place.
*
* Note: URLs for `File` objects will retain file names e.g. in case of
* `new File([bytes], 'cat.png', { type: 'image/png' })` will be transformed
* into a URL that looks like `ipfs://bafy...hash/image/cat.png`. For `Blob`
* objects, the URL will not have a file name name or mime type, instead it
* will be transformed into a URL that looks like
* `ipfs://bafy...hash/image/blob`.
*
* @example
* ```js
* const metadata = await client.store({
* name: 'nft.storage store test',
* description: 'Test ERC-1155 compatible metadata.',
* image: new File(['<DATA>'], 'pinpie.jpg', { type: 'image/jpg' }),
* properties: {
* custom: 'Custom data can appear here, files are auto uploaded.',
* file: new File(['<DATA>'], 'README.md', { type: 'text/plain' }),
* }
* })
*
* console.log('IPFS URL for the metadata:', metadata.url)
* console.log('metadata.json contents:\n', metadata.data)
* console.log('metadata.json with IPFS gateway URLs:\n', metadata.embed())
* ```
*
* @template {import('./lib/interface.js').TokenInput} T
* @param {T} token
*/
store(token) {
return NFTStorage.store(this, token)
}
}
/**
* Cast an iterable to an asyncIterable
* @template T
* @param {Iterable<T>} iterable
* @returns {AsyncIterable<T>}
*/
export function toAsyncIterable(iterable) {
return (async function* () {
for (const item of iterable) {
yield item
}
})()
}
/**
* @template {import('./lib/interface.js').TokenInput} T
* @param {T} metadata
*/
const validateERC1155 = ({ name, description, image, decimals }) => {
// Just validate that expected fields are present
if (typeof name !== 'string') {
throw new TypeError(
'string property `name` identifying the asset is required'
)
}
if (typeof description !== 'string') {
throw new TypeError(
'string property `description` describing asset is required'
)
}
if (!(image instanceof Blob)) {
throw new TypeError('property `image` must be a Blob or File object')
} else if (!image.type.startsWith('image/')) {
console.warn(`According to ERC721 Metadata JSON Schema 'image' must have 'image/*' mime type.
For better interoperability we would highly recommend storing content with different mime type under 'properties' namespace e.g. \`properties: { video: file }\` and using 'image' field for storing a preview image for it instead.
For more context please see ERC-721 specification https://eips.ethereum.org/EIPS/eip-721`)
}
if (typeof decimals !== 'undefined' && typeof decimals !== 'number') {
throw new TypeError('property `decimals` must be an integer value')
}
}
/**
* @param {import('ipfs-car/pack').ImportCandidateStream|Array<{ path: string, content: import('./platform.js').ReadableStream }>} input
* @param {object} [options]
* @param {BlockstoreI} [options.blockstore]
* @param {boolean} [options.wrapWithDirectory]
*/
const packCar = async (input, { blockstore, wrapWithDirectory } = {}) => {
/* c8 ignore next 1 */
blockstore = blockstore || new Blockstore()
const { root: cid } = await pack({ input, blockstore, wrapWithDirectory })
const car = new BlockstoreCarReader(1, [cid], blockstore)
return { cid, car }
}
/**
* @param {Deal[]} deals
* @returns {Deal[]}
*/
const decodeDeals = (deals) =>
deals.map((deal) => {
const { dealActivation, dealExpiration, lastChanged } = {
dealExpiration: null,
dealActivation: null,
...deal,
}
return {
...deal,
lastChanged: new Date(lastChanged),
...(dealActivation && { dealActivation: new Date(dealActivation) }),
...(dealExpiration && { dealExpiration: new Date(dealExpiration) }),
}
})
/**
* @param {Pin} pin
* @returns {Pin}
*/
const decodePin = (pin) => ({ ...pin, created: new Date(pin.created) })
/**
* Convert the passed blob to an "import candidate" - an object suitable for
* passing to the ipfs-unixfs-importer. Note: content is an accessor so that
* the stream is created only when needed.
*
* @param {string} path
* @param {Pick<Blob, 'stream'>|{ stream: () => AsyncIterable<Uint8Array> }} blob
* @returns {import('ipfs-core-types/src/utils.js').ImportCandidate}
*/
function toImportCandidate(path, blob) {
/** @type {AsyncIterable<Uint8Array>} */
let stream
return {
path,
get content() {
stream = stream || blob.stream()
return stream
},
}
}
export { NFTStorage, File, Blob, FormData, toGatewayURL, Token }