-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.mjs
94 lines (87 loc) · 2.96 KB
/
main.mjs
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
import http from 'http'
import readStream from './readStream.mjs'
import Queue from './Queue.mjs'
class Message {
constructor(author, text) {
this.id = lastid++
this.buffer = Buffer.from(`id:${this.id}\ndata:${JSON.stringify({
author,
time: new Date().toISOString(),
text: text.replace(/\s+/g, ' ')
})}\n\n`)
}
}
const esHeaders = {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
}
let lastid = 1
const clients = new Set() // this is where we store the open response streams
const messages = new Queue(new Message('server', 'Hello world!'))
const server = http.createServer((req, res) => {
if (req.headers.origin) res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
// GET / -> start an event-stream response
if (req.url === '/') {
if (req.headers.origin) res.setHeader('Access-Control-Allow-Methods', 'GET')
if (clients.size > 1000) {
res.writeHead(503)
res.end('Il y a trop de monde sur le canal ; revenez plus tard.')
return
}
switch (req.method) {
case 'OPTIONS': break
case 'GET':
res.writeHead(200, esHeaders)
const connectedEvent = Buffer.from(`event:join\ndata:${remoteAddr(req)}\n\n`)
for (const client of clients) client.write(connectedEvent)
clients.add(res)
const lastEventId = req.headers['Last-Event-ID']
for (const message of messages.valuesAfterID(lastEventId)) res.write(message.buffer)
req.socket.addListener('close', () => {
clients.delete(res)
const disconnectedEvent = Buffer.from(`event:quit\ndata:${remoteAddr(req)}\n\n`)
for (const client of clients) client.write(disconnectedEvent)
})
return
default: res.writeHead(405)
}
}
// POST /send -> add a message
else if (req.url === '/send') {
if (req.headers.origin) res.setHeader('Access-Control-Allow-Methods', 'POST')
switch (req.method) {
case 'OPTIONS': break
case 'POST':
readStream(req, 4095)
.then(buffer => {
clearTimeout(keepaliveTimer)
res.writeHead(204)
res.end()
const message = new Message(remoteAddr(req), buffer.toString())
messages.push(message)
if (messages.length > 1000) messages.shift()
for (const client of clients) client.write(message.buffer)
keepaliveTimer = setTimeout(keepalive, 3e4)
})
.catch(err => {
res.writeHead(400)
res.end(err.message)
})
return
default: res.writeHead(405)
}
} else res.writeHead(404)
res.end()
})
server.listen(8916)
// ping to keep connections alive when channel is inactive
const ping = Buffer.from(':ping\n\n')
function keepalive() {
for (const client of clients) client.write(ping)
keepaliveTimer = setTimeout(keepalive, 3e4)
}
let keepaliveTimer = setTimeout(keepalive, 3e4)
function remoteAddr(req) {
return req.headers['x-forwarded-for'] || req.connection.remoteAddress
}