iOS: allow disabling packager access at runtime

Summary:
Allow disabling packager access based on the app runtime environment, e.g. running tests.

Changelog: [Internal]

Differential Revision: D27903019

fbshipit-source-id: cafa25e93efab3cf8e96d60a8fc03de8abb833f4
This commit is contained in:
Kevin Gozali
2021-04-22 08:15:28 -07:00
committed by Facebook GitHub Bot
parent 96930fad13
commit 6155ad0438
2 changed files with 26 additions and 8 deletions
+11 -8
View File
@@ -7,16 +7,19 @@
#import <Foundation/Foundation.h>
#if defined(__cplusplus)
extern "C" {
#endif
#import "RCTDefines.h"
extern NSString *const RCTBundleURLProviderUpdatedNotification;
RCT_EXTERN NSString *const RCTBundleURLProviderUpdatedNotification;
RCT_EXTERN const NSUInteger kRCTBundleURLProviderDefaultPort;
extern const NSUInteger kRCTBundleURLProviderDefaultPort;
#if defined(__cplusplus)
}
#if RCT_DEV_MENU
/**
* Allow/disallow accessing the packager server for various runtime scenario.
* For instance, if a test run should never access the packager, disable it
* by calling this function before initializing React Native (RCTBridge etc).
* By default the access is enabled.
*/
RCT_EXTERN void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed);
#endif
@interface RCTBundleURLProvider : NSObject
+15
View File
@@ -9,11 +9,20 @@
#import "RCTConvert.h"
#import "RCTDefines.h"
#import "RCTLog.h"
NSString *const RCTBundleURLProviderUpdatedNotification = @"RCTBundleURLProviderUpdatedNotification";
const NSUInteger kRCTBundleURLProviderDefaultPort = RCT_METRO_PORT;
#if RCT_DEV_MENU
static BOOL kRCTAllowPackagerAccess = YES;
void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed)
{
kRCTAllowPackagerAccess = allowed;
}
#endif
static NSString *const kRCTJsLocationKey = @"RCT_jsLocation";
static NSString *const kRCTEnableDevKey = @"RCT_enableDev";
static NSString *const kRCTEnableMinificationKey = @"RCT_enableMinification";
@@ -127,6 +136,12 @@ static NSURL *serverRootWithHostPort(NSString *hostPort)
- (NSString *)packagerServerHostPort
{
#if RCT_DEV_MENU
if (!kRCTAllowPackagerAccess) {
RCTLogInfo(@"Packager server access is disabled in this environment");
return nil;
}
#endif
NSString *location = [self jsLocation];
#if RCT_DEV_MENU
if ([location length] && ![RCTBundleURLProvider isPackagerRunning:location]) {