@fastify/sse 0.6.1 → 0.6.2
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/workflows/release.yml +1 -1
- package/index.js +14 -6
- package/package.json +1 -1
- package/test/basic.test.js +49 -0
|
@@ -19,6 +19,6 @@ jobs:
|
|
|
19
19
|
permissions:
|
|
20
20
|
id-token: write # required for npm provenance via OIDC
|
|
21
21
|
contents: write # required to push the release commit and the tag
|
|
22
|
-
uses: fastify/workflows/.github/workflows/reusable-release.yml@
|
|
22
|
+
uses: fastify/workflows/.github/workflows/reusable-release.yml@731d0cca9a6a5b9586dad6b0326d49001ab9216f
|
|
23
23
|
with:
|
|
24
24
|
semver: ${{ inputs.semver }}
|
package/index.js
CHANGED
|
@@ -235,6 +235,14 @@ function resolveSSEConfig (sseField) {
|
|
|
235
235
|
|
|
236
236
|
const MISSING_SSE_ERROR_PATTERN = /Cannot read prop(?:erties)? of undefined.*'(?:send|stream|keepAlive|close|sendHeaders|replay|onClose)'/
|
|
237
237
|
|
|
238
|
+
// CRLF, CR and LF all end a line in an event stream
|
|
239
|
+
const LINE_BREAK = /\r\n|\r|\n/
|
|
240
|
+
|
|
241
|
+
// A line break would end the id or event field and start a new one
|
|
242
|
+
function toSingleLine (value) {
|
|
243
|
+
return String(value).replace(/[\r\n]/g, '')
|
|
244
|
+
}
|
|
245
|
+
|
|
238
246
|
/**
|
|
239
247
|
* Format an SSE message according to the specification
|
|
240
248
|
* @param {Object|string|Buffer} message - The message to format
|
|
@@ -246,35 +254,35 @@ function formatSSEMessage (message, serializer) {
|
|
|
246
254
|
|
|
247
255
|
if (typeof message === 'string') {
|
|
248
256
|
// Plain string message
|
|
249
|
-
for (const line of message.split(
|
|
257
|
+
for (const line of message.split(LINE_BREAK)) {
|
|
250
258
|
payload += `data: ${line}\n`
|
|
251
259
|
}
|
|
252
260
|
} else if (Buffer.isBuffer(message)) {
|
|
253
261
|
// Buffer message
|
|
254
262
|
const str = message.toString('utf-8')
|
|
255
|
-
for (const line of str.split(
|
|
263
|
+
for (const line of str.split(LINE_BREAK)) {
|
|
256
264
|
payload += `data: ${line}\n`
|
|
257
265
|
}
|
|
258
266
|
} else {
|
|
259
267
|
// SSEMessage object
|
|
260
268
|
if (message.id) {
|
|
261
|
-
payload += `id: ${message.id}\n`
|
|
269
|
+
payload += `id: ${toSingleLine(message.id)}\n`
|
|
262
270
|
}
|
|
263
271
|
|
|
264
272
|
if (message.event) {
|
|
265
|
-
payload += `event: ${message.event}\n`
|
|
273
|
+
payload += `event: ${toSingleLine(message.event)}\n`
|
|
266
274
|
}
|
|
267
275
|
|
|
268
276
|
if (message.data !== undefined) {
|
|
269
277
|
// Always serialize the data to ensure consistent format
|
|
270
278
|
const dataStr = serializer(message.data)
|
|
271
279
|
|
|
272
|
-
for (const line of dataStr.split(
|
|
280
|
+
for (const line of dataStr.split(LINE_BREAK)) {
|
|
273
281
|
payload += `data: ${line}\n`
|
|
274
282
|
}
|
|
275
283
|
}
|
|
276
284
|
|
|
277
|
-
if (message.retry) {
|
|
285
|
+
if (Number.isInteger(message.retry) && message.retry > 0) {
|
|
278
286
|
payload += `retry: ${message.retry}\n`
|
|
279
287
|
}
|
|
280
288
|
}
|
package/package.json
CHANGED
package/test/basic.test.js
CHANGED
|
@@ -684,3 +684,52 @@ test('supports route-level serializer and disabled heartbeat options', async (t)
|
|
|
684
684
|
assert.strictEqual(response.body, 'data: route:value\n\n')
|
|
685
685
|
assert.strictEqual(context.heartbeatTimer, null)
|
|
686
686
|
})
|
|
687
|
+
|
|
688
|
+
test('splits data on CR and CRLF line breaks', async (t) => {
|
|
689
|
+
const app = await buildApp(t, { heartbeatInterval: 0 })
|
|
690
|
+
|
|
691
|
+
app.get('/events', { sse: true }, async (request, reply) => {
|
|
692
|
+
await reply.sse.send('a\rb\r\nc')
|
|
693
|
+
await reply.sse.send(Buffer.from('d\re'))
|
|
694
|
+
})
|
|
695
|
+
|
|
696
|
+
const response = await app.inject({
|
|
697
|
+
url: '/events',
|
|
698
|
+
headers: { accept: 'text/event-stream' }
|
|
699
|
+
})
|
|
700
|
+
|
|
701
|
+
assert.strictEqual(response.body, 'data: a\ndata: b\ndata: c\n\ndata: d\ndata: e\n\n')
|
|
702
|
+
})
|
|
703
|
+
|
|
704
|
+
test('keeps id and event on a single line', async (t) => {
|
|
705
|
+
const app = await buildApp(t, { heartbeatInterval: 0 })
|
|
706
|
+
|
|
707
|
+
app.get('/events', { sse: true }, async (request, reply) => {
|
|
708
|
+
await reply.sse.send({ id: 'x\nevent: injected', event: 'e\r\ndata: injected', data: 'd' })
|
|
709
|
+
})
|
|
710
|
+
|
|
711
|
+
const response = await app.inject({
|
|
712
|
+
url: '/events',
|
|
713
|
+
headers: { accept: 'text/event-stream' }
|
|
714
|
+
})
|
|
715
|
+
|
|
716
|
+
assert.strictEqual(response.body, 'id: xevent: injected\nevent: edata: injected\ndata: "d"\n\n')
|
|
717
|
+
})
|
|
718
|
+
|
|
719
|
+
test('only writes retry when it is a positive integer', async (t) => {
|
|
720
|
+
const app = await buildApp(t, { heartbeatInterval: 0 })
|
|
721
|
+
|
|
722
|
+
app.get('/events', { sse: true }, async (request, reply) => {
|
|
723
|
+
await reply.sse.send({ data: 'a', retry: '1000\nevent: injected' })
|
|
724
|
+
await reply.sse.send({ data: 'b', retry: 1.5 })
|
|
725
|
+
await reply.sse.send({ data: 'c', retry: -1 })
|
|
726
|
+
await reply.sse.send({ data: 'd', retry: 2000 })
|
|
727
|
+
})
|
|
728
|
+
|
|
729
|
+
const response = await app.inject({
|
|
730
|
+
url: '/events',
|
|
731
|
+
headers: { accept: 'text/event-stream' }
|
|
732
|
+
})
|
|
733
|
+
|
|
734
|
+
assert.strictEqual(response.body, 'data: "a"\n\ndata: "b"\n\ndata: "c"\n\ndata: "d"\nretry: 2000\n\n')
|
|
735
|
+
})
|