Skip to content

Commit

Permalink
lib: refactor to use validateBuffer
Browse files Browse the repository at this point in the history
Use validateBuffer to remove duplicate implementation.

PR-URL: #46489
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Ricky Zhou <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
deokjinkim authored Feb 6, 2023
1 parent f234bd4 commit d43b532
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
17 changes: 7 additions & 10 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ const {
} = require('internal/errors');
const {
isUint32,
validateAbortSignal,
validateBoolean,
validateBuffer,
validateFunction,
validateInt32,
validateInteger,
validateNumber,
validateString,
validateUint32,
validateAbortSignal,
validateBoolean,
} = require('internal/validators');
const fsPromisesInternal = require('internal/fs/promises');
const { utcDate } = require('internal/http');
Expand Down Expand Up @@ -1377,10 +1378,8 @@ class Http2Session extends EventEmitter {
callback = payload;
payload = undefined;
}
if (payload && !isArrayBufferView(payload)) {
throw new ERR_INVALID_ARG_TYPE('payload',
['Buffer', 'TypedArray', 'DataView'],
payload);
if (payload) {
validateBuffer(payload, 'payload');
}
if (payload && payload.length !== 8) {
throw new ERR_HTTP2_PING_LENGTH();
Expand Down Expand Up @@ -1506,10 +1505,8 @@ class Http2Session extends EventEmitter {
if (this.destroyed)
throw new ERR_HTTP2_INVALID_SESSION();

if (opaqueData !== undefined && !isArrayBufferView(opaqueData)) {
throw new ERR_INVALID_ARG_TYPE('opaqueData',
['Buffer', 'TypedArray', 'DataView'],
opaqueData);
if (opaqueData !== undefined) {
validateBuffer(opaqueData, 'opaqueData');
}
validateNumber(code, 'code');
validateNumber(lastStreamID, 'lastStreamID');
Expand Down
8 changes: 2 additions & 6 deletions lib/internal/tls/secure-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
} = require('internal/util/types');

const {
validateBuffer,
validateInt32,
validateObject,
validateString,
Expand Down Expand Up @@ -292,12 +293,7 @@ function configSecureContext(context, options = kEmptyObject, name = 'options')
}

if (ticketKeys !== undefined && ticketKeys !== null) {
if (!isArrayBufferView(ticketKeys)) {
throw new ERR_INVALID_ARG_TYPE(
`${name}.ticketKeys`,
['Buffer', 'TypedArray', 'DataView'],
ticketKeys);
}
validateBuffer(ticketKeys, `${name}.ticketKeys`);
if (ticketKeys.byteLength !== 48) {
throw new ERR_INVALID_ARG_VALUE(
`${name}.ticketKeys`,
Expand Down
12 changes: 3 additions & 9 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ const {
ERR_CONTEXT_NOT_INITIALIZED,
ERR_INVALID_ARG_TYPE,
} = require('internal/errors').codes;
const {
isArrayBufferView,
} = require('internal/util/types');
const {
validateBoolean,
validateBuffer,
validateFunction,
validateInt32,
validateObject,
Expand Down Expand Up @@ -84,12 +82,8 @@ class Script extends ContextifyScript {
validateString(filename, 'options.filename');
validateInt32(lineOffset, 'options.lineOffset');
validateInt32(columnOffset, 'options.columnOffset');
if (cachedData !== undefined && !isArrayBufferView(cachedData)) {
throw new ERR_INVALID_ARG_TYPE(
'options.cachedData',
['Buffer', 'TypedArray', 'DataView'],
cachedData
);
if (cachedData !== undefined) {
validateBuffer(cachedData, 'options.cachedData');
}
validateBoolean(produceCachedData, 'options.produceCachedData');

Expand Down

0 comments on commit d43b532

Please sign in to comment.