From e4d641f02a920f9dcc8b9a87b4785c88fd5e6720 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 24 Jan 2023 19:28:42 +0530 Subject: [PATCH] lib: refactor to use validators in http2 Refs: https://github.com/nodejs/node/pull/46101 PR-URL: https://github.com/nodejs/node/pull/46174 Reviewed-By: Paolo Insogna Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca Reviewed-By: Minwoo Jung Reviewed-By: Trivikram Kamat Reviewed-By: Rafael Gonzaga --- lib/internal/http2/core.js | 23 ++++++++++--------- ...est-http2-client-request-options-errors.js | 5 +--- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 48b73ae54ce9f6..5c2959d0f26ef6 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -126,7 +126,8 @@ const { validateNumber, validateString, validateUint32, - validateAbortSignal + validateAbortSignal, + validateBoolean, } = require('internal/validators'); const fsPromisesInternal = require('internal/fs/promises'); const { utcDate } = require('internal/http'); @@ -761,26 +762,26 @@ function requestOnConnect(headers, options) { const setAndValidatePriorityOptions = hideStackFrames((options) => { if (options.weight === undefined) { options.weight = NGHTTP2_DEFAULT_WEIGHT; - } else if (typeof options.weight !== 'number') { - throw new ERR_INVALID_ARG_VALUE('options.weight', options.weight); + } else { + validateNumber(options.weight, 'options.weight'); } if (options.parent === undefined) { options.parent = 0; - } else if (typeof options.parent !== 'number' || options.parent < 0) { - throw new ERR_INVALID_ARG_VALUE('options.parent', options.parent); + } else { + validateNumber(options.parent, 'options.parent', 0); } if (options.exclusive === undefined) { options.exclusive = false; - } else if (typeof options.exclusive !== 'boolean') { - throw new ERR_INVALID_ARG_VALUE('options.exclusive', options.exclusive); + } else { + validateBoolean(options.exclusive, 'options.exclusive'); } if (options.silent === undefined) { options.silent = false; - } else if (typeof options.silent !== 'boolean') { - throw new ERR_INVALID_ARG_VALUE('options.silent', options.silent); + } else { + validateBoolean(options.silent, 'options.silent'); } }); @@ -1784,8 +1785,8 @@ class ClientHttp2Session extends Http2Session { // stream by default if the user has not specifically indicated a // preference. options.endStream = isPayloadMeaningless(headers[HTTP2_HEADER_METHOD]); - } else if (typeof options.endStream !== 'boolean') { - throw new ERR_INVALID_ARG_VALUE('options.endStream', options.endStream); + } else { + validateBoolean(options.endStream, 'options.endStream'); } const headersList = mapToHeaders(headers); diff --git a/test/parallel/test-http2-client-request-options-errors.js b/test/parallel/test-http2-client-request-options-errors.js index f2950fe4d287b4..f3c0c57965cf97 100644 --- a/test/parallel/test-http2-client-request-options-errors.js +++ b/test/parallel/test-http2-client-request-options-errors.js @@ -5,7 +5,6 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); const http2 = require('http2'); -const { inspect } = require('util'); // Check if correct errors are emitted when wrong type of data is passed // to certain options of ClientHttp2Session request method @@ -48,9 +47,7 @@ server.listen(0, common.mustCall(() => { [option]: types[type] }), { name: 'TypeError', - code: 'ERR_INVALID_ARG_VALUE', - message: `The property 'options.${option}' is invalid. ` + - `Received ${inspect(types[type])}` + code: 'ERR_INVALID_ARG_TYPE', }); }); });