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

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

‎lib/internal/main/print_help.js‎

Lines changed: 24 additions & 15 deletions
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-
constenvVars=newMap([
31+
constenvVars=newSafeMap(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
functionindent(text,depth){
70-
returntext.replace(/^/gm,' '.repeat(depth));
75+
returnStringPrototypeReplace(text,/^/gm,StringPrototypeRepeat(' ',depth));
7176
}
7277

7378
functionfold(text,width){
74-
returntext.replace(newRegExp(`([^\n]{0,${width}})( |$)`,'g'),
75-
(_,newLine,end)=>newLine+(end===' ' ? '\n' : ''));
79+
returnStringPrototypeReplace(text,
80+
newRegExp(`([^\n]{0,${width}})( |$)`,'g'),
81+
(_,newLine,end)=>
82+
newLine+(end===' ' ? '\n' : ''));
7683
}
7784

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

97-
functionformat({ options, aliases =newMap(), firstColumn, secondColumn }){
104+
functionformat(
105+
{ options, aliases =newSafeMap(), firstColumn, secondColumn }
106+
){
98107
lettext='';
99108
letmaxFirstColumnUsed=0;
100109

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

106115
letdisplayName=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‎

Lines changed: 7 additions & 4 deletions
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
}elseif(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
}elseif(message.type===STDIO_PAYLOAD){

0 commit comments

Comments
 (0)