mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Remove NetworkEventUtil compatibility overloads (#55769)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55769 Follows https://github.com/facebook/react-native/pull/55706. Updates the signature of `onCreateRequest`, and removes legacy `onCreateRequest` and `onResponseReceived` overloads — effectively de-OkHttp-ing this internal API. Changelog: [Internal] Reviewed By: javache Differential Revision: D94510863 fbshipit-source-id: ce438af6e1cd352ce7afff5588621612e03f5cf9
This commit is contained in:
1 parent
947f5cd5b4
commit
0d40f2a178
2 files changed
+19
-69
No files matched your search
+4
-68
@@ -15,49 +15,22 @@ import com.facebook.react.bridge.Arguments
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.WritableMap
|
||||
import com.facebook.react.bridge.buildReadableArray
|
||||
import com.facebook.react.common.build.ReactBuildConfig
|
||||
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
|
||||
import java.net.SocketTimeoutException
|
||||
import okhttp3.Headers
|
||||
import okhttp3.Request
|
||||
|
||||
/**
|
||||
* Utility class for reporting network lifecycle events to JavaScript and InspectorNetworkReporter.
|
||||
*/
|
||||
internal object NetworkEventUtil {
|
||||
@JvmStatic
|
||||
fun onCreateRequest(devToolsRequestId: String, request: Request) {
|
||||
if (ReactNativeFeatureFlags.enableNetworkEventReporting()) {
|
||||
val headersMap = okHttpHeadersToMap(request.headers())
|
||||
var requestBody = ""
|
||||
|
||||
if (ReactBuildConfig.DEBUG) {
|
||||
// Debug build: Process request body for preview (CDP only)
|
||||
requestBody =
|
||||
(request.body() as? ProgressRequestBody)?.getBodyPreview()
|
||||
?: request.body()?.toString().orEmpty()
|
||||
}
|
||||
|
||||
InspectorNetworkReporter.reportRequestStart(
|
||||
devToolsRequestId,
|
||||
request.url().toString(),
|
||||
request.method(),
|
||||
headersMap,
|
||||
requestBody,
|
||||
request.body()?.contentLength() ?: 0,
|
||||
)
|
||||
InspectorNetworkReporter.reportConnectionTiming(devToolsRequestId, headersMap)
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated("Compatibility overload")
|
||||
@JvmStatic
|
||||
fun onCreateRequest(
|
||||
devToolsRequestId: String,
|
||||
requestUrl: String,
|
||||
requestMethod: String,
|
||||
requestHeaders: Map<String, String>,
|
||||
requestBody: String,
|
||||
/** Request body for DevTools preview. Only populate in debug builds. */
|
||||
requestBodyForDevTools: String?,
|
||||
encodedDataLength: Long,
|
||||
) {
|
||||
if (ReactNativeFeatureFlags.enableNetworkEventReporting()) {
|
||||
@@ -66,7 +39,7 @@ internal object NetworkEventUtil {
|
||||
requestUrl,
|
||||
requestMethod,
|
||||
requestHeaders,
|
||||
requestBody,
|
||||
requestBodyForDevTools.orEmpty(),
|
||||
encodedDataLength,
|
||||
)
|
||||
InspectorNetworkReporter.reportConnectionTiming(devToolsRequestId, requestHeaders)
|
||||
@@ -256,45 +229,8 @@ internal object NetworkEventUtil {
|
||||
)
|
||||
}
|
||||
|
||||
@Deprecated("Compatibility overload")
|
||||
@JvmStatic
|
||||
fun onResponseReceived(
|
||||
reactContext: ReactApplicationContext?,
|
||||
requestId: Int,
|
||||
devToolsRequestId: String,
|
||||
statusCode: Int,
|
||||
headers: WritableMap?,
|
||||
url: String?,
|
||||
) {
|
||||
val headersMap = mutableMapOf<String, String>()
|
||||
headers?.let { map ->
|
||||
val iterator = map.keySetIterator()
|
||||
while (iterator.hasNextKey()) {
|
||||
val key = iterator.nextKey()
|
||||
val value = map.getString(key)
|
||||
if (value != null) {
|
||||
headersMap[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val contentLength =
|
||||
headersMap["Content-Length"]?.toLongOrNull()
|
||||
?: headersMap["content-length"]?.toLongOrNull()
|
||||
?: 0L
|
||||
|
||||
onResponseReceived(
|
||||
reactContext,
|
||||
requestId,
|
||||
devToolsRequestId,
|
||||
url,
|
||||
statusCode,
|
||||
headersMap,
|
||||
contentLength,
|
||||
)
|
||||
}
|
||||
|
||||
internal fun okHttpHeadersToMap(headers: Headers): Map<String, String> {
|
||||
fun okHttpHeadersToMap(headers: Headers): Map<String, String> {
|
||||
val responseHeaders = mutableMapOf<String, String>()
|
||||
for (i in 0 until headers.size()) {
|
||||
val headerName = headers.name(i)
|
||||
|
||||
+15
-1
@@ -20,6 +20,7 @@ import com.facebook.react.bridge.ReadableArray
|
||||
import com.facebook.react.bridge.ReadableMap
|
||||
import com.facebook.react.bridge.ReadableType
|
||||
import com.facebook.react.bridge.WritableMap
|
||||
import com.facebook.react.common.build.ReactBuildConfig
|
||||
import com.facebook.react.common.network.OkHttpCallUtil
|
||||
import com.facebook.react.module.annotations.ReactModule
|
||||
import java.io.IOException
|
||||
@@ -603,7 +604,20 @@ public class NetworkingModule(
|
||||
|
||||
addRequest(requestId)
|
||||
val request = requestBuilder.build()
|
||||
NetworkEventUtil.onCreateRequest(devToolsRequestId, request)
|
||||
|
||||
NetworkEventUtil.onCreateRequest(
|
||||
devToolsRequestId,
|
||||
request.url().toString(),
|
||||
request.method(),
|
||||
NetworkEventUtil.okHttpHeadersToMap(request.headers()),
|
||||
if (ReactBuildConfig.DEBUG) {
|
||||
// Debug build: Include request body for preview in DevTools
|
||||
(request.body() as? ProgressRequestBody)?.getBodyPreview() ?: request.body()?.toString()
|
||||
} else {
|
||||
null
|
||||
},
|
||||
request.body()?.contentLength() ?: 0,
|
||||
)
|
||||
|
||||
client
|
||||
.newCall(request)
|
||||
|
||||
Reference in new issue
Block a user