Skip to content

Commit aaa7547

Browse files
committed
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 <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent cc26957 commit aaa7547

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}
@@ -1549,6 +1555,7 @@ where `secureSocket` has the same API as `pair.cleartext`.
15491555
[`server.getTicketKeys()`]: #tls_server_getticketkeys
15501556
[`server.listen()`]: net.html#net_server_listen
15511557
[`server.setTicketKeys()`]: #tls_server_setticketkeys_keys
1558+
[`socket.setTimeout(timeout)`]: #net_socket_settimeout_timeout_callback
15521559
[`tls.DEFAULT_ECDH_CURVE`]: #tls_tls_default_ecdh_curve
15531560
[`tls.Server`]: #tls_class_tls_server
15541561
[`tls.TLSSocket.getPeerCertificate()`]: #tls_tlssocket_getpeercertificate_detailed

lib/_tls_wrap.js

+5
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,11 @@ exports.connect = function connect(...args) {
12561256
localAddress: options.localAddress,
12571257
lookup: options.lookup
12581258
};
1259+
1260+
if (options.timeout) {
1261+
tlssock.setTimeout(options.timeout);
1262+
}
1263+
12591264
tlssock.connect(connectOpt, tlssock._start);
12601265
}
12611266

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)