Remove StreamingCompatibleResponse polyfill (#7981)
This commit is contained in:
parent
b675acb2aa
commit
70f34f5a35
3 changed files with 6 additions and 82 deletions
5
.changeset/purple-buses-prove.md
Normal file
5
.changeset/purple-buses-prove.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Remove StreamingCompatibleResponse polyfill
|
|
@ -2,7 +2,6 @@ import type { RouteData, SSRResult } from '../../../@types/astro';
|
||||||
import { renderComponentToString, type NonAstroPageComponent } from './component.js';
|
import { renderComponentToString, type NonAstroPageComponent } from './component.js';
|
||||||
import type { AstroComponentFactory } from './index';
|
import type { AstroComponentFactory } from './index';
|
||||||
|
|
||||||
import { createResponse } from '../response.js';
|
|
||||||
import { isAstroComponentFactory } from './astro/index.js';
|
import { isAstroComponentFactory } from './astro/index.js';
|
||||||
import { renderToReadableStream, renderToString } from './astro/render.js';
|
import { renderToReadableStream, renderToString } from './astro/render.js';
|
||||||
import { encoder } from './common.js';
|
import { encoder } from './common.js';
|
||||||
|
@ -64,6 +63,6 @@ export async function renderPage(
|
||||||
body = encoder.encode(body);
|
body = encoder.encode(body);
|
||||||
headers.set('Content-Length', body.byteLength.toString());
|
headers.set('Content-Length', body.byteLength.toString());
|
||||||
}
|
}
|
||||||
const response = createResponse(body, { ...init, headers });
|
const response = new Response(body, { ...init, headers });
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
|
Loading…
Reference in a new issue