Skip to content

Commit a38e9c4

Browse files
committed
lib: require globals instead of using the global proxy
In addition, use process.stderr instead of console.error when there is no need to swallow the error. PR-URL: #27112 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 7938238 commit a38e9c4

17 files changed

+42
-24
lines changed

lib/.eslintrc.yaml

-10
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,3 @@ globals:
5858
module: false
5959
internalBinding: false
6060
primordials: false
61-
# Globals
62-
# TODO(joyeecheung): if possible, get these in native modules
63-
# through `require` instead of grabbing them from the global proxy.
64-
clearTimeout: false
65-
setTimeout: false
66-
clearInterval: false
67-
setInterval: false
68-
setImmediate: false
69-
clearImmediate: false
70-
console: false

lib/_http_common.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const { Math } = primordials;
25+
const { setImmediate } = require('timers');
2526

2627
const { getOptionValue } = require('internal/options');
2728

lib/_tls_wrap.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030

3131
assertCrypto();
3232

33+
const { setImmediate } = require('timers');
3334
const assert = require('internal/assert');
3435
const crypto = require('crypto');
3536
const net = require('net');

lib/child_process.js

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const {
4040
ERR_INVALID_OPT_VALUE,
4141
ERR_OUT_OF_RANGE
4242
} = require('internal/errors').codes;
43+
const { clearTimeout, setTimeout } = require('timers');
4344
const { validateString, isInt32 } = require('internal/validators');
4445
const child_process = require('internal/child_process');
4546
const {

lib/internal/freeze_intrinsics.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@
1919
// https://github.com/google/caja/blob/master/src/com/google/caja/ses/repairES5.js
2020
// https://github.com/tc39/proposal-frozen-realms/blob/91ac390e3451da92b5c27e354b39e52b7636a437/shim/src/deep-freeze.js
2121

22-
/* global WebAssembly, SharedArrayBuffer, console */
22+
/* global WebAssembly, SharedArrayBuffer */
2323
/* eslint-disable no-restricted-globals */
2424
'use strict';
2525

2626
module.exports = function() {
27+
const {
28+
clearImmediate,
29+
clearInterval,
30+
clearTimeout,
31+
setImmediate,
32+
setInterval,
33+
setTimeout
34+
} = require('timers');
35+
const console = require('internal/console/global');
2736

2837
const intrinsics = [
2938
// Anonymous Intrinsics
@@ -124,16 +133,12 @@ module.exports = function() {
124133
clearImmediate,
125134
clearInterval,
126135
clearTimeout,
127-
decodeURI,
128-
decodeURIComponent,
129-
encodeURI,
130-
encodeURIComponent,
131136
setImmediate,
132137
setInterval,
133138
setTimeout,
139+
console,
134140

135141
// Other APIs
136-
console,
137142
BigInt,
138143
Atomics,
139144
WebAssembly,

lib/internal/http2/core.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const net = require('net');
2020
const { Duplex } = require('stream');
2121
const tls = require('tls');
2222
const { URL } = require('url');
23+
const { setImmediate } = require('timers');
2324

2425
const { kIncomingMessage } = require('_http_common');
2526
const { kServerResponse } = require('_http_server');

lib/internal/js_stream_socket.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const { setImmediate } = require('timers');
34
const assert = require('internal/assert');
45
const { Socket } = require('net');
56
const { JSStream } = internalBinding('js_stream');

lib/internal/main/repl.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ prepareMainThreadExecution();
1515

1616
// --entry-type flag not supported in REPL
1717
if (require('internal/options').getOptionValue('--entry-type')) {
18-
console.error('Cannot specify --entry-type for REPL');
18+
// If we can't write to stderr, we'd like to make this a noop,
19+
// so use console.error.
20+
const { error } = require('internal/console/global');
21+
error('Cannot specify --entry-type for REPL');
1922
process.exit(1);
2023
}
2124

lib/internal/process/execution.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ function tryGetCwd() {
3636
}
3737

3838
function evalModule(source) {
39+
const { log, error } = require('internal/console/global');
3940
const { decorateErrorStack } = require('internal/util');
4041
const asyncESM = require('internal/process/esm_loader');
4142
asyncESM.loaderPromise.then(async (loader) => {
4243
const { result } = await loader.eval(source);
4344
if (require('internal/options').getOptionValue('--print')) {
44-
console.log(result);
45+
log(result);
4546
}
4647
})
4748
.catch((e) => {
4849
decorateErrorStack(e);
49-
console.error(e);
50+
error(e);
5051
process.exit(1);
5152
});
5253
// Handle any nextTicks added in the first tick of the program.
@@ -79,7 +80,8 @@ function evalScript(name, body, breakFirstLine) {
7980
});\n`;
8081
const result = module._compile(script, `${name}-wrapper`);
8182
if (require('internal/options').getOptionValue('--print')) {
82-
console.log(result);
83+
const { log } = require('internal/console/global');
84+
log(result);
8385
}
8486
// Handle any nextTicks added in the first tick of the program.
8587
process._tickCallback();

lib/internal/process/warning.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ function lazyOption() {
1717
return warningFile;
1818
}
1919

20+
// If we can't write to stderr, we'd like to make this a noop,
21+
// so use console.error.
22+
let error;
2023
function writeOut(message) {
21-
if (console && typeof console.error === 'function')
22-
return console.error(message);
23-
process._rawDebug(message);
24+
if (!error) {
25+
error = require('internal/console/global').error;
26+
}
27+
error(message);
2428
}
2529

2630
function writeToFile(message) {

lib/internal/repl/history.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path');
55
const fs = require('fs');
66
const os = require('os');
77
const debug = require('internal/util/debuglog').debuglog('repl');
8+
const { clearTimeout, setTimeout } = require('timers');
89

910
// XXX(chrisdickinson): The 15ms debounce value is somewhat arbitrary.
1011
// The debounce is to guard against code pasted into the REPL.

lib/internal/stream_base_commons.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const {
2323
setUnrefTimeout,
2424
getTimerDuration
2525
} = require('internal/timers');
26+
const { clearTimeout } = require('timers');
2627

2728
const kMaybeDestroy = Symbol('kMaybeDestroy');
2829
const kUpdateTimer = Symbol('kUpdateTimer');

lib/internal/util/debuglog.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function debuglog(set) {
3939
emitWarningIfNeeded(set);
4040
debugs[set] = function debug(...args) {
4141
const msg = format(...args);
42-
console.error('%s %d: %s', set, pid, msg);
42+
process.stderr.write(format('%s %d: %s\n', set, pid, msg));
4343
};
4444
} else {
4545
debugs[set] = function debug() {};

lib/net.js

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ const {
9797
let cluster;
9898
let dns;
9999

100+
const { clearTimeout } = require('timers');
100101
const { kTimeout } = require('internal/timers');
101102

102103
const DEFAULT_IPV4_ADDR = '0.0.0.0';

lib/perf_hooks.js

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const {
4747
ERR_INVALID_PERFORMANCE_MARK
4848
} = require('internal/errors').codes;
4949

50+
const { setImmediate } = require('timers');
5051
const kHandle = Symbol('handle');
5152
const kMap = Symbol('map');
5253
const kCallback = Symbol('callback');

lib/readline.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const {
4444
stripVTControlCharacters
4545
} = require('internal/readline');
4646

47+
const { clearTimeout, setTimeout } = require('timers');
4748
const {
4849
kEscape,
4950
kClearToBeginning,

lib/util.js

+4
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ function timestamp() {
113113
return [d.getDate(), months[d.getMonth()], time].join(' ');
114114
}
115115

116+
let console;
116117
// Log is just a thin wrapper to console.log that prepends a timestamp
117118
function log(...args) {
119+
if (!console) {
120+
console = require('internal/console/global');
121+
}
118122
console.log('%s - %s', timestamp(), format(...args));
119123
}
120124

0 commit comments

Comments
 (0)