From 266e033d6f271da559f34c67feb91a0af4da4ef8 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 14 Nov 2024 19:03:16 -0800 Subject: [PATCH] node:https: fix prototype chain of Agent (#15160) Co-authored-by: Ciro Spaciari --- src/js/internal/cluster/Worker.ts | 2 +- src/js/node/fs.ts | 5 +++-- src/js/node/http.ts | 2 +- src/js/node/https.ts | 2 +- src/js/node/stream.ts | 6 ++---- test/js/node/http/node-http.test.ts | 10 ++++++++++ 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/js/internal/cluster/Worker.ts b/src/js/internal/cluster/Worker.ts index 14ff825c4d..4249d1364c 100644 --- a/src/js/internal/cluster/Worker.ts +++ b/src/js/internal/cluster/Worker.ts @@ -23,7 +23,7 @@ function Worker(options) { this.process.on("message", (message, handle) => this.emit("message", message, handle)); } } -Worker.prototype = Object.create(EventEmitter.prototype); +$toClass(Worker, "Worker", EventEmitter); Worker.prototype.kill = function () { this.destroy.$apply(this, arguments); diff --git a/src/js/node/fs.ts b/src/js/node/fs.ts index a9126aa871..88b1b7aab3 100644 --- a/src/js/node/fs.ts +++ b/src/js/node/fs.ts @@ -877,7 +877,7 @@ function ReadStream(this: typeof ReadStream, pathOrFd, options) { $assert(overridden_fs); this[kFs] = overridden_fs; } -ReadStream.prototype = Object.create(NativeReadable.prototype); +$toClass(ReadStream, "ReadStream", NativeReadable); ReadStream.prototype._construct = function (callback) { if (NativeReadablePrototype._construct) { @@ -1185,7 +1185,8 @@ var WriteStreamClass = (WriteStream = function WriteStream(path, options = defau }); const NativeWritable = Stream.NativeWritable; -const WriteStreamPrototype = (WriteStream.prototype = Object.create(NativeWritable.prototype)); +$toClass(WriteStream, "WriteStream", NativeWritable); +const WriteStreamPrototype = WriteStream.prototype; Object.defineProperties(WriteStreamPrototype, { autoClose: { diff --git a/src/js/node/http.ts b/src/js/node/http.ts index 7c3cc0a36b..6c15bf58c4 100644 --- a/src/js/node/http.ts +++ b/src/js/node/http.ts @@ -277,7 +277,7 @@ function Agent(options = kEmptyObject) { this.defaultPort = options.defaultPort || 80; this.protocol = options.protocol || "http:"; } -Agent.prototype = Object.create(EventEmitter.prototype); +$toClass(Agent, "Agent", EventEmitter); ObjectDefineProperty(Agent, "globalAgent", { get: function () { diff --git a/src/js/node/https.ts b/src/js/node/https.ts index 9f6c66abbd..9752228462 100644 --- a/src/js/node/https.ts +++ b/src/js/node/https.ts @@ -42,7 +42,7 @@ function Agent(options) { this.maxCachedSessions = this.options.maxCachedSessions; if (this.maxCachedSessions === undefined) this.maxCachedSessions = 100; } -Agent.prototype = Object.create(http.Agent.prototype); +$toClass(Agent, "Agent", http.Agent); Agent.prototype.createConnection = http.createConnection; var https = { diff --git a/src/js/node/stream.ts b/src/js/node/stream.ts index acbf534cf1..874031f49e 100644 --- a/src/js/node/stream.ts +++ b/src/js/node/stream.ts @@ -5441,8 +5441,7 @@ function createNativeStreamReadable(Readable) { ptr.onClose = this[_onClose].bind(this); ptr.onDrain = this[_onDrain].bind(this); } - NativeReadable.prototype = {}; - ObjectSetPrototypeOf(NativeReadable.prototype, Readable.prototype); + $toClass(NativeReadable, "NativeReadable", Readable); NativeReadable.prototype[_onClose] = function () { this.push(null); @@ -5657,8 +5656,7 @@ function NativeWritable(pathOrFdOrSink, options = {}) { this[_pathOrFdOrSink] = pathOrFdOrSink; } -Object.setPrototypeOf(NativeWritable, Writable); -NativeWritable.prototype = Object.create(Writable.prototype); +$toClass(NativeWritable, "NativeWritable", Writable); // These are confusingly two different fns for construct which initially were the same thing because // `_construct` is part of the lifecycle of Writable and is not called lazily, diff --git a/test/js/node/http/node-http.test.ts b/test/js/node/http/node-http.test.ts index af60305a8c..a50a18347a 100644 --- a/test/js/node/http/node-http.test.ts +++ b/test/js/node/http/node-http.test.ts @@ -2390,3 +2390,13 @@ it("must set headersSent to true after headers are sent when using chunk encoded server.close(); } }); + +it("should work when sending https.request with agent:false", async () => { + const { promise, resolve, reject } = Promise.withResolvers(); + const client = https.request("https://example.com/", { agent: false }); + client.on("error", reject); + client.on("close", resolve); + client.end(); + await promise; +}); +