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 (#1841)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Stebalien authored and Alan Shaw committed Feb 19, 2019
1 parent 16ea346 commit fc08243
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
},
karma: {
files: [{
pattern: 'node_modules/interface-ipfs-core/js/test/fixtures/**/*',
pattern: 'node_modules/interface-ipfs-core/test/fixtures/**/*',
watched: false,
served: true,
included: false
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"execa": "^1.0.0",
"form-data": "^2.3.3",
"hat": "0.0.3",
"interface-ipfs-core": "~0.96.0",
"interface-ipfs-core": "~0.97.0",
"ipfsd-ctl": "~0.41.0",
"libp2p-websocket-star": "~0.10.2",
"ncp": "^2.0.0",
Expand Down
34 changes: 18 additions & 16 deletions src/core/components/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ module.exports = (self) => {

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

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

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

let value, remainderPath
doUntil(
(cb) => {
self.block.get(cid, (err, block) => {
Expand All @@ -59,28 +58,31 @@ module.exports = (self) => {
r.resolver.resolve(block.data, path, (err, result) => {
if (err) return cb(err)
value = result.value
path = result.remainderPath
remainderPath = result.remainderPath
cb()
})
})
},
() => {
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['/'])
path = remainderPath
} else if (CID.isCID(value)) {
// If we've hit a CID, replace the current CID.
cid = value
path = remainderPath
} else {
// We've hit a value. Return the current CID and the remaining path.
return true
}

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, remainderPath: path })
}
)
}
Expand Down
2 changes: 1 addition & 1 deletion test/gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const path = require('path')
const hat = require('hat')
const fileType = require('file-type')

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

0 comments on commit fc08243

Please sign in to comment.