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:
parent
76b9cf5424
commit
1a96abe849
20
lib/fs.js
20
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;
|
||||
|
@ -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,
|
||||
|
@ -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) => {
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user