mirror of
https://github.com/nodejs/node.git
synced 2025-08-18 07:08:50 +02:00

In addition, use process.stderr instead of console.error when there is no need to swallow the error. PR-URL: https://github.com/nodejs/node/pull/27112 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const { format } = require('internal/util/inspect');
|
|
|
|
// `debugs` is deliberately initialized to undefined so any call to
|
|
// debuglog() before initializeDebugEnv() is called will throw.
|
|
let debugs;
|
|
|
|
let debugEnvRegex = /^$/;
|
|
|
|
// `debugEnv` is initial value of process.env.NODE_DEBUG
|
|
function initializeDebugEnv(debugEnv) {
|
|
debugs = {};
|
|
if (debugEnv) {
|
|
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
|
|
.replace(/\*/g, '.*')
|
|
.replace(/,/g, '$|^')
|
|
.toUpperCase();
|
|
debugEnvRegex = new RegExp(`^${debugEnv}$`, 'i');
|
|
}
|
|
}
|
|
|
|
// Emits warning when user sets
|
|
// NODE_DEBUG=http or NODE_DEBUG=http2.
|
|
function emitWarningIfNeeded(set) {
|
|
if ('HTTP' === set || 'HTTP2' === set) {
|
|
process.emitWarning('Setting the NODE_DEBUG environment variable ' +
|
|
'to \'' + set.toLowerCase() + '\' can expose sensitive ' +
|
|
'data (such as passwords, tokens and authentication headers) ' +
|
|
'in the resulting log.');
|
|
}
|
|
}
|
|
|
|
function debuglog(set) {
|
|
set = set.toUpperCase();
|
|
if (!debugs[set]) {
|
|
if (debugEnvRegex.test(set)) {
|
|
const pid = process.pid;
|
|
emitWarningIfNeeded(set);
|
|
debugs[set] = function debug(...args) {
|
|
const msg = format(...args);
|
|
process.stderr.write(format('%s %d: %s\n', set, pid, msg));
|
|
};
|
|
} else {
|
|
debugs[set] = function debug() {};
|
|
}
|
|
}
|
|
return debugs[set];
|
|
}
|
|
|
|
module.exports = {
|
|
debuglog,
|
|
initializeDebugEnv
|
|
};
|