Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
19 changes: 19 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,14 @@ function detachSocket(self) {
function destroyNT(self, err) {
self.destroy(err);
}
// Node's `wrap.on('close', () => this.destroy())` for the connection a TLS socket was upgraded over:
// https://github.com/nodejs/node/blob/v26.3.0/lib/internal/tls/wrap.js#L739-L741
Comment thread
robobun marked this conversation as resolved.
Outdated
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 +1591,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 +1898,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 +2035,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 +2054,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 +2088,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 +2103,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 +2400,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 +2430,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 +2454,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
14 changes: 13 additions & 1 deletion src/js/node/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,19 @@ function TLSSocket(socket?, options?) {
throw $ERR_INVALID_ARG_TYPE("socket", "Duplex", socket);
}

options = isNetSocketOrDuplex ? { ...options, allowHalfOpen: false } : options || socket || {};
// A wrapped socket's allowHalfOpen wins over the option:
// https://github.com/nodejs/node/blob/v26.3.0/lib/internal/tls/wrap.js#L592
Comment thread
robobun marked this conversation as resolved.
Outdated
if (isNetSocketOrDuplex) {
options = { ...options, allowHalfOpen: socket.allowHalfOpen };
} else {
options = options || socket || {};
// tls.connect() passes its socket here as options.socket; node passes it as the argument above:
// https://github.com/nodejs/node/blob/v26.3.0/lib/internal/tls/wrap.js#L1756-L1757
Comment thread
robobun marked this conversation as resolved.
Outdated
const wrapped = options.socket;
if (wrapped instanceof Duplex) {
options = { ...options, allowHalfOpen: wrapped.allowHalfOpen };
}
}

this._rejectUnauthorized = !!options.rejectUnauthorized;

Expand Down
Loading
Loading