Skip to content

Commit 74741fa

Browse files
XadillaXaddaleax
authored andcommitted
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: #13599 Fixes: #13584 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Yorkie Liu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 3bb4ec8 commit 74741fa

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

doc/api/https.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ added: v8.0.0
4646

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

49-
## https.createServer(options[, requestListener])
49+
## https.createServer([options][, requestListener])
5050
<!-- YAML
5151
added: v0.3.4
5252
-->

lib/https.js

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const { urlToOptions, searchParamsSymbol } = require('internal/url');
3434
function Server(opts, requestListener) {
3535
if (!(this instanceof Server)) return new Server(opts, requestListener);
3636

37+
if (typeof opts === 'function') {
38+
requestListener = opts;
39+
opts = undefined;
40+
}
41+
opts = util._extend({}, opts);
42+
3743
if (process.features.tls_npn && !opts.NPNProtocols) {
3844
opts.NPNProtocols = ['http/1.1', 'http/1.0'];
3945
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto) {
5+
common.skip('missing crypto');
6+
return;
7+
}
8+
9+
const assert = require('assert');
10+
const https = require('https');
11+
const tls = require('tls');
12+
13+
const dftProtocol = {};
14+
15+
// test for immutable `opts`
16+
{
17+
const opts = { foo: 'bar', NPNProtocols: [ 'http/1.1' ] };
18+
const server = https.createServer(opts);
19+
20+
tls.convertNPNProtocols([ 'http/1.1' ], dftProtocol);
21+
assert.deepStrictEqual(opts, { foo: 'bar', NPNProtocols: [ 'http/1.1' ] });
22+
assert.strictEqual(server.NPNProtocols.compare(dftProtocol.NPNProtocols), 0);
23+
}
24+
25+
26+
// validate that `createServer` can work with the only argument requestListener
27+
{
28+
const mustNotCall = common.mustNotCall();
29+
const server = https.createServer(mustNotCall);
30+
31+
tls.convertNPNProtocols([ 'http/1.1', 'http/1.0' ], dftProtocol);
32+
assert.strictEqual(server.NPNProtocols.compare(dftProtocol.NPNProtocols), 0);
33+
assert.strictEqual(server.listeners('request').length, 1);
34+
assert.strictEqual(server.listeners('request')[0], mustNotCall);
35+
}
36+
37+
38+
// validate that `createServer` can work with no arguments
39+
{
40+
const server = https.createServer();
41+
42+
assert.strictEqual(server.NPNProtocols.compare(dftProtocol.NPNProtocols), 0);
43+
assert.strictEqual(server.listeners('request').length, 0);
44+
}

0 commit comments

Comments
 (0)