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

Commit d61947d

Browse files
committed
another commit
1 parent 167e5f1 commit d61947d

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

src/core/components/resolve.js

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

23-
resolve(name, opts, cb)
24-
})
25-
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])
30-
31-
if (split.length === 3) {
32-
return setImmediate(() => cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}`))
33-
}
23+
const split = name.split('/') // ['', 'ipfs', 'hash or domain', ...path]
24+
const hashOrDomain = split[2]
25+
const path = split.slice(3).join('/')
3426

35-
const path = split.slice(3).join('/')
36-
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-
})
27+
if (isIpfs.cid(hashOrDomain)) {
28+
return resolveCID(hashOrDomain, path, opts, cb)
5529
}
56-
}
5730

58-
// Resolve the given CID + path to a CID.
59-
function resolveCID (cid, path, callback) {
31+
// if its not a cid then its probably a domain name to resolve
32+
return resolveDomain(hashOrDomain, path, opts, cb)
33+
})
34+
35+
// Resolve the given CID + path to a CID (recursive).
36+
function resolveCID (hash, path, opts, callback) {
37+
let cid = new CID(hash)
6038
let value, remainderPath
6139
doUntil(
6240
(cb) => {
@@ -96,8 +74,21 @@ module.exports = (self) => {
9674
},
9775
(err) => {
9876
if (err) return callback(err)
99-
callback(null, { cid, remainderPath: path })
77+
callback(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`)
10078
}
10179
)
10280
}
81+
82+
function resolveDomain (domain, path, opts, callback) {
83+
const recursive = opts.recursive && opts.recursive.toString() === 'true'
84+
return self.dns(domain, (err, result) => {
85+
if (err) return callback(err)
86+
const hash = result.split('/')[2]
87+
const remainderPath = path
88+
if (recursive) {
89+
return resolveCID(hash, remainderPath, opts, callback)
90+
}
91+
callback(null, `/ipfs/${cidToString(hash, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`)
92+
})
93+
}
10394
}

0 commit comments

Comments
 (0)