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

fix: add support for resolving to the middle of an IPLD block #1841

Merged
merged 3 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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