-
Notifications
You must be signed in to change notification settings - Fork 7
/
events.js
167 lines (158 loc) · 4.11 KB
/
events.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
/**
* Send data to the client.
* @callback EmitterOfOneEvent
* @param {string} data
* @throws when `data` is not of type `string`.
* @returns {void}
*/
/**
* @callback ProducerOfOneEvent
* @param {EmitterOfOneEvent} emit
* @returns {void}
*/
/**
* Send data to the client.
* @callback EmitterOfManyEvents
* @param {string} eventName
* @param {string} data
* @throws when `eventname` or `data` are not of type `string`.
* @returns {void}
*/
/**
* @callback ProducerOfManyEvents
* @param {EmitterOfManyEvents} emit
* @returns {void}
*/
/**
* @param {ReadableStreamDefaultController} controller
*/
function createEmitter(controller) {
let id = 1
const encoder = new TextEncoder()
/** @type {function(string, string):void} */
return function emit(eventName, data) {
const typeOfEventName = typeof eventName
const typeOfData = typeof data
if (typeOfEventName !== 'string') {
throw new Error(
`Event name must of type \`string\`, received \`${typeOfEventName}\`.`,
)
}
if (typeOfData !== 'string') {
throw new Error(
`Event data must of type \`string\`, received \`${typeOfData}\`.`,
)
}
if (eventName.includes('\n')) {
throw new Error(
`Event name must not contain new line characters, received "${eventName}".`,
)
}
controller.enqueue(encoder.encode(`id: ${id}\nevent: ${eventName}\n`))
const chunks = data.split('\n')
for (const chunk of chunks) {
controller.enqueue(encoder.encode(`data: ${encodeURIComponent(chunk)}\n`))
}
controller.enqueue(encoder.encode('\n'))
id++
}
}
/**
* @callback OnCancelCallback
* @param {UnderlyingDefaultSource<string>} stream
* @returns {void|PromiseLike<void>}
*/
/**
* @param {ProducerOfManyEvents} producer
* @param {Array<OnCancelCallback>} onCancel
* @returns {ReadableStream<string>}
*/
function createStream(producer, onCancel) {
return new ReadableStream({
async start(controller) {
const customEmitter = createEmitter(controller)
await producer(customEmitter)
controller.close()
},
async cancel() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this
for (const callback of onCancel) {
await callback(self)
}
},
})
}
/**
* Create one stream and emit multiple server sent events.
* @param {ProducerOfManyEvents} producer
*/
export function events(producer) {
/** @type {Array<OnCancelCallback>} */
const onCancel = []
/** @type {Map<string, string>} */
const headers = new Map()
/** @type undefined|ReadableStream */
let stream = undefined
return {
/**
* Set a response header.
*
* ### Note
* The following headers are set by default for all events:
* ```json
* {
* "Cache-Control": "no-store",
* "Content-Type": "text/event-stream",
* "Connection": "keep-alive",
* }
* ```
*
* ### Warning
* Overwriting the default headers is allowed.
*
* Overwriting header `Content-Type` to something other than `text/event-stream` will break the SSE contract and the event will stop working as intended.
* @param {string} key
* @param {string} value
* @returns {ReturnType<events>}
*/
setHeader(key, value) {
headers.set(key, value)
return this
},
/**
* Do something after the stream has been canceled.
* @param {OnCancelCallback} callback
* @returns {ReturnType<events>}
*/
onCancel(callback) {
onCancel.push(callback)
return this
},
/**
* Get the underlying stream used by the event.
* @returns {ReadableStream<string>}
*/
getStream() {
if (!stream) {
stream = createStream(producer, onCancel)
}
return stream
},
/**
* Build a `Response`.
* @returns {Response}
*/
toResponse() {
return new Response(this.getStream(), {
//@ts-ignore
headers: {
'Cache-Control': 'no-store',
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
...headers,
},
})
},
}
}