Skip to content

Commit 36baf89

Browse files
committed
process: refactor process per thread
• 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.
1 parent 549a4c9 commit 36baf89

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

lib/internal/process/per_thread.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,23 @@ const {
3434
errnoException,
3535
codes: {
3636
ERR_ASSERTION,
37-
ERR_INVALID_ARG_TYPE,
3837
ERR_INVALID_ARG_VALUE,
3938
ERR_OUT_OF_RANGE,
4039
ERR_UNKNOWN_SIGNAL
4140
}
4241
} = require('internal/errors');
4342
const format = require('internal/util/inspect').format;
4443
const {
44+
isInt32,
4545
validateArray,
4646
validateNumber,
4747
validateObject,
4848
} = require('internal/validators');
49+
const assert = require('assert');
4950
const constants = internalBinding('constants').os.signals;
5051

5152
const kInternal = Symbol('internal properties');
5253

53-
function assert(x, msg) {
54-
if (!x) throw new ERR_ASSERTION(msg || 'assertion error');
55-
}
56-
5754
function getFastAPIs(binding) {
5855
const {
5956
hrtime: _hrtime
@@ -120,9 +117,9 @@ function wrapProcessMethods(binding) {
120117
function cpuUsage(prevValue) {
121118
// If a previous value was passed in, ensure it has the correct shape.
122119
if (prevValue) {
123-
if (!previousValueIsValid(prevValue.user)) {
124-
validateObject(prevValue, 'prevValue');
120+
validateObject(prevValue, 'prevValue');
125121

122+
if (!previousValueIsValid(prevValue.user)) {
126123
validateNumber(prevValue.user, 'prevValue.user');
127124
throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.user',
128125
prevValue.user);
@@ -192,18 +189,15 @@ function wrapProcessMethods(binding) {
192189
function kill(pid, sig) {
193190
let err;
194191

195-
// eslint-disable-next-line eqeqeq
196-
if (pid != (pid | 0)) {
197-
throw new ERR_INVALID_ARG_TYPE('pid', 'number', pid);
198-
}
192+
validateNumber(pid, 'pid');
199193

200194
// Preserve null signal
201-
if (sig === (sig | 0)) {
195+
if (isInt32(sig)) {
202196
// XXX(joyeecheung): we have to use process._kill here because
203197
// it's monkey-patched by tests.
204198
err = process._kill(pid, sig);
205199
} else {
206-
sig = sig || 'SIGTERM';
200+
sig ||= 'SIGTERM';
207201
if (constants[sig]) {
208202
err = process._kill(pid, constants[sig]);
209203
} else {
@@ -251,7 +245,6 @@ function wrapProcessMethods(binding) {
251245
};
252246
}
253247

254-
const replaceUnderscoresRegex = /_/g;
255248
const leadingDashesRegex = /^--?/;
256249
const trailingValuesRegex = /=.*$/;
257250

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

0 commit comments

Comments
 (0)