From 74741fa52bdb9879b57dee85dc47ec16b8c6147f Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sat, 10 Jun 2017 14:09:35 -0400 Subject: [PATCH] https: make opts optional & immutable when create `opts` in `createServer` will be immutable that won't change origional opts value. What's more, it's optional which can make `requestListener` be the first argument. PR-URL: https://github.com/nodejs/node/pull/13599 Fixes: https://github.com/nodejs/node/issues/13584 Reviewed-By: Refael Ackermann Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig Reviewed-By: Yorkie Liu Reviewed-By: James M Snell Reviewed-By: Brian White --- doc/api/https.md | 2 +- lib/https.js | 6 +++ .../test-https-argument-of-creating.js | 44 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-https-argument-of-creating.js diff --git a/doc/api/https.md b/doc/api/https.md index f4000335a00770..f6c56ef8ed7efb 100644 --- a/doc/api/https.md +++ b/doc/api/https.md @@ -46,7 +46,7 @@ added: v8.0.0 See [`http.Server#keepAliveTimeout`][]. -## https.createServer(options[, requestListener]) +## https.createServer([options][, requestListener]) diff --git a/lib/https.js b/lib/https.js index 457327d6bbb729..fb220872598315 100644 --- a/lib/https.js +++ b/lib/https.js @@ -34,6 +34,12 @@ const { urlToOptions, searchParamsSymbol } = require('internal/url'); function Server(opts, requestListener) { if (!(this instanceof Server)) return new Server(opts, requestListener); + if (typeof opts === 'function') { + requestListener = opts; + opts = undefined; + } + opts = util._extend({}, opts); + if (process.features.tls_npn && !opts.NPNProtocols) { opts.NPNProtocols = ['http/1.1', 'http/1.0']; } diff --git a/test/parallel/test-https-argument-of-creating.js b/test/parallel/test-https-argument-of-creating.js new file mode 100644 index 00000000000000..87d934316f887c --- /dev/null +++ b/test/parallel/test-https-argument-of-creating.js @@ -0,0 +1,44 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) { + common.skip('missing crypto'); + return; +} + +const assert = require('assert'); +const https = require('https'); +const tls = require('tls'); + +const dftProtocol = {}; + +// test for immutable `opts` +{ + const opts = { foo: 'bar', NPNProtocols: [ 'http/1.1' ] }; + const server = https.createServer(opts); + + tls.convertNPNProtocols([ 'http/1.1' ], dftProtocol); + assert.deepStrictEqual(opts, { foo: 'bar', NPNProtocols: [ 'http/1.1' ] }); + assert.strictEqual(server.NPNProtocols.compare(dftProtocol.NPNProtocols), 0); +} + + +// validate that `createServer` can work with the only argument requestListener +{ + const mustNotCall = common.mustNotCall(); + const server = https.createServer(mustNotCall); + + tls.convertNPNProtocols([ 'http/1.1', 'http/1.0' ], dftProtocol); + assert.strictEqual(server.NPNProtocols.compare(dftProtocol.NPNProtocols), 0); + assert.strictEqual(server.listeners('request').length, 1); + assert.strictEqual(server.listeners('request')[0], mustNotCall); +} + + +// validate that `createServer` can work with no arguments +{ + const server = https.createServer(); + + assert.strictEqual(server.NPNProtocols.compare(dftProtocol.NPNProtocols), 0); + assert.strictEqual(server.listeners('request').length, 0); +}