diff --git a/lib/_http_server.js b/lib/_http_server.js index 4195b10001256d..038845de2908f2 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -78,6 +78,9 @@ const { ERR_INVALID_ARG_VALUE, ERR_INVALID_CHAR } = codes; +const { + kEmptyObject, +} = require('internal/util'); const { validateInteger, validateBoolean, @@ -432,9 +435,6 @@ function storeHTTPOptions(options) { validateBoolean(insecureHTTPParser, 'options.insecureHTTPParser'); this.insecureHTTPParser = insecureHTTPParser; - if (options.noDelay === undefined) - options.noDelay = true; - const requestTimeout = options.requestTimeout; if (requestTimeout !== undefined) { validateInteger(requestTimeout, 'requestTimeout', 0); @@ -501,9 +501,9 @@ function Server(options, requestListener) { if (typeof options === 'function') { requestListener = options; - options = {}; + options = kEmptyObject; } else if (options == null) { - options = {}; + options = kEmptyObject; } else { validateObject(options, 'options'); } @@ -511,7 +511,7 @@ function Server(options, requestListener) { storeHTTPOptions.call(this, options); net.Server.call( this, - { allowHalfOpen: true, noDelay: options.noDelay, + { allowHalfOpen: true, noDelay: options.noDelay || true, keepAlive: options.keepAlive, keepAliveInitialDelay: options.keepAliveInitialDelay }); diff --git a/lib/https.js b/lib/https.js index e871b35bcacbc5..dcaeb8abb46e71 100644 --- a/lib/https.js +++ b/lib/https.js @@ -53,25 +53,28 @@ let debug = require('internal/util/debuglog').debuglog('https', (fn) => { debug = fn; }); const { URL, urlToHttpOptions, searchParamsSymbol } = require('internal/url'); +const { validateObject } = require('internal/validators'); function Server(opts, requestListener) { if (!(this instanceof Server)) return new Server(opts, requestListener); if (typeof opts === 'function') { requestListener = opts; - opts = undefined; - } - opts = { ...opts }; - - if (!opts.ALPNProtocols) { - // http/1.0 is not defined as Protocol IDs in IANA - // https://www.iana.org/assignments/tls-extensiontype-values - // /tls-extensiontype-values.xhtml#alpn-protocol-ids - opts.ALPNProtocols = ['http/1.1']; + opts = kEmptyObject; + } else if (opts == null) { + opts = kEmptyObject; + } else { + validateObject(opts, 'options'); } FunctionPrototypeCall(storeHTTPOptions, this, opts); - FunctionPrototypeCall(tls.Server, this, opts, _connectionListener); + FunctionPrototypeCall(tls.Server, this, + { ...opts, + // http/1.0 is not defined as Protocol IDs in IANA + // https://www.iana.org/assignments/tls-extensiontype-values + // /tls-extensiontype-values.xhtml#alpn-protocol-ids + ALPNProtocols: opts.ALPNProtocols || ['http/1.1'] }, + _connectionListener); this.httpAllowHalfOpen = false; diff --git a/test/parallel/test-https-simple.js b/test/parallel/test-https-simple.js index 89707cc6468d68..a65883162f60a2 100644 --- a/test/parallel/test-https-simple.js +++ b/test/parallel/test-https-simple.js @@ -44,6 +44,15 @@ const serverCallback = common.mustCall(function(req, res) { res.end(body); }); +const invalid_options = [ 'foo', 42, true, [] ]; +invalid_options.forEach((option) => { + assert.throws(() => { + new https.Server(option); + }, { + code: 'ERR_INVALID_ARG_TYPE', + }); +}); + const server = https.createServer(options, serverCallback); server.listen(0, common.mustCall(() => {