http2: remove side effects from validateSettings

The function did not only validate the input so far but it also made
a copy of the input object and returned that copy to the callee
function. That copy was not necessary for all call sites and it was
not obvious that the function did not only validate the input but
that it also returned a copy of it. This makes sure the function does
nothing more than validation and copying is happening in the callee
function when required.

PR-URL: https://github.com/nodejs/node/pull/26809
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater 2019-03-20 12:18:16 +01:00
parent 28e2c3771d
commit ce265908eb
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -784,7 +784,7 @@ function pingCallback(cb) {
// 6. enablePush must be a boolean // 6. enablePush must be a boolean
// All settings are optional and may be left undefined // All settings are optional and may be left undefined
const validateSettings = hideStackFrames((settings) => { const validateSettings = hideStackFrames((settings) => {
settings = { ...settings }; if (settings === undefined) return;
assertWithinRange('headerTableSize', assertWithinRange('headerTableSize',
settings.headerTableSize, settings.headerTableSize,
0, kMaxInt); 0, kMaxInt);
@ -805,7 +805,6 @@ const validateSettings = hideStackFrames((settings) => {
throw new ERR_HTTP2_INVALID_SETTING_VALUE('enablePush', throw new ERR_HTTP2_INVALID_SETTING_VALUE('enablePush',
settings.enablePush); settings.enablePush);
} }
return settings;
}); });
// Creates the internal binding.Http2Session handle for an Http2Session // Creates the internal binding.Http2Session handle for an Http2Session
@ -1144,7 +1143,7 @@ class Http2Session extends EventEmitter {
if (this.destroyed) if (this.destroyed)
throw new ERR_HTTP2_INVALID_SESSION(); throw new ERR_HTTP2_INVALID_SESSION();
assertIsObject(settings, 'settings'); assertIsObject(settings, 'settings');
settings = validateSettings(settings); validateSettings(settings);
if (callback && typeof callback !== 'function') if (callback && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(); throw new ERR_INVALID_CALLBACK();
@ -1152,7 +1151,7 @@ class Http2Session extends EventEmitter {
this[kState].pendingAck++; this[kState].pendingAck++;
const settingsFn = submitSettings.bind(this, settings, callback); const settingsFn = submitSettings.bind(this, { ...settings }, callback);
if (this.connecting) { if (this.connecting) {
this.once('connect', settingsFn); this.once('connect', settingsFn);
return; return;
@ -2817,7 +2816,8 @@ function createServer(options, handler) {
// HTTP2-Settings header frame. // HTTP2-Settings header frame.
function getPackedSettings(settings) { function getPackedSettings(settings) {
assertIsObject(settings, 'settings'); assertIsObject(settings, 'settings');
updateSettingsBuffer(validateSettings(settings)); validateSettings(settings);
updateSettingsBuffer({ ...settings });
return binding.packSettings(); return binding.packSettings();
} }