From 167e5f1499a8dbba62786c6cabc5a4e397bb9686 Mon Sep 17 00:00:00 2001 From: Nitin Patel Date: Tue, 2 Apr 2019 02:50:26 +0530 Subject: [PATCH 1/2] ipfs resolve --- src/core/components/resolve.js | 48 ++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/core/components/resolve.js b/src/core/components/resolve.js index 1ca80e8249..7c94731805 100644 --- a/src/core/components/resolve.js +++ b/src/core/components/resolve.js @@ -20,29 +20,43 @@ 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'))) - } + resolve(name, opts, cb) + }) - const split = name.split('/') // ['', 'ipfs', 'hash', ...path] - const cid = new CID(split[2]) + function resolve (name, opts, cb) { + if (isIpfs.ipfsPath(name)) { + const split = name.split('/') // ['', 'ipfs', 'hash', ...path] + const cid = new CID(split[2]) - if (split.length === 3) { - return setImmediate(() => cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}`)) - } + if (split.length === 3) { + return setImmediate(() => cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}`)) + } - const path = split.slice(3).join('/') + 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 : ''}`) - }) - }) + resolveCID(cid, path, (err, res) => { + if (err) return cb(err) + const { cid, remainderPath } = res + cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`) + }) + } else { + const domain = name.split('/')[2] + const path = name.split('/').slice(3).join('/') + console.log(domain, path) + return self.dns(domain, (err, result) => { + if (err) return cb(err) + const cid = new CID(result.split('/')[2]) + resolveCID(cid, path, (err, res) => { + if (err) return cb(err) + 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) { + function resolveCID (cid, path, callback) { let value, remainderPath doUntil( (cb) => { From d61947d4dfcbb96287b93feb603f92801af09dba Mon Sep 17 00:00:00 2001 From: Nitin Patel Date: Tue, 2 Apr 2019 04:14:54 +0530 Subject: [PATCH 2/2] another commit --- src/core/components/resolve.js | 61 +++++++++++++++------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/src/core/components/resolve.js b/src/core/components/resolve.js index 7c94731805..fe43fdb0c5 100644 --- a/src/core/components/resolve.js +++ b/src/core/components/resolve.js @@ -20,43 +20,21 @@ module.exports = (self) => { return setImmediate(() => cb(new Error('invalid argument'))) } - resolve(name, opts, cb) - }) - - function resolve (name, opts, cb) { - if (isIpfs.ipfsPath(name)) { - const split = name.split('/') // ['', 'ipfs', 'hash', ...path] - const cid = new CID(split[2]) - - if (split.length === 3) { - return setImmediate(() => cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}`)) - } + const split = name.split('/') // ['', 'ipfs', 'hash or domain', ...path] + const hashOrDomain = split[2] + const path = split.slice(3).join('/') - const path = split.slice(3).join('/') - - resolveCID(cid, path, (err, res) => { - if (err) return cb(err) - const { cid, remainderPath } = res - cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`) - }) - } else { - const domain = name.split('/')[2] - const path = name.split('/').slice(3).join('/') - console.log(domain, path) - return self.dns(domain, (err, result) => { - if (err) return cb(err) - const cid = new CID(result.split('/')[2]) - resolveCID(cid, path, (err, res) => { - if (err) return cb(err) - const { cid, remainderPath } = res - cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`) - }) - }) + if (isIpfs.cid(hashOrDomain)) { + return resolveCID(hashOrDomain, path, opts, cb) } - } - // Resolve the given CID + path to a CID. - function resolveCID (cid, path, callback) { + // 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 (recursive). + function resolveCID (hash, path, opts, callback) { + let cid = new CID(hash) let value, remainderPath doUntil( (cb) => { @@ -96,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 : ''}`) + }) + } }