Files
react-native/packages/dev-middleware
Cedric van Putten eeb6122f39 fix(dev-middleware): respond with status code 200 when launching RNDT (#46814)
Summary:
This fixes an issue where `POST /open-debugger?appId&device&target` does not return a proper status code, meaning that the request will never be answered and clients might hang until the request timeout is hit.

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[GENERAL] [FIXED] - Respond with status code `200` when successfully launching RNDT

Pull Request resolved: https://github.com/facebook/react-native/pull/46814

Test Plan:
- `curl -v -X POST "<deviceUrl>"`
- This should show a proper response for the request.

before | after
 --- | ---
![image](https://github.com/user-attachments/assets/5b820acd-1168-4642-90ec-f2eeec0afc16) | ![image](https://github.com/user-attachments/assets/82bb2a6c-3c7b-483f-a4a1-ad00e5ca0178)

Reviewed By: NickGerleman

Differential Revision: D63837025

Pulled By: huntie

fbshipit-source-id: ac72fc793e015f0eec498f4a35b4fb9e301c5b32
2024-10-04 02:45:59 -07:00
..
2024-09-12 09:23:23 -07:00

@react-native/dev-middleware

npm package

Dev server middleware supporting core React Native development features. This package is preconfigured in all React Native projects.

Usage

Middleware can be attached to a dev server (e.g. Metro) using the createDevMiddleware API.

import { createDevMiddleware } from '@react-native/dev-middleware';

function myDevServerImpl(args) {
  ...

  const {middleware, websocketEndpoints} = createDevMiddleware({
    projectRoot: metroConfig.projectRoot,
    serverBaseUrl: `http://${args.host}:${args.port}`,
    logger,
  });

  await Metro.runServer(metroConfig, {
    host: args.host,
    ...,
    unstable_extraMiddleware: [
      middleware,
      // Optionally extend with additional HTTP middleware
    ],
    websocketEndpoints: {
      ...websocketEndpoints,
      // Optionally extend with additional WebSocket endpoints
    },
  });
}

Included middleware

@react-native/dev-middleware is designed for integrators such as @expo/dev-server and @react-native/community-cli-plugin. It provides a common default implementation for core React Native dev server responsibilities.

We intend to keep this to a narrow set of functionality, based around:

  • Debugging — The Chrome DevTools protocol (CDP) endpoints supported by React Native, including the Inspector Proxy, which facilitates connections with multiple devices.
  • Dev actions — Endpoints implementing core Dev Menu actions, e.g. reloading the app, opening the debugger frontend.

HTTP endpoints

DevMiddlewareAPI.middleware

These are exposed as a connect middleware handler, assignable to Metro.runServer or other compatible HTTP servers.

GET /json/list, /json (CDP)

Returns the list of available WebSocket targets for all connected React Native app sessions.

GET /json/version (CDP)

Returns version metadata used by Chrome DevTools.

GET /debugger-frontend

Subpaths of this endpoint are reserved to serve the JavaScript debugger frontend.

POST /open-debugger

Open the JavaScript debugger for a given CDP target (direct Hermes debugging).

Example
curl -X POST 'http://localhost:8081/open-debugger?appId=com.meta.RNTester'

WebSocket endpoints

DevMiddlewareAPI.websocketEndpoints

/inspector/device

WebSocket handler for registering device connections.

/inspector/debug

WebSocket handler that proxies CDP messages to/from the corresponding device.

Contributing

Changes to this package can be made locally and tested against the rn-tester app, per the Contributing guide. During development, this package is automatically run from source with no build step.