diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 2e91344e87b4e0..b5b1dc732ebde3 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -1764,6 +1764,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); @@ -1781,8 +1783,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);