UNPKG

nstdlib-nightly

Version:

Node.js standard library converted to runtime-agnostic ES modules.

124 lines (113 loc) 4.34 kB
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/util/inspector.js import * as __hoisted_internal_process_execution__ from "nstdlib/lib/internal/process/execution"; import * as __hoisted_internal_modules_cjs_loader__ from "nstdlib/lib/internal/modules/cjs/loader"; import * as __hoisted_internal_modules_helpers__ from "nstdlib/lib/internal/modules/helpers"; import { validatePort } from "nstdlib/lib/internal/validators"; import * as permission from "nstdlib/lib/internal/process/permission"; import * as __hoisted_inspector__ from "nstdlib/lib/inspector"; const kMinPort = 1024; const kMaxPort = 65535; const kInspectArgRegex = /--inspect(?:-brk|-port)?|--debug-port/; const kInspectMsgRegex = /Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\/|For help, see: https:\/\/nodejs\.org\/en\/docs\/inspector|Debugger attached|Waiting for the debugger to disconnect\.\.\./; const _isUsingInspector = new WeakMap(); function isUsingInspector(execArgv = process.execArgv) { if (!_isUsingInspector.has(execArgv)) { _isUsingInspector.set( execArgv, Array.prototype.some.call( execArgv, (arg) => RegExp.prototype.exec.call(kInspectArgRegex, arg) !== null, ) || RegExp.prototype.exec.call( kInspectArgRegex, process.env.NODE_OPTIONS, ) !== null, ); } return _isUsingInspector.get(execArgv); } let debugPortOffset = 1; function getInspectPort(inspectPort) { if (typeof inspectPort === "function") { inspectPort = inspectPort(); } else if (inspectPort == null) { inspectPort = process.debugPort + debugPortOffset; if (inspectPort > kMaxPort) inspectPort = inspectPort - kMaxPort + kMinPort - 1; debugPortOffset++; } validatePort(inspectPort); return inspectPort; } let session; function sendInspectorCommand(cb, onError) { const { hasInspector } = require("binding/config"); if (!hasInspector) return onError(); // Do not preview when the permission model is enabled // because this feature require access to the inspector, // which is unavailable in this case. if (permission.isEnabled()) return onError(); const inspector = __hoisted_inspector__; if (session === undefined) session = new inspector.Session(); session.connect(); try { return cb(session); } finally { session.disconnect(); } } function isInspectorMessage(string) { return ( isUsingInspector() && RegExp.prototype.exec.call(kInspectMsgRegex, string) !== null ); } // Create a special require function for the inspector command line API function installConsoleExtensions(commandLineApi) { if (commandLineApi.require) { return; } const { tryGetCwd } = __hoisted_internal_process_execution__; const { Module: CJSModule } = __hoisted_internal_modules_cjs_loader__; const { makeRequireFunction } = __hoisted_internal_modules_helpers__; const consoleAPIModule = new CJSModule("<inspector console>"); const cwd = tryGetCwd(); consoleAPIModule.paths = []; Array.prototype.push.apply( consoleAPIModule.paths, CJSModule._nodeModulePaths(cwd), ); Array.prototype.push.apply(consoleAPIModule.paths, CJSModule.globalPaths); commandLineApi.require = makeRequireFunction(consoleAPIModule); } // Wrap a console implemented by Node.js with features from the VM inspector function wrapConsole(consoleFromNode) { const { consoleCall, console: consoleFromVM } = require("binding/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 (Object.prototype.hasOwnProperty.call(consoleFromNode, key)) { consoleFromNode[key] = Function.prototype.bind.call( consoleCall, consoleFromNode, consoleFromVM[key], consoleFromNode[key], ); Object.defineProperty(consoleFromNode[key], "name", { __proto__: null, value: key, }); } else { // Add additional console APIs from the inspector consoleFromNode[key] = consoleFromVM[key]; } } } export { getInspectPort }; export { installConsoleExtensions }; export { isInspectorMessage }; export { isUsingInspector }; export { sendInspectorCommand }; export { wrapConsole };