Skip to content

Commit

Permalink
Revert "lib: print to stdout/stderr directly instead of using console"
Browse files Browse the repository at this point in the history
This reverts commit 2b24ffa.

Fixes: #27819

PR-URL: #27823
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
richardlau authored and targos committed May 28, 2019
1 parent 34ef9e4 commit 9b90385
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 103 deletions.
20 changes: 18 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ const {
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
warnOnNonPortableTemplate,
handleErrorFromBinding
warnOnNonPortableTemplate
} = require('internal/fs/utils');
const {
CHAR_FORWARD_SLASH,
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 1 addition & 21 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const {
ERR_INVALID_OPT_VALUE_ENCODING,
ERR_OUT_OF_RANGE
},
hideStackFrames,
uvException
hideStackFrames
} = require('internal/errors');
const {
isUint8Array,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions lib/internal/main/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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) => {
Expand Down
18 changes: 10 additions & 8 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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)
Expand Down
67 changes: 0 additions & 67 deletions lib/internal/util/print.js

This file was deleted.

1 change: 0 additions & 1 deletion node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 9b90385

Please sign in to comment.