fix(stream): Fixed a problem that onAbort() is called even if request is normally closed in deno (#3079)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Fix(stream): Fixed a problem that onAbort() is called even if request is normally closed in deno * test: fix stream abort test for Node.js
authored by Taku Amano · committed by GitHub · parent ba8fb48 · Browse files · 6 files·+121 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+121 added−2 removed
- tests:+110 added−0 removed
- other:+11 added−2 removed
Provenance
- Commit
ada10e5- Tree
fee3424- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
ba8fb48
6 files changed+121 added−2 removed
modified:runtime_tests/bun/index.test.tsx+30 added
331 unmodified lines332 stream.onAbort(resolve)333 })334 })335 })336+ app.get('/streamHello', (c) => {337+ return stream(c, async (stream) => {338+ stream.onAbort(() => {339+ aborted = true340+ })341+ await stream.write('Hello')342+ })343+ })344 app.get('/streamSSE', (c) => {345 return streamSSE(c, async (stream) => {346 stream.onAbort(() => {347 aborted = true2 unmodified lines350 stream.onAbort(resolve)351 })352 })353 })354+ app.get('/streamSSEHello', (c) => {355+ return streamSSE(c, async (stream) => {356+ stream.onAbort(() => {357+ aborted = true358+ })359+ await stream.write('Hello')360+ })361+ })362 363 beforeEach(() => {364 aborted = false365 server = Bun.serve({ port: 0, fetch: app.fetch })18 unmodified lines384 await new Promise((resolve) => setTimeout(resolve))385 }386 expect(aborted).toBe(true)387 })388+389+ it('Should not be called onAbort if already closed', async () => {390+ expect(aborted).toBe(false)391+ const res = await fetch(`http://localhost:${server.port}/streamHello`)392+ expect(await res.text()).toBe('Hello')393+ expect(aborted).toBe(false)394+ })395 })396 397 describe('streamSSE', () => {398 it('Should call onAbort', async () => {9 unmodified lines408 await new Promise((resolve) => setTimeout(resolve))409 }410 expect(aborted).toBe(true)411 })412+413+ it('Should not be called onAbort if already closed', async () => {414+ expect(aborted).toBe(false)415+ const res = await fetch(`http://localhost:${server.port}/streamSSEHello`)416+ expect(await res.text()).toBe('Hello')417+ expect(aborted).toBe(false)418+ })419 })420 })modified:runtime_tests/deno/stream.test.ts+40 added
33 unmodified lines3435363738394041424344454647484950515253545556575859606124 unmodified lines868788899091929394959697989910010110210310410510610710810933 unmodified lines
await server.shutdown()})
Deno.test('Shuld not call onAbort via stream if already closed', async () => { const app = new Hono() let aborted = false app.get('/stream', (c) => { return stream(c, async (stream) => { stream.onAbort(() => { aborted = true }) await stream.write('Hello') }) })
const server = Deno.serve({ port: 0 }, app.fetch) assertEquals(aborted, false) const res = await fetch(`http://localhost:${server.addr.port}/stream`) assertEquals(await res.text(), 'Hello') assertEquals(aborted, false) await server.shutdown()})
Deno.test('Shuld call onAbort via streamSSE', async () => { const app = new Hono() let aborted = false app.get('/stream', (c) => {24 unmodified lines assertEquals(aborted, true)
await server.shutdown()})
Deno.test('Shuld not call onAbort via streamSSE if already closed', async () => { const app = new Hono() let aborted = false app.get('/stream', (c) => { return streamSSE(c, async (stream) => { stream.onAbort(() => { aborted = true }) await stream.write('Hello') }) })
const server = Deno.serve({ port: 0 }, app.fetch) assertEquals(aborted, false) const res = await fetch(`http://localhost:${server.addr.port}/stream`) assertEquals(await res.text(), 'Hello') assertEquals(aborted, false) await server.shutdown()})modified:runtime_tests/node/index.test.ts+40 added
111 unmodified lines112 stream.onAbort(resolve)113 })114 })115 })116+ app.get('/streamHello', (c) => {117+ return stream(c, async (stream) => {118+ stream.onAbort(() => {119+ aborted = true120+ })121+ await stream.write('Hello')122+ })123+ })124 125 const server = createAdaptorServer(app)126 127+ beforeEach(() => {128+ aborted = false129+ })130+131 it('Should call onAbort', async () => {132 const req = request(server)133 .get('/stream')134 .end(() => {})5 unmodified lines140 await new Promise((resolve) => setTimeout(resolve))141 }142 expect(aborted).toBe(true)143 })144+145+ it('Should not be called onAbort if already closed', async () => {146+ expect(aborted).toBe(false)147+ const res = await request(server).get('/streamHello')148+ expect(res.status).toBe(200)149+ expect(res.text).toBe('Hello')150+ expect(aborted).toBe(false)151+ })152 })153 154 describe('streamSSE', () => {155 const app = new Hono()9 unmodified lines165 stream.onAbort(resolve)166 })167 })168 })169+ app.get('/streamHello', (c) => {170+ return streamSSE(c, async (stream) => {171+ stream.onAbort(() => {172+ aborted = true173+ })174+ await stream.write('Hello')175+ })176+ })177 178 const server = createAdaptorServer(app)179 180+ beforeEach(() => {181+ aborted = false182+ })183+184 it('Should call onAbort', async () => {185 const req = request(server)186 .get('/stream')187 .end(() => {})5 unmodified lines193 await new Promise((resolve) => setTimeout(resolve))194 }195 expect(aborted).toBe(true)196 })197+198+ it('Should not be called onAbort if already closed', async () => {199+ expect(aborted).toBe(false)200+ const res = await request(server).get('/streamHello')201+ expect(res.status).toBe(200)202+ expect(res.text).toBe('Hello')203+ expect(aborted).toBe(false)204+ })205 })modified:src/helper/streaming/sse.ts+3 added-1 removed
67 unmodified lines6869707172727374757677789 unmodified lines67 unmodified lines const stream = new SSEStreamingApi(writable, readable)
// 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() 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)
9 unmodified linesmodified:src/helper/streaming/stream.ts+3 added-1 removed
10 unmodified lines11 const stream = new StreamingApi(writable, readable)12 13 // bun does not cancel response stream when request is canceled, so detect abort by signal14 c.req.raw.signal.addEventListener('abort', () => {15+ if (!stream.closed) {15- stream.abort()16+ stream.abort()17+ }18 })19 // in bun, `c` is destroyed when the request is returned, so hold it until the end of streaming20 contextStash.set(stream.responseReadable, c)21 ;(async () => {15 unmodified linesmodified:src/utils/stream.ts+5 added
11 unmodified lines12 /**13 * Whether the stream has been aborted.14 */15 aborted: boolean = false16+ /**17+ * Whether the stream has been closed normally.18+ */19+ closed: boolean = false20 21 constructor(writable: WritableStream, _readable: ReadableStream) {22 this.writable = writable23 this.writer = writable.getWriter()45 unmodified lines69 await this.writer.close()70 } catch (e) {71 // Do nothing. If you want to handle errors, create a stream by yourself.72 }73+ this.closed = true74 }75 76 async pipe(body: ReadableStream) {77 this.writer.releaseLock()19 unmodified lines