Skip to content

Commit bf1ed7e

Browse files
tadjik1richardlau
authored andcommitted
tls: forward keepAlive, keepAliveInitialDelay, noDelay to socket
`tls.connect()` silently ignores `keepAlive`, `keepAliveInitialDelay`, and `noDelay` options. The documentation states it accepts any `socket.connect()` option, and `net.createConnection()` with the same options works correctly. Forward the options through both code paths so `net.Socket`'s constructor stores them on the internal symbols (`kSetNoDelay`, `kSetKeepAlive`, `kSetKeepAliveInitialDelay`), which `afterConnect()` then applies to the handle. Fixes: #62003 PR-URL: #62004 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
1 parent e55edde commit bf1ed7e

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

lib/internal/net.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ function isLoopback(host) {
100100

101101
module.exports = {
102102
kReinitializeHandle: Symbol('kReinitializeHandle'),
103+
kSetNoDelay: Symbol('kSetNoDelay'),
104+
kSetKeepAlive: Symbol('kSetKeepAlive'),
105+
kSetKeepAliveInitialDelay: Symbol('kSetKeepAliveInitialDelay'),
103106
isIP,
104107
isIPv4,
105108
isIPv6,

lib/internal/tls/wrap.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,9 @@ function TLSSocket(socket, opts) {
590590
highWaterMark: tlsOptions.highWaterMark,
591591
onread: !socket ? tlsOptions.onread : null,
592592
signal: tlsOptions.signal,
593+
noDelay: tlsOptions.noDelay,
594+
keepAlive: tlsOptions.keepAlive,
595+
keepAliveInitialDelay: tlsOptions.keepAliveInitialDelay,
593596
});
594597

595598
// Proxy for API compatibility
@@ -1755,6 +1758,9 @@ exports.connect = function connect(...args) {
17551758
highWaterMark: options.highWaterMark,
17561759
onread: options.onread,
17571760
signal: options.signal,
1761+
noDelay: options.noDelay,
1762+
keepAlive: options.keepAlive,
1763+
keepAliveInitialDelay: options.keepAliveInitialDelay,
17581764
});
17591765

17601766
// rejectUnauthorized property can be explicitly defined as `undefined`

lib/net.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ let debug = require('internal/util/debuglog').debuglog('net', (fn) => {
4848
});
4949
const {
5050
kReinitializeHandle,
51+
kSetNoDelay,
52+
kSetKeepAlive,
53+
kSetKeepAliveInitialDelay,
5154
isIP,
5255
isIPv4,
5356
isIPv6,
@@ -356,9 +359,6 @@ function closeSocketHandle(self, isException, isCleanupPending = false) {
356359

357360
const kBytesRead = Symbol('kBytesRead');
358361
const kBytesWritten = Symbol('kBytesWritten');
359-
const kSetNoDelay = Symbol('kSetNoDelay');
360-
const kSetKeepAlive = Symbol('kSetKeepAlive');
361-
const kSetKeepAliveInitialDelay = Symbol('kSetKeepAliveInitialDelay');
362362
const kSetTOS = Symbol('kSetTOS');
363363

364364
function Socket(options) {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
6+
// This test verifies that tls.connect() forwards keepAlive,
7+
// keepAliveInitialDelay, and noDelay options to the underlying socket.
8+
9+
if (!common.hasCrypto)
10+
common.skip('missing crypto');
11+
12+
const assert = require('assert');
13+
const tls = require('tls');
14+
const fixtures = require('../common/fixtures');
15+
const {
16+
kSetNoDelay,
17+
kSetKeepAlive,
18+
kSetKeepAliveInitialDelay,
19+
} = require('internal/net');
20+
21+
const key = fixtures.readKey('agent1-key.pem');
22+
const cert = fixtures.readKey('agent1-cert.pem');
23+
24+
// Test: keepAlive, keepAliveInitialDelay, and noDelay
25+
{
26+
const server = tls.createServer({ key, cert }, (socket) => {
27+
socket.end();
28+
});
29+
30+
server.listen(0, common.mustCall(() => {
31+
const socket = tls.connect({
32+
port: server.address().port,
33+
rejectUnauthorized: false,
34+
keepAlive: true,
35+
keepAliveInitialDelay: 1000,
36+
noDelay: true,
37+
}, common.mustCall(() => {
38+
assert.strictEqual(socket[kSetKeepAlive], true);
39+
assert.strictEqual(socket[kSetKeepAliveInitialDelay], 1);
40+
assert.strictEqual(socket[kSetNoDelay], true);
41+
socket.destroy();
42+
server.close();
43+
}));
44+
}));
45+
}
46+
47+
// Test: defaults (options not set)
48+
{
49+
const server = tls.createServer({ key, cert }, (socket) => {
50+
socket.end();
51+
});
52+
53+
server.listen(0, common.mustCall(() => {
54+
const socket = tls.connect({
55+
port: server.address().port,
56+
rejectUnauthorized: false,
57+
}, common.mustCall(() => {
58+
assert.strictEqual(socket[kSetKeepAlive], false);
59+
assert.strictEqual(socket[kSetKeepAliveInitialDelay], 0);
60+
assert.strictEqual(socket[kSetNoDelay], false);
61+
socket.destroy();
62+
server.close();
63+
}));
64+
}));
65+
}

0 commit comments

Comments
 (0)