Skip to content

Commit 6155ad0

Browse files
fkgozalifacebook-github-bot
authored andcommittedApr 22, 2021
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
1 parent 96930fa commit 6155ad0

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed
 

‎React/Base/RCTBundleURLProvider.h

+11-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77

88
#import <Foundation/Foundation.h>
99

10-
#if defined(__cplusplus)
11-
extern "C" {
12-
#endif
13-
14-
extern NSString *const RCTBundleURLProviderUpdatedNotification;
10+
#import "RCTDefines.h"
1511

16-
extern const NSUInteger kRCTBundleURLProviderDefaultPort;
12+
RCT_EXTERN NSString *const RCTBundleURLProviderUpdatedNotification;
13+
RCT_EXTERN const NSUInteger kRCTBundleURLProviderDefaultPort;
1714

18-
#if defined(__cplusplus)
19-
}
15+
#if RCT_DEV_MENU
16+
/**
17+
* Allow/disallow accessing the packager server for various runtime scenario.
18+
* For instance, if a test run should never access the packager, disable it
19+
* by calling this function before initializing React Native (RCTBridge etc).
20+
* By default the access is enabled.
21+
*/
22+
RCT_EXTERN void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed);
2023
#endif
2124

2225
@interface RCTBundleURLProvider : NSObject

‎React/Base/RCTBundleURLProvider.mm

+15
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@
99

1010
#import "RCTConvert.h"
1111
#import "RCTDefines.h"
12+
#import "RCTLog.h"
1213

1314
NSString *const RCTBundleURLProviderUpdatedNotification = @"RCTBundleURLProviderUpdatedNotification";
1415

1516
const NSUInteger kRCTBundleURLProviderDefaultPort = RCT_METRO_PORT;
1617

18+
#if RCT_DEV_MENU
19+
static BOOL kRCTAllowPackagerAccess = YES;
20+
void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed)
21+
{
22+
kRCTAllowPackagerAccess = allowed;
23+
}
24+
#endif
25+
1726
static NSString *const kRCTJsLocationKey = @"RCT_jsLocation";
1827
static NSString *const kRCTEnableDevKey = @"RCT_enableDev";
1928
static NSString *const kRCTEnableMinificationKey = @"RCT_enableMinification";
@@ -127,6 +136,12 @@ - (NSString *)packagerServerHost
127136

128137
- (NSString *)packagerServerHostPort
129138
{
139+
#if RCT_DEV_MENU
140+
if (!kRCTAllowPackagerAccess) {
141+
RCTLogInfo(@"Packager server access is disabled in this environment");
142+
return nil;
143+
}
144+
#endif
130145
NSString *location = [self jsLocation];
131146
#if RCT_DEV_MENU
132147
if ([location length] && ![RCTBundleURLProvider isPackagerRunning:location]) {

0 commit comments

Comments
 (0)
Please sign in to comment.