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

Commit

Permalink
fix: use binary blobs directly
Browse files Browse the repository at this point in the history
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) => {…}
  • Loading branch information
vmx committed Feb 12, 2018
1 parent ef4a164 commit 5321d6a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"chai": "^4.1.2",
"deep-freeze": "0.0.1",
"dirty-chai": "^2.0.1",
"garbage": "0.0.0",
"ipfs-block": "~0.6.1"
"garbage": "0.0.0"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand Down
16 changes: 8 additions & 8 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ exports = module.exports
exports.multicodec = 'dag-cbor'

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

util.deserialize(block.data, (err, node) => {
util.deserialize(binaryBlob, (err, node) => {
if (err) {
return callback(err)
}
Expand Down Expand Up @@ -95,15 +95,15 @@ function flattenObject (obj, delimiter) {
* tree: returns a flattened array with paths: values of the project. options
* are option (i.e. nestness)
*/
exports.tree = (block, options, callback) => {
exports.tree = (binaryBlob, options, callback) => {
if (typeof options === 'function') {
callback = options
options = undefined
}

options = options || {}

util.deserialize(block.data, (err, node) => {
util.deserialize(binaryBlob, (err, node) => {
if (err) {
return callback(err)
}
Expand All @@ -114,8 +114,8 @@ exports.tree = (block, options, callback) => {
})
}

exports.isLink = (block, path, callback) => {
exports.resolve(block, path, (err, result) => {
exports.isLink = (binaryBlob, path, callback) => {
exports.resolve(binaryBlob, path, (err, result) => {
if (err) {
return callback(err)
}
Expand Down
33 changes: 12 additions & 21 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const Block = require('ipfs-block')
const map = require('async/map')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const CID = require('cids')
const multihashing = require('multihashing-async')

const dagCBOR = require('../src')
const resolver = dagCBOR.resolver

describe('IPLD format resolver (local)', () => {
let emptyNodeBlock
let nodeBlock
let emptyNodeBlob
let nodeBlob

before((done) => {
const emptyNode = {}
Expand All @@ -44,15 +41,9 @@ describe('IPLD format resolver (local)', () => {
(cb) => dagCBOR.util.serialize(emptyNode, cb),
(cb) => dagCBOR.util.serialize(node, cb)
], cb),
(res, cb) => map(res, (s, cb) => {
multihashing(s, 'sha2-256', (err, multihash) => {
expect(err).to.not.exist()
cb(null, new Block(s, new CID(multihash)))
})
}, cb),
(blocks, cb) => {
emptyNodeBlock = blocks[0]
nodeBlock = blocks[1]
emptyNodeBlob = blocks[0]
nodeBlob = blocks[1]
cb()
}
], done)
Expand All @@ -65,7 +56,7 @@ describe('IPLD format resolver (local)', () => {
describe('empty node', () => {
describe('resolver.resolve', () => {
it('root', (done) => {
resolver.resolve(emptyNodeBlock, '/', (err, result) => {
resolver.resolve(emptyNodeBlob, '/', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.be.eql({})
done()
Expand All @@ -74,7 +65,7 @@ describe('IPLD format resolver (local)', () => {
})

it('resolver.tree', (done) => {
resolver.tree(emptyNodeBlock, (err, paths) => {
resolver.tree(emptyNodeBlob, (err, paths) => {
expect(err).to.not.exist()
expect(paths).to.eql([])
done()
Expand All @@ -84,7 +75,7 @@ describe('IPLD format resolver (local)', () => {

describe('node', () => {
it('resolver.tree', (done) => {
resolver.tree(nodeBlock, (err, paths) => {
resolver.tree(nodeBlob, (err, paths) => {
expect(err).to.not.exist()

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

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

it('resolver.isLink with invalid Link', (done) => {
resolver.isLink(nodeBlock, '', (err, link) => {
resolver.isLink(nodeBlob, '', (err, link) => {
expect(err).to.not.exist()
expect(link).to.equal(false)
done()
Expand All @@ -122,23 +113,23 @@ describe('IPLD format resolver (local)', () => {

describe('resolver.resolve', () => {
it('path within scope', (done) => {
resolver.resolve(nodeBlock, 'name', (err, result) => {
resolver.resolve(nodeBlob, 'name', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.equal('I am a node')
done()
})
})

it('path within scope, but nested', (done) => {
resolver.resolve(nodeBlock, 'nest/foo/bar', (err, result) => {
resolver.resolve(nodeBlob, 'nest/foo/bar', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.equal('baz')
done()
})
})

it('path out of scope', (done) => {
resolver.resolve(nodeBlock, 'someLink/a/b/c', (err, result) => {
resolver.resolve(nodeBlob, 'someLink/a/b/c', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.eql({
'/': new CID('QmaNh5d3hFiqJAGjHmvxihSnWDGqYZCn7H2XHpbttYjCNE').buffer
Expand Down

0 comments on commit 5321d6a

Please sign in to comment.