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: https://github.com/nodejs/node/pull/27112 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
7938238b31
commit
a38e9c438a
@ -58,13 +58,3 @@ globals:
|
|||||||
module: false
|
module: false
|
||||||
internalBinding: false
|
internalBinding: false
|
||||||
primordials: false
|
primordials: false
|
||||||
# Globals
|
|
||||||
# TODO(joyeecheung): if possible, get these in native modules
|
|
||||||
# through `require` instead of grabbing them from the global proxy.
|
|
||||||
clearTimeout: false
|
|
||||||
setTimeout: false
|
|
||||||
clearInterval: false
|
|
||||||
setInterval: false
|
|
||||||
setImmediate: false
|
|
||||||
clearImmediate: false
|
|
||||||
console: false
|
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { Math } = primordials;
|
const { Math } = primordials;
|
||||||
|
const { setImmediate } = require('timers');
|
||||||
|
|
||||||
const { getOptionValue } = require('internal/options');
|
const { getOptionValue } = require('internal/options');
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ const {
|
|||||||
|
|
||||||
assertCrypto();
|
assertCrypto();
|
||||||
|
|
||||||
|
const { setImmediate } = require('timers');
|
||||||
const assert = require('internal/assert');
|
const assert = require('internal/assert');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
|
@ -40,6 +40,7 @@ const {
|
|||||||
ERR_INVALID_OPT_VALUE,
|
ERR_INVALID_OPT_VALUE,
|
||||||
ERR_OUT_OF_RANGE
|
ERR_OUT_OF_RANGE
|
||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
|
const { clearTimeout, setTimeout } = require('timers');
|
||||||
const { validateString, isInt32 } = require('internal/validators');
|
const { validateString, isInt32 } = require('internal/validators');
|
||||||
const child_process = require('internal/child_process');
|
const child_process = require('internal/child_process');
|
||||||
const {
|
const {
|
||||||
|
@ -19,11 +19,20 @@
|
|||||||
// https://github.com/google/caja/blob/master/src/com/google/caja/ses/repairES5.js
|
// https://github.com/google/caja/blob/master/src/com/google/caja/ses/repairES5.js
|
||||||
// https://github.com/tc39/proposal-frozen-realms/blob/91ac390e3451da92b5c27e354b39e52b7636a437/shim/src/deep-freeze.js
|
// https://github.com/tc39/proposal-frozen-realms/blob/91ac390e3451da92b5c27e354b39e52b7636a437/shim/src/deep-freeze.js
|
||||||
|
|
||||||
/* global WebAssembly, SharedArrayBuffer, console */
|
/* global WebAssembly, SharedArrayBuffer */
|
||||||
/* eslint-disable no-restricted-globals */
|
/* eslint-disable no-restricted-globals */
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = function() {
|
module.exports = function() {
|
||||||
|
const {
|
||||||
|
clearImmediate,
|
||||||
|
clearInterval,
|
||||||
|
clearTimeout,
|
||||||
|
setImmediate,
|
||||||
|
setInterval,
|
||||||
|
setTimeout
|
||||||
|
} = require('timers');
|
||||||
|
const console = require('internal/console/global');
|
||||||
|
|
||||||
const intrinsics = [
|
const intrinsics = [
|
||||||
// Anonymous Intrinsics
|
// Anonymous Intrinsics
|
||||||
@ -124,16 +133,12 @@ module.exports = function() {
|
|||||||
clearImmediate,
|
clearImmediate,
|
||||||
clearInterval,
|
clearInterval,
|
||||||
clearTimeout,
|
clearTimeout,
|
||||||
decodeURI,
|
|
||||||
decodeURIComponent,
|
|
||||||
encodeURI,
|
|
||||||
encodeURIComponent,
|
|
||||||
setImmediate,
|
setImmediate,
|
||||||
setInterval,
|
setInterval,
|
||||||
setTimeout,
|
setTimeout,
|
||||||
|
console,
|
||||||
|
|
||||||
// Other APIs
|
// Other APIs
|
||||||
console,
|
|
||||||
BigInt,
|
BigInt,
|
||||||
Atomics,
|
Atomics,
|
||||||
WebAssembly,
|
WebAssembly,
|
||||||
|
@ -20,6 +20,7 @@ const net = require('net');
|
|||||||
const { Duplex } = require('stream');
|
const { Duplex } = require('stream');
|
||||||
const tls = require('tls');
|
const tls = require('tls');
|
||||||
const { URL } = require('url');
|
const { URL } = require('url');
|
||||||
|
const { setImmediate } = require('timers');
|
||||||
|
|
||||||
const { kIncomingMessage } = require('_http_common');
|
const { kIncomingMessage } = require('_http_common');
|
||||||
const { kServerResponse } = require('_http_server');
|
const { kServerResponse } = require('_http_server');
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { setImmediate } = require('timers');
|
||||||
const assert = require('internal/assert');
|
const assert = require('internal/assert');
|
||||||
const { Socket } = require('net');
|
const { Socket } = require('net');
|
||||||
const { JSStream } = internalBinding('js_stream');
|
const { JSStream } = internalBinding('js_stream');
|
||||||
|
@ -15,7 +15,10 @@ prepareMainThreadExecution();
|
|||||||
|
|
||||||
// --entry-type flag not supported in REPL
|
// --entry-type flag not supported in REPL
|
||||||
if (require('internal/options').getOptionValue('--entry-type')) {
|
if (require('internal/options').getOptionValue('--entry-type')) {
|
||||||
console.error('Cannot specify --entry-type for REPL');
|
// If we can't write to stderr, we'd like to make this a noop,
|
||||||
|
// so use console.error.
|
||||||
|
const { error } = require('internal/console/global');
|
||||||
|
error('Cannot specify --entry-type for REPL');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,17 +36,18 @@ function tryGetCwd() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function evalModule(source) {
|
function evalModule(source) {
|
||||||
|
const { log, error } = require('internal/console/global');
|
||||||
const { decorateErrorStack } = require('internal/util');
|
const { decorateErrorStack } = require('internal/util');
|
||||||
const asyncESM = require('internal/process/esm_loader');
|
const asyncESM = require('internal/process/esm_loader');
|
||||||
asyncESM.loaderPromise.then(async (loader) => {
|
asyncESM.loaderPromise.then(async (loader) => {
|
||||||
const { result } = await loader.eval(source);
|
const { result } = await loader.eval(source);
|
||||||
if (require('internal/options').getOptionValue('--print')) {
|
if (require('internal/options').getOptionValue('--print')) {
|
||||||
console.log(result);
|
log(result);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
decorateErrorStack(e);
|
decorateErrorStack(e);
|
||||||
console.error(e);
|
error(e);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
// Handle any nextTicks added in the first tick of the program.
|
// Handle any nextTicks added in the first tick of the program.
|
||||||
@ -79,7 +80,8 @@ function evalScript(name, body, breakFirstLine) {
|
|||||||
});\n`;
|
});\n`;
|
||||||
const result = module._compile(script, `${name}-wrapper`);
|
const result = module._compile(script, `${name}-wrapper`);
|
||||||
if (require('internal/options').getOptionValue('--print')) {
|
if (require('internal/options').getOptionValue('--print')) {
|
||||||
console.log(result);
|
const { log } = require('internal/console/global');
|
||||||
|
log(result);
|
||||||
}
|
}
|
||||||
// Handle any nextTicks added in the first tick of the program.
|
// Handle any nextTicks added in the first tick of the program.
|
||||||
process._tickCallback();
|
process._tickCallback();
|
||||||
|
@ -17,10 +17,14 @@ function lazyOption() {
|
|||||||
return warningFile;
|
return warningFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we can't write to stderr, we'd like to make this a noop,
|
||||||
|
// so use console.error.
|
||||||
|
let error;
|
||||||
function writeOut(message) {
|
function writeOut(message) {
|
||||||
if (console && typeof console.error === 'function')
|
if (!error) {
|
||||||
return console.error(message);
|
error = require('internal/console/global').error;
|
||||||
process._rawDebug(message);
|
}
|
||||||
|
error(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeToFile(message) {
|
function writeToFile(message) {
|
||||||
|
@ -5,6 +5,7 @@ const path = require('path');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const debug = require('internal/util/debuglog').debuglog('repl');
|
const debug = require('internal/util/debuglog').debuglog('repl');
|
||||||
|
const { clearTimeout, setTimeout } = require('timers');
|
||||||
|
|
||||||
// XXX(chrisdickinson): The 15ms debounce value is somewhat arbitrary.
|
// XXX(chrisdickinson): The 15ms debounce value is somewhat arbitrary.
|
||||||
// The debounce is to guard against code pasted into the REPL.
|
// The debounce is to guard against code pasted into the REPL.
|
||||||
|
@ -23,6 +23,7 @@ const {
|
|||||||
setUnrefTimeout,
|
setUnrefTimeout,
|
||||||
getTimerDuration
|
getTimerDuration
|
||||||
} = require('internal/timers');
|
} = require('internal/timers');
|
||||||
|
const { clearTimeout } = require('timers');
|
||||||
|
|
||||||
const kMaybeDestroy = Symbol('kMaybeDestroy');
|
const kMaybeDestroy = Symbol('kMaybeDestroy');
|
||||||
const kUpdateTimer = Symbol('kUpdateTimer');
|
const kUpdateTimer = Symbol('kUpdateTimer');
|
||||||
|
@ -39,7 +39,7 @@ function debuglog(set) {
|
|||||||
emitWarningIfNeeded(set);
|
emitWarningIfNeeded(set);
|
||||||
debugs[set] = function debug(...args) {
|
debugs[set] = function debug(...args) {
|
||||||
const msg = format(...args);
|
const msg = format(...args);
|
||||||
console.error('%s %d: %s', set, pid, msg);
|
process.stderr.write(format('%s %d: %s\n', set, pid, msg));
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
debugs[set] = function debug() {};
|
debugs[set] = function debug() {};
|
||||||
|
@ -97,6 +97,7 @@ const {
|
|||||||
let cluster;
|
let cluster;
|
||||||
let dns;
|
let dns;
|
||||||
|
|
||||||
|
const { clearTimeout } = require('timers');
|
||||||
const { kTimeout } = require('internal/timers');
|
const { kTimeout } = require('internal/timers');
|
||||||
|
|
||||||
const DEFAULT_IPV4_ADDR = '0.0.0.0';
|
const DEFAULT_IPV4_ADDR = '0.0.0.0';
|
||||||
|
@ -47,6 +47,7 @@ const {
|
|||||||
ERR_INVALID_PERFORMANCE_MARK
|
ERR_INVALID_PERFORMANCE_MARK
|
||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
|
|
||||||
|
const { setImmediate } = require('timers');
|
||||||
const kHandle = Symbol('handle');
|
const kHandle = Symbol('handle');
|
||||||
const kMap = Symbol('map');
|
const kMap = Symbol('map');
|
||||||
const kCallback = Symbol('callback');
|
const kCallback = Symbol('callback');
|
||||||
|
@ -44,6 +44,7 @@ const {
|
|||||||
stripVTControlCharacters
|
stripVTControlCharacters
|
||||||
} = require('internal/readline');
|
} = require('internal/readline');
|
||||||
|
|
||||||
|
const { clearTimeout, setTimeout } = require('timers');
|
||||||
const {
|
const {
|
||||||
kEscape,
|
kEscape,
|
||||||
kClearToBeginning,
|
kClearToBeginning,
|
||||||
|
@ -113,8 +113,12 @@ function timestamp() {
|
|||||||
return [d.getDate(), months[d.getMonth()], time].join(' ');
|
return [d.getDate(), months[d.getMonth()], time].join(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let console;
|
||||||
// Log is just a thin wrapper to console.log that prepends a timestamp
|
// Log is just a thin wrapper to console.log that prepends a timestamp
|
||||||
function log(...args) {
|
function log(...args) {
|
||||||
|
if (!console) {
|
||||||
|
console = require('internal/console/global');
|
||||||
|
}
|
||||||
console.log('%s - %s', timestamp(), format(...args));
|
console.log('%s - %s', timestamp(), format(...args));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user