summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Bjorn Lu <bjornlu.dev@gmail.com> 2023-08-08 15:06:20 +0800
committerGravatar Emanuele Stoppa <my.burning@gmail.com> 2023-08-08 11:04:17 +0100
commit70f34f5a355f42526ee9e5355f3de8e510002ea2 (patch)
tree72d227d958b136c0fa6b95e647fae0a9ff3c1a6d
parentb675acb2aa820448e9c0d363339a37fbac873215 (diff)
downloadastro-70f34f5a355f42526ee9e5355f3de8e510002ea2.tar.gz
astro-70f34f5a355f42526ee9e5355f3de8e510002ea2.tar.zst
astro-70f34f5a355f42526ee9e5355f3de8e510002ea2.zip
Remove StreamingCompatibleResponse polyfill (#7981)
-rw-r--r--.changeset/purple-buses-prove.md5
-rw-r--r--packages/astro/src/runtime/server/render/page.ts3
-rw-r--r--packages/astro/src/runtime/server/response.ts80
3 files changed, 6 insertions, 82 deletions
diff --git a/.changeset/purple-buses-prove.md b/.changeset/purple-buses-prove.md
new file mode 100644
index 000000000..c1e4876c9
--- /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 cabbe8dae..74e8a45b7 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 bcfda19aa..000000000
--- 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<string> {
- 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<ArrayBuffer> {
- 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);