@fastify/sse 0.4.0 → 0.6.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.
@@ -2,12 +2,53 @@ version: 2
2
2
  updates:
3
3
  - package-ecosystem: "github-actions"
4
4
  directory: "/"
5
+ commit-message:
6
+ # Prefix all commit messages with "chore: "
7
+ prefix: "chore"
5
8
  schedule:
6
- interval: "monthly"
9
+ interval: "weekly"
10
+ cooldown:
11
+ default-days: 7
12
+ allow:
13
+ - dependency-name: "*"
14
+ update-types:
15
+ - "version-update:semver-major"
7
16
  open-pull-requests-limit: 10
8
17
 
9
18
  - package-ecosystem: "npm"
10
19
  directory: "/"
20
+ commit-message:
21
+ # Prefix all commit messages with "chore: "
22
+ prefix: "chore"
11
23
  schedule:
12
- interval: "monthly"
24
+ interval: "weekly"
25
+ cooldown:
26
+ default-days: 7
27
+ versioning-strategy: "increase-if-necessary"
28
+ allow:
29
+ - dependency-name: "*"
30
+ update-types:
31
+ - "version-update:semver-major"
32
+ ignore:
33
+ # TODO: remove ignore until neostandard support ESLint 10
34
+ - dependency-name: "eslint"
35
+ - dependency-name: "neostandard"
36
+ - dependency-name: "@stylistic/*"
37
+ - dependency-name: "typescript"
13
38
  open-pull-requests-limit: 10
39
+ groups:
40
+ # Production dependencies with breaking changes
41
+ dependencies:
42
+ dependency-type: "production"
43
+ # ESLint related dependencies
44
+ dev-dependencies-eslint:
45
+ patterns:
46
+ - "eslint"
47
+ - "neostandard"
48
+ - "@stylistic/*"
49
+ # TypeScript related dependencies
50
+ dev-dependencies-typescript:
51
+ patterns:
52
+ - "@types/*"
53
+ - "tstyche"
54
+ - "typescript"
@@ -14,6 +14,11 @@ on:
14
14
  - 'docs/**'
15
15
  - '*.md'
16
16
 
17
+ # This allows a subsequently queued workflow run to interrupt previous runs
18
+ concurrency:
19
+ group: "${{ github.workflow }}-${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
20
+ cancel-in-progress: true
21
+
17
22
  permissions:
18
23
  contents: read
19
24
 
@@ -22,7 +27,7 @@ jobs:
22
27
  permissions:
23
28
  contents: write
24
29
  pull-requests: write
25
- uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
30
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@2073dc8e1f9e172bf42daa3843c9dbd31af1e8cb # v6.0.0
26
31
  with:
27
32
  license-check: true
28
33
  lint: true
@@ -0,0 +1,19 @@
1
+ name: Lock Threads
2
+
3
+ on:
4
+ schedule:
5
+ - cron: '0 0 1 * *'
6
+ workflow_dispatch:
7
+
8
+ concurrency:
9
+ group: lock
10
+
11
+ permissions:
12
+ contents: read
13
+
14
+ jobs:
15
+ lock-threads:
16
+ permissions:
17
+ issues: write
18
+ pull-requests: write
19
+ uses: fastify/workflows/.github/workflows/lock-threads.yml@2073dc8e1f9e172bf42daa3843c9dbd31af1e8cb # v6.0.0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present Matteo Collina
4
+ Copyright (c) 2025-present The Fastify team <https://github.com/fastify/fastify#team>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # @fastify/sse
2
2
 
3
- [![NPM Version](https://img.shields.io/npm/v/@fastify/sse.svg)](https://npmjs.org/package/@fastify/sse)
3
+ [![NPM Version](https://img.shields.io/npm/v/@fastify/sse.svg?style=flat)](https://www.npmjs.com/package/@fastify/sse)
4
4
  [![CI](https://github.com/fastify/sse/workflows/CI/badge.svg)](https://github.com/fastify/sse/actions)
5
+ [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
5
6
 
6
7
  Server-Sent Events plugin for Fastify. Provides first-class SSE support with clean API integration, session management, and streaming capabilities.
7
8
 
@@ -33,11 +34,11 @@ await fastify.register(require('@fastify/sse'))
33
34
  fastify.get('/events', { sse: true }, async (request, reply) => {
34
35
  // Send a message
35
36
  await reply.sse.send({ data: 'Hello SSE!' })
36
-
37
+
37
38
  // Send with full options
38
39
  await reply.sse.send({
39
40
  id: '123',
40
- event: 'update',
41
+ event: 'update',
41
42
  data: { message: 'Hello World' },
42
43
  retry: 1000
43
44
  })
@@ -54,7 +55,7 @@ await fastify.listen({ port: 3000 })
54
55
  await fastify.register(require('@fastify/sse'), {
55
56
  // Optional: heartbeat interval in milliseconds (default: 30000)
56
57
  heartbeatInterval: 30000,
57
-
58
+
58
59
  // Optional: default serializer (default: JSON.stringify)
59
60
  serializer: (data) => JSON.stringify(data)
60
61
  })
@@ -62,14 +63,39 @@ await fastify.register(require('@fastify/sse'), {
62
63
 
63
64
  ### Route Configuration
64
65
 
65
- ```js
66
- // Enable SSE for a route
67
- fastify.get('/events', { sse: true }, handler)
66
+ Routes opt into SSE handling by setting the `sse` field. There are three
67
+ forms, each describing what the route serves:
68
68
 
69
- // With options
70
- fastify.get('/events', {
69
+ ```js
70
+ // SSE-only route: handler always streams. Clients that explicitly refuse
71
+ // SSE (e.g. `Accept: application/json`) receive 406 Not Acceptable.
72
+ // Ambiguous Accept headers (*/*, text/*, missing) admit SSE per RFC 9110.
73
+ fastify.get('/events', { sse: 'only' }, handler)
74
+
75
+ // Dual-mode route: the same handler serves both SSE and non-SSE clients.
76
+ // Only an explicit `text/event-stream` token routes to SSE; everything
77
+ // else falls through to the handler with `reply.sse` undefined, so the
78
+ // handler is expected to produce a non-SSE response in that branch.
79
+ fastify.get('/data', { sse: 'dual' }, handler)
80
+
81
+ // Manual route: no Accept negotiation. `reply.sse` is always attached and
82
+ // the handler decides at runtime whether to stream (by calling reply.sse.*)
83
+ // or to return a normal response. Useful for streaming APIs that signal
84
+ // streaming via the request body (e.g. OpenAI-style `{ stream: true }`)
85
+ // rather than the Accept header.
86
+ fastify.post('/v1/chat', { sse: 'manual' }, handler)
87
+
88
+ // Back-compat: `sse: true` behaves like `'dual'` for routing. If the
89
+ // handler is actually SSE-only and trips a TypeError on `reply.sse`
90
+ // because of a wildcard Accept, the plugin rethrows with a message
91
+ // that names `sse: 'only'` as the fix.
92
+ fastify.get('/legacy', { sse: true }, handler)
93
+
94
+ // Object form (supports per-route options):
95
+ fastify.get('/events', {
71
96
  sse: {
72
- heartbeat: false, // Disable heartbeat for this route
97
+ kind: 'only', // 'only' | 'dual' | 'manual' — omit for back-compat
98
+ heartbeat: false, // Disable heartbeat for this route
73
99
  serializer: customSerializer // Custom serializer for this route
74
100
  }
75
101
  }, handler)
@@ -134,7 +160,7 @@ const fs = require('fs')
134
160
 
135
161
  fastify.get('/file-stream', { sse: true }, async (request, reply) => {
136
162
  const fileStream = fs.createReadStream('data.jsonl')
137
-
163
+
138
164
  // Parse each line as JSON and convert to SSE format
139
165
  const parseTransform = new Transform({
140
166
  transform(chunk, encoding, callback) {
@@ -150,7 +176,7 @@ fastify.get('/file-stream', { sse: true }, async (request, reply) => {
150
176
  callback()
151
177
  }
152
178
  })
153
-
179
+
154
180
  // Stream file data through SSE
155
181
  await pipeline(
156
182
  fileStream,
@@ -204,16 +230,16 @@ fastify.get('/events', { sse: true }, async (request, reply) => {
204
230
  await reply.sse.replay(async (lastEventId) => {
205
231
  // Find messages after the last received ID
206
232
  const startIndex = messageHistory.findIndex(msg => msg.id === lastEventId)
207
- const messagesToReplay = startIndex !== -1
233
+ const messagesToReplay = startIndex !== -1
208
234
  ? messageHistory.slice(startIndex + 1)
209
235
  : messageHistory
210
-
236
+
211
237
  // Send missed messages
212
238
  for (const message of messagesToReplay) {
213
239
  await reply.sse.send(message)
214
240
  }
215
241
  })
216
-
242
+
217
243
  // Send new message
218
244
  const newMessage = { id: Date.now(), data: 'New event' }
219
245
  messageHistory.push(newMessage)
@@ -224,40 +250,133 @@ fastify.get('/events', { sse: true }, async (request, reply) => {
224
250
  ### Properties and Methods
225
251
 
226
252
  #### Properties
253
+
227
254
  - `reply.sse.lastEventId`: Client's last received event ID (string | null)
228
255
  - `reply.sse.isConnected`: Connection status (boolean)
229
256
  - `reply.sse.shouldKeepAlive`: Whether connection should be kept alive after handler completion (boolean)
230
257
 
231
258
  #### Methods
259
+
232
260
  - `reply.sse.send(source)`: Send SSE messages from various source types
233
261
  - `reply.sse.stream()`: Create a transform stream for pipeline operations
234
262
  - `reply.sse.keepAlive()`: Prevent connection from auto-closing after handler returns
235
263
  - `reply.sse.close()`: Manually close the connection
236
264
  - `reply.sse.replay(callback)`: Handle message replay using Last-Event-ID
237
265
  - `reply.sse.onClose(callback)`: Register callback for when connection closes
238
- - `reply.sse.sendHeaders()`: Manually send headers (called automatically on first write)
266
+ - `reply.sse.sendHeaders(statusCode?)`: Manually send headers (called automatically on first write)
239
267
 
240
- ## Advanced Usage
268
+ ### Custom Response Status Code
269
+
270
+ The SSE response defaults to `200 OK`. To use a different status, either call
271
+ `reply.code()` before the first SSE write, or pass the status directly to a
272
+ manual `reply.sse.sendHeaders()` call:
241
273
 
242
- ### Fallback to Regular Responses
274
+ ```javascript
275
+ fastify.get('/events', { sse: 'only' }, async (request, reply) => {
276
+ reply.code(201)
277
+ await reply.sse.send({ data: 'created' })
278
+ })
279
+
280
+ fastify.get('/events-explicit', { sse: 'only' }, async (request, reply) => {
281
+ reply.sse.sendHeaders(201)
282
+ await reply.sse.send({ data: 'created' })
283
+ })
284
+ ```
285
+
286
+ An explicit `sendHeaders()` argument takes precedence over `reply.code()`.
287
+ Once the first SSE write commits the response headers, later `reply.code()`
288
+ or `sendHeaders()` calls have no effect.
289
+
290
+ ## Advanced Usage
243
291
 
244
- Routes with `{ sse: true }` automatically fall back to regular handlers when the client doesn't request SSE:
292
+ ### Accept-Header Negotiation
293
+
294
+ The plugin parses the `Accept` header per [RFC 9110 §12.5.1](https://www.rfc-editor.org/rfc/rfc9110.html#name-accept)
295
+ to decide whether to serve a request as SSE. The decision depends on the
296
+ route's declared kind:
297
+
298
+ | Route kind | Accept header | Behavior |
299
+ |----------------|----------------------------|-------------------------|
300
+ | `sse: 'only'` | `text/event-stream` | SSE |
301
+ | `sse: 'only'` | `*/*`, `text/*`, missing | SSE |
302
+ | `sse: 'only'` | `application/json` etc. | **406 Not Acceptable** |
303
+ | `sse: 'only'` | `…, text/event-stream;q=0` | **406 Not Acceptable** |
304
+ | `sse: 'dual'` | `text/event-stream` | SSE branch |
305
+ | `sse: 'dual'` | `*/*`, `text/*`, missing | Non-SSE branch |
306
+ | `sse: 'dual'` | `application/json` | Non-SSE branch |
307
+ | `sse: 'manual'`| (any / missing) | Handler decides at runtime |
308
+ | `sse: true` | (same routing as `'dual'`) | + explanatory error on misuse |
309
+
310
+ In short: explicit `text/event-stream` always routes to SSE; ambiguous
311
+ wildcards route to SSE only when you've declared the route SSE-only.
312
+ This keeps `curl`, `fetch()`, and Postman defaults working with SSE-only
313
+ endpoints while preserving the dual-mode pattern's expectation that a
314
+ wildcard prefers the richer non-SSE representation.
315
+
316
+ ### Fallback to Regular Responses (`sse: 'dual'`)
317
+
318
+ Dual-mode routes serve both SSE and non-SSE clients from a single
319
+ handler. The plugin attaches `reply.sse` only when the client explicitly
320
+ requested `text/event-stream`; otherwise the handler is invoked without
321
+ `reply.sse` and is expected to produce a normal response. Branch on its
322
+ presence:
245
323
 
246
324
  ```js
247
- fastify.get('/data', { sse: true }, async (request, reply) => {
325
+ fastify.get('/data', { sse: 'dual' }, async (request, reply) => {
248
326
  const data = await getData()
249
-
250
- // Check if this is an SSE request
251
- if (request.headers.accept?.includes('text/event-stream')) {
252
- // SSE client - stream the data
327
+
328
+ if (reply.sse) {
329
+ // SSE client — stream the data
253
330
  await reply.sse.send({ data })
254
331
  } else {
255
- // Regular client - return JSON
332
+ // Regular client — return JSON
256
333
  return { data }
257
334
  }
258
335
  })
259
336
  ```
260
337
 
338
+ The SSE-specific response headers (`Content-Type: text/event-stream`
339
+ etc.) are not committed until the first `reply.sse.send()` /
340
+ `reply.sse.stream()` call, so a handler that decorates `reply.sse` but
341
+ then returns a plain value falls through to Fastify's normal
342
+ serialization path without corrupting the response.
343
+
344
+ ### Dynamic Streaming Based on Request Logic (`sse: 'manual'`)
345
+
346
+ Some APIs decide whether to stream based on the request **body** rather
347
+ than the `Accept` header. OpenAI-compatible and other LLM endpoints, for
348
+ example, return a `text/event-stream` response when the request carries
349
+ `{ "stream": true }` and a single JSON object otherwise — the client
350
+ sends no `Accept: text/event-stream` header either way.
351
+
352
+ `sse: 'manual'` covers this: it skips Accept-header negotiation entirely,
353
+ always attaches `reply.sse`, and lets the handler decide at runtime
354
+ whether to stream. As with `'dual'`, SSE headers are committed lazily on
355
+ the first `reply.sse.send()` / `reply.sse.stream()` call, so a handler
356
+ that never streams falls through to Fastify's normal serialization.
357
+
358
+ ```js
359
+ fastify.post('/v1/chat/completions', { sse: 'manual' }, async (request, reply) => {
360
+ const completion = await model.run(request.body)
361
+
362
+ if (request.body.stream) {
363
+ // Stream tokens as SSE — headers commit on the first send().
364
+ await reply.sse.send(completion.tokens) // async iterable / Readable / messages
365
+ return
366
+ }
367
+
368
+ // Non-streaming client — return a normal JSON response.
369
+ return completion.toJSON()
370
+ })
371
+ ```
372
+
373
+ The connection is closed automatically when the handler resolves or
374
+ throws (call `reply.sse.keepAlive()` to keep it open for out-of-band
375
+ sends, then `reply.sse.close()` when done). Because the heartbeat only
376
+ starts once the response is committed as SSE, a non-streaming fallback
377
+ response is never corrupted by a heartbeat write.
378
+
379
+
261
380
  ### Error Handling
262
381
 
263
382
  ```js
@@ -267,11 +386,11 @@ fastify.get('/stream', { sse: true }, async (request, reply) => {
267
386
  yield { data: 'before error' }
268
387
  throw new Error('Something went wrong')
269
388
  }
270
-
389
+
271
390
  await reply.sse.send(riskyGenerator())
272
391
  } catch (error) {
273
392
  // Handle errors gracefully
274
- await reply.sse.send({
393
+ await reply.sse.send({
275
394
  event: 'error',
276
395
  data: { message: 'Stream error occurred' }
277
396
  })
@@ -328,20 +447,20 @@ assert.ok(response.body.includes('data: "Hello SSE!"'))
328
447
  </head>
329
448
  <body>
330
449
  <div id="messages"></div>
331
-
450
+
332
451
  <script>
333
452
  const eventSource = new EventSource('/events')
334
453
  const messagesDiv = document.getElementById('messages')
335
-
454
+
336
455
  eventSource.onmessage = function(event) {
337
456
  const data = JSON.parse(event.data)
338
457
  messagesDiv.innerHTML += '<div>' + JSON.stringify(data) + '</div>'
339
458
  }
340
-
459
+
341
460
  eventSource.addEventListener('update', function(event) {
342
461
  console.log('Update event:', JSON.parse(event.data))
343
462
  })
344
-
463
+
345
464
  eventSource.onerror = function(event) {
346
465
  console.error('SSE error:', event)
347
466
  }
@@ -382,6 +501,7 @@ app.get('/events', { sse: true }, async (request, reply) => {
382
501
  See the [examples](examples/) directory for complete working examples:
383
502
 
384
503
  - [Basic Usage](examples/basic.js) - Simple SSE endpoints
504
+ - [Dynamic Streaming](examples/manual-streaming.js) - `sse: 'manual'`, OpenAI-style stream-or-JSON based on the request body
385
505
  - More examples coming soon...
386
506
 
387
507
  ## Comparison with fastify-sse-v2
@@ -399,4 +519,4 @@ See the [examples](examples/) directory for complete working examples:
399
519
 
400
520
  ## License
401
521
 
402
- [MIT](LICENSE)
522
+ [MIT](LICENSE)
package/eslint.config.js CHANGED
@@ -1,3 +1,6 @@
1
- const neostandard = require('neostandard')
1
+ 'use strict'
2
2
 
3
- module.exports = neostandard()
3
+ module.exports = require('neostandard')({
4
+ ignores: require('neostandard').resolveIgnoresFromGitignore(),
5
+ ts: true
6
+ })