-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: explicitly initialize debuglog during bootstrap
This patch splits the implementation of util.debuglog into a separate file and explicitly initialize it during pre-execution since the initialization depends on environment variables. Also delays the call to `debuglog` in modules that are loaded during bootstrap to make sure we only access the environment variable during pre-execution. PR-URL: #26468 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
- Loading branch information
1 parent
b0afac2
commit 9ce08c8
Showing
10 changed files
with
108 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'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); | ||
console.error('%s %d: %s', set, pid, msg); | ||
}; | ||
} else { | ||
debugs[set] = function debug() {}; | ||
} | ||
} | ||
return debugs[set]; | ||
} | ||
|
||
module.exports = { | ||
debuglog, | ||
initializeDebugEnv | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters