Skip to content

Commit

Permalink
https: make opts optional & immutable when create
Browse files Browse the repository at this point in the history
`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.

Refs: #13584
  • Loading branch information
XadillaX committed Jun 10, 2017
1 parent 8208fda commit 26d4efb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/api/https.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ added: v8.0.0

See [`http.Server#keepAliveTimeout`][].

## https.createServer(options[, requestListener])
## https.createServer([options][, requestListener])
<!-- YAML
added: v0.3.4
-->
Expand Down
6 changes: 6 additions & 0 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = opts ? util._extend({}, opts) : {};

if (process.features.tls_npn && !opts.NPNProtocols) {
opts.NPNProtocols = ['http/1.1', 'http/1.0'];
}
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-https-immutable-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const common = require('../common');

const assert = require('assert');
const https = require('https');
const tls = require('tls');

const dftProtocol = {};
tls.convertNPNProtocols([ 'http/1.1', 'http/1.0' ], dftProtocol);

const opts = { foo: 'bar' };
const server1 = https.createServer(opts);

assert.deepStrictEqual(opts, { foo: 'bar' });
assert.strictEqual(server1.NPNProtocols.compare(dftProtocol.NPNProtocols), 0);

const mustNotCall = common.mustNotCall('dummy callback');
const server2 = https.createServer(mustNotCall);

assert.strictEqual(server2.NPNProtocols.compare(dftProtocol.NPNProtocols), 0);
assert.strictEqual(mustNotCall, server2._events.request);

0 comments on commit 26d4efb

Please sign in to comment.