Skip to content

Commit

Permalink
lib: add signal name validator
Browse files Browse the repository at this point in the history
PR-URL: #27137
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
cjihrig committed Apr 11, 2019
1 parent 72f4a83 commit 5b9e570
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE
ERR_OUT_OF_RANGE,
ERR_UNKNOWN_SIGNAL
}
} = require('internal/errors');
const {
isArrayBufferView
} = require('internal/util/types');
const { signals } = internalBinding('constants').os;

function isInt32(value) {
return value === (value | 0);
Expand Down Expand Up @@ -110,6 +112,20 @@ function validateNumber(value, name) {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}

function validateSignalName(signal, name = 'signal') {
if (typeof signal !== 'string')
throw new ERR_INVALID_ARG_TYPE(name, 'string', signal);

if (signals[signal] === undefined) {
if (signals[signal.toUpperCase()] !== undefined) {
throw new ERR_UNKNOWN_SIGNAL(signal +
' (signals must use all capital letters)');
}

throw new ERR_UNKNOWN_SIGNAL(signal);
}
}

// TODO(BridgeAR): We have multiple validation functions that call
// `require('internal/utils').toBuf()` before validating for array buffer views.
// Those should likely also be consolidated in here.
Expand All @@ -130,5 +146,6 @@ module.exports = {
validateInt32,
validateUint32,
validateString,
validateNumber
validateNumber,
validateSignalName
};

0 comments on commit 5b9e570

Please sign in to comment.