1
1
import type { CID } from 'multiformats/cid'
2
- import type { Blockstore } from 'interface-blockstore '
2
+ import type { Blockstore } from 'ipfs-unixfs-importer '
3
3
import { ByteStream , DirectoryCandidate , FileCandidate , importBytes , importByteStream , ImportCandidateStream , importDirectory , importer , ImporterOptions , importFile , ImportResult } from 'ipfs-unixfs-importer'
4
+ import { balanced } from 'ipfs-unixfs-importer/layout'
5
+ import { fixedSize } from 'ipfs-unixfs-importer/chunker'
6
+
7
+ /**
8
+ * Default importer settings match Filecoin
9
+ */
10
+ const defaultImporterSettings : ImporterOptions = {
11
+ cidVersion : 1 ,
12
+ rawLeaves : true ,
13
+ layout : balanced ( {
14
+ maxChildrenPerNode : 1024
15
+ } ) ,
16
+ chunker : fixedSize ( {
17
+ chunkSize : 1048576
18
+ } )
19
+ }
4
20
5
21
export async function * addAll ( source : ImportCandidateStream , blockstore : Blockstore , options : Partial < ImporterOptions > = { } ) : AsyncGenerator < ImportResult , void , unknown > {
6
- yield * importer ( source , blockstore , options )
22
+ yield * importer ( source , blockstore , {
23
+ ...defaultImporterSettings ,
24
+ ...options
25
+ } )
7
26
}
8
27
9
28
export async function addBytes ( bytes : Uint8Array , blockstore : Blockstore , options : Partial < ImporterOptions > = { } ) : Promise < CID > {
10
- const { cid } = await importBytes ( bytes , blockstore , options )
29
+ const { cid } = await importBytes ( bytes , blockstore , {
30
+ ...defaultImporterSettings ,
31
+ ...options
32
+ } )
11
33
12
34
return cid
13
35
}
14
36
15
37
export async function addByteStream ( bytes : ByteStream , blockstore : Blockstore , options : Partial < ImporterOptions > = { } ) : Promise < CID > {
16
- const { cid } = await importByteStream ( bytes , blockstore , options )
38
+ const { cid } = await importByteStream ( bytes , blockstore , {
39
+ ...defaultImporterSettings ,
40
+ ...options
41
+ } )
17
42
18
43
return cid
19
44
}
20
45
21
46
export async function addFile ( file : FileCandidate , blockstore : Blockstore , options : Partial < ImporterOptions > = { } ) : Promise < CID > {
22
- const { cid } = await importFile ( file , blockstore , options )
47
+ const { cid } = await importFile ( file , blockstore , {
48
+ ...defaultImporterSettings ,
49
+ ...options
50
+ } )
23
51
24
52
return cid
25
53
}
@@ -28,7 +56,10 @@ export async function addDirectory (dir: Partial<DirectoryCandidate>, blockstore
28
56
const { cid } = await importDirectory ( {
29
57
...dir ,
30
58
path : dir . path ?? '-'
31
- } , blockstore , options )
59
+ } , blockstore , {
60
+ ...defaultImporterSettings ,
61
+ ...options
62
+ } )
32
63
33
64
return cid
34
65
}
0 commit comments