Skip to content

Commit

Permalink
tls: document and test option-less createServer
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
sam-github authored and MylesBorins committed Jan 24, 2017
1 parent 85fe05e commit 5421d6e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion doc/api/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ publicly trusted list of CAs as given in
<http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt>.


## tls.createServer(options[, secureConnectionListener])
## tls.createServer([options][, secureConnectionListener])
<!-- YAML
added: v0.3.2
-->
Expand Down
17 changes: 9 additions & 8 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,18 +745,19 @@ TLSSocket.prototype.getProtocol = function() {
// "PATH_LENGTH_EXCEEDED", "INVALID_PURPOSE" "CERT_UNTRUSTED",
// "CERT_REJECTED"
//
function Server(/* [options], listener */) {
var options, listener;
function Server(options, listener) {
if (!(this instanceof Server))
return new Server(options, listener);

if (arguments[0] !== null && typeof arguments[0] === 'object') {
options = arguments[0];
listener = arguments[1];
} else if (typeof arguments[0] === 'function') {
if (typeof options === 'function') {
listener = options;
options = {};
listener = arguments[0];
} else if (options == null || typeof options === 'object') {
options = options || {};
} else {
throw new TypeError('options must be an object');
}

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

this._contexts = [];

Expand Down
19 changes: 17 additions & 2 deletions test/parallel/test-tls-no-cert-required.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var assert = require('assert');
var common = require('../common');

if (!common.hasCrypto) {
Expand All @@ -10,6 +11,20 @@ var tls = require('tls');
// Omitting the cert or pfx option to tls.createServer() should not throw.
// AECDH-NULL-SHA is a no-authentication/no-encryption cipher and hence
// doesn't need a certificate.
tls.createServer({ ciphers: 'AECDH-NULL-SHA' }).listen(0, function() {
tls.createServer({ ciphers: 'AECDH-NULL-SHA' })
.listen(0, common.mustCall(close));

tls.createServer(assert.fail)
.listen(0, common.mustCall(close));

tls.createServer({})
.listen(0, common.mustCall(close));

assert.throws(() => tls.createServer('this is not valid'), TypeError);

tls.createServer()
.listen(0, common.mustCall(close));

function close() {
this.close();
});
}

0 comments on commit 5421d6e

Please sign in to comment.