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

Commit fc08243

Browse files
StebalienAlan Shaw
authored and
Alan Shaw
committed
fix: add support for resolving to the middle of an IPLD block (#1841)
refs ipfs-inactive/interface-js-ipfs-core#385 resolves #1763 BREAKING CHANGE: `ipfs.resolve` now supports resolving to the middle of an IPLD block instead of erroring. Given: ```js b = {"c": "some value"} a = {"b": {"/": cidOf(b) }} ``` `ipfs resolve /ipld/cidOf(a)/b/c` should return `/ipld/cidOf(b)/c`. That is, it resolves the path as much as it can. Previously it would simply fail with an error. License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 16ea346 commit fc08243

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

.aegir.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
},
1616
karma: {
1717
files: [{
18-
pattern: 'node_modules/interface-ipfs-core/js/test/fixtures/**/*',
18+
pattern: 'node_modules/interface-ipfs-core/test/fixtures/**/*',
1919
watched: false,
2020
served: true,
2121
included: false

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"execa": "^1.0.0",
6969
"form-data": "^2.3.3",
7070
"hat": "0.0.3",
71-
"interface-ipfs-core": "~0.96.0",
71+
"interface-ipfs-core": "~0.97.0",
7272
"ipfsd-ctl": "~0.41.0",
7373
"libp2p-websocket-star": "~0.10.2",
7474
"ncp": "^2.0.0",

src/core/components/resolve.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,16 @@ module.exports = (self) => {
3434

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

37-
resolve(cid, path, (err, cid) => {
37+
resolve(cid, path, (err, res) => {
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+
const { cid, remainderPath } = res
40+
cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`)
4141
})
4242
})
4343

4444
// Resolve the given CID + path to a CID.
4545
function resolve (cid, path, callback) {
46-
let value
47-
46+
let value, remainderPath
4847
doUntil(
4948
(cb) => {
5049
self.block.get(cid, (err, block) => {
@@ -59,28 +58,31 @@ module.exports = (self) => {
5958
r.resolver.resolve(block.data, path, (err, result) => {
6059
if (err) return cb(err)
6160
value = result.value
62-
path = result.remainderPath
61+
remainderPath = result.remainderPath
6362
cb()
6463
})
6564
})
6665
},
6766
() => {
68-
const endReached = !path || path === '/'
69-
70-
if (endReached) {
71-
return true
72-
}
73-
74-
if (value) {
67+
if (value && value['/']) {
68+
// If we've hit a CID, replace the current CID.
7569
cid = new CID(value['/'])
70+
path = remainderPath
71+
} else if (CID.isCID(value)) {
72+
// If we've hit a CID, replace the current CID.
73+
cid = value
74+
path = remainderPath
75+
} else {
76+
// We've hit a value. Return the current CID and the remaining path.
77+
return true
7678
}
7779

78-
return false
80+
// Continue resolving unless the path is empty.
81+
return !path || path === '/'
7982
},
8083
(err) => {
8184
if (err) return callback(err)
82-
if (value && value['/']) return callback(null, new CID(value['/']))
83-
callback()
85+
callback(null, { cid, remainderPath: path })
8486
}
8587
)
8688
}

test/gateway/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const path = require('path')
1212
const hat = require('hat')
1313
const fileType = require('file-type')
1414

15-
const bigFile = loadFixture('js/test/fixtures/15mb.random', 'interface-ipfs-core')
15+
const bigFile = loadFixture('test/fixtures/15mb.random', 'interface-ipfs-core')
1616
const directoryContent = {
1717
'index.html': loadFixture('test/gateway/test-folder/index.html'),
1818
'nested-folder/hello.txt': loadFixture('test/gateway/test-folder/nested-folder/hello.txt'),

0 commit comments

Comments
 (0)