-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
147 lines (143 loc) · 3.53 KB
/
Copy pathmain.js
File metadata and controls
147 lines (143 loc) · 3.53 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
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
/* eslint-disable node/no-unsupported-features/es-syntax */
// REFERENCES
// https://www.w3.org/TR/eventsource/
// https://html.spec.whatwg.org/multipage/server-sent-events.html
const STATUS_OK = 200
const DEFAULT_EVENT = 'message'
const EOLS = new RegExp('\\r\\n|\\r|\\n', 'g')
export const message = ({ data, event, id, retry, stringify }) => {
const chunks = []
if (event !== undefined && event !== DEFAULT_EVENT) {
chunks.push(`event:${event}`)
}
if (data !== undefined) {
let chunk
if (stringify === true) {
try {
chunk = JSON.stringify(data)
} catch (error) {
chunk = error
}
if (chunk instanceof Error) {
return chunk // I THINK IT'S A GOOD IDEA TO HANDLE THE ERROR ON THE OTHER SIDE
}
} else if (typeof data === 'string') {
if (EOLS.test(data) === true) {
chunk = data
.split(EOLS)
.map((iterator) => {
return iterator.trim()
})
.join('\ndata:')
} else {
chunk = data.trim()
}
} else {
return Error('Use stringify option to send an object')
}
chunks.push(`data:${chunk}`)
}
if (id !== undefined) {
chunks.push(`id:${id}`)
}
if (retry !== undefined) {
chunks.push(`retry:${retry}`)
}
return chunks.join('\n') + '\n\n'
}
export const sseHandler = ({ queueSizeByEvent = {} } = { queueSizeByEvent: {} }) => {
let clientId = 0
let eventId = 0
const clients = new Map()
const chunksByEvent = new Map()
for (const event in queueSizeByEvent) {
if (queueSizeByEvent[event] > 0) {
chunksByEvent.set(event, [])
}
}
const end = () => {
for (const client of clients.values()) {
client.end()
}
}
const flush = ({ event = DEFAULT_EVENT } = { event: DEFAULT_EVENT }) => {
if (chunksByEvent.has(event) === true) {
chunksByEvent.set(event, [])
}
}
const handle = ({ request, response }) => {
if (request.aborted === true || response.writableEnded === true) {
return undefined
}
let buffer = ''
for (const chunks of chunksByEvent.values()) {
buffer += chunks.join('')
}
if (buffer.length === 0) {
buffer = '\n\n' // THIS FIRES AN OPEN EVENT AT THE EVENTSOURCE
}
response
.writeHead(STATUS_OK, {
'Cache-Control': 'no-store',
'Connection': 'keep-alive',
'Content-Type': 'text/event-stream',
})
.write(buffer)
if (clientId === Number.MAX_SAFE_INTEGER) {
clientId = 1
} else {
clientId++
}
clients.set(clientId, response)
request.once('close', () => {
clients.delete(clientId)
})
return {
clientId,
}
}
const push = ({ data, event = DEFAULT_EVENT, stringify = false }) => {
if (eventId === Number.MAX_SAFE_INTEGER) {
eventId = 1
} else {
eventId++
}
const chunk = message({
data,
event,
id: eventId,
stringify,
})
if (chunk instanceof Error) {
return {
error: chunk,
eventId,
}
}
if (event in queueSizeByEvent === true) {
const queueSize = queueSizeByEvent[event]
const chunks = chunksByEvent.get(event)
const size = chunks.push(chunk)
if (queueSize < size) {
chunks.splice(0, size - queueSize)
}
}
for (const client of clients.values()) {
if (client.writableEnded === true) {
continue
}
client.write(chunk)
}
return {
eventId,
}
}
return {
chunksByEvent,
clients,
end,
flush,
handle,
push,
}
}