Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: move setup of process warnings into node.js #25263

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ function startup() {
wrapped.hasUncaughtExceptionCaptureCallback;
}

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
Expand Down
15 changes: 7 additions & 8 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -129,5 +124,9 @@ function setupProcessWarnings() {
}
if (now) process.emit('warning', warning);
else process.nextTick(doEmitWarning(warning));
};
}

module.exports = {
onWarning,
emitWarning
};