@@ -4,7 +4,8 @@ const mh = require('multihashes')
4
4
const UnixFS = require ( 'ipfs-unixfs' )
5
5
const CID = require ( 'cids' )
6
6
const dagPB = require ( 'ipld-dag-pb' )
7
- const asyncEach = require ( 'async/each' )
7
+ const mapValues = require ( 'async/mapValues' )
8
+ const parallel = require ( 'async/parallel' )
8
9
9
10
const DAGLink = dagPB . DAGLink
10
11
const DAGNode = dagPB . DAGNode
@@ -102,30 +103,17 @@ function createSizeIndex (files) {
102
103
* add as a link to the dirNode
103
104
*/
104
105
function traverse ( tree , sizeIndex , path , ipldResolver , source , done ) {
105
- const keys = Object . keys ( tree )
106
-
107
- let tmp = tree
108
-
109
- asyncEach ( keys , ( key , cb ) => {
110
- if ( isLeaf ( tmp [ key ] ) ) {
111
- cb ( )
112
- } else {
113
- path = path ? path + '/' + key : key
114
- console . log ( '->' , path )
106
+ mapValues ( tree , ( node , key , cb ) => {
107
+ if ( isLeaf ( node ) ) {
108
+ return cb ( null , node )
109
+ }
115
110
116
- traverse ( tmp [ key ] , sizeIndex , path , ipldResolver , source , ( err , multihash ) => {
117
- if ( err ) {
118
- return done ( err )
119
- }
120
- tmp [ key ] = multihash
121
- cb ( )
122
- } )
111
+ traverse ( node , sizeIndex , path ? `${ path } /${ key } ` : key , ipldResolver , source , cb )
112
+ } , ( err , tree ) => {
113
+ if ( err ) {
114
+ return done ( err )
123
115
}
124
- } , ( ) => {
125
- next ( tmp , done )
126
- } )
127
116
128
- function next ( tree , cb ) {
129
117
// at this stage, all keys are multihashes
130
118
// create a dir node
131
119
// add all the multihashes as links
@@ -142,39 +130,36 @@ function traverse (tree, sizeIndex, path, ipldResolver, source, done) {
142
130
node . addRawLink ( link )
143
131
} )
144
132
145
- console . log ( '0---->' , path )
146
- node . multihash ( ( err , multihash ) => {
133
+ parallel ( [
134
+ ( cb ) => node . multihash ( cb ) ,
135
+ ( cb ) => node . size ( cb )
136
+ ] , ( err , res ) => {
147
137
if ( err ) {
148
- return cb ( err )
138
+ return done ( err )
149
139
}
150
- node . size ( ( err , size ) => {
140
+
141
+ const multihash = res [ 0 ]
142
+ const size = res [ 1 ]
143
+
144
+ sizeIndex [ mh . toB58String ( multihash ) ] = size
145
+ ipldResolver . put ( {
146
+ node : node ,
147
+ cid : new CID ( multihash )
148
+ } , ( err ) => {
151
149
if ( err ) {
152
- return cb ( err )
150
+ source . push ( new Error ( 'failed to store dirNode' ) )
151
+ } else if ( path ) {
152
+ source . push ( {
153
+ path : path ,
154
+ multihash : multihash ,
155
+ size : size
156
+ } )
153
157
}
154
158
155
- sizeIndex [ mh . toB58String ( multihash ) ] = size
156
- console . log ( '1---->' , path )
157
-
158
- ipldResolver . put ( {
159
- node : node ,
160
- cid : new CID ( multihash )
161
- } , ( err ) => {
162
- if ( err ) {
163
- source . push ( new Error ( 'failed to store dirNode' ) )
164
- } else if ( path ) {
165
- console . log ( '2---->' , path )
166
- source . push ( {
167
- path : path ,
168
- multihash : multihash ,
169
- size : size
170
- } )
171
- }
172
-
173
- cb ( null , multihash )
174
- } )
159
+ done ( null , multihash )
175
160
} )
176
161
} )
177
- }
162
+ } )
178
163
}
179
164
180
165
function isLeaf ( value ) {
0 commit comments