Skip to content

Commit

Permalink
lib: refactor to use validators in http2
Browse files Browse the repository at this point in the history
  • Loading branch information
debadree25 committed Jan 11, 2023
1 parent 384e1b5 commit 76d9760
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
28 changes: 15 additions & 13 deletions lib/internal/http2/core.js
Expand Up @@ -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');
Expand Down Expand Up @@ -761,27 +762,27 @@ 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);
}
}

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);
}
}

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);
}

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);
}

validateBoolean(options.silent, 'options.silent');
});

// When an error occurs internally at the binding level, immediately
Expand Down Expand Up @@ -1784,10 +1785,11 @@ 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);
}

validateBoolean(options.endStream, 'options.endStream');


const headersList = mapToHeaders(headers);

const stream = new ClientHttp2Stream(this, undefined, undefined, {});
Expand Down
25 changes: 22 additions & 3 deletions test/parallel/test-http2-client-request-options-errors.js
Expand Up @@ -28,6 +28,25 @@ const types = {
symbol: Symbol('test')
};

function determineSpecificType(value) {
if (value == null) {
return '' + value;
}
if (typeof value === 'function' && value.name) {
return `function ${value.name}`;
}
if (typeof value === 'object') {
if (value.constructor?.name) {
return `an instance of ${value.constructor.name}`;
}
return `${inspect(value, { depth: -1 })}`;
}
let inspected = inspect(value, { colors: false });
if (inspected.length > 28) { inspected = `${StringPrototypeSlice(inspected, 0, 25)}...`; }

return `type ${typeof value} (${inspected})`;
}

const server = http2.createServer(common.mustNotCall());

server.listen(0, common.mustCall(() => {
Expand All @@ -48,9 +67,9 @@ 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',
message: `The "options.${option}" property must be of type ${optionsToTest[option]}. ` +
`Received ${determineSpecificType(types[type])}`
});
});
});
Expand Down

0 comments on commit 76d9760

Please sign in to comment.