@fastify/sse 0.2.0 → 0.3.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/index.js +27 -2
- package/package.json +8 -7
- package/test/abort-error.test.js +160 -0
- package/.claude/settings.local.json +0 -9
package/index.js
CHANGED
|
@@ -100,6 +100,15 @@ class SSEContext {
|
|
|
100
100
|
this.cleanup()
|
|
101
101
|
})
|
|
102
102
|
|
|
103
|
+
// Handle errors on the raw response to prevent uncaught exceptions
|
|
104
|
+
this.reply.raw.on('error', (error) => {
|
|
105
|
+
// Client disconnection is expected behavior, handle gracefully
|
|
106
|
+
this._isConnected = false
|
|
107
|
+
this.cleanup()
|
|
108
|
+
// Log as info since client disconnections are normal
|
|
109
|
+
this.reply.log.info({ err: error }, 'SSE connection closed')
|
|
110
|
+
})
|
|
111
|
+
|
|
103
112
|
// Start heartbeat if enabled
|
|
104
113
|
if (options.heartbeatInterval > 0) {
|
|
105
114
|
this.startHeartbeat(options.heartbeatInterval)
|
|
@@ -204,7 +213,19 @@ class SSEContext {
|
|
|
204
213
|
this.sendHeaders()
|
|
205
214
|
|
|
206
215
|
const transform = createSSETransformStream({ serializer: this.serializer })
|
|
207
|
-
|
|
216
|
+
try {
|
|
217
|
+
await pipeline(source, transform, this.reply.raw, { end: false })
|
|
218
|
+
} catch (error) {
|
|
219
|
+
// Distinguish between expected disconnection errors and unexpected errors
|
|
220
|
+
this._isConnected = false
|
|
221
|
+
this.cleanup()
|
|
222
|
+
if (error && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
|
|
223
|
+
this.reply.log.info({ err: error }, 'SSE stream ended (client disconnected)')
|
|
224
|
+
} else {
|
|
225
|
+
this.reply.log.error({ err: error }, 'Unexpected error in SSE stream')
|
|
226
|
+
}
|
|
227
|
+
return
|
|
228
|
+
}
|
|
208
229
|
return
|
|
209
230
|
}
|
|
210
231
|
|
|
@@ -273,7 +294,11 @@ class SSEContext {
|
|
|
273
294
|
|
|
274
295
|
const onError = (err) => {
|
|
275
296
|
this.reply.raw.off('drain', onDrain)
|
|
276
|
-
|
|
297
|
+
// Handle all errors gracefully - client disconnection is normal
|
|
298
|
+
this._isConnected = false
|
|
299
|
+
this.cleanup()
|
|
300
|
+
this.reply.log.info({ err }, 'SSE write ended')
|
|
301
|
+
resolve() // Resolve instead of reject for graceful handling
|
|
277
302
|
}
|
|
278
303
|
|
|
279
304
|
this.reply.raw.once('drain', onDrain)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/sse",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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
|
+
})
|