@fastify/sse 0.3.0 → 0.5.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/.github/dependabot.yml +42 -2
- package/.github/workflows/ci.yml +6 -1
- package/.github/workflows/lock-threads.yml +19 -0
- package/LICENSE +22 -0
- package/README.md +149 -40
- package/bench/accept-header.js +344 -0
- package/bench/in-context.js +210 -0
- package/benching.md +106 -0
- package/eslint.config.js +5 -2
- package/examples/basic.js +130 -88
- package/examples/manual-streaming.js +81 -0
- package/index.js +355 -84
- package/package.json +13 -12
- package/test/basic.test.js +264 -16
- package/test/custom-headers.test.js +0 -6
- package/test/imports.test.js +50 -0
- package/test/lifecycle.test.js +0 -10
- package/test/manual.test.js +205 -0
- package/test/send-headers.test.js +263 -0
- package/types/index.d.ts +56 -11
- package/types/index.tst.ts +233 -0
- package/.github/.stale.yml +0 -21
- package/.github/tests_checker.yml +0 -8
- package/.github/workflows/package-manager-ci.yml +0 -24
- package/test-types/index.test-d.ts +0 -192
|
@@ -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
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Readable } from 'stream'
|
|
2
1
|
import { FastifyPluginAsync } from 'fastify'
|
|
2
|
+
import { Readable } from 'stream'
|
|
3
3
|
|
|
4
4
|
declare module 'fastify' {
|
|
5
5
|
interface FastifyReply {
|
|
@@ -7,13 +7,44 @@ declare module 'fastify' {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
interface RouteShorthandOptions {
|
|
10
|
-
sse?:
|
|
11
|
-
heartbeat?: boolean
|
|
12
|
-
serializer?: (data: any) => string
|
|
13
|
-
}
|
|
10
|
+
sse?: SSERouteOptions
|
|
14
11
|
}
|
|
15
12
|
}
|
|
16
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Declares how a route handles the SSE/non-SSE split.
|
|
16
|
+
*
|
|
17
|
+
* - `'only'` — SSE-only. Lenient gate: any spec-compliant Accept admits
|
|
18
|
+
* SSE. Clients that explicitly refuse SSE receive 406 Not Acceptable.
|
|
19
|
+
* - `'dual'` — Route serves both SSE and non-SSE on the same handler.
|
|
20
|
+
* Strict gate: only an explicit `text/event-stream` token admits SSE;
|
|
21
|
+
* the handler must branch on `reply.sse` to serve other clients.
|
|
22
|
+
* - `'manual'` — No Accept negotiation. `reply.sse` is always attached and
|
|
23
|
+
* the handler decides at runtime whether to stream (by calling
|
|
24
|
+
* `reply.sse.*`) or to return a normal response. Useful for streaming
|
|
25
|
+
* APIs that signal streaming via the request body (e.g. OpenAI-style
|
|
26
|
+
* `{ stream: true }`) rather than the Accept header. The connection is
|
|
27
|
+
* closed automatically when the handler resolves or throws.
|
|
28
|
+
*/
|
|
29
|
+
export type SSERouteKind = 'only' | 'dual' | 'manual'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Per-route SSE options.
|
|
33
|
+
*
|
|
34
|
+
* Accepted values:
|
|
35
|
+
* - `true` — Back-compat. Routes like `'dual'` for gate behavior; on the
|
|
36
|
+
* fallback path the plugin rethrows with a message naming `'only'` as
|
|
37
|
+
* the fix if the handler tries to use `reply.sse`.
|
|
38
|
+
* - `'only'` / `'dual'` / `'manual'` — Shorthand for the matching kind.
|
|
39
|
+
* - Object form — Same kinds via `kind`, plus per-route option overrides.
|
|
40
|
+
* `kind` omitted = back-compat behavior, equivalent to `sse: true`.
|
|
41
|
+
*/
|
|
42
|
+
export type SSERouteOptions = boolean | SSERouteKind | {
|
|
43
|
+
kind?: SSERouteKind
|
|
44
|
+
heartbeat?: boolean
|
|
45
|
+
serializer?: (data: any) => string
|
|
46
|
+
}
|
|
47
|
+
|
|
17
48
|
export interface SSEPluginOptions {
|
|
18
49
|
/**
|
|
19
50
|
* Interval in milliseconds for sending heartbeat comments
|
|
@@ -51,10 +82,10 @@ export interface SSEMessage {
|
|
|
51
82
|
retry?: number
|
|
52
83
|
}
|
|
53
84
|
|
|
54
|
-
export type SSESource =
|
|
55
|
-
| SSEMessage
|
|
56
|
-
| string
|
|
57
|
-
| Buffer
|
|
85
|
+
export type SSESource =
|
|
86
|
+
| SSEMessage
|
|
87
|
+
| string
|
|
88
|
+
| Buffer
|
|
58
89
|
| Readable
|
|
59
90
|
| AsyncIterable<SSEMessage | string | Buffer>
|
|
60
91
|
|
|
@@ -98,8 +129,22 @@ export interface SSEReplyInterface {
|
|
|
98
129
|
* Check if the connection is still active
|
|
99
130
|
*/
|
|
100
131
|
readonly isConnected: boolean
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Check if the connection should be kept alive after handler completion
|
|
135
|
+
*/
|
|
136
|
+
readonly shouldKeepAlive: boolean
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Send HTTP headers for the SSE response if not already sent.
|
|
140
|
+
* This method ensures headers set via reply.header() are transferred
|
|
141
|
+
* to the raw response before calling writeHead(200).
|
|
142
|
+
* Called automatically before the first SSE data is sent, but can
|
|
143
|
+
* also be called manually if needed.
|
|
144
|
+
*/
|
|
145
|
+
sendHeaders(): void
|
|
101
146
|
}
|
|
102
147
|
|
|
103
|
-
declare const fastifySSE: FastifyPluginAsync<SSEPluginOptions>
|
|
148
|
+
export declare const fastifySSE: FastifyPluginAsync<SSEPluginOptions>
|
|
104
149
|
|
|
105
|
-
export default fastifySSE
|
|
150
|
+
export default fastifySSE
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify'
|
|
2
|
+
import fastifySSE, { SSEPluginOptions, SSEMessage, SSESource, SSEReplyInterface } from '.'
|
|
3
|
+
import { Readable } from 'stream'
|
|
4
|
+
import { expect } from 'tstyche'
|
|
5
|
+
|
|
6
|
+
// Test plugin registration
|
|
7
|
+
const app: FastifyInstance = fastify()
|
|
8
|
+
|
|
9
|
+
// Test plugin options - app.register returns the app instance
|
|
10
|
+
expect(app.register(fastifySSE)).type.toBeAssignableTo<FastifyInstance>()
|
|
11
|
+
expect(app.register(fastifySSE, {})).type.toBeAssignableTo<FastifyInstance>()
|
|
12
|
+
expect(app.register(fastifySSE, {
|
|
13
|
+
heartbeatInterval: 10000
|
|
14
|
+
})).type.toBeAssignableTo<FastifyInstance>()
|
|
15
|
+
expect(app.register(fastifySSE, {
|
|
16
|
+
serializer: (data: any) => JSON.stringify(data)
|
|
17
|
+
})).type.toBeAssignableTo<FastifyInstance>()
|
|
18
|
+
expect(app.register(fastifySSE, {
|
|
19
|
+
heartbeatInterval: 0,
|
|
20
|
+
serializer: (data: any) => String(data)
|
|
21
|
+
})).type.toBeAssignableTo<FastifyInstance>()
|
|
22
|
+
|
|
23
|
+
// Test invalid plugin options
|
|
24
|
+
expect(app.register).type.not.toBeCallableWith(fastifySSE, {
|
|
25
|
+
heartbeatInterval: 'invalid'
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
expect(app.register).type.not.toBeCallableWith(fastifySSE, {
|
|
29
|
+
serializer: 123
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
expect(app.register).type.not.toBeCallableWith(fastifySSE, {
|
|
33
|
+
unknownOption: true
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// Test route options
|
|
37
|
+
app.get('/sse', { sse: true }, async (request, reply) => {
|
|
38
|
+
expect(reply.sse).type.toBe<SSEReplyInterface>()
|
|
39
|
+
return reply.sse.send({ data: 'test' })
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
app.get('/sse-with-options', {
|
|
43
|
+
sse: {
|
|
44
|
+
heartbeat: true,
|
|
45
|
+
serializer: (data: any) => JSON.stringify(data)
|
|
46
|
+
}
|
|
47
|
+
}, async (request, reply) => {
|
|
48
|
+
expect(reply.sse).type.toBe<SSEReplyInterface>()
|
|
49
|
+
return reply.sse.send({ data: 'test' })
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
// Test SSE message types
|
|
53
|
+
const message: SSEMessage = {
|
|
54
|
+
data: 'test'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const fullMessage: SSEMessage = {
|
|
58
|
+
id: '123',
|
|
59
|
+
event: 'custom',
|
|
60
|
+
data: { foo: 'bar' },
|
|
61
|
+
retry: 5000
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
expect(message.id).type.toBe<string | undefined>()
|
|
65
|
+
expect(message.event).type.toBe<string | undefined>()
|
|
66
|
+
expect(message.data).type.toBe<any>()
|
|
67
|
+
expect(message.retry).type.toBe<number | undefined>()
|
|
68
|
+
expect(fullMessage).type.toBeAssignableTo<SSEMessage>()
|
|
69
|
+
|
|
70
|
+
// Test SSE sources
|
|
71
|
+
const stringSource: SSESource = 'hello'
|
|
72
|
+
expect(stringSource).type.toBeAssignableTo<SSESource>()
|
|
73
|
+
|
|
74
|
+
const bufferSource: SSESource = Buffer.from('hello')
|
|
75
|
+
expect(bufferSource).type.toBeAssignableTo<SSESource>()
|
|
76
|
+
|
|
77
|
+
const messageSource: SSESource = { data: 'test' }
|
|
78
|
+
expect(messageSource).type.toBeAssignableTo<SSESource>()
|
|
79
|
+
|
|
80
|
+
const streamSource: SSESource = new Readable()
|
|
81
|
+
expect(streamSource).type.toBeAssignableTo<SSESource>()
|
|
82
|
+
|
|
83
|
+
async function * asyncGenerator (): AsyncIterable<SSEMessage> {
|
|
84
|
+
yield { data: 'test' }
|
|
85
|
+
}
|
|
86
|
+
const asyncIterableSource: SSESource = asyncGenerator()
|
|
87
|
+
expect(asyncIterableSource).type.toBeAssignableTo<SSESource>()
|
|
88
|
+
|
|
89
|
+
// Test SSE reply interface
|
|
90
|
+
app.get('/test-reply', { sse: true }, async (request, reply) => {
|
|
91
|
+
// Test properties
|
|
92
|
+
expect(reply.sse.lastEventId).type.toBe<string | null>()
|
|
93
|
+
expect(reply.sse.isConnected).type.toBe<boolean>()
|
|
94
|
+
|
|
95
|
+
// Test send method with different sources
|
|
96
|
+
expect(reply.sse.send('string')).type.toBe<Promise<void>>()
|
|
97
|
+
expect(reply.sse.send(Buffer.from('buffer'))).type.toBe<Promise<void>>()
|
|
98
|
+
expect(reply.sse.send({ data: 'object' })).type.toBe<Promise<void>>()
|
|
99
|
+
expect(reply.sse.send(new Readable())).type.toBe<Promise<void>>()
|
|
100
|
+
expect(reply.sse.send(asyncGenerator())).type.toBe<Promise<void>>()
|
|
101
|
+
|
|
102
|
+
// Test stream method
|
|
103
|
+
expect(reply.sse.stream()).type.toBe<NodeJS.WritableStream>()
|
|
104
|
+
|
|
105
|
+
// Test keepAlive method
|
|
106
|
+
expect(reply.sse.keepAlive()).type.toBe<void>()
|
|
107
|
+
|
|
108
|
+
// Test close method
|
|
109
|
+
expect(reply.sse.close()).type.toBe<void>()
|
|
110
|
+
|
|
111
|
+
// Test replay method
|
|
112
|
+
expect(reply.sse.replay(async (lastEventId) => {
|
|
113
|
+
expect(lastEventId).type.toBe<string>()
|
|
114
|
+
})).type.toBe<Promise<void>>()
|
|
115
|
+
|
|
116
|
+
// Test onClose method
|
|
117
|
+
expect(reply.sse.onClose(() => {
|
|
118
|
+
console.log('closed')
|
|
119
|
+
})).type.toBe<void>()
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
// Test that reply.sse is available only on sse routes
|
|
123
|
+
app.get('/no-sse', async (request, reply) => {
|
|
124
|
+
// Note: reply.sse is added at runtime by the plugin, so we can't test its absence at compile time
|
|
125
|
+
// This is expected behavior for Fastify plugins that decorate at runtime
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
// Test complex SSE message scenarios
|
|
129
|
+
const complexMessage: SSEMessage = {
|
|
130
|
+
id: '456',
|
|
131
|
+
event: 'update',
|
|
132
|
+
data: {
|
|
133
|
+
nested: {
|
|
134
|
+
property: 'value',
|
|
135
|
+
array: [1, 2, 3]
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
retry: 10000
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
expect(complexMessage).type.toBeAssignableTo<SSESource>()
|
|
142
|
+
|
|
143
|
+
// Test async iterator types
|
|
144
|
+
async function * typedAsyncGenerator (): AsyncIterable<string> {
|
|
145
|
+
yield 'test'
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function * mixedAsyncGenerator (): AsyncIterable<SSEMessage | string | Buffer> {
|
|
149
|
+
yield { data: 'message' }
|
|
150
|
+
yield 'string'
|
|
151
|
+
yield Buffer.from('buffer')
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
expect(typedAsyncGenerator()).type.toBeAssignableTo<SSESource>()
|
|
155
|
+
expect(mixedAsyncGenerator()).type.toBeAssignableTo<SSESource>()
|
|
156
|
+
|
|
157
|
+
// Test plugin options interface
|
|
158
|
+
const pluginOptions: SSEPluginOptions = {
|
|
159
|
+
heartbeatInterval: 15000,
|
|
160
|
+
serializer: (data) => {
|
|
161
|
+
expect(data).type.toBe<any>()
|
|
162
|
+
return JSON.stringify(data)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
expect(pluginOptions).type.toBeAssignableTo<SSEPluginOptions>()
|
|
166
|
+
|
|
167
|
+
// Test serializer function type
|
|
168
|
+
const customSerializer = (data: any): string => {
|
|
169
|
+
if (typeof data === 'object') {
|
|
170
|
+
return JSON.stringify(data)
|
|
171
|
+
}
|
|
172
|
+
return String(data)
|
|
173
|
+
}
|
|
174
|
+
expect({
|
|
175
|
+
serializer: customSerializer
|
|
176
|
+
}).type.toBeAssignableTo<SSEPluginOptions>()
|
|
177
|
+
|
|
178
|
+
// Test route options with boolean
|
|
179
|
+
const routeOptions1: RouteShorthandOptions = {
|
|
180
|
+
sse: true
|
|
181
|
+
}
|
|
182
|
+
expect(routeOptions1).type.toBeAssignableTo<RouteShorthandOptions>()
|
|
183
|
+
|
|
184
|
+
const routeOptions2: RouteShorthandOptions = {
|
|
185
|
+
sse: false
|
|
186
|
+
}
|
|
187
|
+
expect(routeOptions2).type.toBeAssignableTo<RouteShorthandOptions>()
|
|
188
|
+
|
|
189
|
+
const routeOptions3: RouteShorthandOptions = {
|
|
190
|
+
sse: {
|
|
191
|
+
heartbeat: false,
|
|
192
|
+
serializer: (data) => String(data)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
expect(routeOptions3).type.toBeAssignableTo<RouteShorthandOptions>()
|
|
196
|
+
|
|
197
|
+
// Test invalid route options - these tests verify type checking
|
|
198
|
+
app.get('/invalid', {
|
|
199
|
+
// @ts-expect-error Type '"invalid"' is not assignable to type 'SSERouteOptions'
|
|
200
|
+
sse: 'invalid'
|
|
201
|
+
}, async (request, reply) => { })
|
|
202
|
+
app.get('/invalid2', {
|
|
203
|
+
sse: {
|
|
204
|
+
// @ts-expect-error 'unknownOption' does not exist in type
|
|
205
|
+
unknownOption: true
|
|
206
|
+
}
|
|
207
|
+
}, async (request, reply) => { })
|
|
208
|
+
|
|
209
|
+
// Test valid kind shorthand
|
|
210
|
+
app.get('/only', { sse: 'only' }, async (request, reply) => {
|
|
211
|
+
return reply.sse.send({ data: 'hello' })
|
|
212
|
+
})
|
|
213
|
+
app.get('/dual', { sse: 'dual' }, async (request, reply) => {
|
|
214
|
+
if (reply.sse) return reply.sse.send({ data: 'hello' })
|
|
215
|
+
return { fallback: true }
|
|
216
|
+
})
|
|
217
|
+
app.post('/manual', { sse: 'manual' }, async (request, reply) => {
|
|
218
|
+
const body = request.body as { stream?: boolean }
|
|
219
|
+
if (body.stream) return reply.sse.send({ data: 'hello' })
|
|
220
|
+
return { fallback: true }
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
// Test object form with kind
|
|
224
|
+
app.get('/only-obj', {
|
|
225
|
+
sse: { kind: 'only', heartbeat: false }
|
|
226
|
+
}, async (request, reply) => {
|
|
227
|
+
return reply.sse.send({ data: 'hello' })
|
|
228
|
+
})
|
|
229
|
+
app.post('/manual-obj', {
|
|
230
|
+
sse: { kind: 'manual', heartbeat: false }
|
|
231
|
+
}, async (request, reply) => {
|
|
232
|
+
return reply.sse.send({ data: 'hello' })
|
|
233
|
+
})
|
package/.github/.stale.yml
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# Number of days of inactivity before an issue becomes stale
|
|
2
|
-
daysUntilStale: 15
|
|
3
|
-
# Number of days of inactivity before a stale issue is closed
|
|
4
|
-
daysUntilClose: 7
|
|
5
|
-
# Issues with these labels will never be considered stale
|
|
6
|
-
exemptLabels:
|
|
7
|
-
- "discussion"
|
|
8
|
-
- "feature request"
|
|
9
|
-
- "bug"
|
|
10
|
-
- "help wanted"
|
|
11
|
-
- "plugin suggestion"
|
|
12
|
-
- "good first issue"
|
|
13
|
-
# Label to use when marking an issue as stale
|
|
14
|
-
staleLabel: stale
|
|
15
|
-
# Comment to post when marking an issue as stale. Set to `false` to disable
|
|
16
|
-
markComment: >
|
|
17
|
-
This issue has been automatically marked as stale because it has not had
|
|
18
|
-
recent activity. It will be closed if no further activity occurs. Thank you
|
|
19
|
-
for your contributions.
|
|
20
|
-
# Comment to post when closing a stale issue. Set to `false` to disable
|
|
21
|
-
closeComment: false
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
name: package-manager-ci
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- main
|
|
7
|
-
- next
|
|
8
|
-
- 'v*'
|
|
9
|
-
paths-ignore:
|
|
10
|
-
- 'docs/**'
|
|
11
|
-
- '*.md'
|
|
12
|
-
pull_request:
|
|
13
|
-
paths-ignore:
|
|
14
|
-
- 'docs/**'
|
|
15
|
-
- '*.md'
|
|
16
|
-
|
|
17
|
-
permissions:
|
|
18
|
-
contents: read
|
|
19
|
-
|
|
20
|
-
jobs:
|
|
21
|
-
test:
|
|
22
|
-
permissions:
|
|
23
|
-
contents: read
|
|
24
|
-
uses: fastify/workflows/.github/workflows/plugins-ci-package-manager.yml@v5
|