Skip to content

Commit

Permalink
http2: refactor state code validation for the http2Stream class
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rickyes authored and himself65 committed May 30, 2020
1 parent b0b268f commit d79c330
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/internal/http2/core.js
Expand Up @@ -99,7 +99,8 @@ const {
},
hideStackFrames
} = require('internal/errors');
const { validateNumber,
const { validateInteger,
validateNumber,
validateString,
validateUint32,
isUint32,
Expand Down Expand Up @@ -2093,9 +2094,8 @@ class Http2Stream extends Duplex {
// close, it is still possible to queue up PRIORITY and RST_STREAM frames,
// but no DATA and HEADERS frames may be sent.
close(code = NGHTTP2_NO_ERROR, callback) {
validateNumber(code, 'code');
if (code < 0 || code > kMaxInt)
throw new ERR_OUT_OF_RANGE('code', `>= 0 && <= ${kMaxInt}`, code);
validateInteger(code, 'code', 0, kMaxInt);

if (callback !== undefined && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);

Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-http2-invalidargtypes-errors.js
Expand Up @@ -18,6 +18,27 @@ server.on('stream', common.mustCall((stream) => {
"Received type string ('string')"
}
);
assert.throws(
() => stream.close(1.01),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "code" is out of range. It must be an integer. ' +
'Received 1.01'
}
);
[-1, 2 ** 32].forEach((code) => {
assert.throws(
() => stream.close(code),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "code" is out of range. ' +
'It must be >= 0 && <= 4294967295. ' +
`Received ${code}`
}
);
});
stream.respond();
stream.end('ok');
}));
Expand Down

0 comments on commit d79c330

Please sign in to comment.