Skip to content

Commit 980acb4

Browse files
sam-githubitaloacasas
authored andcommitted
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 41e1e6e commit 980acb4

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
@@ -959,7 +959,7 @@ publicly trusted list of CAs as given in
959959
<http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt>.
960960

961961

962-
## tls.createServer(options[, secureConnectionListener])
962+
## tls.createServer([options][, secureConnectionListener])
963963
<!-- YAML
964964
added: v0.3.2
965965
-->

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)