| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 983b578 commit 1eac11f
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,7 +37,10 @@ const { | |||
| 37 | 37 | ERR_SOCKET_CANNOT_SEND, | |
| 38 | 38 | ERR_SOCKET_DGRAM_NOT_RUNNING | |
| 39 | 39 | } = errors.codes; | |
| 40 | - const { validateString } = require('internal/validators'); | ||
| 40 | + const { | ||
| 41 | + validateString, | ||
| 42 | + validateNumber | ||
| 43 | + } = require('internal/validators'); | ||
| 41 | 44 | const { Buffer } = require('buffer'); | |
| 42 | 45 | const util = require('util'); | |
| 43 | 46 | const { isUint8Array } = require('internal/util/types'); | |
@@ -258,18 +261,9 @@ Socket.prototype.sendto = function(buffer, | |||
| 258 | 261 | port, | |
| 259 | 262 | address, | |
| 260 | 263 | callback) { | |
| 261 | - if (typeof offset !== 'number') { | ||
| 262 | - throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset); | ||
| 263 | - } | ||
| 264 | - | ||
| 265 | - if (typeof length !== 'number') { | ||
| 266 | - throw new ERR_INVALID_ARG_TYPE('length', 'number', length); | ||
| 267 | - } | ||
| 268 | - | ||
| 269 | - if (typeof port !== 'number') { | ||
| 270 | - throw new ERR_INVALID_ARG_TYPE('port', 'number', port); | ||
| 271 | - } | ||
| 272 | - | ||
| 264 | + validateNumber(offset, 'offset'); | ||
| 265 | + validateNumber(length, 'length'); | ||
| 266 | + validateNumber(port, 'port'); | ||
| 273 | 267 | validateString(address, 'address'); | |
| 274 | 268 | ||
| 275 | 269 | this.send(buffer, offset, length, port, address, callback); | |
@@ -530,9 +524,7 @@ Socket.prototype.setBroadcast = function(arg) { | |||
| 530 | 524 | ||
| 531 | 525 | ||
| 532 | 526 | Socket.prototype.setTTL = function(ttl) { | |
| 533 | - if (typeof ttl !== 'number') { | ||
| 534 | - throw new ERR_INVALID_ARG_TYPE('ttl', 'number', ttl); | ||
| 535 | - } | ||
| 527 | + validateNumber(ttl, 'ttl'); | ||
| 536 | 528 | ||
| 537 | 529 | var err = this[kStateSymbol].handle.setTTL(ttl); | |
| 538 | 530 | if (err) { | |
@@ -544,9 +536,7 @@ Socket.prototype.setTTL = function(ttl) { | |||
| 544 | 536 | ||
| 545 | 537 | ||
| 546 | 538 | Socket.prototype.setMulticastTTL = function(ttl) { | |
| 547 | - if (typeof ttl !== 'number') { | ||
| 548 | - throw new ERR_INVALID_ARG_TYPE('ttl', 'number', ttl); | ||
| 549 | - } | ||
| 539 | + validateNumber(ttl, 'ttl'); | ||
| 550 | 540 | ||
| 551 | 541 | var err = this[kStateSymbol].handle.setMulticastTTL(ttl); | |
| 552 | 542 | if (err) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,9 +3,9 @@ | |||
| 3 | 3 | const binding = process.binding('buffer'); | |
| 4 | 4 | const { | |
| 5 | 5 | ERR_BUFFER_OUT_OF_BOUNDS, | |
| 6 | - ERR_INVALID_ARG_TYPE, | ||
| 7 | 6 | ERR_OUT_OF_RANGE | |
| 8 | 7 | } = require('internal/errors').codes; | |
| 8 | + const { validateNumber } = require('internal/validators'); | ||
| 9 | 9 | const { setupBufferJS } = binding; | |
| 10 | 10 | ||
| 11 | 11 | // Remove from the binding so that function is only available as exported here. | |
@@ -38,9 +38,7 @@ function checkInt(value, min, max, buf, offset, byteLength) { | |||
| 38 | 38 | } | |
| 39 | 39 | ||
| 40 | 40 | function checkNumberType(value, type) { | |
| 41 | - if (typeof value !== 'number') { | ||
| 42 | - throw new ERR_INVALID_ARG_TYPE(type || 'offset', 'number', value); | ||
| 43 | - } | ||
| 41 | + validateNumber(value, type || 'offset'); | ||
| 44 | 42 | } | |
| 45 | 43 | ||
| 46 | 44 | function boundsError(value, length, type) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,16 +8,14 @@ const { | |||
| 8 | 8 | ERR_INVALID_CALLBACK, | |
| 9 | 9 | ERR_OUT_OF_RANGE | |
| 10 | 10 | } = require('internal/errors').codes; | |
| 11 | + const { validateNumber } = require('internal/validators'); | ||
| 11 | 12 | const { isArrayBufferView } = require('internal/util/types'); | |
| 12 | 13 | ||
| 13 | 14 | const kMaxUint32 = 2 ** 32 - 1; | |
| 14 | 15 | const kMaxPossibleLength = Math.min(kMaxLength, kMaxUint32); | |
| 15 | 16 | ||
| 16 | 17 | function assertOffset(offset, elementSize, length) { | |
| 17 | - if (typeof offset !== 'number') { | ||
| 18 | - throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset); | ||
| 19 | - } | ||
| 20 | - | ||
| 18 | + validateNumber(offset, 'offset'); | ||
| 21 | 19 | offset *= elementSize; | |
| 22 | 20 | ||
| 23 | 21 | const maxLength = Math.min(length, kMaxPossibleLength); | |
@@ -29,10 +27,7 @@ function assertOffset(offset, elementSize, length) { | |||
| 29 | 27 | } | |
| 30 | 28 | ||
| 31 | 29 | function assertSize(size, elementSize, offset, length) { | |
| 32 | - if (typeof size !== 'number') { | ||
| 33 | - throw new ERR_INVALID_ARG_TYPE('size', 'number', size); | ||
| 34 | - } | ||
| 35 | - | ||
| 30 | + validateNumber(size, 'size'); | ||
| 36 | 31 | size *= elementSize; | |
| 37 | 32 | ||
| 38 | 33 | if (Number.isNaN(size) || size > kMaxPossibleLength || size < 0) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -75,6 +75,7 @@ const { | |||
| 75 | 75 | ERR_SOCKET_CLOSED | |
| 76 | 76 | } | |
| 77 | 77 | } = require('internal/errors'); | |
| 78 | + const { validateNumber } = require('internal/validators'); | ||
| 78 | 79 | const { utcDate } = require('internal/http'); | |
| 79 | 80 | const { onServerStream, | |
| 80 | 81 | Http2ServerRequest, | |
@@ -1001,8 +1002,7 @@ class Http2Session extends EventEmitter { | |||
| 1001 | 1002 | if (this.destroyed) | |
| 1002 | 1003 | throw new ERR_HTTP2_INVALID_SESSION(); | |
| 1003 | 1004 | ||
| 1004 | - if (typeof id !== 'number') | ||
| 1005 | - throw new ERR_INVALID_ARG_TYPE('id', 'number', id); | ||
| 1005 | + validateNumber(id, 'id'); | ||
| 1006 | 1006 | if (id <= 0 || id > kMaxStreams) | |
| 1007 | 1007 | throw new ERR_OUT_OF_RANGE('id', `> 0 and <= ${kMaxStreams}`, id); | |
| 1008 | 1008 | this[kHandle].setNextStreamID(id); | |
@@ -1144,12 +1144,8 @@ class Http2Session extends EventEmitter { | |||
| 1144 | 1144 | ['Buffer', 'TypedArray', 'DataView'], | |
| 1145 | 1145 | opaqueData); | |
| 1146 | 1146 | } | |
| 1147 | - if (typeof code !== 'number') { | ||
| 1148 | - throw new ERR_INVALID_ARG_TYPE('code', 'number', code); | ||
| 1149 | - } | ||
| 1150 | - if (typeof lastStreamID !== 'number') { | ||
| 1151 | - throw new ERR_INVALID_ARG_TYPE('lastStreamID', 'number', lastStreamID); | ||
| 1152 | - } | ||
| 1147 | + validateNumber(code, 'code'); | ||
| 1148 | + validateNumber(lastStreamID, 'lastStreamID'); | ||
| 1153 | 1149 | ||
| 1154 | 1150 | const goawayFn = submitGoaway.bind(this, code, lastStreamID, opaqueData); | |
| 1155 | 1151 | if (this.connecting) { | |
@@ -1831,8 +1827,7 @@ class Http2Stream extends Duplex { | |||
| 1831 | 1827 | // close, it is still possible to queue up PRIORITY and RST_STREAM frames, | |
| 1832 | 1828 | // but no DATA and HEADERS frames may be sent. | |
| 1833 | 1829 | close(code = NGHTTP2_NO_ERROR, callback) { | |
| 1834 | - if (typeof code !== 'number') | ||
| 1835 | - throw new ERR_INVALID_ARG_TYPE('code', 'number', code); | ||
| 1830 | + validateNumber(code, 'code'); | ||
| 1836 | 1831 | if (code < 0 || code > kMaxInt) | |
| 1837 | 1832 | throw new ERR_OUT_OF_RANGE('code', `>= 0 && <= ${kMaxInt}`, code); | |
| 1838 | 1833 | if (callback !== undefined && typeof callback !== 'function') | |
@@ -2326,8 +2321,7 @@ class ServerHttp2Stream extends Http2Stream { | |||
| 2326 | 2321 | this[kState].flags |= STREAM_FLAGS_HAS_TRAILERS; | |
| 2327 | 2322 | } | |
| 2328 | 2323 | ||
| 2329 | - if (typeof fd !== 'number') | ||
| 2330 | - throw new ERR_INVALID_ARG_TYPE('fd', 'number', fd); | ||
| 2324 | + validateNumber(fd, 'fd'); | ||
| 2331 | 2325 | ||
| 2332 | 2326 | debug(`Http2Stream ${this[kID]} [Http2Session ` + | |
| 2333 | 2327 | `${sessionName(session[kType])}]: initiating response from fd`); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,10 +11,10 @@ const async_id_symbol = Symbol('asyncId'); | |||
| 11 | 11 | const trigger_async_id_symbol = Symbol('triggerId'); | |
| 12 | 12 | ||
| 13 | 13 | const { | |
| 14 | - ERR_INVALID_ARG_TYPE, | ||
| 15 | 14 | ERR_INVALID_CALLBACK, | |
| 16 | 15 | ERR_OUT_OF_RANGE | |
| 17 | 16 | } = require('internal/errors').codes; | |
| 17 | + const { validateNumber } = require('internal/validators'); | ||
| 18 | 18 | ||
| 19 | 19 | // Timeout values > TIMEOUT_MAX are set to 1. | |
| 20 | 20 | const TIMEOUT_MAX = 2 ** 31 - 1; | |
@@ -130,10 +130,7 @@ function setUnrefTimeout(callback, after, arg1, arg2, arg3) { | |||
| 130 | 130 | ||
| 131 | 131 | // Type checking used by timers.enroll() and Socket#setTimeout() | |
| 132 | 132 | function validateTimerDuration(msecs) { | |
| 133 | - if (typeof msecs !== 'number') { | ||
| 134 | - throw new ERR_INVALID_ARG_TYPE('msecs', 'number', msecs); | ||
| 135 | - } | ||
| 136 | - | ||
| 133 | + validateNumber(msecs, 'msecs'); | ||
| 137 | 134 | if (msecs < 0 || !isFinite(msecs)) { | |
| 138 | 135 | throw new ERR_OUT_OF_RANGE('msecs', 'a non-negative finite number', msecs); | |
| 139 | 136 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -125,12 +125,18 @@ function validateString(value, name) { | |||
| 125 | 125 | throw new ERR_INVALID_ARG_TYPE(name, 'string', value); | |
| 126 | 126 | } | |
| 127 | 127 | ||
| 128 | + function validateNumber(value, name) { | ||
| 129 | + if (typeof value !== 'number') | ||
| 130 | + throw new ERR_INVALID_ARG_TYPE(name, 'number', value); | ||
| 131 | + } | ||
| 132 | + | ||
| 128 | 133 | module.exports = { | |
| 129 | 134 | isInt32, | |
| 130 | 135 | isUint32, | |
| 131 | 136 | validateMode, | |
| 132 | 137 | validateInteger, | |
| 133 | 138 | validateInt32, | |
| 134 | 139 | validateUint32, | |
| 135 | - validateString | ||
| 140 | + validateString, | ||
| 141 | + validateNumber | ||
| 136 | 142 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,7 @@ const { | |||
| 27 | 27 | ERR_INVALID_ARG_TYPE, | |
| 28 | 28 | ERR_OUT_OF_RANGE | |
| 29 | 29 | } = errors.codes; | |
| 30 | + const { validateNumber } = require('internal/validators'); | ||
| 30 | 31 | const { TextDecoder, TextEncoder } = require('internal/encoding'); | |
| 31 | 32 | const { isBuffer } = require('buffer').Buffer; | |
| 32 | 33 | ||
@@ -1431,9 +1432,7 @@ function callbackify(original) { | |||
| 1431 | 1432 | } | |
| 1432 | 1433 | ||
| 1433 | 1434 | function getSystemErrorName(err) { | |
| 1434 | - if (typeof err !== 'number') { | ||
| 1435 | - throw new ERR_INVALID_ARG_TYPE('err', 'number', err); | ||
| 1436 | - } | ||
| 1435 | + validateNumber(err, 'err'); | ||
| 1437 | 1436 | if (err >= 0 || !Number.isSafeInteger(err)) { | |
| 1438 | 1437 | throw new ERR_OUT_OF_RANGE('err', 'a negative integer', err); | |
| 1439 | 1438 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments