Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/js/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ const kSetKeepAliveInitialDelay = Symbol("kSetKeepAliveInitialDelay");
const kConnectOptions = Symbol("connect-options");
const kAttach = Symbol("kAttach");
const kCloseRawConnection = Symbol("kCloseRawConnection");
const kOnUpgradedClose = Symbol("kOnUpgradedClose");
const kupgraded = Symbol("kupgraded");
const kAdoptedTLSRaw = Symbol("kAdoptedTLSRaw");
const ksocket = Symbol("ksocket");
Expand Down Expand Up @@ -256,6 +257,13 @@ function detachSocket(self) {
function destroyNT(self, err) {
self.destroy(err);
}
// Node's wrap 'close' -> destroy(): https://github.com/nodejs/node/blob/v26.3.0/lib/internal/tls/wrap.js#L739-L741
function onUpgradedClose(self, connection) {
if (self[kupgraded] === connection) self.destroy();
}
function destroyWhenUpgradedCloses(self, connection) {
connection.once("close", (self[kOnUpgradedClose] = onUpgradedClose.bind(null, self, connection)));
Comment thread
claude[bot] marked this conversation as resolved.
}
let addAbortListener;
function destroyWhenAborted(err) {
if (!this.destroyed) {
Expand Down Expand Up @@ -1582,6 +1590,7 @@ function Socket(options?) {
this._parent = null;
this._parentWrap = null;
this[kupgraded] = null;
this[kOnUpgradedClose] = undefined;

this[kSetNoDelay] = Boolean(noDelay);
this[kSetKeepAlive] = Boolean(keepAlive);
Expand Down Expand Up @@ -1888,6 +1897,8 @@ Socket.prototype[kAttach] = function (port, socket) {

Socket.prototype[kCloseRawConnection] = function () {
const connection = this[kupgraded];
// Only a destroy the connection's owner started counts as it closing under this socket.
if (!connection.destroyed) connection.removeListener("close", this[kOnUpgradedClose]);
connection.connecting = false;
connection._handle = null;
connection.unref();
Expand Down Expand Up @@ -2023,6 +2034,7 @@ Socket.prototype.connect = function connect(...args) {
connection.on("end", events[1]);
connection.on("drain", events[2]);
connection.on("close", events[3]);
destroyWhenUpgradedCloses(this, connection);
this._handle = result;
} else {
// upgradeTLS requires an established socket; a socket that is still
Expand All @@ -2041,6 +2053,7 @@ Socket.prototype.connect = function connect(...args) {
// replace socket
connection._handle = raw;
raw[kAdoptedTLSRaw] = true;
destroyWhenUpgradedCloses(this, connection);
this.once("end", this[kCloseRawConnection]);
raw.connecting = false;
this._handle = tls;
Expand Down Expand Up @@ -2074,6 +2087,7 @@ Socket.prototype.connect = function connect(...args) {
connection.on("end", events[1]);
connection.on("drain", events[2]);
connection.on("close", events[3]);
destroyWhenUpgradedCloses(this, connection);
this._handle = result;
} else {
this[kupgraded] = connection;
Expand All @@ -2088,6 +2102,7 @@ Socket.prototype.connect = function connect(...args) {
// replace socket
connection._handle = raw;
raw[kAdoptedTLSRaw] = true;
destroyWhenUpgradedCloses(this, connection);
this.once("end", this[kCloseRawConnection]);
raw.connecting = false;
this._handle = tls;
Expand Down Expand Up @@ -2384,6 +2399,7 @@ Socket.prototype[Symbol.for("::bunUpgradeServerTLS::")] = function (connection,
connection.on("end", events[1]);
connection.on("drain", events[2]);
connection.on("close", events[3]);
destroyWhenUpgradedCloses(this, connection);
this[kupgraded] = connection;
this._handle = result;
return;
Expand Down Expand Up @@ -2413,6 +2429,7 @@ Socket.prototype[Symbol.for("::bunUpgradeServerTLS::")] = function (connection,
connection.on("end", events[1]);
connection.on("drain", events[2]);
connection.on("close", events[3]);
destroyWhenUpgradedCloses(this, connection);
this._handle = result;
this.emit(kUpgradeAttached);
return;
Expand All @@ -2436,6 +2453,7 @@ Socket.prototype[Symbol.for("::bunUpgradeServerTLS::")] = function (connection,
const [raw, tlsHandle] = result;
connection._handle = raw;
raw[kAdoptedTLSRaw] = true;
destroyWhenUpgradedCloses(this, connection);
this.once("end", this[kCloseRawConnection]);
raw.connecting = false;
this._handle = tlsHandle;
Expand Down
11 changes: 10 additions & 1 deletion src/js/node/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,16 @@ function TLSSocket(socket?, options?) {
throw $ERR_INVALID_ARG_TYPE("socket", "Duplex", socket);
}

options = isNetSocketOrDuplex ? { ...options, allowHalfOpen: false } : options || socket || {};
// The wrapped socket's allowHalfOpen wins: https://github.com/nodejs/node/blob/v26.3.0/lib/internal/tls/wrap.js#L592
if (isNetSocketOrDuplex) {
options = { ...options, allowHalfOpen: socket.allowHalfOpen };
} else {
options = options || socket || {};
const wrapped = options.socket;
if (wrapped instanceof Duplex) {
options = { ...options, allowHalfOpen: wrapped.allowHalfOpen };
}
}

this._rejectUnauthorized = !!options.rejectUnauthorized;

Expand Down
Loading
Loading