-
Notifications
You must be signed in to change notification settings - Fork 34
Handle Node.js Debug Messages and Improve Port Termination #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+97
−11
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This file outputs debugging information to stdout | ||
console.log("Debug message: Module loading"); | ||
console.debug("Debug message: Initializing module"); | ||
|
||
module.exports = function testFunction(input) { | ||
console.log(`Debug message: Function called with input: ${input}`); | ||
console.debug("Debug message: Processing input"); | ||
|
||
return `Processed: ${input}`; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
defmodule NodeJS.DebugModeTest do | ||
use ExUnit.Case | ||
import ExUnit.CaptureLog | ||
|
||
describe "debug_mode functionality" do | ||
test "logs Node.js stdout messages when debug_mode is enabled" do | ||
defmodule TestDebugHandler do | ||
use GenServer | ||
require Logger | ||
|
||
def start_link do | ||
GenServer.start_link(__MODULE__, nil) | ||
end | ||
|
||
def init(_) do | ||
{:ok, nil} | ||
end | ||
|
||
def debug_mode? do | ||
Application.get_env(:nodejs, :debug_mode, false) | ||
end | ||
|
||
# The implementation from worker.ex | ||
def handle_info({_pid, {:data, {_flag, msg}} = _data}, state) do | ||
if debug_mode?() do | ||
Logger.info("NodeJS: #{msg}") | ||
end | ||
|
||
{:noreply, state} | ||
end | ||
end | ||
|
||
# Start the test process | ||
{:ok, pid} = TestDebugHandler.start_link() | ||
|
||
# Test with debug_mode disabled (default) | ||
log_without_debug = | ||
capture_log(fn -> | ||
# Simulate debugger message from Node.js | ||
send(pid, {self(), {:data, {:eol, "Debugger listening on ws://127.0.0.1:9229/abc123"}}}) | ||
# Wait for any potential logging | ||
Process.sleep(50) | ||
end) | ||
|
||
# Verify no logging occurred | ||
refute log_without_debug =~ "NodeJS: Debugger listening" | ||
|
||
# Enable debug_mode | ||
Application.put_env(:nodejs, :debug_mode, true) | ||
|
||
# Test with debug_mode enabled | ||
log_with_debug = | ||
capture_log(fn -> | ||
# Simulate debugger message from Node.js | ||
send(pid, {self(), {:data, {:eol, "Debugger listening on ws://127.0.0.1:9229/abc123"}}}) | ||
# Wait for any potential logging | ||
Process.sleep(50) | ||
end) | ||
|
||
# Clean up | ||
Application.delete_env(:nodejs, :debug_mode) | ||
|
||
# Verify logging occurred | ||
assert log_with_debug =~ "NodeJS: Debugger listening" | ||
end | ||
end | ||
|
||
describe "port safety" do | ||
test "reset_terminal handles invalid ports gracefully" do | ||
defmodule TestPortHandler do | ||
use GenServer | ||
require Logger | ||
|
||
def start_link do | ||
GenServer.start_link(__MODULE__, nil) | ||
end | ||
|
||
def init(_) do | ||
{:ok, nil} | ||
end | ||
|
||
# The implementation from worker.ex | ||
def reset_terminal(port) do | ||
try do | ||
Port.command(port, "\x1b[0m\x1b[?7h\x1b[?25h\x1b[H\x1b[2J") | ||
Port.command(port, "\x1b[!p\x1b[?47l") | ||
rescue | ||
_ -> | ||
Logger.debug("NodeJS: Could not reset terminal - port may be closed") | ||
end | ||
end | ||
end | ||
|
||
log = | ||
capture_log(fn -> | ||
# Try to reset an invalid port | ||
TestPortHandler.reset_terminal(:invalid_port) | ||
Process.sleep(50) | ||
end) | ||
|
||
# Verify proper error handling | ||
assert log =~ "NodeJS: Could not reset terminal" | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added try/rescue to fix crashes when ports are closed during termination. Instead of blowing up with ArgumentError, we now just log and move on. This helps during dev reloading or when containers are shutting down - basically anytime processes are killed unpredictably.