From 1ede072cf556fcbac4465cae9e534c9f41be8193 Mon Sep 17 00:00:00 2001 From: voltrexmaster Date: Sat, 6 Nov 2021 09:38:34 -0700 Subject: [PATCH] process: refactor process per thread MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Use the `assert` subsystem instead of mocking it. • 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. --- lib/internal/process/per_thread.js | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index f1d11911a4444ac..b3f0130709ef38f 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -23,6 +23,7 @@ const { SetPrototypeValues, StringPrototypeEndsWith, StringPrototypeReplace, + StringPrototypeReplaceAll, StringPrototypeSlice, StringPrototypeStartsWith, Symbol, @@ -33,8 +34,6 @@ const { const { errnoException, codes: { - ERR_ASSERTION, - ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_OUT_OF_RANGE, ERR_UNKNOWN_SIGNAL @@ -42,18 +41,16 @@ const { } = require('internal/errors'); const format = require('internal/util/inspect').format; const { + isInt32, validateArray, validateNumber, validateObject, } = require('internal/validators'); +const assert = require('assert'); const constants = internalBinding('constants').os.signals; const kInternal = Symbol('internal properties'); -function assert(x, msg) { - if (!x) throw new ERR_ASSERTION(msg || 'assertion error'); -} - function getFastAPIs(binding) { const { hrtime: _hrtime @@ -120,9 +117,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); @@ -192,18 +189,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 { @@ -251,7 +245,6 @@ function wrapProcessMethods(binding) { }; } -const replaceUnderscoresRegex = /_/g; const leadingDashesRegex = /^--?/; const trailingValuesRegex = /=.*$/; @@ -333,7 +326,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);