11/* eslint-env mocha */
2+ /* eslint-disable max-nested-callbacks */
23'use strict'
34
45const bs58 = require ( 'bs58' )
56const hat = require ( 'hat' )
67const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
8+ const {
9+ calculateCid
10+ } = require ( '../utils/dag-pb' )
711
812module . exports = ( createCommon , options ) => {
913 const describe = getDescribe ( options )
@@ -41,36 +45,40 @@ module.exports = (createCommon, options) => {
4145 ipfs . object . put ( testObj , ( err , node ) => {
4246 expect ( err ) . to . not . exist ( )
4347
44- ipfs . object . data ( node . multihash , ( err , data ) => {
48+ calculateCid ( node , ( err , nodeCid ) => {
4549 expect ( err ) . to . not . exist ( )
4650
47- // because js-ipfs-api can't infer
48- // if the returned Data is Buffer or String
49- if ( typeof data === 'string' ) {
50- data = Buffer . from ( data )
51- }
52- expect ( node . data ) . to . eql ( data )
53- done ( )
51+ ipfs . object . data ( nodeCid , ( err , data ) => {
52+ expect ( err ) . to . not . exist ( )
53+
54+ // because js-ipfs-api can't infer
55+ // if the returned Data is Buffer or String
56+ if ( typeof data === 'string' ) {
57+ data = Buffer . from ( data )
58+ }
59+ expect ( node . data ) . to . eql ( data )
60+ done ( )
61+ } )
5462 } )
5563 } )
5664 } )
5765
58- it ( 'should get data by multihash (promised)' , ( ) => {
66+ it ( 'should get data by multihash (promised)' , async ( ) => {
5967 const testObj = {
6068 Data : Buffer . from ( hat ( ) ) ,
6169 Links : [ ]
6270 }
6371
64- return ipfs . object . put ( testObj ) . then ( ( node ) => {
65- return ipfs . object . data ( node . multihash ) . then ( ( data ) => {
66- // because js- ipfs-api can't infer
67- // if the returned Data is Buffer or String
68- if ( typeof data === 'string' ) {
69- data = Buffer . from ( data )
70- }
71- expect ( node . data ) . to . deep . equal ( data )
72- } )
73- } )
72+ const node = await ipfs . object . put ( testObj )
73+ const nodeCid = await calculateCid ( node )
74+ let data = await ipfs . object . data ( nodeCid )
75+
76+ // because js-ipfs-api can't infer
77+ // if the returned Data is Buffer or String
78+ if ( typeof data === 'string' ) {
79+ data = Buffer . from ( data )
80+ }
81+ expect ( node . data ) . to . deep . equal ( data )
7482 } )
7583
7684 it ( 'should get data by base58 encoded multihash' , ( done ) => {
@@ -82,16 +90,20 @@ module.exports = (createCommon, options) => {
8290 ipfs . object . put ( testObj , ( err , node ) => {
8391 expect ( err ) . to . not . exist ( )
8492
85- ipfs . object . data ( bs58 . encode ( node . multihash ) , { enc : 'base58' } , ( err , data ) => {
93+ calculateCid ( node , ( err , nodeCid ) => {
8694 expect ( err ) . to . not . exist ( )
8795
88- // because js-ipfs-api can't infer
89- // if the returned Data is Buffer or String
90- if ( typeof data === 'string' ) {
91- data = Buffer . from ( data )
92- }
93- expect ( node . data ) . to . eql ( data )
94- done ( )
96+ ipfs . object . data ( bs58 . encode ( nodeCid . buffer ) , { enc : 'base58' } , ( err , data ) => {
97+ expect ( err ) . to . not . exist ( )
98+
99+ // because js-ipfs-api can't infer
100+ // if the returned Data is Buffer or String
101+ if ( typeof data === 'string' ) {
102+ data = Buffer . from ( data )
103+ }
104+ expect ( node . data ) . to . eql ( data )
105+ done ( )
106+ } )
95107 } )
96108 } )
97109 } )
@@ -105,16 +117,20 @@ module.exports = (createCommon, options) => {
105117 ipfs . object . put ( testObj , ( err , node ) => {
106118 expect ( err ) . to . not . exist ( )
107119
108- ipfs . object . data ( bs58 . encode ( node . multihash ) . toString ( ) , { enc : 'base58' } , ( err , data ) => {
120+ calculateCid ( node , ( err , nodeCid ) => {
109121 expect ( err ) . to . not . exist ( )
110122
111- // because js-ipfs-api can't infer if the
112- // returned Data is Buffer or String
113- if ( typeof data === 'string' ) {
114- data = Buffer . from ( data )
115- }
116- expect ( node . data ) . to . eql ( data )
117- done ( )
123+ ipfs . object . data ( bs58 . encode ( nodeCid . buffer ) . toString ( ) , { enc : 'base58' } , ( err , data ) => {
124+ expect ( err ) . to . not . exist ( )
125+
126+ // because js-ipfs-api can't infer if the
127+ // returned Data is Buffer or String
128+ if ( typeof data === 'string' ) {
129+ data = Buffer . from ( data )
130+ }
131+ expect ( node . data ) . to . eql ( data )
132+ done ( )
133+ } )
118134 } )
119135 } )
120136 } )
0 commit comments