@@ -6,13 +6,11 @@ const pull = require('pull-stream')
66const through = require ( 'pull-through' )
77const parallel = require ( 'async/parallel' )
88const 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' )
1310const reduce = require ( './reduce' )
14-
15- const DAGNode = dagPB . DAGNode
11+ const {
12+ DAGNode
13+ } = require ( 'ipld-dag-pb' )
1614
1715const defaultOptions = {
1816 chunkerOptions : {
@@ -27,12 +25,6 @@ const defaultOptions = {
2725
2826module . exports = function builder ( createChunker , ipld , createReducer , _options ) {
2927 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- }
3628
3729 return function ( source ) {
3830 return function ( items , cb ) {
@@ -71,33 +63,17 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
7163 const d = new UnixFS ( 'directory' )
7264
7365 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 ) => {
9469 if ( err ) {
9570 return callback ( err )
9671 }
72+
9773 callback ( null , {
9874 path : item . path ,
99- multihash : node . multihash ,
100- size : node . size
75+ multihash : result . cid . buffer ,
76+ size : result . node . size
10177 } )
10278 } )
10379 }
@@ -134,55 +110,42 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
134110 } ) ,
135111 pull . asyncMap ( ( buffer , callback ) => {
136112 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
149117 } )
150118 }
151119
152120 const file = new UnixFS ( options . leafType , buffer )
153121
154- DAGNode . create ( file . marshal ( ) , [ ] , options . hashAlg , ( err , node ) => {
122+ DAGNode . create ( file . marshal ( ) , [ ] , ( err , node ) => {
155123 if ( err ) {
156124 return callback ( err )
157125 }
158126
159127 callback ( null , {
160- multihash : node . multihash ,
161128 size : node . size ,
162129 leafSize : file . fileSize ( ) ,
163- cid : new CID ( options . cidVersion , 'dag-pb' , node . multihash ) ,
164130 data : node
165131 } )
166132 } )
167133 } ) ,
168134 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+ }
172139
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+ } )
186149 } ) ,
187150 through ( // mark as single node if only one single node
188151 function onData ( data ) {
0 commit comments