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

Commit

Permalink
fix: add support for resolving to the middle of an IPLD block
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Jan 23, 2019
1 parent 748fccf commit d34747d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/core/components/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@ module.exports = (self) => {

const path = split.slice(3).join('/')

resolve(cid, path, (err, cid) => {
resolve(cid, path, (err, cid, remainder) => {
if (err) return cb(err)
if (!cid) return cb(new Error('found non-link at given path'))
cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}`)
cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${!remainder ? '/' + remainder : ''}`)
})
})

// Resolve the given CID + path to a CID.
function resolve (cid, path, callback) {
let value

doUntil(
(cb) => {
self.block.get(cid, (err, block) => {
Expand All @@ -65,22 +63,20 @@ module.exports = (self) => {
})
},
() => {
const endReached = !path || path === '/'

if (endReached) {
return true
}

if (value) {
if (value && value['/']) {
// If we've hit a CID, replace the current CID.
cid = new CID(value['/'])
} else {
// We've hit a value. Return the current CID and the remaining path.
return false
}

return false
// Continue resolving unless the path is empty.
return !path || path === '/'
},
(err) => {
if (err) return callback(err)
if (value && value['/']) return callback(null, new CID(value['/']))
callback()
callback(null, cid, path)
}
)
}
Expand Down
17 changes: 17 additions & 0 deletions test/cli/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,21 @@ describe('resolve', () => runOnAndOff((thing) => {
expect(out).to.contain(`/ipfs/${fileHash}`)
})
})

it('should resolve an IPFS path non-link', (done) => {
const content = { path: { to: { file: 'foobar' } } }
const options = { format: 'dag-cbor', hashAlg: 'sha2-256' }

ipfs.dag.put(content, options, (err, cid) => {
expect(err).to.not.exist()

// FIXME: This should be /ipld/... but that's not supported yet.
const path = `/ipfs/${cid.toBaseEncodedString()}/path/to/file`
ipfs.resolve(path, (err, path) => {
expect(err).to.not.exist()
expect(path).to.equal(path)
done()
})
})
})
}))

0 comments on commit d34747d

Please sign in to comment.