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
Provenance
- Commit
f4123ed- Tree
c2eba93- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
80a9837
2 files changed+86 added−0 removed
modified:src/helper/streaming/sse.test.tsx+80 added
294 unmodified lines295 296 expect(decodedValue).toBe('event: test-mixed\ndata: A\ndata: B\ndata: C\ndata: D\n\n')297 })298 299+ 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+ })318+319+ 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+ })338+339+ 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+ })358+359+ 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+ })378+379 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 linesmodified:src/helper/streaming/sse.ts+6 added
22 unmodified lines232425262728293031323334353664 unmodified lines22 unmodified lines return `data: ${line}` }) .join('\n')
for (const key of ['event', 'id', 'retry'] as (keyof SSEMessage)[]) { if (message[key] && /[\r\n]/.test(message[key] as string)) { throw new Error(`${key} must not contain "\\r" or "\\n"`) } }
const sseData = [ message.event && `event: ${message.event}`, dataLines,64 unmodified lines