From 49b7fff2a041aca5d8bfaa750d03ff26fc310b9f Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Thu, 26 Jun 2025 07:59:16 -0700 Subject: [PATCH] Support IPv6 dev server URLs in legacy standalone RDT connection Summary: Changelog: [Internal] A minimal tweak to a legacy code path for React DevTools in React Native (**NOT** Fusebox!) that enables it to work / not crash when encountering an IPv6 dev server address. See doc comment for more. Reviewed By: hoxyq Differential Revision: D77150288 fbshipit-source-id: c11c742aad7b83861a1242dd13c5ed2753fbdf29 --- .../Libraries/Core/setUpReactDevTools.js | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/react-native/Libraries/Core/setUpReactDevTools.js b/packages/react-native/Libraries/Core/setUpReactDevTools.js index e25f0a44c98..9bdf66b3018 100644 --- a/packages/react-native/Libraries/Core/setUpReactDevTools.js +++ b/packages/react-native/Libraries/Core/setUpReactDevTools.js @@ -143,10 +143,7 @@ if (__DEV__) { // Get hostname from development server (packager) const devServer = getDevServer(); const host = devServer.bundleLoadedFromServer - ? devServer.url - .replace(/https?:\/\//, '') - .replace(/\/$/, '') - .split(':')[0] + ? guessHostFromDevServerUrl(devServer.url) : 'localhost'; // Read the optional global variable for backward compatibility. @@ -259,3 +256,23 @@ function readReloadAndProfileConfig( onReloadAndProfileFlagsReset, }; } + +/** + * This is a bad, no good, broken hack to get the host from a dev server URL for the purposes + * of connecting to the legacy React DevTools socket (for the standalone react-devtools package). + * It has too many bugs to list. Please don't use it in new code. + * + * The correct implementation would just be `return new URL(url).host`, but React Native does not + * ship with a spec-compliant `URL` class yet. Alternatively, this can be deleted when we delete + * `connectToWSBasedReactDevToolsFrontend`. + */ +function guessHostFromDevServerUrl(url: string): string { + const hopefullyHostAndPort = url + .replace(/https?:\/\//, '') + .replace(/\/$/, ''); + // IPv6 addresses contain colons, so the split(':') below will return garbage. + if (hopefullyHostAndPort.includes(']')) { + return hopefullyHostAndPort.split(']')[0] + ']'; + } + return hopefullyHostAndPort.split(':')[0]; +}