From 234d1b304530333b85dbe88b75c91d3efe851602 Mon Sep 17 00:00:00 2001 From: cirospaciari Date: Sun, 2 Jul 2023 23:37:24 -0300 Subject: [PATCH] add TLSSocket socket first parameter --- src/js/node/net.js | 24 ++++++++++++++++++------ src/js/node/tls.js | 8 +++++--- src/js/out/modules/node/net.js | 25 +++++++++++++++---------- src/js/out/modules/node/tls.js | 8 +++++--- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/js/node/net.js b/src/js/node/net.js index 96f1e039493ad6..1b7742dd17a537 100644 --- a/src/js/node/net.js +++ b/src/js/node/net.js @@ -300,9 +300,10 @@ const Socket = (function (InternalSocket) { _handle; _parent; _parentWrap; + #socket; constructor(options) { - const { signal, write, read, allowHalfOpen = false, ...opts } = options || {}; + const { socket, signal, write, read, allowHalfOpen = false, ...opts } = options || {}; super({ ...opts, allowHalfOpen, @@ -313,6 +314,9 @@ const Socket = (function (InternalSocket) { this._parent = this; this._parentWrap = this; this.#pendingRead = undefined; + if (socket instanceof Socket) { + this.#socket = socket; + } signal?.once("abort", () => this.destroy()); this.once("connect", () => this.emit("ready")); } @@ -342,7 +346,7 @@ const Socket = (function (InternalSocket) { connect(port, host, connectListener) { var path; - var connection; + var connection = this.#socket; if (typeof port === "string") { path = port; port = undefined; @@ -383,9 +387,6 @@ const Socket = (function (InternalSocket) { this.servername = servername; if (socket) { - if (typeof socket !== "object" || !(socket instanceof Socket) || typeof socket[bunTlsSymbol] === "function") { - throw new TypeError("socket must be an instance of net.Socket"); - } connection = socket; } } @@ -415,9 +416,20 @@ const Socket = (function (InternalSocket) { } else { tls.rejectUnauthorized = rejectUnauthorized; tls.requestCert = true; + if (!connection && tls.socket) { + connection = tls.socket; + } + } + } + if (connection) { + if ( + typeof connection !== "object" || + !(connection instanceof Socket) || + typeof connection[bunTlsSymbol] === "function" + ) { + throw new TypeError("socket must be an instance of net.Socket"); } } - this.authorized = false; this.secureConnecting = true; this._secureEstablished = false; diff --git a/src/js/node/tls.js b/src/js/node/tls.js index 50df62667c6c86..5b9b551e535eb7 100644 --- a/src/js/node/tls.js +++ b/src/js/node/tls.js @@ -317,17 +317,18 @@ const TLSSocket = (function (InternalTLSSocket) { class TLSSocket extends InternalTCPSocket { #secureContext; ALPNProtocols; + #socket; constructor(socket, options) { - super(options || socket); + super(socket instanceof InternalTCPSocket ? options : options || socket); options = options || socket || {}; - if (options) { + if (typeof options === "object") { const { ALPNProtocols } = options; if (ALPNProtocols) { convertALPNProtocols(ALPNProtocols, this); } if (socket instanceof InternalTCPSocket) { - options.socket = socket; + this.#socket = socket; } } @@ -401,6 +402,7 @@ const TLSSocket = (function (InternalTLSSocket) { [buntls](port, host) { return { + socket: this.#socket, ALPNProtocols: this.ALPNProtocols, serverName: this.servername || host || "localhost", ...this.#secureContext, diff --git a/src/js/out/modules/node/net.js b/src/js/out/modules/node/net.js index e25b8a3d9a003a..c34f86b04bce40 100644 --- a/src/js/out/modules/node/net.js +++ b/src/js/out/modules/node/net.js @@ -172,15 +172,18 @@ var isIPv4 = function(s) { _handle; _parent; _parentWrap; + #socket; constructor(options) { - const { signal, write, read, allowHalfOpen = !1, ...opts } = options || {}; + const { socket, signal, write, read, allowHalfOpen = !1, ...opts } = options || {}; super({ ...opts, allowHalfOpen, readable: !0, writable: !0 }); - this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = void 0, signal?.once("abort", () => this.destroy()), this.once("connect", () => this.emit("ready")); + if (this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = void 0, socket instanceof Socket2) + this.#socket = socket; + signal?.once("abort", () => this.destroy()), this.once("connect", () => this.emit("ready")); } address() { return { @@ -196,7 +199,7 @@ var isIPv4 = function(s) { this.remotePort = port, socket.data = this, socket.timeout(this.timeout), socket.ref(), this[bunSocketInternal] = socket, this.connecting = !1, this.emit("connect", this), Socket2.#Drain(socket); } connect(port, host, connectListener) { - var path, connection; + var path, connection = this.#socket; if (typeof port === "string") { if (path = port, port = void 0, typeof host === "function") connectListener = host, host = void 0; @@ -224,11 +227,8 @@ var isIPv4 = function(s) { pauseOnConnect, servername } = port; - if (this.servername = servername, socket) { - if (typeof socket !== "object" || !(socket instanceof Socket2) || typeof socket[bunTlsSymbol] === "function") - throw new TypeError("socket must be an instance of net.Socket"); + if (this.servername = servername, socket) connection = socket; - } } if (!pauseOnConnect) this.resume(); @@ -236,14 +236,19 @@ var isIPv4 = function(s) { const bunTLS = this[bunTlsSymbol]; var tls = void 0; if (typeof bunTLS === "function") { - if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls) + if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls) { if (typeof tls !== "object") tls = { rejectUnauthorized, requestCert: !0 }; - else - tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0; + else if (tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0, !connection && tls.socket) + connection = tls.socket; + } + if (connection) { + if (typeof connection !== "object" || !(connection instanceof Socket2) || typeof connection[bunTlsSymbol] === "function") + throw new TypeError("socket must be an instance of net.Socket"); + } if (this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0, connectListener) this.on("secureConnect", connectListener); } else if (connectListener) diff --git a/src/js/out/modules/node/tls.js b/src/js/out/modules/node/tls.js index fd634771fccb7e..cd75883df5ff99 100644 --- a/src/js/out/modules/node/tls.js +++ b/src/js/out/modules/node/tls.js @@ -183,14 +183,15 @@ var createServer = function(options, connectionListener) { }(class TLSSocket2 extends InternalTCPSocket { #secureContext; ALPNProtocols; + #socket; constructor(socket, options) { - super(options || socket); - if (options = options || socket || {}, options) { + super(socket instanceof InternalTCPSocket ? options : options || socket); + if (options = options || socket || {}, typeof options === "object") { const { ALPNProtocols } = options; if (ALPNProtocols) convertALPNProtocols(ALPNProtocols, this); if (socket instanceof InternalTCPSocket) - options.socket = socket; + this.#socket = socket; } this.#secureContext = options.secureContext || createSecureContext(options), this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0; } @@ -237,6 +238,7 @@ var createServer = function(options, connectionListener) { } [buntls](port, host2) { return { + socket: this.#socket, ALPNProtocols: this.ALPNProtocols, serverName: this.servername || host2 || "localhost", ...this.#secureContext