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

Commit d34747d

Browse files
committed
fix: add support for resolving to the middle of an IPLD block
see: ipfs-inactive/interface-js-ipfs-core#385
1 parent 748fccf commit d34747d

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/core/components/resolve.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,15 @@ module.exports = (self) => {
3434

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

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

4443
// Resolve the given CID + path to a CID.
4544
function resolve (cid, path, callback) {
4645
let value
47-
4846
doUntil(
4947
(cb) => {
5048
self.block.get(cid, (err, block) => {
@@ -65,22 +63,20 @@ module.exports = (self) => {
6563
})
6664
},
6765
() => {
68-
const endReached = !path || path === '/'
69-
70-
if (endReached) {
71-
return true
72-
}
73-
74-
if (value) {
66+
if (value && value['/']) {
67+
// If we've hit a CID, replace the current CID.
7568
cid = new CID(value['/'])
69+
} else {
70+
// We've hit a value. Return the current CID and the remaining path.
71+
return false
7672
}
7773

78-
return false
74+
// Continue resolving unless the path is empty.
75+
return !path || path === '/'
7976
},
8077
(err) => {
8178
if (err) return callback(err)
82-
if (value && value['/']) return callback(null, new CID(value['/']))
83-
callback()
79+
callback(null, cid, path)
8480
}
8581
)
8682
}

test/cli/resolve.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,21 @@ describe('resolve', () => runOnAndOff((thing) => {
7171
expect(out).to.contain(`/ipfs/${fileHash}`)
7272
})
7373
})
74+
75+
it('should resolve an IPFS path non-link', (done) => {
76+
const content = { path: { to: { file: 'foobar' } } }
77+
const options = { format: 'dag-cbor', hashAlg: 'sha2-256' }
78+
79+
ipfs.dag.put(content, options, (err, cid) => {
80+
expect(err).to.not.exist()
81+
82+
// FIXME: This should be /ipld/... but that's not supported yet.
83+
const path = `/ipfs/${cid.toBaseEncodedString()}/path/to/file`
84+
ipfs.resolve(path, (err, path) => {
85+
expect(err).to.not.exist()
86+
expect(path).to.equal(path)
87+
done()
88+
})
89+
})
90+
})
7491
}))

0 commit comments

Comments
 (0)