@fastify/sse 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -9
- package/examples/basic.js +21 -0
- package/index.js +70 -56
- package/package.json +8 -7
- package/test/abort-error.test.js +160 -0
- package/test/basic.test.js +0 -16
- package/test/custom-headers.test.js +0 -6
- package/test/lifecycle.test.js +0 -10
- package/test/send-headers.test.js +263 -0
- package/types/index.d.ts +14 -0
- package/.claude/settings.local.json +0 -9
package/README.md
CHANGED
|
@@ -168,10 +168,13 @@ fastify.get('/file-stream', { sse: true }, async (request, reply) => {
|
|
|
168
168
|
fastify.get('/live', { sse: true }, async (request, reply) => {
|
|
169
169
|
// Keep connection alive (prevents automatic close)
|
|
170
170
|
reply.sse.keepAlive()
|
|
171
|
-
|
|
171
|
+
|
|
172
172
|
// Send initial message
|
|
173
173
|
await reply.sse.send({ data: 'Connected' })
|
|
174
|
-
|
|
174
|
+
|
|
175
|
+
// Check if keepAlive was called
|
|
176
|
+
console.log('Keep alive status:', reply.sse.shouldKeepAlive) // true
|
|
177
|
+
|
|
175
178
|
// Set up periodic updates
|
|
176
179
|
const interval = setInterval(async () => {
|
|
177
180
|
if (reply.sse.isConnected) {
|
|
@@ -180,7 +183,7 @@ fastify.get('/live', { sse: true }, async (request, reply) => {
|
|
|
180
183
|
clearInterval(interval)
|
|
181
184
|
}
|
|
182
185
|
}, 1000)
|
|
183
|
-
|
|
186
|
+
|
|
184
187
|
// Clean up when connection closes
|
|
185
188
|
reply.sse.onClose(() => {
|
|
186
189
|
clearInterval(interval)
|
|
@@ -220,12 +223,19 @@ fastify.get('/events', { sse: true }, async (request, reply) => {
|
|
|
220
223
|
|
|
221
224
|
### Properties and Methods
|
|
222
225
|
|
|
223
|
-
|
|
226
|
+
#### Properties
|
|
227
|
+
- `reply.sse.lastEventId`: Client's last received event ID (string | null)
|
|
224
228
|
- `reply.sse.isConnected`: Connection status (boolean)
|
|
225
|
-
- `reply.sse.
|
|
229
|
+
- `reply.sse.shouldKeepAlive`: Whether connection should be kept alive after handler completion (boolean)
|
|
230
|
+
|
|
231
|
+
#### Methods
|
|
232
|
+
- `reply.sse.send(source)`: Send SSE messages from various source types
|
|
233
|
+
- `reply.sse.stream()`: Create a transform stream for pipeline operations
|
|
234
|
+
- `reply.sse.keepAlive()`: Prevent connection from auto-closing after handler returns
|
|
226
235
|
- `reply.sse.close()`: Manually close the connection
|
|
227
|
-
- `reply.sse.replay(callback)`: Handle message replay
|
|
228
|
-
- `reply.sse.onClose(callback)`: Register
|
|
236
|
+
- `reply.sse.replay(callback)`: Handle message replay using Last-Event-ID
|
|
237
|
+
- `reply.sse.onClose(callback)`: Register callback for when connection closes
|
|
238
|
+
- `reply.sse.sendHeaders()`: Manually send headers (called automatically on first write)
|
|
229
239
|
|
|
230
240
|
## Advanced Usage
|
|
231
241
|
|
|
@@ -357,12 +367,13 @@ app.get('/events', { sse: true }, async (request, reply) => {
|
|
|
357
367
|
event: 'test',
|
|
358
368
|
data: { hello: 'world' }
|
|
359
369
|
}
|
|
360
|
-
|
|
370
|
+
|
|
361
371
|
await reply.sse.send(message)
|
|
362
|
-
|
|
372
|
+
|
|
363
373
|
// TypeScript knows about SSE properties
|
|
364
374
|
console.log(reply.sse.isConnected) // boolean
|
|
365
375
|
console.log(reply.sse.lastEventId) // string | null
|
|
376
|
+
console.log(reply.sse.shouldKeepAlive) // boolean
|
|
366
377
|
})
|
|
367
378
|
```
|
|
368
379
|
|
package/examples/basic.js
CHANGED
|
@@ -69,6 +69,26 @@ fastify.get('/live', { sse: true }, async (request, reply) => {
|
|
|
69
69
|
})
|
|
70
70
|
})
|
|
71
71
|
|
|
72
|
+
// Custom headers with sendHeaders
|
|
73
|
+
fastify.get('/headers', { sse: true }, async (request, reply) => {
|
|
74
|
+
// Set custom headers using Fastify's header methods
|
|
75
|
+
reply.header('X-Session-ID', 'session-12345')
|
|
76
|
+
reply.header('X-API-Version', 'v1.2.3')
|
|
77
|
+
reply.headers({
|
|
78
|
+
'X-User-Agent': request.headers['user-agent'] || 'unknown',
|
|
79
|
+
'X-Request-Time': new Date().toISOString()
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
// Manually send headers before any SSE data
|
|
83
|
+
reply.sse.sendHeaders()
|
|
84
|
+
|
|
85
|
+
// Now send SSE data
|
|
86
|
+
await reply.sse.send({
|
|
87
|
+
data: 'Headers sent manually before this message',
|
|
88
|
+
event: 'custom-headers'
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
|
|
72
92
|
// Replay functionality
|
|
73
93
|
const messageHistory = []
|
|
74
94
|
let eventId = 0
|
|
@@ -111,6 +131,7 @@ const start = async () => {
|
|
|
111
131
|
console.log(' GET /events - Basic SSE messages')
|
|
112
132
|
console.log(' GET /stream - Streaming with async generator')
|
|
113
133
|
console.log(' GET /live - Persistent connection with heartbeat')
|
|
134
|
+
console.log(' GET /headers - Custom headers with sendHeaders()')
|
|
114
135
|
console.log(' GET /replay - Messages with replay support')
|
|
115
136
|
} catch (err) {
|
|
116
137
|
fastify.log.error(err)
|
package/index.js
CHANGED
|
@@ -4,9 +4,6 @@ const fp = require('fastify-plugin')
|
|
|
4
4
|
const { Readable, Transform } = require('stream')
|
|
5
5
|
const { pipeline } = require('stream/promises')
|
|
6
6
|
|
|
7
|
-
const SSE_DECORATOR = Symbol.for('@fastify/sse.decorator')
|
|
8
|
-
const SSE_CONTEXT = Symbol.for('@fastify/sse.context')
|
|
9
|
-
|
|
10
7
|
/**
|
|
11
8
|
* Format an SSE message according to the specification
|
|
12
9
|
* @param {Object|string|Buffer} message - The message to format
|
|
@@ -84,20 +81,34 @@ function createSSETransformStream (options = {}) {
|
|
|
84
81
|
* SSE Context class for managing connection state
|
|
85
82
|
*/
|
|
86
83
|
class SSEContext {
|
|
84
|
+
#lastEventId
|
|
85
|
+
#isConnected
|
|
86
|
+
#keepAlive
|
|
87
|
+
#headersSent
|
|
88
|
+
|
|
87
89
|
constructor (options) {
|
|
88
90
|
this.reply = options.reply
|
|
89
|
-
this
|
|
90
|
-
this
|
|
91
|
-
this
|
|
92
|
-
this
|
|
91
|
+
this.#lastEventId = options.lastEventId || null
|
|
92
|
+
this.#isConnected = true
|
|
93
|
+
this.#keepAlive = false
|
|
94
|
+
this.#headersSent = false
|
|
93
95
|
this.heartbeatTimer = null
|
|
94
96
|
this.closeCallbacks = []
|
|
95
97
|
this.serializer = options.serializer
|
|
96
98
|
|
|
97
99
|
// Set up connection close handler
|
|
98
100
|
this.reply.raw.on('close', () => {
|
|
99
|
-
this
|
|
101
|
+
this.#isConnected = false
|
|
102
|
+
this.cleanup()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
// Handle errors on the raw response to prevent uncaught exceptions
|
|
106
|
+
this.reply.raw.on('error', (error) => {
|
|
107
|
+
// Client disconnection is expected behavior, handle gracefully
|
|
108
|
+
this.#isConnected = false
|
|
100
109
|
this.cleanup()
|
|
110
|
+
// Log as info since client disconnections are normal
|
|
111
|
+
this.reply.log.info({ err: error }, 'SSE connection closed')
|
|
101
112
|
})
|
|
102
113
|
|
|
103
114
|
// Start heartbeat if enabled
|
|
@@ -111,7 +122,7 @@ class SSEContext {
|
|
|
111
122
|
* @returns {string|null} The last event ID or null if not present
|
|
112
123
|
*/
|
|
113
124
|
get lastEventId () {
|
|
114
|
-
return this
|
|
125
|
+
return this.#lastEventId
|
|
115
126
|
}
|
|
116
127
|
|
|
117
128
|
/**
|
|
@@ -119,7 +130,15 @@ class SSEContext {
|
|
|
119
130
|
* @returns {boolean} True if connected, false otherwise
|
|
120
131
|
*/
|
|
121
132
|
get isConnected () {
|
|
122
|
-
return this
|
|
133
|
+
return this.#isConnected
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Checks if the connection should be kept alive after handler completion.
|
|
138
|
+
* @returns {boolean} True if connection should be kept alive
|
|
139
|
+
*/
|
|
140
|
+
get shouldKeepAlive () {
|
|
141
|
+
return this.#keepAlive
|
|
123
142
|
}
|
|
124
143
|
|
|
125
144
|
/**
|
|
@@ -127,7 +146,7 @@ class SSEContext {
|
|
|
127
146
|
* Without calling this, the connection will close after the handler returns.
|
|
128
147
|
*/
|
|
129
148
|
keepAlive () {
|
|
130
|
-
this
|
|
149
|
+
this.#keepAlive = true
|
|
131
150
|
}
|
|
132
151
|
|
|
133
152
|
/**
|
|
@@ -135,9 +154,9 @@ class SSEContext {
|
|
|
135
154
|
* Safe to call multiple times.
|
|
136
155
|
*/
|
|
137
156
|
close () {
|
|
138
|
-
if (!this
|
|
157
|
+
if (!this.#isConnected) return
|
|
139
158
|
|
|
140
|
-
this
|
|
159
|
+
this.#isConnected = false
|
|
141
160
|
this.cleanup()
|
|
142
161
|
this.reply.raw.end()
|
|
143
162
|
}
|
|
@@ -156,20 +175,20 @@ class SSEContext {
|
|
|
156
175
|
* @param {Function} callback - Async function that receives the last event ID
|
|
157
176
|
*/
|
|
158
177
|
async replay (callback) {
|
|
159
|
-
if (!this
|
|
178
|
+
if (!this.#lastEventId) return
|
|
160
179
|
|
|
161
|
-
await callback(this
|
|
180
|
+
await callback(this.#lastEventId)
|
|
162
181
|
}
|
|
163
182
|
|
|
164
183
|
/**
|
|
165
184
|
* Sends HTTP headers for the SSE response if not already sent.
|
|
166
185
|
* This method ensures headers set via reply.header() are transferred
|
|
167
186
|
* to the raw response before calling writeHead(200).
|
|
168
|
-
* Called automatically before the first SSE data is sent
|
|
169
|
-
*
|
|
187
|
+
* Called automatically before the first SSE data is sent, but can
|
|
188
|
+
* also be called manually if needed.
|
|
170
189
|
*/
|
|
171
190
|
sendHeaders () {
|
|
172
|
-
if (!this
|
|
191
|
+
if (!this.#headersSent) {
|
|
173
192
|
// Get any headers set via reply.header() and transfer to raw response
|
|
174
193
|
const replyHeaders = this.reply.getHeaders()
|
|
175
194
|
for (const [name, value] of Object.entries(replyHeaders)) {
|
|
@@ -177,7 +196,7 @@ class SSEContext {
|
|
|
177
196
|
}
|
|
178
197
|
|
|
179
198
|
this.reply.raw.writeHead(200)
|
|
180
|
-
this
|
|
199
|
+
this.#headersSent = true
|
|
181
200
|
}
|
|
182
201
|
}
|
|
183
202
|
|
|
@@ -188,7 +207,7 @@ class SSEContext {
|
|
|
188
207
|
* @throws {TypeError} If source type is invalid
|
|
189
208
|
*/
|
|
190
209
|
async send (source) {
|
|
191
|
-
if (!this
|
|
210
|
+
if (!this.#isConnected) {
|
|
192
211
|
throw new Error('SSE connection is closed')
|
|
193
212
|
}
|
|
194
213
|
|
|
@@ -204,14 +223,26 @@ class SSEContext {
|
|
|
204
223
|
this.sendHeaders()
|
|
205
224
|
|
|
206
225
|
const transform = createSSETransformStream({ serializer: this.serializer })
|
|
207
|
-
|
|
226
|
+
try {
|
|
227
|
+
await pipeline(source, transform, this.reply.raw, { end: false })
|
|
228
|
+
} catch (error) {
|
|
229
|
+
// Distinguish between expected disconnection errors and unexpected errors
|
|
230
|
+
this.#isConnected = false
|
|
231
|
+
this.cleanup()
|
|
232
|
+
if (error && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
|
|
233
|
+
this.reply.log.info({ err: error }, 'SSE stream ended (client disconnected)')
|
|
234
|
+
} else {
|
|
235
|
+
this.reply.log.error({ err: error }, 'Unexpected error in SSE stream')
|
|
236
|
+
}
|
|
237
|
+
return
|
|
238
|
+
}
|
|
208
239
|
return
|
|
209
240
|
}
|
|
210
241
|
|
|
211
242
|
// Handle AsyncIterable
|
|
212
243
|
if (this.isAsyncIterable(source)) {
|
|
213
244
|
for await (const chunk of source) {
|
|
214
|
-
if (!this
|
|
245
|
+
if (!this.#isConnected) break
|
|
215
246
|
const formatted = formatSSEMessage(chunk, this.serializer)
|
|
216
247
|
await this.writeToStream(formatted)
|
|
217
248
|
}
|
|
@@ -228,7 +259,7 @@ class SSEContext {
|
|
|
228
259
|
* @throws {Error} If connection is closed
|
|
229
260
|
*/
|
|
230
261
|
stream () {
|
|
231
|
-
if (!this
|
|
262
|
+
if (!this.#isConnected) {
|
|
232
263
|
throw new Error('SSE connection is closed')
|
|
233
264
|
}
|
|
234
265
|
|
|
@@ -252,7 +283,7 @@ class SSEContext {
|
|
|
252
283
|
*/
|
|
253
284
|
writeToStream (data) {
|
|
254
285
|
return new Promise((resolve, reject) => {
|
|
255
|
-
if (!this
|
|
286
|
+
if (!this.#isConnected) {
|
|
256
287
|
reject(new Error('SSE connection is closed'))
|
|
257
288
|
return
|
|
258
289
|
}
|
|
@@ -273,7 +304,11 @@ class SSEContext {
|
|
|
273
304
|
|
|
274
305
|
const onError = (err) => {
|
|
275
306
|
this.reply.raw.off('drain', onDrain)
|
|
276
|
-
|
|
307
|
+
// Handle all errors gracefully - client disconnection is normal
|
|
308
|
+
this.#isConnected = false
|
|
309
|
+
this.cleanup()
|
|
310
|
+
this.reply.log.info({ err }, 'SSE write ended')
|
|
311
|
+
resolve() // Resolve instead of reject for graceful handling
|
|
277
312
|
}
|
|
278
313
|
|
|
279
314
|
this.reply.raw.once('drain', onDrain)
|
|
@@ -289,7 +324,7 @@ class SSEContext {
|
|
|
289
324
|
*/
|
|
290
325
|
startHeartbeat (interval) {
|
|
291
326
|
this.heartbeatTimer = setInterval(() => {
|
|
292
|
-
if (this
|
|
327
|
+
if (this.#isConnected) {
|
|
293
328
|
this.sendHeaders()
|
|
294
329
|
this.reply.raw.write(': heartbeat\n\n')
|
|
295
330
|
} else {
|
|
@@ -393,48 +428,27 @@ async function fastifySSE (fastify, opts) {
|
|
|
393
428
|
})
|
|
394
429
|
|
|
395
430
|
// Store context on reply
|
|
396
|
-
reply
|
|
397
|
-
|
|
398
|
-
// Decorate reply with SSE interface
|
|
399
|
-
const sseInterface = {}
|
|
400
|
-
|
|
401
|
-
// Add SSE methods
|
|
402
|
-
Object.defineProperty(sseInterface, 'lastEventId', {
|
|
403
|
-
get: () => context.lastEventId
|
|
404
|
-
})
|
|
405
|
-
|
|
406
|
-
sseInterface.send = (source) => context.send(source)
|
|
407
|
-
sseInterface.stream = () => context.stream()
|
|
408
|
-
sseInterface.keepAlive = () => context.keepAlive()
|
|
409
|
-
sseInterface.close = () => context.close()
|
|
410
|
-
sseInterface.replay = (callback) => context.replay(callback)
|
|
411
|
-
sseInterface.onClose = (callback) => context.onClose(callback)
|
|
412
|
-
|
|
413
|
-
Object.defineProperty(sseInterface, 'isConnected', {
|
|
414
|
-
get: () => context.isConnected
|
|
415
|
-
})
|
|
416
|
-
|
|
417
|
-
reply[SSE_DECORATOR] = sseInterface
|
|
418
|
-
Object.defineProperty(reply, 'sse', {
|
|
419
|
-
get () { return this[SSE_DECORATOR] }
|
|
420
|
-
})
|
|
431
|
+
reply.sse = context
|
|
421
432
|
|
|
433
|
+
let res
|
|
422
434
|
// Call original handler with SSE-enabled reply
|
|
423
435
|
// Note: Headers will be sent on first SSE send
|
|
424
436
|
try {
|
|
425
|
-
await originalHandler.call(this, request, reply)
|
|
437
|
+
res = await originalHandler.call(this, request, reply)
|
|
426
438
|
} catch (error) {
|
|
427
439
|
// If handler doesn't call keepAlive, close connection
|
|
428
|
-
if (!
|
|
429
|
-
|
|
440
|
+
if (!context.shouldKeepAlive) {
|
|
441
|
+
context.close()
|
|
430
442
|
}
|
|
431
443
|
throw error
|
|
432
444
|
}
|
|
433
445
|
|
|
434
446
|
// If handler doesn't call keepAlive, close connection
|
|
435
|
-
if (!
|
|
436
|
-
|
|
447
|
+
if (!context.shouldKeepAlive) {
|
|
448
|
+
context.close()
|
|
437
449
|
}
|
|
450
|
+
|
|
451
|
+
return res
|
|
438
452
|
}
|
|
439
453
|
})
|
|
440
454
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/sse",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Server-Sent Events plugin for Fastify",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
|
-
"url": "https://github.com/fastify/
|
|
27
|
+
"url": "https://github.com/fastify/sse.git"
|
|
28
28
|
},
|
|
29
29
|
"bugs": {
|
|
30
|
-
"url": "https://github.com/fastify/
|
|
30
|
+
"url": "https://github.com/fastify/sse/issues"
|
|
31
31
|
},
|
|
32
|
-
"homepage": "https://github.com/fastify/
|
|
32
|
+
"homepage": "https://github.com/fastify/sse#readme",
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"fastify": "^5.x"
|
|
35
35
|
},
|
|
@@ -40,11 +40,12 @@
|
|
|
40
40
|
"@types/node": "^24.3.0",
|
|
41
41
|
"eslint": "^9.0.0",
|
|
42
42
|
"fastify": "^5.0.0",
|
|
43
|
-
"neostandard": "^0.
|
|
43
|
+
"neostandard": "^0.12.2",
|
|
44
44
|
"tsd": "^0.33.0",
|
|
45
|
-
"typescript": "^5.9.2"
|
|
45
|
+
"typescript": "^5.9.2",
|
|
46
|
+
"undici": "^7.15.0"
|
|
46
47
|
},
|
|
47
48
|
"engines": {
|
|
48
|
-
"node": ">=
|
|
49
|
+
"node": ">=20"
|
|
49
50
|
}
|
|
50
51
|
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('node:test')
|
|
4
|
+
const { strict: assert } = require('node:assert')
|
|
5
|
+
const { setTimeout: sleep } = require('node:timers/promises')
|
|
6
|
+
const Fastify = require('fastify')
|
|
7
|
+
const { request } = require('undici')
|
|
8
|
+
|
|
9
|
+
test('handles body.destroy() without uncaught exception', async (t) => {
|
|
10
|
+
const app = Fastify({ logger: false })
|
|
11
|
+
|
|
12
|
+
t.after(async () => {
|
|
13
|
+
await app.close()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
// Register @fastify/sse plugin
|
|
17
|
+
await app.register(require('../index.js'))
|
|
18
|
+
|
|
19
|
+
// Simple SSE endpoint that sends data and keeps alive
|
|
20
|
+
app.get('/sse', { sse: true }, async (request, reply) => {
|
|
21
|
+
reply.sse.send({ data: 'connected' })
|
|
22
|
+
// Don't keep alive - let it close naturally
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
await app.listen({ port: 0 })
|
|
26
|
+
const baseUrl = `http://localhost:${app.server.address().port}`
|
|
27
|
+
|
|
28
|
+
// Create SSE connection using undici
|
|
29
|
+
const sseResponse = await request(`${baseUrl}/sse`, {
|
|
30
|
+
method: 'GET',
|
|
31
|
+
headers: {
|
|
32
|
+
Accept: 'text/event-stream'
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
assert.strictEqual(sseResponse.statusCode, 200)
|
|
37
|
+
assert.strictEqual(sseResponse.headers['content-type'], 'text/event-stream')
|
|
38
|
+
|
|
39
|
+
// Start consuming the body
|
|
40
|
+
sseResponse.body.on('data', () => {})
|
|
41
|
+
|
|
42
|
+
// Wait for connection to be established
|
|
43
|
+
await sleep(50)
|
|
44
|
+
|
|
45
|
+
// Destroy the body - should not cause uncaught exception
|
|
46
|
+
sseResponse.body.destroy()
|
|
47
|
+
|
|
48
|
+
// Wait to ensure no uncaught exceptions occur
|
|
49
|
+
await sleep(100)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('handles body.destroy() with keepAlive connection', async (t) => {
|
|
53
|
+
const app = Fastify({ logger: false })
|
|
54
|
+
|
|
55
|
+
t.after(async () => {
|
|
56
|
+
await app.close()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
// Register @fastify/sse plugin
|
|
60
|
+
await app.register(require('../index.js'))
|
|
61
|
+
|
|
62
|
+
// SSE endpoint that keeps connection alive
|
|
63
|
+
app.get('/sse', { sse: true }, async (request, reply) => {
|
|
64
|
+
reply.sse.send({ data: 'connected' })
|
|
65
|
+
reply.sse.keepAlive()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
await app.listen({ port: 0 })
|
|
69
|
+
const baseUrl = `http://localhost:${app.server.address().port}`
|
|
70
|
+
|
|
71
|
+
// Create SSE connection using undici
|
|
72
|
+
const sseResponse = await request(`${baseUrl}/sse`, {
|
|
73
|
+
method: 'GET',
|
|
74
|
+
headers: {
|
|
75
|
+
Accept: 'text/event-stream'
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
assert.strictEqual(sseResponse.statusCode, 200)
|
|
80
|
+
assert.strictEqual(sseResponse.headers['content-type'], 'text/event-stream')
|
|
81
|
+
|
|
82
|
+
// Start consuming the body
|
|
83
|
+
sseResponse.body.on('data', () => {})
|
|
84
|
+
|
|
85
|
+
// Wait for connection to be established
|
|
86
|
+
await sleep(50)
|
|
87
|
+
|
|
88
|
+
// Destroy the body - should not cause uncaught exception
|
|
89
|
+
sseResponse.body.destroy()
|
|
90
|
+
|
|
91
|
+
// Wait to ensure no uncaught exceptions occur
|
|
92
|
+
await sleep(100)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('handles body.destroy() with streaming data', async (t) => {
|
|
96
|
+
const app = Fastify({ logger: false })
|
|
97
|
+
|
|
98
|
+
t.after(async () => {
|
|
99
|
+
await app.close()
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
// Register @fastify/sse plugin
|
|
103
|
+
await app.register(require('../index.js'))
|
|
104
|
+
|
|
105
|
+
// SSE endpoint that streams data
|
|
106
|
+
app.get('/sse', { sse: true }, async (request, reply) => {
|
|
107
|
+
reply.sse.keepAlive()
|
|
108
|
+
|
|
109
|
+
// Send initial message immediately
|
|
110
|
+
await reply.sse.send({ data: 'connected' })
|
|
111
|
+
|
|
112
|
+
// Stream data periodically
|
|
113
|
+
const interval = setInterval(async () => {
|
|
114
|
+
if (reply.sse.isConnected) {
|
|
115
|
+
try {
|
|
116
|
+
await reply.sse.send({ data: 'ping' })
|
|
117
|
+
} catch (e) {
|
|
118
|
+
// Connection closed
|
|
119
|
+
clearInterval(interval)
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
clearInterval(interval)
|
|
123
|
+
}
|
|
124
|
+
}, 100)
|
|
125
|
+
|
|
126
|
+
reply.sse.onClose(() => {
|
|
127
|
+
clearInterval(interval)
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
await app.listen({ port: 0 })
|
|
132
|
+
const baseUrl = `http://localhost:${app.server.address().port}`
|
|
133
|
+
|
|
134
|
+
// Create SSE connection using undici
|
|
135
|
+
const sseResponse = await request(`${baseUrl}/sse`, {
|
|
136
|
+
method: 'GET',
|
|
137
|
+
headers: {
|
|
138
|
+
Accept: 'text/event-stream'
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
assert.strictEqual(sseResponse.statusCode, 200)
|
|
143
|
+
assert.strictEqual(sseResponse.headers['content-type'], 'text/event-stream')
|
|
144
|
+
|
|
145
|
+
// Start consuming the body
|
|
146
|
+
let dataReceived = false
|
|
147
|
+
sseResponse.body.on('data', (chunk) => {
|
|
148
|
+
dataReceived = true
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
// Wait for some data to be received
|
|
152
|
+
await sleep(100)
|
|
153
|
+
assert.ok(dataReceived, 'Should have received some data')
|
|
154
|
+
|
|
155
|
+
// Destroy the body while streaming - should not cause uncaught exception
|
|
156
|
+
sseResponse.body.destroy()
|
|
157
|
+
|
|
158
|
+
// Wait to ensure no uncaught exceptions occur
|
|
159
|
+
await sleep(100)
|
|
160
|
+
})
|
package/test/basic.test.js
CHANGED
|
@@ -19,8 +19,6 @@ test('basic SSE functionality', async (t) => {
|
|
|
19
19
|
await reply.sse.send({ data: 'hello world' })
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
-
await fastify.listen({ port: 0 })
|
|
23
|
-
|
|
24
22
|
const response = await fastify.inject({
|
|
25
23
|
method: 'GET',
|
|
26
24
|
url: '/events',
|
|
@@ -57,8 +55,6 @@ test('SSE message formatting', async (t) => {
|
|
|
57
55
|
})
|
|
58
56
|
})
|
|
59
57
|
|
|
60
|
-
await fastify.listen({ port: 0 })
|
|
61
|
-
|
|
62
58
|
const response = await fastify.inject({
|
|
63
59
|
method: 'GET',
|
|
64
60
|
url: '/events',
|
|
@@ -87,8 +83,6 @@ test('string message support', async (t) => {
|
|
|
87
83
|
await reply.sse.send('plain text message')
|
|
88
84
|
})
|
|
89
85
|
|
|
90
|
-
await fastify.listen({ port: 0 })
|
|
91
|
-
|
|
92
86
|
const response = await fastify.inject({
|
|
93
87
|
method: 'GET',
|
|
94
88
|
url: '/events',
|
|
@@ -114,8 +108,6 @@ test('multiline data handling', async (t) => {
|
|
|
114
108
|
await reply.sse.send({ data: 'line1\nline2\nline3' })
|
|
115
109
|
})
|
|
116
110
|
|
|
117
|
-
await fastify.listen({ port: 0 })
|
|
118
|
-
|
|
119
111
|
const response = await fastify.inject({
|
|
120
112
|
method: 'GET',
|
|
121
113
|
url: '/events',
|
|
@@ -147,8 +139,6 @@ test('async generator support', async (t) => {
|
|
|
147
139
|
await reply.sse.send(generate())
|
|
148
140
|
})
|
|
149
141
|
|
|
150
|
-
await fastify.listen({ port: 0 })
|
|
151
|
-
|
|
152
142
|
const response = await fastify.inject({
|
|
153
143
|
method: 'GET',
|
|
154
144
|
url: '/stream',
|
|
@@ -185,8 +175,6 @@ test('readable stream support', async (t) => {
|
|
|
185
175
|
await reply.sse.send(stream)
|
|
186
176
|
})
|
|
187
177
|
|
|
188
|
-
await fastify.listen({ port: 0 })
|
|
189
|
-
|
|
190
178
|
const response = await fastify.inject({
|
|
191
179
|
method: 'GET',
|
|
192
180
|
url: '/stream',
|
|
@@ -254,8 +242,6 @@ test('custom serializer', async (t) => {
|
|
|
254
242
|
await reply.sse.send({ data: 'test' })
|
|
255
243
|
})
|
|
256
244
|
|
|
257
|
-
await fastify.listen({ port: 0 })
|
|
258
|
-
|
|
259
245
|
const response = await fastify.inject({
|
|
260
246
|
method: 'GET',
|
|
261
247
|
url: '/events',
|
|
@@ -291,8 +277,6 @@ test('reply.sse.stream() for pipeline operations', async (t) => {
|
|
|
291
277
|
await pipeline(sourceStream, reply.sse.stream(), reply.raw, { end: false })
|
|
292
278
|
})
|
|
293
279
|
|
|
294
|
-
await fastify.listen({ port: 0 })
|
|
295
|
-
|
|
296
280
|
const response = await fastify.inject({
|
|
297
281
|
method: 'GET',
|
|
298
282
|
url: '/pipeline',
|
|
@@ -23,8 +23,6 @@ test('should allow setting custom headers in SSE responses', async (t) => {
|
|
|
23
23
|
await reply.sse.send({ data: 'hello' })
|
|
24
24
|
})
|
|
25
25
|
|
|
26
|
-
await fastify.listen({ port: 0 })
|
|
27
|
-
|
|
28
26
|
const response = await fastify.inject({
|
|
29
27
|
method: 'GET',
|
|
30
28
|
url: '/events',
|
|
@@ -62,8 +60,6 @@ test('should allow setting headers via reply.header() method', async (t) => {
|
|
|
62
60
|
await reply.sse.send({ data: 'test' })
|
|
63
61
|
})
|
|
64
62
|
|
|
65
|
-
await fastify.listen({ port: 0 })
|
|
66
|
-
|
|
67
63
|
const response = await fastify.inject({
|
|
68
64
|
method: 'GET',
|
|
69
65
|
url: '/events',
|
|
@@ -102,8 +98,6 @@ test('should allow setting headers in preHandler hook', async (t) => {
|
|
|
102
98
|
await reply.sse.send({ data: 'prehandler test' })
|
|
103
99
|
})
|
|
104
100
|
|
|
105
|
-
await fastify.listen({ port: 0 })
|
|
106
|
-
|
|
107
101
|
const response = await fastify.inject({
|
|
108
102
|
method: 'GET',
|
|
109
103
|
url: '/events',
|
package/test/lifecycle.test.js
CHANGED
|
@@ -19,8 +19,6 @@ test('Last-Event-ID header parsing', async (t) => {
|
|
|
19
19
|
await reply.sse.send({ id: '43', data: 'next event' })
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
-
await fastify.listen({ port: 0 })
|
|
23
|
-
|
|
24
22
|
const response = await fastify.inject({
|
|
25
23
|
method: 'GET',
|
|
26
24
|
url: '/events',
|
|
@@ -67,8 +65,6 @@ test('replay functionality', async (t) => {
|
|
|
67
65
|
await reply.sse.send({ id: '4', data: 'latest' })
|
|
68
66
|
})
|
|
69
67
|
|
|
70
|
-
await fastify.listen({ port: 0 })
|
|
71
|
-
|
|
72
68
|
const response = await fastify.inject({
|
|
73
69
|
method: 'GET',
|
|
74
70
|
url: '/events',
|
|
@@ -105,8 +101,6 @@ test('connection state during handler execution', async (t) => {
|
|
|
105
101
|
await reply.sse.send({ data: 'connected' })
|
|
106
102
|
})
|
|
107
103
|
|
|
108
|
-
await fastify.listen({ port: 0 })
|
|
109
|
-
|
|
110
104
|
const response = await fastify.inject({
|
|
111
105
|
method: 'GET',
|
|
112
106
|
url: '/events',
|
|
@@ -145,8 +139,6 @@ test('SSE interface methods exist', async (t) => {
|
|
|
145
139
|
await reply.sse.send({ data: 'test' })
|
|
146
140
|
})
|
|
147
141
|
|
|
148
|
-
await fastify.listen({ port: 0 })
|
|
149
|
-
|
|
150
142
|
const response = await fastify.inject({
|
|
151
143
|
method: 'GET',
|
|
152
144
|
url: '/events',
|
|
@@ -181,8 +173,6 @@ test('error handling in async iterator', async (t) => {
|
|
|
181
173
|
}
|
|
182
174
|
})
|
|
183
175
|
|
|
184
|
-
await fastify.listen({ port: 0 })
|
|
185
|
-
|
|
186
176
|
const response = await fastify.inject({
|
|
187
177
|
method: 'GET',
|
|
188
178
|
url: '/error-stream',
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('node:test')
|
|
4
|
+
const { strict: assert } = require('node:assert')
|
|
5
|
+
const Fastify = require('fastify')
|
|
6
|
+
const fastifySSE = require('../index.js')
|
|
7
|
+
|
|
8
|
+
test('reply.sse.sendHeaders() should send headers manually', async (t) => {
|
|
9
|
+
const fastify = Fastify({ logger: false })
|
|
10
|
+
|
|
11
|
+
t.after(async () => {
|
|
12
|
+
await fastify.close()
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
await fastify.register(fastifySSE)
|
|
16
|
+
|
|
17
|
+
fastify.get('/events', { sse: true }, async (request, reply) => {
|
|
18
|
+
reply.header('X-Manual-Header', 'manual-value')
|
|
19
|
+
reply.header('X-Custom-ID', '12345')
|
|
20
|
+
|
|
21
|
+
// Manually call sendHeaders before sending any data
|
|
22
|
+
reply.sse.sendHeaders()
|
|
23
|
+
|
|
24
|
+
await reply.sse.send({ data: 'hello after manual headers' })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const response = await fastify.inject({
|
|
28
|
+
method: 'GET',
|
|
29
|
+
url: '/events',
|
|
30
|
+
headers: {
|
|
31
|
+
accept: 'text/event-stream'
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
assert.strictEqual(response.statusCode, 200)
|
|
36
|
+
assert.strictEqual(response.headers['x-manual-header'], 'manual-value')
|
|
37
|
+
assert.strictEqual(response.headers['x-custom-id'], '12345')
|
|
38
|
+
|
|
39
|
+
const body = response.body
|
|
40
|
+
assert.ok(body.includes('data: "hello after manual headers"'))
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('reply.sse.sendHeaders() should be idempotent', async (t) => {
|
|
44
|
+
const fastify = Fastify({ logger: false })
|
|
45
|
+
|
|
46
|
+
t.after(async () => {
|
|
47
|
+
await fastify.close()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
await fastify.register(fastifySSE)
|
|
51
|
+
|
|
52
|
+
fastify.get('/events', { sse: true }, async (request, reply) => {
|
|
53
|
+
reply.header('X-Idempotent-Test', 'test-value')
|
|
54
|
+
|
|
55
|
+
// Call sendHeaders multiple times - should be safe
|
|
56
|
+
reply.sse.sendHeaders()
|
|
57
|
+
reply.sse.sendHeaders()
|
|
58
|
+
reply.sse.sendHeaders()
|
|
59
|
+
|
|
60
|
+
await reply.sse.send({ data: 'message after multiple sendHeaders calls' })
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
const response = await fastify.inject({
|
|
64
|
+
method: 'GET',
|
|
65
|
+
url: '/events',
|
|
66
|
+
headers: {
|
|
67
|
+
accept: 'text/event-stream'
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
assert.strictEqual(response.statusCode, 200)
|
|
72
|
+
assert.strictEqual(response.headers['x-idempotent-test'], 'test-value')
|
|
73
|
+
|
|
74
|
+
const body = response.body
|
|
75
|
+
assert.ok(body.includes('data: "message after multiple sendHeaders calls"'))
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('reply.sse.sendHeaders() should transfer all reply headers to raw response', async (t) => {
|
|
79
|
+
const fastify = Fastify({ logger: false })
|
|
80
|
+
|
|
81
|
+
t.after(async () => {
|
|
82
|
+
await fastify.close()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
await fastify.register(fastifySSE)
|
|
86
|
+
|
|
87
|
+
fastify.get('/events', { sse: true }, async (request, reply) => {
|
|
88
|
+
// Set various types of headers
|
|
89
|
+
reply.header('X-Single-Header', 'single-value')
|
|
90
|
+
reply.headers({
|
|
91
|
+
'X-Multi-Header-1': 'multi1',
|
|
92
|
+
'X-Multi-Header-2': 'multi2'
|
|
93
|
+
})
|
|
94
|
+
reply.type('text/event-stream')
|
|
95
|
+
|
|
96
|
+
// Manually send headers
|
|
97
|
+
reply.sse.sendHeaders()
|
|
98
|
+
|
|
99
|
+
// Set more headers after sendHeaders (these won't be sent)
|
|
100
|
+
reply.header('X-After-Headers', 'after-value')
|
|
101
|
+
|
|
102
|
+
await reply.sse.send({ data: 'test' })
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const response = await fastify.inject({
|
|
106
|
+
method: 'GET',
|
|
107
|
+
url: '/events',
|
|
108
|
+
headers: {
|
|
109
|
+
accept: 'text/event-stream'
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
assert.strictEqual(response.statusCode, 200)
|
|
114
|
+
|
|
115
|
+
// Headers set before sendHeaders() should be present
|
|
116
|
+
assert.strictEqual(response.headers['x-single-header'], 'single-value')
|
|
117
|
+
assert.strictEqual(response.headers['x-multi-header-1'], 'multi1')
|
|
118
|
+
assert.strictEqual(response.headers['x-multi-header-2'], 'multi2')
|
|
119
|
+
assert.strictEqual(response.headers['content-type'], 'text/event-stream')
|
|
120
|
+
|
|
121
|
+
// Headers set after sendHeaders() should NOT be present
|
|
122
|
+
assert.strictEqual(response.headers['x-after-headers'], undefined)
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test('reply.sse.sendHeaders() should work with preHandler hooks', async (t) => {
|
|
126
|
+
const fastify = Fastify({ logger: false })
|
|
127
|
+
|
|
128
|
+
t.after(async () => {
|
|
129
|
+
await fastify.close()
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
await fastify.register(fastifySSE)
|
|
133
|
+
|
|
134
|
+
fastify.get('/events', {
|
|
135
|
+
sse: true,
|
|
136
|
+
preHandler: async (request, reply) => {
|
|
137
|
+
reply.header('X-PreHandler-Header', 'prehandler-value')
|
|
138
|
+
}
|
|
139
|
+
}, async (request, reply) => {
|
|
140
|
+
reply.header('X-Handler-Header', 'handler-value')
|
|
141
|
+
|
|
142
|
+
// Manually send headers
|
|
143
|
+
reply.sse.sendHeaders()
|
|
144
|
+
|
|
145
|
+
await reply.sse.send({ data: 'prehandler test' })
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
const response = await fastify.inject({
|
|
149
|
+
method: 'GET',
|
|
150
|
+
url: '/events',
|
|
151
|
+
headers: {
|
|
152
|
+
accept: 'text/event-stream'
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
assert.strictEqual(response.statusCode, 200)
|
|
157
|
+
assert.strictEqual(response.headers['x-prehandler-header'], 'prehandler-value')
|
|
158
|
+
assert.strictEqual(response.headers['x-handler-header'], 'handler-value')
|
|
159
|
+
|
|
160
|
+
const body = response.body
|
|
161
|
+
assert.ok(body.includes('data: "prehandler test"'))
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('reply.sse.sendHeaders() should preserve default SSE headers', async (t) => {
|
|
165
|
+
const fastify = Fastify({ logger: false })
|
|
166
|
+
|
|
167
|
+
t.after(async () => {
|
|
168
|
+
await fastify.close()
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
await fastify.register(fastifySSE)
|
|
172
|
+
|
|
173
|
+
fastify.get('/events', { sse: true }, async (request, reply) => {
|
|
174
|
+
// Call sendHeaders without setting any custom headers
|
|
175
|
+
reply.sse.sendHeaders()
|
|
176
|
+
|
|
177
|
+
await reply.sse.send({ data: 'default headers preserved' })
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
const response = await fastify.inject({
|
|
181
|
+
method: 'GET',
|
|
182
|
+
url: '/events',
|
|
183
|
+
headers: {
|
|
184
|
+
accept: 'text/event-stream'
|
|
185
|
+
}
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
assert.strictEqual(response.statusCode, 200)
|
|
189
|
+
|
|
190
|
+
// Default SSE headers should still be present
|
|
191
|
+
assert.strictEqual(response.headers['content-type'], 'text/event-stream')
|
|
192
|
+
assert.strictEqual(response.headers['cache-control'], 'no-cache')
|
|
193
|
+
assert.strictEqual(response.headers['connection'], 'keep-alive')
|
|
194
|
+
assert.strictEqual(response.headers['x-accel-buffering'], 'no')
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
test('headers should still work when sendHeaders() is not called manually', async (t) => {
|
|
198
|
+
const fastify = Fastify({ logger: false })
|
|
199
|
+
|
|
200
|
+
t.after(async () => {
|
|
201
|
+
await fastify.close()
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
await fastify.register(fastifySSE)
|
|
205
|
+
|
|
206
|
+
fastify.get('/events', { sse: true }, async (request, reply) => {
|
|
207
|
+
reply.header('X-Auto-Header', 'auto-value')
|
|
208
|
+
|
|
209
|
+
// Don't call sendHeaders manually - should be called automatically
|
|
210
|
+
await reply.sse.send({ data: 'automatic headers' })
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
const response = await fastify.inject({
|
|
214
|
+
method: 'GET',
|
|
215
|
+
url: '/events',
|
|
216
|
+
headers: {
|
|
217
|
+
accept: 'text/event-stream'
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
assert.strictEqual(response.statusCode, 200)
|
|
222
|
+
assert.strictEqual(response.headers['x-auto-header'], 'auto-value')
|
|
223
|
+
|
|
224
|
+
const body = response.body
|
|
225
|
+
assert.ok(body.includes('data: "automatic headers"'))
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
test('reply.sse.sendHeaders() should work with keepAlive()', async (t) => {
|
|
229
|
+
const fastify = Fastify({ logger: false })
|
|
230
|
+
|
|
231
|
+
t.after(async () => {
|
|
232
|
+
await fastify.close()
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
await fastify.register(fastifySSE)
|
|
236
|
+
|
|
237
|
+
fastify.get('/events', { sse: true }, async (request, reply) => {
|
|
238
|
+
reply.header('X-KeepAlive-Header', 'keepalive-value')
|
|
239
|
+
|
|
240
|
+
reply.sse.sendHeaders()
|
|
241
|
+
reply.sse.keepAlive()
|
|
242
|
+
|
|
243
|
+
// Send a message after headers are sent
|
|
244
|
+
setTimeout(async () => {
|
|
245
|
+
await reply.sse.send({ data: 'delayed message' })
|
|
246
|
+
reply.sse.close()
|
|
247
|
+
}, 50)
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
const response = await fastify.inject({
|
|
251
|
+
method: 'GET',
|
|
252
|
+
url: '/events',
|
|
253
|
+
headers: {
|
|
254
|
+
accept: 'text/event-stream'
|
|
255
|
+
}
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
assert.strictEqual(response.statusCode, 200)
|
|
259
|
+
assert.strictEqual(response.headers['x-keepalive-header'], 'keepalive-value')
|
|
260
|
+
|
|
261
|
+
const body = response.body
|
|
262
|
+
assert.ok(body.includes('data: "delayed message"'))
|
|
263
|
+
})
|
package/types/index.d.ts
CHANGED
|
@@ -98,6 +98,20 @@ export interface SSEReplyInterface {
|
|
|
98
98
|
* Check if the connection is still active
|
|
99
99
|
*/
|
|
100
100
|
readonly isConnected: boolean
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Check if the connection should be kept alive after handler completion
|
|
104
|
+
*/
|
|
105
|
+
readonly shouldKeepAlive: boolean
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Send HTTP headers for the SSE response if not already sent.
|
|
109
|
+
* This method ensures headers set via reply.header() are transferred
|
|
110
|
+
* to the raw response before calling writeHead(200).
|
|
111
|
+
* Called automatically before the first SSE data is sent, but can
|
|
112
|
+
* also be called manually if needed.
|
|
113
|
+
*/
|
|
114
|
+
sendHeaders(): void
|
|
101
115
|
}
|
|
102
116
|
|
|
103
117
|
declare const fastifySSE: FastifyPluginAsync<SSEPluginOptions>
|