From 58df08f9b698a8f9778205e009037643b5ff72a3 Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Thu, 5 Apr 2018 18:16:56 -0700 Subject: [PATCH 1/9] Refactor ipld to use pull streams for get --- src/index.js | 104 ++++++++++++++++++++++------------------- test/ipld-all.js | 3 +- test/ipld-bitcoin.js | 15 +++--- test/ipld-dag-cbor.js | 58 ++++++++++++++++------- test/ipld-dag-pb.js | 17 ++++--- test/ipld-eth-block.js | 20 +++++--- test/ipld-eth.js | 12 ++--- test/ipld-git.js | 22 +++++---- test/ipld-zcash.js | 17 ++++--- 9 files changed, 163 insertions(+), 105 deletions(-) diff --git a/src/index.js b/src/index.js index 278d542..7145b07 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,6 @@ const Block = require('ipfs-block') const pull = require('pull-stream') const CID = require('cids') -const doUntil = require('async/doUntil') const IPFSRepo = require('ipfs-repo') const BlockService = require('ipfs-block-service') const joinPath = require('path').join @@ -125,9 +124,28 @@ class IPLDResolver { options = {} } + if (!options) { + options = {} + } + + pull(this.getPullStream(cid, path, options), + pull.reduce((arr, item) => { + if (!options.onlyNode) { + arr.push(item) + } else { + // replace existing entry + arr[0] = item + } + return arr + }, [], callback) + ) + } + + getPullStream (cid, path, options) { // this removes occurrences of ./, //, ../ // makes sure that path never starts with ./ or / // path.join is OS specific. Need to convert back to POSIX format. + if (typeof path === 'string') { path = joinPath('/', path) .substr(1) @@ -135,67 +153,55 @@ class IPLDResolver { .join('/') } + let stop = false if (path === '' || !path) { - return this._get(cid, (err, node) => { - if (err) { - return callback(err) - } - callback(null, { - value: node, - remainderPath: '' + return function read (abort, cb) { + if (stop) return cb(stop) + this._get(cid, (err, node) => { + if (err) { + return cb(err) + } + stop = true + cb(null, { + value: node, + remainderPath: '' + }) }) - }) + }.bind(this) } - let value - - doUntil( - (cb) => { - // get block - // use local resolver - // update path value - this.bs.get(cid, (err, block) => { + return function read (abort, cb) { + if (stop) return cb(stop) + this.bs.get(cid, (err, block) => { + if (err || abort) { + return cb(err) + } + const r = this.resolvers[cid.codec] + if (!r) { + return cb(new Error('No resolver found for codec "' + cid.codec + '"')) + } + r.resolver.resolve(block.data, path, (err, result) => { if (err) { return cb(err) } - const r = this.resolvers[cid.codec] - if (!r) { - return cb(new Error('No resolver found for codec "' + cid.codec + '"')) + const value = result.value + path = result.remainderPath + const endReached = !path || path === '' || path === '/' + const isTerminal = value && !value['/'] + + if ((endReached && isTerminal) || options.localResolve) { + stop = true + return cb(null, { value, remainderPath: path }) } - r.resolver.resolve(block.data, path, (err, result) => { - if (err) { - return cb(err) - } - value = result.value - path = result.remainderPath - cb() - }) - }) - }, - () => { - const endReached = !path || path === '' || path === '/' - const isTerminal = value && !value['/'] - if ((endReached && isTerminal) || options.localResolve) { - return true - } else { - // continue traversing if (value) { cid = new CID(value['/']) } - return false - } - }, - (err, results) => { - if (err) { - return callback(err) - } - return callback(null, { - value: value, - remainderPath: path + + cb(null, { value, remainderPath: path }) }) - } - ) + }) + }.bind(this) } getStream (cid, path, options) { diff --git a/test/ipld-all.js b/test/ipld-all.js index aa5baaf..8d8c22e 100644 --- a/test/ipld-all.js +++ b/test/ipld-all.js @@ -62,7 +62,8 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { it('resolve through different formats', (done) => { resolver.get(cidCbor, 'pb/Data', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(Buffer.from('I am inside a Protobuf')) + expect(result.length).to.eq(2) + expect(result[1].value).to.eql(Buffer.from('I am inside a Protobuf')) done() }) }) diff --git a/test/ipld-bitcoin.js b/test/ipld-bitcoin.js index d382cb0..1eab2a8 100644 --- a/test/ipld-bitcoin.js +++ b/test/ipld-bitcoin.js @@ -109,7 +109,7 @@ module.exports = (repo) => { expect(err).to.not.exist() resolver.get(cid1, (err, result) => { expect(err).to.not.exist() - expect(node1.version).to.eql(result.value.version) + expect(node1.version).to.eql(result[0].value.version) done() }) }) @@ -125,7 +125,7 @@ module.exports = (repo) => { resolver.get(cid1, '/', (err, result) => { expect(err).to.not.exist() - ipldBitcoin.util.cid(result.value, (err, cid) => { + ipldBitcoin.util.cid(result[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() @@ -136,7 +136,8 @@ module.exports = (repo) => { it('value within 1st node scope', (done) => { resolver.get(cid1, 'version', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(1) + expect(result.length).to.eq(1) + expect(result[0].value).to.eql(1) done() }) }) @@ -144,7 +145,8 @@ module.exports = (repo) => { it('value within nested scope (1 level)', (done) => { resolver.get(cid2, 'parent/version', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(1) + expect(result.length).to.eq(2) + expect(result[1].value).to.eql(1) done() }) }) @@ -152,7 +154,8 @@ module.exports = (repo) => { it('value within nested scope (2 levels)', (done) => { resolver.get(cid3, 'parent/parent/version', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(1) + expect(result.length).to.eq(3) + expect(result[2].value).to.eql(1) done() }) }) @@ -162,7 +165,7 @@ module.exports = (repo) => { expect(err).to.not.exist() resolver.get(cid1, (err, result) => { expect(err).to.not.exist() - expect(result.value.version).to.eql(1) + expect(result[0].value.version).to.eql(1) remove() }) }) diff --git a/test/ipld-dag-cbor.js b/test/ipld-dag-cbor.js index 4b488fd..19ab1b5 100644 --- a/test/ipld-dag-cbor.js +++ b/test/ipld-dag-cbor.js @@ -114,11 +114,26 @@ module.exports = (repo) => { }, done) }) + it('resolver.getPullStream just CID', (done) => { + pull(resolver.getPullStream(cid1, null), + pull.collect((err, results) => { + expect(err).to.not.exist() + expect(results.length).to.eq(1) + + dagCBOR.util.cid(results[0].value, (err, cid) => { + expect(err).to.not.exist() + expect(cid).to.eql(cid1) + }) + })) + done() + }) + it('resolver.get just CID', (done) => { - resolver.get(cid1, (err, result) => { + resolver.get(cid1, (err, results) => { + expect(results.length).to.eq(1) expect(err).to.not.exist() - dagCBOR.util.cid(result.value, (err, cid) => { + dagCBOR.util.cid(results[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() @@ -127,10 +142,11 @@ module.exports = (repo) => { }) it('resolver.get root path', (done) => { - resolver.get(cid1, '/', (err, result) => { + resolver.get(cid1, '/', (err, results) => { + expect(results.length).to.eq(1) expect(err).to.not.exist() - dagCBOR.util.cid(result.value, (err, cid) => { + dagCBOR.util.cid(results[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() @@ -141,8 +157,9 @@ module.exports = (repo) => { it('resolver.get relative path `.` (same as get /)', (done) => { resolver.get(cid1, '.', (err, result) => { expect(err).to.not.exist() + expect(result.length).to.eq(1) - dagCBOR.util.cid(result.value, (err, cid) => { + dagCBOR.util.cid(result[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() @@ -153,8 +170,9 @@ module.exports = (repo) => { it('resolver.get relative path `./` (same as get /)', (done) => { resolver.get(cid1, './', (err, result) => { expect(err).to.not.exist() + expect(result.length).to.eq(1) - dagCBOR.util.cid(result.value, (err, cid) => { + dagCBOR.util.cid(result[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() @@ -165,7 +183,8 @@ module.exports = (repo) => { it('resolver.get relative path `./one/someData` (same as get one/someData)', (done) => { resolver.get(cid2, './one/someData', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql('I am 1') + expect(result.length).to.eq(2) + expect(result[1].value).to.eql('I am 1') done() }) }) @@ -173,7 +192,8 @@ module.exports = (repo) => { it('resolver.get relative path `one/./someData` (same as get one/someData)', (done) => { resolver.get(cid2, 'one/./someData', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql('I am 1') + expect(result.length).to.eq(2) + expect(result[1].value).to.eql('I am 1') done() }) }) @@ -181,7 +201,8 @@ module.exports = (repo) => { it('resolver.get double slash at the beginning `//one/someData` (same as get one/someData)', (done) => { resolver.get(cid2, '//one/someData', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql('I am 1') + expect(result.length).to.eq(2) + expect(result[1].value).to.eql('I am 1') done() }) }) @@ -189,7 +210,8 @@ module.exports = (repo) => { it('resolver.get double slash in the middle `one//someData` (same as get one/someData)', (done) => { resolver.get(cid2, 'one//someData', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql('I am 1') + expect(result.length).to.eq(2) + expect(result[1].value).to.eql('I am 1') done() }) }) @@ -197,7 +219,8 @@ module.exports = (repo) => { it('resolver.get value within 1st node scope', (done) => { resolver.get(cid1, 'someData', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql('I am 1') + expect(result.length).to.eq(1) + expect(result[0].value).to.eql('I am 1') done() }) }) @@ -205,7 +228,8 @@ module.exports = (repo) => { it('resolver.get value within nested scope (0 level)', (done) => { resolver.get(cid2, 'one', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql({ + expect(result.length).to.eq(2) + expect(result[1].value).to.eql({ someData: 'I am 1' }) done() @@ -215,7 +239,8 @@ module.exports = (repo) => { it('resolver.get value within nested scope (1 level)', (done) => { resolver.get(cid2, 'one/someData', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql('I am 1') + expect(result.length).to.eq(2) + expect(result[1].value).to.eql('I am 1') done() }) }) @@ -223,8 +248,9 @@ module.exports = (repo) => { it('resolver.get value within nested scope (2 levels)', (done) => { resolver.get(cid3, 'two/one/someData', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql('I am 1') - expect(result.remainderPath).to.eql('') + expect(result.length).to.eq(3) + expect(result[2].value).to.eql('I am 1') + expect(result[2].remainderPath).to.eql('') done() }) @@ -306,7 +332,7 @@ module.exports = (repo) => { expect(err).to.not.exist() resolver.get(cid1, (err, result) => { expect(err).to.not.exist() - expect(node1).to.eql(result.value) + expect(node1).to.eql(result[0].value) remove() }) }) diff --git a/test/ipld-dag-pb.js b/test/ipld-dag-pb.js index 763ead1..7f20db1 100644 --- a/test/ipld-dag-pb.js +++ b/test/ipld-dag-pb.js @@ -178,8 +178,9 @@ module.exports = (repo) => { it('resolver.get root path', (done) => { resolver.get(cid1, '/', (err, result) => { expect(err).to.not.exist() + expect(result.length).to.eq(1) - dagPB.util.cid(result.value, (err, cid) => { + dagPB.util.cid(result[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() @@ -190,7 +191,8 @@ module.exports = (repo) => { it('resolver.get value within 1st node scope', (done) => { resolver.get(cid1, 'Data', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(Buffer.from('I am 1')) + expect(result.length).to.eq(1) + expect(result[0].value).to.eql(Buffer.from('I am 1')) done() }) }) @@ -198,7 +200,8 @@ module.exports = (repo) => { it('resolver.get value within nested scope (1 level)', (done) => { resolver.get(cid2, 'Links/0/Hash/Data', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(Buffer.from('I am 1')) + expect(result.length).to.eq(2) + expect(result[1].value).to.eql(Buffer.from('I am 1')) done() }) }) @@ -206,7 +209,8 @@ module.exports = (repo) => { it('resolver.get value within nested scope (2 levels)', (done) => { resolver.get(cid3, 'Links/1/Hash/Links/0/Hash/Data', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(Buffer.from('I am 1')) + expect(result.length).to.eq(3) + expect(result[2].value).to.eql(Buffer.from('I am 1')) done() }) }) @@ -214,8 +218,9 @@ module.exports = (repo) => { it('resolver.get with option localResolve: true', (done) => { resolver.get(cid3, 'Links/1/Hash/Links/0/Hash/Data', { localResolve: true }, (err, result) => { expect(err).to.not.exist() - expect(result.remainderPath).to.equal('Links/0/Hash/Data') - expect(result.value).to.eql({ + expect(result.length).to.eq(1) + expect(result[0].remainderPath).to.equal('Links/0/Hash/Data') + expect(result[0].value).to.eql({ '/': 'QmS149H7EbyMuZ2wtEF1sAd7gPwjj4rKAorweAjKMkxr8D' }) done() diff --git a/test/ipld-eth-block.js b/test/ipld-eth-block.js index 16f9f2f..ac4fa38 100644 --- a/test/ipld-eth-block.js +++ b/test/ipld-eth-block.js @@ -96,9 +96,10 @@ module.exports = (repo) => { expect(err).to.not.exist() resolver.get(cid1, (err, result) => { expect(err).to.not.exist() + expect(result.length).to.eq(1) expect(node1.number.toString('hex')).to.eql('01') - expect(node1.raw).to.eql(result.value.raw) - expect(node1.hash()).to.eql(result.value.hash()) + expect(node1.raw).to.eql(result[0].value.raw) + expect(node1.hash()).to.eql(result[0].value.hash()) done() }) }) @@ -113,8 +114,9 @@ module.exports = (repo) => { it('root path (same as get)', (done) => { resolver.get(cid1, '/', (err, result) => { expect(err).to.not.exist() + expect(result.length).to.eq(1) - ipldEthBlock.util.cid(result.value, (err, cid) => { + ipldEthBlock.util.cid(result[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() @@ -125,7 +127,8 @@ module.exports = (repo) => { it('value within 1st node scope', (done) => { resolver.get(cid1, 'number', (err, result) => { expect(err).to.not.exist() - expect(result.value.toString('hex')).to.eql('01') + expect(result.length).to.eq(1) + expect(result[0].value.toString('hex')).to.eql('01') done() }) }) @@ -133,7 +136,8 @@ module.exports = (repo) => { it('value within nested scope (1 level)', (done) => { resolver.get(cid2, 'parent/number', (err, result) => { expect(err).to.not.exist() - expect(result.value.toString('hex')).to.eql('01') + expect(result.length).to.eq(2) + expect(result[1].value.toString('hex')).to.eql('01') done() }) }) @@ -141,7 +145,9 @@ module.exports = (repo) => { it('value within nested scope (2 levels)', (done) => { resolver.get(cid3, 'parent/parent/number', (err, result) => { expect(err).to.not.exist() - expect(result.value.toString('hex')).to.eql('01') + expect(err).to.not.exist() + expect(result.length).to.eq(3) + expect(result[2].value.toString('hex')).to.eql('01') done() }) }) @@ -151,7 +157,7 @@ module.exports = (repo) => { expect(err).to.not.exist() resolver.get(cid1, (err, result) => { expect(err).to.not.exist() - const node = result.value + const node = result[0].value expect(node1.raw).to.eql(node.raw) expect(node1.hash()).to.eql(node.hash()) remove() diff --git a/test/ipld-eth.js b/test/ipld-eth.js index 2331b33..42ab98e 100644 --- a/test/ipld-eth.js +++ b/test/ipld-eth.js @@ -87,19 +87,19 @@ module.exports = (repo) => { describe('resolver.get', () => { it('block-to-block', (done) => { - resolver.get(ethObjs.child.cid, '/parent', (err, result) => { + resolver.get(ethObjs.child.cid, '/parent', {onlyNode: true}, (err, result) => { expect(err).to.not.exist() - expect(result.remainderPath).to.equal('') - expect(result.value.number.toString('hex')).to.equal('302516') + expect(result[0].remainderPath).to.equal('') + expect(result[0].value.number.toString('hex')).to.equal('302516') done() }) }) it('block-to-account resolve', (done) => { - resolver.get(ethObjs.child.cid, '/parent/state/0/0/0/0/1/7/2/7/8/a/1/e/6/e/9/6/3/5/e/1/a/3/f/1/1/e/b/0/2/2/d/a/1/f/5/7/e/a/0/0/4/d/8/5/2/d/9/d/1/9/4/2/d/4/3/6/0/8/5/4/0/4/7/1/nonce', (err, result) => { + resolver.get(ethObjs.child.cid, '/parent/state/0/0/0/0/1/7/2/7/8/a/1/e/6/e/9/6/3/5/e/1/a/3/f/1/1/e/b/0/2/2/d/a/1/f/5/7/e/a/0/0/4/d/8/5/2/d/9/d/1/9/4/2/d/4/3/6/0/8/5/4/0/4/7/1/nonce', {onlyNode: true}, (err, result) => { expect(err).to.not.exist() - expect(result.value.toString('hex'), '03') - expect(result.remainderPath).to.equal('') + expect(result[0].value.toString('hex'), '03') + expect(result[0].remainderPath).to.equal('') done() }) }) diff --git a/test/ipld-git.js b/test/ipld-git.js index 47df288..37c415b 100644 --- a/test/ipld-git.js +++ b/test/ipld-git.js @@ -163,7 +163,7 @@ module.exports = (repo) => { expect(err).to.not.exist() resolver.get(blobCid, (err, result) => { expect(err).to.not.exist() - expect(blobNode.toString('hex')).to.eql(result.value.toString('hex')) + expect(blobNode.toString('hex')).to.eql(result[0].value.toString('hex')) done() }) }) @@ -178,8 +178,9 @@ module.exports = (repo) => { it('resolver.get root path', (done) => { resolver.get(blobCid, '/', (err, result) => { expect(err).to.not.exist() + expect(result.length).to.eq(1) - ipldGit.util.cid(result.value, (err, cid) => { + ipldGit.util.cid(result[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(blobCid) done() @@ -190,7 +191,8 @@ module.exports = (repo) => { it('value within 1st node scope', (done) => { resolver.get(commitCid, 'message', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql('Initial commit\n') + expect(result.length).to.eq(1) + expect(result[0].value).to.eql('Initial commit\n') done() }) }) @@ -198,7 +200,8 @@ module.exports = (repo) => { it('value within nested node scope (commit/tree)', (done) => { resolver.get(commitCid, 'tree/somefile/mode', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql('100644') + expect(result.length).to.eq(2) + expect(result[1].value).to.eql('100644') done() }) }) @@ -206,7 +209,8 @@ module.exports = (repo) => { it('value within nested node scope (commit/tree/blob)', (done) => { resolver.get(commitCid, 'tree/somefile/hash', (err, result) => { expect(err).to.not.exist() - expect(blobNode.toString('hex')).to.eql(result.value.toString('hex')) + expect(result.length).to.eq(3) + expect(blobNode.toString('hex')).to.eql(result[2].value.toString('hex')) done() }) }) @@ -214,7 +218,8 @@ module.exports = (repo) => { it('value within nested node scope (commit/commit/tree/blob)', (done) => { resolver.get(commit2Cid, 'parents/0/tree/somefile/hash', (err, result) => { expect(err).to.not.exist() - expect(blobNode.toString('hex')).to.eql(result.value.toString('hex')) + expect(result.length).to.eq(4) + expect(blobNode.toString('hex')).to.eql(result[3].value.toString('hex')) done() }) }) @@ -222,7 +227,8 @@ module.exports = (repo) => { it('value within nested node scope (tag/commit/commit/tree/blob)', (done) => { resolver.get(tagCid, 'object/parents/0/tree/somefile/hash', (err, result) => { expect(err).to.not.exist() - expect(blobNode.toString('hex')).to.eql(result.value.toString('hex')) + expect(result.length).to.eq(5) + expect(blobNode.toString('hex')).to.eql(result[4].value.toString('hex')) done() }) }) @@ -232,7 +238,7 @@ module.exports = (repo) => { expect(err).to.not.exist() resolver.get(blobCid, (err, result) => { expect(err).to.not.exist() - const node = result.value + const node = result[0].value expect(blobNode.toString('hex')).to.eql(node.toString('hex')) remove() }) diff --git a/test/ipld-zcash.js b/test/ipld-zcash.js index d33c9d5..b3f1e09 100644 --- a/test/ipld-zcash.js +++ b/test/ipld-zcash.js @@ -114,7 +114,8 @@ module.exports = (repo) => { expect(err).to.not.exist() resolver.get(cid1, (err, result) => { expect(err).to.not.exist() - expect(node1.header.version).to.eql(result.value.header.version) + expect(result.length).to.eq(1) + expect(node1.header.version).to.eql(result[0].value.header.version) done() }) }) @@ -129,8 +130,9 @@ module.exports = (repo) => { it('root path (same as get)', (done) => { resolver.get(cid1, '/', (err, result) => { expect(err).to.not.exist() + expect(result.length).to.eq(1) - ipldZcash.util.cid(result.value, (err, cid) => { + ipldZcash.util.cid(result[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() @@ -141,7 +143,8 @@ module.exports = (repo) => { it('value within 1st node scope', (done) => { resolver.get(cid1, 'version', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(1) + expect(result.length).to.eq(1) + expect(result[0].value).to.eql(1) done() }) }) @@ -149,7 +152,8 @@ module.exports = (repo) => { it('value within nested scope (1 level)', (done) => { resolver.get(cid2, 'parent/version', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(1) + expect(result.length).to.eq(2) + expect(result[1].value).to.eql(1) done() }) }) @@ -157,7 +161,8 @@ module.exports = (repo) => { it('value within nested scope (2 levels)', (done) => { resolver.get(cid3, 'parent/parent/version', (err, result) => { expect(err).to.not.exist() - expect(result.value).to.eql(1) + expect(result.length).to.eq(3) + expect(result[2].value).to.eql(1) done() }) }) @@ -167,7 +172,7 @@ module.exports = (repo) => { expect(err).to.not.exist() resolver.get(cid1, (err, result) => { expect(err).to.not.exist() - expect(result.value.header.version).to.eql(1) + expect(result[0].value.header.version).to.eql(1) remove() }) }) From 94d51be57280966a797922351a01c452a4b369da Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Wed, 25 Apr 2018 10:07:27 -0700 Subject: [PATCH 2/9] Refactor conditional logic --- src/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 7145b07..74e80e0 100644 --- a/src/index.js +++ b/src/index.js @@ -130,11 +130,11 @@ class IPLDResolver { pull(this.getPullStream(cid, path, options), pull.reduce((arr, item) => { - if (!options.onlyNode) { - arr.push(item) - } else { - // replace existing entry + if (options.onlyNode) { arr[0] = item + } else { + // reducing to the last item + arr.push(item) } return arr }, [], callback) From d570ab982daed9b4789a658d4980c345e7618b31 Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Wed, 25 Apr 2018 10:42:46 -0700 Subject: [PATCH 3/9] Prefix unused variable with _ --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 74e80e0..e3e76a5 100644 --- a/src/index.js +++ b/src/index.js @@ -155,7 +155,7 @@ class IPLDResolver { let stop = false if (path === '' || !path) { - return function read (abort, cb) { + return function read (_abort, cb) { if (stop) return cb(stop) this._get(cid, (err, node) => { if (err) { From c5c05cdba2e885af8dcab3d72b0a290a80211378 Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Thu, 26 Apr 2018 17:48:35 -0700 Subject: [PATCH 4/9] Explicitly check the path of results --- test/ipld-all.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/ipld-all.js b/test/ipld-all.js index 8d8c22e..56bf50a 100644 --- a/test/ipld-all.js +++ b/test/ipld-all.js @@ -63,8 +63,10 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { resolver.get(cidCbor, 'pb/Data', (err, result) => { expect(err).to.not.exist() expect(result.length).to.eq(2) + dagCBOR.util.cid(result[0].value['/'], (cid) => expect(cid).to.eql(cidPb)) expect(result[1].value).to.eql(Buffer.from('I am inside a Protobuf')) done() }) }) + }) From 1dce3d5a263cd87d385d2594342c8dff18f41523 Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Thu, 26 Apr 2018 17:59:12 -0700 Subject: [PATCH 5/9] Add test for only node ipld get option --- test/ipld-all.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/ipld-all.js b/test/ipld-all.js index 56bf50a..8a7cecf 100644 --- a/test/ipld-all.js +++ b/test/ipld-all.js @@ -69,4 +69,13 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { }) }) + it('resolve honors onlyNode option', (done) => { + resolver.get(cidCbor, 'pb/Data', { onlyNode: true }, (err, result) => { + expect(err).to.not.exist() + expect(result.length).to.eq(1) + expect(result[0].value).to.eql(Buffer.from('I am inside a Protobuf')) + done() + }) + }) + }) From 54beacb152ff2207a152cb5d579859221b83b564 Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Thu, 26 Apr 2018 19:55:39 -0700 Subject: [PATCH 6/9] Add explicit test for resolver.getPullStream --- src/index.js | 2 +- test/ipld-all.js | 13 ++++++++++++- test/ipld-dag-cbor.js | 14 -------------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/index.js b/src/index.js index e3e76a5..7ee3db0 100644 --- a/src/index.js +++ b/src/index.js @@ -189,7 +189,7 @@ class IPLDResolver { const endReached = !path || path === '' || path === '/' const isTerminal = value && !value['/'] - if ((endReached && isTerminal) || options.localResolve) { + if ((endReached && isTerminal) || (options && options.localResolve)) { stop = true return cb(null, { value, remainderPath: path }) } diff --git a/test/ipld-all.js b/test/ipld-all.js index 8a7cecf..0b043e5 100644 --- a/test/ipld-all.js +++ b/test/ipld-all.js @@ -15,6 +15,7 @@ const dagPB = require('ipld-dag-pb') const dagCBOR = require('ipld-dag-cbor') const each = require('async/each') const waterfall = require('async/waterfall') +const pull = require('pull-stream') const IPLDResolver = require('../src') @@ -69,6 +70,17 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { }) }) + it('resolver.getPullStream through different formats', (done) => { + pull(resolver.getPullStream(cidCbor, 'pb/Data'), + pull.collect((err, results) => { + expect(err).to.not.exist() + expect(results.length).to.eq(2) + dagCBOR.util.cid(results[0].value['/'], (cid) => expect(cid).to.eql(cidPb)) + expect(results[1].value).to.eql(Buffer.from('I am inside a Protobuf')) + done() + })) + }) + it('resolve honors onlyNode option', (done) => { resolver.get(cidCbor, 'pb/Data', { onlyNode: true }, (err, result) => { expect(err).to.not.exist() @@ -77,5 +89,4 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { done() }) }) - }) diff --git a/test/ipld-dag-cbor.js b/test/ipld-dag-cbor.js index 19ab1b5..88681dd 100644 --- a/test/ipld-dag-cbor.js +++ b/test/ipld-dag-cbor.js @@ -114,20 +114,6 @@ module.exports = (repo) => { }, done) }) - it('resolver.getPullStream just CID', (done) => { - pull(resolver.getPullStream(cid1, null), - pull.collect((err, results) => { - expect(err).to.not.exist() - expect(results.length).to.eq(1) - - dagCBOR.util.cid(results[0].value, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.eql(cid1) - }) - })) - done() - }) - it('resolver.get just CID', (done) => { resolver.get(cid1, (err, results) => { expect(results.length).to.eq(1) From 21ccc0fad8063a3e270bafe75ca2a20cf75b0789 Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Fri, 27 Apr 2018 16:51:29 -0700 Subject: [PATCH 7/9] Fix new tests to check for right value --- test/ipld-all.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/ipld-all.js b/test/ipld-all.js index 0b043e5..6c2a6e1 100644 --- a/test/ipld-all.js +++ b/test/ipld-all.js @@ -61,11 +61,11 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { }) it('resolve through different formats', (done) => { - resolver.get(cidCbor, 'pb/Data', (err, result) => { + resolver.get(cidCbor, 'pb/Data', (err, results) => { expect(err).to.not.exist() - expect(result.length).to.eq(2) - dagCBOR.util.cid(result[0].value['/'], (cid) => expect(cid).to.eql(cidPb)) - expect(result[1].value).to.eql(Buffer.from('I am inside a Protobuf')) + expect(results.length).to.eq(2) + expect(results[0].value['/']).to.eql(cidPb.multihash) + expect(results[1].value).to.eql(Buffer.from('I am inside a Protobuf')) done() }) }) @@ -75,17 +75,17 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { pull.collect((err, results) => { expect(err).to.not.exist() expect(results.length).to.eq(2) - dagCBOR.util.cid(results[0].value['/'], (cid) => expect(cid).to.eql(cidPb)) + expect(results[0].value['/']).to.eql(cidPb.multihash) expect(results[1].value).to.eql(Buffer.from('I am inside a Protobuf')) done() })) }) it('resolve honors onlyNode option', (done) => { - resolver.get(cidCbor, 'pb/Data', { onlyNode: true }, (err, result) => { + resolver.get(cidCbor, 'pb/Data', { onlyNode: true }, (err, results) => { expect(err).to.not.exist() - expect(result.length).to.eq(1) - expect(result[0].value).to.eql(Buffer.from('I am inside a Protobuf')) + expect(results.length).to.eq(1) + expect(results[0].value).to.eql(Buffer.from('I am inside a Protobuf')) done() }) }) From a76efbd4e7a50443f57b63ae7dd9bf83bda1d6ed Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Sun, 6 May 2018 16:58:40 -0700 Subject: [PATCH 8/9] Make naming consistent --- test/ipld-dag-cbor.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/ipld-dag-cbor.js b/test/ipld-dag-cbor.js index 88681dd..0ab9be3 100644 --- a/test/ipld-dag-cbor.js +++ b/test/ipld-dag-cbor.js @@ -115,11 +115,11 @@ module.exports = (repo) => { }) it('resolver.get just CID', (done) => { - resolver.get(cid1, (err, results) => { - expect(results.length).to.eq(1) + resolver.get(cid1, (err, result) => { + expect(result.length).to.eq(1) expect(err).to.not.exist() - dagCBOR.util.cid(results[0].value, (err, cid) => { + dagCBOR.util.cid(result[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() @@ -128,11 +128,11 @@ module.exports = (repo) => { }) it('resolver.get root path', (done) => { - resolver.get(cid1, '/', (err, results) => { - expect(results.length).to.eq(1) + resolver.get(cid1, '/', (err, result) => { + expect(result.length).to.eq(1) expect(err).to.not.exist() - dagCBOR.util.cid(results[0].value, (err, cid) => { + dagCBOR.util.cid(result[0].value, (err, cid) => { expect(err).to.not.exist() expect(cid).to.eql(cid1) done() From ed6cd411e621df3f8be2ec912526e89080ed0398 Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Sun, 6 May 2018 16:59:40 -0700 Subject: [PATCH 9/9] Fix comment to be on the correct line --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 7ee3db0..d1f58db 100644 --- a/src/index.js +++ b/src/index.js @@ -131,9 +131,9 @@ class IPLDResolver { pull(this.getPullStream(cid, path, options), pull.reduce((arr, item) => { if (options.onlyNode) { + // reducing to the last item arr[0] = item } else { - // reducing to the last item arr.push(item) } return arr