Skip to content

Commit 9ce08c8

Browse files
joyeecheungtargos
authored andcommitted
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>
1 parent b0afac2 commit 9ce08c8

File tree

10 files changed

+108
-45
lines changed

10 files changed

+108
-45
lines changed

lib/_stream_readable.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ const EE = require('events');
2828
const Stream = require('stream');
2929
const { Buffer } = require('buffer');
3030
const util = require('util');
31-
const debug = util.debuglog('stream');
31+
32+
let debuglog;
33+
function debug(...args) {
34+
if (!debuglog) {
35+
debuglog = require('internal/util/debuglog').debuglog('stream');
36+
}
37+
debuglog(...args);
38+
}
39+
3240
const BufferList = require('internal/streams/buffer_list');
3341
const destroyImpl = require('internal/streams/destroy');
3442
const { getHighWaterMark } = require('internal/streams/state');

lib/internal/bootstrap/pre_execution.js

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ function prepareMainThreadExecution() {
3232
'DeprecationWarning', 'DEP0062', undefined, true);
3333
}
3434

35+
setupDebugEnv();
36+
3537
// Only main thread receives signals.
3638
setupSignalHandlers();
3739

@@ -105,6 +107,10 @@ function initializeReport() {
105107
});
106108
}
107109

110+
function setupDebugEnv() {
111+
require('internal/util/debuglog').initializeDebugEnv(process.env.NODE_DEBUG);
112+
}
113+
108114
function setupSignalHandlers() {
109115
const {
110116
createSignalHandlers
@@ -306,6 +312,7 @@ function loadPreloadModules() {
306312
module.exports = {
307313
setupCoverageHooks,
308314
setupWarningHandler,
315+
setupDebugEnv,
309316
prepareMainThreadExecution,
310317
initializeDeprecations,
311318
initializeESMLoader,

lib/internal/main/worker_thread.js

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
const {
77
setupCoverageHooks,
88
setupWarningHandler,
9+
setupDebugEnv,
910
initializeDeprecations,
1011
initializeESMLoader,
1112
initializeFrozenIntrinsics,
@@ -39,6 +40,9 @@ const {
3940
} = require('internal/process/execution');
4041

4142
const publicWorker = require('worker_threads');
43+
44+
setupDebugEnv();
45+
4246
const debug = require('util').debuglog('worker');
4347

4448
setupWarningHandler();

lib/internal/modules/cjs/loader.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,13 @@ Object.defineProperty(Module, 'wrapper', {
174174
}
175175
});
176176

177-
const debug = util.debuglog('module');
177+
let debuglog;
178+
function debug(...args) {
179+
if (!debuglog) {
180+
debuglog = require('internal/util/debuglog').debuglog('module');
181+
}
182+
debuglog(...args);
183+
}
178184

179185
Module._debug = util.deprecate(debug, 'Module._debug is deprecated.',
180186
'DEP0077');

lib/internal/util/debuglog.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const { format } = require('internal/util/inspect');
4+
5+
// `debugs` is deliberately initialized to undefined so any call to
6+
// debuglog() before initializeDebugEnv() is called will throw.
7+
let debugs;
8+
9+
let debugEnvRegex = /^$/;
10+
11+
// `debugEnv` is initial value of process.env.NODE_DEBUG
12+
function initializeDebugEnv(debugEnv) {
13+
debugs = {};
14+
if (debugEnv) {
15+
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
16+
.replace(/\*/g, '.*')
17+
.replace(/,/g, '$|^')
18+
.toUpperCase();
19+
debugEnvRegex = new RegExp(`^${debugEnv}$`, 'i');
20+
}
21+
}
22+
23+
// Emits warning when user sets
24+
// NODE_DEBUG=http or NODE_DEBUG=http2.
25+
function emitWarningIfNeeded(set) {
26+
if ('HTTP' === set || 'HTTP2' === set) {
27+
process.emitWarning('Setting the NODE_DEBUG environment variable ' +
28+
'to \'' + set.toLowerCase() + '\' can expose sensitive ' +
29+
'data (such as passwords, tokens and authentication headers) ' +
30+
'in the resulting log.');
31+
}
32+
}
33+
34+
function debuglog(set) {
35+
set = set.toUpperCase();
36+
if (!debugs[set]) {
37+
if (debugEnvRegex.test(set)) {
38+
const pid = process.pid;
39+
emitWarningIfNeeded(set);
40+
debugs[set] = function debug(...args) {
41+
const msg = format(...args);
42+
console.error('%s %d: %s', set, pid, msg);
43+
};
44+
} else {
45+
debugs[set] = function debug() {};
46+
}
47+
}
48+
return debugs[set];
49+
}
50+
51+
module.exports = {
52+
debuglog,
53+
initializeDebugEnv
54+
};

lib/internal/worker.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const EventEmitter = require('events');
44
const assert = require('internal/assert');
55
const path = require('path');
6-
const util = require('util');
6+
77
const {
88
ERR_WORKER_PATH,
99
ERR_WORKER_UNSERIALIZABLE_ERROR,
@@ -45,7 +45,13 @@ const kOnCouldNotSerializeErr = Symbol('kOnCouldNotSerializeErr');
4545
const kOnErrorMessage = Symbol('kOnErrorMessage');
4646
const kParentSideStdio = Symbol('kParentSideStdio');
4747

48-
const debug = util.debuglog('worker');
48+
let debuglog;
49+
function debug(...args) {
50+
if (!debuglog) {
51+
debuglog = require('internal/util/debuglog').debuglog('worker');
52+
}
53+
debuglog(...args);
54+
}
4955

5056
class Worker extends EventEmitter {
5157
constructor(filename, options = {}) {

lib/internal/worker/io.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ const { threadId } = internalBinding('worker');
1515
const { Readable, Writable } = require('stream');
1616
const EventEmitter = require('events');
1717
const util = require('util');
18-
const debug = util.debuglog('worker');
18+
19+
let debuglog;
20+
function debug(...args) {
21+
if (!debuglog) {
22+
debuglog = require('internal/util/debuglog').debuglog('worker');
23+
}
24+
debuglog(...args);
25+
}
1926

2027
const kIncrementsPortRef = Symbol('kIncrementsPortRef');
2128
const kName = Symbol('kName');

lib/timers.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ const {
4242
const internalUtil = require('internal/util');
4343
const util = require('util');
4444
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
45-
const debug = util.debuglog('timer');
45+
46+
let debuglog;
47+
function debug(...args) {
48+
if (!debuglog) {
49+
debuglog = require('internal/util/debuglog').debuglog('timer');
50+
}
51+
debuglog(...args);
52+
}
53+
4654
const {
4755
destroyHooksExist,
4856
// The needed emit*() functions.

lib/util.js

+1-39
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
formatWithOptions,
2828
inspect
2929
} = require('internal/util/inspect');
30+
const { debuglog } = require('internal/util/debuglog');
3031
const {
3132
ERR_FALSY_VALUE_REJECTION,
3233
ERR_INVALID_ARG_TYPE,
@@ -52,45 +53,6 @@ const objectToString = uncurryThis(Object.prototype.toString);
5253

5354
let internalDeepEqual;
5455

55-
const debugs = {};
56-
let debugEnvRegex = /^$/;
57-
if (process.env.NODE_DEBUG) {
58-
let debugEnv = process.env.NODE_DEBUG;
59-
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
60-
.replace(/\*/g, '.*')
61-
.replace(/,/g, '$|^')
62-
.toUpperCase();
63-
debugEnvRegex = new RegExp(`^${debugEnv}$`, 'i');
64-
}
65-
66-
// Emits warning when user sets
67-
// NODE_DEBUG=http or NODE_DEBUG=http2.
68-
function emitWarningIfNeeded(set) {
69-
if ('HTTP' === set || 'HTTP2' === set) {
70-
process.emitWarning('Setting the NODE_DEBUG environment variable ' +
71-
'to \'' + set.toLowerCase() + '\' can expose sensitive ' +
72-
'data (such as passwords, tokens and authentication headers) ' +
73-
'in the resulting log.');
74-
}
75-
}
76-
77-
function debuglog(set) {
78-
set = set.toUpperCase();
79-
if (!debugs[set]) {
80-
if (debugEnvRegex.test(set)) {
81-
const pid = process.pid;
82-
emitWarningIfNeeded(set);
83-
debugs[set] = function debug() {
84-
const msg = exports.format.apply(exports, arguments);
85-
console.error('%s %d: %s', set, pid, msg);
86-
};
87-
} else {
88-
debugs[set] = function debug() {};
89-
}
90-
}
91-
return debugs[set];
92-
}
93-
9456
function isBoolean(arg) {
9557
return typeof arg === 'boolean';
9658
}

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
'lib/internal/url.js',
188188
'lib/internal/util.js',
189189
'lib/internal/util/comparisons.js',
190+
'lib/internal/util/debuglog.js',
190191
'lib/internal/util/inspect.js',
191192
'lib/internal/util/inspector.js',
192193
'lib/internal/util/types.js',

0 commit comments

Comments
 (0)