From 7d44959940b7f7b03feefde0e9a15382f04dad6d Mon Sep 17 00:00:00 2001 From: Jimmy Zhang Date: Fri, 28 Aug 2020 15:58:45 -0700 Subject: [PATCH] Respect port information if available from RCTBundleURLProvider Summary: Changelog: Sometimes a port different than kRCTBundleURLProviderDefaultPort (8081) can be specified to RCTBundleURLProvider for packager checking or requesting resources through saving them in JSLocation, this adds support for that rather than always falling back to kRCTBundleURLProviderDefaultPort Reviewed By: PeteTheHeat Differential Revision: D23395548 fbshipit-source-id: b7a6f0816d1f226a2e3fb82bf2dc0ab9e79ef966 --- React/Base/RCTBundleURLProvider.h | 12 +++++++- React/Base/RCTBundleURLProvider.mm | 28 +++++++++++-------- .../DevSupport/RCTInspectorDevServerHelper.mm | 4 ++- React/DevSupport/RCTPackagerConnection.mm | 27 ++++++++++++------ 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/React/Base/RCTBundleURLProvider.h b/React/Base/RCTBundleURLProvider.h index 48a115e94f0748..5dc5400d664cd0 100644 --- a/React/Base/RCTBundleURLProvider.h +++ b/React/Base/RCTBundleURLProvider.h @@ -37,7 +37,17 @@ extern const NSUInteger kRCTBundleURLProviderDefaultPort; */ - (NSString *)packagerServerHost; -+ (BOOL)isPackagerRunning:(NSString *)host; +/** + * Return the server host with optional port. If its a development build and there's no jsLocation defined, + * it will return the server host IP address + */ +- (NSString *)packagerServerHostPort; + +/** + * Returns if there's a packager running at the given host port. + * The port is optional, if not specified, kRCTBundleURLProviderDefaultPort will be used + */ ++ (BOOL)isPackagerRunning:(NSString *)hostPort; /** * Returns the jsBundleURL for a given bundle entrypoint and diff --git a/React/Base/RCTBundleURLProvider.mm b/React/Base/RCTBundleURLProvider.mm index 146280bf717507..d43e2a0e96954c 100644 --- a/React/Base/RCTBundleURLProvider.mm +++ b/React/Base/RCTBundleURLProvider.mm @@ -76,9 +76,9 @@ - (void)resetToDefaults } #if RCT_DEV_MENU -+ (BOOL)isPackagerRunning:(NSString *)host ++ (BOOL)isPackagerRunning:(NSString *)hostPort { - NSURL *url = [serverRootWithHostPort(host) URLByAppendingPathComponent:@"status"]; + NSURL *url = [serverRootWithHostPort(hostPort) URLByAppendingPathComponent:@"status"]; NSURLSession *session = [NSURLSession sharedSession]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; @@ -116,7 +116,7 @@ - (NSString *)guessPackagerHost return nil; } #else -+ (BOOL)isPackagerRunning:(NSString *)host ++ (BOOL)isPackagerRunning:(NSString *)hostPort { return false; } @@ -124,13 +124,19 @@ + (BOOL)isPackagerRunning:(NSString *)host - (NSString *)packagerServerHost { - NSString *location = [self jsLocation]; + NSString *location = [self packagerServerHostPort]; if (location) { NSInteger index = [location rangeOfString:@":"].location; if (index != NSNotFound) { location = [location substringToIndex:index]; } } + return location; +} + +- (NSString *)packagerServerHostPort +{ + NSString *location = [self jsLocation]; #if RCT_DEV_MENU if ([location length] && ![RCTBundleURLProvider isPackagerRunning:location]) { location = nil; @@ -150,12 +156,12 @@ - (NSString *)packagerServerHost - (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot fallbackURLProvider:(NSURL * (^)(void))fallbackURLProvider { - NSString *packagerServerHost = [self packagerServerHost]; - if (!packagerServerHost) { + NSString *packagerServerHostPort = [self packagerServerHostPort]; + if (!packagerServerHostPort) { return fallbackURLProvider(); } else { return [RCTBundleURLProvider jsBundleURLForBundleRoot:bundleRoot - packagerHost:packagerServerHost + packagerHost:packagerServerHostPort enableDev:[self enableDev] enableMinification:[self enableMinification]]; } @@ -164,7 +170,7 @@ - (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot fallbackURLProvider:( - (NSURL *)jsBundleURLForSplitBundleRoot:(NSString *)bundleRoot { return [RCTBundleURLProvider jsBundleURLForBundleRoot:bundleRoot - packagerHost:[self packagerServerHost] + packagerHost:[self packagerServerHostPort] enableDev:[self enableDev] enableMinification:[self enableMinification] modulesOnly:YES @@ -198,14 +204,14 @@ - (NSURL *)resourceURLForResourceRoot:(NSString *)root resourceExtension:(NSString *)extension offlineBundle:(NSBundle *)offlineBundle { - NSString *packagerServerHost = [self packagerServerHost]; - if (!packagerServerHost) { + NSString *packagerServerHostPort = [self packagerServerHostPort]; + if (!packagerServerHostPort) { // Serve offline bundle (local file) NSBundle *bundle = offlineBundle ?: [NSBundle mainBundle]; return [bundle URLForResource:name withExtension:extension]; } NSString *path = [NSString stringWithFormat:@"/%@/%@.%@", root, name, extension]; - return [[self class] resourceURLForResourcePath:path packagerHost:packagerServerHost query:nil]; + return [[self class] resourceURLForResourcePath:path packagerHost:packagerServerHostPort query:nil]; } + (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot diff --git a/React/DevSupport/RCTInspectorDevServerHelper.mm b/React/DevSupport/RCTInspectorDevServerHelper.mm index 5885f214ebb9dd..21000646fad76f 100644 --- a/React/DevSupport/RCTInspectorDevServerHelper.mm +++ b/React/DevSupport/RCTInspectorDevServerHelper.mm @@ -24,7 +24,9 @@ if (portStr && [portStr length] > 0) { port = [NSNumber numberWithInt:[portStr intValue]]; } - + if ([bundleURL port]) { + port = [bundleURL port]; + } NSString *host = [bundleURL host]; if (!host) { host = @"localhost"; diff --git a/React/DevSupport/RCTPackagerConnection.mm b/React/DevSupport/RCTPackagerConnection.mm index 3be7e65c20ac9d..4a9ed5b9995314 100644 --- a/React/DevSupport/RCTPackagerConnection.mm +++ b/React/DevSupport/RCTPackagerConnection.mm @@ -40,7 +40,7 @@ @implementation RCTPackagerConnection { std::mutex _mutex; // protects all ivars RCTReconnectingWebSocket *_socket; BOOL _socketConnected; - NSString *_serverHostForSocket; + NSString *_serverHostPortForSocket; id _bundleURLChangeObserver; uint32_t _nextToken; std::vector> _notificationRegistrations; @@ -62,8 +62,8 @@ - (instancetype)init { if (self = [super init]) { _nextToken = 1; // Prevent randomly erasing a handler if you pass a bogus 0 token - _serverHostForSocket = [[RCTBundleURLProvider sharedSettings] packagerServerHost]; - _socket = socketForLocation(_serverHostForSocket); + _serverHostPortForSocket = [[RCTBundleURLProvider sharedSettings] packagerServerHostPort]; + _socket = socketForLocation(_serverHostPortForSocket); _socket.delegate = self; [_socket start]; @@ -79,12 +79,21 @@ - (instancetype)init return self; } -static RCTReconnectingWebSocket *socketForLocation(NSString *const serverHost) +static RCTReconnectingWebSocket *socketForLocation(NSString *const serverHostPort) { + NSString *serverHost; + NSString *serverPort; + NSArray *locationComponents = [serverHostPort componentsSeparatedByString:@":"]; + if ([locationComponents count] > 0) { + serverHost = locationComponents[0]; + } + if ([locationComponents count] > 1) { + serverPort = locationComponents[1]; + } NSURLComponents *const components = [NSURLComponents new]; components.host = serverHost ?: @"localhost"; components.scheme = @"http"; - components.port = @(kRCTBundleURLProviderDefaultPort); + components.port = serverPort ? @(serverPort.integerValue) : @(kRCTBundleURLProviderDefaultPort); components.path = @"/message"; components.queryItems = @[ [NSURLQueryItem queryItemWithName:@"role" value:@"ios"] ]; static dispatch_queue_t queue; @@ -118,15 +127,15 @@ - (void)bundleURLSettingsChanged return; // already stopped } - NSString *const serverHost = [[RCTBundleURLProvider sharedSettings] packagerServerHost]; - if ([serverHost isEqual:_serverHostForSocket]) { + NSString *const serverHostPort = [[RCTBundleURLProvider sharedSettings] packagerServerHostPort]; + if ([serverHostPort isEqual:_serverHostPortForSocket]) { return; // unchanged } _socket.delegate = nil; [_socket stop]; - _serverHostForSocket = serverHost; - _socket = socketForLocation(serverHost); + _serverHostPortForSocket = serverHostPort; + _socket = socketForLocation(serverHostPort); _socket.delegate = self; [_socket start]; }