@@ -6,13 +6,11 @@ const pull = require('pull-stream')
6
6
const through = require ( 'pull-through' )
7
7
const parallel = require ( 'async/parallel' )
8
8
const waterfall = require ( 'async/waterfall' )
9
- const dagPB = require ( 'ipld-dag-pb' )
10
- const CID = require ( 'cids' )
11
- const multihash = require ( 'multihashing-async' )
12
-
9
+ const persist = require ( '../utils/persist' )
13
10
const reduce = require ( './reduce' )
14
-
15
- const DAGNode = dagPB . DAGNode
11
+ const {
12
+ DAGNode
13
+ } = require ( 'ipld-dag-pb' )
16
14
17
15
const defaultOptions = {
18
16
chunkerOptions : {
@@ -27,12 +25,6 @@ const defaultOptions = {
27
25
28
26
module . exports = function builder ( createChunker , ipld , createReducer , _options ) {
29
27
const options = extend ( { } , defaultOptions , _options )
30
- options . cidVersion = options . cidVersion || options . cidVersion
31
- options . hashAlg = options . hashAlg || defaultOptions . hashAlg
32
-
33
- if ( options . hashAlg !== 'sha2-256' ) {
34
- options . cidVersion = 1
35
- }
36
28
37
29
return function ( source ) {
38
30
return function ( items , cb ) {
@@ -71,33 +63,17 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
71
63
const d = new UnixFS ( 'directory' )
72
64
73
65
waterfall ( [
74
- ( cb ) => DAGNode . create ( d . marshal ( ) , [ ] , options . hashAlg , cb ) ,
75
- ( node , cb ) => {
76
- if ( options . onlyHash ) {
77
- return cb ( null , node )
78
- }
79
-
80
- const cid = new CID ( options . cidVersion , 'dag-pb' , node . multihash )
81
-
82
- node = new DAGNode (
83
- node . data ,
84
- node . links ,
85
- node . serialized ,
86
- cid
87
- )
88
-
89
- ipld . put ( node , {
90
- cid
91
- } , ( err ) => cb ( err , node ) )
92
- }
93
- ] , ( err , node ) => {
66
+ ( cb ) => DAGNode . create ( d . marshal ( ) , [ ] , cb ) ,
67
+ ( node , cb ) => persist ( node , ipld , options , cb )
68
+ ] , ( err , result ) => {
94
69
if ( err ) {
95
70
return callback ( err )
96
71
}
72
+
97
73
callback ( null , {
98
74
path : item . path ,
99
- multihash : node . multihash ,
100
- size : node . size
75
+ multihash : result . cid . buffer ,
76
+ size : result . node . size
101
77
} )
102
78
} )
103
79
}
@@ -134,55 +110,42 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
134
110
} ) ,
135
111
pull . asyncMap ( ( buffer , callback ) => {
136
112
if ( options . rawLeaves ) {
137
- return multihash ( buffer , options . hashAlg , ( error , hash ) => {
138
- if ( error ) {
139
- return callback ( error )
140
- }
141
-
142
- return callback ( null , {
143
- multihash : hash ,
144
- size : buffer . length ,
145
- leafSize : buffer . length ,
146
- cid : new CID ( 1 , 'raw' , hash ) ,
147
- data : buffer
148
- } )
113
+ return callback ( null , {
114
+ size : buffer . length ,
115
+ leafSize : buffer . length ,
116
+ data : buffer
149
117
} )
150
118
}
151
119
152
120
const file = new UnixFS ( options . leafType , buffer )
153
121
154
- DAGNode . create ( file . marshal ( ) , [ ] , options . hashAlg , ( err , node ) => {
122
+ DAGNode . create ( file . marshal ( ) , [ ] , ( err , node ) => {
155
123
if ( err ) {
156
124
return callback ( err )
157
125
}
158
126
159
127
callback ( null , {
160
- multihash : node . multihash ,
161
128
size : node . size ,
162
129
leafSize : file . fileSize ( ) ,
163
- cid : new CID ( options . cidVersion , 'dag-pb' , node . multihash ) ,
164
130
data : node
165
131
} )
166
132
} )
167
133
} ) ,
168
134
pull . asyncMap ( ( leaf , callback ) => {
169
- if ( options . onlyHash ) {
170
- return callback ( null , leaf )
171
- }
135
+ persist ( leaf . data , ipld , options , ( error , results ) => {
136
+ if ( error ) {
137
+ return callback ( error )
138
+ }
172
139
173
- ipld . put ( leaf . data , {
174
- cid : leaf . cid
175
- } , ( error ) => callback ( error , leaf ) )
176
- } ) ,
177
- pull . map ( ( leaf ) => {
178
- return {
179
- path : file . path ,
180
- multihash : leaf . cid . buffer ,
181
- size : leaf . size ,
182
- leafSize : leaf . leafSize ,
183
- name : '' ,
184
- cid : leaf . cid
185
- }
140
+ callback ( null , {
141
+ size : leaf . size ,
142
+ leafSize : leaf . leafSize ,
143
+ data : results . node ,
144
+ multihash : results . cid . buffer ,
145
+ path : leaf . path ,
146
+ name : ''
147
+ } )
148
+ } )
186
149
} ) ,
187
150
through ( // mark as single node if only one single node
188
151
function onData ( data ) {
0 commit comments