Skip to content

Commit aeba9ec

Browse files
authored
fix(sse): emit retry feild when retry is 0 (#5135)
1 parent d796450 commit aeba9ec

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/helper/streaming/sse.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ describe('SSE Streaming helper', () => {
132132
expect(decodedValue).toContain(expectedRetryValue)
133133
})
134134

135+
it('Should accept retry: 0 and not skip it', async () => {
136+
const res = streamSSE(c, async (stream) => {
137+
await stream.writeSSE({
138+
data: 'This is a test message',
139+
retry: 0,
140+
})
141+
})
142+
143+
expect(res).not.toBeNull()
144+
expect(res.status).toBe(200)
145+
146+
if (!res.body) {
147+
throw new Error('Body is null')
148+
}
149+
const reader = res.body.getReader()
150+
const decoder = new TextDecoder()
151+
const { value } = await reader.read()
152+
const decodedValue = decoder.decode(value)
153+
154+
expect(decodedValue).toContain('retry: 0\n\n')
155+
})
156+
135157
it('Check stream Response if error occurred', async () => {
136158
const onError = vi.fn()
137159
const res = streamSSE(

src/helper/streaming/sse.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ export class SSEStreamingApi extends StreamingApi {
2424
})
2525
.join('\n')
2626

27-
for (const key of ['event', 'id', 'retry'] as (keyof SSEMessage)[]) {
28-
if (message[key] && /[\r\n]/.test(message[key] as string)) {
27+
for (const key of ['event', 'id'] as const) {
28+
const value = message[key]
29+
if (value && /[\r\n]/.test(value)) {
2930
throw new Error(`${key} must not contain "\\r" or "\\n"`)
3031
}
3132
}
@@ -35,7 +36,7 @@ export class SSEStreamingApi extends StreamingApi {
3536
message.event && `event: ${message.event}`,
3637
dataLines,
3738
message.id && `id: ${message.id}`,
38-
message.retry && `retry: ${message.retry}`,
39+
message.retry !== undefined && `retry: ${message.retry}`,
3940
]
4041
.filter(Boolean)
4142
.join('\n') + '\n\n'

0 commit comments

Comments
 (0)