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

Commit

Permalink
ipfs resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
niinpatel committed Apr 9, 2019
1 parent d5a1b89 commit 167e5f1
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/core/components/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 167e5f1

Please sign in to comment.