|
1 | 1 | # @react-native/dev-middleware
|
2 | 2 |
|
3 |
| - |
| 3 | + |
4 | 4 |
|
5 | 5 | Dev server middleware supporting core React Native development features. This package is preconfigured in all React Native projects.
|
6 | 6 |
|
7 |
| -## Endpoints |
| 7 | +## Usage |
8 | 8 |
|
9 |
| -### `/open-debugger` |
| 9 | +Middleware can be attached to a dev server (e.g. [Metro](https://facebook.github.io/metro/docs/getting-started)) using the `createDevMiddleware` API. |
| 10 | + |
| 11 | +```js |
| 12 | +import { createDevMiddleware } from '@react-native/dev-middleware'; |
| 13 | + |
| 14 | +function myDevServerImpl(args) { |
| 15 | + ... |
| 16 | + |
| 17 | + const {middleware, websocketEndpoints} = createDevMiddleware({ |
| 18 | + host: args.host, |
| 19 | + port: metroConfig.server.port, |
| 20 | + projectRoot: metroConfig.projectRoot, |
| 21 | + logger, |
| 22 | + }); |
| 23 | + |
| 24 | + await Metro.runServer(metroConfig, { |
| 25 | + host: args.host, |
| 26 | + ..., |
| 27 | + unstable_extraMiddleware: [ |
| 28 | + middleware, |
| 29 | + // Optionally extend with additional HTTP middleware |
| 30 | + ], |
| 31 | + websocketEndpoints: { |
| 32 | + ...websocketEndpoints, |
| 33 | + // Optionally extend with additional WebSocket endpoints |
| 34 | + }, |
| 35 | + }); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +## Included middleware |
| 40 | + |
| 41 | +`@react-native/dev-middleware` is designed for integrators such as [`@expo/dev-server`](https://www.npmjs.com/package/@expo/dev-server) and [`@react-native/community-cli-plugin`](https://github.com/facebook/react-native/tree/main/packages/community-cli-plugin). It provides a common default implementation for core React Native dev server responsibilities. |
| 42 | + |
| 43 | +We intend to keep this to a narrow set of functionality, based around: |
| 44 | + |
| 45 | +- **Debugging** — The [Chrome DevTools protocol (CDP)](https://chromedevtools.github.io/devtools-protocol/) endpoints supported by React Native, including the Inspector Proxy, which facilitates connections with multiple devices. |
| 46 | +- **Dev actions** — Endpoints implementing core [Dev Menu](https://reactnative.dev/docs/debugging#accessing-the-dev-menu) actions, e.g. reloading the app, opening the debugger frontend. |
| 47 | + |
| 48 | +### HTTP endpoints |
| 49 | + |
| 50 | +<small>`DevMiddlewareAPI.middleware`</small> |
| 51 | + |
| 52 | +These are exposed as a [`connect`](https://www.npmjs.com/package/connect) middleware handler, assignable to `Metro.runServer` or other compatible HTTP servers. |
| 53 | + |
| 54 | +#### GET `/json/list`, `/json` ([CDP](https://chromedevtools.github.io/devtools-protocol/#endpoints)) |
| 55 | + |
| 56 | +Returns the list of available WebSocket targets for all connected React Native app sessions. |
| 57 | + |
| 58 | +#### GET `/json/version` ([CDP](https://chromedevtools.github.io/devtools-protocol/#endpoints)) |
| 59 | + |
| 60 | +Returns version metadata used by Chrome DevTools. |
| 61 | + |
| 62 | +#### POST `/open-debugger` |
10 | 63 |
|
11 | 64 | Open the JavaScript debugger for a given CDP target (direct Hermes debugging).
|
| 65 | + |
| 66 | +<details> |
| 67 | +<summary>Example</summary> |
| 68 | + |
| 69 | + curl -X POST 'http://localhost:8081/open-debugger?appId=com.meta.RNTester' |
| 70 | +</details> |
| 71 | + |
| 72 | +### WebSocket endpoints |
| 73 | + |
| 74 | +<small>`DevMiddlewareAPI.websocketEndpoints`</small> |
| 75 | + |
| 76 | +#### `/inspector/device` |
| 77 | + |
| 78 | +WebSocket handler for registering device connections. |
| 79 | + |
| 80 | +#### `/inspector/debug` |
| 81 | + |
| 82 | +WebSocket handler that proxies CDP messages to/from the corresponding device. |
0 commit comments