3
3
const promisify = require ( 'promisify-es6' )
4
4
const CID = require ( 'cids' )
5
5
const pull = require ( 'pull-stream' )
6
+ const iterToPull = require ( 'async-iterator-to-pull-stream' )
6
7
const mapAsync = require ( 'async/map' )
7
8
const setImmediate = require ( 'async/setImmediate' )
8
9
const flattenDeep = require ( 'lodash/flattenDeep' )
9
10
const errCode = require ( 'err-code' )
11
+ const multicodec = require ( 'multicodec' )
10
12
11
13
module . exports = function dag ( self ) {
12
14
return {
@@ -25,21 +27,42 @@ module.exports = function dag (self) {
25
27
}
26
28
27
29
const optionDefaults = {
28
- format : 'dag-cbor' ,
29
- hashAlg : 'sha2-256'
30
+ format : multicodec . DAG_CBOR ,
31
+ hashAlg : multicodec . SHA2_256
30
32
}
31
33
32
- options = options . cid ? options : Object . assign ( { } , optionDefaults , options )
34
+ // The IPLD expects the format and hashAlg as constants
35
+ if ( options . format && typeof options . format === 'string' ) {
36
+ const constantName = options . format . toUpperCase ( ) . replace ( / - / g, '_' )
37
+ options . format = multicodec [ constantName ]
38
+ }
39
+ if ( options . hashAlg && typeof options . hashAlg === 'string' ) {
40
+ const constantName = options . hashAlg . toUpperCase ( ) . replace ( / - / g, '_' )
41
+ options . hashAlg = multicodec [ constantName ]
42
+ }
33
43
34
- self . _ipld . put ( dagNode , options , ( err , cid ) => {
35
- if ( err ) return callback ( err )
44
+ options = options . cid ? options : Object . assign ( { } , optionDefaults , options )
36
45
37
- if ( options . preload !== false ) {
38
- self . _preload ( cid )
39
- }
46
+ // js-ipld defaults to verion 1 CIDs. Hence set version 0 explicitly for
47
+ // dag-pb nodes
48
+ if ( options . format === multicodec . DAG_PB &&
49
+ options . hashAlg === multicodec . SHA2_256 &&
50
+ options . version === undefined ) {
51
+ options . version = 0
52
+ }
40
53
41
- callback ( null , cid )
42
- } )
54
+ self . _ipld . put ( dagNode , options . format , {
55
+ hashAlg : options . hashAlg ,
56
+ cidVersion : options . version
57
+ } ) . then (
58
+ ( cid ) => {
59
+ if ( options . preload !== false ) {
60
+ self . _preload ( cid )
61
+ }
62
+ return callback ( null , cid )
63
+ } ,
64
+ ( error ) => callback ( error )
65
+ )
43
66
} ) ,
44
67
45
68
get : promisify ( ( cid , path , options , callback ) => {
@@ -54,7 +77,7 @@ module.exports = function dag (self) {
54
77
// Allow options in path position
55
78
if ( typeof path !== 'string' ) {
56
79
options = path
57
- path = null
80
+ path = undefined
58
81
} else {
59
82
options = { }
60
83
}
@@ -90,7 +113,26 @@ module.exports = function dag (self) {
90
113
self . _preload ( cid )
91
114
}
92
115
93
- self . _ipld . get ( cid , path , options , callback )
116
+ if ( path === undefined || path === '/' ) {
117
+ self . _ipld . get ( cid ) . then (
118
+ ( value ) => {
119
+ callback ( null , {
120
+ value,
121
+ remainderPath : ''
122
+ } )
123
+ } ,
124
+ ( error ) => callback ( error )
125
+ )
126
+ } else {
127
+ const result = self . _ipld . resolve ( cid , path )
128
+ const promisedValue = options . localResolve ?
129
+ result . first ( ) :
130
+ result . last ( )
131
+ promisedValue . then (
132
+ ( value ) => callback ( null , value ) ,
133
+ ( error ) => callback ( error )
134
+ )
135
+ }
94
136
} ) ,
95
137
96
138
tree : promisify ( ( cid , path , options , callback ) => {
@@ -135,7 +177,7 @@ module.exports = function dag (self) {
135
177
}
136
178
137
179
pull (
138
- self . _ipld . treeStream ( cid , path , options ) ,
180
+ iterToPull ( self . _ipld . tree ( cid , path , options ) ) ,
139
181
pull . collect ( callback )
140
182
)
141
183
} ) ,
0 commit comments