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

Commit 167e5f1

Browse files
committed
ipfs resolve
1 parent d5a1b89 commit 167e5f1

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/core/components/resolve.js

+31-17
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,43 @@ module.exports = (self) => {
2020
return setImmediate(() => cb(new Error('invalid argument')))
2121
}
2222

23-
// TODO remove this and update subsequent code when IPNS is implemented
24-
if (!isIpfs.ipfsPath(name)) {
25-
return setImmediate(() => cb(new Error('resolve non-IPFS names is not implemented')))
26-
}
23+
resolve(name, opts, cb)
24+
})
2725

28-
const split = name.split('/') // ['', 'ipfs', 'hash', ...path]
29-
const cid = new CID(split[2])
26+
function resolve (name, opts, cb) {
27+
if (isIpfs.ipfsPath(name)) {
28+
const split = name.split('/') // ['', 'ipfs', 'hash', ...path]
29+
const cid = new CID(split[2])
3030

31-
if (split.length === 3) {
32-
return setImmediate(() => cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}`))
33-
}
31+
if (split.length === 3) {
32+
return setImmediate(() => cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}`))
33+
}
3434

35-
const path = split.slice(3).join('/')
35+
const path = split.slice(3).join('/')
3636

37-
resolve(cid, path, (err, res) => {
38-
if (err) return cb(err)
39-
const { cid, remainderPath } = res
40-
cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`)
41-
})
42-
})
37+
resolveCID(cid, path, (err, res) => {
38+
if (err) return cb(err)
39+
const { cid, remainderPath } = res
40+
cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`)
41+
})
42+
} else {
43+
const domain = name.split('/')[2]
44+
const path = name.split('/').slice(3).join('/')
45+
console.log(domain, path)
46+
return self.dns(domain, (err, result) => {
47+
if (err) return cb(err)
48+
const cid = new CID(result.split('/')[2])
49+
resolveCID(cid, path, (err, res) => {
50+
if (err) return cb(err)
51+
const { cid, remainderPath } = res
52+
cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`)
53+
})
54+
})
55+
}
56+
}
4357

4458
// Resolve the given CID + path to a CID.
45-
function resolve (cid, path, callback) {
59+
function resolveCID (cid, path, callback) {
4660
let value, remainderPath
4761
doUntil(
4862
(cb) => {

0 commit comments

Comments
 (0)