Skip to content

Commit 83c5b9e

Browse files
committed
feat: respect be network body limit (#1397)
* chore(ios): add respect BE network body limit custom build * feat(ios): add getNetworkBodyMaxSize API * feat:add getNetworkBodyMaxSize API * chore(android): add respect network body limit snapshot * chore(ios): sync pdfile.lock * Revert "Merge pull request #1388 from Instabug/refactor/replace-reflection" This reverts commit 256e72a, reversing changes made to 196b481. * feat(android): add getNetworkBodyMaxSize API * feat:add feature flag change listener for android * chore: fix sync_generated_files CI job * feat(ios): add getNetworkBodyMaxSize API * chore: add change log * Revert duplicate "feat(ios): add getNetworkBodyMaxSize API" This reverts commit ef8710e. * chore: edit a change log * chore: update test cases titles
1 parent acbeb9c commit 83c5b9e

File tree

17 files changed

+159
-64
lines changed

17 files changed

+159
-64
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
- Add support for network spans. ([#1394](https://github.com/Instabug/Instabug-React-Native/pull/1394))
1414

15+
- Add respect to backend network body limit. ([#1397](https://github.com/Instabug/Instabug-React-Native/pull/1397))
16+
1517
### Changed
1618

1719
- Bump Instabug iOS SDK to v15.1.1 ([#1402](https://github.com/Instabug/Instabug-React-Native/pull/1402)). [See release notes](https://github.com/Instabug/Instabug-iOS/releases/tag/15.1.1).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class Constants {
1313
final static String IBG_ON_FEATURES_UPDATED_CALLBACK = "IBGOnFeatureUpdatedCallback";
1414
final static String IBG_NETWORK_LOGGER_HANDLER = "IBGNetworkLoggerHandler";
1515

16-
final static String IBG_ON_NEW_W3C_FLAGS_UPDATE_RECEIVED_CALLBACK = "IBGOnNewW3CFlagsUpdateReceivedCallback";
16+
final static String IBG_ON_FEATURE_FLAGS_UPDATE_RECEIVED_CALLBACK = "IBGOnNewFeatureFlagsUpdateReceivedCallback";
1717

1818
final static String IBG_SESSION_REPLAY_ON_SYNC_CALLBACK_INVOCATION = "IBGSessionReplayOnSyncCallback";
1919

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,10 +1159,10 @@ public void run() {
11591159
}
11601160

11611161
/**
1162-
* Register a listener for W3C flags value change
1162+
* Register a listener for feature flags value change
11631163
*/
11641164
@ReactMethod
1165-
public void registerW3CFlagsChangeListener() {
1165+
public void registerFeatureFlagsChangeListener() {
11661166

11671167
MainThreadHandler.runOnMainThread(new Runnable() {
11681168
@Override
@@ -1175,8 +1175,9 @@ public void invoke(@NonNull CoreFeaturesState featuresState) {
11751175
params.putBoolean("isW3ExternalTraceIDEnabled", featuresState.isW3CExternalTraceIdEnabled());
11761176
params.putBoolean("isW3ExternalGeneratedHeaderEnabled", featuresState.isAttachingGeneratedHeaderEnabled());
11771177
params.putBoolean("isW3CaughtHeaderEnabled", featuresState.isAttachingCapturedHeaderEnabled());
1178+
params.putInt("networkBodyLimit",featuresState.getNetworkLogCharLimit());
11781179

1179-
sendEvent(Constants.IBG_ON_NEW_W3C_FLAGS_UPDATE_RECEIVED_CALLBACK, params);
1180+
sendEvent(Constants.IBG_ON_FEATURE_FLAGS_UPDATE_RECEIVED_CALLBACK, params);
11801181
}
11811182
});
11821183
} catch (Exception e) {
@@ -1306,7 +1307,7 @@ public void run() {
13061307
}
13071308
});
13081309
}
1309-
/**
1310+
13101311
/**
13111312
* Sets the auto mask screenshots types.
13121313
*
@@ -1331,4 +1332,23 @@ public void run() {
13311332

13321333
});
13331334
}
1335+
1336+
/**
1337+
* Get network body size limit
1338+
*/
1339+
@ReactMethod
1340+
public void getNetworkBodyMaxSize(Promise promise) {
1341+
1342+
MainThreadHandler.runOnMainThread(new Runnable() {
1343+
@Override
1344+
public void run() {
1345+
try {
1346+
promise.resolve(InternalCore.INSTANCE.get_networkLogCharLimit());
1347+
} catch (Exception e) {
1348+
e.printStackTrace();
1349+
promise.resolve(false);
1350+
}
1351+
}
1352+
});
1353+
}
13341354
}

android/src/test/java/com/instabug/reactlibrary/RNInstabugReactnativeModuleTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,22 @@ public void testEnableAutoMasking(){
686686
String maskTextInputs = "textInputs";
687687
String maskMedia = "media";
688688
String maskNone = "none";
689-
689+
690690
rnModule.enableAutoMasking(JavaOnlyArray.of(maskLabel, maskMedia, maskTextInputs,maskNone));
691-
691+
692692
mockInstabug.verify(() -> Instabug.setAutoMaskScreenshotsTypes(MaskingType.LABELS,MaskingType.MEDIA,MaskingType.TEXT_INPUTS,MaskingType.MASK_NOTHING));
693693
}
694+
695+
@Test
696+
public void testGetNetworkBodyMaxSize_resolvesPromiseWithExpectedValue() {
697+
Promise promise = mock(Promise.class);
698+
InternalCore internalAPM = mock(InternalCore.class);
699+
int expected = 10240;
700+
when(internalAPM.get_networkLogCharLimit()).thenReturn(expected);
701+
702+
rnModule.getNetworkBodyMaxSize(promise);
703+
704+
verify(promise).resolve(expected);
705+
}
706+
694707
}

examples/default/ios/InstabugTests/InstabugSampleTests.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,4 +634,23 @@ - (void)testSetNetworkLogBodyEnabled {
634634
OCMVerify([mock setLogBodyEnabled:isEnabled]);
635635
}
636636

637+
- (void)testGetNetworkBodyMaxSize {
638+
id mock = OCMClassMock([IBGNetworkLogger class]);
639+
double expectedValue = 10240.0;
640+
641+
OCMStub([mock getNetworkBodyMaxSize]).andReturn(expectedValue);
642+
643+
XCTestExpectation *expectation = [self expectationWithDescription:@"Call resolve block"];
644+
RCTPromiseResolveBlock resolve = ^(NSNumber *result) {
645+
XCTAssertEqual(result.doubleValue, expectedValue);
646+
[expectation fulfill];
647+
};
648+
649+
[self.instabugBridge getNetworkBodyMaxSize:resolve :nil];
650+
[self waitForExpectationsWithTimeout:1.0 handler:nil];
651+
652+
OCMVerify(ClassMethod([mock getNetworkBodyMaxSize]));
653+
}
654+
655+
637656
@end

examples/default/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const App: React.FC = () => {
5050
token: 'deb1910a7342814af4e4c9210c786f35',
5151
invocationEvents: [InvocationEvent.floatingButton],
5252
debugLogsLevel: LogLevel.verbose,
53-
networkInterceptionMode: NetworkInterceptionMode.native,
53+
networkInterceptionMode: NetworkInterceptionMode.javascript,
5454
});
5555

5656
CrashReporting.setNDKCrashesEnabled(true);

ios/RNInstabug/InstabugReactBridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,6 @@ w3cExternalTraceAttributes:(NSDictionary * _Nullable)w3cExternalTraceAttributes;
140140
- (void)removeAllFeatureFlags;
141141
- (void)setNetworkLogBodyEnabled:(BOOL)isEnabled;
142142
- (void)enableAutoMasking:(NSArray *)autoMaskingTypes;
143+
- (void)getNetworkBodyMaxSize:(RCTPromiseResolveBlock)resolve :(RCTPromiseRejectBlock)reject;
143144

144145
@end

ios/RNInstabug/InstabugReactBridge.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ + (BOOL)iOSVersionIsLessThan:(NSString *)iOSVersion {
452452
[Instabug setAutoMaskScreenshots: autoMaskingOptions];
453453
};
454454

455+
RCT_EXPORT_METHOD(getNetworkBodyMaxSize:(RCTPromiseResolveBlock)resolve :(RCTPromiseRejectBlock)reject) {
456+
resolve(@(IBGNetworkLogger.getNetworkBodyMaxSize));
457+
}
458+
455459
RCT_EXPORT_METHOD(setNetworkLogBodyEnabled:(BOOL)isEnabled) {
456460
IBGNetworkLogger.logBodyEnabled = isEnabled;
457461
}

ios/RNInstabug/Util/IBGNetworkLogger+CP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ NS_ASSUME_NONNULL_BEGIN
6161
+ (void)setCPRequestAsyncObfuscationHandler:(void (^)(NSURLRequest * requestToBeObfuscated, void (^ completion)(NSURLRequest * obfuscatedRequest)))asyncObfuscationHandler;
6262
+ (void)setCPRequestFilteringHandler:(void (^)(NSURLRequest * request, void (^completion)(BOOL keep)))requestFilteringHandler;
6363
+ (void)setCPResponseFilteringHandler:(void (^)(NSURLResponse * response, void (^comppletion)(BOOL keep)))responseFilteringHandler;
64+
+ (double)getNetworkBodyMaxSize;
6465

6566
@end
6667

src/modules/Instabug.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { NavigationAction, NavigationState as NavigationStateV4 } from 'rea
1616
import type { InstabugConfig } from '../models/InstabugConfig';
1717
import Report from '../models/Report';
1818
import { emitter, NativeEvents, NativeInstabug } from '../native/NativeInstabug';
19-
import { registerW3CFlagsListener } from '../utils/FeatureFlags';
19+
import { registerFeatureFlagsListener } from '../utils/FeatureFlags';
2020
import {
2121
AutoMaskingType,
2222
ColorTheme,
@@ -87,7 +87,7 @@ function reportCurrentViewForAndroid(screenName: string | null) {
8787
export const init = async (config: InstabugConfig) => {
8888
if (Platform.OS === 'android') {
8989
// Add android feature flags listener for android
90-
registerW3CFlagsListener();
90+
registerFeatureFlagsListener();
9191
addOnFeatureUpdatedListener(config);
9292
} else {
9393
isNativeInterceptionFeatureEnabled = await NativeNetworkLogger.isNativeInterceptionEnabled();
@@ -871,20 +871,21 @@ export const componentDidAppearListener = (event: ComponentDidAppearEvent) => {
871871
};
872872

873873
/**
874-
* Sets listener to W3ExternalTraceID flag changes
874+
* Sets listener to feature flag changes
875875
* @param handler A callback that gets the update value of the flag
876876
*/
877-
export const _registerW3CFlagsChangeListener = (
877+
export const _registerFeatureFlagsChangeListener = (
878878
handler: (payload: {
879879
isW3ExternalTraceIDEnabled: boolean;
880880
isW3ExternalGeneratedHeaderEnabled: boolean;
881881
isW3CaughtHeaderEnabled: boolean;
882+
networkBodyLimit: number;
882883
}) => void,
883884
) => {
884-
emitter.addListener(NativeEvents.ON_W3C_FLAGS_CHANGE, (payload) => {
885+
emitter.addListener(NativeEvents.ON_FEATURE_FLAGS_CHANGE, (payload) => {
885886
handler(payload);
886887
});
887-
NativeInstabug.registerW3CFlagsChangeListener();
888+
NativeInstabug.registerFeatureFlagsChangeListener();
888889
};
889890

890891
/**

src/modules/NetworkLogger.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export const setEnabled = (isEnabled: boolean) => {
4545
xhr.setOnDoneCallback(async (network) => {
4646
// eslint-disable-next-line no-new-func
4747
const predicate = Function('network', 'return ' + _requestFilterExpression);
48+
4849
if (!predicate(network)) {
50+
const MAX_NETWORK_BODY_SIZE_IN_BYTES = await NativeInstabug.getNetworkBodyMaxSize();
4951
try {
5052
if (_networkDataObfuscationHandler) {
5153
network = await _networkDataObfuscationHandler(network);
@@ -57,14 +59,28 @@ export const setEnabled = (isEnabled: boolean) => {
5759
return;
5860
}
5961
}
60-
if (network.requestBodySize > InstabugConstants.MAX_NETWORK_BODY_SIZE_IN_BYTES) {
61-
network.requestBody = InstabugConstants.MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE;
62-
Logger.warn('IBG-RN:', InstabugConstants.MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE);
62+
if (network.requestBodySize > MAX_NETWORK_BODY_SIZE_IN_BYTES) {
63+
network.requestBody = `${InstabugConstants.MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE}${
64+
MAX_NETWORK_BODY_SIZE_IN_BYTES / 1024
65+
} Kb`;
66+
Logger.warn(
67+
'IBG-RN:',
68+
`${InstabugConstants.MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE}${
69+
MAX_NETWORK_BODY_SIZE_IN_BYTES / 1024
70+
} Kb`,
71+
);
6372
}
6473

65-
if (network.responseBodySize > InstabugConstants.MAX_NETWORK_BODY_SIZE_IN_BYTES) {
66-
network.responseBody = InstabugConstants.MAX_RESPONSE_BODY_SIZE_EXCEEDED_MESSAGE;
67-
Logger.warn('IBG-RN:', InstabugConstants.MAX_RESPONSE_BODY_SIZE_EXCEEDED_MESSAGE);
74+
if (network.responseBodySize > MAX_NETWORK_BODY_SIZE_IN_BYTES) {
75+
network.responseBody = `${InstabugConstants.MAX_RESPONSE_BODY_SIZE_EXCEEDED_MESSAGE}${
76+
MAX_NETWORK_BODY_SIZE_IN_BYTES / 1024
77+
} Kb`;
78+
Logger.warn(
79+
'IBG-RN:',
80+
`${InstabugConstants.MAX_RESPONSE_BODY_SIZE_EXCEEDED_MESSAGE}${
81+
MAX_NETWORK_BODY_SIZE_IN_BYTES / 1024
82+
} Kb`,
83+
);
6884
}
6985

7086
if (network.requestBody && isContentTypeNotAllowed(network.requestContentType)) {

src/native/NativeInstabug.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,20 @@ export interface InstabugNativeModule extends NativeModule {
152152

153153
isW3CaughtHeaderEnabled(): Promise<boolean>;
154154

155-
// W3C Feature Flags Listener for Android
156-
registerW3CFlagsChangeListener(): void;
155+
// Feature Flags Listener for Android
156+
registerFeatureFlagsChangeListener(): void;
157157

158158
setOnFeaturesUpdatedListener(handler?: (params: any) => void): void; // android only
159159
enableAutoMasking(autoMaskingTypes: AutoMaskingType[]): void;
160+
getNetworkBodyMaxSize(): Promise<number>;
160161
}
161162

162163
export const NativeInstabug = NativeModules.Instabug;
163164

164165
export enum NativeEvents {
165166
PRESENDING_HANDLER = 'IBGpreSendingHandler',
166167
IBG_ON_FEATURES_UPDATED_CALLBACK = 'IBGOnFeatureUpdatedCallback',
167-
ON_W3C_FLAGS_CHANGE = 'IBGOnNewW3CFlagsUpdateReceivedCallback',
168+
ON_FEATURE_FLAGS_CHANGE = 'IBGOnNewFeatureFlagsUpdateReceivedCallback',
168169
}
169170

170171
export const emitter = new NativeEventEmitter(NativeInstabug);

src/utils/FeatureFlags.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import { NativeInstabug } from '../native/NativeInstabug';
2-
import { _registerW3CFlagsChangeListener } from '../modules/Instabug';
2+
import { _registerFeatureFlagsChangeListener } from '../modules/Instabug';
33

44
export const FeatureFlags = {
55
isW3ExternalTraceID: () => NativeInstabug.isW3ExternalTraceIDEnabled(),
66
isW3ExternalGeneratedHeader: () => NativeInstabug.isW3ExternalGeneratedHeaderEnabled(),
77
isW3CaughtHeader: () => NativeInstabug.isW3CaughtHeaderEnabled(),
8+
networkLogLimit: () => NativeInstabug.getNetworkBodyMaxSize(),
89
};
910

10-
export const registerW3CFlagsListener = () => {
11-
_registerW3CFlagsChangeListener(
11+
export const registerFeatureFlagsListener = () => {
12+
_registerFeatureFlagsChangeListener(
1213
(res: {
1314
isW3ExternalTraceIDEnabled: boolean;
1415
isW3ExternalGeneratedHeaderEnabled: boolean;
1516
isW3CaughtHeaderEnabled: boolean;
17+
networkBodyLimit: number;
1618
}) => {
1719
FeatureFlags.isW3ExternalTraceID = async () => {
1820
return res.isW3ExternalTraceIDEnabled;
@@ -23,6 +25,9 @@ export const registerW3CFlagsListener = () => {
2325
FeatureFlags.isW3CaughtHeader = async () => {
2426
return res.isW3CaughtHeaderEnabled;
2527
};
28+
FeatureFlags.networkLogLimit = async () => {
29+
return res.networkBodyLimit;
30+
};
2631
},
2732
);
2833
};

src/utils/InstabugConstants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const InstabugConstants = {
44
// TODO: dyanmically get the max size from the native SDK and update the error message to reflect the dynamic size.
55
MAX_NETWORK_BODY_SIZE_IN_BYTES: 1024 * 10, // 10 KB
66
MAX_RESPONSE_BODY_SIZE_EXCEEDED_MESSAGE:
7-
'The response body has not been logged because it exceeds the maximum size of 10 Kb',
7+
'The response body has not been logged because it exceeds the maximum size of ',
88
MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE:
9-
'The request body has not been logged because it exceeds the maximum size of 10 Kb',
9+
'The request body has not been logged because it exceeds the maximum size of ',
1010
SET_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE:
1111
'IBG-RN: Expected key and value passed to setUserAttribute to be of type string',
1212
REMOVE_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE:

test/mocks/mockInstabug.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ const mockInstabug: InstabugNativeModule = {
7272
isW3ExternalTraceIDEnabled: jest.fn(),
7373
isW3ExternalGeneratedHeaderEnabled: jest.fn(),
7474
isW3CaughtHeaderEnabled: jest.fn(),
75-
registerW3CFlagsChangeListener: jest.fn(),
75+
registerFeatureFlagsChangeListener: jest.fn(),
7676
setNetworkLogBodyEnabled: jest.fn(),
7777
setOnFeaturesUpdatedListener: jest.fn(),
7878
enableAutoMasking: jest.fn(),
79+
getNetworkBodyMaxSize: jest.fn().mockResolvedValue(10240), // 10 KB
7980
};
8081

8182
export default mockInstabug;

test/modules/Instabug.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -900,19 +900,19 @@ describe('Instabug Module', () => {
900900
expect(NativeInstabug.willRedirectToStore).toBeCalledTimes(1);
901901
});
902902

903-
it('should register W3C flag listener', async () => {
903+
it('should register feature flag listener', async () => {
904904
const callback = jest.fn();
905-
Instabug._registerW3CFlagsChangeListener(callback);
905+
Instabug._registerFeatureFlagsChangeListener(callback);
906906

907-
expect(NativeInstabug.registerW3CFlagsChangeListener).toBeCalledTimes(1);
907+
expect(NativeInstabug.registerFeatureFlagsChangeListener).toBeCalledTimes(1);
908908
});
909909

910-
it('should invoke callback on emitting the event IBGOnNewW3CFlagsUpdateReceivedCallback', () => {
910+
it('should invoke callback on emitting the event IBGOnNewFeatureFlagsUpdateReceivedCallback', () => {
911911
const callback = jest.fn();
912-
Instabug._registerW3CFlagsChangeListener(callback);
913-
emitter.emit(NativeEvents.ON_W3C_FLAGS_CHANGE);
912+
Instabug._registerFeatureFlagsChangeListener(callback);
913+
emitter.emit(NativeEvents.ON_FEATURE_FLAGS_CHANGE);
914914

915-
expect(emitter.listenerCount(NativeEvents.ON_W3C_FLAGS_CHANGE)).toBe(1);
915+
expect(emitter.listenerCount(NativeEvents.ON_FEATURE_FLAGS_CHANGE)).toBe(1);
916916
expect(callback).toHaveBeenCalled();
917917
});
918918

0 commit comments

Comments
 (0)