Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 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,18 @@ function detachSocket(self) {
function destroyNT(self, err) {
self.destroy(err);
}
// 'close' listener armed on the connection a TLS socket is upgraded over: the
// connection going away (its owner destroyed it, or the transport under a
// wrapped duplex closed) takes the TLS socket with it, like node's wrap 'close'
// listener. The connection's EOF alone does not; that is what an allowHalfOpen
// TLS socket outlives.
// https://github.com/nodejs/node/blob/v26.3.0/lib/internal/tls/wrap.js#L739-L741
Comment thread
robobun marked this conversation as resolved.
Resolved
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.
Resolved
}
let addAbortListener;
function destroyWhenAborted(err) {
if (!this.destroyed) {
Expand Down Expand Up @@ -1582,6 +1595,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 @@ -1886,8 +1900,13 @@ Socket.prototype[kAttach] = function (port, socket) {
SocketHandlers.drain(socket);
};

// 'end' listener of an fd-upgraded TLS socket: retires the net.Socket whose fd
// this socket took over.
Comment thread
robobun marked this conversation as resolved.
Resolved
Socket.prototype[kCloseRawConnection] = function () {
const connection = this[kupgraded];
// The 'close' this retirement emits is not the connection going away under
// this socket; one its owner already started (connection.destroyed) is.
Comment thread
robobun marked this conversation as resolved.
Resolved
if (!connection.destroyed) connection.removeListener("close", this[kOnUpgradedClose]);
connection.connecting = false;
connection._handle = null;
connection.unref();
Expand Down Expand Up @@ -2023,6 +2042,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 +2061,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 +2095,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 +2110,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 +2407,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 +2437,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 +2461,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
16 changes: 15 additions & 1 deletion src/js/node/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,21 @@ function TLSSocket(socket?, options?) {
throw $ERR_INVALID_ARG_TYPE("socket", "Duplex", socket);
}

options = isNetSocketOrDuplex ? { ...options, allowHalfOpen: false } : options || socket || {};
// A wrapped socket decides allowHalfOpen; the option only applies when this
// socket opens its own connection. The wrapped socket is the first argument
// here, or `options.socket` on the tls.connect({ socket }) path (node's
// connect() forwards it as the first argument).
// https://github.com/nodejs/node/blob/v26.3.0/lib/internal/tls/wrap.js#L592
// https://github.com/nodejs/node/blob/v26.3.0/lib/internal/tls/wrap.js#L1756-L1757
Comment thread
robobun marked this conversation as resolved.
Resolved
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