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>
This commit is contained in:
Richard Lau 2019-05-22 17:54:34 +01:00 committed by Anna Henningsen
parent 76b9cf5424
commit 1a96abe849
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
6 changed files with 35 additions and 103 deletions

View File

@ -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;

View File

@ -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,

View File

@ -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) => {

View File

@ -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)

View File

@ -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
};

View File

@ -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',