GeezerVison is showing github.com — timers: extract enroll() validation into a fn · nodejs/node@593941a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/timers.js +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,6 +26,7 @@ module.exports = { | 26 | 26 | trigger_async_id_symbol, | 27 | 27 | Timeout, | 28 | 28 | setUnrefTimeout, | | 29 | + validateTimerDuration | 29 | 30 | }; | 30 | 31 |
| 31 | 32 | // Timer constructor function. | @@ -105,3 +106,26 @@ function setUnrefTimeout(callback, after, arg1, arg2, arg3) { | 105 | 106 |
| 106 | 107 | return timer; | 107 | 108 | } | | 109 | + | | 110 | +// Type checking used by timers.enroll() and Socket#setTimeout() | | 111 | +function validateTimerDuration(msecs) { | | 112 | + if (typeof msecs !== 'number') { | | 113 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs', | | 114 | + 'number', msecs); | | 115 | + } | | 116 | + | | 117 | + if (msecs < 0 || !isFinite(msecs)) { | | 118 | + throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs', | | 119 | + 'a non-negative finite number', msecs); | | 120 | + } | | 121 | + | | 122 | + // Ensure that msecs fits into signed int32 | | 123 | + if (msecs > TIMEOUT_MAX) { | | 124 | + process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` + | | 125 | + `\nTimer duration was truncated to ${TIMEOUT_MAX}.`, | | 126 | + 'TimeoutOverflowWarning'); | | 127 | + return TIMEOUT_MAX; | | 128 | + } | | 129 | + | | 130 | + return msecs; | | 131 | +} |
Collapse file Copy file name to clipboardExpand all lines: lib/net.js +6-18Lines changed: 6 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,7 +57,11 @@ var cluster = null; | 57 | 57 | const errnoException = util._errnoException; | 58 | 58 | const exceptionWithHostPort = util._exceptionWithHostPort; | 59 | 59 |
| 60 | | -const { kTimeout, TIMEOUT_MAX, setUnrefTimeout } = require('internal/timers'); | | 60 | +const { | | 61 | + kTimeout, | | 62 | + setUnrefTimeout, | | 63 | + validateTimerDuration | | 64 | +} = require('internal/timers'); | 61 | 65 |
| 62 | 66 | function noop() {} | 63 | 67 |
| @@ -394,23 +398,7 @@ Socket.prototype.read = function(n) { | 394 | 398 |
| 395 | 399 | Socket.prototype.setTimeout = function(msecs, callback) { | 396 | 400 | // Type checking identical to timers.enroll() | 397 | | - if (typeof msecs !== 'number') { | 398 | | - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs', | 399 | | - 'number', msecs); | 400 | | - } | 401 | | - | 402 | | - if (msecs < 0 || !isFinite(msecs)) { | 403 | | - throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs', | 404 | | - 'a non-negative finite number', msecs); | 405 | | - } | 406 | | - | 407 | | - // Ensure that msecs fits into signed int32 | 408 | | - if (msecs > TIMEOUT_MAX) { | 409 | | - process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` + | 410 | | - `\nTimer duration was truncated to ${TIMEOUT_MAX}.`, | 411 | | - 'TimeoutOverflowWarning'); | 412 | | - msecs = TIMEOUT_MAX; | 413 | | - } | | 401 | + msecs = validateTimerDuration(msecs); | 414 | 402 |
| 415 | 403 | // Attempt to clear an existing timer lear in both cases - | 416 | 404 | // even if it will be rescheduled we don't want to leak an existing timer. | |
Collapse file Copy file name to clipboardExpand all lines: lib/timers.js +1-21Lines changed: 1 addition & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,9 +55,6 @@ delete process._scheduledImmediateCount; | 55 | 55 | const activateImmediateCheck = process._activateImmediateCheck; | 56 | 56 | delete process._activateImmediateCheck; | 57 | 57 |
| 58 | | -// Timeout values > TIMEOUT_MAX are set to 1. | 59 | | -const TIMEOUT_MAX = timerInternals.TIMEOUT_MAX; | 60 | | - | 61 | 58 | // The Timeout class | 62 | 59 | const Timeout = timerInternals.Timeout; | 63 | 60 |
| @@ -392,29 +389,12 @@ const unenroll = exports.unenroll = function(item) { | 392 | 389 | // This function does not start the timer, see `active()`. | 393 | 390 | // Using existing objects as timers slightly reduces object overhead. | 394 | 391 | exports.enroll = function(item, msecs) { | 395 | | - if (typeof msecs !== 'number') { | 396 | | - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs', | 397 | | - 'number', msecs); | 398 | | - } | 399 | | - | 400 | | - if (msecs < 0 || !isFinite(msecs)) { | 401 | | - throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs', | 402 | | - 'a non-negative finite number', msecs); | 403 | | - } | | 392 | + item._idleTimeout = timerInternals.validateTimerDuration(msecs); | 404 | 393 |
| 405 | 394 | // if this item was already in a list somewhere | 406 | 395 | // then we should unenroll it from that | 407 | 396 | if (item._idleNext) unenroll(item); | 408 | 397 |
| 409 | | - // Ensure that msecs fits into signed int32 | 410 | | - if (msecs > TIMEOUT_MAX) { | 411 | | - process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` + | 412 | | - `\nTimer duration was truncated to ${TIMEOUT_MAX}.`, | 413 | | - 'TimeoutOverflowWarning'); | 414 | | - msecs = TIMEOUT_MAX; | 415 | | - } | 416 | | - | 417 | | - item._idleTimeout = msecs; | 418 | 398 | L.init(item); | 419 | 399 | }; | 420 | 400 |
| |
|