Skip to content
Myr

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

6 files+121 added−2 removed

modified:runtime_tests/bun/index.test.tsx+30 added
331 unmodified lines
332 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 = true
2 unmodified lines
350 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 })362363 beforeEach(() => {364 aborted = false365 server = Bun.serve({ port: 0, fetch: app.fetch })
18 unmodified lines
384 await new Promise((resolve) => setTimeout(resolve))385 }386 expect(aborted).toBe(true)387 })388389 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 })396397 describe('streamSSE', () => {398 it('Should call onAbort', async () => {
9 unmodified lines
408 await new Promise((resolve) => setTimeout(resolve))409 }410 expect(aborted).toBe(true)411 })412413 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
modified:runtime_tests/node/index.test.ts+40 added
111 unmodified lines
112 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 })124125 const server = createAdaptorServer(app)126127 beforeEach(() => {128 aborted = false129 })130131 it('Should call onAbort', async () => {132 const req = request(server)133 .get('/stream')134 .end(() => {})
5 unmodified lines
140 await new Promise((resolve) => setTimeout(resolve))141 }142 expect(aborted).toBe(true)143 })144145 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})153154describe('streamSSE', () => {155 const app = new Hono()
9 unmodified lines
165 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 })177178 const server = createAdaptorServer(app)179180 beforeEach(() => {181 aborted = false182 })183184 it('Should call onAbort', async () => {185 const req = request(server)186 .get('/stream')187 .end(() => {})
5 unmodified lines
193 await new Promise((resolve) => setTimeout(resolve))194 }195 expect(aborted).toBe(true)196 })197198 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
modified:src/helper/streaming/stream.ts+3 added-1 removed
10 unmodified lines
11 const stream = new StreamingApi(writable, readable)1213 // 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 lines
modified:src/utils/stream.ts+5 added
11 unmodified lines
12 /**13 * Whether the stream has been aborted.14 */15 aborted: boolean = false16 /**17 * Whether the stream has been closed normally.18 */19 closed: boolean = false2021 constructor(writable: WritableStream, _readable: ReadableStream) {22 this.writable = writable23 this.writer = writable.getWriter()
45 unmodified lines
69 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 }7576 async pipe(body: ReadableStream) {77 this.writer.releaseLock()
19 unmodified lines