33const promisify = require ( 'promisify-es6' )
44const CID = require ( 'cids' )
55const pull = require ( 'pull-stream' )
6+ const iterToPull = require ( 'async-iterator-to-pull-stream' )
67const mapAsync = require ( 'async/map' )
78const setImmediate = require ( 'async/setImmediate' )
89const flattenDeep = require ( 'lodash/flattenDeep' )
910const errCode = require ( 'err-code' )
11+ const multicodec = require ( 'multicodec' )
1012
1113module . exports = function dag ( self ) {
1214 return {
@@ -25,21 +27,42 @@ module.exports = function dag (self) {
2527 }
2628
2729 const optionDefaults = {
28- format : 'dag-cbor' ,
29- hashAlg : 'sha2-256'
30+ format : multicodec . DAG_CBOR ,
31+ hashAlg : multicodec . SHA2_256
3032 }
3133
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+ }
3343
34- self . _ipld . put ( dagNode , options , ( err , cid ) => {
35- if ( err ) return callback ( err )
44+ options = options . cid ? options : Object . assign ( { } , optionDefaults , options )
3645
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+ }
4053
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+ )
4366 } ) ,
4467
4568 get : promisify ( ( cid , path , options , callback ) => {
@@ -54,7 +77,7 @@ module.exports = function dag (self) {
5477 // Allow options in path position
5578 if ( typeof path !== 'string' ) {
5679 options = path
57- path = null
80+ path = undefined
5881 } else {
5982 options = { }
6083 }
@@ -90,7 +113,26 @@ module.exports = function dag (self) {
90113 self . _preload ( cid )
91114 }
92115
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+ }
94136 } ) ,
95137
96138 tree : promisify ( ( cid , path , options , callback ) => {
@@ -135,7 +177,7 @@ module.exports = function dag (self) {
135177 }
136178
137179 pull (
138- self . _ipld . treeStream ( cid , path , options ) ,
180+ iterToPull ( self . _ipld . tree ( cid , path , options ) ) ,
139181 pull . collect ( callback )
140182 )
141183 } ) ,
0 commit comments