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
215 lines (183 loc) · 5.68 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
209
210
211
212
213
214
215
/* eslint-env mocha */
'use strict'
const { series, eachSeries } = require('async')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const dagCBOR = require('ipld-dag-cbor')
const { spawnNodeWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')
module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()
describe('.dag.get', () => {
let ipfs
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
done()
})
})
})
after((done) => common.teardown(done))
let pbNode
let cborNode
let nodePb
let nodeCbor
let cidPb
let cidCbor
before((done) => {
series([
(cb) => {
const someData = Buffer.from('some other data')
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
}
dagCBOR.util.cid(nodeCbor, (err, cid) => {
expect(err).to.not.exist()
cidCbor = cid
cb()
})
},
(cb) => {
eachSeries([
{ node: nodePb, multicodec: 'dag-pb', hashAlg: 'sha2-256' },
{ node: nodeCbor, multicodec: 'dag-cbor', hashAlg: 'sha2-256' }
], (el, cb) => {
ipfs.dag.put(el.node, {
format: el.multicodec,
hashAlg: el.hashAlg
}, cb)
}, cb)
}
], done)
})
it('should get a 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('should get a 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()
})
})
})
it('should get a dag-pb node with path', (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('should get a dag-pb node local value', function (done) {
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('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', (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('should get a dag-cbor node local value', (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('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', function (done) {
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('should get by 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('should get by CID string + path', function (done) {
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()
})
})
})
}