This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathget.js
208 lines (155 loc) · 6.26 KB
/
get.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
/* eslint-env mocha */
'use strict'
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const dagCBOR = require('ipld-dag-cbor')
const Unixfs = require('ipfs-unixfs')
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.get', () => {
let ipfs
before(async () => { ipfs = (await common.spawn()).api })
after(() => common.clean())
let pbNode
let cborNode
let nodePb
let nodeCbor
let cidPb
let cidCbor
before(async () => {
const someData = Buffer.from('some other data')
pbNode = new DAGNode(someData)
cborNode = {
data: someData
}
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 a dag-pb node', async () => {
const cid = await ipfs.dag.put(pbNode, {
format: 'dag-pb',
hashAlg: 'sha2-256'
})
const result = await ipfs.dag.get(cid)
const node = result.value
expect(pbNode.toJSON()).to.eql(node.toJSON())
})
it('should get a dag-cbor node', async () => {
const cid = await ipfs.dag.put(cborNode, {
format: 'dag-cbor',
hashAlg: 'sha2-256'
})
const result = await ipfs.dag.get(cid)
const node = result.value
expect(cborNode).to.eql(node)
})
it('should get a dag-pb node with path', async () => {
const result = await ipfs.dag.get(cidPb, '/')
const node = result.value
const cid = await dagPB.util.cid(node.serialize())
expect(cid).to.eql(cidPb)
})
it('should get a dag-pb node local value', async function () {
const result = await ipfs.dag.get(cidPb, 'Data')
expect(result.value).to.eql(Buffer.from('I am inside a Protobuf'))
})
it.skip('should get a dag-pb node value one level deep', (done) => {})
it.skip('should get a dag-pb node value two levels deep', (done) => {})
it('should get a dag-cbor node with path', async () => {
const result = await ipfs.dag.get(cidCbor, '/')
const node = result.value
const cid = await dagCBOR.util.cid(dagCBOR.util.serialize(node))
expect(cid).to.eql(cidCbor)
})
it('should get a dag-cbor node local value', async () => {
const result = await ipfs.dag.get(cidCbor, 'someData')
expect(result.value).to.eql('I am inside a Cbor object')
})
it.skip('should get dag-cbor node value one level deep', (done) => {})
it.skip('should get dag-cbor node value two levels deep', (done) => {})
it.skip('should get dag-cbor value via dag-pb node', (done) => {})
it('should get dag-pb value via dag-cbor node', async function () {
const result = await ipfs.dag.get(cidCbor, 'pb/Data')
expect(result.value).to.eql(Buffer.from('I am inside a Protobuf'))
})
it('should get by CID string', async () => {
const cidCborStr = cidCbor.toBaseEncodedString()
const result = await ipfs.dag.get(cidCborStr)
const node = result.value
const cid = await dagCBOR.util.cid(dagCBOR.util.serialize(node))
expect(cid).to.eql(cidCbor)
})
it('should get by CID string + path', async function () {
const cidCborStr = cidCbor.toBaseEncodedString()
const result = await ipfs.dag.get(cidCborStr + '/pb/Data')
expect(result.value).to.eql(Buffer.from('I am inside a Protobuf'))
})
it('should get only a CID, due to resolving locally only', async function () {
const result = await ipfs.dag.get(cidCbor, 'pb/Data', { localResolve: true })
expect(result.value.equals(cidPb)).to.be.true()
})
it('should get a node added as CIDv0 with a CIDv1', async () => {
const input = Buffer.from(`TEST${Date.now()}`)
const node = new DAGNode(input)
const cid = await ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' })
expect(cid.version).to.equal(0)
const cidv1 = cid.toV1()
const output = await ipfs.dag.get(cidv1)
expect(output.value.Data).to.eql(input)
})
it('should get a node added as CIDv1 with a CIDv0', async () => {
const input = Buffer.from(`TEST${Date.now()}`)
const res = await all(ipfs.add(input, { cidVersion: 1, rawLeaves: false }))
const cidv1 = res[0].cid
expect(cidv1.version).to.equal(1)
const cidv0 = cidv1.toV0()
const output = await ipfs.dag.get(cidv0)
expect(Unixfs.unmarshal(output.value.Data).data).to.eql(input)
})
it('should be able to get part of a dag-cbor node', async () => {
const cbor = {
foo: 'dag-cbor-bar'
}
let cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
expect(cid.codec).to.equal('dag-cbor')
cid = cid.toBaseEncodedString('base32')
expect(cid).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')
const result = await ipfs.dag.get(cid, 'foo')
expect(result.value).to.equal('dag-cbor-bar')
})
it('should be able to traverse from one dag-cbor node to another', async () => {
const cbor1 = {
foo: 'dag-cbor-bar'
}
const cid1 = await ipfs.dag.put(cbor1, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cbor2 = { other: cid1 }
const cid2 = await ipfs.dag.put(cbor2, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const result = await ipfs.dag.get(cid2, 'other/foo')
expect(result.value).to.equal('dag-cbor-bar')
})
it('should be able to get a DAG node with format raw', async () => {
const buf = Buffer.from([0, 1, 2, 3])
const cid = await ipfs.dag.put(buf, {
format: 'raw',
hashAlg: 'sha2-256'
})
const result = await ipfs.dag.get(cid)
expect(result.value).to.deep.equal(buf)
})
})
}