diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 315ea280bfb81a..09f1f993c38d07 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -1761,6 +1761,8 @@ class Http2Stream extends Duplex { throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'number'); if (code < 0 || code > kMaxInt) throw new errors.RangeError('ERR_OUT_OF_RANGE', 'code'); + if (callback !== undefined && typeof callback !== 'function') + throw new errors.TypeError('ERR_INVALID_CALLBACK'); // Unenroll the timeout. unenroll(this); @@ -1778,8 +1780,6 @@ class Http2Stream extends Duplex { state.rstCode = code; if (callback !== undefined) { - if (typeof callback !== 'function') - throw new errors.TypeError('ERR_INVALID_CALLBACK'); this.once('close', callback); } diff --git a/test/parallel/test-http2-client-rststream-before-connect.js b/test/parallel/test-http2-client-rststream-before-connect.js index 7909fd97fc313b..aeb31949db074e 100644 --- a/test/parallel/test-http2-client-rststream-before-connect.js +++ b/test/parallel/test-http2-client-rststream-before-connect.js @@ -28,6 +28,18 @@ server.listen(0, common.mustCall(() => { ); assert.strictEqual(req.closed, false); + [true, 1, {}, [], null, 'test'].forEach((notFunction) => { + common.expectsError( + () => req.close(closeCode, notFunction), + { + type: TypeError, + code: 'ERR_INVALID_CALLBACK', + message: 'callback must be a function' + } + ); + assert.strictEqual(req.closed, false); + }); + req.close(closeCode, common.mustCall()); assert.strictEqual(req.closed, true);