Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 48eb863

Browse files
dignifiedquiredaviddias
authored andcommitted
feat: use async interfaces
1 parent f76db73 commit 48eb863

5 files changed

Lines changed: 135 additions & 87 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"babel-runtime": "^6.6.1",
4141
"bs58": "^3.0.0",
4242
"cbor": "^2.0.1",
43-
"cbor-sync": "^1.0.2",
4443
"cids": "^0.2.0",
4544
"multihashes": "^0.2.2",
4645
"multihashing": "^0.2.1",

src/resolver.js

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,66 +11,90 @@ exports.multicodec = 'dag-cbor'
1111
* resolve: receives a path and a block and returns the value on path,
1212
* throw if not possible. `block` is an IPFS Block instance (contains data + key)
1313
*/
14-
exports.resolve = (block, path) => {
15-
let node = util.deserialize(block.data)
14+
exports.resolve = (block, path, callback) => {
15+
if (typeof path === 'function') {
16+
callback = path
17+
path = undefined
18+
}
1619

17-
// root
20+
util.deserialize(block.data, (err, node) => {
21+
if (err) {
22+
return callback(err)
23+
}
1824

19-
if (!path || path === '/') {
20-
return { value: node, remainderPath: '' }
21-
}
25+
// root
2226

23-
// within scope
27+
if (!path || path === '/') {
28+
return callback(null, {
29+
value: node,
30+
remainderPath: ''
31+
})
32+
}
2433

25-
// const tree = exports.tree(block)
26-
const parts = path.split('/')
27-
const val = traverse(node).get(parts)
34+
// within scope
2835

29-
if (val) {
30-
return { value: val, remainderPath: '' }
31-
}
32-
console.log(val)
36+
// const tree = exports.tree(block)
37+
const parts = path.split('/')
38+
const val = traverse(node).get(parts)
39+
40+
if (val) {
41+
return callback(null, {
42+
value: val,
43+
remainderPath: ''
44+
})
45+
}
3346

34-
// out of scope
47+
// out of scope
3548

36-
// TODO this was my first try at writting this out of scope traversal code,
37-
// it REALLY needs way more testing.
38-
let value
49+
// TODO this was my first try at writting this out of scope traversal code,
50+
// it REALLY needs way more testing.
51+
let value
3952

40-
for (let i = 0; i < parts.length; i++) {
41-
let partialPath = parts.shift()
53+
for (let i = 0; i < parts.length; i++) {
54+
let partialPath = parts.shift()
4255

43-
if (Array.isArray(node) && !Buffer.isBuffer(node)) {
44-
value = node[Number(partialPath)]
45-
} if (node[partialPath]) {
46-
value = node[partialPath]
47-
} else {
48-
// can't traverse more
49-
if (!value) {
50-
throw new Error('path not available at root')
56+
if (Array.isArray(node) && !Buffer.isBuffer(node)) {
57+
value = node[Number(partialPath)]
58+
} if (node[partialPath]) {
59+
value = node[partialPath]
5160
} else {
52-
parts.unshift(partialPath)
53-
return {
54-
value: value,
55-
remainderPath: parts.join('/')
61+
// can't traverse more
62+
if (!value) {
63+
return callback(new Error('path not available at root'))
64+
} else {
65+
parts.unshift(partialPath)
66+
return callback(null, {
67+
value: value,
68+
remainderPath: parts.join('/')
69+
})
5670
}
5771
}
72+
node = value
5873
}
59-
node = value
60-
}
74+
})
6175
}
6276

6377
/*
6478
* tree: returns a flattened array with paths: values of the project. options
6579
* are option (i.e. nestness)
6680
*/
67-
exports.tree = (block, options) => {
81+
exports.tree = (block, options, callback) => {
82+
if (typeof options === 'function') {
83+
callback = options
84+
options = undefined
85+
}
86+
6887
if (!options) {
6988
options = {}
7089
}
7190

72-
const node = util.deserialize(block.data)
73-
return flattenObject(node)
91+
util.deserialize(block.data, (err, node) => {
92+
if (err) {
93+
return callback(err)
94+
}
95+
96+
callback(null, flattenObject(node))
97+
})
7498
}
7599

76100
// TODO recheck this API

src/util.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const cbor = require('cbor-sync')
3+
const cbor = require('cbor')
44
const multihashing = require('multihashing')
55
const CID = require('cids')
66
const resolver = require('./resolver')
@@ -11,12 +11,12 @@ exports.serialize = (dagNode) => {
1111
return cbor.encode(dagNode)
1212
}
1313

14-
exports.deserialize = (data) => {
15-
return cbor.decode(data)
14+
exports.deserialize = (data, callback) => {
15+
cbor.decodeFirst(data, callback)
1616
}
1717

18-
exports.cid = (dagNode) => {
18+
exports.cid = (dagNode, callback) => {
1919
const serialized = exports.serialize(dagNode)
2020
const mh = multihashing(serialized, 'sha2-256')
21-
return new CID(1, resolver.multicodec, mh)
21+
callback(null, new CID(1, resolver.multicodec, mh))
2222
}

test/resolver.spec.js

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
12
/* eslint-env mocha */
23
'use strict'
34

@@ -37,58 +38,76 @@ describe('IPLD format resolver (local)', () => {
3738

3839
describe('empty node', () => {
3940
describe('resolver.resolve', () => {
40-
it('root', () => {
41-
const result = resolver.resolve(emptyNodeBlock, '/')
42-
expect(result.value).to.be.eql({})
41+
it('root', (done) => {
42+
resolver.resolve(emptyNodeBlock, '/', (err, result) => {
43+
expect(err).to.not.exist
44+
expect(result.value).to.be.eql({})
45+
done()
46+
})
4347
})
4448
})
4549

46-
it('resolver.tree', () => {
47-
const paths = resolver.tree(emptyNodeBlock)
48-
expect(paths).to.eql([])
50+
it('resolver.tree', (done) => {
51+
resolver.tree(emptyNodeBlock, (err, paths) => {
52+
expect(err).to.not.exist
53+
expect(paths).to.eql([])
54+
done()
55+
})
4956
})
5057

51-
it.skip('resolver.patch', (done) => {})
58+
it.skip('resolver.patch', () => {})
5259
})
5360

5461
describe('node', () => {
5562
describe('resolver.resolve', () => {
56-
it('path within scope', () => {
57-
const result = resolver.resolve(nodeBlock, 'name')
58-
expect(result.value).to.equal('I am a node')
63+
it('path within scope', (done) => {
64+
resolver.resolve(nodeBlock, 'name', (err, result) => {
65+
expect(err).to.not.exist
66+
expect(result.value).to.equal('I am a node')
67+
done()
68+
})
5969
})
6070

61-
it('path within scope, but nested', () => {
62-
const result = resolver.resolve(nodeBlock, 'nest/foo/bar')
63-
expect(result.value).to.equal('baz')
71+
it('path within scope, but nested', (done) => {
72+
resolver.resolve(nodeBlock, 'nest/foo/bar', (err, result) => {
73+
expect(err).to.not.exist
74+
expect(result.value).to.equal('baz')
75+
done()
76+
})
6477
})
6578

66-
it('path out of scope', () => {
67-
const result = resolver.resolve(nodeBlock, 'someLink/a/b/c')
68-
expect(result.value).to.eql({ '/': 'LINK' })
69-
expect(result.remainderPath).to.equal('a/b/c')
79+
it('path out of scope', (done) => {
80+
resolver.resolve(nodeBlock, 'someLink/a/b/c', (err, result) => {
81+
expect(err).to.not.exist
82+
expect(result.value).to.eql({ '/': 'LINK' })
83+
expect(result.remainderPath).to.equal('a/b/c')
84+
done()
85+
})
7086
})
7187
})
7288

73-
it('resolver.tree', () => {
74-
const paths = resolver.tree(nodeBlock)
75-
expect(paths).to.eql([{
76-
path: 'name',
77-
value: 'I am a node'
78-
}, {
79-
// TODO: confirm how to represent links in tree
80-
path: 'someLink//',
81-
value: 'LINK'
82-
}, {
83-
path: 'nest/foo/bar',
84-
value: 'baz'
85-
}, {
86-
path: 'array/0/a',
87-
value: 'b'
88-
}, {
89-
path: 'array/1',
90-
value: 2
91-
}])
89+
it('resolver.tree', (done) => {
90+
resolver.tree(nodeBlock, (err, paths) => {
91+
expect(err).to.not.exist
92+
expect(paths).to.eql([{
93+
path: 'name',
94+
value: 'I am a node'
95+
}, {
96+
// TODO: confirm how to represent links in tree
97+
path: 'someLink//',
98+
value: 'LINK'
99+
}, {
100+
path: 'nest/foo/bar',
101+
value: 'baz'
102+
}, {
103+
path: 'array/0/a',
104+
value: 'b'
105+
}, {
106+
path: 'array/1',
107+
value: 2
108+
}])
109+
done()
110+
})
92111
})
93112

94113
it.skip('resolver.patch', () => {})

test/util.spec.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@ describe('util', () => {
1010
link: { '/': 'aaaaa' }
1111
}
1212

13-
it('.serialize and .deserialize', () => {
13+
it('.serialize and .deserialize', (done) => {
1414
const serialized = dagCBOR.util.serialize(obj)
1515
expect(Buffer.isBuffer(serialized)).to.be.true
1616

17-
const deserialized = dagCBOR.util.deserialize(serialized)
18-
expect(obj).to.eql(deserialized)
17+
dagCBOR.util.deserialize(serialized, (err, deserialized) => {
18+
expect(err).to.not.exist
19+
expect(obj).to.eql(deserialized)
20+
done()
21+
})
1922
})
2023

21-
it('.cid', () => {
22-
const cid = dagCBOR.util.cid(obj)
23-
expect(cid.version).to.equal(1)
24-
expect(cid.codec).to.equal('dag-cbor')
25-
expect(cid.multihash).to.exist
24+
it('.cid', (done) => {
25+
dagCBOR.util.cid(obj, (err, cid) => {
26+
expect(err).to.not.exist
27+
expect(cid.version).to.equal(1)
28+
expect(cid.codec).to.equal('dag-cbor')
29+
expect(cid.multihash).to.exist
30+
done()
31+
})
2632
})
2733
})

0 commit comments

Comments
 (0)