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‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ const EE = require('events');
3030
constStream=require('stream');
3131
const{ Buffer }=require('buffer');
3232

33-
letdebuglog;
34-
functiondebug(...args){
35-
if(!debuglog){
36-
debuglog=require('internal/util/debuglog').debuglog('stream');
37-
}
38-
debuglog(...args);
39-
}
40-
33+
constdebug=require('internal/util/debuglog').debuglog('stream');
4134
constBufferList=require('internal/streams/buffer_list');
4235
constdestroyImpl=require('internal/streams/destroy');
4336
const{ getHighWaterMark }=require('internal/streams/state');

‎lib/internal/main/worker_thread.js‎

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

4646
constpublicWorker=require('worker_threads');
47+
constdebug=require('internal/util/debuglog').debuglog('worker');
4748

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

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

54-
constdebug=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‎

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

169-
letdebuglog;
170-
functiondebug(...args){
171-
if(!debuglog){
172-
debuglog=require('internal/util/debuglog').debuglog('module');
173-
}
174-
debuglog(...args);
175-
}
176-
169+
constdebug=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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
3131
constkHandle=Symbol('kHandle');
3232
constkSession=Symbol('kSession');
3333

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

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

‎lib/internal/timers.js‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,7 @@ const L = require('internal/linkedlist');
107107
constPriorityQueue=require('internal/priority_queue');
108108

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

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

‎lib/internal/util/debuglog.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function emitWarningIfNeeded(set){
3131
}
3232
}
3333

34-
functiondebuglog(set){
34+
functiondebuglogImpl(set){
3535
set=set.toUpperCase();
3636
if(!debugs[set]){
3737
if(debugEnvRegex.test(set)){
@@ -48,6 +48,22 @@ function debuglog(set){
4848
returndebugs[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+
functiondebuglog(set){
56+
letdebug;
57+
returnfunction(...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‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,7 @@ const kOnErrorMessage = Symbol('kOnErrorMessage');
4848
constkParentSideStdio=Symbol('kParentSideStdio');
4949

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

6053
classWorkerextendsEventEmitter{
6154
constructor(filename,options={}){

‎lib/internal/worker/io.js‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@ const{
2121
const{ Readable, Writable }=require('stream');
2222
constEventEmitter=require('events');
2323
const{ inspect }=require('internal/util/inspect');
24-
25-
letdebuglog;
26-
functiondebug(...args){
27-
if(!debuglog){
28-
debuglog=require('internal/util/debuglog').debuglog('worker');
29-
}
30-
debuglog(...args);
31-
}
24+
constdebug=require('internal/util/debuglog').debuglog('worker');
3225

3326
constkIncrementsPortRef=Symbol('kIncrementsPortRef');
3427
constkName=Symbol('kName');

‎lib/timers.js‎

Lines changed: 1 addition & 8 deletions
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-
letdebuglog;
55-
functiondebug(...args){
56-
if(!debuglog){
57-
debuglog=require('internal/util/debuglog').debuglog('timer');
58-
}
59-
debuglog(...args);
60-
}
53+
constdebug=require('internal/util/debuglog').debuglog('timer');
6154

6255
const{
6356
destroyHooksExist,

0 commit comments

Comments
(0)