Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: return nested value from dag.get (#3966)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain authored Dec 6, 2021
1 parent 9018432 commit 45ac973
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/interface-ipfs-core/src/block/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
Expand Down
22 changes: 22 additions & 0 deletions packages/interface-ipfs-core/src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
}
12 changes: 5 additions & 7 deletions packages/ipfs-core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -248,4 +241,9 @@ export const resolve = async function * (cid, path, codecs, repo, options) {
value = await load(value)
}
}

yield {
value,
remainderPath: ''
}
}
12 changes: 5 additions & 7 deletions packages/ipfs-http-client/src/lib/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -62,4 +55,9 @@ export async function * resolve (cid, path, codecs, getBlock, options) {
value = await load(value)
}
}

yield {
value,
remainderPath: ''
}
}

0 comments on commit 45ac973

Please sign in to comment.