From 6fc9f9d46d104dc2d702d901bcfbe71cd1630177 Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Tue, 8 Aug 2023 14:50:34 -0700 Subject: [PATCH] Backout "Remove runInspectorProxy option" Summary: Backs out D48066221. This change caused direct debugging of Hermes in Flipper and Chrome inspector to stop working. Reviewed By: motiz88 Differential Revision: D48158187 fbshipit-source-id: c47b6d874918d8daabef6407386a9e0b5494294b --- .../__snapshots__/loadConfig-test.js.snap | 4 ++++ packages/metro-config/src/configTypes.flow.js | 2 ++ packages/metro-config/src/defaults/index.js | 1 + packages/metro-config/src/loadConfig.js | 6 ++++++ packages/metro-config/types/configTypes.d.ts | 2 ++ packages/metro/package.json | 1 + packages/metro/src/index.flow.js | 18 ++++++++++++++++++ packages/metro/types/index.d.ts | 1 + 8 files changed, 35 insertions(+) diff --git a/packages/metro-config/src/__tests__/__snapshots__/loadConfig-test.js.snap b/packages/metro-config/src/__tests__/__snapshots__/loadConfig-test.js.snap index 5156df2dbb..89f21b7fb6 100644 --- a/packages/metro-config/src/__tests__/__snapshots__/loadConfig-test.js.snap +++ b/packages/metro-config/src/__tests__/__snapshots__/loadConfig-test.js.snap @@ -101,6 +101,7 @@ Object { "enhanceMiddleware": [Function], "port": 8081, "rewriteRequestUrl": [Function], + "runInspectorProxy": true, "unstable_serverRoot": null, "useGlobalHotkey": true, "verifyConnections": false, @@ -278,6 +279,7 @@ Object { "enhanceMiddleware": [Function], "port": 8081, "rewriteRequestUrl": [Function], + "runInspectorProxy": true, "unstable_serverRoot": null, "useGlobalHotkey": true, "verifyConnections": false, @@ -455,6 +457,7 @@ Object { "enhanceMiddleware": [Function], "port": 8081, "rewriteRequestUrl": [Function], + "runInspectorProxy": true, "unstable_serverRoot": null, "useGlobalHotkey": true, "verifyConnections": false, @@ -632,6 +635,7 @@ Object { "enhanceMiddleware": [Function], "port": 8081, "rewriteRequestUrl": [Function], + "runInspectorProxy": true, "unstable_serverRoot": null, "useGlobalHotkey": true, "verifyConnections": false, diff --git a/packages/metro-config/src/configTypes.flow.js b/packages/metro-config/src/configTypes.flow.js index 6dafad4ea4..29542dd352 100644 --- a/packages/metro-config/src/configTypes.flow.js +++ b/packages/metro-config/src/configTypes.flow.js @@ -168,6 +168,7 @@ type ServerConfigT = { enhanceMiddleware: (Middleware, MetroServer) => Middleware | Server, port: number, rewriteRequestUrl: string => string, + runInspectorProxy: boolean, unstable_serverRoot: ?string, useGlobalHotkey: boolean, verifyConnections: boolean, @@ -261,6 +262,7 @@ export type YargArguments = $ReadOnly<{ transformer?: string, 'reset-cache'?: boolean, resetCache?: boolean, + runInspectorProxy?: boolean, verbose?: boolean, ... }>; diff --git a/packages/metro-config/src/defaults/index.js b/packages/metro-config/src/defaults/index.js index 86c9be1f75..e30928c8c1 100644 --- a/packages/metro-config/src/defaults/index.js +++ b/packages/metro-config/src/defaults/index.js @@ -76,6 +76,7 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({ enhanceMiddleware: (middleware, _) => middleware, port: 8081, rewriteRequestUrl: url => url, + runInspectorProxy: true, unstable_serverRoot: null, useGlobalHotkey: true, verifyConnections: false, diff --git a/packages/metro-config/src/loadConfig.js b/packages/metro-config/src/loadConfig.js index d2c65b0438..22517d5714 100644 --- a/packages/metro-config/src/loadConfig.js +++ b/packages/metro-config/src/loadConfig.js @@ -234,6 +234,12 @@ function overrideConfigWithArguments( output.server.port = Number(argv.port); } + if (argv.runInspectorProxy != null) { + // $FlowFixMe[incompatible-use] + // $FlowFixMe[cannot-write] + output.server.runInspectorProxy = Boolean(argv.runInspectorProxy); + } + if (argv.projectRoot != null) { output.projectRoot = argv.projectRoot; } diff --git a/packages/metro-config/types/configTypes.d.ts b/packages/metro-config/types/configTypes.d.ts index 8d6e0d6721..c4a89aabdf 100644 --- a/packages/metro-config/types/configTypes.d.ts +++ b/packages/metro-config/types/configTypes.d.ts @@ -166,6 +166,7 @@ export interface ServerConfigT { ) => Middleware | Server; port: number; rewriteRequestUrl: (url: string) => string; + runInspectorProxy: boolean; unstable_serverRoot: string | null; useGlobalHotkey: boolean; verifyConnections: boolean; @@ -250,5 +251,6 @@ export interface YargArguments { transformer?: string; 'reset-cache'?: boolean; resetCache?: boolean; + runInspectorProxy?: boolean; verbose?: boolean; } diff --git a/packages/metro/package.json b/packages/metro/package.json index 15509b9aa5..f1941a2f8d 100644 --- a/packages/metro/package.json +++ b/packages/metro/package.json @@ -41,6 +41,7 @@ "metro-config": "0.78.0", "metro-core": "0.78.0", "metro-file-map": "0.78.0", + "metro-inspector-proxy": "0.78.0", "metro-minify-terser": "0.78.0", "metro-resolver": "0.78.0", "metro-runtime": "0.78.0", diff --git a/packages/metro/src/index.flow.js b/packages/metro/src/index.flow.js index 0e751e7788..4ba27a4db3 100644 --- a/packages/metro/src/index.flow.js +++ b/packages/metro/src/index.flow.js @@ -47,6 +47,7 @@ const { resolveConfig, } = require('metro-config'); const {Terminal} = require('metro-core'); +const {InspectorProxy} = require('metro-inspector-proxy'); const net = require('net'); const {parse} = require('url'); @@ -67,6 +68,7 @@ export type RunServerOptions = $ReadOnly<{ host?: string, onError?: (Error & {code?: string}) => void, onReady?: (server: HttpServer | HttpsServer) => void, + runInspectorProxy?: boolean, secureServerOptions?: Object, secure?: boolean, // deprecated secureCert?: string, // deprecated @@ -279,6 +281,11 @@ exports.runServer = async ( serverApp.use(handler); } + let inspectorProxy: ?InspectorProxy = null; + if (config.server.runInspectorProxy) { + inspectorProxy = new InspectorProxy(config.projectRoot); + } + let httpServer; if (secure || secureServerOptions != null) { @@ -315,6 +322,9 @@ exports.runServer = async ( websocketEndpoints = { ...websocketEndpoints, + ...(inspectorProxy + ? {...inspectorProxy.createWebSocketListeners(httpServer)} + : {}), '/hot': createWebsocketServer({ websocketServer: new MetroHmrServer( metroServer.getBundler(), @@ -340,6 +350,14 @@ exports.runServer = async ( } }); + if (inspectorProxy) { + // TODO(hypuk): Refactor inspectorProxy.processRequest into separate request handlers + // so that we could provide routes (/json/list and /json/version) here. + // Currently this causes Metro to give warning about T31407894. + // $FlowFixMe[method-unbinding] added when improving typing for this parameters + serverApp.use(inspectorProxy.processRequest.bind(inspectorProxy)); + } + resolve(httpServer); }); diff --git a/packages/metro/types/index.d.ts b/packages/metro/types/index.d.ts index 62dee34c60..8238e20145 100644 --- a/packages/metro/types/index.d.ts +++ b/packages/metro/types/index.d.ts @@ -62,6 +62,7 @@ export interface RunServerOptions { host?: string; onError?: (error: Error & {code?: string}) => void; onReady?: (server: HttpServer | HttpsServer) => void; + runInspectorProxy?: boolean; secureServerOptions?: Record; /** @deprecated since version 0.61 */