Skip to content

Commit

Permalink
lib: refactor console startup
Browse files Browse the repository at this point in the history
PR-URL: #15111
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
BridgeAR authored and jasnell committed Sep 20, 2017
1 parent 6a94c1a commit 8ce0e9a
Showing 1 changed file with 33 additions and 59 deletions.
92 changes: 33 additions & 59 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@
setupProcessICUVersions();

setupGlobalVariables();

const _process = NativeModule.require('internal/process');
_process.setupConfig(NativeModule._source);
NativeModule.require('internal/process/warning').setup();
NativeModule.require('internal/process/next_tick').setup();
NativeModule.require('internal/process/stdio').setup();

const browserGlobals = !process._noBrowserGlobals;
if (browserGlobals) {
setupGlobalTimeouts();
setupGlobalConsole();
}

const _process = NativeModule.require('internal/process');
const perf = process.binding('performance');
const {
NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE,
Expand All @@ -50,26 +56,6 @@
_process.setup_performance();
_process.setup_cpuUsage();
_process.setupMemoryUsage();
_process.setupConfig(NativeModule._source);
NativeModule.require('internal/process/warning').setup();
NativeModule.require('internal/process/next_tick').setup();
NativeModule.require('internal/process/stdio').setup();
if (browserGlobals) {
// Instantiate eagerly in case the first call is under stack overflow
// conditions where instantiation doesn't work.
const console = global.console;
console.assert;
console.clear;
console.count;
console.countReset;
console.dir;
console.error;
console.log;
console.time;
console.timeEnd;
console.trace;
console.warn;
}
_process.setupKillAndExit();
_process.setupSignalHandlers();
if (global.__coverage__)
Expand Down Expand Up @@ -319,62 +305,50 @@

function setupGlobalConsole() {
const originalConsole = global.console;
let console;
const Module = NativeModule.require('module');
// Setup Node.js global.console
const wrappedConsole = NativeModule.require('console');
Object.defineProperty(global, 'console', {
configurable: true,
enumerable: true,
get: function() {
if (!console) {
console = (originalConsole === undefined) ?
NativeModule.require('console') :
installInspectorConsole(originalConsole);
}
return console;
get() {
return wrappedConsole;
}
});
setupInspectorCommandLineAPI();
setupInspector(originalConsole, wrappedConsole, Module);
}

function installInspectorConsole(globalConsole) {
const wrappedConsole = NativeModule.require('console');
const inspector = process.binding('inspector');
if (!inspector.consoleCall) {
return wrappedConsole;
function setupInspector(originalConsole, wrappedConsole, Module) {
const { addCommandLineAPI, consoleCall } = process.binding('inspector');
if (!consoleCall) {
return;
}
// Setup inspector command line API
const { makeRequireFunction } = NativeModule.require('internal/module');
const path = NativeModule.require('path');
const cwd = tryGetCwd(path);

const consoleAPIModule = new Module('<inspector console>');
consoleAPIModule.paths =
Module._nodeModulePaths(cwd).concat(Module.globalPaths);
addCommandLineAPI('require', makeRequireFunction(consoleAPIModule));
const config = {};
for (const key of Object.keys(wrappedConsole)) {
if (!globalConsole.hasOwnProperty(key))
if (!originalConsole.hasOwnProperty(key))
continue;
// If global console has the same method as inspector console,
// then wrap these two methods into one. Native wrapper will preserve
// the original stack.
wrappedConsole[key] = inspector.consoleCall.bind(wrappedConsole,
globalConsole[key],
wrappedConsole[key],
config);
wrappedConsole[key] = consoleCall.bind(wrappedConsole,
originalConsole[key],
wrappedConsole[key],
config);
}
for (const key of Object.keys(globalConsole)) {
for (const key of Object.keys(originalConsole)) {
if (wrappedConsole.hasOwnProperty(key))
continue;
wrappedConsole[key] = globalConsole[key];
wrappedConsole[key] = originalConsole[key];
}
return wrappedConsole;
}

function setupInspectorCommandLineAPI() {
const { addCommandLineAPI } = process.binding('inspector');
if (!addCommandLineAPI) return;

const Module = NativeModule.require('module');
const { makeRequireFunction } = NativeModule.require('internal/module');
const path = NativeModule.require('path');
const cwd = tryGetCwd(path);

const consoleAPIModule = new Module('<inspector console>');
consoleAPIModule.paths =
Module._nodeModulePaths(cwd).concat(Module.globalPaths);

addCommandLineAPI('require', makeRequireFunction(consoleAPIModule));
}

function setupProcessFatal() {
Expand Down

0 comments on commit 8ce0e9a

Please sign in to comment.