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 <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
Debadree Chatterjee 2023-01-24 19:28:42 +05:30 committed by GitHub
parent e8d40154d8
commit e4d641f02a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 15 deletions

View File

@ -126,7 +126,8 @@ const {
validateNumber, validateNumber,
validateString, validateString,
validateUint32, validateUint32,
validateAbortSignal validateAbortSignal,
validateBoolean,
} = require('internal/validators'); } = require('internal/validators');
const fsPromisesInternal = require('internal/fs/promises'); const fsPromisesInternal = require('internal/fs/promises');
const { utcDate } = require('internal/http'); const { utcDate } = require('internal/http');
@ -761,26 +762,26 @@ function requestOnConnect(headers, options) {
const setAndValidatePriorityOptions = hideStackFrames((options) => { const setAndValidatePriorityOptions = hideStackFrames((options) => {
if (options.weight === undefined) { if (options.weight === undefined) {
options.weight = NGHTTP2_DEFAULT_WEIGHT; options.weight = NGHTTP2_DEFAULT_WEIGHT;
} else if (typeof options.weight !== 'number') { } else {
throw new ERR_INVALID_ARG_VALUE('options.weight', options.weight); validateNumber(options.weight, 'options.weight');
} }
if (options.parent === undefined) { if (options.parent === undefined) {
options.parent = 0; options.parent = 0;
} else if (typeof options.parent !== 'number' || options.parent < 0) { } else {
throw new ERR_INVALID_ARG_VALUE('options.parent', options.parent); validateNumber(options.parent, 'options.parent', 0);
} }
if (options.exclusive === undefined) { if (options.exclusive === undefined) {
options.exclusive = false; options.exclusive = false;
} else if (typeof options.exclusive !== 'boolean') { } else {
throw new ERR_INVALID_ARG_VALUE('options.exclusive', options.exclusive); validateBoolean(options.exclusive, 'options.exclusive');
} }
if (options.silent === undefined) { if (options.silent === undefined) {
options.silent = false; options.silent = false;
} else if (typeof options.silent !== 'boolean') { } else {
throw new ERR_INVALID_ARG_VALUE('options.silent', options.silent); validateBoolean(options.silent, 'options.silent');
} }
}); });
@ -1784,8 +1785,8 @@ class ClientHttp2Session extends Http2Session {
// stream by default if the user has not specifically indicated a // stream by default if the user has not specifically indicated a
// preference. // preference.
options.endStream = isPayloadMeaningless(headers[HTTP2_HEADER_METHOD]); options.endStream = isPayloadMeaningless(headers[HTTP2_HEADER_METHOD]);
} else if (typeof options.endStream !== 'boolean') { } else {
throw new ERR_INVALID_ARG_VALUE('options.endStream', options.endStream); validateBoolean(options.endStream, 'options.endStream');
} }
const headersList = mapToHeaders(headers); const headersList = mapToHeaders(headers);

View File

@ -5,7 +5,6 @@ if (!common.hasCrypto)
common.skip('missing crypto'); common.skip('missing crypto');
const assert = require('assert'); const assert = require('assert');
const http2 = require('http2'); const http2 = require('http2');
const { inspect } = require('util');
// Check if correct errors are emitted when wrong type of data is passed // Check if correct errors are emitted when wrong type of data is passed
// to certain options of ClientHttp2Session request method // to certain options of ClientHttp2Session request method
@ -48,9 +47,7 @@ server.listen(0, common.mustCall(() => {
[option]: types[type] [option]: types[type]
}), { }), {
name: 'TypeError', name: 'TypeError',
code: 'ERR_INVALID_ARG_VALUE', code: 'ERR_INVALID_ARG_TYPE',
message: `The property 'options.${option}' is invalid. ` +
`Received ${inspect(types[type])}`
}); });
}); });
}); });