diff --git a/lib/internal/timers.js b/lib/internal/timers.js index 677da4e8ea8..e4fbc6e0a36 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -26,6 +26,7 @@ module.exports = { trigger_async_id_symbol, Timeout, setUnrefTimeout, + validateTimerDuration }; // Timer constructor function. @@ -105,3 +106,26 @@ function setUnrefTimeout(callback, after, arg1, arg2, arg3) { return timer; } + +// Type checking used by timers.enroll() and Socket#setTimeout() +function validateTimerDuration(msecs) { + if (typeof msecs !== 'number') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs', + 'number', msecs); + } + + if (msecs < 0 || !isFinite(msecs)) { + throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs', + 'a non-negative finite number', msecs); + } + + // Ensure that msecs fits into signed int32 + if (msecs > TIMEOUT_MAX) { + process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` + + `\nTimer duration was truncated to ${TIMEOUT_MAX}.`, + 'TimeoutOverflowWarning'); + return TIMEOUT_MAX; + } + + return msecs; +} diff --git a/lib/net.js b/lib/net.js index b6cfa30c69b..09ebc1b082a 100644 --- a/lib/net.js +++ b/lib/net.js @@ -57,7 +57,11 @@ var cluster = null; const errnoException = util._errnoException; const exceptionWithHostPort = util._exceptionWithHostPort; -const { kTimeout, TIMEOUT_MAX, setUnrefTimeout } = require('internal/timers'); +const { + kTimeout, + setUnrefTimeout, + validateTimerDuration +} = require('internal/timers'); function noop() {} @@ -394,23 +398,7 @@ Socket.prototype.read = function(n) { Socket.prototype.setTimeout = function(msecs, callback) { // Type checking identical to timers.enroll() - if (typeof msecs !== 'number') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs', - 'number', msecs); - } - - if (msecs < 0 || !isFinite(msecs)) { - throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs', - 'a non-negative finite number', msecs); - } - - // Ensure that msecs fits into signed int32 - if (msecs > TIMEOUT_MAX) { - process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` + - `\nTimer duration was truncated to ${TIMEOUT_MAX}.`, - 'TimeoutOverflowWarning'); - msecs = TIMEOUT_MAX; - } + msecs = validateTimerDuration(msecs); // Attempt to clear an existing timer lear in both cases - // even if it will be rescheduled we don't want to leak an existing timer. diff --git a/lib/timers.js b/lib/timers.js index e9a39ee281b..5539234352b 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -55,9 +55,6 @@ delete process._scheduledImmediateCount; const activateImmediateCheck = process._activateImmediateCheck; delete process._activateImmediateCheck; -// Timeout values > TIMEOUT_MAX are set to 1. -const TIMEOUT_MAX = timerInternals.TIMEOUT_MAX; - // The Timeout class const Timeout = timerInternals.Timeout; @@ -392,29 +389,12 @@ const unenroll = exports.unenroll = function(item) { // This function does not start the timer, see `active()`. // Using existing objects as timers slightly reduces object overhead. exports.enroll = function(item, msecs) { - if (typeof msecs !== 'number') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs', - 'number', msecs); - } - - if (msecs < 0 || !isFinite(msecs)) { - throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs', - 'a non-negative finite number', msecs); - } + item._idleTimeout = timerInternals.validateTimerDuration(msecs); // if this item was already in a list somewhere // then we should unenroll it from that if (item._idleNext) unenroll(item); - // Ensure that msecs fits into signed int32 - if (msecs > TIMEOUT_MAX) { - process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` + - `\nTimer duration was truncated to ${TIMEOUT_MAX}.`, - 'TimeoutOverflowWarning'); - msecs = TIMEOUT_MAX; - } - - item._idleTimeout = msecs; L.init(item); };