From 7d192a371484e579322f67687daabb61f8df77bb Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Wed, 22 May 2019 17:54:34 +0100 Subject: [PATCH] Revert "lib: print to stdout/stderr directly instead of using console" This reverts commit 2b24ffae2240163a74ae11e49ee198e98abb07dc. Fixes: https://github.com/nodejs/node/issues/27819 --- lib/fs.js | 20 ++++++++- lib/internal/fs/utils.js | 22 +--------- lib/internal/main/repl.js | 10 +++-- lib/internal/process/execution.js | 18 +++++---- lib/internal/util/print.js | 67 ------------------------------- node.gyp | 1 - 6 files changed, 35 insertions(+), 103 deletions(-) delete mode 100644 lib/internal/util/print.js diff --git a/lib/fs.js b/lib/fs.js index 7a31e26ccb1401..89f234b25d1888 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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 4cb06690bf558f..14abad81ec4e54 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 b38102a15482fd..93b932f0bdd15f 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 227c1c2149cef5..2b7fc41ccddf01 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 4c9327502ebad2..00000000000000 --- 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 -}; diff --git a/node.gyp b/node.gyp index df70d26f35f4e7..091fe0470c4802 100644 --- a/node.gyp +++ b/node.gyp @@ -184,7 +184,6 @@ 'lib/internal/url.js', 'lib/internal/util.js', 'lib/internal/util/comparisons.js', - 'lib/internal/util/print.js', 'lib/internal/util/debuglog.js', 'lib/internal/util/inspect.js', 'lib/internal/util/inspector.js',