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
/
tree.js
83 lines (68 loc) · 2.22 KB
/
tree.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
/* eslint-env mocha */
'use strict'
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const dagCBOR = require('ipld-dag-cbor')
const all = require('it-all')
const { getDescribe, getIt, expect } = require('../utils/mocha')
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
* @param {Object} options
*/
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.dag.tree', () => {
let ipfs
before(async () => { ipfs = (await common.spawn()).api })
after(() => common.clean())
let nodePb
let nodeCbor
let cidPb
let cidCbor
before(async function () {
nodePb = new DAGNode(Buffer.from('I am inside a Protobuf'))
cidPb = await dagPB.util.cid(nodePb.serialize())
nodeCbor = {
someData: 'I am inside a Cbor object',
pb: cidPb
}
cidCbor = await dagCBOR.util.cid(dagCBOR.util.serialize(nodeCbor))
await ipfs.dag.put(nodePb, { format: 'dag-pb', hashAlg: 'sha2-256' })
await ipfs.dag.put(nodeCbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
})
it('should get tree with CID', async () => {
const paths = await all(ipfs.dag.tree(cidCbor))
expect(paths).to.eql([
'pb',
'someData'
])
})
it('should get tree with CID and path', async () => {
const paths = await all(ipfs.dag.tree(cidCbor, 'someData'))
expect(paths).to.eql([])
})
it('should get tree with CID and path as String', async () => {
const cidCborStr = cidCbor.toBaseEncodedString()
const paths = await all(ipfs.dag.tree(cidCborStr + '/someData'))
expect(paths).to.eql([])
})
it('should get tree with CID recursive (accross different formats)', async () => {
const paths = await all(ipfs.dag.tree(cidCbor, { recursive: true }))
expect(paths).to.have.members([
'pb',
'someData',
'pb/Links',
'pb/Data'
])
})
it('should get tree with CID and path recursive', async () => {
const paths = await all(ipfs.dag.tree(cidCbor, 'pb', { recursive: true }))
expect(paths).to.have.members([
'Links',
'Data'
])
})
})
}