Skip to content

Commit

Permalink
Respect port information if available from RCTBundleURLProvider
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jimmy623 authored and facebook-github-bot committed Aug 28, 2020
1 parent 54e19a6 commit 7d44959
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
12 changes: 11 additions & 1 deletion React/Base/RCTBundleURLProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 17 additions & 11 deletions React/Base/RCTBundleURLProvider.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -116,21 +116,27 @@ - (NSString *)guessPackagerHost
return nil;
}
#else
+ (BOOL)isPackagerRunning:(NSString *)host
+ (BOOL)isPackagerRunning:(NSString *)hostPort
{
return false;
}
#endif

- (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;
Expand All @@ -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]];
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion React/DevSupport/RCTInspectorDevServerHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
27 changes: 18 additions & 9 deletions React/DevSupport/RCTPackagerConnection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<Registration<RCTNotificationHandler>> _notificationRegistrations;
Expand All @@ -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];

Expand All @@ -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;
Expand Down Expand Up @@ -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];
}
Expand Down

0 comments on commit 7d44959

Please sign in to comment.