• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  ObjectDefineProperty,
5  ObjectKeys,
6} = primordials;
7
8let session;
9function sendInspectorCommand(cb, onError) {
10  const { hasInspector } = internalBinding('config');
11  if (!hasInspector) return onError();
12  const inspector = require('inspector');
13  if (session === undefined) session = new inspector.Session();
14  session.connect();
15  try {
16    return cb(session);
17  } finally {
18    session.disconnect();
19  }
20}
21
22// Create a special require function for the inspector command line API
23function installConsoleExtensions(commandLineApi) {
24  if (commandLineApi.require) { return; }
25  const { tryGetCwd } = require('internal/process/execution');
26  const CJSModule = require('internal/modules/cjs/loader').Module;
27  const { makeRequireFunction } = require('internal/modules/cjs/helpers');
28  const consoleAPIModule = new CJSModule('<inspector console>');
29  const cwd = tryGetCwd();
30  consoleAPIModule.paths =
31      CJSModule._nodeModulePaths(cwd).concat(CJSModule.globalPaths);
32  commandLineApi.require = makeRequireFunction(consoleAPIModule);
33}
34
35// Wrap a console implemented by Node.js with features from the VM inspector
36function wrapConsole(consoleFromNode, consoleFromVM) {
37  const { consoleCall } = internalBinding('inspector');
38  for (const key of ObjectKeys(consoleFromVM)) {
39    // If global console has the same method as inspector console,
40    // then wrap these two methods into one. Native wrapper will preserve
41    // the original stack.
42    if (consoleFromNode.hasOwnProperty(key)) {
43      consoleFromNode[key] = consoleCall.bind(consoleFromNode,
44                                              consoleFromVM[key],
45                                              consoleFromNode[key]);
46      ObjectDefineProperty(consoleFromNode[key], 'name', {
47        value: key
48      });
49    } else {
50      // Add additional console APIs from the inspector
51      consoleFromNode[key] = consoleFromVM[key];
52    }
53  }
54}
55
56// Stores the console from VM, should be set during bootstrap.
57let consoleFromVM;
58module.exports = {
59  installConsoleExtensions,
60  sendInspectorCommand,
61  wrapConsole,
62  get consoleFromVM() {
63    return consoleFromVM;
64  },
65  set consoleFromVM(val) {
66    consoleFromVM = val;
67  }
68};
69