CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-metro-inspector-proxy

Inspector proxy for React Native and dev tools integration that bridges React Native apps and Chrome DevTools.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

inspector-proxy.mddocs/

Inspector Proxy

Core proxy functionality for managing inspector connections and HTTP/WebSocket servers. The InspectorProxy class handles device registration, HTTP endpoints for Chrome DevTools integration, and WebSocket communication management.

Capabilities

InspectorProxy Class

Main Inspector Proxy class that connects JavaScript VM inside Android/iOS apps and JS debugger.

/**
 * Main Inspector Proxy class that manages device connections and debugger sessions
 * @param projectRoot - Root of the project used for relative to absolute source path conversion
 */
class InspectorProxy {
  constructor(projectRoot: string);
  
  /**
   * Process HTTP request sent to server
   * Handles /json, /json/list, and /json/version endpoints
   * @param request - Incoming HTTP request
   * @param response - HTTP response object
   * @param next - Next middleware function
   */
  processRequest(
    request: IncomingMessage,
    response: ServerResponse,
    next: (?Error) => mixed
  ): void;
  
  /**
   * Adds websocket listeners to the provided HTTP/HTTPS server
   * @param serverOrBaseUrl - HTTP/HTTPS server instance or base URL string
   * @returns Object mapping WebSocket paths to WebSocket.Server instances
   */
  createWebSocketListeners(
    serverOrBaseUrl: HttpServer | HttpsServer | string
  ): { [path: string]: WS.Server };
}

Usage Examples:

const { InspectorProxy } = require("metro-inspector-proxy");
const http = require("http");
const connect = require("connect");

// Create inspector proxy instance
const inspectorProxy = new InspectorProxy("/Users/dev/MyReactNativeApp");

// Set up HTTP middleware
const app = connect();
app.use(inspectorProxy.processRequest.bind(inspectorProxy));

// Create HTTP server
const httpServer = http.createServer(app);
httpServer.listen(8081, "127.0.0.1", () => {
  console.log("Inspector proxy server running on port 8081");
  
  // Set up WebSocket endpoints
  const websocketEndpoints = inspectorProxy.createWebSocketListeners(httpServer);
  
  // Handle WebSocket upgrades
  httpServer.on("upgrade", (request, socket, head) => {
    const { pathname } = require("url").parse(request.url);
    if (pathname && websocketEndpoints[pathname]) {
      websocketEndpoints[pathname].handleUpgrade(request, socket, head, (ws) => {
        websocketEndpoints[pathname].emit("connection", ws, request);
      });
    } else {
      socket.destroy();
    }
  });
});

runInspectorProxy Function

Convenience function that runs new HTTP Server and attaches Inspector Proxy to it. This is the simplest way to start a complete inspector proxy server.

/**
 * Runs new HTTP Server and attaches Inspector Proxy to it
 * @param port - Port number to listen on
 * @param projectRoot - Root directory of the project
 */
function runInspectorProxy(port: number, projectRoot: string): void;

Usage Examples:

const { runInspectorProxy } = require("metro-inspector-proxy");

// Start inspector proxy on port 8081 with project root
runInspectorProxy(8081, "/Users/dev/MyReactNativeApp");

// Server automatically handles:
// - HTTP endpoints: /json, /json/list, /json/version
// - WebSocket endpoints: /inspector/device, /inspector/debug
// - Device connection management
// - Debugger session management

HTTP Endpoints

The InspectorProxy automatically handles these HTTP endpoints:

GET /json and /json/list

Returns list of inspectable pages from all connected devices.

Response Format:

[
  {
    "id": "device1-page1",
    "description": "MyApp",
    "title": "React Native",
    "faviconUrl": "https://reactjs.org/favicon.ico",
    "devtoolsFrontendUrl": "devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=localhost:8081/inspector/debug?device=device1&page=page1",
    "type": "node",
    "webSocketDebuggerUrl": "ws://localhost:8081/inspector/debug?device=device1&page=page1",
    "vm": "Hermes",
    "deviceName": "iPhone Simulator"
  }
]

GET /json/version

Returns Chrome debugger protocol version information.

Response Format:

{
  "Browser": "Mobile JavaScript",
  "Protocol-Version": "1.1"
}

WebSocket Endpoints

The InspectorProxy creates two WebSocket endpoints:

/inspector/device

WebSocket endpoint for React Native devices to connect to the proxy. Devices connect here and provide device information via query parameters.

Query Parameters:

  • device - Device ID (optional, auto-generated if not provided)
  • name - Device name (optional, defaults to "Unknown")
  • app - App name (optional, defaults to "Unknown")

/inspector/debug

WebSocket endpoint for debuggers (Chrome DevTools) to connect to specific device pages.

Query Parameters:

  • device - Device ID (required)
  • page - Page ID (required)

Configuration

// WebSocket endpoint paths
const WS_DEVICE_URL = "/inspector/device";
const WS_DEBUGGER_URL = "/inspector/debug";

// HTTP endpoint paths
const PAGES_LIST_JSON_URL = "/json";
const PAGES_LIST_JSON_URL_2 = "/json/list";
const PAGES_LIST_JSON_VERSION_URL = "/json/version";

// Configuration constants
const PAGES_POLLING_INTERVAL = 1000; // milliseconds - device page polling frequency
const INTERNAL_ERROR_CODE = 1011;    // WebSocket error code for internal errors

// Additional constants
const REACT_NATIVE_RELOADABLE_PAGE_ID = "-1"; // Special page ID for reloadable React Native debugging
const EMULATOR_LOCALHOST_ADDRESSES = ["10.0.2.2", "10.0.3.2"]; // Android emulator addresses
const FILE_PREFIX = "file://"; // Prefix added to alphanumeric script IDs for Chrome DevTools compatibility

docs

cli-interface.md

device-management.md

index.md

inspector-proxy.md

tile.json