Skip to content
Myr

fix(sse): handle `\r` and `\r\n` line endings in writeSSE (#4644)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

According to the SSE specification, lines can end with `\r`, `\n`, or `\r\n`.

authored by AprilNEA · committed by GitHub · parent 8078bbf · Browse files · 2 files+78 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+78 added−1 removed

  • tests:+77 added−0 removed
  • other:+1 added−1 removed

2 files+78 added−1 removed

modified:src/helper/streaming/sse.test.tsx+77 added
236 unmodified lines
237 const { value } = await reader.read()238 const decodedValue = decoder.decode(value)239 expect(decodedValue).toBe('data: <div>Error</div>\n\n')240 })241242 it('Check streamSSE handles \\r (CR) line ending correctly', async () => {243 const res = streamSSE(c, async (stream) => {244 await stream.writeSSE({245 data: 'Line1\rLine2',246 event: 'test-cr',247 })248 })249250 if (!res.body) {251 throw new Error('Body is null')252 }253 const reader = res.body.getReader()254 const decoder = new TextDecoder()255 const { value } = await reader.read()256 const decodedValue = decoder.decode(value)257258 expect(decodedValue).toBe('event: test-cr\ndata: Line1\ndata: Line2\n\n')259 })260261 it('Check streamSSE handles \\r\\n (CRLF) line ending correctly', async () => {262 const res = streamSSE(c, async (stream) => {263 await stream.writeSSE({264 data: 'Line1\r\nLine2',265 event: 'test-crlf',266 })267 })268269 if (!res.body) {270 throw new Error('Body is null')271 }272 const reader = res.body.getReader()273 const decoder = new TextDecoder()274 const { value } = await reader.read()275 const decodedValue = decoder.decode(value)276277 expect(decodedValue).toBe('event: test-crlf\ndata: Line1\ndata: Line2\n\n')278 })279280 it('Check streamSSE handles mixed line endings correctly', async () => {281 const res = streamSSE(c, async (stream) => {282 await stream.writeSSE({283 data: 'A\nB\rC\r\nD',284 event: 'test-mixed',285 })286 })287288 if (!res.body) {289 throw new Error('Body is null')290 }291 const reader = res.body.getReader()292 const decoder = new TextDecoder()293 const { value } = await reader.read()294 const decodedValue = decoder.decode(value)295296 expect(decodedValue).toBe('event: test-mixed\ndata: A\ndata: B\ndata: C\ndata: D\n\n')297 })298299 it('Check streamSSE handles consecutive \\r correctly', async () => {300 const res = streamSSE(c, async (stream) => {301 await stream.writeSSE({302 data: 'Left\r\rRight',303 event: 'test-double-cr',304 })305 })306307 if (!res.body) {308 throw new Error('Body is null')309 }310 const reader = res.body.getReader()311 const decoder = new TextDecoder()312 const { value } = await reader.read()313 const decodedValue = decoder.decode(value)314315 // Two \r should produce an empty line in between316 expect(decodedValue).toBe('event: test-double-cr\ndata: Left\ndata: \ndata: Right\n\n')317 })318})
modified:src/helper/streaming/sse.ts+1 added-1 removed