3
3
4
4
const dagPB = require ( 'ipld-dag-pb' )
5
5
const DAGNode = dagPB . DAGNode
6
- const bs58 = require ( 'bs58' )
7
6
const series = require ( 'async/series' )
8
7
const hat = require ( 'hat' )
9
8
const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
10
9
const UnixFs = require ( 'ipfs-unixfs' )
11
10
const crypto = require ( 'crypto' )
11
+ const {
12
+ calculateCid,
13
+ asDAGLink
14
+ } = require ( '../utils/dag-pb' )
12
15
13
16
module . exports = ( createCommon , options ) => {
14
17
const describe = getDescribe ( options )
@@ -44,18 +47,26 @@ module.exports = (createCommon, options) => {
44
47
}
45
48
46
49
let node1
50
+ let node1Cid
47
51
let node2
48
52
49
53
series ( [
50
54
( cb ) => {
51
55
ipfs . object . put ( obj , ( err , node ) => {
52
56
expect ( err ) . to . not . exist ( )
53
57
node1 = node
54
- cb ( )
58
+
59
+ calculateCid ( node , ( err , result ) => {
60
+ expect ( err ) . to . not . exist ( )
61
+
62
+ node1Cid = result
63
+
64
+ cb ( )
65
+ } )
55
66
} )
56
67
} ,
57
68
( cb ) => {
58
- ipfs . object . get ( node1 . multihash , ( err , node ) => {
69
+ ipfs . object . get ( node1Cid , ( err , node ) => {
59
70
expect ( err ) . to . not . exist ( )
60
71
node2 = node
61
72
@@ -70,30 +81,29 @@ module.exports = (createCommon, options) => {
70
81
( cb ) => {
71
82
expect ( node1 . data ) . to . eql ( node2 . data )
72
83
expect ( node1 . links ) . to . eql ( node2 . links )
73
- expect ( node1 . multihash ) . to . eql ( node2 . multihash )
74
84
cb ( )
75
85
}
76
86
] , done )
77
87
} )
78
88
79
- it ( 'should get object by multihash (promised)' , ( ) => {
89
+ it ( 'should get object by multihash (promised)' , async ( ) => {
80
90
const testObj = {
81
91
Data : Buffer . from ( hat ( ) ) ,
82
92
Links : [ ]
83
93
}
84
94
85
- return ipfs . object . put ( testObj ) . then ( ( node1 ) => {
86
- return ipfs . object . get ( node1 . multihash ) . then ( ( node2 ) => {
87
- // because js-ipfs-api can't infer if the
88
- // returned Data is Buffer or String
89
- if ( typeof node2 . data === 'string' ) {
90
- node2 . data = Buffer . from ( node2 . data )
91
- }
95
+ const node1 = await ipfs . object . put ( testObj )
96
+ const node1Cid = await calculateCid ( node1 )
97
+ const node2 = await ipfs . object . get ( node1Cid )
92
98
93
- expect ( node1 . data ) . to . deep . equal ( node2 . data )
94
- expect ( node1 . links ) . to . deep . equal ( node2 . links )
95
- } )
96
- } )
99
+ // because js-ipfs-api can't infer if the
100
+ // returned Data is Buffer or String
101
+ if ( typeof node2 . data === 'string' ) {
102
+ node2 . data = Buffer . from ( node2 . data )
103
+ }
104
+
105
+ expect ( node1 . data ) . to . deep . equal ( node2 . data )
106
+ expect ( node1 . links ) . to . deep . equal ( node2 . links )
97
107
} )
98
108
99
109
it ( 'should get object by multihash string' , ( done ) => {
@@ -103,95 +113,126 @@ module.exports = (createCommon, options) => {
103
113
}
104
114
105
115
let node1
116
+ let node1Cid
106
117
let node2
118
+ let node2Cid
107
119
108
120
series ( [
109
121
( cb ) => {
110
122
ipfs . object . put ( obj , ( err , node ) => {
111
123
expect ( err ) . to . not . exist ( )
112
124
node1 = node
113
- cb ( )
125
+
126
+ calculateCid ( node , ( err , result ) => {
127
+ expect ( err ) . to . not . exist ( )
128
+
129
+ node1Cid = result
130
+
131
+ cb ( )
132
+ } )
114
133
} )
115
134
} ,
116
135
( cb ) => {
117
136
// get object from ipfs multihash string
118
- ipfs . object . get ( node1 . toJSON ( ) . multihash , ( err , node ) => {
137
+ ipfs . object . get ( node1Cid . toBaseEncodedString ( ) , ( err , node ) => {
119
138
expect ( err ) . to . not . exist ( )
120
139
// because js-ipfs-api can't infer if the
121
140
// returned Data is Buffer or String
122
141
if ( typeof node . data === 'string' ) {
123
142
node . data = Buffer . from ( node . data )
124
143
}
125
144
node2 = node
126
- cb ( )
145
+
146
+ calculateCid ( node , ( err , result ) => {
147
+ expect ( err ) . to . not . exist ( )
148
+
149
+ node2Cid = result
150
+
151
+ cb ( )
152
+ } )
127
153
} )
128
154
} ,
129
155
( cb ) => {
130
156
expect ( node1 . data ) . to . eql ( node2 . data )
131
157
expect ( node1 . links ) . to . eql ( node2 . links )
132
- expect ( node1 . multihash ) . to . eql ( node2 . multihash )
158
+ expect ( node1Cid ) . to . deep . eql ( node2Cid )
133
159
cb ( )
134
160
}
135
161
] , done )
136
162
} )
137
163
138
- it ( 'should get object by multihash string (promised)' , ( ) => {
164
+ it ( 'should get object by multihash string (promised)' , async ( ) => {
139
165
const obj = {
140
166
Data : Buffer . from ( hat ( ) ) ,
141
167
Links : [ ]
142
168
}
143
169
144
- return ipfs . object . put ( obj )
145
- . then ( ( node1 ) => {
146
- return ipfs . object . get ( node1 . toJSON ( ) . multihash )
147
- . then ( ( node2 ) => {
148
- // because js-ipfs-api can't infer if the
149
- // returned Data is Buffer or String
150
- if ( typeof node2 . data === 'string' ) {
151
- node2 . data = Buffer . from ( node2 . data )
152
- }
153
-
154
- expect ( node1 . data ) . to . deep . equal ( node2 . data )
155
- expect ( node1 . links ) . to . deep . equal ( node2 . links )
156
- } )
157
- } )
170
+ const node1 = await ipfs . object . put ( obj )
171
+ const node1Cid = await calculateCid ( node1 )
172
+ const node2 = await ipfs . object . get ( node1Cid . toBaseEncodedString ( ) )
173
+ const node2Cid = await calculateCid ( node2 )
174
+ // because js-ipfs-api can't infer if the
175
+ // returned Data is Buffer or String
176
+ if ( typeof node2 . data === 'string' ) {
177
+ node2 . data = Buffer . from ( node2 . data )
178
+ }
179
+
180
+ expect ( node1 . data ) . to . deep . equal ( node2 . data )
181
+ expect ( node1 . links ) . to . deep . equal ( node2 . links )
182
+ expect ( node1Cid ) . to . deep . eql ( node2Cid )
158
183
} )
159
184
160
185
it ( 'should get object with links by multihash string' , ( done ) => {
161
186
let node1a
162
187
let node1b
188
+ let node1bCid
163
189
let node1c
190
+ let node1cCid
164
191
let node2
165
192
166
193
series ( [
167
194
( cb ) => {
168
195
DAGNode . create ( Buffer . from ( 'Some data 1' ) , ( err , node ) => {
169
196
expect ( err ) . to . not . exist ( )
170
197
node1a = node
198
+
171
199
cb ( )
172
200
} )
173
201
} ,
174
202
( cb ) => {
175
203
DAGNode . create ( Buffer . from ( 'Some data 2' ) , ( err , node ) => {
176
204
expect ( err ) . to . not . exist ( )
177
205
node2 = node
206
+
178
207
cb ( )
179
208
} )
180
209
} ,
181
210
( cb ) => {
182
- const link = node2 . toJSON ( )
183
- link . name = 'some-link'
184
- DAGNode . addLink ( node1a , link , ( err , node ) => {
211
+ asDAGLink ( node2 , 'some-link' , ( err , link ) => {
185
212
expect ( err ) . to . not . exist ( )
186
- node1b = node
187
- cb ( )
213
+
214
+ DAGNode . addLink ( node1a , link , ( err , node ) => {
215
+ expect ( err ) . to . not . exist ( )
216
+ node1b = node
217
+ cb ( )
218
+ } )
188
219
} )
189
220
} ,
190
221
( cb ) => {
191
- ipfs . object . put ( node1b , cb )
222
+ ipfs . object . put ( node1b , ( err , node ) => {
223
+ expect ( err ) . to . not . exist ( )
224
+
225
+ calculateCid ( node , ( err , result ) => {
226
+ expect ( err ) . to . not . exist ( )
227
+
228
+ node1bCid = result
229
+
230
+ cb ( )
231
+ } )
232
+ } )
192
233
} ,
193
234
( cb ) => {
194
- ipfs . object . get ( node1b . multihash , ( err , node ) => {
235
+ ipfs . object . get ( node1bCid , ( err , node ) => {
195
236
expect ( err ) . to . not . exist ( )
196
237
197
238
// because js-ipfs-api can't infer if the
@@ -201,12 +242,19 @@ module.exports = (createCommon, options) => {
201
242
}
202
243
203
244
node1c = node
204
- cb ( )
245
+
246
+ calculateCid ( node , ( err , result ) => {
247
+ expect ( err ) . to . not . exist ( )
248
+
249
+ node1cCid = result
250
+
251
+ cb ( )
252
+ } )
205
253
} )
206
254
} ,
207
255
( cb ) => {
208
256
expect ( node1a . data ) . to . eql ( node1c . data )
209
- expect ( node1b . multihash ) . to . eql ( node1c . multihash )
257
+ expect ( node1bCid ) . to . eql ( node1cCid )
210
258
cb ( )
211
259
}
212
260
] , done )
@@ -219,30 +267,46 @@ module.exports = (createCommon, options) => {
219
267
}
220
268
221
269
let node1a
270
+ let node1aCid
222
271
let node1b
272
+ let node1bCid
223
273
224
274
series ( [
225
275
( cb ) => {
226
276
ipfs . object . put ( obj , ( err , node ) => {
227
277
expect ( err ) . to . not . exist ( )
228
278
node1a = node
229
- cb ( )
279
+
280
+ calculateCid ( node , ( err , result ) => {
281
+ expect ( err ) . to . not . exist ( )
282
+
283
+ node1aCid = result
284
+
285
+ cb ( )
286
+ } )
230
287
} )
231
288
} ,
232
289
( cb ) => {
233
- ipfs . object . get ( node1a . multihash , { enc : 'base58' } , ( err , node ) => {
290
+ ipfs . object . get ( node1aCid , { enc : 'base58' } , ( err , node ) => {
234
291
expect ( err ) . to . not . exist ( )
235
292
// because js-ipfs-api can't infer if the
236
293
// returned Data is Buffer or String
237
294
if ( typeof node . data === 'string' ) {
238
295
node . data = Buffer . from ( node . data )
239
296
}
240
297
node1b = node
241
- cb ( )
298
+
299
+ calculateCid ( node , ( err , result ) => {
300
+ expect ( err ) . to . not . exist ( )
301
+
302
+ node1bCid = result
303
+
304
+ cb ( )
305
+ } )
242
306
} )
243
307
} ,
244
308
( cb ) => {
245
- expect ( node1a . multihash ) . to . eql ( node1b . multihash )
309
+ expect ( node1aCid ) . to . deep . eql ( node1bCid )
246
310
expect ( node1a . data ) . to . eql ( node1b . data )
247
311
expect ( node1a . links ) . to . eql ( node1b . links )
248
312
cb ( )
@@ -257,30 +321,46 @@ module.exports = (createCommon, options) => {
257
321
}
258
322
259
323
let node1a
324
+ let node1aCid
260
325
let node1b
326
+ let node1bCid
261
327
262
328
series ( [
263
329
( cb ) => {
264
330
ipfs . object . put ( obj , ( err , node ) => {
265
331
expect ( err ) . to . not . exist ( )
266
332
node1a = node
267
- cb ( )
333
+
334
+ calculateCid ( node , ( err , result ) => {
335
+ expect ( err ) . to . not . exist ( )
336
+
337
+ node1aCid = result
338
+
339
+ cb ( )
340
+ } )
268
341
} )
269
342
} ,
270
343
( cb ) => {
271
- ipfs . object . get ( bs58 . encode ( node1a . multihash ) . toString ( ) , { enc : 'base58' } , ( err , node ) => {
344
+ ipfs . object . get ( node1aCid . toBaseEncodedString ( ) , { enc : 'base58' } , ( err , node ) => {
272
345
expect ( err ) . to . not . exist ( )
273
346
// because js-ipfs-api can't infer if the
274
347
// returned Data is Buffer or String
275
348
if ( typeof node . data === 'string' ) {
276
349
node . data = Buffer . from ( node . data )
277
350
}
278
351
node1b = node
279
- cb ( )
352
+
353
+ calculateCid ( node , ( err , result ) => {
354
+ expect ( err ) . to . not . exist ( )
355
+
356
+ node1bCid = result
357
+
358
+ cb ( )
359
+ } )
280
360
} )
281
361
} ,
282
362
( cb ) => {
283
- expect ( node1a . multihash ) . to . eql ( node1b . multihash )
363
+ expect ( node1aCid ) . to . deep . eql ( node1bCid )
284
364
expect ( node1a . data ) . to . eql ( node1b . data )
285
365
expect ( node1a . links ) . to . eql ( node1b . links )
286
366
cb ( )
0 commit comments