Skip to content

Commit 1471df9

Browse files
committed
Added NodeJS inspector
1 parent 7205363 commit 1471df9

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

code/framework/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ macro(link_shared_deps target_name)
205205
endif()
206206
endmacro()
207207

208+
# Node.js inspector support for script debugging (auto-enabled for Debug builds)
209+
option(FW_NODE_INSPECTOR "Enable Node.js inspector for script debugging" OFF)
210+
208211
# FrameworkNodeEngine - Object library for node_engine.cpp
209212
# This is separate because it needs NODE_WANT_INTERNALS and libuv headers which
210213
# cause Windows SDK errno conflicts (EINVAL/ERANGE undefined) when applied to PCH.
@@ -235,6 +238,10 @@ target_compile_definitions(FrameworkNodeEngine PRIVATE NODE_WANT_INTERNALS=1)
235238
target_compile_definitions(FrameworkNodeEngine PRIVATE MG_ENABLE_LOG=0)
236239
target_compile_features(FrameworkNodeEngine PRIVATE cxx_std_20)
237240

241+
# Node.js inspector: enabled via explicit option OR Debug builds
242+
target_compile_definitions(FrameworkNodeEngine PRIVATE
243+
$<$<OR:$<BOOL:${FW_NODE_INSPECTOR}>,$<CONFIG:Debug>>:FW_NODE_INSPECTOR>)
244+
238245
# Link the solutions based on the platform
239246
link_shared_deps(Framework)
240247
target_link_libraries(Framework v8 v8pp libnode)
@@ -245,10 +252,14 @@ target_sources(Framework PRIVATE $<TARGET_OBJECTS:FrameworkNodeEngine>)
245252
if(WIN32)
246253
link_shared_deps(FrameworkClient)
247254
target_link_libraries(FrameworkClient v8 v8pp libnode)
255+
target_compile_definitions(FrameworkClient PRIVATE
256+
$<$<OR:$<BOOL:${FW_NODE_INSPECTOR}>,$<CONFIG:Debug>>:FW_NODE_INSPECTOR>)
248257
endif()
249258

250259
link_shared_deps(FrameworkServer)
251260
target_link_libraries(FrameworkServer v8 v8pp MafiaHubServices libsig libnode)
261+
target_compile_definitions(FrameworkServer PRIVATE
262+
$<$<OR:$<BOOL:${FW_NODE_INSPECTOR}>,$<CONFIG:Debug>>:FW_NODE_INSPECTOR>)
252263

253264
# FrameworkServer-specific includes
254265
target_include_directories(FrameworkServer PRIVATE

code/framework/src/integrations/client/scripting/module.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ namespace Framework::Integrations::Client::Scripting {
1919
Framework::Scripting::NodeEngineOptions options;
2020
options.sandboxed = true;
2121
options.processName = "mafiahub-client";
22+
#ifdef FW_NODE_INSPECTOR
23+
options.enableInspector = true;
24+
options.inspectorPort = 9230;
25+
#endif
2226
_nodeEngine = std::make_unique<Framework::Scripting::NodeEngine>(options);
2327
}
2428

code/framework/src/integrations/server/scripting/module.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ namespace Framework::Integrations::Server::Scripting {
1717
Framework::Scripting::NodeEngineOptions options;
1818
options.sandboxed = false;
1919
options.processName = "mafiahub-server";
20+
#ifdef FW_NODE_INSPECTOR
21+
options.enableInspector = true;
22+
options.inspectorPort = 9229;
23+
#endif
2024
_nodeEngine = std::make_unique<Framework::Scripting::NodeEngine>(options);
2125
}
2226

code/framework/src/scripting/node_engine.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "node_engine.h"
22
#include "builtins/messages.h"
33

4+
#include <spdlog/spdlog.h>
5+
46
#include <filesystem>
57
#include <fstream>
68
#include <sstream>
@@ -143,6 +145,14 @@ namespace Framework::Scripting {
143145
// Build args from options
144146
std::vector<std::string> nodeArgs = {_options.processName};
145147

148+
#ifdef FW_NODE_INSPECTOR
149+
if (_options.enableInspector) {
150+
std::string flag = _options.inspectorWaitForDebugger ? "--inspect-brk=" : "--inspect=";
151+
flag += _options.inspectorHost + ":" + std::to_string(_options.inspectorPort);
152+
nodeArgs.push_back(flag);
153+
}
154+
#endif
155+
146156
// Initialize Node.js with flags to control V8 platform ourselves
147157
// Using initializer_list syntax as shown in Node.js docs
148158
_initResult = node::InitializeOncePerProcess(
@@ -168,6 +178,13 @@ namespace Framework::Scripting {
168178
v8::V8::Initialize();
169179

170180
_platformInitialized = true;
181+
182+
#ifdef FW_NODE_INSPECTOR
183+
if (_options.enableInspector) {
184+
spdlog::info("Node.js inspector listening on {}:{}", _options.inspectorHost, _options.inspectorPort);
185+
}
186+
#endif
187+
171188
return true;
172189
}
173190

@@ -358,8 +375,6 @@ namespace Framework::Scripting {
358375
// Other dangerous modules
359376
'vm', 'node:vm',
360377
'v8', 'node:v8',
361-
'inspector', 'node:inspector',
362-
'inspector/promises', 'node:inspector/promises',
363378
'trace_events', 'node:trace_events',
364379
'perf_hooks', 'node:perf_hooks',
365380
'async_hooks', 'node:async_hooks',
@@ -490,14 +505,31 @@ namespace Framework::Scripting {
490505
// This is also set at the C++ level but we reinforce it here
491506
// Note: This would require context-level settings which we do in C++
492507
508+
// Block inspector module unless explicitly enabled for debugging
509+
if (!globalThis.__INSPECTOR_ENABLED__) {
510+
blockedModules.add('inspector');
511+
blockedModules.add('node:inspector');
512+
blockedModules.add('inspector/promises');
513+
blockedModules.add('node:inspector/promises');
514+
}
515+
493516
// Mark sandbox as applied
494517
globalThis.__SANDBOX_APPLIED__ = true;
495518
})();
496519
)JS";
497520

498-
v8::TryCatch tryCatch(_isolate);
499521
v8::Local<v8::Context> context = _setup->context();
500522

523+
// Set inspector flag before sandbox code runs so it can conditionally
524+
// allow the inspector module for debugging
525+
if (_options.enableInspector) {
526+
v8::Local<v8::String> key =
527+
v8::String::NewFromUtf8(_isolate, "__INSPECTOR_ENABLED__").ToLocalChecked();
528+
context->Global()->Set(context, key, v8::Boolean::New(_isolate, true)).Check();
529+
}
530+
531+
v8::TryCatch tryCatch(_isolate);
532+
501533
v8::Local<v8::String> source =
502534
v8::String::NewFromUtf8(_isolate, sandboxCode).ToLocalChecked();
503535
v8::ScriptOrigin origin(

code/framework/src/scripting/node_engine.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ namespace Framework::Scripting {
3939
* Process name shown in Node.js (argv[0]).
4040
*/
4141
std::string processName = "mafiahub";
42+
43+
/**
44+
* Enable the Node.js inspector agent for debugging.
45+
* Only effective when compiled with FW_NODE_INSPECTOR define.
46+
*/
47+
bool enableInspector = false;
48+
49+
/**
50+
* Port for the inspector agent to listen on.
51+
*/
52+
int inspectorPort = 9229;
53+
54+
/**
55+
* Host for the inspector agent to bind to.
56+
*/
57+
std::string inspectorHost = "127.0.0.1";
58+
59+
/**
60+
* If true, pause execution at start until a debugger connects.
61+
*/
62+
bool inspectorWaitForDebugger = false;
4263
};
4364

4465
/**

0 commit comments

Comments
 (0)