Skip to main content
Module

x/hono/helper/streaming/stream.ts

Web Framework built on Web Standards
Extremely Popular
Go to Latest
File
import type { Context } from '../../context.ts'import { StreamingApi } from '../../utils/stream.ts'
export const stream = ( c: Context, cb: (stream: StreamingApi) => Promise<void>, onError?: (e: Error, stream: StreamingApi) => Promise<void>): Response => { const { readable, writable } = new TransformStream() const stream = new StreamingApi(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() } })() return c.newResponse(stream.responseReadable)}