refactor(helper/streaming): Avoid attaching AbortSignal on newer versions of Bun (#3859)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* refactor(helper/streaming): Avoid attaching AbortSignal on newer versions of Bun * fixed the tests and refactored ---------
authored by Jarred Sumner · committed by GitHub · parent d40fffb · Browse files · 5 files·+53 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
Diff stats
5 files+53 added−12 removed
- tests:+20 added−0 removed
- other:+33 added−12 removed
Provenance
- Commit
d72aa4b- Tree
5e5a040- Author
Jarred Sumner <jarred@jarredsumner.com>- Committer
GitHub <noreply@github.com>- Parent
d40fffb
5 files changed+53 added−12 removed
modified:src/helper/streaming/sse.test.tsx+5 added
74 unmodified lines75 expect(aborted).toBeTruthy()76 })77 78 it('Check streamSSE Response if aborted by abort signal', async () => {79+ // Emulate an old version of Bun (version 1.1.0) for this specific test case80+ // @ts-expect-error Bun is not typed81+ global.Bun = {82+ version: '1.1.0',83+ }84 const ac = new AbortController()85 const req = new Request('http://localhost/', { signal: ac.signal })86 const c = new Context(req)87 154 unmodified linesmodified:src/helper/streaming/sse.ts+11 added-6 removed
1234567852 unmodified lines61626364656667686970717273747273747576777576777879808182838485868 unmodified linesimport type { Context } from '../../context'import { HtmlEscapedCallbackPhase, resolveCallback } from '../../utils/html'import { StreamingApi } from '../../utils/stream'import { isOldBunVersion } from './utils'
export interface SSEMessage { data: string | Promise<string> event?: string52 unmodified lines }}
const contextStash: WeakMap<ReadableStream, Context> = new WeakMap<ReadableStream, Context>()
export const streamSSE = ( c: Context, cb: (stream: SSEStreamingApi) => Promise<void>, onError?: (e: Error, stream: SSEStreamingApi) => Promise<void>): Response => { const { readable, writable } = new TransformStream() const stream = new SSEStreamingApi(writable, readable)
// Until Bun v1.1.27, Bun didn't call cancel() on the ReadableStream for Response objects from Bun.serve() // bun does not cancel response stream when request is canceled, so detect abort by signal c.req.raw.signal.addEventListener('abort', () => { if (!stream.closed) { stream.abort() } }) if (isOldBunVersion()) { c.req.raw.signal.addEventListener('abort', () => { if (!stream.closed) { stream.abort() } }) }
// in bun, `c` is destroyed when the request is returned, so hold it until the end of streaming contextStash.set(stream.responseReadable, c)
c.header('Transfer-Encoding', 'chunked')8 unmodified linesmodified:src/helper/streaming/stream.test.ts+15 added
1 import { Context } from '../../context'2+import { isOldBunVersion } from './utils'3 import { stream } from '.'4 5 describe('Basic Streaming Helper', () => {6 const req = new Request('http://localhost/')40 unmodified lines47 expect(aborted).toBeTruthy()48 })49 50 it('Check stream Response if aborted by abort signal', async () => {51+ // Emulate an old version of Bun (version 1.1.0) for this specific test case52+ // @ts-expect-error Bun is not typed53+ global.Bun = {54+ version: '1.1.0',55+ }56 const ac = new AbortController()57 const req = new Request('http://localhost/', { signal: ac.signal })58 const c = new Context(req)59 14 unmodified lines74 const { value } = await reader.read()75 expect(value).toEqual(new Uint8Array([0]))76 ac.abort()77 expect(aborted).toBeTruthy()78+ // @ts-expect-error Bun is not typed79+ delete global.Bun80 })81 82 it('Check stream Response if pipe is aborted by abort signal', async () => {83+ // Emulate an old version of Bun (version 1.1.0) for this specific test case84+ // @ts-expect-error Bun is not typed85+ global.Bun = {86+ version: '1.1.0',87+ }88 const ac = new AbortController()89 const req = new Request('http://localhost/', { signal: ac.signal })90 const c = new Context(req)91 11 unmodified lines103 const pReading = reader.read()104 ac.abort()105 await pReading106 expect(aborted).toBeTruthy()107+ // @ts-expect-error Bun is not typed108+ delete global.Bun109 })110 111 it('Check stream Response if error occurred', async () => {112 const onError = vi.fn()17 unmodified linesmodified:src/helper/streaming/stream.ts+11 added-6 removed
12345678910111213141513141516171816171819202122232425262718 unmodified linesimport type { Context } from '../../context'import { StreamingApi } from '../../utils/stream'import { isOldBunVersion } from './utils'
const contextStash: WeakMap<ReadableStream, Context> = new WeakMap<ReadableStream, Context>()
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)
// Until Bun v1.1.27, Bun didn't call cancel() on the ReadableStream for Response objects from Bun.serve() // bun does not cancel response stream when request is canceled, so detect abort by signal c.req.raw.signal.addEventListener('abort', () => { if (!stream.closed) { stream.abort() } }) if (isOldBunVersion()) { c.req.raw.signal.addEventListener('abort', () => { if (!stream.closed) { stream.abort() } }) }
// in bun, `c` is destroyed when the request is returned, so hold it until the end of streaming contextStash.set(stream.responseReadable, c) ;(async () => { try {18 unmodified linesadded:src/helper/streaming/utils.ts+11 added
1234567891011export let isOldBunVersion = (): boolean => { // @ts-expect-error @types/bun is not installed const version: string = typeof Bun !== 'undefined' ? Bun.version : undefined if (version === undefined) { return false } const result = version.startsWith('1.1') || version.startsWith('1.0') || version.startsWith('0.') // Avoid running this check on every call isOldBunVersion = () => result return result}