From 5321d6a0b33a4ff598f0f8f0ff6e6d3a9e9f260b Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Mon, 5 Feb 2018 13:04:28 +0100 Subject: [PATCH] fix: use binary blobs directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 https://github.com/ipld/interface-ipld-format/issues/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) => {…} --- package.json | 3 +-- src/resolver.js | 16 ++++++++-------- test/resolver.spec.js | 33 ++++++++++++--------------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 8de3541..1f1aba0 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/resolver.js b/src/resolver.js index fcea70a..3e8e3bc 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -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) } @@ -95,7 +95,7 @@ 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 @@ -103,7 +103,7 @@ exports.tree = (block, options, callback) => { options = options || {} - util.deserialize(block.data, (err, node) => { + util.deserialize(binaryBlob, (err, node) => { if (err) { return callback(err) } @@ -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) } diff --git a/test/resolver.spec.js b/test/resolver.spec.js index af8d0ab..2fb33a5 100644 --- a/test/resolver.spec.js +++ b/test/resolver.spec.js @@ -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 = {} @@ -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) @@ -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() @@ -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() @@ -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([ @@ -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) @@ -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() @@ -122,7 +113,7 @@ 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() @@ -130,7 +121,7 @@ describe('IPLD format resolver (local)', () => { }) 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() @@ -138,7 +129,7 @@ describe('IPLD format resolver (local)', () => { }) 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