diff options
author | Jon Moss <me@jonathanmoss.me> | 2018-08-03 11:23:26 -0400 |
---|---|---|
committer | Jon Moss <me@jonathanmoss.me> | 2018-08-14 11:34:34 -0400 |
commit | 3e44b8c91ef717913364ac67a12cb7cd638630db (patch) | |
tree | 885c6798245c7522a1d0f31bc76945bdcaa2afa5 /lib/internal | |
parent | 682f9b4709f0d7e634568209ce3adfe18e345774 (diff) | |
download | android-node-v8-3e44b8c91ef717913364ac67a12cb7cd638630db.tar.gz android-node-v8-3e44b8c91ef717913364ac67a12cb7cd638630db.tar.bz2 android-node-v8-3e44b8c91ef717913364ac67a12cb7cd638630db.zip |
lib: extract validateNumber validator
Pulls out another common argument validator to `internal/validators`
PR-URL: https://github.com/nodejs/node/pull/22249
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'lib/internal')
-rw-r--r-- | lib/internal/buffer.js | 5 | ||||
-rw-r--r-- | lib/internal/crypto/random.js | 11 | ||||
-rw-r--r-- | lib/internal/fs/streams.js | 4 | ||||
-rw-r--r-- | lib/internal/http2/core.js | 18 | ||||
-rw-r--r-- | lib/internal/timers.js | 7 | ||||
-rw-r--r-- | lib/internal/validators.js | 8 |
6 files changed, 22 insertions, 31 deletions
diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index 54e13ff30b..3ea34af59e 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -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. @@ -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) { diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index 15fbc37c22..ea73c85ec5 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -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); @@ -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) { diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index 14d1306275..502b3c1430 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -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 { @@ -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( diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 288c97d105..e5c2bf99ec 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -76,6 +76,7 @@ const { ERR_SOCKET_CLOSED } } = require('internal/errors'); +const { validateNumber } = require('internal/validators'); const { utcDate } = require('internal/http'); const { onServerStream, Http2ServerRequest, @@ -1002,8 +1003,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); @@ -1145,12 +1145,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) { @@ -1832,8 +1828,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') @@ -2327,8 +2322,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`); diff --git a/lib/internal/timers.js b/lib/internal/timers.js index c7d9d1750e..093284a80d 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -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; @@ -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); } diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 85e69e6125..0ecf286266 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -125,6 +125,11 @@ 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, @@ -132,5 +137,6 @@ module.exports = { validateInteger, validateInt32, validateUint32, - validateString + validateString, + validateNumber }; |