mirror of
https://github.com/nodejs/node.git
synced 2025-05-08 11:08:24 +00:00

`debuglog()` depends on `process.pid` and `process.env.NODE_DEBUG`, so it needs to be called lazily in top scopes of internal modules that may be loaded before these run time states are allowed to be accessed. This patch makes its implementation lazy by default, the process states are only accessed when the returned debug function is called for the first time. PR-URL: https://github.com/nodejs/node/pull/27281 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
71 lines
1.9 KiB
JavaScript
71 lines
1.9 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 debuglogImpl(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];
|
|
}
|
|
|
|
// debuglogImpl depends on process.pid and process.env.NODE_DEBUG,
|
|
// so it needs to be called lazily in top scopes of internal modules
|
|
// that may be loaded before these run time states are allowed to
|
|
// be accessed.
|
|
function debuglog(set) {
|
|
let debug;
|
|
return function(...args) {
|
|
if (!debug) {
|
|
// Only invokes debuglogImpl() when the debug function is
|
|
// called for the first time.
|
|
debug = debuglogImpl(set);
|
|
}
|
|
debug(...args);
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
debuglog,
|
|
initializeDebugEnv
|
|
};
|