diff --git a/.changeset/purple-buses-prove.md b/.changeset/purple-buses-prove.md new file mode 100644 index 000000000000..c1e4876c9c53 --- /dev/null +++ b/.changeset/purple-buses-prove.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Remove StreamingCompatibleResponse polyfill diff --git a/packages/astro/src/runtime/server/render/page.ts b/packages/astro/src/runtime/server/render/page.ts index cabbe8dae72d..74e8a45b7691 100644 --- a/packages/astro/src/runtime/server/render/page.ts +++ b/packages/astro/src/runtime/server/render/page.ts @@ -2,7 +2,6 @@ import type { RouteData, SSRResult } from '../../../@types/astro'; import { renderComponentToString, type NonAstroPageComponent } from './component.js'; import type { AstroComponentFactory } from './index'; -import { createResponse } from '../response.js'; import { isAstroComponentFactory } from './astro/index.js'; import { renderToReadableStream, renderToString } from './astro/render.js'; import { encoder } from './common.js'; @@ -64,6 +63,6 @@ export async function renderPage( body = encoder.encode(body); headers.set('Content-Length', body.byteLength.toString()); } - const response = createResponse(body, { ...init, headers }); + const response = new Response(body, { ...init, headers }); return response; } diff --git a/packages/astro/src/runtime/server/response.ts b/packages/astro/src/runtime/server/response.ts deleted file mode 100644 index bcfda19aa13d..000000000000 --- a/packages/astro/src/runtime/server/response.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { streamAsyncIterator } from './util.js'; - -const isNodeJS = - typeof process === 'object' && Object.prototype.toString.call(process) === '[object process]'; - -let StreamingCompatibleResponse: typeof Response | undefined; - -function createResponseClass() { - StreamingCompatibleResponse = class extends Response { - #isStream: boolean; - #body: any; - constructor(body?: BodyInit | null, init?: ResponseInit) { - let isStream = body instanceof ReadableStream; - super(isStream ? null : body, init); - this.#isStream = isStream; - this.#body = body; - } - - get body() { - return this.#body; - } - - async text(): Promise { - if (this.#isStream && isNodeJS) { - let decoder = new TextDecoder(); - let body = this.#body; - let out = ''; - for await (let chunk of streamAsyncIterator(body)) { - out += decoder.decode(chunk); - } - return out; - } - return super.text(); - } - - async arrayBuffer(): Promise { - if (this.#isStream && isNodeJS) { - let body = this.#body; - let chunks: Uint8Array[] = []; - let len = 0; - for await (let chunk of streamAsyncIterator(body)) { - chunks.push(chunk); - len += chunk.length; - } - let ab = new Uint8Array(len); - let offset = 0; - for (const chunk of chunks) { - ab.set(chunk, offset); - offset += chunk.length; - } - return ab; - } - return super.arrayBuffer(); - } - - clone() { - return new StreamingCompatibleResponse!(this.#body, { - status: this.status, - statusText: this.statusText, - headers: this.headers, - }); - } - }; - - return StreamingCompatibleResponse; -} - -type CreateResponseFn = (body?: BodyInit | null, init?: ResponseInit) => Response; - -export const createResponse: CreateResponseFn = isNodeJS - ? (body, init) => { - if (typeof body === 'string' || ArrayBuffer.isView(body)) { - return new Response(body, init); - } - if (typeof StreamingCompatibleResponse === 'undefined') { - return new (createResponseClass())(body, init); - } - return new StreamingCompatibleResponse(body, init); - } - : (body, init) => new Response(body, init);