-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cancel response stream when connection closes (#9071)
* cancel stream on close * add changeset * add test * Update .changeset/modern-ways-develop.md Co-authored-by: Sarah Rainsberger <[email protected]> --------- Co-authored-by: lilnasy <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
eeac288
commit c948713
Showing
6 changed files
with
55 additions
and
233 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/node': patch | ||
--- | ||
|
||
Fixes a bug where the response stream would not cancel when the connection closed |
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 was deleted.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
packages/integrations/node/test/fixtures/api-route/src/pages/hash.ts
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
22 changes: 22 additions & 0 deletions
22
packages/integrations/node/test/fixtures/api-route/src/pages/streaming.ts
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,22 @@ | ||
export const GET = ({ locals }) => { | ||
let sentChunks = 0; | ||
|
||
const readableStream = new ReadableStream({ | ||
async pull(controller) { | ||
if (sentChunks === 3) return controller.close(); | ||
else sentChunks++; | ||
|
||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
controller.enqueue(new TextEncoder().encode('hello\n')); | ||
}, | ||
cancel() { | ||
locals.cancelledByTheServer = true; | ||
} | ||
}); | ||
|
||
return new Response(readableStream, { | ||
headers: { | ||
"Content-Type": "text/event-stream" | ||
} | ||
}); | ||
} |