@@ -9,6 +9,7 @@ const Bucket = require('hamt-sharding/src/bucket')
9
9
const DirSharded = require ( 'ipfs-unixfs-importer/src/importer/dir-sharded' )
10
10
const log = require ( 'debug' ) ( 'ipfs:mfs:core:utils:hamt-utils' )
11
11
const UnixFS = require ( 'ipfs-unixfs' )
12
+ const toMulticodecCode = require ( './to-multicodec-code' )
12
13
13
14
const updateHamtDirectory = ( context , links , bucket , options , callback ) => {
14
15
// update parent with new bit field
@@ -23,15 +24,18 @@ const updateHamtDirectory = (context, links, bucket, options, callback) => {
23
24
} ,
24
25
( parent , done ) => {
25
26
// Persist the new parent DAGNode
26
- context . ipld . put ( parent , {
27
- version : options . cidVersion ,
28
- format : options . codec ,
29
- hashAlg : options . hashAlg ,
30
- hashOnly : ! options . flush
31
- } , ( error , cid ) => done ( error , {
32
- node : parent ,
33
- cid
34
- } ) )
27
+ context . ipld . put (
28
+ parent ,
29
+ toMulticodecCode ( options . codec ) ,
30
+ {
31
+ cidVersion : options . cidVersion ,
32
+ hashAlg : toMulticodecCode ( options . hashAlg ) ,
33
+ hashOnly : ! options . flush
34
+ }
35
+ ) . then (
36
+ ( cid ) => done ( null , { cid, node : parent } ) ,
37
+ ( error ) => done ( error )
38
+ )
35
39
}
36
40
] , callback )
37
41
}
@@ -133,43 +137,41 @@ const generatePath = (context, fileName, rootNode, callback) => {
133
137
134
138
// found subshard
135
139
log ( `Found subshard ${ segment . prefix } ` )
136
- context . ipld . get ( link . cid , ( err , result ) => {
137
- if ( err ) {
138
- return next ( err )
139
- }
140
-
141
- // subshard hasn't been loaded, descend to the next level of the HAMT
142
- if ( ! path [ index - 1 ] ) {
143
- log ( `Loaded new subshard ${ segment . prefix } ` )
144
- const node = result . value
145
-
146
- return recreateHamtLevel ( node . links , rootBucket , segment . bucket , parseInt ( segment . prefix , 16 ) , async ( err , bucket ) => {
147
- if ( err ) {
148
- return next ( err )
149
- }
150
-
151
- const position = await rootBucket . _findNewBucketAndPos ( fileName )
152
-
153
- index ++
154
- path . unshift ( {
155
- bucket : position . bucket ,
156
- prefix : toPrefix ( position . pos ) ,
157
- node : node
140
+ context . ipld . get ( link . cid ) . then (
141
+ ( node ) => {
142
+ // subshard hasn't been loaded, descend to the next level of the HAMT
143
+ if ( ! path [ index - 1 ] ) {
144
+ log ( `Loaded new subshard ${ segment . prefix } ` )
145
+
146
+ return recreateHamtLevel ( node . links , rootBucket , segment . bucket , parseInt ( segment . prefix , 16 ) , async ( err , bucket ) => {
147
+ if ( err ) {
148
+ return next ( err )
149
+ }
150
+
151
+ const position = await rootBucket . _findNewBucketAndPos ( fileName )
152
+
153
+ index ++
154
+ path . unshift ( {
155
+ bucket : position . bucket ,
156
+ prefix : toPrefix ( position . pos ) ,
157
+ node : node
158
+ } )
159
+
160
+ next ( )
158
161
} )
162
+ }
159
163
160
- next ( )
161
- } )
162
- }
163
-
164
- const nextSegment = path [ index - 1 ]
164
+ const nextSegment = path [ index - 1 ]
165
165
166
- // add intermediate links to bucket
167
- addLinksToHamtBucket ( result . value . links , nextSegment . bucket , rootBucket , ( error ) => {
168
- nextSegment . node = result . value
166
+ // add intermediate links to bucket
167
+ addLinksToHamtBucket ( node . links , nextSegment . bucket , rootBucket , ( error ) => {
168
+ nextSegment . node = node
169
169
170
- next ( error )
171
- } )
172
- } )
170
+ next ( error )
171
+ } )
172
+ } ,
173
+ ( error ) => next ( error )
174
+ )
173
175
} ,
174
176
async ( err , path ) => {
175
177
await rootBucket . put ( fileName , true )
0 commit comments