-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
170 lines (149 loc) · 3.88 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict'
const browserify = require('browserify')
const bankai = require('bankai')
const http = require('http')
const path = require('path')
const WebSocketServer = require('ws').Server
const client = path.join(__dirname, 'client')
const pump = require('pump')
const fs = require('fs')
function build (upring) {
const assets = bankai()
const css = assets.css()
const js = assets.js(browserify, client)
const html = assets.html()
var _ready = false
var isClient = false
upring.on('up', function () {
_ready = true
try {
upring.mymeta()
} catch (err) {
isClient = err && true
}
})
const server = http.createServer((req, res) => {
if (req.url.indexOf('/peer/') === 0) {
return ready(res) && peerDetails(req, res)
}
switch (req.url) {
case '/': return html(req, res).pipe(res)
case '/whoami': return ready(res) && whoami(req, res)
case '/peers': return ready(res) && peers(req, res)
case '/ring': return ready(res) && ring(req, res)
case '/hash': return ready(res) && hash(req, res)
case '/bundle.js': return js(req, res).pipe(res)
case '/bundle.css': return css(req, res).pipe(res)
case '/upring.png':
res.setHeader('Content-Type', 'image/png')
return pump(fs.createReadStream(path.join(__dirname, './upring.png')), res)
default:
res.statusCode = 404
return res.end('404 not found')
}
})
const wss = new WebSocketServer({ server: server })
const connections = new Set()
wss.on('connection', function connection (ws) {
connections.add(ws)
ws.on('close', function () {
connections.delete(ws)
})
ws.send(JSON.stringify(computeRing()))
})
upring.on('peerUp', peerUp)
upring.on('peerDown', sendRing)
function sendRing () {
const ring = computeRing()
connections.forEach(function (conn) {
conn.send(JSON.stringify(ring))
})
}
function peerUp (peer) {
sendRing()
upring.peerConn(peer).request({
ns: 'monitoring',
cmd: 'trace'
}, function (err, res) {
if (err) {
upring.logger.warn(err)
return
}
upring.logger.info({ peer }, 'trace set up')
res.streams.trace.on('data', function (trace) {
if (trace.keys.length === 0) {
return
}
connections.forEach(function (conn) {
conn.send(JSON.stringify({ trace }))
})
})
})
}
return server
function ready (res) {
if (!_ready) {
res.statusCode = 503
res.end('ring not ready yet')
}
return _ready
}
function whoami (req, res) {
const id = upring.whoami()
res.end(id)
}
function peers (req, res) {
const peers = upring.peers(!isClient)
res.end(JSON.stringify(peers, null, 2))
}
function ring (req, res) {
res.end(JSON.stringify(computeRing(), null, 2))
}
function hash (req, res) {
if (req.method !== 'POST') {
res.statusCode = 404
res.end()
return
}
var chunks = ''
req.setEncoding('utf8')
req.on('data', function (d) {
chunks += d
})
req.on('end', function () {
if (chunks.length === 0) {
res.statusCode = 422
res.end()
return
}
res.end(JSON.stringify({
point: upring._hashring.hash(chunks)
}, null, 2))
})
}
function computeRing () {
const ring = upring._hashring._entries.map(function (entry) {
return {
id: entry.peer.id,
point: entry.point
}
})
return { ring }
}
function peerDetails (req, res) {
const id = req.url.split('/peer/')[1]
const conn = upring.peerConn({ id })
conn.request({
ns: 'monitoring',
cmd: 'info'
}, function (err, info) {
if (err) {
res.statusCode = 500
res.end(err.message)
return
}
res.end(JSON.stringify(info, null, 2))
})
}
}
module.exports = build