All files / lib/internal/util inspector.js

100% Statements 64/64
66.67% Branches 4/6
100% Functions 3/3
100% Lines 64/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 651x 1x 1x 1x 1x 1x 1x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 84x 1x 1x 1x 1x 1x  
'use strict';
 
const {
  ObjectKeys,
} = primordials;
 
let session;
function sendInspectorCommand(cb, onError) {
  const { hasInspector } = internalBinding('config');
  if (!hasInspector) return onError();
  const inspector = require('inspector');
  if (session === undefined) session = new inspector.Session();
  session.connect();
  try {
    return cb(session);
  } finally {
    session.disconnect();
  }
}
 
// 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').Module;
  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);
}
 
// Wrap a console implemented by Node.js with features from the VM inspector
function wrapConsole(consoleFromNode, consoleFromVM) {
  const { consoleCall } = internalBinding('inspector');
  for (const key of ObjectKeys(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]);
    } 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 = {
  installConsoleExtensions,
  sendInspectorCommand,
  wrapConsole,
  get consoleFromVM() {
    return consoleFromVM;
  },
  set consoleFromVM(val) {
    consoleFromVM = val;
  }
};