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

Commit 3164a81

Browse files
dignifiedquiredaviddias
authored andcommitted
feat: switch to borc (#31)
This makes cbor encoding and decoding a lot faster. Fixes #23
1 parent aefa686 commit 3164a81

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
"homepage": "https://github.com/ipld/js-ipld-dag-cbor",
3838
"dependencies": {
3939
"async": "^2.1.4",
40+
"borc": "^2.0.0",
4041
"bs58": "^3.1.0",
41-
"cbor": "^3.0.0",
4242
"cids": "^0.2.0",
4343
"multihashes": "^0.3.0",
4444
"multihashing-async": "^0.3.0",
@@ -58,4 +58,4 @@
5858
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>",
5959
"wanderer <mjbecze@gmail.com>"
6060
]
61-
}
61+
}

src/util.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

3-
const cbor = require('cbor')
3+
const cbor = require('borc')
44
const multihashing = require('multihashing-async')
55
const CID = require('cids')
66
const waterfall = require('async/waterfall')
7+
const setImmediate = require('async/setImmediate')
78

89
const resolver = require('./resolver')
910

@@ -15,13 +16,28 @@ exports.serialize = (dagNode, callback) => {
1516
serialized = cbor.encode(dagNode)
1617
} catch (err) {
1718
// return is important, otherwise in case of error the execution would continue
18-
return callback(err)
19+
return setImmediate(() => {
20+
callback(err)
21+
})
1922
}
20-
callback(null, serialized)
23+
setImmediate(() => {
24+
callback(null, serialized)
25+
})
2126
}
2227

2328
exports.deserialize = (data, callback) => {
24-
cbor.decodeFirst(data, callback)
29+
let res
30+
try {
31+
res = cbor.decodeFirst(data)
32+
} catch (err) {
33+
return setImmediate(() => {
34+
callback(err)
35+
})
36+
}
37+
38+
setImmediate(() => {
39+
callback(null, res)
40+
})
2541
}
2642

2743
exports.cid = (dagNode, callback) => {

test/resolver.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ describe('IPLD format resolver (local)', () => {
104104
expect(paths).to.eql([{
105105
path: 'name',
106106
value: 'I am a node'
107-
}, {
108-
// TODO: confirm how to represent links in tree
109-
path: 'someLink//',
110-
value: 'LINK'
111107
}, {
112108
path: 'nest/foo/bar',
113109
value: 'baz'
@@ -117,6 +113,10 @@ describe('IPLD format resolver (local)', () => {
117113
}, {
118114
path: 'array/1',
119115
value: 2
116+
}, {
117+
// TODO: confirm how to represent links in tree
118+
path: 'someLink//',
119+
value: 'LINK'
120120
}])
121121
done()
122122
})

0 commit comments

Comments
 (0)