diff --git a/packages/request-state/src/runtime/middleware.ts b/packages/request-state/src/runtime/middleware.ts index f03da1c9..f7efd142 100644 --- a/packages/request-state/src/runtime/middleware.ts +++ b/packages/request-state/src/runtime/middleware.ts @@ -1,8 +1,10 @@ -import type { ReadableStream } from 'node:stream/web'; +import type { ReadableStream as Stream } from 'node:stream/web'; import { defineMiddleware } from 'astro/middleware'; import { collectState } from './serverState.js'; import { parse } from 'content-type'; +declare const ReadableStream: typeof Stream; + export const onRequest = defineMiddleware(async (_, next) => { const { getState, result } = await collectState(next); @@ -26,6 +28,11 @@ export const onRequest = defineMiddleware(async (_, next) => { } } - // @ts-expect-error generator not assignable to ReadableStream - return new Response(render(), result); + if (typeof ReadableStream === 'undefined') { + // @ts-expect-error generator not assignable to ReadableStream + return new Response(render(), result); + } + + // @ts-expect-error ReadableStream from Node doesn't match ReadableStream from Web API + return new Response(ReadableStream.from(render()), result); });