diff options
Diffstat (limited to 'src/js/node/net.js')
-rw-r--r-- | src/js/node/net.js | 120 |
1 files changed, 67 insertions, 53 deletions
diff --git a/src/js/node/net.js b/src/js/node/net.js index f0873ae22..5283538c3 100644 --- a/src/js/node/net.js +++ b/src/js/node/net.js @@ -66,6 +66,11 @@ const bunSocketServerOptions = Symbol.for("::bunnetserveroptions::"); const bunSocketInternal = Symbol.for("::bunnetsocketinternal::"); const bunTLSConnectOptions = Symbol.for("::buntlsconnectoptions::"); +function endNT(socket, callback, err) { + socket.end(); + callback(err); +} + var SocketClass; const Socket = (function (InternalSocket) { SocketClass = InternalSocket; @@ -446,34 +451,11 @@ const Socket = (function (InternalSocket) { } else if (connectListener) this.on("connect", connectListener); // start using existing connection - if (connection) { - const socket = connection[bunSocketInternal]; - - if (socket) { - this.connecting = true; - this.#upgraded = true; - const result = socket.upgradeTLS({ - data: this, - tls, - socket: Socket.#Handlers, - }); - if (result) { - const [raw, tls] = result; - // replace socket - connection[bunSocketInternal] = raw; - raw.timeout(raw.timeout); - raw.connecting = false; - this[bunSocketInternal] = tls; - } else { - this[bunSocketInternal] = null; - throw new Error("Invalid socket"); - } - } else { - // wait to be connected - connection.once("connect", () => { - const socket = connection[bunSocketInternal]; - if (!socket) return; + try { + if (connection) { + const socket = connection[bunSocketInternal]; + if (socket) { this.connecting = true; this.#upgraded = true; const result = socket.upgradeTLS({ @@ -481,7 +463,6 @@ const Socket = (function (InternalSocket) { tls, socket: Socket.#Handlers, }); - if (result) { const [raw, tls] = result; // replace socket @@ -493,38 +474,66 @@ const Socket = (function (InternalSocket) { this[bunSocketInternal] = null; throw new Error("Invalid socket"); } + } else { + // wait to be connected + connection.once("connect", () => { + const socket = connection[bunSocketInternal]; + if (!socket) return; + + this.connecting = true; + this.#upgraded = true; + const result = socket.upgradeTLS({ + data: this, + tls, + socket: Socket.#Handlers, + }); + + if (result) { + const [raw, tls] = result; + // replace socket + connection[bunSocketInternal] = raw; + raw.timeout(raw.timeout); + raw.connecting = false; + this[bunSocketInternal] = tls; + } else { + this[bunSocketInternal] = null; + throw new Error("Invalid socket"); + } + }); + } + } else if (path) { + // start using unix socket + bunConnect({ + data: this, + unix: path, + socket: Socket.#Handlers, + tls, + }).catch(error => { + this.emit("error", error); + this.emit("close"); + }); + } else { + // default start + bunConnect({ + data: this, + hostname: host || "localhost", + port: port, + socket: Socket.#Handlers, + tls, + }).catch(error => { + this.emit("error", error); + this.emit("close"); }); } - } else if (path) { - // start using unix socket - bunConnect({ - data: this, - unix: path, - socket: Socket.#Handlers, - tls, - }).catch(error => { - this.emit("error", error); - this.emit("close"); - }); - } else { - // default start - bunConnect({ - data: this, - hostname: host || "localhost", - port: port, - socket: Socket.#Handlers, - tls, - }).catch(error => { - this.emit("error", error); - this.emit("close"); - }); + } catch (error) { + process.nextTick(emitErrorAndCloseNextTick, this, error); } return this; } _destroy(err, callback) { - this[bunSocketInternal]?.end(); - callback(err); + const socket = this[bunSocketInternal]; + socket && process.nextTick(endNT, socket, callback, err); } _final(callback) { @@ -864,6 +873,11 @@ function emitErrorNextTick(self, error) { self.emit("error", error); } +function emitErrorAndCloseNextTick(self, error) { + self.emit("error", error); + self.emit("close"); +} + function emitListeningNextTick(self, onListen) { if (typeof onListen === "function") { try { |