@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.
@@ -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',
@@ -309,3 +293,267 @@ test('reply.sse.stream() for pipeline operations', async (t) => {
309
293
  assert.ok(body.includes('id: 3'))
310
294
  assert.ok(body.includes('data: "third"'))
311
295
  })
296
+
297
+ // -----------------------------------------------------------------------
298
+ // sse: 'only' (SSE-only routes)
299
+ // -----------------------------------------------------------------------
300
+
301
+ test("sse: 'only' admits SSE for */*", async (t) => {
302
+ const fastify = Fastify({ logger: false })
303
+ t.after(async () => { await fastify.close() })
304
+ await fastify.register(fastifySSE)
305
+
306
+ fastify.get('/events', { sse: 'only' }, async (request, reply) => {
307
+ await reply.sse.send({ data: 'hello' })
308
+ })
309
+
310
+ const response = await fastify.inject({
311
+ method: 'GET',
312
+ url: '/events',
313
+ headers: { accept: '*/*' }
314
+ })
315
+ assert.strictEqual(response.statusCode, 200)
316
+ assert.strictEqual(response.headers['content-type'], 'text/event-stream')
317
+ assert.ok(response.body.includes('data: "hello"'))
318
+ })
319
+
320
+ test("sse: 'only' admits SSE for missing Accept", async (t) => {
321
+ const fastify = Fastify({ logger: false })
322
+ t.after(async () => { await fastify.close() })
323
+ await fastify.register(fastifySSE)
324
+
325
+ fastify.get('/events', { sse: 'only' }, async (request, reply) => {
326
+ await reply.sse.send({ data: 'hello' })
327
+ })
328
+
329
+ const response = await fastify.inject({ method: 'GET', url: '/events' })
330
+ assert.strictEqual(response.statusCode, 200)
331
+ assert.strictEqual(response.headers['content-type'], 'text/event-stream')
332
+ assert.ok(response.body.includes('data: "hello"'))
333
+ })
334
+
335
+ test("sse: 'only' admits SSE for text/*", async (t) => {
336
+ const fastify = Fastify({ logger: false })
337
+ t.after(async () => { await fastify.close() })
338
+ await fastify.register(fastifySSE)
339
+
340
+ fastify.get('/events', { sse: 'only' }, async (request, reply) => {
341
+ await reply.sse.send({ data: 'hello' })
342
+ })
343
+
344
+ const response = await fastify.inject({
345
+ method: 'GET',
346
+ url: '/events',
347
+ headers: { accept: 'text/*' }
348
+ })
349
+ assert.strictEqual(response.headers['content-type'], 'text/event-stream')
350
+ })
351
+
352
+ test("sse: 'only' returns 406 when client explicitly refuses SSE", async (t) => {
353
+ const fastify = Fastify({ logger: false })
354
+ t.after(async () => { await fastify.close() })
355
+ await fastify.register(fastifySSE)
356
+
357
+ fastify.get('/events', { sse: 'only' }, async (request, reply) => {
358
+ await reply.sse.send({ data: 'hello' })
359
+ })
360
+
361
+ const response = await fastify.inject({
362
+ method: 'GET',
363
+ url: '/events',
364
+ headers: { accept: 'application/json' }
365
+ })
366
+ assert.strictEqual(response.statusCode, 406)
367
+ assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
368
+ const body = JSON.parse(response.body)
369
+ assert.strictEqual(body.statusCode, 406)
370
+ assert.strictEqual(body.error, 'Not Acceptable')
371
+ assert.match(body.message, /text\/event-stream/)
372
+ })
373
+
374
+ test("sse: 'only' returns 406 when client sends text/event-stream;q=0", async (t) => {
375
+ const fastify = Fastify({ logger: false })
376
+ t.after(async () => { await fastify.close() })
377
+ await fastify.register(fastifySSE)
378
+
379
+ fastify.get('/events', { sse: 'only' }, async (request, reply) => {
380
+ await reply.sse.send({ data: 'hello' })
381
+ })
382
+
383
+ const response = await fastify.inject({
384
+ method: 'GET',
385
+ url: '/events',
386
+ headers: { accept: '*/*, text/event-stream;q=0' }
387
+ })
388
+ assert.strictEqual(response.statusCode, 406)
389
+ })
390
+
391
+ test("sse: { kind: 'only', heartbeat: false } object form is supported", async (t) => {
392
+ const fastify = Fastify({ logger: false })
393
+ t.after(async () => { await fastify.close() })
394
+ await fastify.register(fastifySSE)
395
+
396
+ fastify.get('/events', {
397
+ sse: { kind: 'only', heartbeat: false }
398
+ }, async (request, reply) => {
399
+ await reply.sse.send({ data: 'hello' })
400
+ })
401
+
402
+ const wildcard = await fastify.inject({
403
+ method: 'GET',
404
+ url: '/events',
405
+ headers: { accept: '*/*' }
406
+ })
407
+ assert.strictEqual(wildcard.headers['content-type'], 'text/event-stream')
408
+
409
+ const refused = await fastify.inject({
410
+ method: 'GET',
411
+ url: '/events',
412
+ headers: { accept: 'application/json' }
413
+ })
414
+ assert.strictEqual(refused.statusCode, 406)
415
+ })
416
+
417
+ // -----------------------------------------------------------------------
418
+ // sse: 'dual' (explicit dual-mode routes)
419
+ // -----------------------------------------------------------------------
420
+
421
+ test("sse: 'dual' routes only explicit text/event-stream to SSE", async (t) => {
422
+ const fastify = Fastify({ logger: false })
423
+ t.after(async () => { await fastify.close() })
424
+ await fastify.register(fastifySSE)
425
+
426
+ fastify.get('/data', { sse: 'dual' }, async (request, reply) => {
427
+ if (reply.sse) {
428
+ await reply.sse.send({ data: 'streamed' })
429
+ } else {
430
+ return { message: 'json' }
431
+ }
432
+ })
433
+
434
+ // */* → falls through to JSON
435
+ const wildcard = await fastify.inject({
436
+ method: 'GET',
437
+ url: '/data',
438
+ headers: { accept: '*/*' }
439
+ })
440
+ assert.strictEqual(wildcard.headers['content-type'], 'application/json; charset=utf-8')
441
+ assert.deepStrictEqual(JSON.parse(wildcard.body), { message: 'json' })
442
+
443
+ // missing → JSON
444
+ const missing = await fastify.inject({ method: 'GET', url: '/data' })
445
+ assert.deepStrictEqual(JSON.parse(missing.body), { message: 'json' })
446
+
447
+ // application/json → JSON
448
+ const jsonResponse = await fastify.inject({
449
+ method: 'GET',
450
+ url: '/data',
451
+ headers: { accept: 'application/json' }
452
+ })
453
+ assert.deepStrictEqual(JSON.parse(jsonResponse.body), { message: 'json' })
454
+
455
+ // text/event-stream → SSE
456
+ const explicit = await fastify.inject({
457
+ method: 'GET',
458
+ url: '/data',
459
+ headers: { accept: 'text/event-stream' }
460
+ })
461
+ assert.strictEqual(explicit.headers['content-type'], 'text/event-stream')
462
+ assert.ok(explicit.body.includes('data: "streamed"'))
463
+ })
464
+
465
+ // -----------------------------------------------------------------------
466
+ // sse: true (legacy back-compat + explanatory error on misuse)
467
+ // -----------------------------------------------------------------------
468
+
469
+ test("sse: true behaves like 'dual' for routing (back-compat)", async (t) => {
470
+ const fastify = Fastify({ logger: false })
471
+ t.after(async () => { await fastify.close() })
472
+ await fastify.register(fastifySSE)
473
+
474
+ fastify.get('/data', { sse: true }, async (request, reply) => {
475
+ const acceptHeader = request.headers.accept || ''
476
+ if (acceptHeader.includes('text/event-stream')) {
477
+ await reply.sse.send({ data: 'streamed' })
478
+ } else {
479
+ return { message: 'json' }
480
+ }
481
+ })
482
+
483
+ // */* → falls through (strict gate); handler returns JSON
484
+ const wildcard = await fastify.inject({
485
+ method: 'GET',
486
+ url: '/data',
487
+ headers: { accept: '*/*' }
488
+ })
489
+ assert.deepStrictEqual(JSON.parse(wildcard.body), { message: 'json' })
490
+
491
+ // text/event-stream → SSE
492
+ const explicit = await fastify.inject({
493
+ method: 'GET',
494
+ url: '/data',
495
+ headers: { accept: 'text/event-stream' }
496
+ })
497
+ assert.strictEqual(explicit.headers['content-type'], 'text/event-stream')
498
+ })
499
+
500
+ test('sse: true with SSE-only handler emits an explanatory error on misuse', async (t) => {
501
+ const fastify = Fastify({ logger: false })
502
+ t.after(async () => { await fastify.close() })
503
+ await fastify.register(fastifySSE)
504
+
505
+ // Handler is SSE-only — calls reply.sse.send unconditionally — but
506
+ // registered with the legacy `sse: true` flag. When a wildcard Accept
507
+ // arrives, the strict gate falls through, reply.sse is undefined, and the
508
+ // handler trips the TypeError. The plugin should rethrow with guidance to
509
+ // switch to `sse: 'only'`.
510
+ fastify.get('/events', { sse: true }, async (request, reply) => {
511
+ await reply.sse.send({ data: 'hello' })
512
+ })
513
+
514
+ const response = await fastify.inject({
515
+ method: 'GET',
516
+ url: '/events',
517
+ headers: { accept: '*/*' }
518
+ })
519
+
520
+ // The rethrown error reaches Fastify's default error handler.
521
+ assert.strictEqual(response.statusCode, 500)
522
+ assert.match(response.body, /sse: 'only'/)
523
+ })
524
+
525
+ test('sse: true with object form (no kind) is treated as legacy', async (t) => {
526
+ const fastify = Fastify({ logger: false })
527
+ t.after(async () => { await fastify.close() })
528
+ await fastify.register(fastifySSE)
529
+
530
+ // No `kind` field — back-compat with existing `{ heartbeat, serializer }` users.
531
+ fastify.get('/events', {
532
+ sse: { heartbeat: false }
533
+ }, async (request, reply) => {
534
+ const acceptHeader = request.headers.accept || ''
535
+ if (acceptHeader.includes('text/event-stream')) {
536
+ await reply.sse.send({ data: 'streamed' })
537
+ } else {
538
+ return { message: 'json' }
539
+ }
540
+ })
541
+
542
+ const wildcard = await fastify.inject({
543
+ method: 'GET',
544
+ url: '/events',
545
+ headers: { accept: '*/*' }
546
+ })
547
+ assert.deepStrictEqual(JSON.parse(wildcard.body), { message: 'json' })
548
+ })
549
+
550
+ test('unknown sse kind throws at registration', async (t) => {
551
+ const fastify = Fastify({ logger: false })
552
+ t.after(async () => { await fastify.close() })
553
+ await fastify.register(fastifySSE)
554
+
555
+ assert.throws(
556
+ () => fastify.get('/events', { sse: { kind: 'maybe' } }, async () => {}),
557
+ /unknown sse kind 'maybe'/
558
+ )
559
+ })
@@ -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',
@@ -0,0 +1,50 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('node:test')
4
+ const { strict: assert } = require('node:assert')
5
+ const Fastify = require('fastify')
6
+
7
+ async function runTest (SSE, t) {
8
+ const fastify = Fastify({ logger: false })
9
+
10
+ t.after(async () => {
11
+ await fastify.close()
12
+ })
13
+
14
+ await fastify.register(SSE)
15
+ fastify.get('/events', { sse: true }, async (request, reply) => {
16
+ await reply.sse.send({ data: 'hello world' })
17
+ })
18
+
19
+ const response = await fastify.inject({
20
+ method: 'GET',
21
+ url: '/events',
22
+ headers: {
23
+ accept: 'text/event-stream'
24
+ }
25
+ })
26
+
27
+ assert.strictEqual(response.statusCode, 200)
28
+ assert.strictEqual(response.headers['content-type'], 'text/event-stream')
29
+ assert.strictEqual(response.headers['cache-control'], 'no-cache')
30
+ assert.strictEqual(response.headers.connection, 'keep-alive')
31
+
32
+ const { body: responseBody } = response
33
+ assert.ok(responseBody.includes('data: "hello world"'))
34
+ assert.ok(responseBody.endsWith('\n\n'))
35
+ }
36
+
37
+ test('module import', async (t) => {
38
+ const fastifySSE = require('../index.js')
39
+ await runTest(fastifySSE, t)
40
+ })
41
+
42
+ test('default import', async (t) => {
43
+ const { default: fastifySSE } = require('../index.js')
44
+ await runTest(fastifySSE, t)
45
+ })
46
+
47
+ test('named import', async (t) => {
48
+ const { fastifySSE } = require('../index.js')
49
+ await runTest(fastifySSE, t)
50
+ })
@@ -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,205 @@
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
+ // `sse: 'manual'` attaches reply.sse without Accept-header negotiation and
9
+ // lets the handler decide at runtime whether to stream. This mirrors the way
10
+ // OpenAI-style / LLM APIs signal streaming through the request body rather
11
+ // than the Accept header.
12
+
13
+ test('manual mode streams when the handler chooses to (no Accept header)', async (t) => {
14
+ const fastify = Fastify({ logger: false })
15
+ t.after(() => fastify.close())
16
+
17
+ await fastify.register(fastifySSE)
18
+
19
+ fastify.post('/v1/chat', { sse: 'manual' }, async (request, reply) => {
20
+ if (request.body.stream) {
21
+ await reply.sse.send({ data: 'chunk-1' })
22
+ await reply.sse.send({ data: 'chunk-2' })
23
+ return
24
+ }
25
+ return { content: 'full response' }
26
+ })
27
+
28
+ const streamed = await fastify.inject({
29
+ method: 'POST',
30
+ url: '/v1/chat',
31
+ payload: { stream: true }
32
+ })
33
+
34
+ assert.strictEqual(streamed.statusCode, 200)
35
+ assert.strictEqual(streamed.headers['content-type'], 'text/event-stream')
36
+ assert.ok(streamed.body.includes('data: "chunk-1"'))
37
+ assert.ok(streamed.body.includes('data: "chunk-2"'))
38
+ })
39
+
40
+ test('manual mode falls back to a normal JSON response without streaming', async (t) => {
41
+ const fastify = Fastify({ logger: false })
42
+ t.after(() => fastify.close())
43
+
44
+ await fastify.register(fastifySSE)
45
+
46
+ fastify.post('/v1/chat', { sse: 'manual' }, async (request, reply) => {
47
+ if (request.body.stream) {
48
+ await reply.sse.send({ data: 'chunk' })
49
+ return
50
+ }
51
+ return { content: 'full response' }
52
+ })
53
+
54
+ const json = await fastify.inject({
55
+ method: 'POST',
56
+ url: '/v1/chat',
57
+ payload: { stream: false }
58
+ })
59
+
60
+ assert.strictEqual(json.statusCode, 200)
61
+ assert.match(json.headers['content-type'], /application\/json/)
62
+ assert.notStrictEqual(json.headers['content-type'], 'text/event-stream')
63
+ assert.deepStrictEqual(json.json(), { content: 'full response' })
64
+ })
65
+
66
+ test('manual mode streams regardless of an Accept header that refuses SSE', async (t) => {
67
+ const fastify = Fastify({ logger: false })
68
+ t.after(() => fastify.close())
69
+
70
+ await fastify.register(fastifySSE)
71
+
72
+ fastify.get('/events', { sse: 'manual' }, async (request, reply) => {
73
+ await reply.sse.send({ data: 'hello' })
74
+ })
75
+
76
+ // Accept: application/json would 406 on an `only` route and skip SSE on a
77
+ // `dual` route; `manual` ignores it entirely.
78
+ const response = await fastify.inject({
79
+ method: 'GET',
80
+ url: '/events',
81
+ headers: { accept: 'application/json' }
82
+ })
83
+
84
+ assert.strictEqual(response.statusCode, 200)
85
+ assert.strictEqual(response.headers['content-type'], 'text/event-stream')
86
+ assert.ok(response.body.includes('data: "hello"'))
87
+ })
88
+
89
+ test('manual mode closes the stream after the handler resolves', async (t) => {
90
+ const fastify = Fastify({ logger: false })
91
+ t.after(() => fastify.close())
92
+
93
+ await fastify.register(fastifySSE)
94
+
95
+ let closed = false
96
+ fastify.get('/events', { sse: 'manual' }, async (request, reply) => {
97
+ reply.sse.onClose(() => { closed = true })
98
+ await reply.sse.send({ data: 'one' })
99
+ })
100
+
101
+ const response = await fastify.inject({
102
+ method: 'GET',
103
+ url: '/events'
104
+ })
105
+
106
+ assert.strictEqual(response.statusCode, 200)
107
+ assert.ok(response.body.includes('data: "one"'))
108
+ // inject resolves only once the response stream has ended.
109
+ assert.strictEqual(closed, true)
110
+ })
111
+
112
+ test('manual mode closes the stream when the handler throws', async (t) => {
113
+ const fastify = Fastify({ logger: false })
114
+ t.after(() => fastify.close())
115
+
116
+ await fastify.register(fastifySSE)
117
+
118
+ let closed = false
119
+ fastify.get('/events', { sse: 'manual' }, async (request, reply) => {
120
+ reply.sse.onClose(() => { closed = true })
121
+ await reply.sse.send({ data: 'before error' })
122
+ throw new Error('boom')
123
+ })
124
+
125
+ const response = await fastify.inject({
126
+ method: 'GET',
127
+ url: '/events'
128
+ })
129
+
130
+ // Headers were already committed as SSE before the throw, so the partial
131
+ // stream is flushed and the connection is closed during cleanup.
132
+ assert.ok(response.body.includes('data: "before error"'))
133
+ assert.strictEqual(closed, true)
134
+ })
135
+
136
+ test('manual mode respects keepAlive() (no auto-close after handler)', async (t) => {
137
+ const fastify = Fastify({ logger: false })
138
+ t.after(() => fastify.close())
139
+
140
+ await fastify.register(fastifySSE)
141
+
142
+ fastify.get('/events', { sse: 'manual' }, async (request, reply) => {
143
+ reply.sse.keepAlive()
144
+ await reply.sse.send({ data: 'first' })
145
+
146
+ setTimeout(async () => {
147
+ await reply.sse.send({ data: 'second' })
148
+ reply.sse.close()
149
+ }, 30)
150
+ })
151
+
152
+ const response = await fastify.inject({
153
+ method: 'GET',
154
+ url: '/events'
155
+ })
156
+
157
+ assert.strictEqual(response.statusCode, 200)
158
+ assert.ok(response.body.includes('data: "first"'))
159
+ assert.ok(response.body.includes('data: "second"'))
160
+ })
161
+
162
+ test("manual mode via object form ({ kind: 'manual' })", async (t) => {
163
+ const fastify = Fastify({ logger: false })
164
+ t.after(() => fastify.close())
165
+
166
+ await fastify.register(fastifySSE)
167
+
168
+ fastify.get('/events', {
169
+ sse: { kind: 'manual', serializer: (data) => `CUSTOM:${JSON.stringify(data)}` }
170
+ }, async (request, reply) => {
171
+ await reply.sse.send({ data: 'test' })
172
+ })
173
+
174
+ const response = await fastify.inject({
175
+ method: 'GET',
176
+ url: '/events'
177
+ })
178
+
179
+ assert.strictEqual(response.statusCode, 200)
180
+ assert.ok(response.body.includes('CUSTOM:"test"'))
181
+ })
182
+
183
+ test('a non-SSE fallback response is not corrupted by a heartbeat', async (t) => {
184
+ const fastify = Fastify({ logger: false })
185
+ t.after(() => fastify.close())
186
+
187
+ // A 1ms heartbeat would fire during the handler if it were started eagerly.
188
+ await fastify.register(fastifySSE, { heartbeatInterval: 1 })
189
+
190
+ fastify.post('/v1/chat', { sse: 'manual' }, async (request, reply) => {
191
+ // Give the (deferred) heartbeat timer a chance to fire before returning.
192
+ await new Promise((resolve) => setTimeout(resolve, 20))
193
+ return { content: 'full response' }
194
+ })
195
+
196
+ const response = await fastify.inject({
197
+ method: 'POST',
198
+ url: '/v1/chat',
199
+ payload: { stream: false }
200
+ })
201
+
202
+ assert.strictEqual(response.statusCode, 200)
203
+ assert.match(response.headers['content-type'], /application\/json/)
204
+ assert.deepStrictEqual(response.json(), { content: 'full response' })
205
+ })