Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

https: make opts optional & immutable when create #13599

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
Copy link
Contributor

@mscdex mscdex Jun 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
Contributor Author

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 arguments
const server2 = https.createServer();
assert.ok(server2);
assert.strictEqual(server1.NPNProtocols.compare(dftProtocol.NPNProtocols), 0);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@refack done.

<!-- 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;
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
Contributor Author

@XadillaX XadillaX Jun 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

@yorkie yorkie Jun 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
Contributor Author

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?

...

const realOpts = 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 :)

}
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

@mscdex mscdex Jun 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
Contributor Author

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(var i=0; i < 1E7; ++i) { Object.assign({1:1, 2:2, 3:3}, {a:1, b:2, c:3}); };console.timeEnd('assign')
assign: 9825.217ms
undefined
> console.time('_extend');for(var i=0; i < 1E7; ++i) { u._extend({1:1, 2:2, 3:3}, {a:1, b:2, c:3}); };console.timeEnd('_extend')
_extend: 6174.819ms
undefined

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

Copy link
Contributor Author

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
Contributor Author

@XadillaX XadillaX Jun 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...


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);
Copy link
Contributor

@mscdex mscdex Jun 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
Contributor Author

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

@mscdex mscdex Jun 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?).


const mustNotCall = 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.

const server2 = https.createServer(mustNotCall);
Copy link
Contributor

@mscdex mscdex Jun 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.


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);