-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (31 loc) · 917 Bytes
/
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
const hypersource = require('hypersource')
const hypercore = require('hypercore')
const debug = require('debug')('echo.hypersource.club')
const pump = require('pump')
const ram = require('random-access-memory')
function createServer(opts) {
const server = hypersource.createServer(onrequest)
server.on('error', debug)
return server
function onrequest(req, res) {
const source = hypercore(ram, req.key, req)
const echo = hypercore(ram, res.key, res)
source.on('error', debug)
echo.on('error', debug)
req.on('error', debug)
res.on('error', debug)
source.replicate(req)
source.update(() => {
pump(
source.createReadStream(),
echo.createWriteStream(),
() => echo.replicate(res).once('close', onclose).on('end', onclose)
)
})
function onclose() {
source.close()
echo.close()
}
}
}
module.exports = createServer