mirror of
https://github.com/nodejs/node.git
synced 2025-04-28 13:40:37 +00:00
lib: refactor to use more primordials
PR-URL: https://github.com/nodejs/node/pull/36140 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
ad0a01caed
commit
3e0194e88e
@ -1,12 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
ArrayPrototypeConcat,
|
||||
ArrayPrototypeSort,
|
||||
Boolean,
|
||||
Map,
|
||||
MathFloor,
|
||||
MathMax,
|
||||
ObjectKeys,
|
||||
RegExp,
|
||||
StringPrototypeTrimLeft,
|
||||
StringPrototypeRepeat,
|
||||
StringPrototypeReplace,
|
||||
SafeMap,
|
||||
} = primordials;
|
||||
|
||||
const { types } = internalBinding('options');
|
||||
@ -23,7 +28,7 @@ for (const key of ObjectKeys(types))
|
||||
// Environment variables are parsed ad-hoc throughout the code base,
|
||||
// so we gather the documentation here.
|
||||
const { hasIntl, hasSmallICU, hasNodeOptions } = internalBinding('config');
|
||||
const envVars = new Map([
|
||||
const envVars = new SafeMap(ArrayPrototypeConcat([
|
||||
['NODE_DEBUG', { helpText: "','-separated list of core modules that " +
|
||||
'should print debug information' }],
|
||||
['NODE_DEBUG_NATIVE', { helpText: "','-separated list of C++ core debug " +
|
||||
@ -51,28 +56,30 @@ const envVars = new Map([
|
||||
'to' }],
|
||||
['UV_THREADPOOL_SIZE', { helpText: 'sets the number of threads used in ' +
|
||||
'libuv\'s threadpool' }]
|
||||
].concat(hasIntl ? [
|
||||
], hasIntl ? [
|
||||
['NODE_ICU_DATA', { helpText: 'data path for ICU (Intl object) data' +
|
||||
hasSmallICU ? '' : ' (will extend linked-in data)' }]
|
||||
] : []).concat(hasNodeOptions ? [
|
||||
] : []), (hasNodeOptions ? [
|
||||
['NODE_OPTIONS', { helpText: 'set CLI options in the environment via a ' +
|
||||
'space-separated list' }]
|
||||
] : []).concat(hasCrypto ? [
|
||||
] : []), hasCrypto ? [
|
||||
['OPENSSL_CONF', { helpText: 'load OpenSSL configuration from file' }],
|
||||
['SSL_CERT_DIR', { helpText: 'sets OpenSSL\'s directory of trusted ' +
|
||||
'certificates when used in conjunction with --use-openssl-ca' }],
|
||||
['SSL_CERT_FILE', { helpText: 'sets OpenSSL\'s trusted certificate file ' +
|
||||
'when used in conjunction with --use-openssl-ca' }],
|
||||
] : []));
|
||||
] : []);
|
||||
|
||||
|
||||
function indent(text, depth) {
|
||||
return text.replace(/^/gm, ' '.repeat(depth));
|
||||
return StringPrototypeReplace(text, /^/gm, StringPrototypeRepeat(' ', depth));
|
||||
}
|
||||
|
||||
function fold(text, width) {
|
||||
return text.replace(new RegExp(`([^\n]{0,${width}})( |$)`, 'g'),
|
||||
(_, newLine, end) => newLine + (end === ' ' ? '\n' : ''));
|
||||
return StringPrototypeReplace(text,
|
||||
new RegExp(`([^\n]{0,${width}})( |$)`, 'g'),
|
||||
(_, newLine, end) =>
|
||||
newLine + (end === ' ' ? '\n' : ''));
|
||||
}
|
||||
|
||||
function getArgDescription(type) {
|
||||
@ -94,13 +101,15 @@ function getArgDescription(type) {
|
||||
}
|
||||
}
|
||||
|
||||
function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
|
||||
function format(
|
||||
{ options, aliases = new SafeMap(), firstColumn, secondColumn }
|
||||
) {
|
||||
let text = '';
|
||||
let maxFirstColumnUsed = 0;
|
||||
|
||||
for (const [
|
||||
name, { helpText, type, value }
|
||||
] of [...options.entries()].sort()) {
|
||||
] of ArrayPrototypeSort([...options.entries()])) {
|
||||
if (!helpText) continue;
|
||||
|
||||
let displayName = name;
|
||||
@ -136,12 +145,12 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
|
||||
text += displayName;
|
||||
maxFirstColumnUsed = MathMax(maxFirstColumnUsed, displayName.length);
|
||||
if (displayName.length >= firstColumn)
|
||||
text += '\n' + ' '.repeat(firstColumn);
|
||||
text += '\n' + StringPrototypeRepeat(' ', firstColumn);
|
||||
else
|
||||
text += ' '.repeat(firstColumn - displayName.length);
|
||||
text += StringPrototypeRepeat(' ', firstColumn - displayName.length);
|
||||
|
||||
text += indent(fold(displayHelpText, secondColumn),
|
||||
firstColumn).trimLeft() + '\n';
|
||||
text += indent(StringPrototypeTrimLeft(fold(displayHelpText, secondColumn),
|
||||
firstColumn)) + '\n';
|
||||
}
|
||||
|
||||
if (maxFirstColumnUsed < firstColumn - 4) {
|
||||
|
@ -4,7 +4,10 @@
|
||||
// message port.
|
||||
|
||||
const {
|
||||
ArrayPrototypeConcat,
|
||||
ArrayPrototypeSplice,
|
||||
ObjectDefineProperty,
|
||||
PromisePrototypeCatch,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
@ -122,7 +125,7 @@ port.on('message', (message) => {
|
||||
loadPreloadModules();
|
||||
initializeFrozenIntrinsics();
|
||||
if (argv !== undefined) {
|
||||
process.argv = process.argv.concat(argv);
|
||||
process.argv = ArrayPrototypeConcat(process.argv, argv);
|
||||
}
|
||||
publicWorker.parentPort = publicPort;
|
||||
publicWorker.workerData = workerData;
|
||||
@ -159,18 +162,18 @@ port.on('message', (message) => {
|
||||
enumerable: true,
|
||||
value: filename,
|
||||
});
|
||||
process.argv.splice(1, 0, name);
|
||||
ArrayPrototypeSplice(process.argv, 1, 0, name);
|
||||
evalScript(name, filename);
|
||||
} else if (doEval === 'module') {
|
||||
const { evalModule } = require('internal/process/execution');
|
||||
evalModule(filename).catch((e) => {
|
||||
PromisePrototypeCatch(evalModule(filename), (e) => {
|
||||
workerOnGlobalUncaughtException(e, true);
|
||||
});
|
||||
} else {
|
||||
// script filename
|
||||
// runMain here might be monkey-patched by users in --require.
|
||||
// XXX: the monkey-patchability here should probably be deprecated.
|
||||
process.argv.splice(1, 0, filename);
|
||||
ArrayPrototypeSplice(process.argv, 1, 0, filename);
|
||||
CJSLoader.Module.runMain(filename);
|
||||
}
|
||||
} else if (message.type === STDIO_PAYLOAD) {
|
||||
|
Loading…
Reference in New Issue
Block a user