Skip to content

Commit d79c330

Browse files
rickyeshimself65
authored andcommitted
http2: refactor state code validation for the http2Stream class
PR-URL: #33535 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b0b268f commit d79c330

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/internal/http2/core.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ const {
9999
},
100100
hideStackFrames
101101
} = require('internal/errors');
102-
const { validateNumber,
102+
const { validateInteger,
103+
validateNumber,
103104
validateString,
104105
validateUint32,
105106
isUint32,
@@ -2093,9 +2094,8 @@ class Http2Stream extends Duplex {
20932094
// close, it is still possible to queue up PRIORITY and RST_STREAM frames,
20942095
// but no DATA and HEADERS frames may be sent.
20952096
close(code = NGHTTP2_NO_ERROR, callback) {
2096-
validateNumber(code, 'code');
2097-
if (code < 0 || code > kMaxInt)
2098-
throw new ERR_OUT_OF_RANGE('code', `>= 0 && <= ${kMaxInt}`, code);
2097+
validateInteger(code, 'code', 0, kMaxInt);
2098+
20992099
if (callback !== undefined && typeof callback !== 'function')
21002100
throw new ERR_INVALID_CALLBACK(callback);
21012101

test/parallel/test-http2-invalidargtypes-errors.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@ server.on('stream', common.mustCall((stream) => {
1818
"Received type string ('string')"
1919
}
2020
);
21+
assert.throws(
22+
() => stream.close(1.01),
23+
{
24+
code: 'ERR_OUT_OF_RANGE',
25+
name: 'RangeError',
26+
message: 'The value of "code" is out of range. It must be an integer. ' +
27+
'Received 1.01'
28+
}
29+
);
30+
[-1, 2 ** 32].forEach((code) => {
31+
assert.throws(
32+
() => stream.close(code),
33+
{
34+
code: 'ERR_OUT_OF_RANGE',
35+
name: 'RangeError',
36+
message: 'The value of "code" is out of range. ' +
37+
'It must be >= 0 && <= 4294967295. ' +
38+
`Received ${code}`
39+
}
40+
);
41+
});
2142
stream.respond();
2243
stream.end('ok');
2344
}));

0 commit comments

Comments
 (0)