Skip to content

Conversation

@XadillaX
Copy link
Contributor

@XadillaXXadillaX commented Jun 10, 2017

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

Checklist
  • make -j4 test (UNIX)
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

https

`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: nodejs#13584
@nodejs-github-botnodejs-github-bot added the https Issues or PRs related to the https subsystem. label Jun 10, 2017
See [`http.Server#keepAliveTimeout`][].

## https.createServer(options[, requestListener])
## https.createServer([options][, requestListener])
Copy link
Contributor

@mscdexmscdexJun 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have to change this in the documentation, since tls.Server() will still require a configuration (a certificate and key at minimum). The benefit of guarding early before tls.Server() is called is to avoid strange JavaScript runtime errors. Huh... I take that back, it doesn't seem to enforce a minimum configuration...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But a test in either way should be added.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack the test is in test/parallel/test-https-immutable-options.js, which tested in server2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a test like

// validate that `createServer` can work with no argumentsconstserver2=https.createServer();assert.ok(server2);assert.strictEqual(server1.NPNProtocols.compare(dftProtocol.NPNProtocols),0);

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack done.

Copy link
Contributor

@refackrefack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 style nit
1 request for test (assert signature)

lib/https.js Outdated
requestListener=opts;
opts=undefined;
}
opts=opts ? util._extend({},opts) : {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use Object.assign (and give opts a default in L34)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack Did you benchmark that util._extend is faster than Object.assign by now? Last I heard it was not, which is why we still use util._extend for most internals.

Copy link
Contributor

@mscdexmscdexJun 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax I was about to say that but I'm not sure it matters in the case of starting a server. *shrug*

util._extend() does have significantly more usage in core though according to a quick grep.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm, so I'm waiting for a result.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this is a "cold spot" so I opt for standard 🤷‍♂️
Any way this could be simply

opts = util._extend({}, opts); 

@XadillaX I think you're free to decide

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>console.time('assign');for(vari=0;i<1E7;++i){Object.assign({1:1,2:2,3:3},{a:1,b:2,c:3});};console.timeEnd('assign') assign: 9825.217msundefined>console.time('_extend');for(vari=0;i<1E7;++i){u._extend({1:1,2:2,3:3},{a:1,b:2,c:3});};console.timeEnd('_extend') _extend: 6174.819msundefined

so util._extend is ~ 60% of Object.assign.
IMHO if it's not better than 50%, Object.assign should be prefered

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If util._extend uses more than Object.assign, I think I can change it back to util._extend.

Copy link
ContributorAuthor

@XadillaXXadillaXJun 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack, And if necessary, I think we can open a new PR about changing all util._extend to Object.assign in this file next time. Because it not only contains one util._extend here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack, And if necessary, I think we can open a new PR about changing all util._extend to Object.assign in this file next time. Because it not only contains one util._extend here.

We can do this when V8 optimizes Object.assign to be <= util._extend...

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

## https.createServer(options[, requestListener])
## https.createServer([options][, requestListener])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But a test in either way should be added.

@XadillaXXadillaXforce-pushed the feature/https-optional-immutable-opt branch from 35b1e77 to 26d4efbCompareJune 10, 2017 18:39
assert.deepStrictEqual(opts,{foo: 'bar'});
assert.strictEqual(server1.NPNProtocols.compare(dftProtocol.NPNProtocols),0);

constmustNotCall=common.mustNotCall('dummy callback');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be best to just omit the argument here, since if the function is called for some odd reason the default message should be slightly more descriptive and good enough.

constserver1=https.createServer(opts);

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

@mscdexmscdexJun 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should suffice to just compare the NPNProtocols references. That way we know for sure that our options weren't overridden:

assert.strictEqual(server1.NPNProtocols,dftProtocol.NPNProtocols);

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mscdex I think we can't, because NPNProtocols hasn't been cached yet. 2 calls to tls.convertNPNProtocols won't return the same reference.

Copy link
Contributor

@mscdexmscdexJun 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok I see it now. Perhaps we should at least choose a different set of protocols then, so as to differentiate from the default NPN protocols (perhaps just use one protocol?).

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

## https.createServer(options[, requestListener])
## https.createServer([options][, requestListener])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a test like

// validate that `createServer` can work with no argumentsconstserver2=https.createServer();assert.ok(server2);assert.strictEqual(server1.NPNProtocols.compare(dftProtocol.NPNProtocols),0);

lib/https.js Outdated
requestListener=opts;
opts=undefined;
}
opts=opts ? Object.assign({},opts) : {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can change this to

opts=Object.assign({},opts);

constserver2=https.createServer(mustNotCall);

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace this with

assert.strictEqual(server2.listeners('request'),1);assert.strictEqual(server2.listeners('request')[0],mustNotCall);

assert.strictEqual(server1.NPNProtocols.compare(dftProtocol.NPNProtocols),0);

constmustNotCall=common.mustNotCall();
constserver2=https.createServer(mustNotCall);
Copy link
Contributor

@mscdexmscdexJun 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment below doesn't match the behavior of this line. If we're testing no arguments, it should just be https.createServer().

Otherwise we could add a separate test below for no arguments.


// validate that `createServer` can work with no arguments
tls.convertNPNProtocols(['http/1.1','http/1.0'],dftProtocol);
assert.ok(server2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It doesn't harm but I think this is redundant. An error would be thrown below if server2 is falsy.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack, Shall I?

Copy link
Contributor

@refackrefackJun 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a style we now use more often (helps frame every test case), but IMHO it's up to you... that was for the other comment
I'm 👍 for keeping it, but maybe put if just below the const server2 =


// test for immutable `opts`
constopts={foo: 'bar',NPNProtocols: ['http/1.1']};
constserver1=https.createServer(opts);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nit: instead of using server1, server2, and server3 we can use block scope.

{// test for immutable `opts`}{// validate that `createServer` can work with the only argument requestListener}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a style we now use more often (helps frame every test case), but IMHO it's up to you...

@mscdex
Copy link
Contributor

Copy link
Contributor

@refackrefack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯
LGTM as is

@mscdex
Copy link
Contributor

LGTM

Copy link
Contributor

@cjihrigcjihrig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a comment

'use strict';

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add this:

if(!common.hasCrypto){common.skip('missing crypto');return;}

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. 👌

Copy link
Contributor

@yorkieyorkie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nit


if(typeofopts==='function'){
requestListener=opts;
opts=undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: The optional value is an object, but here reset to undefined, this does not appear consistent :)

Copy link
ContributorAuthor

@XadillaXXadillaXJun 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yorkie, Because I think util._extend({}, undefined) performance is better than util._extend({},{})

Copy link
Contributor

@yorkieyorkieJun 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I think you would make the opts default be unset as well for performance. BTW perf is not that crucial here than consistence and readability, this constructor needn't be called too much times in any of applications :)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is opts = util._extend({}, opts); shows it's not a consistent.

Do you mean that?

... constrealOpts=util._extend({},opts);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mean problem is the default value, removing that like https://github.com/nodejs/node/pull/13599/files#r121620077 works for me :)

lib/https.js Outdated
const{ urlToOptions, searchParamsSymbol }=require('internal/url');

functionServer(opts,requestListener){
functionServer(opts={},requestListener){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that using default values is potentially a breaking change given that it changes the value of Server.length

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall I delete the default value?

@refack
Copy link
Contributor

refack commented Jun 12, 2017

@TrottTrott added the semver-minor PRs that contain new features and should be released in the next minor version. label Jun 13, 2017
Copy link
Contributor

@refackrefack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
if test still pass removing default was a good call

@refackrefack self-assigned this Jun 14, 2017
@refack
Copy link
Contributor

refack pushed a commit to refack/node that referenced this pull request Jun 14, 2017
`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: nodejs#13599Fixes: nodejs#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]>
@refack
Copy link
Contributor

Landed in c1c2267

@refackrefack closed this Jun 14, 2017
addaleax pushed a commit that referenced this pull request Jun 17, 2017
`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]>
@addaleaxaddaleax mentioned this pull request Jun 17, 2017
addaleax pushed a commit that referenced this pull request Jun 21, 2017
`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]>
addaleax pushed a commit that referenced this pull request Jun 24, 2017
`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]>
rvagg pushed a commit that referenced this pull request Jun 29, 2017
`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]>
addaleax pushed a commit that referenced this pull request Jul 11, 2017
`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]>
@gibfahn
Copy link
Member

Release team decided not to land on v6.x, if you disagree let us know.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

httpsIssues or PRs related to the https subsystem.semver-minorPRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

11 participants

@XadillaX@mscdex@refack@gibfahn@jasnell@addaleax@lpinca@yorkie@cjihrig@Trott@nodejs-github-bot