Skip to content

Commit

Permalink
lib: extract validateNumber validator
Browse files Browse the repository at this point in the history
Pulls out another common argument validator to `internal/validators`

PR-URL: #22249
Reviewed-By: Bryan English <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
maclover7 authored and targos committed Sep 3, 2018
1 parent 983b578 commit 1eac11f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 52 deletions.
28 changes: 9 additions & 19 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ const {
ERR_SOCKET_CANNOT_SEND,
ERR_SOCKET_DGRAM_NOT_RUNNING
} = errors.codes;
const { validateString } = require('internal/validators');
const {
validateString,
validateNumber
} = require('internal/validators');
const { Buffer } = require('buffer');
const util = require('util');
const { isUint8Array } = require('internal/util/types');
Expand Down Expand Up @@ -258,18 +261,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 @@ -530,9 +524,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 @@ -544,9 +536,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
6 changes: 2 additions & 4 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const binding = process.binding('buffer');
const {
ERR_BUFFER_OUT_OF_BOUNDS,
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 +38,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
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 @@ -2326,8 +2321,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 @@ -11,10 +11,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 @@ -130,10 +130,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 @@ -1431,9 +1432,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

0 comments on commit 1eac11f

Please sign in to comment.