Skip to content

Commit df04d3b

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

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
@@ -967,6 +967,9 @@ similar to:
967967
<!-- YAML
968968
added: v0.11.3
969969
changes:
970+
- version: REPLACEME
971+
pr-url: https://github.com/nodejs/node/pull/25517
972+
description: The `timeout` option is supported now.
970973
- version: v8.0.0
971974
pr-url: https://github.com/nodejs/node/pull/12839
972975
description: The `lookup` option is supported now.
@@ -1029,6 +1032,9 @@ changes:
10291032
`tls.createSecureContext()`.
10301033
* `lookup`: {Function} Custom lookup function. **Default:**
10311034
[`dns.lookup()`][].
1035+
* `timeout`: {number} If set and if a socket is created internally, will call
1036+
[`socket.setTimeout(timeout)`][] after the socket is created, but before it
1037+
starts the connection.
10321038
* ...: [`tls.createSecureContext()`][] options that are used if the
10331039
`secureContext` option is missing, otherwise they are ignored.
10341040
* `callback` {Function}
@@ -1481,6 +1487,7 @@ where `secureSocket` has the same API as `pair.cleartext`.
14811487
[`server.getTicketKeys()`]: #tls_server_getticketkeys
14821488
[`server.listen()`]: net.html#net_server_listen
14831489
[`server.setTicketKeys()`]: #tls_server_setticketkeys_keys
1490+
[`socket.setTimeout(timeout)`]: #net_socket_settimeout_timeout_callback
14841491
[`tls.DEFAULT_ECDH_CURVE`]: #tls_tls_default_ecdh_curve
14851492
[`tls.Server`]: #tls_class_tls_server
14861493
[`tls.TLSSocket.getPeerCertificate()`]: #tls_tlssocket_getpeercertificate_detailed

lib/_tls_wrap.js

+5
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,11 @@ exports.connect = function connect(...args) {
11541154
localPort: options.localPort,
11551155
lookup: options.lookup
11561156
};
1157+
1158+
if (options.timeout) {
1159+
tlssock.setTimeout(options.timeout);
1160+
}
1161+
11571162
tlssock.connect(connectOpt, tlssock._start);
11581163
}
11591164

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)