-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.js
More file actions
87 lines (77 loc) · 2.81 KB
/
webserver.js
File metadata and controls
87 lines (77 loc) · 2.81 KB
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
const { createServer } = require('http')
const { WebSocketServer } = require('ws')
const { io } = globalThis.vault
module.exports = webserver
function webserver ({ port = 8080, host = '127.0.0.1', flag = '' }) {
const wss = new WebSocketServer({ noServer: true })
wss.on('connection', onconnection)
const server = createServer(handlerX) // HANDLER syscall
server.on('upgrade', onupgrade) // ONUPGRADE syscall
server.listen(port, host, onlisten)
process.on('SIGINT', onterminate)
// --------------------------------------------------------------------------
function onconnection (ws, request) {
const { url: path } = request
console.log('CONNECTION', { path }) // log
// const send = protocol(message => ws.send(message))
// const send = io.to(path, message => {
// // @TODO: THINK HOW DOES IT REALLY WORK?!?
// // 1. create logkeeper listener with websocket intention
// })
io.to(path, { ws, request })
// const { id, name, stack, fn: protocol } = ROUTES[path]
// const { protocol } = ws
// ws.on('open', event => send(event))
// ws.on('error', event => send(event))
// ws.on('close', event => send(event))
// ws.on('message', event => send(event))
}
function handlerX (request, response) {
const { url } = request
const info = {}
// const info = {
// publickey,
// topics,
// prioritized,
// ban(status = true)
// }
// const stream = io.to('http', info)
// request.pipe(stream).pipe(response)
io.to(url, { request, response })
}
/************************************/
async function onupgrade (request, socket, head) {
socket.on('error', onerror)
try { await authenticate(request) } catch (error) {
console.error('ERROR', error)
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n')
socket.destroy()
}
socket.removeListener('error', onerror)
const { url } = request
// try {
// io.to(url, { request, response })
// } catch (error) {
// }
// const on = ROUTES[url] // @TODO: ...
if (io.has(url)) return wss.handleUpgrade(request, socket, head, ws => {
wss.emit('connection', ws, request)
})
socket.write('HTTP/1.1 404 Not Found\r\n\r\n')
socket.destroy()
}
function onlisten () {
const { address, family, port } = server.address()
const url = `http://${address}:${port}`
console.log('webserver listening on', url)
if (flag !== 'open') return
const command = `${{ darwin: 'open', win32: 'start' }[process.platform] || 'xdg-open'/*linux*/} ${url}`
require('child_process').exec(command)
}
function onterminate () {
console['log']("\n Terminating all processes")
process.exit()
}
function onerror (error) { console.log('[ws]:error>', error) }
function authenticate (request) { return request }
}