Skip to content

Commit 593941a

Browse files
committed
timers: extract enroll() validation into a fn
This should help keep everything consistent. PR-URL: #17704 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
1 parent 24dd92e commit 593941a

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

lib/internal/timers.js

+24
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
trigger_async_id_symbol,
2727
Timeout,
2828
setUnrefTimeout,
29+
validateTimerDuration
2930
};
3031

3132
// Timer constructor function.
@@ -105,3 +106,26 @@ function setUnrefTimeout(callback, after, arg1, arg2, arg3) {
105106

106107
return timer;
107108
}
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+
}

lib/net.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ var cluster = null;
5757
const errnoException = util._errnoException;
5858
const exceptionWithHostPort = util._exceptionWithHostPort;
5959

60-
const { kTimeout, TIMEOUT_MAX, setUnrefTimeout } = require('internal/timers');
60+
const {
61+
kTimeout,
62+
setUnrefTimeout,
63+
validateTimerDuration
64+
} = require('internal/timers');
6165

6266
function noop() {}
6367

@@ -394,23 +398,7 @@ Socket.prototype.read = function(n) {
394398

395399
Socket.prototype.setTimeout = function(msecs, callback) {
396400
// 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);
414402

415403
// Attempt to clear an existing timer lear in both cases -
416404
// even if it will be rescheduled we don't want to leak an existing timer.

lib/timers.js

+1-21
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ delete process._scheduledImmediateCount;
5555
const activateImmediateCheck = process._activateImmediateCheck;
5656
delete process._activateImmediateCheck;
5757

58-
// Timeout values > TIMEOUT_MAX are set to 1.
59-
const TIMEOUT_MAX = timerInternals.TIMEOUT_MAX;
60-
6158
// The Timeout class
6259
const Timeout = timerInternals.Timeout;
6360

@@ -392,29 +389,12 @@ const unenroll = exports.unenroll = function(item) {
392389
// This function does not start the timer, see `active()`.
393390
// Using existing objects as timers slightly reduces object overhead.
394391
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);
404393

405394
// if this item was already in a list somewhere
406395
// then we should unenroll it from that
407396
if (item._idleNext) unenroll(item);
408397

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;
418398
L.init(item);
419399
};
420400

0 commit comments

Comments
 (0)