From 9d160d4af65016766bb925bac29d42405fd7cecd Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 8 Nov 2023 02:55:23 -0800 Subject: [PATCH] Fixes #6200 --- src/js/thirdparty/node-fetch.ts | 55 ++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/src/js/thirdparty/node-fetch.ts b/src/js/thirdparty/node-fetch.ts index 55a77e91f18967..9c634f6f3ae8f4 100644 --- a/src/js/thirdparty/node-fetch.ts +++ b/src/js/thirdparty/node-fetch.ts @@ -30,6 +30,11 @@ const kHeaders = Symbol("kHeaders"); const kBody = Symbol("kBody"); const HeadersPrototype = Headers.prototype; +var BodyReadable; +function loadBodyReadable() { + ({ _ReadableFromWebForUndici: BodyReadable } = require("node:stream")[Symbol.for("::bunternal::")]); +} + class Response extends WebResponse { [kBody]: any; [kHeaders]; @@ -46,10 +51,10 @@ class Response extends WebResponse { get body() { let body = this[kBody]; if (!body) { - const { Readable } = require("node:stream"); - const web = super.body; + var web = super.body; if (!web) return null; - body = this[kBody] = Readable.fromWeb(web); + if (!BodyReadable) loadBodyReadable(); + body = this[kBody] = new BodyReadable({}, web); } return body; @@ -58,8 +63,50 @@ class Response extends WebResponse { get headers() { return (this[kHeaders] ??= Object.setPrototypeOf(super.headers, HeadersPrototype) as any); } + + clone() { + return Object.setPrototypeOf(super.clone(this), ResponsePrototype); + } + + async arrayBuffer() { + // load the getter + this.body; + return await super.arrayBuffer(); + } + + async blob() { + // load the getter + this.body; + return await super.blob(); + } + + async formData() { + // load the getter + this.body; + return await super.formData(); + } + + async json() { + // load the getter + this.body; + return await super.json(); + } + + async text() { + // load the getter + this.body; + return await super.text(); + } + + get type() { + if (!super.ok) { + return "error"; + } + + return "default"; + } } -const ResponsePrototype = Response.prototype; +var ResponsePrototype = Response.prototype; /** * `node-fetch` works like the browser-fetch API, except it's a little more strict on some features,