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: #13599Fixes: #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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const{urlToOptions, searchParamsSymbol } = require('internal/url');
3434
functionServer(opts,requestListener){
3535
if(!(thisinstanceofServer))returnnewServer(opts,requestListener);
3636

37+
if(typeofopts==='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
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto){
5+
common.skip('missing crypto');
6+
return;
7+
}
8+
9+
constassert=require('assert');
10+
consthttps=require('https');
11+
consttls=require('tls');
12+
13+
constdftProtocol={};
14+
15+
// test for immutable `opts`
16+
{
17+
constopts={foo: 'bar',NPNProtocols: ['http/1.1']};
18+
constserver=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+
constmustNotCall=common.mustNotCall();
29+
constserver=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+
constserver=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)