Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/react-native/React/CoreModules/RCTDevMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@

RCT_EXTERN NSString *const RCTShowDevMenuNotification;

#if RCT_DEV_MENU

/**
* The bundler host and port shown in Configure Bundler. Exposed for testing.
* The host comes from the bundle URL, then `jsLocation`, then `ipFileHost`, then localhost.
* The port comes from the same source as the host, otherwise it is the default Metro port.
*/
RCT_EXTERN void RCTDevMenuGetCurrentBundler(
NSURL *bundleURL,
NSString *jsLocation,
NSString *packagerScheme,
NSString *ipFileHost,
NSString **host,
NSNumber **port);

#endif

@interface RCTDevMenuConfiguration : NSObject

#if RCT_DEV_MENU
Expand Down
32 changes: 22 additions & 10 deletions packages/react-native/React/CoreModules/RCTDevMenu.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,29 @@ - (RCTDevMenuItem *)devMenuItem;

// The bundler the app uses now, to prefill Configure Bundler: the host of the bundle URL, then the saved
// jsLocation, then ip.txt (written by react-native-xcode.sh for devices), then localhost.
static void RCTDevMenuGetCurrentBundler(NSURL *bundleURL, NSString **host, NSNumber **port)
// The port comes from the same source as the host, otherwise it is the default Metro port.
void RCTDevMenuGetCurrentBundler(
NSURL *bundleURL,
NSString *jsLocation,
NSString *packagerScheme,
NSString *ipFileHost,
NSString **host,
NSNumber **port)
{
NSString *currentHost = bundleURL.host;
NSNumber *currentPort = bundleURL.port;
RCTBundleURLProvider *bundleURLProvider = [RCTBundleURLProvider sharedSettings];

NSString *jsLocation = bundleURLProvider.jsLocation;
if (currentHost.length == 0 && jsLocation.length > 0) {
NSString *jsLocationURLString = [jsLocation containsString:@"://"]
? jsLocation
: [NSString stringWithFormat:@"%@://%@", bundleURLProvider.packagerScheme, jsLocation];
: [NSString stringWithFormat:@"%@://%@", packagerScheme, jsLocation];
NSURLComponents *jsLocationComponents = [NSURLComponents componentsWithString:jsLocationURLString];
currentHost = jsLocationComponents.host;
currentPort = jsLocationComponents.port;
}

if (currentHost.length == 0) {
NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"];
if (ipPath != nil) {
currentHost = [[NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
currentHost = [ipFileHost stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

*host = currentHost.length > 0 ? currentHost : @"localhost";
Expand Down Expand Up @@ -402,7 +403,18 @@ - (void)setDefaultJSBundle
handler:^{
NSString *currentHost = nil;
NSNumber *currentPort = nil;
RCTDevMenuGetCurrentBundler(bundleManager.bundleURL, &currentHost, &currentPort);
RCTBundleURLProvider *bundleURLProvider = [RCTBundleURLProvider sharedSettings];
NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"];
NSString *ipFileHost = ipPath != nil
? [NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil]
: nil;
RCTDevMenuGetCurrentBundler(
bundleManager.bundleURL,
bundleURLProvider.jsLocation,
bundleURLProvider.packagerScheme,
ipFileHost,
&currentHost,
&currentPort);

UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Configure Bundler"
Expand Down
4 changes: 4 additions & 0 deletions packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
E62F11832A5C6580000BF1C8 /* FlexibleSizeExampleView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 27F441E81BEBE5030039B79C /* FlexibleSizeExampleView.mm */; };
E62F11842A5C6584000BF1C8 /* UpdatePropertiesExampleView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 272E6B3C1BEA849E001FCF37 /* UpdatePropertiesExampleView.mm */; };
E7DB20D122B2BAA6005AC45F /* RCTBundleURLProviderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20A922B2BAA3005AC45F /* RCTBundleURLProviderTests.m */; };
E7DB20D222B2BAA6005AC45F /* RCTDevMenuTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20AA22B2BAA3005AC45F /* RCTDevMenuTests.m */; };
E7DB20D322B2BAA6005AC45F /* RCTBlobManagerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20AB22B2BAA3005AC45F /* RCTBlobManagerTests.m */; };
E7DB20D522B2BAA6005AC45F /* RCTPerformanceLoggerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20AD22B2BAA3005AC45F /* RCTPerformanceLoggerTests.m */; };
E7DB20D622B2BAA6005AC45F /* RCTFontTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20AF22B2BAA4005AC45F /* RCTFontTests.m */; };
Expand Down Expand Up @@ -106,6 +107,7 @@
E7DB209F22B2BA84005AC45F /* RNTesterUnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RNTesterUnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
E7DB20A322B2BA84005AC45F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E7DB20A922B2BAA3005AC45F /* RCTBundleURLProviderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTBundleURLProviderTests.m; sourceTree = "<group>"; };
E7DB20AA22B2BAA3005AC45F /* RCTDevMenuTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDevMenuTests.m; sourceTree = "<group>"; };
E7DB20AB22B2BAA3005AC45F /* RCTBlobManagerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTBlobManagerTests.m; sourceTree = "<group>"; };
E7DB20AD22B2BAA3005AC45F /* RCTPerformanceLoggerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTPerformanceLoggerTests.m; sourceTree = "<group>"; };
E7DB20AF22B2BAA4005AC45F /* RCTFontTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTFontTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -299,6 +301,7 @@
E7DB20B222B2BAA4005AC45F /* RCTAnimationUtilsTests.m */,
E7DB20AB22B2BAA3005AC45F /* RCTBlobManagerTests.m */,
E7DB20A922B2BAA3005AC45F /* RCTBundleURLProviderTests.m */,
E7DB20AA22B2BAA3005AC45F /* RCTDevMenuTests.m */,
E7DB20CA22B2BAA5005AC45F /* RCTConvert_NSURLTests.m */,
E7DB20CE22B2BAA5005AC45F /* RCTConvert_YGValueTests.m */,
383889D923A7398900D06C3E /* RCTConvert_UIColorTests.m */,
Expand Down Expand Up @@ -621,6 +624,7 @@
E7DB20D522B2BAA6005AC45F /* RCTPerformanceLoggerTests.m in Sources */,
E7DB20D922B2BAA6005AC45F /* RCTAnimationUtilsTests.m in Sources */,
E7DB20D122B2BAA6005AC45F /* RCTBundleURLProviderTests.m in Sources */,
E7DB20D222B2BAA6005AC45F /* RCTDevMenuTests.m in Sources */,
E7DB20EC22B2BAA6005AC45F /* RCTMultipartStreamReaderTests.m in Sources */,
E7DB20E022B2BAA6005AC45F /* RCTMethodArgumentTests.m in Sources */,
E7DB20E822B2BAA6005AC45F /* RCTModuleMethodTests.mm in Sources */,
Expand Down
100 changes: 100 additions & 0 deletions packages/rn-tester/RNTesterUnitTests/RCTDevMenuTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.
*/

#import <XCTest/XCTest.h>

#import <React/RCTDefines.h>
#import <React/RCTDevMenu.h>

#if RCT_DEV_MENU

@interface RCTDevMenuTests : XCTestCase
@end

@implementation RCTDevMenuTests

- (void)assertBundlerForBundleURL:(NSURL *)bundleURL
jsLocation:(NSString *)jsLocation
ipFileHost:(NSString *)ipFileHost
host:(NSString *)expectedHost
port:(NSNumber *)expectedPort
{
NSString *host = nil;
NSNumber *port = nil;
RCTDevMenuGetCurrentBundler(bundleURL, jsLocation, @"http", ipFileHost, &host, &port);
XCTAssertEqualObjects(host, expectedHost);
XCTAssertEqualObjects(port, expectedPort);
}

- (void)testBundleURLHostAndPort
{
[self assertBundlerForBundleURL:[NSURL URLWithString:@"http://192.168.1.5:8082/index.bundle?platform=ios"]
jsLocation:@"10.0.0.1:9000"
ipFileHost:@"10.0.0.2"
host:@"192.168.1.5"
port:@8082];
}

- (void)testBundleURLWithoutPortUsesDefaultPort
{
[self assertBundlerForBundleURL:[NSURL URLWithString:@"https://bundler.example.com/index.bundle?platform=ios"]
jsLocation:nil
ipFileHost:nil
host:@"bundler.example.com"
port:@(RCT_METRO_PORT)];
}

- (void)testJSLocationHostAndPortWhenBundleURLHasNoHost
{
[self assertBundlerForBundleURL:[NSURL fileURLWithPath:@"/tmp/main.jsbundle"]
jsLocation:@"192.168.1.5:8082"
ipFileHost:@"10.0.0.2"
host:@"192.168.1.5"
port:@8082];
}

- (void)testJSLocationWithoutPortDoesNotUseBundleURLPort
{
// A bundle URL with a port but no host must not lend its port to a jsLocation without one.
[self assertBundlerForBundleURL:[NSURL URLWithString:@"file://:8082/main.jsbundle"]
jsLocation:@"192.168.1.5"
ipFileHost:nil
host:@"192.168.1.5"
port:@(RCT_METRO_PORT)];
}

- (void)testJSLocationWithScheme
{
[self assertBundlerForBundleURL:nil
jsLocation:@"https://bundler.example.com:8443"
ipFileHost:nil
host:@"bundler.example.com"
port:@8443];
}

- (void)testJSLocationIPv6
{
[self assertBundlerForBundleURL:nil jsLocation:@"[::1]:8082" ipFileHost:nil host:@"[::1]" port:@8082];
}

- (void)testIPFileHostWhenNoBundleURLOrJSLocation
{
[self assertBundlerForBundleURL:nil
jsLocation:nil
ipFileHost:@"192.168.1.5\n"
host:@"192.168.1.5"
port:@(RCT_METRO_PORT)];
}

- (void)testLocalhostByDefault
{
[self assertBundlerForBundleURL:nil jsLocation:@"" ipFileHost:nil host:@"localhost" port:@(RCT_METRO_PORT)];
}

@end

#endif
Loading