-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
console: refactor inspector console extension installation #25450
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
'use strict'; | ||
|
||
const { hasInspector } = internalBinding('config'); | ||
const inspector = hasInspector ? require('inspector') : undefined; | ||
|
||
let session; | ||
|
||
function sendInspectorCommand(cb, onError) { | ||
const { hasInspector } = internalBinding('config'); | ||
const inspector = hasInspector ? require('inspector') : undefined; | ||
if (!hasInspector) return onError(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: maybe just switch around the above two lines? That way there's no need for the ternary conditional. |
||
if (session === undefined) session = new inspector.Session(); | ||
try { | ||
|
@@ -20,6 +18,49 @@ function sendInspectorCommand(cb, onError) { | |
} | ||
} | ||
|
||
// Create a special require function for the inspector command line API | ||
function installConsoleExtensions(commandLineApi) { | ||
if (commandLineApi.require) { return; } | ||
const { tryGetCwd } = require('internal/process/execution'); | ||
const CJSModule = require('internal/modules/cjs/loader'); | ||
const { makeRequireFunction } = require('internal/modules/cjs/helpers'); | ||
const consoleAPIModule = new CJSModule('<inspector console>'); | ||
const cwd = tryGetCwd(); | ||
consoleAPIModule.paths = | ||
CJSModule._nodeModulePaths(cwd).concat(CJSModule.globalPaths); | ||
commandLineApi.require = makeRequireFunction(consoleAPIModule); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👆 Could someone explain this bit. I'm unfamiliar with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inspector console is the one in the chrome inspector. By doing this, a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this something user accessible? If so, what does accessing it look like? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jdalton: it's accessed when using |
||
|
||
// Wrap a console implemented by Node.js with features from the VM inspector | ||
function wrapConsole(consoleFromNode, consoleFromVM) { | ||
const config = {}; | ||
const { consoleCall } = internalBinding('inspector'); | ||
for (const key of Object.keys(consoleFromVM)) { | ||
// If global console has the same method as inspector console, | ||
// then wrap these two methods into one. Native wrapper will preserve | ||
// the original stack. | ||
if (consoleFromNode.hasOwnProperty(key)) { | ||
consoleFromNode[key] = consoleCall.bind(consoleFromNode, | ||
consoleFromVM[key], | ||
consoleFromNode[key], | ||
config); | ||
} else { | ||
// Add additional console APIs from the inspector | ||
consoleFromNode[key] = consoleFromVM[key]; | ||
} | ||
} | ||
} | ||
|
||
// Stores the console from VM, should be set during bootstrap. | ||
let consoleFromVM; | ||
module.exports = { | ||
sendInspectorCommand | ||
installConsoleExtensions, | ||
sendInspectorCommand, | ||
wrapConsole, | ||
get consoleFromVM() { | ||
return consoleFromVM; | ||
}, | ||
set consoleFromVM(val) { | ||
consoleFromVM = val; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to move this into
internal/util/inspector
. That way there's no need for the getter onmodule.exports
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The console from the VM is only accessible during bootstrap,
global.console
gets overridden with our console implementation - an alternative may be to store it in C++ and make it accessible (and cached) through'internal/util/inspector'
, but otherwise this has to be done inlib/internal/bootstrap/node.js
one way or another (stored either as a value or through a setter)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consoleFromVM
is passed toinspector.wrapConsole()
in the line below. Therefore theconsoleFromVM
variable inlib/internal/util/inspector.js
could be set during thewrapConsole
function call.But it's just a nit and is not blocking for me.