1
1
'use strict'
2
2
3
3
const importer = require ( 'ipfs-unixfs-importer' )
4
+ const toAsyncIterator = require ( 'pull-stream-to-async-iterator' )
5
+ const toPullStream = require ( 'async-iterator-to-pull-stream' )
4
6
const pull = require ( 'pull-stream' )
5
7
const toPull = require ( 'stream-to-pull-stream' )
6
8
const waterfall = require ( 'async/waterfall' )
7
9
const isStream = require ( 'is-stream' )
8
10
const isSource = require ( 'is-pull-stream' ) . isSource
9
- const CID = require ( 'cids' )
10
11
const { parseChunkerString } = require ( './utils' )
11
12
12
13
const WRAPPER = 'wrapper/'
@@ -16,30 +17,28 @@ function noop () {}
16
17
function prepareFile ( file , self , opts , callback ) {
17
18
opts = opts || { }
18
19
19
- let cid = new CID ( file . multihash )
20
-
21
- if ( opts . cidVersion === 1 ) {
22
- cid = cid . toV1 ( )
23
- }
20
+ let cid = file . cid
24
21
25
22
waterfall ( [
26
23
( cb ) => opts . onlyHash
27
24
? cb ( null , file )
28
- : self . object . get ( file . multihash , Object . assign ( { } , opts , { preload : false } ) , cb ) ,
25
+ : self . object . get ( file . cid , Object . assign ( { } , opts , { preload : false } ) , cb ) ,
29
26
( node , cb ) => {
30
- const b58Hash = cid . toBaseEncodedString ( )
27
+ if ( opts . cidVersion === 1 ) {
28
+ cid = cid . toV1 ( )
29
+ }
31
30
31
+ const b58Hash = cid . toBaseEncodedString ( )
32
32
let size = node . size
33
33
34
34
if ( Buffer . isBuffer ( node ) ) {
35
35
size = node . length
36
36
}
37
37
38
38
cb ( null , {
39
- path : opts . wrapWithDirectory
40
- ? file . path . substring ( WRAPPER . length )
41
- : ( file . path || b58Hash ) ,
39
+ path : file . path === undefined ? b58Hash : ( file . path || '' ) ,
42
40
hash : b58Hash ,
41
+ // multihash: b58Hash,
43
42
size
44
43
} )
45
44
}
@@ -80,16 +79,16 @@ function normalizeContent (content, opts) {
80
79
throw new Error ( 'Must provide a path when wrapping with a directory' )
81
80
}
82
81
83
- if ( opts . wrapWithDirectory ) {
84
- data . path = WRAPPER + data . path
85
- }
82
+ // if (opts.wrapWithDirectory) {
83
+ // data.path = WRAPPER + data.path
84
+ // }
86
85
87
86
return data
88
87
} )
89
88
}
90
89
91
90
function preloadFile ( file , self , opts ) {
92
- const isRootFile = opts . wrapWithDirectory
91
+ const isRootFile = ! file . path || opts . wrapWithDirectory
93
92
? file . path === ''
94
93
: ! file . path . includes ( '/' )
95
94
@@ -130,7 +129,10 @@ module.exports = function (self) {
130
129
shardSplitThreshold : self . _options . EXPERIMENTAL . sharding
131
130
? 1000
132
131
: Infinity
133
- } , options , chunkerOptions )
132
+ } , options , {
133
+ ...chunkerOptions . chunkerOptions ,
134
+ chunker : chunkerOptions . chunker
135
+ } )
134
136
135
137
// CID v0 is for multihashes encoded with sha2-256
136
138
if ( opts . hashAlg && opts . cidVersion !== 1 ) {
@@ -149,7 +151,11 @@ module.exports = function (self) {
149
151
return pull (
150
152
pull . map ( content => normalizeContent ( content , opts ) ) ,
151
153
pull . flatten ( ) ,
152
- importer ( self . _ipld , opts ) ,
154
+ pull . map ( file => ( {
155
+ path : file . path ? file . path : undefined ,
156
+ content : file . content ? toAsyncIterator ( file . content ) : undefined
157
+ } ) ) ,
158
+ toPullStream . transform ( source => importer ( source , self . _ipld , opts ) ) ,
153
159
pull . asyncMap ( ( file , cb ) => prepareFile ( file , self , opts , cb ) ) ,
154
160
pull . map ( file => preloadFile ( file , self , opts ) ) ,
155
161
pull . asyncMap ( ( file , cb ) => pinFile ( file , self , opts , cb ) )
0 commit comments