Skip to content

Commit 2e4ceb5

Browse files
committed
util: access process states lazily in debuglog
`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: #27281 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]>
1 parent 49ee010 commit 2e4ceb5

File tree

9 files changed

+25
-51
lines changed

9 files changed

+25
-51
lines changed

lib/_stream_readable.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ const EE = require('events');
3030
const Stream = require('stream');
3131
const { Buffer } = require('buffer');
3232

33-
let debuglog;
34-
function debug(...args) {
35-
if (!debuglog) {
36-
debuglog = require('internal/util/debuglog').debuglog('stream');
37-
}
38-
debuglog(...args);
39-
}
40-
33+
const debug = require('internal/util/debuglog').debuglog('stream');
4134
const BufferList = require('internal/streams/buffer_list');
4235
const destroyImpl = require('internal/streams/destroy');
4336
const { getHighWaterMark } = require('internal/streams/state');

lib/internal/main/worker_thread.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ const {
4444
} = require('internal/process/execution');
4545

4646
const publicWorker = require('worker_threads');
47+
const debug = require('internal/util/debuglog').debuglog('worker');
4748

4849
const assert = require('internal/assert');
4950

5051
patchProcessObject();
5152
setupInspectorHooks();
5253
setupDebugEnv();
5354

54-
const debug = require('internal/util/debuglog').debuglog('worker');
55-
5655
setupWarningHandler();
5756

5857
// Since worker threads cannot switch cwd, we do not need to

lib/internal/modules/cjs/loader.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,7 @@ Object.defineProperty(Module, 'wrapper', {
166166
}
167167
});
168168

169-
let debuglog;
170-
function debug(...args) {
171-
if (!debuglog) {
172-
debuglog = require('internal/util/debuglog').debuglog('module');
173-
}
174-
debuglog(...args);
175-
}
176-
169+
const debug = require('internal/util/debuglog').debuglog('module');
177170
Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
178171

179172
// Given a module name, and a list of paths to test, returns the first

lib/internal/stream_base_commons.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
3131
const kHandle = Symbol('kHandle');
3232
const kSession = Symbol('kSession');
3333

34-
const debug = require('util').debuglog('stream');
34+
const debug = require('internal/util/debuglog').debuglog('stream');
3535

3636
function handleWriteReq(req, data, encoding) {
3737
const { handle } = req;

lib/internal/timers.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,7 @@ const L = require('internal/linkedlist');
107107
const PriorityQueue = require('internal/priority_queue');
108108

109109
const { inspect } = require('internal/util/inspect');
110-
let debuglog;
111-
function debug(...args) {
112-
if (!debuglog) {
113-
debuglog = require('internal/util/debuglog').debuglog('timer');
114-
}
115-
debuglog(...args);
116-
}
110+
const debug = require('internal/util/debuglog').debuglog('timer');
117111

118112
// *Must* match Environment::ImmediateInfo::Fields in src/env.h.
119113
const kCount = 0;

lib/internal/util/debuglog.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function emitWarningIfNeeded(set) {
3131
}
3232
}
3333

34-
function debuglog(set) {
34+
function debuglogImpl(set) {
3535
set = set.toUpperCase();
3636
if (!debugs[set]) {
3737
if (debugEnvRegex.test(set)) {
@@ -48,6 +48,22 @@ function debuglog(set) {
4848
return debugs[set];
4949
}
5050

51+
// debuglogImpl depends on process.pid and process.env.NODE_DEBUG,
52+
// so it needs to be called lazily in top scopes of internal modules
53+
// that may be loaded before these run time states are allowed to
54+
// be accessed.
55+
function debuglog(set) {
56+
let debug;
57+
return function(...args) {
58+
if (!debug) {
59+
// Only invokes debuglogImpl() when the debug function is
60+
// called for the first time.
61+
debug = debuglogImpl(set);
62+
}
63+
debug(...args);
64+
};
65+
}
66+
5167
module.exports = {
5268
debuglog,
5369
initializeDebugEnv

lib/internal/worker.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,7 @@ const kOnErrorMessage = Symbol('kOnErrorMessage');
4848
const kParentSideStdio = Symbol('kParentSideStdio');
4949

5050
const SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');
51-
52-
let debuglog;
53-
function debug(...args) {
54-
if (!debuglog) {
55-
debuglog = require('internal/util/debuglog').debuglog('worker');
56-
}
57-
debuglog(...args);
58-
}
51+
const debug = require('internal/util/debuglog').debuglog('worker');
5952

6053
class Worker extends EventEmitter {
6154
constructor(filename, options = {}) {

lib/internal/worker/io.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@ const {
2121
const { Readable, Writable } = require('stream');
2222
const EventEmitter = require('events');
2323
const { inspect } = require('internal/util/inspect');
24-
25-
let debuglog;
26-
function debug(...args) {
27-
if (!debuglog) {
28-
debuglog = require('internal/util/debuglog').debuglog('worker');
29-
}
30-
debuglog(...args);
31-
}
24+
const debug = require('internal/util/debuglog').debuglog('worker');
3225

3326
const kIncrementsPortRef = Symbol('kIncrementsPortRef');
3427
const kName = Symbol('kName');

lib/timers.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,7 @@ const {
5050
deprecate
5151
} = require('internal/util');
5252
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
53-
54-
let debuglog;
55-
function debug(...args) {
56-
if (!debuglog) {
57-
debuglog = require('internal/util/debuglog').debuglog('timer');
58-
}
59-
debuglog(...args);
60-
}
53+
const debug = require('internal/util/debuglog').debuglog('timer');
6154

6255
const {
6356
destroyHooksExist,

0 commit comments

Comments
 (0)