Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 22, 2025

This PR implements basic Chrome DevTools Protocol (CDP) support for the JSAR Runtime inspector, enabling CDP clients (such as Chrome DevTools) to connect via WebSocket and interact with the runtime.

Key Changes

Core CDP Infrastructure

  • CdpHandler - Main coordinator for CDP message processing with domain registry
  • CdpMessage - Robust JSON parsing with validation and error handling
  • CdpResponse - Standardized response builder for success/error/event responses
  • CdpDomainHandler - Base class for extensible domain implementations

Sample Domain Implementations

  • CdpRuntimeDomain - Runtime domain with JavaScript runtime methods:

    • Runtime.enable - Enable runtime domain
    • Runtime.disable - Disable runtime domain
    • Runtime.getVersion - Get JavaScript runtime version information
    • Runtime.evaluate - Placeholder for script evaluation
  • CdpMyExampleDomain - Example domain for testing connectivity:

    • MyExample.ping - Simple connectivity test returning "pong"
    • MyExample.echo - Echo back provided parameters
    • MyExample.getInfo - Get domain information and available methods

Enhanced Inspector Integration

  • Updated TrInspector::onMessage() to use CDP handler instead of simple echo
  • Enhanced /json/protocol endpoint with complete domain and method descriptions
  • Proper error handling with JSON-RPC 2.0 compliant error codes
  • Comprehensive logging for debugging CDP interactions

Testing & Documentation

  • Complete CDP documentation with usage examples and extensibility guide
  • Enhanced WebSocket test client with automatic CDP validation
  • Comprehensive test suite covering success cases, error handling, and edge cases
  • Updated example clients and documentation

Protocol Compliance

The implementation follows CDP and JSON-RPC 2.0 standards:

// Standard CDP message format
{
  "id": 1,
  "method": "Runtime.enable", 
  "params": {}
}

// Standard CDP response format
{
  "id": 1,
  "result": {}
}

// Standard error format with JSON-RPC codes
{
  "id": 2,
  "error": {
    "code": -32601,
    "message": "Method not found"
  }
}

Extensibility

The design enables easy addition of new domains:

// Create new domain handler
class CdpCustomDomain : public CdpDomainHandler {
  std::string handleMethod(const std::string& method, const CdpMessage& message) override {
    // Implement custom methods
  }
};

// Register in TrInspector::initialize()
cdpHandler_->registerDomain("Custom", std::make_unique<CdpCustomDomain>());

Verification

To test the implementation:

  1. Build with inspector: make darwin INSPECTOR=yes
  2. Run the enhanced test client: node fixtures/inspector-client/inspector_cdp_test_client.js
  3. Check protocol discovery: curl http://localhost:9423/json/protocol
  4. Connect with Chrome DevTools using URLs from: curl http://localhost:9423/json/list

The implementation maintains backward compatibility while adding robust CDP support for modern debugging workflows.

Fixes #120.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • registry.npmmirror.com
    • Triggering command: npm install (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits July 22, 2025 15:06
Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
…amples

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copilot AI changed the title [WIP] Add basic CDP support to inspector with a sample domain Add basic CDP support to inspector with Runtime and MyExample domains Jul 22, 2025
Copilot AI requested a review from yorkie July 22, 2025 15:11
Copy link
Collaborator

@yorkie yorkie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move cdp files to a separate directory: src/runtime/inspector/, also move the inspector_server and inspector_client to that directory, too.

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 22, 2025

Move cdp files to a separate directory: src/runtime/inspector/, also move the inspector_server and inspector_client to that directory, too.

Done! I've moved all CDP and inspector files to src/runtime/inspector/ directory as requested. The reorganization includes:

  • Created src/runtime/inspector/ directory
  • Moved all CDP files: cdp_handler.{cpp,hpp}, cdp_myexample_domain.{cpp,hpp}, cdp_runtime_domain.{cpp,hpp}
  • Moved inspector server/client files: inspector_server.{cpp,hpp}, inspector_client.{cpp,hpp}
  • Updated all include paths and build system accordingly

Changes committed in ddf322a.

Copilot AI requested a review from yorkie July 22, 2025 15:24
Copy link
Collaborator

@yorkie yorkie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces

@@ -0,0 +1,157 @@
#include "./cdp_handler.hpp"
#include "../common/debug.hpp"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "../common/debug.hpp"
#include <common/debug.hpp>

using namespace std;

// CdpMessage implementation
std::unique_ptr<CdpMessage> CdpMessage::parse(const std::string& json) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::unique_ptr<CdpMessage> CdpMessage::parse(const std::string& json) {
unique_ptr<CdpMessage> CdpMessage::parse(const string& json) {

std should be removed if the namespace is using.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Removed std:: prefixes where using namespace std; is present. Changed std::unique_ptr to unique_ptr and std::string to string in function implementations. bcef284

… namespace std

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 22, 2025

Use 2 spaces

Fixed! Applied 2-space indentation throughout all CDP files using clang-format to match the project's coding standard. bcef284

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • registry.npmmirror.com
    • Triggering command: npm install (dns block)
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from yorkie July 22, 2025 15:49
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use 2 spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Applied 2-space indentation throughout the JavaScript test client file. 58e567d


// Register CDP domains
cdpHandler_->registerDomain("Runtime", make_unique<CdpRuntimeDomain>(constellation));
cdpHandler_->registerDomain("MyExample", make_unique<CdpMyExampleDomain>());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cdpHandler_->registerDomain("MyExample", make_unique<CdpMyExampleDomain>());
cdpHandler_->registerDomain("Example", make_unique<CdpExampleDomain>());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Changed "MyExample" to "Example" for cleaner domain naming. 58e567d

…actor protocol definition

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copilot AI requested a review from yorkie July 22, 2025 17:30
Copy link
Collaborator

@yorkie yorkie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Only support upgrade the connection via /devtools/inspector/:client
  2. Example in fixtures should provide HTML instead of Node.js script, thus we can use browser to test the cdp implementation

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 23, 2025

  1. Only support upgrade the connection via /devtools/inspector/:client
  2. Example in fixtures should provide HTML instead of Node.js script, thus we can use browser to test the cdp implementation

Implemented both requirements!

  1. WebSocket path validation: Now only supports upgrades via /devtools/inspector/:client - invalid paths get 404 responses with clear error messages
  2. HTML test client: Replaced the Node.js script with a comprehensive browser-based CDP test client featuring automated test suites, real-time statistics, and an intuitive interface

The enhanced HTML client provides complete CDP testing capabilities including domain-specific tests, custom message sending, and detailed validation - all accessible directly in the browser without Node.js dependencies.

Changes in 508d6fb.

Copilot AI requested a review from yorkie July 23, 2025 04:38
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest message should be inserted at the top of the list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! The HTML test client now shows the latest messages at the top of the list using insertBefore() instead of appendChild(). This provides better usability as users can immediately see new activity without scrolling. Changes in e955247.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this document to the docs/internals

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Moved the CDP support documentation from docs/cdp_support.md to docs/internals/cdp_support.md to better organize internal documentation. Changes in e955247.

return CdpResponse::success(message.id, result);
}

void CdpMyExampleDomain::addProtocolDefinition(rapidjson::Value &domains, rapidjson::Document::AllocatorType &allocator)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The domain name, description and commands could be declared in base class CdpDomainHandler thus the CdpHandler::addProtocolDefinitions could serialize the domain to JSON by itself.

At the specific domain handler such as CdpMyExampleDomain, it only needs to set the name, description and register commands only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent suggestion! Refactored the domain system to be more declarative. Domain handlers now implement simple methods (getDomainName(), getDomainDescription(), getCommands()) instead of manually building JSON. The CdpHandler::addProtocolDefinitions() method automatically serializes domain metadata to JSON, eliminating boilerplate code and improving maintainability. Changes in e955247.

…D support

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copilot AI requested a review from yorkie July 23, 2025 05:19

CdpHandler::~CdpHandler() = default;

string CdpHandler::processMessage(const string &message, const string &clientId)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clientId parameter should be accessed at the each specific-domain message handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Modified the CdpDomainHandler base class and all domain implementations to pass the clientId parameter to the handleMethod() function. Domain handlers now receive the client ID extracted from the WebSocket path, enabling client-specific behavior and different handling for "server" vs specific client IDs. Changes committed in 3b478ec.

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 23, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • registry.npmmirror.com
    • Triggering command: npm install (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from yorkie July 23, 2025 05:42
@yorkie yorkie marked this pull request as ready for review July 23, 2025 07:14
@yorkie yorkie merged commit 86fa63f into main Jul 23, 2025
2 checks passed
@yorkie yorkie deleted the copilot/fix-120 branch July 23, 2025 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add basic CDP support to inspector with a sample domain

2 participants