Skip to content

Commit d4111d0

Browse files
committed
http2: general cleanups
PR-URL: #17328 Fixes: #15303 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Sebastiaan Deckers <sebdeckers83@gmail.com>
1 parent f3686f2 commit d4111d0

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

lib/internal/http2/core.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const { _connectionListener: httpConnectionListener } = require('http');
2828
const { createPromise, promiseResolve } = process.binding('util');
2929
const debug = util.debuglog('http2');
3030

31+
const kMaxFrameSize = (2 ** 24) - 1;
32+
const kMaxInt = (2 ** 32) - 1;
3133
const kMaxStreams = (2 ** 31) - 1;
3234

3335
const {
@@ -332,9 +334,9 @@ function emitGoaway(self, code, lastStreamID, buf) {
332334
return;
333335
if (!state.shuttingDown && !state.shutdown) {
334336
self.shutdown({}, self.destroy.bind(self));
335-
} else {
336-
self.destroy();
337+
return;
337338
}
339+
self.destroy();
338340
}
339341

340342
// Called by the native layer when a goaway frame has been received
@@ -582,14 +584,15 @@ function doShutdown(options) {
582584
function submitShutdown(options) {
583585
const type = this[kType];
584586
debug(`Http2Session ${sessionName(type)}: submitting shutdown request`);
587+
const fn = doShutdown.bind(this, options);
585588
if (type === NGHTTP2_SESSION_SERVER && options.graceful === true) {
586589
// first send a shutdown notice
587590
this[kHandle].shutdownNotice();
588591
// then, on flip of the event loop, do the actual shutdown
589-
setImmediate(doShutdown.bind(this), options);
590-
} else {
591-
doShutdown.call(this, options);
592+
setImmediate(fn);
593+
return;
592594
}
595+
fn();
593596
}
594597

595598
function finishSessionDestroy(socket) {
@@ -844,19 +847,19 @@ class Http2Session extends EventEmitter {
844847
settings = Object.assign(Object.create(null), settings);
845848
assertWithinRange('headerTableSize',
846849
settings.headerTableSize,
847-
0, 2 ** 32 - 1);
850+
0, kMaxInt);
848851
assertWithinRange('initialWindowSize',
849852
settings.initialWindowSize,
850-
0, 2 ** 32 - 1);
853+
0, kMaxInt);
851854
assertWithinRange('maxFrameSize',
852855
settings.maxFrameSize,
853-
16384, 2 ** 24 - 1);
856+
16384, kMaxFrameSize);
854857
assertWithinRange('maxConcurrentStreams',
855858
settings.maxConcurrentStreams,
856859
0, kMaxStreams);
857860
assertWithinRange('maxHeaderListSize',
858861
settings.maxHeaderListSize,
859-
0, 2 ** 32 - 1);
862+
0, kMaxInt);
860863
if (settings.enablePush !== undefined &&
861864
typeof settings.enablePush !== 'boolean') {
862865
const err = new errors.TypeError('ERR_HTTP2_INVALID_SETTING_VALUE',
@@ -871,11 +874,12 @@ class Http2Session extends EventEmitter {
871874
debug(`Http2Session ${sessionName(this[kType])}: sending settings`);
872875

873876
state.pendingAck++;
877+
const fn = submitSettings.bind(this, settings);
874878
if (state.connecting) {
875-
this.once('connect', submitSettings.bind(this, settings));
879+
this.once('connect', fn);
876880
return;
877881
}
878-
submitSettings.call(this, settings);
882+
fn();
879883
}
880884

881885
// Destroy the Http2Session
@@ -961,13 +965,14 @@ class Http2Session extends EventEmitter {
961965
this.on('shutdown', callback);
962966
}
963967

968+
const fn = submitShutdown.bind(this, options);
964969
if (state.connecting) {
965-
this.once('connect', submitShutdown.bind(this, options));
970+
this.once('connect', fn);
966971
return;
967972
}
968973

969974
debug(`Http2Session ${sessionName(type)}: sending shutdown`);
970-
submitShutdown.call(this, options);
975+
fn();
971976
}
972977

973978
_onTimeout() {
@@ -1368,7 +1373,7 @@ class Http2Stream extends Duplex {
13681373
rstStream(code = NGHTTP2_NO_ERROR) {
13691374
if (typeof code !== 'number')
13701375
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'number');
1371-
if (code < 0 || code > 2 ** 32 - 1)
1376+
if (code < 0 || code > kMaxInt)
13721377
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'code');
13731378

13741379
const fn = submitRstStream.bind(this, code);
@@ -2362,19 +2367,19 @@ function getPackedSettings(settings) {
23622367
settings = settings || Object.create(null);
23632368
assertWithinRange('headerTableSize',
23642369
settings.headerTableSize,
2365-
0, 2 ** 32 - 1);
2370+
0, kMaxInt);
23662371
assertWithinRange('initialWindowSize',
23672372
settings.initialWindowSize,
2368-
0, 2 ** 32 - 1);
2373+
0, kMaxInt);
23692374
assertWithinRange('maxFrameSize',
23702375
settings.maxFrameSize,
2371-
16384, 2 ** 24 - 1);
2376+
16384, kMaxFrameSize);
23722377
assertWithinRange('maxConcurrentStreams',
23732378
settings.maxConcurrentStreams,
23742379
0, kMaxStreams);
23752380
assertWithinRange('maxHeaderListSize',
23762381
settings.maxHeaderListSize,
2377-
0, 2 ** 32 - 1);
2382+
0, kMaxInt);
23782383
if (settings.enablePush !== undefined &&
23792384
typeof settings.enablePush !== 'boolean') {
23802385
const err = new errors.TypeError('ERR_HTTP2_INVALID_SETTING_VALUE',
@@ -2425,22 +2430,22 @@ function getUnpackedSettings(buf, options = {}) {
24252430
if (options != null && options.validate) {
24262431
assertWithinRange('headerTableSize',
24272432
settings.headerTableSize,
2428-
0, 2 ** 32 - 1);
2433+
0, kMaxInt);
24292434
assertWithinRange('enablePush',
24302435
settings.enablePush,
24312436
0, 1);
24322437
assertWithinRange('initialWindowSize',
24332438
settings.initialWindowSize,
2434-
0, 2 ** 32 - 1);
2439+
0, kMaxInt);
24352440
assertWithinRange('maxFrameSize',
24362441
settings.maxFrameSize,
2437-
16384, 2 ** 24 - 1);
2442+
16384, kMaxFrameSize);
24382443
assertWithinRange('maxConcurrentStreams',
24392444
settings.maxConcurrentStreams,
24402445
0, kMaxStreams);
24412446
assertWithinRange('maxHeaderListSize',
24422447
settings.maxHeaderListSize,
2443-
0, 2 ** 32 - 1);
2448+
0, kMaxInt);
24442449
}
24452450

24462451
if (settings.enablePush !== undefined) {

0 commit comments

Comments
 (0)