Skip to content

Commit 5b9e570

Browse files
committed
lib: add signal name validator
PR-URL: #27137 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 72f4a83 commit 5b9e570

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lib/internal/validators.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ const {
55
codes: {
66
ERR_INVALID_ARG_TYPE,
77
ERR_INVALID_ARG_VALUE,
8-
ERR_OUT_OF_RANGE
8+
ERR_OUT_OF_RANGE,
9+
ERR_UNKNOWN_SIGNAL
910
}
1011
} = require('internal/errors');
1112
const {
1213
isArrayBufferView
1314
} = require('internal/util/types');
15+
const { signals } = internalBinding('constants').os;
1416

1517
function isInt32(value) {
1618
return value === (value | 0);
@@ -110,6 +112,20 @@ function validateNumber(value, name) {
110112
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
111113
}
112114

115+
function validateSignalName(signal, name = 'signal') {
116+
if (typeof signal !== 'string')
117+
throw new ERR_INVALID_ARG_TYPE(name, 'string', signal);
118+
119+
if (signals[signal] === undefined) {
120+
if (signals[signal.toUpperCase()] !== undefined) {
121+
throw new ERR_UNKNOWN_SIGNAL(signal +
122+
' (signals must use all capital letters)');
123+
}
124+
125+
throw new ERR_UNKNOWN_SIGNAL(signal);
126+
}
127+
}
128+
113129
// TODO(BridgeAR): We have multiple validation functions that call
114130
// `require('internal/utils').toBuf()` before validating for array buffer views.
115131
// Those should likely also be consolidated in here.
@@ -130,5 +146,6 @@ module.exports = {
130146
validateInt32,
131147
validateUint32,
132148
validateString,
133-
validateNumber
149+
validateNumber,
150+
validateSignalName
134151
};

0 commit comments

Comments
 (0)