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

Commit 3c2b1ad

Browse files
committed
refactor: gateway resolver
1 parent 57a7b8d commit 3c2b1ad

File tree

1 file changed

+63
-73
lines changed

1 file changed

+63
-73
lines changed

src/http/gateway/resolver.js

+63-73
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,30 @@ const PathUtil = require('./utils/path')
1212

1313
const INDEX_HTML_FILES = [ 'index.html', 'index.htm', 'index.shtml' ]
1414

15+
function noop () {}
16+
1517
const resolveDirectory = promisify((ipfs, path, multihash, callback) => {
16-
if (!callback) {
17-
callback = noop
18-
}
18+
callback = callback || noop
1919

2020
mh.validate(mh.fromB58String(multihash))
2121

22-
ipfs
23-
.object
24-
.get(multihash, { enc: 'base58' })
25-
.then((DAGNode) => {
26-
const links = DAGNode.links
27-
const indexFiles = links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1)
22+
ipfs.object.get(multihash, { enc: 'base58' }, (err, dagNode) => {
23+
if (err) { return callback(err) }
2824

29-
// found index file in links
30-
if (indexFiles.length > 0) {
31-
return callback(null, indexFiles)
32-
}
25+
const links = dagNode.links
26+
const indexFiles = links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1)
3327

34-
return callback(null, html.build(path, links))
35-
})
36-
})
28+
// found index file in links
29+
if (indexFiles.length > 0) {
30+
return callback(null, indexFiles)
31+
}
3732

38-
const noop = function () {}
33+
return callback(null, html.build(path, links))
34+
})
35+
})
3936

4037
const resolveMultihash = promisify((ipfs, path, callback) => {
41-
if (!callback) {
42-
callback = noop
43-
}
38+
callback = callback || noop
4439

4540
const parts = PathUtil.splitPath(path)
4641
const partsLength = parts.length
@@ -53,62 +48,57 @@ const resolveMultihash = promisify((ipfs, path, callback) => {
5348
log('currentMultihash: ', currentMultihash)
5449
log('currentIndex: ', currentIndex, '/', partsLength)
5550

56-
ipfs
57-
.object
58-
.get(currentMultihash, { enc: 'base58' })
59-
.then((DAGNode) => {
60-
// log('DAGNode: ', DAGNode)
61-
if (currentIndex === partsLength - 1) {
62-
// leaf node
63-
log('leaf node: ', currentMultihash)
64-
// log('DAGNode: ', DAGNode.links)
65-
66-
if (DAGNode.links &&
67-
DAGNode.links.length > 0 &&
68-
DAGNode.links[0].name.length > 0) {
69-
// this is a directory.
70-
let isDirErr = new Error('This dag node is a directory')
71-
// add currentMultihash as a fileName so it can be used by resolveDirectory
72-
isDirErr.fileName = currentMultihash
73-
return next(isDirErr)
74-
}
75-
76-
next()
77-
} else {
78-
// find multihash of requested named-file
79-
// in current DAGNode's links
80-
let multihashOfNextFile
81-
const nextFileName = parts[currentIndex + 1]
82-
const links = DAGNode.links
83-
84-
for (let link of links) {
85-
if (link.name === nextFileName) {
86-
// found multihash of requested named-file
87-
multihashOfNextFile = mh.toB58String(link.multihash)
88-
log('found multihash: ', multihashOfNextFile)
89-
break
90-
}
91-
}
92-
93-
if (!multihashOfNextFile) {
94-
log.error(`no link named "${nextFileName}" under ${currentMultihash}`)
95-
throw new Error(`no link named "${nextFileName}" under ${currentMultihash}`)
96-
}
97-
98-
currentMultihash = multihashOfNextFile
99-
next()
100-
}
101-
})
51+
ipfs.object.get(currentMultihash, { enc: 'base58' }, (err, dagNode) => {
52+
if (err) { return next(err) }
53+
54+
if (currentIndex === partsLength - 1) {
55+
// leaf node
56+
log('leaf node: ', currentMultihash)
57+
58+
// TODO: Check if it is a directory by using Unixfs Type, right now
59+
// it won't detect empty dirs
60+
if (dagNode.links &&
61+
dagNode.links.length > 0 &&
62+
dagNode.links[0].name.length > 0) {
63+
// this is a directory.
64+
65+
let isDirErr = new Error('This dag node is a directory')
66+
// add currentMultihash as a fileName so it can be used by resolveDirectory
67+
isDirErr.fileName = currentMultihash
68+
return next(isDirErr)
69+
}
70+
return next()
71+
}
72+
73+
// find multihash of requested named-file in current dagNode's links
74+
let multihashOfNextFile
75+
const nextFileName = parts[currentIndex + 1]
76+
const links = dagNode.links
77+
78+
for (let link of links) {
79+
if (link.name === nextFileName) {
80+
// found multihash of requested named-file
81+
multihashOfNextFile = mh.toB58String(link.multihash)
82+
log('found multihash: ', multihashOfNextFile)
83+
break
84+
}
85+
}
86+
87+
if (!multihashOfNextFile) {
88+
log.error(`no link named "${nextFileName}" under ${currentMultihash}`)
89+
return next(new Error(`no link named "${nextFileName}" under ${currentMultihash}`))
90+
}
91+
92+
currentMultihash = multihashOfNextFile
93+
next()
94+
})
10295
}, (err) => {
103-
if (err) {
104-
log.error(err)
105-
return callback(err)
106-
}
107-
callback(null, {multihash: currentMultihash})
96+
if (err) { return callback(err) }
97+
callback(null, { multihash: currentMultihash })
10898
})
10999
})
110100

111101
module.exports = {
112-
resolveDirectory,
113-
resolveMultihash
102+
resolveDirectory: resolveDirectory,
103+
resolveMultihash: resolveMultihash
114104
}

0 commit comments

Comments
 (0)