Skip to content

Commit

Permalink
Fixes #6200
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Nov 8, 2023
1 parent 131b96b commit 9d160d4
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions src/js/thirdparty/node-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit 9d160d4

Please sign in to comment.