Skip to content

Commit 25b68e1

Browse files
huntiezoontek
authored andcommitted
Remove NetworkEventUtil compatibility overloads (facebook#55769)
Summary: Pull Request resolved: facebook#55769 Follows facebook#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
1 parent e32ed79 commit 25b68e1

File tree

2 files changed

+19
-69
lines changed

2 files changed

+19
-69
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkEventUtil.kt

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,22 @@ import com.facebook.react.bridge.Arguments
1515
import com.facebook.react.bridge.ReactApplicationContext
1616
import com.facebook.react.bridge.WritableMap
1717
import com.facebook.react.bridge.buildReadableArray
18-
import com.facebook.react.common.build.ReactBuildConfig
1918
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
2019
import java.net.SocketTimeoutException
2120
import okhttp3.Headers
22-
import okhttp3.Request
2321

2422
/**
2523
* Utility class for reporting network lifecycle events to JavaScript and InspectorNetworkReporter.
2624
*/
2725
internal object NetworkEventUtil {
28-
@JvmStatic
29-
fun onCreateRequest(devToolsRequestId: String, request: Request) {
30-
if (ReactNativeFeatureFlags.enableNetworkEventReporting()) {
31-
val headersMap = okHttpHeadersToMap(request.headers())
32-
var requestBody = ""
33-
34-
if (ReactBuildConfig.DEBUG) {
35-
// Debug build: Process request body for preview (CDP only)
36-
requestBody =
37-
(request.body() as? ProgressRequestBody)?.getBodyPreview()
38-
?: request.body()?.toString().orEmpty()
39-
}
40-
41-
InspectorNetworkReporter.reportRequestStart(
42-
devToolsRequestId,
43-
request.url().toString(),
44-
request.method(),
45-
headersMap,
46-
requestBody,
47-
request.body()?.contentLength() ?: 0,
48-
)
49-
InspectorNetworkReporter.reportConnectionTiming(devToolsRequestId, headersMap)
50-
}
51-
}
52-
53-
@Deprecated("Compatibility overload")
5426
@JvmStatic
5527
fun onCreateRequest(
5628
devToolsRequestId: String,
5729
requestUrl: String,
5830
requestMethod: String,
5931
requestHeaders: Map<String, String>,
60-
requestBody: String,
32+
/** Request body for DevTools preview. Only populate in debug builds. */
33+
requestBodyForDevTools: String?,
6134
encodedDataLength: Long,
6235
) {
6336
if (ReactNativeFeatureFlags.enableNetworkEventReporting()) {
@@ -66,7 +39,7 @@ internal object NetworkEventUtil {
6639
requestUrl,
6740
requestMethod,
6841
requestHeaders,
69-
requestBody,
42+
requestBodyForDevTools.orEmpty(),
7043
encodedDataLength,
7144
)
7245
InspectorNetworkReporter.reportConnectionTiming(devToolsRequestId, requestHeaders)
@@ -256,45 +229,8 @@ internal object NetworkEventUtil {
256229
)
257230
}
258231

259-
@Deprecated("Compatibility overload")
260232
@JvmStatic
261-
fun onResponseReceived(
262-
reactContext: ReactApplicationContext?,
263-
requestId: Int,
264-
devToolsRequestId: String,
265-
statusCode: Int,
266-
headers: WritableMap?,
267-
url: String?,
268-
) {
269-
val headersMap = mutableMapOf<String, String>()
270-
headers?.let { map ->
271-
val iterator = map.keySetIterator()
272-
while (iterator.hasNextKey()) {
273-
val key = iterator.nextKey()
274-
val value = map.getString(key)
275-
if (value != null) {
276-
headersMap[key] = value
277-
}
278-
}
279-
}
280-
281-
val contentLength =
282-
headersMap["Content-Length"]?.toLongOrNull()
283-
?: headersMap["content-length"]?.toLongOrNull()
284-
?: 0L
285-
286-
onResponseReceived(
287-
reactContext,
288-
requestId,
289-
devToolsRequestId,
290-
url,
291-
statusCode,
292-
headersMap,
293-
contentLength,
294-
)
295-
}
296-
297-
internal fun okHttpHeadersToMap(headers: Headers): Map<String, String> {
233+
fun okHttpHeadersToMap(headers: Headers): Map<String, String> {
298234
val responseHeaders = mutableMapOf<String, String>()
299235
for (i in 0 until headers.size()) {
300236
val headerName = headers.name(i)

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.facebook.react.bridge.ReadableArray
2020
import com.facebook.react.bridge.ReadableMap
2121
import com.facebook.react.bridge.ReadableType
2222
import com.facebook.react.bridge.WritableMap
23+
import com.facebook.react.common.build.ReactBuildConfig
2324
import com.facebook.react.common.network.OkHttpCallUtil
2425
import com.facebook.react.module.annotations.ReactModule
2526
import java.io.IOException
@@ -603,7 +604,20 @@ public class NetworkingModule(
603604

604605
addRequest(requestId)
605606
val request = requestBuilder.build()
606-
NetworkEventUtil.onCreateRequest(devToolsRequestId, request)
607+
608+
NetworkEventUtil.onCreateRequest(
609+
devToolsRequestId,
610+
request.url().toString(),
611+
request.method(),
612+
NetworkEventUtil.okHttpHeadersToMap(request.headers()),
613+
if (ReactBuildConfig.DEBUG) {
614+
// Debug build: Include request body for preview in DevTools
615+
(request.body() as? ProgressRequestBody)?.getBodyPreview() ?: request.body()?.toString()
616+
} else {
617+
null
618+
},
619+
request.body()?.contentLength() ?: 0,
620+
)
607621

608622
client
609623
.newCall(request)

0 commit comments

Comments
 (0)