From 45ac9730d6484e8324acfbc3579fce052b8452d7 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Mon, 6 Dec 2021 10:32:25 +0000 Subject: [PATCH] fix: return nested value from dag.get (#3966) If we're traversing thorough and object and the thing at the end of the path is a CID, load that thing and return it as the final value, unless `localResolve` is true, in which case return the CID. Fixes #3957 --- packages/interface-ipfs-core/src/block/rm.js | 4 ++-- packages/interface-ipfs-core/src/dag/get.js | 22 ++++++++++++++++++++ packages/ipfs-core/src/utils.js | 12 +++++------ packages/ipfs-http-client/src/lib/resolve.js | 12 +++++------ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/packages/interface-ipfs-core/src/block/rm.js b/packages/interface-ipfs-core/src/block/rm.js index e7248c7c11..15458d1bcd 100644 --- a/packages/interface-ipfs-core/src/block/rm.js +++ b/packages/interface-ipfs-core/src/block/rm.js @@ -78,8 +78,8 @@ export function testRm (factory, options) { expect(result).to.have.lengthOf(3) - result.forEach((res, index) => { - expect(res.cid.toString()).to.equal(cids[index].toString()) + result.forEach((res) => { + expect(cids.map(cid => cid.toString())).to.include(res.cid.toString()) expect(res).to.not.have.property('error') }) }) diff --git a/packages/interface-ipfs-core/src/dag/get.js b/packages/interface-ipfs-core/src/dag/get.js index 3f81031df2..be3b5211c0 100644 --- a/packages/interface-ipfs-core/src/dag/get.js +++ b/packages/interface-ipfs-core/src/dag/get.js @@ -283,5 +283,27 @@ export function testGet (factory, options) { return expect(ipfs.dag.get(uint8ArrayFromString('INVALID CID'))) .to.eventually.be.rejected() }) + + it('should return nested content when getting a CID with a path', async () => { + const regularContent = { test: '123' } + const cid1 = await ipfs.dag.put(regularContent) + const linkedContent = { link: cid1 } + const cid2 = await ipfs.dag.put(linkedContent) + + const atPath = await ipfs.dag.get(cid2, { path: '/link' }) + + expect(atPath).to.have.deep.property('value', regularContent) + }) + + it('should not return nested content when getting a CID with a path and localResolve is true', async () => { + const regularContent = { test: '123' } + const cid1 = await ipfs.dag.put(regularContent) + const linkedContent = { link: cid1 } + const cid2 = await ipfs.dag.put(linkedContent) + + const atPath = await ipfs.dag.get(cid2, { path: '/link', localResolve: true }) + + expect(atPath).to.have.deep.property('value').that.is.an.instanceOf(CID) + }) }) } diff --git a/packages/ipfs-core/src/utils.js b/packages/ipfs-core/src/utils.js index 6336d3681b..719386279d 100644 --- a/packages/ipfs-core/src/utils.js +++ b/packages/ipfs-core/src/utils.js @@ -200,13 +200,6 @@ export const resolve = async function * (cid, path, codecs, repo, options) { let value = await load(cid) let lastCid = cid - if (!parts.length) { - yield { - value, - remainderPath: '' - } - } - // End iteration if there isn't a CID to follow any more while (parts.length) { const key = parts.shift() @@ -248,4 +241,9 @@ export const resolve = async function * (cid, path, codecs, repo, options) { value = await load(value) } } + + yield { + value, + remainderPath: '' + } } diff --git a/packages/ipfs-http-client/src/lib/resolve.js b/packages/ipfs-http-client/src/lib/resolve.js index 33dd67d5e0..b0a1e32058 100644 --- a/packages/ipfs-http-client/src/lib/resolve.js +++ b/packages/ipfs-http-client/src/lib/resolve.js @@ -29,13 +29,6 @@ export async function * resolve (cid, path, codecs, getBlock, options) { let value = await load(cid) let lastCid = cid - if (!parts.length) { - yield { - value, - remainderPath: '' - } - } - // End iteration if there isn't a CID to follow any more while (parts.length) { const key = parts.shift() @@ -62,4 +55,9 @@ export async function * resolve (cid, path, codecs, getBlock, options) { value = await load(value) } } + + yield { + value, + remainderPath: '' + } }