Skip to content

Commit 58952a1

Browse files
lpincaaddaleax
authored andcommitted
tls: make tls.connect() accept a timeout option
If specified, and only when a socket is created internally, the option will make `socket.setTimeout()` to be called on the created socket with the given timeout. This is consistent with the `timeout` option of `net.connect()` and prevents the `timeout` option of the `https.Agent` from being ignored when a socket is created. PR-URL: #25517 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent faa1776 commit 58952a1

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

doc/api/tls.md

+7
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,9 @@ being issued by trusted CA (`options.ca`).
10231023
<!-- YAML
10241024
added: v0.11.3
10251025
changes:
1026+
- version: REPLACEME
1027+
pr-url: https://github.com/nodejs/node/pull/25517
1028+
description: The `timeout` option is supported now.
10261029
- version: v8.0.0
10271030
pr-url: https://github.com/nodejs/node/pull/12839
10281031
description: The `lookup` option is supported now.
@@ -1088,6 +1091,9 @@ changes:
10881091
`tls.createSecureContext()`.
10891092
* `lookup`: {Function} Custom lookup function. **Default:**
10901093
[`dns.lookup()`][].
1094+
* `timeout`: {number} If set and if a socket is created internally, will call
1095+
[`socket.setTimeout(timeout)`][] after the socket is created, but before it
1096+
starts the connection.
10911097
* ...: [`tls.createSecureContext()`][] options that are used if the
10921098
`secureContext` option is missing, otherwise they are ignored.
10931099
* `callback` {Function}
@@ -1547,6 +1553,7 @@ where `secureSocket` has the same API as `pair.cleartext`.
15471553
[`server.getTicketKeys()`]: #tls_server_getticketkeys
15481554
[`server.listen()`]: net.html#net_server_listen
15491555
[`server.setTicketKeys()`]: #tls_server_setticketkeys_keys
1556+
[`socket.setTimeout(timeout)`]: #net_socket_settimeout_timeout_callback
15501557
[`tls.DEFAULT_ECDH_CURVE`]: #tls_tls_default_ecdh_curve
15511558
[`tls.Server`]: #tls_class_tls_server
15521559
[`tls.TLSSocket.getPeerCertificate()`]: #tls_tlssocket_getpeercertificate_detailed

lib/_tls_wrap.js

+5
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,11 @@ exports.connect = function connect(...args) {
12511251
localAddress: options.localAddress,
12521252
lookup: options.lookup
12531253
};
1254+
1255+
if (options.timeout) {
1256+
tlssock.setTimeout(options.timeout);
1257+
}
1258+
12541259
tlssock.connect(connectOpt, tlssock._start);
12551260
}
12561261

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
// This test verifies that `tls.connect()` honors the `timeout` option when the
6+
// socket is internally created.
7+
8+
if (!common.hasCrypto)
9+
common.skip('missing crypto');
10+
11+
const assert = require('assert');
12+
const tls = require('tls');
13+
14+
const socket = tls.connect({
15+
lookup: () => {},
16+
timeout: 1000
17+
});
18+
19+
assert.strictEqual(socket.timeout, 1000);

0 commit comments

Comments
 (0)