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,
|
validateOffsetLengthRead,
|
||||||
validateOffsetLengthWrite,
|
validateOffsetLengthWrite,
|
||||||
validatePath,
|
validatePath,
|
||||||
warnOnNonPortableTemplate,
|
warnOnNonPortableTemplate
|
||||||
handleErrorFromBinding
|
|
||||||
} = require('internal/fs/utils');
|
} = require('internal/fs/utils');
|
||||||
const {
|
const {
|
||||||
CHAR_FORWARD_SLASH,
|
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) {
|
function maybeCallback(cb) {
|
||||||
if (typeof cb === 'function')
|
if (typeof cb === 'function')
|
||||||
return cb;
|
return cb;
|
||||||
|
@ -12,8 +12,7 @@ const {
|
|||||||
ERR_INVALID_OPT_VALUE_ENCODING,
|
ERR_INVALID_OPT_VALUE_ENCODING,
|
||||||
ERR_OUT_OF_RANGE
|
ERR_OUT_OF_RANGE
|
||||||
},
|
},
|
||||||
hideStackFrames,
|
hideStackFrames
|
||||||
uvException
|
|
||||||
} = require('internal/errors');
|
} = require('internal/errors');
|
||||||
const {
|
const {
|
||||||
isUint8Array,
|
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 = {
|
module.exports = {
|
||||||
handleErrorFromBinding,
|
|
||||||
assertEncoding,
|
assertEncoding,
|
||||||
copyObject,
|
copyObject,
|
||||||
Dirent,
|
Dirent,
|
||||||
|
@ -11,7 +11,7 @@ const {
|
|||||||
evalScript
|
evalScript
|
||||||
} = require('internal/process/execution');
|
} = require('internal/process/execution');
|
||||||
|
|
||||||
const { print, kStderr, kStdout } = require('internal/util/print');
|
const console = require('internal/console/global');
|
||||||
|
|
||||||
const { getOptionValue } = require('internal/options');
|
const { getOptionValue } = require('internal/options');
|
||||||
|
|
||||||
@ -21,11 +21,13 @@ markBootstrapComplete();
|
|||||||
|
|
||||||
// --input-type flag not supported in REPL
|
// --input-type flag not supported in REPL
|
||||||
if (getOptionValue('--input-type')) {
|
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);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
print(kStdout, `Welcome to Node.js ${process.version}.\n` +
|
console.log(`Welcome to Node.js ${process.version}.\n` +
|
||||||
'Type ".help" for more information.');
|
'Type ".help" for more information.');
|
||||||
|
|
||||||
const cliRepl = require('internal/repl');
|
const cliRepl = require('internal/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 { decorateErrorStack } = require('internal/util');
|
||||||
const asyncESM = require('internal/process/esm_loader');
|
const asyncESM = require('internal/process/esm_loader');
|
||||||
const { kStdout, kStderr, print } = require('internal/util/print');
|
|
||||||
asyncESM.loaderPromise.then(async (loader) => {
|
asyncESM.loaderPromise.then(async (loader) => {
|
||||||
const { result } = await loader.eval(source);
|
const { result } = await loader.eval(source);
|
||||||
if (printResult) { print(kStdout, result); }
|
if (print) {
|
||||||
|
log(result);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
decorateErrorStack(e);
|
decorateErrorStack(e);
|
||||||
print(kStderr, e);
|
error(e);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function evalScript(name, body, breakFirstLine, printResult) {
|
function evalScript(name, body, breakFirstLine, print) {
|
||||||
const CJSModule = require('internal/modules/cjs/loader');
|
const CJSModule = require('internal/modules/cjs/loader');
|
||||||
const { kVmBreakFirstLineSymbol } = require('internal/util');
|
const { kVmBreakFirstLineSymbol } = require('internal/util');
|
||||||
|
|
||||||
@ -76,9 +78,9 @@ function evalScript(name, body, breakFirstLine, printResult) {
|
|||||||
[kVmBreakFirstLineSymbol]: ${!!breakFirstLine}
|
[kVmBreakFirstLineSymbol]: ${!!breakFirstLine}
|
||||||
});\n`;
|
});\n`;
|
||||||
const result = module._compile(script, `${name}-wrapper`);
|
const result = module._compile(script, `${name}-wrapper`);
|
||||||
if (printResult) {
|
if (print) {
|
||||||
const { kStdout, print } = require('internal/util/print');
|
const { log } = require('internal/console/global');
|
||||||
print(kStdout, result);
|
log(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (origModule !== undefined)
|
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
|
|
||||||
};
|
|
1
node.gyp
1
node.gyp
@ -184,7 +184,6 @@
|
|||||||
'lib/internal/url.js',
|
'lib/internal/url.js',
|
||||||
'lib/internal/util.js',
|
'lib/internal/util.js',
|
||||||
'lib/internal/util/comparisons.js',
|
'lib/internal/util/comparisons.js',
|
||||||
'lib/internal/util/print.js',
|
|
||||||
'lib/internal/util/debuglog.js',
|
'lib/internal/util/debuglog.js',
|
||||||
'lib/internal/util/inspect.js',
|
'lib/internal/util/inspect.js',
|
||||||
'lib/internal/util/inspector.js',
|
'lib/internal/util/inspector.js',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user