This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
resolver.js
120 lines (98 loc) · 2.97 KB
/
resolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict'
const mh = require('multihashes')
const promisify = require('promisify-es6')
const reduce = require('async/reduce')
const CID = require('cids')
const Unixfs = require('ipfs-unixfs')
const debug = require('debug')
const log = debug('jsipfs:http:response:resolver')
log.error = debug('jsipfs:http:response:resolver:error')
const dirView = require('./dir-view')
const pathUtil = require('./utils/path')
function getIndexFiles (links) {
const INDEX_HTML_FILES = [
'index.html',
'index.htm',
'index.shtml'
]
return links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1)
}
const directory = promisify((ipfs, path, multihash, callback) => {
mh.validate(mh.fromB58String(multihash))
ipfs.object.get(multihash, { enc: 'base58' }, (err, dagNode) => {
if (err) {
return callback(err)
}
// Test if it is a Website
const indexFiles = getIndexFiles(dagNode.links)
if (indexFiles.length) {
return callback(null, indexFiles)
}
return callback(null, dirView.render(path, dagNode.links))
})
})
const multihash = promisify((ipfs, path, callback) => {
const parts = pathUtil.splitPath(path)
let firstMultihash = parts.shift()
let currentCid
reduce(
parts,
firstMultihash,
(memo, item, next) => {
try {
currentCid = new CID(mh.fromB58String(memo))
} catch (err) {
return next(err)
}
log('memo: ', memo)
log('item: ', item)
ipfs.dag.get(currentCid, (err, result) => {
if (err) {
return next(err)
}
const dagNode = result.value
// find multihash of requested named-file in current dagNode's links
let multihashOfNextFile
const nextFileName = item
for (let link of dagNode.links) {
if (link.name === nextFileName) {
// found multihash of requested named-file
multihashOfNextFile = mh.toB58String(link.multihash)
log('found multihash: ', multihashOfNextFile)
break
}
}
if (!multihashOfNextFile) {
return next(new Error(`no link named "${nextFileName}" under ${memo}`))
}
next(null, multihashOfNextFile)
})
}, (err, result) => {
if (err) {
return callback(err)
}
let cid
try {
cid = new CID(mh.fromB58String(result))
} catch (err) {
return callback(err)
}
ipfs.dag.get(cid, (err, dagResult) => {
if (err) {
return callback(err)
}
let dagDataObj = Unixfs.unmarshal(dagResult.value.data)
if (dagDataObj.type === 'directory') {
let isDirErr = new Error('This dag node is a directory')
// add memo (last multihash) as a fileName so it can be used by directory
isDirErr.fileName = result
return callback(isDirErr)
}
callback(null, { multihash: result })
})
})
})
module.exports = {
directory: directory,
multihash: multihash
}