Skip to content
Myr

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

3 files+59 added−41 removed

modified:deno_dist/helper/streaming/sse.ts+27 added-19 removed
modified:src/helper/streaming/sse.test.ts+5 added-3 removed
modified:src/helper/streaming/sse.ts+27 added-19 removed
33 unmodified lines
34 await this.write(sseData)35 }36}3738const 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')38const 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)4849 await stream.writeSSE({50 event: 'error',51 data: e.message,52 })53 } else {54 console.error(e)55 }56 }57}5859export const streamSSE = (60 c: Context,
1 unmodified line
62 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)6667 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')7172 run(stream, cb, onError)7374 return c.newResponse(stream.responseReadable)75}