Skip to content
Permalink
Browse files

lib: add signal name validator

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 8, 2019
1 parent 72f4a83 commit 5b9e57012a5c4d4132700e3c345dceb08ba947ed
Showing with 19 additions and 2 deletions.
  1. +19 −2 lib/internal/validators.js
@@ -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);
@@ -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.
@@ -130,5 +146,6 @@ module.exports = {
validateInt32,
validateUint32,
validateString,
validateNumber
validateNumber,
validateSignalName
};

0 comments on commit 5b9e570

Please sign in to comment.
You can’t perform that action at this time.