-
-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(streaming): call stream.abort() explicitly when request is aborted (
#3042) * feat(utils/stream): enable to abort streaming manually * feat(utils/stream): prevent multiple aborts, and enable to get the abort status * fix(streaming): call `stream.abort()` explicitly when request is aborted * test: add tests for streaming * docs(stream): add comments * test: add --allow-net to deno test command in ci.yml * test(streaming): update test code * test(stream): retry flaky test up to 3 times at "bun" * test(streaming): refactor test to use afterEach * fix(streaming): in bun, `c` is destroyed when the request is returned, so hold it until the end of streaming * refactor(streaming): tweaks code layout
- Loading branch information
Showing
11 changed files
with
318 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Hono } from '../../src/hono.ts' | ||
import { assertEquals } from './deps.ts' | ||
import { stream, streamSSE } from '../../src/helper/streaming/index.ts' | ||
|
||
Deno.test('Shuld call onAbort via stream', async () => { | ||
const app = new Hono() | ||
let aborted = false | ||
app.get('/stream', (c) => { | ||
return stream(c, async (stream) => { | ||
stream.onAbort(() => { | ||
aborted = true | ||
}) | ||
return new Promise<void>((resolve) => { | ||
stream.onAbort(resolve) | ||
}) | ||
}) | ||
}) | ||
|
||
const server = Deno.serve({ port: 0 }, app.fetch) | ||
const ac = new AbortController() | ||
const req = new Request(`http://localhost:${server.addr.port}/stream`, { | ||
signal: ac.signal, | ||
}) | ||
assertEquals | ||
const res = fetch(req).catch(() => {}) | ||
assertEquals(aborted, false) | ||
await new Promise((resolve) => setTimeout(resolve, 10)) | ||
ac.abort() | ||
await res | ||
while (!aborted) { | ||
await new Promise((resolve) => setTimeout(resolve)) | ||
} | ||
assertEquals(aborted, true) | ||
|
||
await server.shutdown() | ||
}) | ||
|
||
Deno.test('Shuld call onAbort via streamSSE', async () => { | ||
const app = new Hono() | ||
let aborted = false | ||
app.get('/stream', (c) => { | ||
return streamSSE(c, async (stream) => { | ||
stream.onAbort(() => { | ||
aborted = true | ||
}) | ||
return new Promise<void>((resolve) => { | ||
stream.onAbort(resolve) | ||
}) | ||
}) | ||
}) | ||
|
||
const server = Deno.serve({ port: 0 }, app.fetch) | ||
const ac = new AbortController() | ||
const req = new Request(`http://localhost:${server.addr.port}/stream`, { | ||
signal: ac.signal, | ||
}) | ||
assertEquals | ||
const res = fetch(req).catch(() => {}) | ||
assertEquals(aborted, false) | ||
await new Promise((resolve) => setTimeout(resolve, 10)) | ||
ac.abort() | ||
await res | ||
while (!aborted) { | ||
await new Promise((resolve) => setTimeout(resolve)) | ||
} | ||
assertEquals(aborted, true) | ||
|
||
await server.shutdown() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.