0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

workaround(node ssr): cancellation support for renderToAsyncIterable (#10319)

* workaround(node ssr): cancellation support for renderToAsyncIterable

* add changeset

* Update .changeset/nice-pets-tie.md
This commit is contained in:
Arsh 2024-03-04 21:15:13 +05:30 committed by GitHub
parent 9076dc821c
commit 19ecccedaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes an issue where streaming SSR responses sometimes failed with "`iterator.result` is not a function" on node-based adapters.

View file

@ -201,10 +201,14 @@ export async function renderToAsyncIterable(
// The `next` is an object `{ promise, resolve, reject }` that we use to wait
// for chunks to be pushed into the buffer.
let next = promiseWithResolvers<void>();
// keep track of whether the client connection is still interested in the response.
let cancelled = false;
const buffer: Uint8Array[] = []; // []Uint8Array
const iterator = {
const iterator: AsyncIterator<Uint8Array> = {
async next() {
if (cancelled) return { done: true, value: undefined };
await next.promise;
// If an error occurs during rendering, throw the error as we cannot proceed.
@ -238,6 +242,10 @@ export async function renderToAsyncIterable(
return returnValue;
},
async return() {
cancelled = true;
return { done: true, value: undefined };
}
};
const destination: RenderDestination = {