From 986e79aa9dbcfd8b8d1c3acd0b303cd011b4c91c Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 15 Nov 2020 19:18:33 +0100 Subject: [PATCH] lib: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36140 Reviewed-By: Rich Trott Reviewed-By: James M Snell --- lib/internal/main/print_help.js | 39 ++++++++++++++++++------------ lib/internal/main/worker_thread.js | 11 ++++++--- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/internal/main/print_help.js b/lib/internal/main/print_help.js index 68f49a6697519c..b7d718391db184 100644 --- a/lib/internal/main/print_help.js +++ b/lib/internal/main/print_help.js @@ -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) { diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index 6f8cb6b942b5f2..2c6902b5d7462a 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -4,7 +4,10 @@ // message port. const { + ArrayPrototypeConcat, + ArrayPrototypeSplice, ObjectDefineProperty, + PromisePrototypeCatch, } = primordials; const { @@ -120,7 +123,7 @@ port.on('message', (message) => { initializeESMLoader(); if (argv !== undefined) { - process.argv = process.argv.concat(argv); + process.argv = ArrayPrototypeConcat(process.argv, argv); } publicWorker.parentPort = publicPort; publicWorker.workerData = workerData; @@ -162,18 +165,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) {