From a5d76403e27102e4f6a16f9f553718f19c778e7e Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 19 Jul 2018 14:49:36 +0100 Subject: [PATCH 1/4] fix: add support for resolving links by name --- src/resolver.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/resolver.js b/src/resolver.js index a07986d..a4f4406 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -57,6 +57,8 @@ exports.resolve = (binaryBlob, path, callback) => { values[link.name] = link.multihash }) + console.log(values) + let value = values[split[1]] // if remainderPath exists, value needs to be CID @@ -74,7 +76,33 @@ exports.resolve = (binaryBlob, path, callback) => { } else if (split[0] === 'Data') { cb(null, { value: node.data, remainderPath: '' }) } else { - cb(new Error('path not available')) + const values = {} + + // populate both index number and name to enable both cases + // for the resolver + node.links.forEach((l, i) => { + const link = l.toJSON() + values[i] = { + hash: link.multihash, + name: link.name, + size: link.size + } + // TODO by enabling something to resolve through link name, we are + // applying a transformation (a view) to the data, confirm if this + // is exactly what we want + values[link.name] = link.multihash + }) + + const val = values[split[0]] + + if (val) { + return cb(null, { + value: { '/': val.hash }, + remainderPath: split.slice(3).join('/') + }) + } else { + cb(new Error('path not available')) + } } } ], callback) From ed9af60372b1bcafd3185cf7aa04f82d9fce2b65 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 19 Jul 2018 22:28:53 +0100 Subject: [PATCH 2/4] fix: resolver links by name License: MIT Signed-off-by: Alan Shaw --- .gitignore | 1 + src/resolver.js | 32 ++++++++++++++------------------ test/resolver.spec.js | 9 +++++++++ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 6292faf..eca58f9 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ lib-cov # Coverage directory used by tools like istanbul coverage +.nyc_output # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt diff --git a/src/resolver.js b/src/resolver.js index a4f4406..18a6356 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -46,19 +46,16 @@ exports.resolve = (binaryBlob, path, callback) => { // for the resolver node.links.forEach((l, i) => { const link = l.toJSON() - values[i] = { + // TODO by enabling something to resolve through link name, we are + // applying a transformation (a view) to the data, confirm if this + // is exactly what we want + values[i] = values[link.name] = { hash: link.multihash, name: link.name, size: link.size } - // TODO by enabling something to resolve through link name, we are - // applying a transformation (a view) to the data, confirm if this - // is exactly what we want - values[link.name] = link.multihash }) - console.log(values) - let value = values[split[1]] // if remainderPath exists, value needs to be CID @@ -82,27 +79,26 @@ exports.resolve = (binaryBlob, path, callback) => { // for the resolver node.links.forEach((l, i) => { const link = l.toJSON() - values[i] = { + // TODO by enabling something to resolve through link name, we are + // applying a transformation (a view) to the data, confirm if this + // is exactly what we want + values[i] = values[link.name] = { hash: link.multihash, name: link.name, size: link.size } - // TODO by enabling something to resolve through link name, we are - // applying a transformation (a view) to the data, confirm if this - // is exactly what we want - values[link.name] = link.multihash }) - const val = values[split[0]] + const value = values[split[0]] - if (val) { + if (value) { return cb(null, { - value: { '/': val.hash }, - remainderPath: split.slice(3).join('/') + value: { '/': value.hash }, + remainderPath: split.slice(1).join('/') }) - } else { - cb(new Error('path not available')) } + + cb(new Error('path not available')) } } ], callback) diff --git a/test/resolver.spec.js b/test/resolver.spec.js index 6329a15..0c4307c 100644 --- a/test/resolver.spec.js +++ b/test/resolver.spec.js @@ -146,6 +146,15 @@ describe('IPLD Format resolver (local)', () => { }) }) + it('links by name', (done) => { + resolver.resolve(linksNodeBlob, 'named link', (err, result) => { + expect(err).to.not.exist() + expect(result.value['/']).to.eql(links[1].multihash) + expect(result.remainderPath).to.eql('') + done() + }) + }) + it('yield remainderPath if impossible to resolve through (a)', (done) => { resolver.resolve(linksNodeBlob, 'Links/1/Hash/Data', (err, result) => { expect(err).to.not.exist() From f1816c9e18f1d751a1659501e1b2c36e73d95072 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 20 Jul 2018 10:52:04 +0100 Subject: [PATCH 3/4] test: add tests for missing link and named link remainderPath License: MIT Signed-off-by: Alan Shaw --- test/resolver.spec.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/resolver.spec.js b/test/resolver.spec.js index 0c4307c..a20133c 100644 --- a/test/resolver.spec.js +++ b/test/resolver.spec.js @@ -155,6 +155,14 @@ describe('IPLD Format resolver (local)', () => { }) }) + it('missing link by name', (done) => { + resolver.resolve(linksNodeBlob, 'missing link', (err, result) => { + expect(err).to.exist() + expect(err.message).to.equal('path not available') + done() + }) + }) + it('yield remainderPath if impossible to resolve through (a)', (done) => { resolver.resolve(linksNodeBlob, 'Links/1/Hash/Data', (err, result) => { expect(err).to.not.exist() @@ -175,6 +183,27 @@ describe('IPLD Format resolver (local)', () => { done() }) }) + + it('yield remainderPath if impossible to resolve through named link (a)', (done) => { + resolver.resolve(linksNodeBlob, 'named link/Data', (err, result) => { + expect(err).to.not.exist() + expect(result.value['/']).to.exist() + expect(result.value['/']).to.equal('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V') + expect(result.remainderPath).to.equal('Data') + done() + }) + }) + + it('yield remainderPath if impossible to resolve through named link (b)', (done) => { + resolver.resolve(linksNodeBlob, 'named link/Links/0/Hash/Data', (err, result) => { + expect(err).to.not.exist() + expect(result.value['/']) + .to.equal('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V') + + expect(result.remainderPath).to.equal('Links/0/Hash/Data') + done() + }) + }) }) it('resolver.tree', (done) => { From bfd288604607c5f8d0c8734e3008f7cf043e4a80 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 20 Jul 2018 10:52:30 +0100 Subject: [PATCH 4/4] fix: tweaks from PR review License: MIT Signed-off-by: Alan Shaw --- src/resolver.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/resolver.js b/src/resolver.js index 18a6356..a2b964b 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -46,9 +46,6 @@ exports.resolve = (binaryBlob, path, callback) => { // for the resolver node.links.forEach((l, i) => { const link = l.toJSON() - // TODO by enabling something to resolve through link name, we are - // applying a transformation (a view) to the data, confirm if this - // is exactly what we want values[i] = values[link.name] = { hash: link.multihash, name: link.name, @@ -73,16 +70,15 @@ exports.resolve = (binaryBlob, path, callback) => { } else if (split[0] === 'Data') { cb(null, { value: node.data, remainderPath: '' }) } else { + // If split[0] is not 'Data' or 'Links' then we might be trying to refer + // to a named link from the Links array. This is because go-ipfs and + // js-ipfs have historically supported the ability to do + // `ipfs dag get CID/a` where a is a named link in a dag-pb. const values = {} - // populate both index number and name to enable both cases - // for the resolver node.links.forEach((l, i) => { const link = l.toJSON() - // TODO by enabling something to resolve through link name, we are - // applying a transformation (a view) to the data, confirm if this - // is exactly what we want - values[i] = values[link.name] = { + values[link.name] = { hash: link.multihash, name: link.name, size: link.size