diff --git a/src/core/components/resolve.js b/src/core/components/resolve.js index 1ca80e8249..fe43fdb0c5 100644 --- a/src/core/components/resolve.js +++ b/src/core/components/resolve.js @@ -20,29 +20,21 @@ module.exports = (self) => { return setImmediate(() => cb(new Error('invalid argument'))) } - // TODO remove this and update subsequent code when IPNS is implemented - if (!isIpfs.ipfsPath(name)) { - return setImmediate(() => cb(new Error('resolve non-IPFS names is not implemented'))) - } - - const split = name.split('/') // ['', 'ipfs', 'hash', ...path] - const cid = new CID(split[2]) + const split = name.split('/') // ['', 'ipfs', 'hash or domain', ...path] + const hashOrDomain = split[2] + const path = split.slice(3).join('/') - if (split.length === 3) { - return setImmediate(() => cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}`)) + if (isIpfs.cid(hashOrDomain)) { + return resolveCID(hashOrDomain, path, opts, cb) } - const path = split.slice(3).join('/') - - resolve(cid, path, (err, res) => { - if (err) return cb(err) - const { cid, remainderPath } = res - cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`) - }) + // if its not a cid then its probably a domain name to resolve + return resolveDomain(hashOrDomain, path, opts, cb) }) - // Resolve the given CID + path to a CID. - function resolve (cid, path, callback) { + // Resolve the given CID + path to a CID (recursive). + function resolveCID (hash, path, opts, callback) { + let cid = new CID(hash) let value, remainderPath doUntil( (cb) => { @@ -82,8 +74,21 @@ module.exports = (self) => { }, (err) => { if (err) return callback(err) - callback(null, { cid, remainderPath: path }) + callback(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`) } ) } + + function resolveDomain (domain, path, opts, callback) { + const recursive = opts.recursive && opts.recursive.toString() === 'true' + return self.dns(domain, (err, result) => { + if (err) return callback(err) + const hash = result.split('/')[2] + const remainderPath = path + if (recursive) { + return resolveCID(hash, remainderPath, opts, callback) + } + callback(null, `/ipfs/${cidToString(hash, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`) + }) + } }