Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit d9812ec

Browse files
author
Pedro Santos
committed
chore: convert missing object.get api to async/await syntax
1 parent dcaf0a3 commit d9812ec

File tree

1 file changed

+57
-252
lines changed

1 file changed

+57
-252
lines changed

src/object/get.js

Lines changed: 57 additions & 252 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const dagPB = require('ipld-dag-pb')
55
const DAGNode = dagPB.DAGNode
6-
const series = require('async/series')
76
const hat = require('hat')
87
const { getDescribe, getIt, expect } = require('../utils/mocha')
98
const UnixFs = require('ipfs-unixfs')
@@ -30,59 +29,13 @@ module.exports = (common, options) => {
3029

3130
after(() => common.teardown())
3231

33-
it('should get object by multihash', (done) => {
32+
it('should get object by multihash', async () => {
3433
const obj = {
3534
Data: Buffer.from(hat()),
3635
Links: []
3736
}
3837

39-
let node1
40-
let node1Cid
41-
let node2
42-
43-
series([
44-
(cb) => {
45-
ipfs.object.put(obj, (err, cid) => {
46-
expect(err).to.not.exist()
47-
node1Cid = cid
48-
49-
ipfs.object.get(cid, (err, node) => {
50-
expect(err).to.not.exist()
51-
node1 = node
52-
cb()
53-
})
54-
})
55-
},
56-
(cb) => {
57-
ipfs.object.get(node1Cid, (err, node) => {
58-
expect(err).to.not.exist()
59-
60-
// because js-ipfs-api can't infer if the
61-
// returned Data is Buffer or String
62-
if (typeof node.Data === 'string') {
63-
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
64-
}
65-
66-
node2 = node
67-
68-
cb()
69-
})
70-
},
71-
(cb) => {
72-
expect(node1.Data).to.eql(node2.Data)
73-
expect(node1.Links).to.eql(node2.Links)
74-
cb()
75-
}
76-
], done)
77-
})
78-
79-
it('should get object by multihash (promised)', async () => {
80-
const testObj = {
81-
Data: Buffer.from(hat()),
82-
Links: []
83-
}
84-
85-
const node1Cid = await ipfs.object.put(testObj)
38+
const node1Cid = await ipfs.object.put(obj)
8639
const node1 = await ipfs.object.get(node1Cid)
8740
let node2 = await ipfs.object.get(node1Cid)
8841

@@ -92,56 +45,11 @@ module.exports = (common, options) => {
9245
node2 = new DAGNode(Buffer.from(node2.Data), node2.Links, node2.size)
9346
}
9447

95-
expect(node1.Data).to.deep.equal(node2.Data)
96-
expect(node1.Links).to.deep.equal(node2.Links)
97-
})
98-
99-
it('should get object by multihash string', (done) => {
100-
const obj = {
101-
Data: Buffer.from(hat()),
102-
Links: []
103-
}
104-
105-
let node1
106-
let node1Cid
107-
let node2
108-
109-
series([
110-
(cb) => {
111-
ipfs.object.put(obj, (err, cid) => {
112-
expect(err).to.not.exist()
113-
node1Cid = cid
114-
115-
ipfs.object.get(node1Cid, (err, node) => {
116-
expect(err).to.not.exist()
117-
node1 = node
118-
cb()
119-
})
120-
})
121-
},
122-
(cb) => {
123-
// get object from ipfs multihash string
124-
ipfs.object.get(node1Cid.toBaseEncodedString(), (err, node) => {
125-
expect(err).to.not.exist()
126-
// because js-ipfs-api can't infer if the
127-
// returned Data is Buffer or String
128-
if (typeof node.Data === 'string') {
129-
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
130-
}
131-
132-
node2 = node
133-
cb()
134-
})
135-
},
136-
(cb) => {
137-
expect(node1.Data).to.eql(node2.Data)
138-
expect(node1.Links).to.eql(node2.Links)
139-
cb()
140-
}
141-
], done)
48+
expect(node1.Data).to.eql(node2.Data)
49+
expect(node1.Links).to.eql(node2.Links)
14250
})
14351

144-
it('should get object by multihash string (promised)', async () => {
52+
it('should get object by multihash string', async () => {
14553
const obj = {
14654
Data: Buffer.from(hat()),
14755
Links: []
@@ -161,189 +69,86 @@ module.exports = (common, options) => {
16169
expect(node1.Links).to.deep.equal(node2.Links)
16270
})
16371

164-
it('should get object with links by multihash string', (done) => {
165-
let node1a
166-
let node1b
167-
let node1bCid
168-
let node1c
169-
let node2
170-
171-
series([
172-
(cb) => {
173-
try {
174-
node1a = new DAGNode(Buffer.from('Some data 1'))
175-
} catch (err) {
176-
return cb(err)
177-
}
178-
179-
cb()
180-
},
181-
(cb) => {
182-
try {
183-
node2 = new DAGNode(Buffer.from('Some data 2'))
184-
} catch (err) {
185-
return cb(err)
186-
}
187-
188-
cb()
189-
},
190-
(cb) => {
191-
asDAGLink(node2, 'some-link', (err, link) => {
192-
if (err) {
193-
return cb(err)
194-
}
195-
196-
node1b = new DAGNode(node1a.Data, node1a.Links.concat(link))
197-
198-
cb()
199-
})
200-
},
201-
(cb) => {
202-
ipfs.object.put(node1b, (err, cid) => {
203-
expect(err).to.not.exist()
204-
node1bCid = cid
205-
cb()
206-
})
207-
},
208-
(cb) => {
209-
ipfs.object.get(node1bCid, (err, node) => {
210-
expect(err).to.not.exist()
211-
212-
// because js-ipfs-api can't infer if the
213-
// returned Data is Buffer or String
214-
if (typeof node.Data === 'string') {
215-
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
216-
}
217-
218-
node1c = node
219-
cb()
220-
})
221-
},
222-
(cb) => {
223-
expect(node1a.Data).to.eql(node1c.Data)
224-
cb()
225-
}
226-
], done)
72+
it('should get object with links by multihash string', async () => {
73+
const node1a = new DAGNode(Buffer.from('Some data 1'))
74+
const node2 = new DAGNode(Buffer.from('Some data 2'))
75+
76+
const link = await asDAGLink(node2, 'some-link')
77+
const node1b = new DAGNode(node1a.Data, node1a.Links.concat(link))
78+
79+
const node1bCid = await ipfs.object.put(node1b)
80+
let node1c = await ipfs.object.get(node1bCid)
81+
82+
// because js-ipfs-api can't infer if the
83+
// returned Data is Buffer or String
84+
if (typeof node1c.Data === 'string') {
85+
node1c = new DAGNode(Buffer.from(node1c.Data), node1c.Links, node1c.size)
86+
}
87+
88+
expect(node1a.Data).to.eql(node1c.Data)
22789
})
22890

229-
it('should get object by base58 encoded multihash', (done) => {
91+
it('should get object by base58 encoded multihash', async () => {
23092
const obj = {
23193
Data: Buffer.from(hat()),
23294
Links: []
23395
}
23496

235-
let node1a
236-
let node1aCid
237-
let node1b
238-
239-
series([
240-
(cb) => {
241-
ipfs.object.put(obj, (err, cid) => {
242-
expect(err).to.not.exist()
243-
node1aCid = cid
244-
245-
ipfs.object.get(cid, (err, node) => {
246-
expect(err).to.not.exist()
247-
node1a = node
248-
cb()
249-
})
250-
})
251-
},
252-
(cb) => {
253-
ipfs.object.get(node1aCid, { enc: 'base58' }, (err, node) => {
254-
expect(err).to.not.exist()
255-
// because js-ipfs-api can't infer if the
256-
// returned Data is Buffer or String
257-
if (typeof node.Data === 'string') {
258-
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
259-
}
260-
node1b = node
261-
cb()
262-
})
263-
},
264-
(cb) => {
265-
expect(node1a.Data).to.eql(node1b.Data)
266-
expect(node1a.Links).to.eql(node1b.Links)
267-
cb()
268-
}
269-
], done)
97+
const node1aCid = await ipfs.object.put(obj)
98+
const node1a = await ipfs.object.get(node1aCid)
99+
let node1b = await ipfs.object.get(node1aCid, { enc: 'base58' })
100+
101+
// because js-ipfs-api can't infer if the
102+
// returned Data is Buffer or String
103+
if (typeof node1b.Data === 'string') {
104+
node1b = new DAGNode(Buffer.from(node1b.Data), node1b.Links, node1b.size)
105+
}
106+
107+
expect(node1a.Data).to.eql(node1b.Data)
108+
expect(node1a.Links).to.eql(node1b.Links)
270109
})
271110

272-
it('should get object by base58 encoded multihash string', (done) => {
111+
it('should get object by base58 encoded multihash string', async () => {
273112
const obj = {
274113
Data: Buffer.from(hat()),
275114
Links: []
276115
}
277116

278-
let node1a
279-
let node1aCid
280-
let node1b
281-
282-
series([
283-
(cb) => {
284-
ipfs.object.put(obj, (err, cid) => {
285-
expect(err).to.not.exist()
286-
node1aCid = cid
287-
288-
ipfs.object.get(cid, (err, node) => {
289-
expect(err).to.not.exist()
290-
node1a = node
291-
cb()
292-
})
293-
})
294-
},
295-
(cb) => {
296-
ipfs.object.get(node1aCid.toBaseEncodedString(), { enc: 'base58' }, (err, node) => {
297-
expect(err).to.not.exist()
298-
// because js-ipfs-api can't infer if the
299-
// returned Data is Buffer or String
300-
if (typeof node.Data === 'string') {
301-
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
302-
}
303-
node1b = node
304-
cb()
305-
})
306-
},
307-
(cb) => {
308-
expect(node1a.Data).to.eql(node1b.Data)
309-
expect(node1a.Links).to.eql(node1b.Links)
310-
cb()
311-
}
312-
], done)
117+
const node1aCid = await ipfs.object.put(obj)
118+
const node1a = await ipfs.object.get(node1aCid)
119+
let node1b = await ipfs.object.get(node1aCid.toBaseEncodedString(), { enc: 'base58' })
120+
121+
// because js-ipfs-api can't infer if the
122+
// returned Data is Buffer or String
123+
if (typeof node1b.Data === 'string') {
124+
node1b = new DAGNode(Buffer.from(node1b.Data), node1b.Links, node1b.size)
125+
}
126+
127+
expect(node1a.Data).to.eql(node1b.Data)
128+
expect(node1a.Links).to.eql(node1b.Links)
313129
})
314130

315-
it('should supply unaltered data', () => {
131+
it('should supply unaltered data', async () => {
316132
// has to be big enough to span several DAGNodes
317133
const data = crypto.randomBytes(1024 * 3000)
318134

319-
return ipfs.add({
135+
const result = await ipfs.add({
320136
path: '',
321137
content: data
322138
})
323-
.then((result) => {
324-
return ipfs.object.get(result[0].hash)
325-
})
326-
.then((node) => {
327-
const meta = UnixFs.unmarshal(node.Data)
328-
329-
expect(meta.fileSize()).to.equal(data.length)
330-
})
139+
140+
const node = await ipfs.object.get(result[0].hash)
141+
const meta = UnixFs.unmarshal(node.Data)
142+
143+
expect(meta.fileSize()).to.equal(data.length)
331144
})
332145

333146
it('should error for request without argument', () => {
334-
return ipfs.object.get(null)
335-
.then(
336-
() => expect.fail('should have returned an error for invalid argument'),
337-
(err) => expect(err).to.be.an.instanceof(Error)
338-
)
147+
return expect(ipfs.object.get(null)).to.eventually.be.rejected.and.be.an.instanceOf(Error)
339148
})
340149

341150
it('returns error for request with invalid argument', () => {
342-
return ipfs.object.get('invalid', { enc: 'base58' })
343-
.then(
344-
() => expect.fail('should have returned an error for invalid argument'),
345-
(err) => expect(err).to.be.an.instanceof(Error)
346-
)
151+
return expect(ipfs.object.get('invalid', { enc: 'base58' })).to.eventually.be.rejected.and.be.an.instanceOf(Error)
347152
})
348153
})
349154
}

0 commit comments

Comments
 (0)