Skip to content

Commit d4050b3

Browse files
committed
tls: document and test option-less createServer
Either the options or the listener argument to tls.createServer() was optional, but not both. This makes no sense, so align the argument checking and documentation with net.createServer(), which accepts the same option sequence, and which tls.createServer() is modelled on. PR-URL: #9800 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent c3839f7 commit d4050b3

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

doc/api/tls.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ publicly trusted list of CAs as given in
948948
<http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt>.
949949

950950

951-
## tls.createServer(options[, secureConnectionListener])
951+
## tls.createServer([options][, secureConnectionListener])
952952
<!-- YAML
953953
added: v0.3.2
954954
-->

lib/_tls_wrap.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -745,18 +745,19 @@ TLSSocket.prototype.getProtocol = function() {
745745
// "PATH_LENGTH_EXCEEDED", "INVALID_PURPOSE" "CERT_UNTRUSTED",
746746
// "CERT_REJECTED"
747747
//
748-
function Server(/* [options], listener */) {
749-
var options, listener;
748+
function Server(options, listener) {
749+
if (!(this instanceof Server))
750+
return new Server(options, listener);
750751

751-
if (arguments[0] !== null && typeof arguments[0] === 'object') {
752-
options = arguments[0];
753-
listener = arguments[1];
754-
} else if (typeof arguments[0] === 'function') {
752+
if (typeof options === 'function') {
753+
listener = options;
755754
options = {};
756-
listener = arguments[0];
755+
} else if (options == null || typeof options === 'object') {
756+
options = options || {};
757+
} else {
758+
throw new TypeError('options must be an object');
757759
}
758760

759-
if (!(this instanceof Server)) return new Server(options, listener);
760761

761762
this._contexts = [];
762763

test/parallel/test-tls-no-cert-required.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
var assert = require('assert');
23
var common = require('../common');
34

45
if (!common.hasCrypto) {
@@ -10,6 +11,20 @@ var tls = require('tls');
1011
// Omitting the cert or pfx option to tls.createServer() should not throw.
1112
// AECDH-NULL-SHA is a no-authentication/no-encryption cipher and hence
1213
// doesn't need a certificate.
13-
tls.createServer({ ciphers: 'AECDH-NULL-SHA' }).listen(0, function() {
14+
tls.createServer({ ciphers: 'AECDH-NULL-SHA' })
15+
.listen(0, common.mustCall(close));
16+
17+
tls.createServer(assert.fail)
18+
.listen(0, common.mustCall(close));
19+
20+
tls.createServer({})
21+
.listen(0, common.mustCall(close));
22+
23+
assert.throws(() => tls.createServer('this is not valid'), TypeError);
24+
25+
tls.createServer()
26+
.listen(0, common.mustCall(close));
27+
28+
function close() {
1429
this.close();
15-
});
30+
}

0 commit comments

Comments
 (0)