This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
dag.js
450 lines (393 loc) · 12.1 KB
/
dag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const series = require('async/series')
const pull = require('pull-stream')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const dagCBOR = require('ipld-dag-cbor')
const CID = require('cids')
const { spawnNodeWithId } = require('./utils/spawn')
module.exports = (common) => {
describe('.dag', () => {
let ipfs
let withGo
before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)
common.setup((err, factory) => {
expect(err).to.not.exist()
spawnNodeWithId(factory, (err, node) => {
expect(err).to.not.exist()
ipfs = node
withGo = node.peerId.agentVersion.startsWith('go-ipfs')
done()
})
})
})
after((done) => common.teardown(done))
let pbNode
let cborNode
before((done) => {
const someData = Buffer.from('some data')
pbNode = DAGNode.create(someData, (err, node) => {
expect(err).to.not.exist()
pbNode = node
done()
})
cborNode = {
data: someData
}
})
describe('.put', () => {
it('dag-pb with default hash func (sha2-256)', (done) => {
ipfs.dag.put(pbNode, {
format: 'dag-pb',
hashAlg: 'sha2-256'
}, done)
})
it('dag-pb with custom hash func (sha3-512)', (done) => {
ipfs.dag.put(pbNode, {
format: 'dag-pb',
hashAlg: 'sha3-512'
}, done)
})
// This works because dag-cbor will just treat pbNode as a regular object
it.skip('dag-pb node with wrong multicodec', (done) => {
ipfs.dag.put(pbNode, 'dag-cbor', 'sha3-512', (err) => {
expect(err).to.exist()
done()
})
})
it('dag-cbor with default hash func (sha2-256)', (done) => {
ipfs.dag.put(cborNode, {
format: 'dag-cbor',
hashAlg: 'sha2-256'
}, done)
})
it('dag-cbor with custom hash func (sha3-512)', (done) => {
ipfs.dag.put(cborNode, {
format: 'dag-cbor',
hashAlg: 'sha3-512'
}, done)
})
it('dag-cbor node with wrong multicodec', function (done) {
// This works in go-ipfs because dag-pb will serialize any object. If
// the object has neither a `data` nor `links` field it's serialized
// as an empty object
if (withGo) {
this.skip()
}
ipfs.dag.put(cborNode, {
format: 'dag-pb',
hashAlg: 'sha3-512'
}, (err) => {
expect(err).to.exist()
done()
})
})
it('returns the cid', (done) => {
ipfs.dag.put(cborNode, {
format: 'dag-cbor',
hashAlg: 'sha2-256'
}, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(CID.isCID(cid)).to.equal(true)
dagCBOR.util.cid(cborNode, (err, _cid) => {
expect(err).to.not.exist()
expect(cid.buffer).to.eql(_cid.buffer)
done()
})
})
})
it.skip('pass the cid instead of format and hashAlg', (done) => {})
// TODO it.skip('Promises support', (done) => {})
})
describe('.get', () => {
let pbNode
let cborNode
let nodePb
let nodeCbor
let cidPb
let cidCbor
before((done) => {
series([
(cb) => {
const someData = Buffer.from('some other data')
pbNode = DAGNode.create(someData, (err, node) => {
expect(err).to.not.exist()
pbNode = node
cb()
})
cborNode = {
data: someData
}
},
(cb) => {
dagPB.DAGNode.create(Buffer.from('I am inside a Protobuf'), (err, node) => {
expect(err).to.not.exist()
nodePb = node
cb()
})
},
(cb) => {
dagPB.util.cid(nodePb, (err, cid) => {
expect(err).to.not.exist()
cidPb = cid
cb()
})
},
(cb) => {
nodeCbor = {
someData: 'I am inside a Cbor object',
pb: { '/': cidPb.toBaseEncodedString() }
}
dagCBOR.util.cid(nodeCbor, (err, cid) => {
expect(err).to.not.exist()
cidCbor = cid
cb()
})
}
], store)
function store () {
pull(
pull.values([
{ node: nodePb, multicodec: 'dag-pb', hashAlg: 'sha2-256' },
{ node: nodeCbor, multicodec: 'dag-cbor', hashAlg: 'sha2-256' }
]),
pull.asyncMap((el, cb) => {
ipfs.dag.put(el.node, {
format: el.multicodec,
hashAlg: el.hashAlg
}, cb)
}),
pull.onEnd(done)
)
}
})
it('dag-pb node', (done) => {
ipfs.dag.put(pbNode, {
format: 'dag-pb',
hashAlg: 'sha2-256'
}, (err, cid) => {
expect(err).to.not.exist()
ipfs.dag.get(cid, (err, result) => {
expect(err).to.not.exist()
const node = result.value
expect(pbNode.toJSON()).to.eql(node.toJSON())
done()
})
})
})
it('dag-cbor node', (done) => {
ipfs.dag.put(cborNode, {
format: 'dag-cbor',
hashAlg: 'sha2-256'
}, (err, cid) => {
expect(err).to.not.exist()
ipfs.dag.get(cid, (err, result) => {
expect(err).to.not.exist()
const node = result.value
expect(cborNode).to.eql(node)
done()
})
})
})
describe('with path', () => {
it('dag-pb get the node', (done) => {
ipfs.dag.get(cidPb, '/', (err, result) => {
expect(err).to.not.exist()
const node = result.value
dagPB.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.eql(cidPb)
done()
})
})
})
it('dag-pb local scope', function (done) {
// TODO vmx 2018-02-22: Currently not supported in go-ipfs, it might
// be possible once https://github.com/ipfs/go-ipfs/issues/4728 is
// done
if (withGo) {
this.skip()
}
ipfs.dag.get(cidPb, 'Data', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.eql(Buffer.from('I am inside a Protobuf'))
done()
})
})
it.skip('dag-pb one level', (done) => {})
it.skip('dag-pb two levels', (done) => {})
it('dag-cbor get the node', (done) => {
ipfs.dag.get(cidCbor, '/', (err, result) => {
expect(err).to.not.exist()
const node = result.value
dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.eql(cidCbor)
done()
})
})
})
it('dag-cbor local scope', (done) => {
ipfs.dag.get(cidCbor, 'someData', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.eql('I am inside a Cbor object')
done()
})
})
it.skip('dag-cbor one level', (done) => {})
it.skip('dag-cbor two levels', (done) => {})
it.skip('from dag-pb to dag-cbor', (done) => {})
it('from dag-cbor to dag-pb', function (done) {
// TODO vmx 2018-02-22: Currently not supported in go-ipfs, it might
// be possible once https://github.com/ipfs/go-ipfs/issues/4728 is
// done
if (withGo) {
this.skip()
}
ipfs.dag.get(cidCbor, 'pb/Data', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.eql(Buffer.from('I am inside a Protobuf'))
done()
})
})
it('CID String', (done) => {
const cidCborStr = cidCbor.toBaseEncodedString()
ipfs.dag.get(cidCborStr, (err, result) => {
expect(err).to.not.exist()
const node = result.value
dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.eql(cidCbor)
done()
})
})
})
it('CID String + path', function (done) {
// TODO vmx 2018-02-22: Currently not supported in go-ipfs, it might
// be possible once https://github.com/ipfs/go-ipfs/issues/4728 is
// done
if (withGo) {
this.skip()
}
const cidCborStr = cidCbor.toBaseEncodedString()
ipfs.dag.get(cidCborStr + '/pb/Data', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.eql(Buffer.from('I am inside a Protobuf'))
done()
})
})
})
})
describe('.tree', function () {
let nodePb
let nodeCbor
let cidPb
let cidCbor
before(function (done) {
// TODO vmx 2018-02-22: Currently the tree API is not exposed in go-ipfs
if (withGo) {
this.skip()
}
series([
(cb) => {
dagPB.DAGNode.create(Buffer.from('I am inside a Protobuf'), (err, node) => {
expect(err).to.not.exist()
nodePb = node
cb()
})
},
(cb) => {
dagPB.util.cid(nodePb, (err, cid) => {
expect(err).to.not.exist()
cidPb = cid
cb()
})
},
(cb) => {
nodeCbor = {
someData: 'I am inside a Cbor object',
pb: { '/': cidPb.toBaseEncodedString() }
}
dagCBOR.util.cid(nodeCbor, (err, cid) => {
expect(err).to.not.exist()
cidCbor = cid
cb()
})
}
], store)
function store () {
pull(
pull.values([
{ node: nodePb, multicodec: 'dag-pb', hashAlg: 'sha2-256' },
{ node: nodeCbor, multicodec: 'dag-cbor', hashAlg: 'sha2-256' }
]),
pull.asyncMap((el, cb) => {
ipfs.dag.put(el.node, {
format: el.multicodec,
hashAlg: el.hashAlg
}, cb)
}),
pull.onEnd(done)
)
}
})
it('.tree with CID', (done) => {
ipfs.dag.tree(cidCbor, (err, paths) => {
expect(err).to.not.exist()
expect(paths).to.eql([
'pb',
'someData'
])
done()
})
})
it('.tree with CID and path', (done) => {
ipfs.dag.tree(cidCbor, 'someData', (err, paths) => {
expect(err).to.not.exist()
expect(paths).to.eql([])
done()
})
})
it('.tree with CID and path as String', (done) => {
const cidCborStr = cidCbor.toBaseEncodedString()
ipfs.dag.tree(cidCborStr + '/someData', (err, paths) => {
expect(err).to.not.exist()
expect(paths).to.eql([])
done()
})
})
it('.tree with CID recursive (accross different formats)', (done) => {
ipfs.dag.tree(cidCbor, { recursive: true }, (err, paths) => {
expect(err).to.not.exist()
expect(paths).to.eql([
'pb',
'someData',
'pb/Links',
'pb/Data'
])
done()
})
})
it('.tree with CID and path recursive', (done) => {
ipfs.dag.tree(cidCbor, 'pb', { recursive: true }, (err, paths) => {
expect(err).to.not.exist()
expect(paths).to.eql([
'Links',
'Data'
])
done()
})
})
})
})
}