Skip to content

Commit c82cf64

Browse files
huntiefacebook-github-bot
authored andcommitted
Move metro-inspector-proxy into dev-middleware (#39045)
Summary: Pull Request resolved: #39045 ## Context RFC: Decoupling Flipper from React Native core: react-native-community/discussions-and-proposals#641 ## Changes - Relocates `metro-inspector-proxy` source from the Metro repo into the React Native repo as part of the `react-native/dev-middleware` package. - Drops the `runInspectorProxy` entry point. - Attaches the Inspector Proxy to the `createDevMiddleware()` API as the new integration point for this functionality. - Documents migrated endpoints + usage of `createDevMiddleware()` in README. Changelog: [Internal] Metro changelog: None (`metro-inspector-proxy` is now an internal component of `react-native`, covered in the [release notes for 0.78.1](https://github.com/facebook/metro/releases/tag/v0.78.1)) Reviewed By: motiz88, blakef Differential Revision: D48066213 fbshipit-source-id: 3fbef5d881f6f451cb5955dcbbc362c53347437e
1 parent 2ec920e commit c82cf64

File tree

10 files changed

+1133
-16
lines changed

10 files changed

+1133
-16
lines changed

flow-typed/npm/@react-native-community/cli-server-api_v12.x.x.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ declare module '@react-native-community/cli-server-api' {
2323
): {
2424
middleware: Server,
2525
websocketEndpoints: {
26-
'/debugger-proxy': ws$WebSocketServer,
27-
'/message': ws$WebSocketServer,
28-
'/events': ws$WebSocketServer,
26+
[path: string]: ws$WebSocketServer,
2927
},
3028
debuggerProxyEndpoint: {
3129
server: ws$WebSocketServer,

flow-typed/npm/debug_v2.x.x.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
11+
// https://github.com/visionmedia/debug
12+
// https://www.npmjs.com/package/debug
13+
14+
declare module 'debug' {
15+
declare module.exports: (namespace: string) => (...Array<mixed>) => void;
16+
}

packages/dev-middleware/README.md

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,82 @@
11
# @react-native/dev-middleware
22

3-
![https://img.shields.io/npm/v/@react-native/dev-middleware?color=brightgreen&label=npm%20package](https://www.npmjs.com/package/@react-native/dev-middleware)
3+
![npm package](https://img.shields.io/npm/v/@react-native/dev-middleware?color=brightgreen&label=npm%20package)
44

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

7-
## Endpoints
7+
## Usage
88

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`
1063

1164
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.

packages/dev-middleware/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dependencies": {
2525
"chrome-launcher": "^0.15.2",
2626
"connect": "^3.6.5",
27+
"debug": "^2.2.0",
2728
"node-fetch": "^2.2.0",
2829
"temp-dir": "^2.0.0"
2930
},

packages/dev-middleware/src/createDevMiddleware.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
* @oncall react_native
1010
*/
@@ -14,18 +14,36 @@ import type {Logger} from './types/Logger';
1414

1515
import connect from 'connect';
1616
import openDebuggerMiddleware from './middleware/openDebuggerMiddleware';
17+
import InspectorProxy from './inspector-proxy/InspectorProxy';
1718

1819
type Options = $ReadOnly<{
20+
host: string,
21+
port: number,
22+
projectRoot: string,
1923
logger?: Logger,
2024
}>;
2125

22-
export default function createDevMiddleware({logger}: Options = {}): {
26+
type DevMiddlewareAPI = $ReadOnly<{
2327
middleware: NextHandleFunction,
24-
} {
25-
const middleware = connect().use(
26-
'/open-debugger',
27-
openDebuggerMiddleware({logger}),
28-
);
28+
websocketEndpoints: {[path: string]: ws$WebSocketServer},
29+
}>;
30+
31+
export default function createDevMiddleware({
32+
host,
33+
port,
34+
projectRoot,
35+
logger,
36+
}: Options): DevMiddlewareAPI {
37+
const inspectorProxy = new InspectorProxy(projectRoot);
38+
39+
const middleware = connect()
40+
.use('/open-debugger', openDebuggerMiddleware({logger}))
41+
.use((...args) => inspectorProxy.processRequest(...args));
2942

30-
return {middleware};
43+
return {
44+
middleware,
45+
websocketEndpoints: inspectorProxy.createWebSocketListeners(
46+
`${host}:${port}`,
47+
),
48+
};
3149
}

packages/dev-middleware/src/index.flow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
* @oncall react_native
1010
*/

0 commit comments

Comments
 (0)