Skip to content

Commit 0fbe945

Browse files
aduh95targos
authored andcommitted
lib: refactor to use more primordials
PR-URL: #36140 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74fe1d8 commit 0fbe945

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

lib/internal/main/print_help.js

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
'use strict';
22

33
const {
4+
ArrayPrototypeConcat,
5+
ArrayPrototypeSort,
46
Boolean,
5-
Map,
67
MathFloor,
78
MathMax,
89
ObjectKeys,
910
RegExp,
11+
StringPrototypeTrimLeft,
12+
StringPrototypeRepeat,
13+
StringPrototypeReplace,
14+
SafeMap,
1015
} = primordials;
1116

1217
const { types } = internalBinding('options');
@@ -23,7 +28,7 @@ for (const key of ObjectKeys(types))
2328
// Environment variables are parsed ad-hoc throughout the code base,
2429
// so we gather the documentation here.
2530
const { hasIntl, hasSmallICU, hasNodeOptions } = internalBinding('config');
26-
const envVars = new Map([
31+
const envVars = new SafeMap(ArrayPrototypeConcat([
2732
['NODE_DEBUG', { helpText: "','-separated list of core modules that " +
2833
'should print debug information' }],
2934
['NODE_DEBUG_NATIVE', { helpText: "','-separated list of C++ core debug " +
@@ -51,28 +56,30 @@ const envVars = new Map([
5156
'to' }],
5257
['UV_THREADPOOL_SIZE', { helpText: 'sets the number of threads used in ' +
5358
'libuv\'s threadpool' }]
54-
].concat(hasIntl ? [
59+
], hasIntl ? [
5560
['NODE_ICU_DATA', { helpText: 'data path for ICU (Intl object) data' +
5661
hasSmallICU ? '' : ' (will extend linked-in data)' }]
57-
] : []).concat(hasNodeOptions ? [
62+
] : []), (hasNodeOptions ? [
5863
['NODE_OPTIONS', { helpText: 'set CLI options in the environment via a ' +
5964
'space-separated list' }]
60-
] : []).concat(hasCrypto ? [
65+
] : []), hasCrypto ? [
6166
['OPENSSL_CONF', { helpText: 'load OpenSSL configuration from file' }],
6267
['SSL_CERT_DIR', { helpText: 'sets OpenSSL\'s directory of trusted ' +
6368
'certificates when used in conjunction with --use-openssl-ca' }],
6469
['SSL_CERT_FILE', { helpText: 'sets OpenSSL\'s trusted certificate file ' +
6570
'when used in conjunction with --use-openssl-ca' }],
66-
] : []));
71+
] : []);
6772

6873

6974
function indent(text, depth) {
70-
return text.replace(/^/gm, ' '.repeat(depth));
75+
return StringPrototypeReplace(text, /^/gm, StringPrototypeRepeat(' ', depth));
7176
}
7277

7378
function fold(text, width) {
74-
return text.replace(new RegExp(`([^\n]{0,${width}})( |$)`, 'g'),
75-
(_, newLine, end) => newLine + (end === ' ' ? '\n' : ''));
79+
return StringPrototypeReplace(text,
80+
new RegExp(`([^\n]{0,${width}})( |$)`, 'g'),
81+
(_, newLine, end) =>
82+
newLine + (end === ' ' ? '\n' : ''));
7683
}
7784

7885
function getArgDescription(type) {
@@ -94,13 +101,15 @@ function getArgDescription(type) {
94101
}
95102
}
96103

97-
function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
104+
function format(
105+
{ options, aliases = new SafeMap(), firstColumn, secondColumn }
106+
) {
98107
let text = '';
99108
let maxFirstColumnUsed = 0;
100109

101110
for (const [
102111
name, { helpText, type, value }
103-
] of [...options.entries()].sort()) {
112+
] of ArrayPrototypeSort([...options.entries()])) {
104113
if (!helpText) continue;
105114

106115
let displayName = name;
@@ -136,12 +145,12 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
136145
text += displayName;
137146
maxFirstColumnUsed = MathMax(maxFirstColumnUsed, displayName.length);
138147
if (displayName.length >= firstColumn)
139-
text += '\n' + ' '.repeat(firstColumn);
148+
text += '\n' + StringPrototypeRepeat(' ', firstColumn);
140149
else
141-
text += ' '.repeat(firstColumn - displayName.length);
150+
text += StringPrototypeRepeat(' ', firstColumn - displayName.length);
142151

143-
text += indent(fold(displayHelpText, secondColumn),
144-
firstColumn).trimLeft() + '\n';
152+
text += indent(StringPrototypeTrimLeft(fold(displayHelpText, secondColumn),
153+
firstColumn)) + '\n';
145154
}
146155

147156
if (maxFirstColumnUsed < firstColumn - 4) {

lib/internal/main/worker_thread.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
// message port.
55

66
const {
7+
ArrayPrototypeConcat,
8+
ArrayPrototypeSplice,
79
ObjectDefineProperty,
10+
PromisePrototypeCatch,
811
} = primordials;
912

1013
const {
@@ -120,7 +123,7 @@ port.on('message', (message) => {
120123
initializeESMLoader();
121124

122125
if (argv !== undefined) {
123-
process.argv = process.argv.concat(argv);
126+
process.argv = ArrayPrototypeConcat(process.argv, argv);
124127
}
125128
publicWorker.parentPort = publicPort;
126129
publicWorker.workerData = workerData;
@@ -162,18 +165,18 @@ port.on('message', (message) => {
162165
enumerable: true,
163166
value: filename,
164167
});
165-
process.argv.splice(1, 0, name);
168+
ArrayPrototypeSplice(process.argv, 1, 0, name);
166169
evalScript(name, filename);
167170
} else if (doEval === 'module') {
168171
const { evalModule } = require('internal/process/execution');
169-
evalModule(filename).catch((e) => {
172+
PromisePrototypeCatch(evalModule(filename), (e) => {
170173
workerOnGlobalUncaughtException(e, true);
171174
});
172175
} else {
173176
// script filename
174177
// runMain here might be monkey-patched by users in --require.
175178
// XXX: the monkey-patchability here should probably be deprecated.
176-
process.argv.splice(1, 0, filename);
179+
ArrayPrototypeSplice(process.argv, 1, 0, filename);
177180
CJSLoader.Module.runMain(filename);
178181
}
179182
} else if (message.type === STDIO_PAYLOAD) {

0 commit comments

Comments
 (0)