Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: extract validateNumber validator #22249

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ const {
ERR_SOCKET_DGRAM_NOT_RUNNING,
ERR_INVALID_FD_TYPE
} = errors.codes;
const { isInt32, validateString } = require('internal/validators');
const {
isInt32,
validateString,
validateNumber
} = require('internal/validators');
const { Buffer } = require('buffer');
const util = require('util');
const { isUint8Array } = require('internal/util/types');
Expand Down Expand Up @@ -310,18 +314,9 @@ Socket.prototype.sendto = function(buffer,
port,
address,
callback) {
if (typeof offset !== 'number') {
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
}

if (typeof length !== 'number') {
throw new ERR_INVALID_ARG_TYPE('length', 'number', length);
}

if (typeof port !== 'number') {
throw new ERR_INVALID_ARG_TYPE('port', 'number', port);
}

validateNumber(offset, 'offset');
validateNumber(length, 'length');
validateNumber(port, 'port');
validateString(address, 'address');

this.send(buffer, offset, length, port, address, callback);
Expand Down Expand Up @@ -582,9 +577,7 @@ Socket.prototype.setBroadcast = function(arg) {


Socket.prototype.setTTL = function(ttl) {
if (typeof ttl !== 'number') {
throw new ERR_INVALID_ARG_TYPE('ttl', 'number', ttl);
}
validateNumber(ttl, 'ttl');

var err = this[kStateSymbol].handle.setTTL(ttl);
if (err) {
Expand All @@ -596,9 +589,7 @@ Socket.prototype.setTTL = function(ttl) {


Socket.prototype.setMulticastTTL = function(ttl) {
if (typeof ttl !== 'number') {
throw new ERR_INVALID_ARG_TYPE('ttl', 'number', ttl);
}
validateNumber(ttl, 'ttl');

var err = this[kStateSymbol].handle.setMulticastTTL(ttl);
if (err) {
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');
const { setupBufferJS } = binding;

// Remove from the binding so that function is only available as exported here.
Expand Down Expand Up @@ -38,9 +39,7 @@ function checkInt(value, min, max, buf, offset, byteLength) {
}

function checkNumberType(value, type) {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(type || 'offset', 'number', value);
}
validateNumber(value, type || 'offset');
}

function boundsError(value, length, type) {
Expand Down
11 changes: 3 additions & 8 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ const {
ERR_INVALID_CALLBACK,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');
const { isArrayBufferView } = require('internal/util/types');

const kMaxUint32 = 2 ** 32 - 1;
const kMaxPossibleLength = Math.min(kMaxLength, kMaxUint32);

function assertOffset(offset, elementSize, length) {
if (typeof offset !== 'number') {
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
}

validateNumber(offset, 'offset');
offset *= elementSize;

const maxLength = Math.min(length, kMaxPossibleLength);
Expand All @@ -29,10 +27,7 @@ function assertOffset(offset, elementSize, length) {
}

function assertSize(size, elementSize, offset, length) {
if (typeof size !== 'number') {
throw new ERR_INVALID_ARG_TYPE('size', 'number', size);
}

validateNumber(size, 'size');
size *= elementSize;

if (Number.isNaN(size) || size > kMaxPossibleLength || size < 0) {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');
const fs = require('fs');
const { Buffer } = require('buffer');
const {
Expand Down Expand Up @@ -65,8 +66,7 @@ function ReadStream(path, options) {

if (this.start !== undefined) {
if (!Number.isSafeInteger(this.start)) {
if (typeof this.start !== 'number')
throw new ERR_INVALID_ARG_TYPE('start', 'number', this.start);
validateNumber(this.start, 'start');
if (!Number.isInteger(this.start))
throw new ERR_OUT_OF_RANGE('start', 'an integer', this.start);
throw new ERR_OUT_OF_RANGE(
Expand Down
18 changes: 6 additions & 12 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const {
ERR_SOCKET_CLOSED
}
} = require('internal/errors');
const { validateNumber } = require('internal/validators');
const { utcDate } = require('internal/http');
const { onServerStream,
Http2ServerRequest,
Expand Down Expand Up @@ -1001,8 +1002,7 @@ class Http2Session extends EventEmitter {
if (this.destroyed)
throw new ERR_HTTP2_INVALID_SESSION();

if (typeof id !== 'number')
throw new ERR_INVALID_ARG_TYPE('id', 'number', id);
validateNumber(id, 'id');
if (id <= 0 || id > kMaxStreams)
throw new ERR_OUT_OF_RANGE('id', `> 0 and <= ${kMaxStreams}`, id);
this[kHandle].setNextStreamID(id);
Expand Down Expand Up @@ -1144,12 +1144,8 @@ class Http2Session extends EventEmitter {
['Buffer', 'TypedArray', 'DataView'],
opaqueData);
}
if (typeof code !== 'number') {
throw new ERR_INVALID_ARG_TYPE('code', 'number', code);
}
if (typeof lastStreamID !== 'number') {
throw new ERR_INVALID_ARG_TYPE('lastStreamID', 'number', lastStreamID);
}
validateNumber(code, 'code');
validateNumber(lastStreamID, 'lastStreamID');

const goawayFn = submitGoaway.bind(this, code, lastStreamID, opaqueData);
if (this.connecting) {
Expand Down Expand Up @@ -1831,8 +1827,7 @@ class Http2Stream extends Duplex {
// close, it is still possible to queue up PRIORITY and RST_STREAM frames,
// but no DATA and HEADERS frames may be sent.
close(code = NGHTTP2_NO_ERROR, callback) {
if (typeof code !== 'number')
throw new ERR_INVALID_ARG_TYPE('code', 'number', code);
validateNumber(code, 'code');
if (code < 0 || code > kMaxInt)
throw new ERR_OUT_OF_RANGE('code', `>= 0 && <= ${kMaxInt}`, code);
if (callback !== undefined && typeof callback !== 'function')
Expand Down Expand Up @@ -2324,8 +2319,7 @@ class ServerHttp2Stream extends Http2Stream {
this[kState].flags |= STREAM_FLAGS_HAS_TRAILERS;
}

if (typeof fd !== 'number')
throw new ERR_INVALID_ARG_TYPE('fd', 'number', fd);
validateNumber(fd, 'fd');

debug(`Http2Stream ${this[kID]} [Http2Session ` +
`${sessionName(session[kType])}]: initiating response from fd`);
Expand Down
7 changes: 2 additions & 5 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const async_id_symbol = Symbol('asyncId');
const trigger_async_id_symbol = Symbol('triggerId');

const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');

// Timeout values > TIMEOUT_MAX are set to 1.
const TIMEOUT_MAX = 2 ** 31 - 1;
Expand Down Expand Up @@ -126,10 +126,7 @@ function setUnrefTimeout(callback, after, arg1, arg2, arg3) {

// Type checking used by timers.enroll() and Socket#setTimeout()
function validateTimerDuration(msecs) {
if (typeof msecs !== 'number') {
throw new ERR_INVALID_ARG_TYPE('msecs', 'number', msecs);
}

validateNumber(msecs, 'msecs');
if (msecs < 0 || !isFinite(msecs)) {
throw new ERR_OUT_OF_RANGE('msecs', 'a non-negative finite number', msecs);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,18 @@ function validateString(value, name) {
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
}

function validateNumber(value, name) {
if (typeof value !== 'number')
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}

module.exports = {
isInt32,
isUint32,
validateMode,
validateInteger,
validateInt32,
validateUint32,
validateString
validateString,
validateNumber
};
5 changes: 2 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE
} = errors.codes;
const { validateNumber } = require('internal/validators');
const { TextDecoder, TextEncoder } = require('internal/encoding');
const { isBuffer } = require('buffer').Buffer;

Expand Down Expand Up @@ -1455,9 +1456,7 @@ function callbackify(original) {
}

function getSystemErrorName(err) {
if (typeof err !== 'number') {
throw new ERR_INVALID_ARG_TYPE('err', 'number', err);
}
validateNumber(err, 'err');
if (err >= 0 || !Number.isSafeInteger(err)) {
throw new ERR_OUT_OF_RANGE('err', 'a negative integer', err);
}
Expand Down