Skip to content

Commit 35013f6

Browse files
authored
feat: support switching to native network interception (#1120)
1 parent 0942b38 commit 35013f6

File tree

13 files changed

+107
-10
lines changed

13 files changed

+107
-10
lines changed

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public void init(
127127
final String token,
128128
final ReadableArray invocationEventValues,
129129
final String logLevel,
130+
final boolean useNativeNetworkInterception,
130131
@Nullable final String codePushVersion
131132
) {
132133
MainThreadHandler.runOnMainThread(new Runnable() {

examples/default/ios/InstabugTests/InstabugSampleTests.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ - (void)testInit {
7070
NSString *appToken = @"app_token";
7171
NSString *codePushVersion = @"1.0.0(1)";
7272
NSArray *invocationEvents = [NSArray arrayWithObjects:[NSNumber numberWithInteger:floatingButtonInvocationEvent], nil];
73+
BOOL useNativeNetworkInterception = YES;
7374
IBGSDKDebugLogsLevel sdkDebugLogsLevel = IBGSDKDebugLogsLevelDebug;
7475

7576
OCMStub([mock setCodePushVersion:codePushVersion]);
7677

77-
[self.instabugBridge init:appToken invocationEvents:invocationEvents debugLogsLevel:sdkDebugLogsLevel codePushVersion:codePushVersion];
78+
[self.instabugBridge init:appToken invocationEvents:invocationEvents debugLogsLevel:sdkDebugLogsLevel useNativeNetworkInterception:useNativeNetworkInterception codePushVersion:codePushVersion];
7879
OCMVerify([mock setCodePushVersion:codePushVersion]);
7980

80-
OCMVerify([self.mRNInstabug initWithToken:appToken invocationEvents:floatingButtonInvocationEvent debugLogsLevel:sdkDebugLogsLevel]);
81+
OCMVerify([self.mRNInstabug initWithToken:appToken invocationEvents:floatingButtonInvocationEvent debugLogsLevel:sdkDebugLogsLevel useNativeNetworkInterception:useNativeNetworkInterception]);
8182
}
8283

8384
- (void)testSetUserData {

examples/default/ios/InstabugTests/RNInstabugTests.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ - (void)testInitWithoutLogsLevel {
4141
OCMVerify([self.mIBGNetworkLogger setEnabled:YES]);
4242
}
4343

44+
- (void)testInitWithNativeNetworkInterception {
45+
NSString *token = @"app-token";
46+
IBGInvocationEvent invocationEvents = IBGInvocationEventFloatingButton | IBGInvocationEventShake;
47+
BOOL useNativeNetworkInterception = YES;
48+
49+
[RNInstabug initWithToken:token invocationEvents:invocationEvents useNativeNetworkInterception:useNativeNetworkInterception];
50+
51+
OCMVerify([self.mInstabug startWithToken:token invocationEvents:invocationEvents]);
52+
OCMVerify([self.mInstabug setCurrentPlatform:IBGPlatformReactNative]);
53+
OCMVerify(never(), [self.mIBGNetworkLogger disableAutomaticCapturingOfNetworkLogs]);
54+
OCMVerify([self.mIBGNetworkLogger setEnabled:YES]);
55+
}
56+
4457
- (void)testInitWithLogsLevel {
4558
NSString *token = @"app-token";
4659
IBGInvocationEvent invocationEvents = IBGInvocationEventFloatingButton | IBGInvocationEventShake;

ios/RNInstabug/InstabugReactBridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
- (void)setEnabled:(BOOL)isEnabled;
2929

30-
- (void)init:(NSString *)token invocationEvents:(NSArray *)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel codePushVersion:(NSString *)codePushVersion;
30+
- (void)init:(NSString *)token invocationEvents:(NSArray *)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel useNativeNetworkInterception:(BOOL)useNativeNetworkInterception codePushVersion:(NSString *)codePushVersion;
3131

3232
- (void)setUserData:(NSString *)userData;
3333

ios/RNInstabug/InstabugReactBridge.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ - (dispatch_queue_t)methodQueue {
4040
RCT_EXPORT_METHOD(init:(NSString *)token
4141
invocationEvents:(NSArray *)invocationEventsArray
4242
debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel
43+
useNativeNetworkInterception:(BOOL)useNativeNetworkInterception
4344
codePushVersion:(NSString *)codePushVersion) {
4445
IBGInvocationEvent invocationEvents = 0;
4546

@@ -51,7 +52,8 @@ - (dispatch_queue_t)methodQueue {
5152

5253
[RNInstabug initWithToken:token
5354
invocationEvents:invocationEvents
54-
debugLogsLevel:sdkDebugLogsLevel];
55+
debugLogsLevel:sdkDebugLogsLevel
56+
useNativeNetworkInterception:useNativeNetworkInterception];
5557
}
5658

5759
RCT_EXPORT_METHOD(setReproStepsConfig:(IBGUserStepsMode)bugMode :(IBGUserStepsMode)crashMode:(IBGUserStepsMode)sessionReplayMode) {

ios/RNInstabug/RNInstabug.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
@interface RNInstabug : NSObject
77

88
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents debugLogsLevel:(IBGSDKDebugLogsLevel)debugLogsLevel;
9+
10+
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents debugLogsLevel:(IBGSDKDebugLogsLevel)debugLogsLevel
11+
useNativeNetworkInterception:(BOOL)useNativeNetworkInterception;
12+
13+
+ (void)initWithToken:(NSString *)token
14+
invocationEvents:(IBGInvocationEvent)invocationEvents
15+
useNativeNetworkInterception:(BOOL)useNativeNetworkInterception;
16+
917
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents;
1018

1119
/**

ios/RNInstabug/RNInstabug.m

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ + (void)reset {
1313
didInit = NO;
1414
}
1515

16-
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents {
16+
+ (void)initWithToken:(NSString *)token
17+
invocationEvents:(IBGInvocationEvent)invocationEvents
18+
useNativeNetworkInterception:(BOOL)useNativeNetworkInterception {
1719

1820
didInit = YES;
1921

2022
[Instabug setCurrentPlatform:IBGPlatformReactNative];
2123

22-
// Disable automatic network logging in the iOS SDK to avoid duplicate network logs coming
23-
// from both the iOS and React Native SDKs
24-
[IBGNetworkLogger disableAutomaticCapturingOfNetworkLogs];
24+
if (!useNativeNetworkInterception) {
25+
// Disable automatic network logging in the iOS SDK to avoid duplicate network logs coming
26+
// from both the iOS and React Native SDKs
27+
[IBGNetworkLogger disableAutomaticCapturingOfNetworkLogs];
28+
}
2529

2630
[Instabug startWithToken:token invocationEvents:invocationEvents];
2731

@@ -37,6 +41,15 @@ + (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)inv
3741
IBGAPM.hotAppLaunchEnabled = NO;
3842
}
3943

44+
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents {
45+
[self initWithToken:token invocationEvents:invocationEvents useNativeNetworkInterception:NO];
46+
}
47+
48+
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents debugLogsLevel:(IBGSDKDebugLogsLevel)debugLogsLevel useNativeNetworkInterception:(BOOL)useNativeNetworkInterception {
49+
[Instabug setSdkDebugLogsLevel:debugLogsLevel];
50+
[self initWithToken:token invocationEvents:invocationEvents useNativeNetworkInterception:useNativeNetworkInterception];
51+
}
52+
4053
+ (void)initWithToken:(NSString *)token
4154
invocationEvents:(IBGInvocationEvent)invocationEvents
4255
debugLogsLevel:(IBGSDKDebugLogsLevel)debugLogsLevel {

src/models/InstabugConfig.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { InvocationEvent, LogLevel } from '../utils/Enums';
1+
import type { InvocationEvent, LogLevel, NetworkInterceptionMode } from '../utils/Enums';
22

33
export interface InstabugConfig {
44
/**
@@ -18,4 +18,15 @@ export interface InstabugConfig {
1818
* An optional code push version to be used for all reports.
1919
*/
2020
codePushVersion?: string;
21+
22+
/**
23+
* An optional network interception mode, this determines whether network interception
24+
* is done in the JavaScript side or in the native Android and iOS SDK side.
25+
*
26+
* When set to `NetworkInterceptionMode.native`, configuring network logging
27+
* should be done through native code not JavaScript (e.g. network request obfuscation).
28+
*
29+
* @default NetworkInterceptionMode.javascript
30+
*/
31+
networkInterceptionMode?: NetworkInterceptionMode;
2132
}

src/modules/Instabug.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ColorTheme,
1313
Locale,
1414
LogLevel,
15+
NetworkInterceptionMode,
1516
ReproStepsMode,
1617
StringKey,
1718
WelcomeMessageMode,
@@ -61,12 +62,19 @@ function reportCurrentViewForAndroid(screenName: string | null) {
6162
export const init = (config: InstabugConfig) => {
6263
InstabugUtils.captureJsErrors();
6364
captureUnhandledRejections();
64-
NetworkLogger.setEnabled(true);
65+
66+
// Default networkInterceptionMode to JavaScript
67+
config.networkInterceptionMode ??= NetworkInterceptionMode.javascript;
68+
69+
if (config.networkInterceptionMode === NetworkInterceptionMode.javascript) {
70+
NetworkLogger.setEnabled(true);
71+
}
6572

6673
NativeInstabug.init(
6774
config.token,
6875
config.invocationEvents,
6976
config.debugLogsLevel ?? LogLevel.error,
77+
config.networkInterceptionMode === NetworkInterceptionMode.native,
7078
config.codePushVersion,
7179
);
7280

src/native/NativeInstabug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface InstabugNativeModule extends NativeModule {
2323
token: string,
2424
invocationEvents: InvocationEvent[],
2525
debugLogsLevel: LogLevel,
26+
useNativeNetworkInterception: boolean,
2627
codePushVersion?: string,
2728
): void;
2829
show(): void;

0 commit comments

Comments
 (0)