Skip to content

Commit

Permalink
fix: append remainder
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias authored and lidel committed May 6, 2019
1 parent 2091b89 commit ccff48b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"execa": "^1.0.0",
"form-data": "^2.3.3",
"hat": "0.0.3",
"interface-ipfs-core": "~0.99.1",
"interface-ipfs-core": "ipfs/interface-js-ipfs-core#feat/name-resolve-dns",
"ipfsd-ctl": "~0.42.0",
"libp2p-websocket-star": "~0.10.2",
"ncp": "^2.0.0",
Expand Down
6 changes: 1 addition & 5 deletions src/cli/commands/name/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ module.exports = {
const ipfs = await argv.getIpfs()
const result = await ipfs.name.resolve(argv.name, opts)

if (result && result.path) {
print(result.path)
} else {
print(result)
}
print(result)
})())
}
}
39 changes: 26 additions & 13 deletions src/core/components/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ const keyLookup = (ipfsNode, kname, callback) => {
})
}

const appendRemainder = (cb, remainder) => {
return (err, result) => {
if (err) {
return cb(err)
}
if (remainder.length) {
return cb(null, result + '/' + remainder.join('/'))
}
return cb(null, result)
}
}

/**
* @typedef { import("../index") } IPFS
*/
Expand All @@ -46,7 +58,7 @@ const keyLookup = (ipfsNode, kname, callback) => {
* IPNS - Inter-Planetary Naming System
*
* @param {IPFS} self
* @returns {Function}
* @returns {Object}
*/
module.exports = function name (self) {
return {
Expand Down Expand Up @@ -162,27 +174,28 @@ module.exports = function name (self) {
name = `/ipns/${name}`
}

const [ , hash ] = name.slice(1).split('/')
const [ namespace, hash, ...remainder ] = name.slice(1).split('/')
try {
mh.fromB58String(hash)

// ipns resolve needs a online daemon
if (!self.isOnline() && !offline) {
const errMsg = utils.OFFLINE_ERROR

log.error(errMsg)
return callback(errcode(errMsg, 'OFFLINE_ERROR'))
}
self._ipns.resolve(name, options, callback)
} catch (err) {
// lets check if we have a domain ex. /ipns/ipfs.io and resolve with dns
if (isDomain(hash)) {
return self.dns(hash, options, callback)
return self.dns(hash, options, appendRemainder(callback, remainder))
}

log.error(err)
callback(errcode(new Error('Invalid IPNS name.'), 'ERR_IPNS_INVALID_NAME'))
return callback(errcode(new Error('Invalid IPNS name.'), 'ERR_IPNS_INVALID_NAME'))
}

// multihash is valid lets resolve with IPNS
// IPNS resolve needs a online daemon
if (!self.isOnline() && !offline) {
const errMsg = utils.OFFLINE_ERROR

log.error(errMsg)
return callback(errcode(errMsg, 'OFFLINE_ERROR'))
}
self._ipns.resolve(`/${namespace}/${hash}`, options, appendRemainder(callback, remainder))
}),
pubsub: namePubsub(self)
}
Expand Down

0 comments on commit ccff48b

Please sign in to comment.