fix(streaming): Fix stream SSE, not necessary close stream. (#2320)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* 🐛 Fix stream SSE, not necessary close stream. * ✨ Remove unused * ✨ Add deno files
authored by Damián Pumar · committed by GitHub · parent bc6330c · Browse files · 3 files·+59 added−41 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+59 added−41 removed
- tests:+5 added−3 removed
- other:+54 added−38 removed
Provenance
- Commit
b827efb- Tree
0b9d8bd- Author
Damián Pumar <damianpumar@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
bc6330c
3 files changed+59 added−41 removed
modified:deno_dist/helper/streaming/sse.ts+27 added-19 removed
33 unmodified lines3435363738394041423839404142434445464748495051525354555657585960616263646552535455565758596061626364656667686970717273747533 unmodified lines await this.write(sseData) }}
const setSSEHeaders = (context: Context) => { context.header('Transfer-Encoding', 'chunked') context.header('Content-Type', 'text/event-stream') context.header('Cache-Control', 'no-cache') context.header('Connection', 'keep-alive')const run = async ( stream: SSEStreamingApi, cb: (stream: SSEStreamingApi) => Promise<void>, onError?: (e: Error, stream: SSEStreamingApi) => Promise<void>) => { try { await cb(stream) } catch (e) { if (e instanceof Error && onError) { await onError(e, stream)
await stream.writeSSE({ event: 'error', data: e.message, }) } else { console.error(e) } }}
export const streamSSE = ( c: Context, cb: (stream: SSEStreamingApi) => Promise<void>, onError?: (e: Error, stream: SSEStreamingApi) => Promise<void>) => { const { readable, writable } = new TransformStream() const stream = new SSEStreamingApi(writable, readable) ;(async () => { try { await cb(stream) } catch (e) { if (e instanceof Error && onError) { await onError(e, stream) } else { console.error(e) } } finally { stream.close() } })() setSSEHeaders(c)
c.header('Transfer-Encoding', 'chunked') c.header('Content-Type', 'text/event-stream') c.header('Cache-Control', 'no-cache') c.header('Connection', 'keep-alive')
run(stream, cb, onError)
return c.newResponse(stream.responseReadable)}modified:src/helper/streaming/sse.test.ts+5 added-3 removed
97 unmodified lines989910010110210210310410510610710810911011111111211311411311511611797 unmodified lines const onError = vi.fn() const res = streamSSE( c, async () => { throw new Error('error') throw new Error('Test error') }, onError ) if (!res.body) { throw new Error('Body is null') } const reader = res.body.getReader() const decoder = new TextDecoder() const { value } = await reader.read() expect(value).toBeUndefined() const decodedValue = decoder.decode(value) expect(decodedValue).toBe('event: error\ndata: Test error\n\n') expect(onError).toBeCalledTimes(1) expect(onError).toBeCalledWith(new Error('error'), expect.anything()) // 2nd argument is StreamingApi instance expect(onError).toBeCalledWith(new Error('Test error'), expect.anything()) // 2nd argument is StreamingApi instance })})modified:src/helper/streaming/sse.ts+27 added-19 removed
33 unmodified lines34 await this.write(sseData)35 }36 }37 38-const setSSEHeaders = (context: Context) => {39- context.header('Transfer-Encoding', 'chunked')40- context.header('Content-Type', 'text/event-stream')41- context.header('Cache-Control', 'no-cache')42- context.header('Connection', 'keep-alive')38+const run = async (39+ stream: SSEStreamingApi,40+ cb: (stream: SSEStreamingApi) => Promise<void>,41+ onError?: (e: Error, stream: SSEStreamingApi) => Promise<void>42+) => {43+ try {44+ await cb(stream)45+ } catch (e) {46+ if (e instanceof Error && onError) {47+ await onError(e, stream)48+49+ await stream.writeSSE({50+ event: 'error',51+ data: e.message,52+ })53+ } else {54+ console.error(e)55+ }56+ }57 }58 59 export const streamSSE = (60 c: Context,1 unmodified line62 onError?: (e: Error, stream: SSEStreamingApi) => Promise<void>63 ) => {64 const { readable, writable } = new TransformStream()65 const stream = new SSEStreamingApi(writable, readable)52- ;(async () => {53- try {54- await cb(stream)55- } catch (e) {56- if (e instanceof Error && onError) {57- await onError(e, stream)58- } else {59- console.error(e)60- }61- } finally {62- stream.close()63- }64- })()65- setSSEHeaders(c)66+67+ c.header('Transfer-Encoding', 'chunked')68+ c.header('Content-Type', 'text/event-stream')69+ c.header('Cache-Control', 'no-cache')70+ c.header('Connection', 'keep-alive')71+72+ run(stream, cb, onError)73+74 return c.newResponse(stream.responseReadable)75 }