Skip to content

Commit 937d582

Browse files
committed
tls: forward keepAlive, keepAliveInitialDelay, noDelay to socket
1 parent abddfc9 commit 937d582

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

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

0 commit comments

Comments
 (0)