|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const mh = require('multihashes') |
4 | | -const promisify = require('promisify-es6') |
5 | 4 | const CID = require('cids') |
6 | 5 | const debug = require('debug') |
7 | | -const tryEach = require('async/tryEach') |
8 | | -const waterfall = require('async/waterfall') |
9 | 6 | const log = debug('jsipfs:http:response:resolver') |
10 | 7 | log.error = debug('jsipfs:http:response:resolver:error') |
11 | 8 | const dirView = require('./dir-view') |
| 9 | +const pTryEeach = require('./utils/p-try-each') |
12 | 10 |
|
13 | 11 | const INDEX_HTML_FILES = [ |
14 | 12 | 'index.html', |
15 | 13 | 'index.htm', |
16 | 14 | 'index.shtml' |
17 | 15 | ] |
18 | 16 |
|
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 | + } |
29 | 26 | } |
30 | | - }), callback) |
| 27 | + })) |
31 | 28 | } |
32 | 29 |
|
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) |
47 | 33 |
|
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) |
49 | 41 | } |
50 | 42 |
|
51 | | - callback(err, [{ |
52 | | - Name: res.name |
53 | | - }]) |
54 | | - }) |
55 | | -}) |
| 43 | + throw err |
| 44 | + } |
| 45 | +} |
56 | 46 |
|
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) |
62 | 49 |
|
63 | | - const cid = new CID(stats.hash) |
| 50 | + const cid = new CID(stats.hash) |
64 | 51 |
|
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 |
70 | 57 |
|
71 | | - return callback(err) |
72 | | - } |
| 58 | + throw err |
| 59 | + } |
73 | 60 |
|
74 | | - callback(err, { |
75 | | - cid |
76 | | - }) |
77 | | - }) |
78 | | -}) |
| 61 | + return { cid } |
| 62 | +} |
79 | 63 |
|
80 | | -const multihash = promisify((ipfs, path, callback) => { |
| 64 | +const multihash = async (ipfs, path) => { |
81 | 65 | // deprecated, use 'cid' instead |
82 | 66 | // (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 | +} |
87 | 71 |
|
88 | 72 | module.exports = { |
89 | 73 | directory: directory, |
|
0 commit comments