From e1a6a59fb7098873ddc85ef75ff559bdbd558bf3 Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Mon, 23 Jul 2018 21:08:49 -0500 Subject: [PATCH 1/6] feat: add missing method in dag * add method `tree` in dag. But it is not following the links when options.recursive is true at this moment. * add some tests for dag operations (put, get, and tree). --- src/dag/index.js | 3 +- src/dag/tree.js | 37 +++++++++++++++++++++++ test/dag.spec.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/dag/tree.js create mode 100644 test/dag.spec.js diff --git a/src/dag/index.js b/src/dag/index.js index bb6b1333c..36da97643 100644 --- a/src/dag/index.js +++ b/src/dag/index.js @@ -7,6 +7,7 @@ module.exports = (arg) => { return { get: require('./get')(send), - put: require('./put')(send) + put: require('./put')(send), + tree: require('./tree')(send) } } diff --git a/src/dag/tree.js b/src/dag/tree.js new file mode 100644 index 000000000..2e16db0cc --- /dev/null +++ b/src/dag/tree.js @@ -0,0 +1,37 @@ +'use strict' +const promisify = require('promisify-es6') +const block = require('../block') + +const DAGFormats = { + 'dag-cbor': require('ipld-dag-cbor'), + 'dag-pb': require('ipld-dag-pb') +} + +module.exports = (send) => { + return promisify((cid, path, options, callback) => { + if (typeof path === 'function') { + callback = path + path = undefined + } + + if (typeof options === 'function') { + callback = options + options = {} + } + + options = options || {} + path = path || '' + + // FIXME: handle case when options.recursive is true + block(send).get(cid, options, (err, ipfsBlock) => { + if (err) return callback(err, null) + + let codec = ipfsBlock.cid.codec + if (codec in DAGFormats) { + DAGFormats[codec].resolver.tree(ipfsBlock.data, callback) + } else { + callback(new Error(`codec ${codec} is not valid`), null) + } + }) + }) +} diff --git a/test/dag.spec.js b/test/dag.spec.js new file mode 100644 index 000000000..6bf4ad2fd --- /dev/null +++ b/test/dag.spec.js @@ -0,0 +1,77 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) + +const IPFSApi = require('../src') +const f = require('./utils/factory') + +describe('.dag', function () { + this.timeout(50 * 1000) // slow CI + + let ipfs + let ipfsd + + const obj = { + a: 1, + b: [1, 2, 3], + c: { + ca: [5, 6, 7], + cb: 'foo' + } + } + + before((done) => { + f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => { + expect(err).to.not.exist() + ipfsd = _ipfsd + ipfs = IPFSApi(_ipfsd.apiAddr) + done() + }) + }) + + after((done) => { + if (!ipfsd) return done() + ipfsd.stop(done) + }) + + it('.dag.tree', (done) => { + const expectedPaths = [ + 'a', 'b', 'b/0', 'b/1', 'b/2', 'c', 'c/ca', + 'c/ca/0', 'c/ca/1', 'c/ca/2', 'c/cb' + ] + + ipfs.dag.put(obj, (err, cid) => { + expect(err).to.not.exist() + ipfs.dag.tree(cid, {}, (err, paths) => { + expect(err).to.not.exist() + expect(paths).deep.equal(expectedPaths) + done() + }) + }) + }) + + it('.dag.put', (done) => { + let expectedCidStr = 'zdpuArMWc9Ee3B7zUDucRjvA1bDgYpWt8rpUXXjY3tbmBw619' + ipfs.dag.put(obj, (err, cid) => { + expect(err).to.not.exist() + let cidStr = cid.toBaseEncodedString() + expect(cidStr).to.be.equal(expectedCidStr) + done() + }) + }) + + it('.dag.get', (done) => { + ipfs.dag.put(obj, (err, cid) => { + expect(err).to.not.exist() + ipfs.dag.get(cid, (err, data) => { + expect(err).to.not.exist() + expect(data.value).deep.equal(obj) + done() + }) + }) + }) +}) From 046d305569811507c605e07f557fea8d05999b8f Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Tue, 24 Jul 2018 11:33:28 -0500 Subject: [PATCH 2/6] fix: change return value from null to undefined --- src/dag/tree.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dag/tree.js b/src/dag/tree.js index 2e16db0cc..fb5ccb1e8 100644 --- a/src/dag/tree.js +++ b/src/dag/tree.js @@ -24,9 +24,9 @@ module.exports = (send) => { // FIXME: handle case when options.recursive is true block(send).get(cid, options, (err, ipfsBlock) => { - if (err) return callback(err, null) + if (err) return callback(err) - let codec = ipfsBlock.cid.codec + const codec = ipfsBlock.cid.codec if (codec in DAGFormats) { DAGFormats[codec].resolver.tree(ipfsBlock.data, callback) } else { From 96843ae0cd8e37f97b63685b35362383ed62fda4 Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Wed, 25 Jul 2018 12:16:57 -0500 Subject: [PATCH 3/6] fix: change return value from null to undefined --- src/dag/tree.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dag/tree.js b/src/dag/tree.js index fb5ccb1e8..fb2f9e894 100644 --- a/src/dag/tree.js +++ b/src/dag/tree.js @@ -30,7 +30,7 @@ module.exports = (send) => { if (codec in DAGFormats) { DAGFormats[codec].resolver.tree(ipfsBlock.data, callback) } else { - callback(new Error(`codec ${codec} is not valid`), null) + callback(new Error(`codec ${codec} is not valid`)) } }) }) From 341609eff81e697f4e93d9fecbfae23cb3a3627f Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Wed, 25 Jul 2018 12:18:05 -0500 Subject: [PATCH 4/6] fix: add missing test for dag.tree --- test/dag.spec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/dag.spec.js b/test/dag.spec.js index 6bf4ad2fd..b755ffa72 100644 --- a/test/dag.spec.js +++ b/test/dag.spec.js @@ -1,6 +1,7 @@ /* eslint-env mocha */ 'use strict' +const CID = require('cids') const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect @@ -74,4 +75,13 @@ describe('.dag', function () { }) }) }) + + it('.dag.tree returns error when codec for cid is invalid', (done) => { + let cid = new CID('zdpuArMWc9Ee3B7zUDucRjvA1bDgYpWt8rpUXXjY3tbmBw619') + cid.codec = 'invalid-codec' + ipfs.dag.tree(cid, (err, paths) => { + expect(err).to.exist() + done() + }) + }) }) From a2290d13376da0d53a3e3357abfcf945c2e14388 Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Wed, 25 Jul 2018 12:54:34 -0500 Subject: [PATCH 5/6] fix: move dag.tree tests into a describe --- test/dag.spec.js | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/test/dag.spec.js b/test/dag.spec.js index b755ffa72..8efad79dd 100644 --- a/test/dag.spec.js +++ b/test/dag.spec.js @@ -39,20 +39,31 @@ describe('.dag', function () { ipfsd.stop(done) }) - it('.dag.tree', (done) => { - const expectedPaths = [ - 'a', 'b', 'b/0', 'b/1', 'b/2', 'c', 'c/ca', - 'c/ca/0', 'c/ca/1', 'c/ca/2', 'c/cb' - ] - - ipfs.dag.put(obj, (err, cid) => { - expect(err).to.not.exist() - ipfs.dag.tree(cid, {}, (err, paths) => { + describe('.dag.tree', () => { + it('should return all the paths', (done) => { + const expectedPaths = [ + 'a', 'b', 'b/0', 'b/1', 'b/2', 'c', 'c/ca', + 'c/ca/0', 'c/ca/1', 'c/ca/2', 'c/cb' + ] + ipfs.dag.put(obj, (err, cid) => { expect(err).to.not.exist() - expect(paths).deep.equal(expectedPaths) + ipfs.dag.tree(cid, (err, paths) => { + expect(err).to.not.exist() + expect(paths).deep.equal(expectedPaths) + done() + }) + }) + }) + + it('should return error when codec for cid is invalid', (done) => { + let cid = new CID('zdpuArMWc9Ee3B7zUDucRjvA1bDgYpWt8rpUXXjY3tbmBw619') + cid.codec = 'invalid-codec' + ipfs.dag.tree(cid, (err, paths) => { + expect(err).to.exist() done() }) }) + }) it('.dag.put', (done) => { @@ -75,13 +86,4 @@ describe('.dag', function () { }) }) }) - - it('.dag.tree returns error when codec for cid is invalid', (done) => { - let cid = new CID('zdpuArMWc9Ee3B7zUDucRjvA1bDgYpWt8rpUXXjY3tbmBw619') - cid.codec = 'invalid-codec' - ipfs.dag.tree(cid, (err, paths) => { - expect(err).to.exist() - done() - }) - }) }) From 319f76491f6b93bdd6697f92d3c441294b533a06 Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Wed, 25 Jul 2018 14:37:08 -0500 Subject: [PATCH 6/6] fix: solve lint errors in dag.spec.js --- test/dag.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dag.spec.js b/test/dag.spec.js index 8efad79dd..643cc76bc 100644 --- a/test/dag.spec.js +++ b/test/dag.spec.js @@ -1,4 +1,5 @@ /* eslint-env mocha */ +/* eslint max-nested-callbacks: ["error", 8] */ 'use strict' const CID = require('cids') @@ -63,7 +64,6 @@ describe('.dag', function () { done() }) }) - }) it('.dag.put', (done) => {