-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[go] add network inspector support (expo#22741)
# Why add network inspector support for expo go close ENG-8010 # How integrate the ExpoRequestCdpInterceptor from expo-modules-core. unlike expo-dev-launcher to use hacky solution, we want to enable the network inspector on release mode, so the implementation follows formal way. - on ios, we leverage the `RCTSetCustomNSURLSessionConfigurationProvider` from react-native to create dedicated `URLSessionConfiguration`. - on android, we already have a dedicated okhttp client from expo go. this pr just adds the interceptors. - found image requests are not intercepted, it is because we don't use okhttp for fresco image pipeline. this pr tries to use the okhttp for fresco as react-native. - android expo go has multiple react instances. however, the network inspector currently only support single inspector target to metro-inspector-proxy. as the result, we only limit the current foreground activity to send network inspector events. # Test Plan ncl + expo go to test the network inspector
- Loading branch information
Showing
10 changed files
with
254 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
android/expoview/src/main/java/versioned/host/exp/exponent/ExpoNetworkInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2015-present 650 Industries. All rights reserved. | ||
|
||
package versioned.host.exp.exponent | ||
|
||
import com.facebook.react.ReactInstanceManager | ||
import com.facebook.react.bridge.Inspector | ||
import com.facebook.react.devsupport.DevServerHelper | ||
import com.facebook.react.devsupport.DevSupportManagerBase | ||
import com.facebook.react.devsupport.InspectorPackagerConnection | ||
import expo.modules.kotlin.devtools.ExpoRequestCdpInterceptor | ||
import expo.modules.manifests.core.Manifest | ||
import java.io.Closeable | ||
|
||
@Suppress("unused") | ||
class ExpoNetworkInterceptor : Closeable, ExpoRequestCdpInterceptor.Delegate { | ||
private var isStarted = false | ||
private val inspectorPackagerConnection = InspectorPackagerConnectionWrapper() | ||
private var reactInstanceManager: ReactInstanceManager? = null | ||
|
||
fun start(manifest: Manifest, reactInstanceManager: ReactInstanceManager) { | ||
val buildProps = (manifest?.getPluginProperties("expo-build-properties")?.get("android") as? Map<*, *>) | ||
?.mapKeys { it.key.toString() } | ||
val enableNetworkInspector = buildProps?.get("networkInspector") as? Boolean ?: true | ||
isStarted = enableNetworkInspector | ||
|
||
this.onResume(reactInstanceManager) | ||
} | ||
|
||
fun onResume(reactInstanceManager: ReactInstanceManager) { | ||
if (!isStarted) { | ||
return | ||
} | ||
this.reactInstanceManager = reactInstanceManager | ||
ExpoRequestCdpInterceptor.setDelegate(this) | ||
} | ||
|
||
fun onPause() { | ||
if (!isStarted) { | ||
return | ||
} | ||
ExpoRequestCdpInterceptor.setDelegate(null) | ||
this.reactInstanceManager = null | ||
} | ||
|
||
override fun close() { | ||
this.onPause() | ||
} | ||
|
||
override fun dispatch(event: String) { | ||
reactInstanceManager?.let { | ||
inspectorPackagerConnection.sendWrappedEventToAllPages(it, event) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A `InspectorPackagerConnection` wrapper to expose private members with reflection | ||
*/ | ||
internal class InspectorPackagerConnectionWrapper { | ||
private val devServerHelperField = DevSupportManagerBase::class.java.getDeclaredField("mDevServerHelper") | ||
private val inspectorPackagerConnectionField = DevServerHelper::class.java.getDeclaredField("mInspectorPackagerConnection") | ||
private val sendWrappedEventMethod = InspectorPackagerConnection::class.java.getDeclaredMethod("sendWrappedEvent", String::class.java, String::class.java) | ||
|
||
init { | ||
devServerHelperField.isAccessible = true | ||
inspectorPackagerConnectionField.isAccessible = true | ||
sendWrappedEventMethod.isAccessible = true | ||
} | ||
|
||
fun sendWrappedEventToAllPages(reactInstanceManager: ReactInstanceManager, event: String) { | ||
val devServerHelper = devServerHelperField[reactInstanceManager.devSupportManager] | ||
val inspectorPackagerConnection = inspectorPackagerConnectionField[devServerHelper] as? InspectorPackagerConnection | ||
for (page in Inspector.getPages()) { | ||
if (!page.title.contains("Reanimated")) { | ||
sendWrappedEventMethod.invoke(inspectorPackagerConnection, page.id.toString(), event) | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
ios/Exponent/Versioned/Core/EXVersionedNetworkInterceptor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2015-present 650 Industries. All rights reserved. | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@class RCTInspectorPackagerConnection; | ||
|
||
@interface EXVersionedNetworkInterceptor : NSObject | ||
|
||
- (instancetype)initWithRCTInspectorPackagerConnection:(RCTInspectorPackagerConnection *)inspectorPackgerConnection; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
110 changes: 110 additions & 0 deletions
110
ios/Exponent/Versioned/Core/EXVersionedNetworkInterceptor.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2015-present 650 Industries. All rights reserved. | ||
|
||
#import "EXVersionedNetworkInterceptor.h" | ||
|
||
#import <React/RCTHTTPRequestHandler.h> | ||
#import <React/RCTInspector.h> | ||
#import <React/RCTInspectorPackagerConnection.h> | ||
#import <SocketRocket/SRWebSocket.h> | ||
|
||
#import "Expo_Go-Swift.h" | ||
#import "ExpoModulesCore-Swift.h" | ||
|
||
#pragma mark - RCTInspectorPackagerConnection category interface | ||
|
||
@interface RCTInspectorPackagerConnection(sendWrappedEventToAllPages) | ||
|
||
- (BOOL)isReadyToSend; | ||
- (void)sendWrappedEventToAllPages:(NSString *)event; | ||
|
||
@end | ||
|
||
#pragma mark - | ||
|
||
@interface EXVersionedNetworkInterceptor () <EXRequestCdpInterceptorDelegate> | ||
|
||
@property (nonatomic, strong) RCTInspectorPackagerConnection *inspectorPackgerConnection; | ||
|
||
@end | ||
|
||
@implementation EXVersionedNetworkInterceptor | ||
|
||
- (instancetype)initWithRCTInspectorPackagerConnection:(RCTInspectorPackagerConnection *)inspectorPackgerConnection | ||
{ | ||
if (self = [super init]) { | ||
self.inspectorPackgerConnection = inspectorPackgerConnection; | ||
[EXRequestCdpInterceptor.shared setDelegate:self]; | ||
|
||
Class requestInterceptorClass = [EXRequestInterceptorProtocol class]; | ||
RCTSetCustomNSURLSessionConfigurationProvider(^{ | ||
NSURLSessionConfiguration *urlSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; | ||
NSMutableArray<Class> *protocolClasses = [urlSessionConfiguration.protocolClasses mutableCopy]; | ||
if (![protocolClasses containsObject:requestInterceptorClass]) { | ||
[protocolClasses insertObject:requestInterceptorClass atIndex:0]; | ||
} | ||
urlSessionConfiguration.protocolClasses = protocolClasses; | ||
|
||
[urlSessionConfiguration setHTTPShouldSetCookies:YES]; | ||
[urlSessionConfiguration setHTTPCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; | ||
[urlSessionConfiguration setHTTPCookieStorage:[NSHTTPCookieStorage sharedHTTPCookieStorage]]; | ||
return urlSessionConfiguration; | ||
}); | ||
} | ||
return self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[EXRequestCdpInterceptor.shared setDelegate:nil]; | ||
} | ||
|
||
#pragma mark - EXRequestCdpInterceptorDelegate implementations | ||
|
||
- (void)dispatch:(NSString * _Nonnull)event { | ||
[self.inspectorPackgerConnection sendWrappedEventToAllPages:event]; | ||
} | ||
|
||
@end | ||
|
||
#pragma mark - RCTInspectorPackagerConnection category | ||
|
||
@interface RCTInspectorPackagerConnection(sendWrappedEventToAllPages) | ||
|
||
- (BOOL)isReadyToSend; | ||
- (void)sendWrappedEventToAllPages:(NSString *)event; | ||
|
||
@end | ||
|
||
#pragma mark - RCTInspectorPackagerConnection category implementation | ||
|
||
@implementation RCTInspectorPackagerConnection(sendWrappedEventToAllPages) | ||
|
||
- (BOOL)isReadyToSend | ||
{ | ||
if ([self isConnected]) { | ||
return YES; | ||
} | ||
|
||
SRWebSocket *websocket = [self valueForKey:@"_webSocket"]; | ||
return websocket.readyState == SR_OPEN; | ||
} | ||
|
||
- (void)sendWrappedEventToAllPages:(NSString *)event | ||
{ | ||
if (![self isReadyToSend]) { | ||
return; | ||
} | ||
|
||
SEL selector = NSSelectorFromString(@"sendWrappedEvent:message:"); | ||
if ([self respondsToSelector:selector]) { | ||
IMP sendWrappedEventIMP = [self methodForSelector:selector]; | ||
void (*functor)(id, SEL, NSString *, NSString *) = (void *)sendWrappedEventIMP; | ||
for (RCTInspectorPage* page in RCTInspector.pages) { | ||
if (![page.title containsString:@"Reanimated"]) { | ||
functor(self, selector, [@(page.id) stringValue], event); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.