diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index e8623bfb2d2eeb..2e92ce188850a7 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -99,7 +99,8 @@ const { }, hideStackFrames } = require('internal/errors'); -const { validateNumber, +const { validateInteger, + validateNumber, validateString, validateUint32, isUint32, @@ -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); diff --git a/test/parallel/test-http2-invalidargtypes-errors.js b/test/parallel/test-http2-invalidargtypes-errors.js index 2230ba1d1f30e4..5f126b060a9f49 100644 --- a/test/parallel/test-http2-invalidargtypes-errors.js +++ b/test/parallel/test-http2-invalidargtypes-errors.js @@ -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'); }));