process: move setup of process warnings into node.js

To clarify the side effects and conditions of the warning setup
during bootstrap.

PR-URL: https://github.com/nodejs/node/pull/25263
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
This commit is contained in:
Joyee Cheung 2018-12-29 11:41:16 +08:00
parent d28980c464
commit 0878b6172e
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
2 changed files with 88 additions and 81 deletions

View File

@ -107,7 +107,15 @@ function startup() {
process.exit = wrapped.exit;
}
NativeModule.require('internal/process/warning').setup();
const {
onWarning,
emitWarning
} = NativeModule.require('internal/process/warning');
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
process.on('warning', onWarning);
}
process.emitWarning = emitWarning;
const {
nextTick,
runNextTicks

View File

@ -3,8 +3,6 @@
const prefix = `(${process.release.name}:${process.pid}) `;
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
exports.setup = setupProcessWarnings;
// Lazily loaded
let fs;
let fd;
@ -51,9 +49,7 @@ function doEmitWarning(warning) {
return () => process.emit('warning', warning);
}
function setupProcessWarnings() {
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
process.on('warning', (warning) => {
function onWarning(warning) {
if (!(warning instanceof Error)) return;
const isDeprecation = warning.name === 'DeprecationWarning';
if (isDeprecation && process.noDeprecation) return;
@ -78,13 +74,12 @@ function setupProcessWarnings() {
return writeToFile(msg);
}
writeOut(msg);
});
}
// process.emitWarning(error)
// process.emitWarning(str[, type[, code]][, ctor])
// process.emitWarning(str[, options])
process.emitWarning = (warning, type, code, ctor, now) => {
function emitWarning(warning, type, code, ctor, now) {
let detail;
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
ctor = type.ctor;
@ -129,5 +124,9 @@ function setupProcessWarnings() {
}
if (now) process.emit('warning', warning);
else process.nextTick(doEmitWarning(warning));
};
}
module.exports = {
onWarning,
emitWarning
};