Skip to content
Myr

Merge commit from fork

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

* fix: sse event must not contain \r or \n * fix: improve error message

authored by Taku Amano · committed by GitHub · parent 80a9837 · Browse files · 2 files+86 added−0 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+86 added−0 removed

  • tests:+80 added−0 removed
  • other:+6 added−0 removed

2 files+86 added−0 removed

modified:src/helper/streaming/sse.test.tsx+80 added
294 unmodified lines
295296 expect(decodedValue).toBe('event: test-mixed\ndata: A\ndata: B\ndata: C\ndata: D\n\n')297 })298299 it('Should throw error if event contains \\n', async () => {300 const onError = vi.fn()301 const res = streamSSE(302 c,303 async (stream) => {304 await stream.writeSSE({ data: 'test', event: 'test\nevent' })305 },306 onError307 )308 if (!res.body) {309 throw new Error('Body is null')310 }311 const reader = res.body.getReader()312 const decoder = new TextDecoder()313 const { value } = await reader.read()314 const decodedValue = decoder.decode(value)315 expect(decodedValue).toContain('event: error')316 expect(onError).toBeCalledTimes(1)317 })318319 it('Should throw error if event contains \\r', async () => {320 const onError = vi.fn()321 const res = streamSSE(322 c,323 async (stream) => {324 await stream.writeSSE({ data: 'test', event: 'test\revent' })325 },326 onError327 )328 if (!res.body) {329 throw new Error('Body is null')330 }331 const reader = res.body.getReader()332 const decoder = new TextDecoder()333 const { value } = await reader.read()334 const decodedValue = decoder.decode(value)335 expect(decodedValue).toContain('event: error')336 expect(onError).toBeCalledTimes(1)337 })338339 it('Should throw error if id contains \\n', async () => {340 const onError = vi.fn()341 const res = streamSSE(342 c,343 async (stream) => {344 await stream.writeSSE({ data: 'test', id: 'test\nid' })345 },346 onError347 )348 if (!res.body) {349 throw new Error('Body is null')350 }351 const reader = res.body.getReader()352 const decoder = new TextDecoder()353 const { value } = await reader.read()354 const decodedValue = decoder.decode(value)355 expect(decodedValue).toContain('event: error')356 expect(onError).toBeCalledTimes(1)357 })358359 it('Should throw error if id contains \\r', async () => {360 const onError = vi.fn()361 const res = streamSSE(362 c,363 async (stream) => {364 await stream.writeSSE({ data: 'test', id: 'test\rid' })365 },366 onError367 )368 if (!res.body) {369 throw new Error('Body is null')370 }371 const reader = res.body.getReader()372 const decoder = new TextDecoder()373 const { value } = await reader.read()374 const decodedValue = decoder.decode(value)375 expect(decodedValue).toContain('event: error')376 expect(onError).toBeCalledTimes(1)377 })378379 it('Check streamSSE handles consecutive \\r correctly', async () => {380 const res = streamSSE(c, async (stream) => {381 await stream.writeSSE({382 data: 'Left\r\rRight',
16 unmodified lines
modified:src/helper/streaming/sse.ts+6 added