diff options
author | Richard Lau <riclau@uk.ibm.com> | 2019-05-22 17:54:34 +0100 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-05-26 16:44:16 +0200 |
commit | 1a96abe84912c4cbd4a53c7e8de874e457bebdd1 (patch) | |
tree | 79c3e1045d33f845f98a7bcc0fbb0542901e2a59 /lib | |
parent | 76b9cf5424d01b7449e3b4902d305d3a04f355ea (diff) | |
download | android-node-v8-1a96abe84912c4cbd4a53c7e8de874e457bebdd1.tar.gz android-node-v8-1a96abe84912c4cbd4a53c7e8de874e457bebdd1.tar.bz2 android-node-v8-1a96abe84912c4cbd4a53c7e8de874e457bebdd1.zip |
Revert "lib: print to stdout/stderr directly instead of using console"
This reverts commit 2b24ffae2240163a74ae11e49ee198e98abb07dc.
Fixes: https://github.com/nodejs/node/issues/27819
PR-URL: https://github.com/nodejs/node/pull/27823
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/fs.js | 20 | ||||
-rw-r--r-- | lib/internal/fs/utils.js | 22 | ||||
-rw-r--r-- | lib/internal/main/repl.js | 10 | ||||
-rw-r--r-- | lib/internal/process/execution.js | 18 | ||||
-rw-r--r-- | lib/internal/util/print.js | 67 |
5 files changed, 35 insertions, 102 deletions
@@ -75,8 +75,7 @@ const { validateOffsetLengthRead, validateOffsetLengthWrite, validatePath, - warnOnNonPortableTemplate, - handleErrorFromBinding + warnOnNonPortableTemplate } = require('internal/fs/utils'); const { CHAR_FORWARD_SLASH, @@ -119,6 +118,23 @@ function showTruncateDeprecation() { } } +function handleErrorFromBinding(ctx) { + if (ctx.errno !== undefined) { // libuv error numbers + const err = uvException(ctx); + // eslint-disable-next-line no-restricted-syntax + Error.captureStackTrace(err, handleErrorFromBinding); + throw err; + } + if (ctx.error !== undefined) { // Errors created in C++ land. + // TODO(joyeecheung): currently, ctx.error are encoding errors + // usually caused by memory problems. We need to figure out proper error + // code(s) for this. + // eslint-disable-next-line no-restricted-syntax + Error.captureStackTrace(ctx.error, handleErrorFromBinding); + throw ctx.error; + } +} + function maybeCallback(cb) { if (typeof cb === 'function') return cb; diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 4cb06690bf..14abad81ec 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -12,8 +12,7 @@ const { ERR_INVALID_OPT_VALUE_ENCODING, ERR_OUT_OF_RANGE }, - hideStackFrames, - uvException + hideStackFrames } = require('internal/errors'); const { isUint8Array, @@ -452,26 +451,7 @@ function warnOnNonPortableTemplate(template) { } } -// This handles errors following the convention of the fs binding. -function handleErrorFromBinding(ctx) { - if (ctx.errno !== undefined) { // libuv error numbers - const err = uvException(ctx); - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(err, handleErrorFromBinding); - throw err; - } - if (ctx.error !== undefined) { // Errors created in C++ land. - // TODO(joyeecheung): currently, ctx.error are encoding errors - // usually caused by memory problems. We need to figure out proper error - // code(s) for this. - // eslint-disable-next-line no-restricted-syntax - Error.captureStackTrace(ctx.error, handleErrorFromBinding); - throw ctx.error; - } -} - module.exports = { - handleErrorFromBinding, assertEncoding, copyObject, Dirent, diff --git a/lib/internal/main/repl.js b/lib/internal/main/repl.js index b38102a154..93b932f0bd 100644 --- a/lib/internal/main/repl.js +++ b/lib/internal/main/repl.js @@ -11,7 +11,7 @@ const { evalScript } = require('internal/process/execution'); -const { print, kStderr, kStdout } = require('internal/util/print'); +const console = require('internal/console/global'); const { getOptionValue } = require('internal/options'); @@ -21,12 +21,14 @@ markBootstrapComplete(); // --input-type flag not supported in REPL if (getOptionValue('--input-type')) { - print(kStderr, 'Cannot specify --input-type for REPL'); + // If we can't write to stderr, we'd like to make this a noop, + // so use console.error. + console.error('Cannot specify --input-type for REPL'); process.exit(1); } -print(kStdout, `Welcome to Node.js ${process.version}.\n` + - 'Type ".help" for more information.'); +console.log(`Welcome to Node.js ${process.version}.\n` + + 'Type ".help" for more information.'); const cliRepl = require('internal/repl'); cliRepl.createInternalRepl(process.env, (err, repl) => { diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 227c1c2149..2b7fc41ccd 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -35,22 +35,24 @@ function tryGetCwd() { } } -function evalModule(source, printResult) { +function evalModule(source, print) { + const { log, error } = require('internal/console/global'); const { decorateErrorStack } = require('internal/util'); const asyncESM = require('internal/process/esm_loader'); - const { kStdout, kStderr, print } = require('internal/util/print'); asyncESM.loaderPromise.then(async (loader) => { const { result } = await loader.eval(source); - if (printResult) { print(kStdout, result); } + if (print) { + log(result); + } }) .catch((e) => { decorateErrorStack(e); - print(kStderr, e); + error(e); process.exit(1); }); } -function evalScript(name, body, breakFirstLine, printResult) { +function evalScript(name, body, breakFirstLine, print) { const CJSModule = require('internal/modules/cjs/loader'); const { kVmBreakFirstLineSymbol } = require('internal/util'); @@ -76,9 +78,9 @@ function evalScript(name, body, breakFirstLine, printResult) { [kVmBreakFirstLineSymbol]: ${!!breakFirstLine} });\n`; const result = module._compile(script, `${name}-wrapper`); - if (printResult) { - const { kStdout, print } = require('internal/util/print'); - print(kStdout, result); + if (print) { + const { log } = require('internal/console/global'); + log(result); } if (origModule !== undefined) diff --git a/lib/internal/util/print.js b/lib/internal/util/print.js deleted file mode 100644 index 4c9327502e..0000000000 --- a/lib/internal/util/print.js +++ /dev/null @@ -1,67 +0,0 @@ -'use strict'; - -// This implements a light-weight printer that writes to stdout/stderr -// directly to avoid the overhead in the console abstraction. - -const { formatWithOptions } = require('internal/util/inspect'); -const { writeString } = internalBinding('fs'); -const { handleErrorFromBinding } = require('internal/fs/utils'); -const { guessHandleType } = internalBinding('util'); -const { log } = require('internal/console/global'); - -const kStdout = 1; -const kStderr = 2; -const handleType = [undefined, undefined, undefined]; -function getFdType(fd) { - if (handleType[fd] === undefined) { - handleType[fd] = guessHandleType(fd); - } - return handleType[fd]; -} - -function formatAndWrite(fd, obj, ignoreErrors, colors = false) { - const str = `${formatWithOptions({ colors }, obj)}\n`; - const ctx = {}; - writeString(fd, str, null, undefined, undefined, ctx); - if (!ignoreErrors) { - handleErrorFromBinding(ctx); - } -} - -let colors; -function getColors() { - if (colors === undefined) { - colors = require('internal/tty').getColorDepth() > 2; - } - return colors; -} - -// TODO(joyeecheung): replace more internal process._rawDebug() -// and console.log() usage with this if possible. -function print(fd, obj, ignoreErrors = true) { - switch (getFdType(fd)) { - case 'TTY': - formatAndWrite(fd, obj, ignoreErrors, getColors()); - break; - case 'FILE': - formatAndWrite(fd, obj, ignoreErrors); - break; - case 'PIPE': - case 'TCP': - // Fallback to console.log to handle IPC. - if (process.channel && process.channel.fd === fd) { - log(obj); - } else { - formatAndWrite(fd, obj, ignoreErrors); - } - break; - default: - log(obj); - } -} - -module.exports = { - print, - kStderr, - kStdout -}; |