Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls: make tls.connect() accept a timeout option #25517

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/api/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,9 @@ being issued by trusted CA (`options.ca`).
<!-- YAML
added: v0.11.3
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/25517
description: The `timeout` option is supported now.
- version: v8.0.0
pr-url: https://github.com/nodejs/node/pull/12839
description: The `lookup` option is supported now.
Expand Down Expand Up @@ -1088,6 +1091,9 @@ changes:
`tls.createSecureContext()`.
* `lookup`: {Function} Custom lookup function. **Default:**
[`dns.lookup()`][].
* `timeout`: {number} If set and if a socket is created internally, will call
[`socket.setTimeout(timeout)`][] after the socket is created, but before it
starts the connection.
* ...: [`tls.createSecureContext()`][] options that are used if the
`secureContext` option is missing, otherwise they are ignored.
* `callback` {Function}
Expand Down Expand Up @@ -1541,6 +1547,7 @@ where `secureSocket` has the same API as `pair.cleartext`.
[`server.getTicketKeys()`]: #tls_server_getticketkeys
[`server.listen()`]: net.html#net_server_listen
[`server.setTicketKeys()`]: #tls_server_setticketkeys_keys
[`socket.setTimeout(timeout)`]: #net_socket_settimeout_timeout_callback
[`tls.DEFAULT_ECDH_CURVE`]: #tls_tls_default_ecdh_curve
[`tls.Server`]: #tls_class_tls_server
[`tls.TLSSocket.getPeerCertificate()`]: #tls_tlssocket_getpeercertificate_detailed
Expand Down
5 changes: 5 additions & 0 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,11 @@ exports.connect = function connect(...args) {
localAddress: options.localAddress,
lookup: options.lookup
};

if (options.timeout) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeof options.timeout === 'number' to allow setting a timeout of 0.

Copy link
Member Author

@lpinca lpinca Jan 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 == disabled idle timeout so I'm not sure if it's very useful. I mean it's like not setting it at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, keep it as is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code for tls.connect() is identical to that in net.connect(),

node/lib/net.js

Lines 158 to 160 in 2c7f4f4

if (options.timeout) {
socket.setTimeout(options.timeout);
}
, which is what I'd expect, so LGTM as is.

tlssock.setTimeout(options.timeout);
}

tlssock.connect(connectOpt, tlssock._start);
}

Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-tls-connect-timeout-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const common = require('../common');

// This test verifies that `tls.connect()` honors the `timeout` option when the
// socket is internally created.

if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const tls = require('tls');

const socket = tls.connect({
lookup: () => {},
timeout: 1000
});

assert.strictEqual(socket.timeout, 1000);