Skip to content

Commit

Permalink
process: refactor process per thread
Browse files Browse the repository at this point in the history
• Validate the `prevValue` parameter in the `cpuUsage()` function at
the correct place.
• Use validation methods for consistency.
• Use the logical OR assignment operator (`||=`) where appropriate.
• Use `StringPrototypeReplaceAll()` where appropriate.
  • Loading branch information
VoltrexKeyva committed Nov 7, 2021
1 parent 96e00e0 commit 31df23c
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
SetPrototypeValues,
StringPrototypeEndsWith,
StringPrototypeReplace,
StringPrototypeReplaceAll,
StringPrototypeSlice,
StringPrototypeStartsWith,
Symbol,
Expand All @@ -33,15 +34,14 @@ const {
const {
errnoException,
codes: {
ERR_ASSERTION,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE,
ERR_UNKNOWN_SIGNAL
}
} = require('internal/errors');
const format = require('internal/util/inspect').format;
const {
isInt32,
validateArray,
validateNumber,
validateObject,
Expand Down Expand Up @@ -120,9 +120,9 @@ function wrapProcessMethods(binding) {
function cpuUsage(prevValue) {
// If a previous value was passed in, ensure it has the correct shape.
if (prevValue) {
if (!previousValueIsValid(prevValue.user)) {
validateObject(prevValue, 'prevValue');
validateObject(prevValue, 'prevValue');

if (!previousValueIsValid(prevValue.user)) {
validateNumber(prevValue.user, 'prevValue.user');
throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.user',
prevValue.user);
Expand Down Expand Up @@ -192,18 +192,15 @@ function wrapProcessMethods(binding) {
function kill(pid, sig) {
let err;

// eslint-disable-next-line eqeqeq
if (pid != (pid | 0)) {
throw new ERR_INVALID_ARG_TYPE('pid', 'number', pid);
}
validateNumber(pid, 'pid');

// Preserve null signal
if (sig === (sig | 0)) {
if (isInt32(sig)) {
// XXX(joyeecheung): we have to use process._kill here because
// it's monkey-patched by tests.
err = process._kill(pid, sig);
} else {
sig = sig || 'SIGTERM';
sig ||= 'SIGTERM';
if (constants[sig]) {
err = process._kill(pid, constants[sig]);
} else {
Expand Down Expand Up @@ -251,7 +248,6 @@ function wrapProcessMethods(binding) {
};
}

const replaceUnderscoresRegex = /_/g;
const leadingDashesRegex = /^--?/;
const trailingValuesRegex = /=.*$/;

Expand Down Expand Up @@ -333,7 +329,7 @@ function buildAllowedFlags() {
// on a dummy option set and see whether it rejects the argument or
// not.
if (typeof key === 'string') {
key = StringPrototypeReplace(key, replaceUnderscoresRegex, '-');
key = StringPrototypeReplaceAll(key, '_', '-');
if (RegExpPrototypeTest(leadingDashesRegex, key)) {
key = StringPrototypeReplace(key, trailingValuesRegex, '');
return ArrayPrototypeIncludes(this[kInternal].array, key);
Expand Down

0 comments on commit 31df23c

Please sign in to comment.