Skip to content
This repository was archived by the owner on Sep 28, 2021. It is now read-only.

Commit 018d7ca

Browse files
author
Pedro Santos
committed
chore: convert resolver to async/await syntax
1 parent 3284ae2 commit 018d7ca

File tree

3 files changed

+66
-59
lines changed

3 files changed

+66
-59
lines changed

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
},
3232
"homepage": "https://github.com./ipfs/js-ipfs-http-response#readme",
3333
"dependencies": {
34-
"async": "^2.6.1",
3534
"cids": "~0.7.1",
3635
"debug": "^4.1.1",
3736
"file-type": "^8.0.0",
@@ -40,7 +39,6 @@
4039
"ipfs-unixfs": "~0.1.16",
4140
"mime-types": "^2.1.21",
4241
"multihashes": "~0.4.14",
43-
"promisify-es6": "^1.0.3",
4442
"stream-to-blob": "^2.0.0"
4543
},
4644
"devDependencies": {

src/resolver.js

+41-57
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,73 @@
11
'use strict'
22

33
const mh = require('multihashes')
4-
const promisify = require('promisify-es6')
54
const CID = require('cids')
65
const debug = require('debug')
7-
const tryEach = require('async/tryEach')
8-
const waterfall = require('async/waterfall')
96
const log = debug('jsipfs:http:response:resolver')
107
log.error = debug('jsipfs:http:response:resolver:error')
118
const dirView = require('./dir-view')
9+
const pTryEeach = require('./utils/p-try-each')
1210

1311
const INDEX_HTML_FILES = [
1412
'index.html',
1513
'index.htm',
1614
'index.shtml'
1715
]
1816

19-
const findIndexFile = (ipfs, path, callback) => {
20-
return tryEach(INDEX_HTML_FILES.map(file => {
21-
return (cb) => {
22-
waterfall([
23-
(cb) => ipfs.files.stat(`${path}/${file}`, cb),
24-
(stats, cb) => cb(null, {
25-
name: file,
26-
cid: new CID(stats.hash)
27-
})
28-
], cb)
17+
const findIndexFile = (ipfs, path) => {
18+
return pTryEeach(INDEX_HTML_FILES.map(file => {
19+
return async () => {
20+
const stats = await ipfs.files.stat(`${path}/${file}`)
21+
22+
return {
23+
name: file,
24+
cid: new CID(stats.hash)
25+
}
2926
}
30-
}), callback)
27+
}))
3128
}
3229

33-
const directory = promisify((ipfs, path, cid, callback) => {
34-
// Test if it is a Website
35-
findIndexFile(ipfs, path, (err, res) => {
36-
if (err) {
37-
if (err.message.includes('does not exist')) {
38-
// not a website, just show a directory listing
39-
return ipfs.dag.get(cid, (err, result) => {
40-
if (err) {
41-
return callback(err)
42-
}
43-
44-
return callback(null, dirView.render(path, result.value.Links))
45-
})
46-
}
30+
const directory = async (ipfs, path, cid) => {
31+
try {
32+
const res = await findIndexFile(ipfs, path)
4733

48-
return callback(err)
34+
return [{ Name: res.name }]
35+
} catch (err) {
36+
if (err.message.includes('does not exist')) {
37+
// not a website, just show a directory listing
38+
const result = await ipfs.dag.get(cid)
39+
40+
return dirView.render(path, result.value.Links)
4941
}
5042

51-
callback(err, [{
52-
Name: res.name
53-
}])
54-
})
55-
})
43+
throw err
44+
}
45+
}
5646

57-
const cid = promisify((ipfs, path, callback) => {
58-
ipfs.files.stat(path, (err, stats) => {
59-
if (err) {
60-
return callback(err)
61-
}
47+
const cid = async (ipfs, path) => {
48+
const stats = await ipfs.files.stat(path)
6249

63-
const cid = new CID(stats.hash)
50+
const cid = new CID(stats.hash)
6451

65-
if (stats.type.includes('directory')) {
66-
const err = new Error('This dag node is a directory')
67-
err.cid = cid
68-
err.fileName = stats.name
69-
err.dagDirType = stats.type
52+
if (stats.type.includes('directory')) {
53+
const err = new Error('This dag node is a directory')
54+
err.cid = cid
55+
err.fileName = stats.name
56+
err.dagDirType = stats.type
7057

71-
return callback(err)
72-
}
58+
throw err
59+
}
7360

74-
callback(err, {
75-
cid
76-
})
77-
})
78-
})
61+
return { cid }
62+
}
7963

80-
const multihash = promisify((ipfs, path, callback) => {
64+
const multihash = async (ipfs, path) => {
8165
// deprecated, use 'cid' instead
8266
// (left for backward-compatibility)
83-
cid(ipfs, path)
84-
.then((result) => { callback(null, { multihash: mh.toB58String(result.cid.multihash) }) })
85-
.catch((err) => { callback(err) })
86-
})
67+
const result = await cid(ipfs, path)
68+
69+
return { multihash: mh.toB58String(result.cid.multihash) }
70+
}
8771

8872
module.exports = {
8973
directory: directory,

src/utils/p-try-each.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const pTryEeach = async (iterable) => {
4+
let error
5+
6+
for (const element of iterable) {
7+
const type = typeof element
8+
9+
if (type !== 'function') {
10+
throw new TypeError(`Expected element to be a \`Function\`, received \`${type}\` instead`)
11+
}
12+
13+
try {
14+
const res = await element()
15+
16+
return res
17+
} catch (err) {
18+
error = err
19+
}
20+
}
21+
22+
throw error
23+
}
24+
25+
module.exports = pTryEeach

0 commit comments

Comments
 (0)