-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
105 lines (98 loc) · 2.62 KB
/
index.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
'use strict'
const Dat = require('dat-node')
const http = require('http')
const hyperdriveHttp = require('hyperdrive-http')
const LRU = require('lru-cache')
const resolveDat = require('dat-link-resolve')
function log () {
let msg = arguments[0]
arguments[0] = '[dat-gateway] ' + msg
if (process.env.DEBUG || process.env.LOG) {
console.log.apply(console, arguments)
}
}
module.exports =
class DatGateway {
constructor ({ dir, max, maxAge }) {
this.dir = dir
this.datOptions = { temp: true }
log('Starting gateway at %s with options %j', this.dir, { max, maxAge })
this.cache = new LRU({
dispose: function (key, dat) {
log('Disposing of archive %s', key)
dat.close()
},
max,
maxAge
})
this.server = http.createServer((req, res) => {
log('%s %s', req.method, req.url)
// TODO redirect /:key to /:key/
let urlParts = req.url.split('/')
let address = urlParts[1]
let path = urlParts.slice(2).join('/')
return this.resolveDat(address).then((key) => {
return this.getDat(key)
}).then((dat) => {
// handle it!!
req.url = `/${path}`
dat.onrequest(req, res)
}).catch((e) => {
log(e)
if (e.message === 'DNS record not found') {
res.writeHead(404)
res.end('Not found')
} else {
res.writeHead(500)
res.end(JSON.stringify(e))
}
})
})
}
listen (port, host) {
return new Promise((resolve, reject) => {
this.server.listen(port, host, (err) => {
if (err) return reject(err)
else return resolve()
})
})
}
close () {
return new Promise((resolve) => {
this.server.close(resolve)
}).then(() => {
this.cache.reset()
})
}
getDat (key) {
// check local cache
if (this.cache.has(key)) return Promise.resolve(this.cache.get(key))
// retrieve from the web
return new Promise((resolve, reject) => {
const opts = Object.assign({}, this.datOptions, { key })
Dat(this.dir, opts, (err, dat) => {
if (err) {
return reject(err)
} else {
this.cache.set(key, dat)
dat.joinNetwork()
dat.onrequest = hyperdriveHttp(dat.archive, { live: true, exposeHeaders: true })
dat.archive.metadata.update(() => {
resolve(dat)
})
}
})
})
}
resolveDat (address) {
return new Promise((resolve, reject) => {
resolveDat(address, (err, key) => {
if (err) {
return reject(err)
} else {
return resolve(key)
}
})
})
}
}