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:
Antoine du Hamel 2020-11-15 19:18:33 +01:00
parent ad0a01caed
commit 3e0194e88e
2 changed files with 31 additions and 19 deletions

View File

@ -1,12 +1,17 @@
'use strict'; 'use strict';
const { const {
ArrayPrototypeConcat,
ArrayPrototypeSort,
Boolean, Boolean,
Map,
MathFloor, MathFloor,
MathMax, MathMax,
ObjectKeys, ObjectKeys,
RegExp, RegExp,
StringPrototypeTrimLeft,
StringPrototypeRepeat,
StringPrototypeReplace,
SafeMap,
} = primordials; } = primordials;
const { types } = internalBinding('options'); const { types } = internalBinding('options');
@ -23,7 +28,7 @@ for (const key of ObjectKeys(types))
// Environment variables are parsed ad-hoc throughout the code base, // Environment variables are parsed ad-hoc throughout the code base,
// so we gather the documentation here. // so we gather the documentation here.
const { hasIntl, hasSmallICU, hasNodeOptions } = internalBinding('config'); const { hasIntl, hasSmallICU, hasNodeOptions } = internalBinding('config');
const envVars = new Map([ const envVars = new SafeMap(ArrayPrototypeConcat([
['NODE_DEBUG', { helpText: "','-separated list of core modules that " + ['NODE_DEBUG', { helpText: "','-separated list of core modules that " +
'should print debug information' }], 'should print debug information' }],
['NODE_DEBUG_NATIVE', { helpText: "','-separated list of C++ core debug " + ['NODE_DEBUG_NATIVE', { helpText: "','-separated list of C++ core debug " +
@ -51,28 +56,30 @@ const envVars = new Map([
'to' }], 'to' }],
['UV_THREADPOOL_SIZE', { helpText: 'sets the number of threads used in ' + ['UV_THREADPOOL_SIZE', { helpText: 'sets the number of threads used in ' +
'libuv\'s threadpool' }] 'libuv\'s threadpool' }]
].concat(hasIntl ? [ ], hasIntl ? [
['NODE_ICU_DATA', { helpText: 'data path for ICU (Intl object) data' + ['NODE_ICU_DATA', { helpText: 'data path for ICU (Intl object) data' +
hasSmallICU ? '' : ' (will extend linked-in data)' }] hasSmallICU ? '' : ' (will extend linked-in data)' }]
] : []).concat(hasNodeOptions ? [ ] : []), (hasNodeOptions ? [
['NODE_OPTIONS', { helpText: 'set CLI options in the environment via a ' + ['NODE_OPTIONS', { helpText: 'set CLI options in the environment via a ' +
'space-separated list' }] 'space-separated list' }]
] : []).concat(hasCrypto ? [ ] : []), hasCrypto ? [
['OPENSSL_CONF', { helpText: 'load OpenSSL configuration from file' }], ['OPENSSL_CONF', { helpText: 'load OpenSSL configuration from file' }],
['SSL_CERT_DIR', { helpText: 'sets OpenSSL\'s directory of trusted ' + ['SSL_CERT_DIR', { helpText: 'sets OpenSSL\'s directory of trusted ' +
'certificates when used in conjunction with --use-openssl-ca' }], 'certificates when used in conjunction with --use-openssl-ca' }],
['SSL_CERT_FILE', { helpText: 'sets OpenSSL\'s trusted certificate file ' + ['SSL_CERT_FILE', { helpText: 'sets OpenSSL\'s trusted certificate file ' +
'when used in conjunction with --use-openssl-ca' }], 'when used in conjunction with --use-openssl-ca' }],
] : [])); ] : []);
function indent(text, depth) { function indent(text, depth) {
return text.replace(/^/gm, ' '.repeat(depth)); return StringPrototypeReplace(text, /^/gm, StringPrototypeRepeat(' ', depth));
} }
function fold(text, width) { function fold(text, width) {
return text.replace(new RegExp(`([^\n]{0,${width}})( |$)`, 'g'), return StringPrototypeReplace(text,
(_, newLine, end) => newLine + (end === ' ' ? '\n' : '')); new RegExp(`([^\n]{0,${width}})( |$)`, 'g'),
(_, newLine, end) =>
newLine + (end === ' ' ? '\n' : ''));
} }
function getArgDescription(type) { 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 text = '';
let maxFirstColumnUsed = 0; let maxFirstColumnUsed = 0;
for (const [ for (const [
name, { helpText, type, value } name, { helpText, type, value }
] of [...options.entries()].sort()) { ] of ArrayPrototypeSort([...options.entries()])) {
if (!helpText) continue; if (!helpText) continue;
let displayName = name; let displayName = name;
@ -136,12 +145,12 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
text += displayName; text += displayName;
maxFirstColumnUsed = MathMax(maxFirstColumnUsed, displayName.length); maxFirstColumnUsed = MathMax(maxFirstColumnUsed, displayName.length);
if (displayName.length >= firstColumn) if (displayName.length >= firstColumn)
text += '\n' + ' '.repeat(firstColumn); text += '\n' + StringPrototypeRepeat(' ', firstColumn);
else else
text += ' '.repeat(firstColumn - displayName.length); text += StringPrototypeRepeat(' ', firstColumn - displayName.length);
text += indent(fold(displayHelpText, secondColumn), text += indent(StringPrototypeTrimLeft(fold(displayHelpText, secondColumn),
firstColumn).trimLeft() + '\n'; firstColumn)) + '\n';
} }
if (maxFirstColumnUsed < firstColumn - 4) { if (maxFirstColumnUsed < firstColumn - 4) {

View File

@ -4,7 +4,10 @@
// message port. // message port.
const { const {
ArrayPrototypeConcat,
ArrayPrototypeSplice,
ObjectDefineProperty, ObjectDefineProperty,
PromisePrototypeCatch,
} = primordials; } = primordials;
const { const {
@ -122,7 +125,7 @@ port.on('message', (message) => {
loadPreloadModules(); loadPreloadModules();
initializeFrozenIntrinsics(); initializeFrozenIntrinsics();
if (argv !== undefined) { if (argv !== undefined) {
process.argv = process.argv.concat(argv); process.argv = ArrayPrototypeConcat(process.argv, argv);
} }
publicWorker.parentPort = publicPort; publicWorker.parentPort = publicPort;
publicWorker.workerData = workerData; publicWorker.workerData = workerData;
@ -159,18 +162,18 @@ port.on('message', (message) => {
enumerable: true, enumerable: true,
value: filename, value: filename,
}); });
process.argv.splice(1, 0, name); ArrayPrototypeSplice(process.argv, 1, 0, name);
evalScript(name, filename); evalScript(name, filename);
} else if (doEval === 'module') { } else if (doEval === 'module') {
const { evalModule } = require('internal/process/execution'); const { evalModule } = require('internal/process/execution');
evalModule(filename).catch((e) => { PromisePrototypeCatch(evalModule(filename), (e) => {
workerOnGlobalUncaughtException(e, true); workerOnGlobalUncaughtException(e, true);
}); });
} else { } else {
// script filename // script filename
// runMain here might be monkey-patched by users in --require. // runMain here might be monkey-patched by users in --require.
// XXX: the monkey-patchability here should probably be deprecated. // 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); CJSLoader.Module.runMain(filename);
} }
} else if (message.type === STDIO_PAYLOAD) { } else if (message.type === STDIO_PAYLOAD) {