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

Commit 5321d6a

Browse files
committed
fix: use binary blobs directly
IPLD shouldn't need to know about IPFS. Hence work directly with the binary data instead of using an IPFS block. This is part of ipld/interface-ipld-format#21 BREAKING CHANGE: Everyone calling the functions of `resolve` need to pass in the binary data instead of an IPFS block. So if your input is an IPFS block, the code changes from resolver.resolve(block, path, (err, result) => {…} to resolver.resolve(block.data, path, (err, result) => {…}
1 parent ef4a164 commit 5321d6a

3 files changed

Lines changed: 21 additions & 31 deletions

File tree

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151
"chai": "^4.1.2",
5252
"deep-freeze": "0.0.1",
5353
"dirty-chai": "^2.0.1",
54-
"garbage": "0.0.0",
55-
"ipfs-block": "~0.6.1"
54+
"garbage": "0.0.0"
5655
},
5756
"contributors": [
5857
"David Dias <daviddias.p@gmail.com>",

src/resolver.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ exports = module.exports
88
exports.multicodec = 'dag-cbor'
99

1010
/*
11-
* resolve: receives a path and a block and returns the value on path,
12-
* throw if not possible. `block` is an IPFS Block instance (contains data + cid)
11+
* resolve: receives a path and a binary blob and returns the value on path,
12+
* throw if not possible. `binaryBlob` is CBOR encoded data.
1313
*/
14-
exports.resolve = (block, path, callback) => {
14+
exports.resolve = (binaryBlob, path, callback) => {
1515
if (typeof path === 'function') {
1616
callback = path
1717
path = undefined
1818
}
1919

20-
util.deserialize(block.data, (err, node) => {
20+
util.deserialize(binaryBlob, (err, node) => {
2121
if (err) {
2222
return callback(err)
2323
}
@@ -95,15 +95,15 @@ function flattenObject (obj, delimiter) {
9595
* tree: returns a flattened array with paths: values of the project. options
9696
* are option (i.e. nestness)
9797
*/
98-
exports.tree = (block, options, callback) => {
98+
exports.tree = (binaryBlob, options, callback) => {
9999
if (typeof options === 'function') {
100100
callback = options
101101
options = undefined
102102
}
103103

104104
options = options || {}
105105

106-
util.deserialize(block.data, (err, node) => {
106+
util.deserialize(binaryBlob, (err, node) => {
107107
if (err) {
108108
return callback(err)
109109
}
@@ -114,8 +114,8 @@ exports.tree = (block, options, callback) => {
114114
})
115115
}
116116

117-
exports.isLink = (block, path, callback) => {
118-
exports.resolve(block, path, (err, result) => {
117+
exports.isLink = (binaryBlob, path, callback) => {
118+
exports.resolve(binaryBlob, path, (err, result) => {
119119
if (err) {
120120
return callback(err)
121121
}

test/resolver.spec.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ const dirtyChai = require('dirty-chai')
77
const expect = chai.expect
88
chai.use(dirtyChai)
99

10-
const Block = require('ipfs-block')
11-
const map = require('async/map')
1210
const waterfall = require('async/waterfall')
1311
const parallel = require('async/parallel')
1412
const CID = require('cids')
15-
const multihashing = require('multihashing-async')
1613

1714
const dagCBOR = require('../src')
1815
const resolver = dagCBOR.resolver
1916

2017
describe('IPLD format resolver (local)', () => {
21-
let emptyNodeBlock
22-
let nodeBlock
18+
let emptyNodeBlob
19+
let nodeBlob
2320

2421
before((done) => {
2522
const emptyNode = {}
@@ -44,15 +41,9 @@ describe('IPLD format resolver (local)', () => {
4441
(cb) => dagCBOR.util.serialize(emptyNode, cb),
4542
(cb) => dagCBOR.util.serialize(node, cb)
4643
], cb),
47-
(res, cb) => map(res, (s, cb) => {
48-
multihashing(s, 'sha2-256', (err, multihash) => {
49-
expect(err).to.not.exist()
50-
cb(null, new Block(s, new CID(multihash)))
51-
})
52-
}, cb),
5344
(blocks, cb) => {
54-
emptyNodeBlock = blocks[0]
55-
nodeBlock = blocks[1]
45+
emptyNodeBlob = blocks[0]
46+
nodeBlob = blocks[1]
5647
cb()
5748
}
5849
], done)
@@ -65,7 +56,7 @@ describe('IPLD format resolver (local)', () => {
6556
describe('empty node', () => {
6657
describe('resolver.resolve', () => {
6758
it('root', (done) => {
68-
resolver.resolve(emptyNodeBlock, '/', (err, result) => {
59+
resolver.resolve(emptyNodeBlob, '/', (err, result) => {
6960
expect(err).to.not.exist()
7061
expect(result.value).to.be.eql({})
7162
done()
@@ -74,7 +65,7 @@ describe('IPLD format resolver (local)', () => {
7465
})
7566

7667
it('resolver.tree', (done) => {
77-
resolver.tree(emptyNodeBlock, (err, paths) => {
68+
resolver.tree(emptyNodeBlob, (err, paths) => {
7869
expect(err).to.not.exist()
7970
expect(paths).to.eql([])
8071
done()
@@ -84,7 +75,7 @@ describe('IPLD format resolver (local)', () => {
8475

8576
describe('node', () => {
8677
it('resolver.tree', (done) => {
87-
resolver.tree(nodeBlock, (err, paths) => {
78+
resolver.tree(nodeBlob, (err, paths) => {
8879
expect(err).to.not.exist()
8980

9081
expect(paths).to.eql([
@@ -104,7 +95,7 @@ describe('IPLD format resolver (local)', () => {
10495
})
10596

10697
it('resolver.isLink with valid Link', (done) => {
107-
resolver.isLink(nodeBlock, 'someLink', (err, link) => {
98+
resolver.isLink(nodeBlob, 'someLink', (err, link) => {
10899
expect(err).to.not.exist()
109100
const linkCID = new CID(link['/'])
110101
expect(CID.isCID(linkCID)).to.equal(true)
@@ -113,7 +104,7 @@ describe('IPLD format resolver (local)', () => {
113104
})
114105

115106
it('resolver.isLink with invalid Link', (done) => {
116-
resolver.isLink(nodeBlock, '', (err, link) => {
107+
resolver.isLink(nodeBlob, '', (err, link) => {
117108
expect(err).to.not.exist()
118109
expect(link).to.equal(false)
119110
done()
@@ -122,23 +113,23 @@ describe('IPLD format resolver (local)', () => {
122113

123114
describe('resolver.resolve', () => {
124115
it('path within scope', (done) => {
125-
resolver.resolve(nodeBlock, 'name', (err, result) => {
116+
resolver.resolve(nodeBlob, 'name', (err, result) => {
126117
expect(err).to.not.exist()
127118
expect(result.value).to.equal('I am a node')
128119
done()
129120
})
130121
})
131122

132123
it('path within scope, but nested', (done) => {
133-
resolver.resolve(nodeBlock, 'nest/foo/bar', (err, result) => {
124+
resolver.resolve(nodeBlob, 'nest/foo/bar', (err, result) => {
134125
expect(err).to.not.exist()
135126
expect(result.value).to.equal('baz')
136127
done()
137128
})
138129
})
139130

140131
it('path out of scope', (done) => {
141-
resolver.resolve(nodeBlock, 'someLink/a/b/c', (err, result) => {
132+
resolver.resolve(nodeBlob, 'someLink/a/b/c', (err, result) => {
142133
expect(err).to.not.exist()
143134
expect(result.value).to.eql({
144135
'/': new CID('QmaNh5d3hFiqJAGjHmvxihSnWDGqYZCn7H2XHpbttYjCNE').buffer

0 commit comments

Comments
 (0)