11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { 41cb0ef41Sopenharmony_ci ArrayPrototypeSome, 51cb0ef41Sopenharmony_ci ArrayPrototypePushApply, 61cb0ef41Sopenharmony_ci FunctionPrototypeBind, 71cb0ef41Sopenharmony_ci ObjectDefineProperty, 81cb0ef41Sopenharmony_ci ObjectKeys, 91cb0ef41Sopenharmony_ci ObjectPrototypeHasOwnProperty, 101cb0ef41Sopenharmony_ci RegExpPrototypeExec, 111cb0ef41Sopenharmony_ci SafeWeakMap, 121cb0ef41Sopenharmony_ci} = primordials; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst { validatePort } = require('internal/validators'); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst kMinPort = 1024; 171cb0ef41Sopenharmony_ciconst kMaxPort = 65535; 181cb0ef41Sopenharmony_ciconst kInspectArgRegex = /--inspect(?:-brk|-port)?|--debug-port/; 191cb0ef41Sopenharmony_ciconst kInspectMsgRegex = /Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\/|For help, see: https:\/\/nodejs\.org\/en\/docs\/inspector|Debugger attached|Waiting for the debugger to disconnect\.\.\./; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciconst _isUsingInspector = new SafeWeakMap(); 221cb0ef41Sopenharmony_cifunction isUsingInspector(execArgv = process.execArgv) { 231cb0ef41Sopenharmony_ci if (!_isUsingInspector.has(execArgv)) { 241cb0ef41Sopenharmony_ci _isUsingInspector.set(execArgv, 251cb0ef41Sopenharmony_ci ArrayPrototypeSome(execArgv, (arg) => RegExpPrototypeExec(kInspectArgRegex, arg) !== null) || 261cb0ef41Sopenharmony_ci RegExpPrototypeExec(kInspectArgRegex, process.env.NODE_OPTIONS) !== null); 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci return _isUsingInspector.get(execArgv); 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cilet debugPortOffset = 1; 321cb0ef41Sopenharmony_cifunction getInspectPort(inspectPort) { 331cb0ef41Sopenharmony_ci if (typeof inspectPort === 'function') { 341cb0ef41Sopenharmony_ci inspectPort = inspectPort(); 351cb0ef41Sopenharmony_ci } else if (inspectPort == null) { 361cb0ef41Sopenharmony_ci inspectPort = process.debugPort + debugPortOffset; 371cb0ef41Sopenharmony_ci if (inspectPort > kMaxPort) 381cb0ef41Sopenharmony_ci inspectPort = inspectPort - kMaxPort + kMinPort - 1; 391cb0ef41Sopenharmony_ci debugPortOffset++; 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci validatePort(inspectPort); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci return inspectPort; 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_cilet session; 471cb0ef41Sopenharmony_cifunction sendInspectorCommand(cb, onError) { 481cb0ef41Sopenharmony_ci const { hasInspector } = internalBinding('config'); 491cb0ef41Sopenharmony_ci if (!hasInspector) return onError(); 501cb0ef41Sopenharmony_ci const inspector = require('inspector'); 511cb0ef41Sopenharmony_ci if (session === undefined) session = new inspector.Session(); 521cb0ef41Sopenharmony_ci session.connect(); 531cb0ef41Sopenharmony_ci try { 541cb0ef41Sopenharmony_ci return cb(session); 551cb0ef41Sopenharmony_ci } finally { 561cb0ef41Sopenharmony_ci session.disconnect(); 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_cifunction isInspectorMessage(string) { 611cb0ef41Sopenharmony_ci return isUsingInspector() && RegExpPrototypeExec(kInspectMsgRegex, string) !== null; 621cb0ef41Sopenharmony_ci} 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci// Create a special require function for the inspector command line API 651cb0ef41Sopenharmony_cifunction installConsoleExtensions(commandLineApi) { 661cb0ef41Sopenharmony_ci if (commandLineApi.require) { return; } 671cb0ef41Sopenharmony_ci const { tryGetCwd } = require('internal/process/execution'); 681cb0ef41Sopenharmony_ci const CJSModule = require('internal/modules/cjs/loader').Module; 691cb0ef41Sopenharmony_ci const { makeRequireFunction } = require('internal/modules/helpers'); 701cb0ef41Sopenharmony_ci const consoleAPIModule = new CJSModule('<inspector console>'); 711cb0ef41Sopenharmony_ci const cwd = tryGetCwd(); 721cb0ef41Sopenharmony_ci consoleAPIModule.paths = []; 731cb0ef41Sopenharmony_ci ArrayPrototypePushApply(consoleAPIModule.paths, CJSModule._nodeModulePaths(cwd)); 741cb0ef41Sopenharmony_ci ArrayPrototypePushApply(consoleAPIModule.paths, CJSModule.globalPaths); 751cb0ef41Sopenharmony_ci commandLineApi.require = makeRequireFunction(consoleAPIModule); 761cb0ef41Sopenharmony_ci} 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci// Wrap a console implemented by Node.js with features from the VM inspector 791cb0ef41Sopenharmony_cifunction wrapConsole(consoleFromNode) { 801cb0ef41Sopenharmony_ci const { consoleCall, console: consoleFromVM } = internalBinding('inspector'); 811cb0ef41Sopenharmony_ci for (const key of ObjectKeys(consoleFromVM)) { 821cb0ef41Sopenharmony_ci // If global console has the same method as inspector console, 831cb0ef41Sopenharmony_ci // then wrap these two methods into one. Native wrapper will preserve 841cb0ef41Sopenharmony_ci // the original stack. 851cb0ef41Sopenharmony_ci if (ObjectPrototypeHasOwnProperty(consoleFromNode, key)) { 861cb0ef41Sopenharmony_ci consoleFromNode[key] = FunctionPrototypeBind( 871cb0ef41Sopenharmony_ci consoleCall, 881cb0ef41Sopenharmony_ci consoleFromNode, 891cb0ef41Sopenharmony_ci consoleFromVM[key], 901cb0ef41Sopenharmony_ci consoleFromNode[key], 911cb0ef41Sopenharmony_ci ); 921cb0ef41Sopenharmony_ci ObjectDefineProperty(consoleFromNode[key], 'name', { 931cb0ef41Sopenharmony_ci __proto__: null, 941cb0ef41Sopenharmony_ci value: key, 951cb0ef41Sopenharmony_ci }); 961cb0ef41Sopenharmony_ci } else { 971cb0ef41Sopenharmony_ci // Add additional console APIs from the inspector 981cb0ef41Sopenharmony_ci consoleFromNode[key] = consoleFromVM[key]; 991cb0ef41Sopenharmony_ci } 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci} 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_cimodule.exports = { 1041cb0ef41Sopenharmony_ci getInspectPort, 1051cb0ef41Sopenharmony_ci installConsoleExtensions, 1061cb0ef41Sopenharmony_ci isInspectorMessage, 1071cb0ef41Sopenharmony_ci isUsingInspector, 1081cb0ef41Sopenharmony_ci sendInspectorCommand, 1091cb0ef41Sopenharmony_ci wrapConsole, 1101cb0ef41Sopenharmony_ci}; 111