Files
react-native/packages/dev-middleware/src/utils/getDevToolsFrontendUrl.js
T
Rob Hogan acf384a72e dev-middleware: Redefine "serverBaseUrl" as server-relative, '/json/list' by requestor (#47628)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47628

`serverBaseUrl` is currently documented as:

> The base URL to the dev server, as addressible from the local developer machine

This is problematic in general because `dev-middleware` on a server doesn't necessarily know about where clients might be reaching it from, how tunnels or port-forwards are set up, etc., and this can change over the lifetime of the server and vary between clients.

Indeed, our own use of `serverBaseUrl` from both `community-cli-plugin` and internally simply sets it to the host and port the dev server is listening on - ie it's the address of the dev server accessible *from the server*.

This PR changes the docs, redefining `serverBaseUrl`, to match the way we currently specify it.

One usage where we *do* want the previously documented behaviour is in responses to `/json/list` (`getPageDescriptions`) where the URLs in the response should be reachable by a browser requesting `/json/list`.

Here, we use the request (host header, etc.) to attempt to get working base URL.

History:
It should be mentioned that this is the latest in a series of changes like this:
 - https://github.com/facebook/react-native/pull/39394
 - https://github.com/facebook/react-native/pull/39456

Learning from those:
 - This change does *not* break Android emulators, which routes `10.0.2.2` to localhost, or other routed devices, because `/open-debugger` still uses server-relative URLs, and now formally delegates to `BrowserLauncher` to decide what to do with those URLs (internally, VSCode / `xdg-open` handles port forwarding)
 - Middleware configuration is no longer required to specify how it is reachable from clients.

This sets up some subsequent changes for more robust handling of tunnelled connections.

Changelog:
[General][Breaking] dev-middleware: Frameworks should specify `serverBaseUrl` relative to the middleware host.

Reviewed By: huntie

Differential Revision: D65974487

fbshipit-source-id: 1face8fc7715df387f75b329e80932d8543ee419
2024-11-18 13:46:50 -08:00

82 lines
2.3 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import type {Experiments} from '../types/Experiments';
/**
* Get the DevTools frontend URL to debug a given React Native CDP target.
*/
export default function getDevToolsFrontendUrl(
experiments: Experiments,
webSocketDebuggerUrl: string,
devServerUrl: string,
options?: $ReadOnly<{
relative?: boolean,
launchId?: string,
/** Whether to use the modern `rn_fusebox.html` entry point. */
useFuseboxEntryPoint?: boolean,
}>,
): string {
const wsParam = getWsParam({
webSocketDebuggerUrl,
devServerUrl,
});
const appUrl =
(options?.relative === true ? '' : devServerUrl) +
'/debugger-frontend/' +
(options?.useFuseboxEntryPoint === true
? 'rn_fusebox.html'
: 'rn_inspector.html');
const searchParams = new URLSearchParams([
[wsParam.key, wsParam.value],
['sources.hide_add_folder', 'true'],
]);
if (experiments.enableNetworkInspector) {
searchParams.append('unstable_enableNetworkPanel', 'true');
}
if (options?.launchId != null && options.launchId !== '') {
searchParams.append('launchId', options.launchId);
}
return appUrl + '?' + searchParams.toString();
}
function getWsParam({
webSocketDebuggerUrl,
devServerUrl,
}: $ReadOnly<{
webSocketDebuggerUrl: string,
devServerUrl: string,
}>): {
key: string,
value: string,
} {
const wsUrl = new URL(webSocketDebuggerUrl);
const serverHost = new URL(devServerUrl).host;
let value;
if (wsUrl.host === serverHost) {
// Use a path-absolute (host-relative) URL if the WS server and frontend
// server are colocated. This is more robust for cases where the frontend
// may actually load through a tunnel or proxy, and the WS connection
// should therefore do the same.
//
// Depends on https://github.com/facebookexperimental/rn-chrome-devtools-frontend/pull/4
value = wsUrl.pathname + wsUrl.search + wsUrl.hash;
} else {
// Standard URL format accepted by the DevTools frontend
value = wsUrl.host + wsUrl.pathname + wsUrl.search + wsUrl.hash;
}
const key = wsUrl.protocol.slice(0, -1);
return {key, value};
}