Skip to content

Commit

Permalink
lib: created isValidCallback helper
Browse files Browse the repository at this point in the history
check for callback function is moved to a separate function.
This piece of code is being shared by other entities as well.

PR-URL: #32665
Reviewed-By: Zeyu Yang <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gerhard Stöbich <[email protected]>
  • Loading branch information
yashLadha authored and BethGriggs committed Apr 14, 2020
1 parent c0f43bf commit 399749e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
9 changes: 8 additions & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE,
ERR_UNKNOWN_SIGNAL
ERR_UNKNOWN_SIGNAL,
ERR_INVALID_CALLBACK,
}
} = require('internal/errors');
const { normalizeEncoding } = require('internal/util');
Expand Down Expand Up @@ -194,6 +195,11 @@ function validatePort(port, name = 'Port', { allowZero = true } = {}) {
return port | 0;
}

const validateCallback = hideStackFrames((callback) => {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
});

module.exports = {
isInt32,
isUint32,
Expand All @@ -210,4 +216,5 @@ module.exports = {
validateSignalName,
validateString,
validateUint32,
validateCallback,
};
14 changes: 4 additions & 10 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const {
promisify: { custom: customPromisify },
deprecate
} = require('internal/util');
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
const debug = require('internal/util/debuglog').debuglog('timer');
const { validateCallback } = require('internal/validators');

const {
destroyHooksExist,
Expand Down Expand Up @@ -118,9 +118,7 @@ function enroll(item, msecs) {


function setTimeout(callback, after, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
validateCallback(callback);

let i, args;
switch (arguments.length) {
Expand Down Expand Up @@ -165,9 +163,7 @@ function clearTimeout(timer) {
}

function setInterval(callback, repeat, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
validateCallback(callback);

let i, args;
switch (arguments.length) {
Expand Down Expand Up @@ -249,9 +245,7 @@ const Immediate = class Immediate {
};

function setImmediate(callback, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
validateCallback(callback);

let i, args;
switch (arguments.length) {
Expand Down

0 comments on commit 399749e

Please sign in to comment.