Skip to content
This repository has been archived by the owner on Aug 24, 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 f5d9d68 commit 334f2f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,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
12 changes: 6 additions & 6 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const personInfoPaths = [
'date'
]

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 @@ -76,15 +76,15 @@ exports.resolve = (block, path, callback) => {
})
}

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 Down Expand Up @@ -133,8 +133,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
63 changes: 27 additions & 36 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ 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 ipldGit = require('../src')
const resolver = ipldGit.resolver

describe('IPLD format resolver (local)', () => {
let commitBlock
let tagBlock
let treeBlock
let blobBlock
let commitBlob
let tagBlob
let treeBlob
let blobBlob

before((done) => {
const commitNode = {
Expand Down Expand Up @@ -77,25 +74,19 @@ describe('IPLD format resolver (local)', () => {
(cb) => ipldGit.util.serialize(treeNode, cb),
(cb) => ipldGit.util.serialize(blobNode, cb)
], cb),
(res, cb) => map(res, (s, cb) => {
multihashing(s, 'sha1', (err, multihash) => {
expect(err).to.not.exist()
cb(null, new Block(s, new CID(multihash)))
})
}, cb),
(blocks, cb) => {
commitBlock = blocks[0]
tagBlock = blocks[1]
treeBlock = blocks[2]
blobBlock = blocks[3]
commitBlob = blocks[0]
tagBlob = blocks[1]
treeBlob = blocks[2]
blobBlob = blocks[3]
cb()
}
], done)
})

describe('commit', () => {
it('resolver.tree', (done) => {
resolver.tree(commitBlock, (err, paths) => {
resolver.tree(commitBlob, (err, paths) => {
expect(err).to.not.exist()

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

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

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

describe('resolver.resolve', () => {
it('path within scope', (done) => {
resolver.resolve(commitBlock, 'message', (err, result) => {
resolver.resolve(commitBlob, 'message', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.equal('Encoded\n')
done()
})
})

it('path within scope, but nested', (done) => {
resolver.resolve(commitBlock, 'author/name', (err, result) => {
resolver.resolve(commitBlob, 'author/name', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.equal('John Doe')
done()
})
})

it('path out of scope', (done) => {
resolver.resolve(commitBlock, 'tree/foo/hash/bar/mode', (err, result) => {
resolver.resolve(commitBlob, 'tree/foo/hash/bar/mode', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.eql({
'/': new CID('z8mWaJ1dZ9fH5EetPuRsj8jj26pXsgpsr').buffer
Expand All @@ -166,7 +157,7 @@ describe('IPLD format resolver (local)', () => {

describe('tag', () => {
it('resolver.tree', (done) => {
resolver.tree(tagBlock, (err, paths) => {
resolver.tree(tagBlob, (err, paths) => {
expect(err).to.not.exist()

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

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

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

describe('resolver.resolve', () => {
it('path within scope', (done) => {
resolver.resolve(tagBlock, 'message', (err, result) => {
resolver.resolve(tagBlob, 'message', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.equal('A message\n')
done()
})
})

it('path within scope, but nested', (done) => {
resolver.resolve(tagBlock, 'tagger/name', (err, result) => {
resolver.resolve(tagBlob, 'tagger/name', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.equal('John Doe')
done()
})
})

it('path out of scope', (done) => {
resolver.resolve(tagBlock, 'object/tree/foo/mode', (err, result) => {
resolver.resolve(tagBlob, 'object/tree/foo/mode', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.eql({
'/': new CID('z8mWaHQaEAKd5KMRNU3npB3saSZmhFh3e').buffer
Expand All @@ -233,7 +224,7 @@ describe('IPLD format resolver (local)', () => {

describe('tree', () => {
it('resolver.tree', (done) => {
resolver.tree(treeBlock, (err, paths) => {
resolver.tree(treeBlob, (err, paths) => {
expect(err).to.not.exist()

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

it('resolver.isLink with valid Link', (done) => {
resolver.isLink(treeBlock, 'somefile/hash', (err, link) => {
resolver.isLink(treeBlob, 'somefile/hash', (err, link) => {
expect(err).to.not.exist()
const linkCID = new CID(link['/'])
expect(CID.isCID(linkCID)).to.equal(true)
Expand All @@ -259,7 +250,7 @@ describe('IPLD format resolver (local)', () => {
})

it('resolver.isLink with invalid Link', (done) => {
resolver.isLink(treeBlock, '', (err, link) => {
resolver.isLink(treeBlob, '', (err, link) => {
expect(err).to.not.exist()
expect(link).to.equal(false)
done()
Expand All @@ -268,15 +259,15 @@ describe('IPLD format resolver (local)', () => {

describe('resolver.resolve', () => {
it('path within scope, nested', (done) => {
resolver.resolve(treeBlock, 'somedir/mode', (err, result) => {
resolver.resolve(treeBlob, 'somedir/mode', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.equal('40000')
done()
})
})

it('path out of scope', (done) => {
resolver.resolve(treeBlock, 'somedir/hash/subfile/mode', (err, result) => {
resolver.resolve(treeBlob, 'somedir/hash/subfile/mode', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.eql({
'/': new CID('z8mWaFY1zpiZSXTBrz8i6A3o9vNvAs2CH').buffer
Expand All @@ -290,15 +281,15 @@ describe('IPLD format resolver (local)', () => {

describe('blob', () => {
it('resolver.tree', (done) => {
resolver.tree(blobBlock, (err, paths) => {
resolver.tree(blobBlob, (err, paths) => {
expect(err).to.not.exist()
expect(paths).to.eql([])
done()
})
})

it('resolver.isLink with invalid Link', (done) => {
resolver.isLink(treeBlock, '', (err, link) => {
resolver.isLink(treeBlob, '', (err, link) => {
expect(err).to.not.exist()
expect(link).to.equal(false)
done()
Expand Down

0 comments on commit 334f2f0

Please sign in to comment.