-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
461 lines (399 loc) · 12.4 KB
/
index.js
File metadata and controls
461 lines (399 loc) · 12.4 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
'use strict'
const fp = require('fastify-plugin')
const { Readable, Transform } = require('stream')
const { pipeline } = require('stream/promises')
/**
* Format an SSE message according to the specification
* @param {Object|string|Buffer} message - The message to format
* @param {Function} serializer - Function to serialize data
* @returns {string} Formatted SSE message
*/
function formatSSEMessage (message, serializer) {
let payload = ''
if (typeof message === 'string') {
// Plain string message
for (const line of message.split('\n')) {
payload += `data: ${line}\n`
}
} else if (Buffer.isBuffer(message)) {
// Buffer message
const str = message.toString('utf-8')
for (const line of str.split('\n')) {
payload += `data: ${line}\n`
}
} else {
// SSEMessage object
if (message.id) {
payload += `id: ${message.id}\n`
}
if (message.event) {
payload += `event: ${message.event}\n`
}
if (message.data !== undefined) {
// Always serialize the data to ensure consistent format
const dataStr = serializer(message.data)
for (const line of dataStr.split('\n')) {
payload += `data: ${line}\n`
}
}
if (message.retry) {
payload += `retry: ${message.retry}\n`
}
}
// Add trailing newline to separate events
if (payload) {
payload += '\n'
}
return payload
}
/**
* Create a transform stream that converts objects to SSE format
* @param {Object} options - Transform options
* @returns {Transform} Transform stream
*/
function createSSETransformStream (options = {}) {
const { serializer = JSON.stringify } = options
return new Transform({
objectMode: true,
transform (chunk, encoding, callback) {
try {
const formatted = formatSSEMessage(chunk, serializer)
callback(null, formatted)
} catch (error) {
callback(error)
}
}
})
}
/**
* SSE Context class for managing connection state
*/
class SSEContext {
#lastEventId
#isConnected
#keepAlive
#headersSent
constructor (options) {
this.reply = options.reply
this.#lastEventId = options.lastEventId || null
this.#isConnected = true
this.#keepAlive = false
this.#headersSent = false
this.heartbeatTimer = null
this.closeCallbacks = []
this.serializer = options.serializer
// Set up connection close handler
this.reply.raw.on('close', () => {
this.#isConnected = false
this.cleanup()
})
// Handle errors on the raw response to prevent uncaught exceptions
this.reply.raw.on('error', (error) => {
// Client disconnection is expected behavior, handle gracefully
this.#isConnected = false
this.cleanup()
// Log as info since client disconnections are normal
this.reply.log.info({ err: error }, 'SSE connection closed')
})
// Start heartbeat if enabled
if (options.heartbeatInterval > 0) {
this.startHeartbeat(options.heartbeatInterval)
}
}
/**
* Gets the last event ID from the client's Last-Event-ID header.
* @returns {string|null} The last event ID or null if not present
*/
get lastEventId () {
return this.#lastEventId
}
/**
* Checks if the SSE connection is still active.
* @returns {boolean} True if connected, false otherwise
*/
get isConnected () {
return this.#isConnected
}
/**
* Checks if the connection should be kept alive after handler completion.
* @returns {boolean} True if connection should be kept alive
*/
get shouldKeepAlive () {
return this.#keepAlive
}
/**
* Marks the connection to be kept alive after the handler completes.
* Without calling this, the connection will close after the handler returns.
*/
keepAlive () {
this.#keepAlive = true
}
/**
* Closes the SSE connection and performs cleanup.
* Safe to call multiple times.
*/
close () {
if (!this.#isConnected) return
this.#isConnected = false
this.cleanup()
this.reply.raw.end()
}
/**
* Registers a callback to be called when the connection closes.
* @param {Function} callback - Function to call on connection close
*/
onClose (callback) {
this.closeCallbacks.push(callback)
}
/**
* Executes a callback with the last event ID for replay functionality.
* Only calls the callback if a last event ID exists.
* @param {Function} callback - Async function that receives the last event ID
*/
async replay (callback) {
if (!this.#lastEventId) return
await callback(this.#lastEventId)
}
/**
* Sends HTTP headers for the SSE response if not already sent.
* This method ensures headers set via reply.header() are transferred
* to the raw response before calling writeHead(200).
* Called automatically before the first SSE data is sent, but can
* also be called manually if needed.
*/
sendHeaders () {
if (!this.#headersSent) {
// Get any headers set via reply.header() and transfer to raw response
const replyHeaders = this.reply.getHeaders()
for (const [name, value] of Object.entries(replyHeaders)) {
this.reply.raw.setHeader(name, value)
}
this.reply.raw.writeHead(200)
this.#headersSent = true
}
}
/**
* Sends SSE data from various source types.
* @param {string|Buffer|Object|ReadableStream|AsyncIterable} source - The data source to send
* @throws {Error} If connection is closed
* @throws {TypeError} If source type is invalid
*/
async send (source) {
if (!this.#isConnected) {
throw new Error('SSE connection is closed')
}
// Handle single message
if (typeof source === 'string' || Buffer.isBuffer(source) || this.isSSEMessage(source)) {
const formatted = formatSSEMessage(source, this.serializer)
await this.writeToStream(formatted)
return
}
// Handle Readable stream
if (source instanceof Readable) {
this.sendHeaders()
const transform = createSSETransformStream({ serializer: this.serializer })
try {
await pipeline(source, transform, this.reply.raw, { end: false })
} catch (error) {
// Distinguish between expected disconnection errors and unexpected errors
this.#isConnected = false
this.cleanup()
if (error && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
this.reply.log.info({ err: error }, 'SSE stream ended (client disconnected)')
} else {
this.reply.log.error({ err: error }, 'Unexpected error in SSE stream')
}
return
}
return
}
// Handle AsyncIterable
if (this.isAsyncIterable(source)) {
for await (const chunk of source) {
if (!this.#isConnected) break
const formatted = formatSSEMessage(chunk, this.serializer)
await this.writeToStream(formatted)
}
return
}
throw new TypeError('Invalid SSE source type')
}
/**
* Creates a transform stream for SSE formatting.
* The returned stream automatically sends headers on first write.
* @returns {Transform} A transform stream that formats data as SSE
* @throws {Error} If connection is closed
*/
stream () {
if (!this.#isConnected) {
throw new Error('SSE connection is closed')
}
const transform = createSSETransformStream({ serializer: this.serializer })
// Wrap the transform to send headers on first write
const originalWrite = transform._write
transform._write = (chunk, encoding, callback) => {
this.sendHeaders()
originalWrite.call(transform, chunk, encoding, callback)
}
return transform
}
/**
* Writes data to the response stream with backpressure handling.
* @param {string|Buffer} data - The data to write
* @returns {Promise<void>} Resolves when data is written
* @private
*/
writeToStream (data) {
return new Promise((resolve, reject) => {
if (!this.#isConnected) {
reject(new Error('SSE connection is closed'))
return
}
// Send headers on first write
this.sendHeaders()
const canWrite = this.reply.raw.write(data)
if (canWrite) {
resolve()
} else {
// Handle backpressure
const onDrain = () => {
this.reply.raw.off('error', onError)
resolve()
}
const onError = (err) => {
this.reply.raw.off('drain', onDrain)
// Handle all errors gracefully - client disconnection is normal
this.#isConnected = false
this.cleanup()
this.reply.log.info({ err }, 'SSE write ended')
resolve() // Resolve instead of reject for graceful handling
}
this.reply.raw.once('drain', onDrain)
this.reply.raw.once('error', onError)
}
})
}
/**
* Starts sending periodic heartbeat messages to keep the connection alive.
* @param {number} interval - Heartbeat interval in milliseconds
* @private
*/
startHeartbeat (interval) {
this.heartbeatTimer = setInterval(() => {
if (this.#isConnected) {
this.sendHeaders()
this.reply.raw.write(': heartbeat\n\n')
} else {
this.stopHeartbeat()
}
}, interval)
// Ensure timer doesn't keep process alive
this.heartbeatTimer.unref()
}
/**
* Stops the heartbeat timer.
* @private
*/
stopHeartbeat () {
if (this.heartbeatTimer) {
clearInterval(this.heartbeatTimer)
this.heartbeatTimer = null
}
}
/**
* Performs cleanup operations when the connection closes.
* Stops heartbeat and executes all registered close callbacks.
* @private
*/
cleanup () {
this.stopHeartbeat()
// Call all close callbacks
for (const callback of this.closeCallbacks) {
try {
callback()
} catch (error) {
// Log error but don't throw
console.error('Error in SSE close callback:', error)
}
}
this.closeCallbacks = []
}
/**
* Checks if a value is a valid SSE message object.
* @param {*} value - The value to check
* @returns {boolean} True if value is an SSE message object
* @private
*/
isSSEMessage (value) {
return typeof value === 'object' &&
value !== null &&
'data' in value
}
/**
* Checks if a value is an async iterable.
* @param {*} value - The value to check
* @returns {boolean} True if value is async iterable
* @private
*/
isAsyncIterable (value) {
return value != null &&
typeof value[Symbol.asyncIterator] === 'function'
}
}
async function fastifySSE (fastify, opts) {
const {
heartbeatInterval = 30000,
serializer = JSON.stringify
} = opts
// Add route-level SSE handler
fastify.addHook('onRoute', (routeOptions) => {
if (!routeOptions.sse) return
const originalHandler = routeOptions.handler
const sseOptions = typeof routeOptions.sse === 'object' ? routeOptions.sse : {}
routeOptions.handler = async function sseHandler (request, reply) {
// Check if client accepts SSE
const acceptHeader = request.headers.accept || ''
if (!acceptHeader.includes('text/event-stream')) {
// Fall back to regular handler for non-SSE requests
return await originalHandler.call(this, request, reply)
}
// Set up SSE response
reply.raw.setHeader('Content-Type', 'text/event-stream')
reply.raw.setHeader('Cache-Control', 'no-cache')
reply.raw.setHeader('Connection', 'keep-alive')
reply.raw.setHeader('X-Accel-Buffering', 'no') // Disable Nginx buffering
// Create SSE context
const context = new SSEContext({
reply,
lastEventId: request.headers['last-event-id'],
heartbeatInterval: sseOptions.heartbeat !== false ? heartbeatInterval : 0,
serializer: sseOptions.serializer || serializer
})
// Store context on reply
reply.sse = context
let res
// Call original handler with SSE-enabled reply
// Note: Headers will be sent on first SSE send
try {
res = await originalHandler.call(this, request, reply)
} catch (error) {
// If handler doesn't call keepAlive, close connection
if (!context.shouldKeepAlive) {
context.close()
}
throw error
}
// If handler doesn't call keepAlive, close connection
if (!context.shouldKeepAlive) {
context.close()
}
return res
}
})
}
module.exports = fp(fastifySSE, {
fastify: '5.x',
name: '@fastify/sse'
})
module.exports.default = fastifySSE
module.exports.fastifySSE = fastifySSE