Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import com.facebook.jni.HybridData
import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.proguard.annotations.DoNotStripAny
import com.facebook.react.common.annotations.VisibleForTesting
import com.facebook.react.devsupport.inspector.DevSupportHttpClient
import com.facebook.soloader.SoLoader
import java.io.Closeable
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets
import java.util.ArrayDeque
import java.util.Queue
import java.util.concurrent.TimeUnit
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.WebSocket
Expand Down Expand Up @@ -173,12 +172,7 @@ internal class CxxInspectorPackagerConnection(

/** Java implementation of the C++ InspectorPackagerConnectionDelegate interface. */
private class DelegateImpl {
private val httpClient =
OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
.build()
private val httpClient = DevSupportHttpClient.websocketClient

private val handler = Handler(Looper.getMainLooper())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.facebook.react.bridge.ReactContext
import com.facebook.react.common.ReactConstants
import com.facebook.react.devsupport.InspectorFlags.getFuseboxEnabled
import com.facebook.react.devsupport.InspectorFlags.getIsProfilingBuild
import com.facebook.react.devsupport.inspector.DevSupportHttpClient
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback
import com.facebook.react.modules.debug.interfaces.DeveloperSettings
Expand All @@ -39,7 +40,6 @@ import java.io.UnsupportedEncodingException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.util.Locale
import java.util.concurrent.TimeUnit
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -86,12 +86,7 @@ public open class DevServerHelper(
MAP("map"),
}

private val client: OkHttpClient =
OkHttpClient.Builder()
.connectTimeout(HTTP_CONNECT_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS)
.build()
private val client: OkHttpClient = DevSupportHttpClient.httpClient
private val bundleDownloader: BundleDownloader = BundleDownloader(client)
private val packagerStatusCheck: PackagerStatusCheck = PackagerStatusCheck(client)
private val packageName: String = applicationContext.packageName
Expand Down Expand Up @@ -397,7 +392,6 @@ public open class DevServerHelper(
}

private companion object {
private const val HTTP_CONNECT_TIMEOUT_MS = 5000
private const val DEBUGGER_MSG_DISABLE = "{ \"id\":1,\"method\":\"Debugger.disable\" }"

private fun getSHA256(string: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,20 @@ package com.facebook.react.devsupport

import com.facebook.common.logging.FLog
import com.facebook.react.common.ReactConstants
import com.facebook.react.devsupport.inspector.DevSupportHttpClient
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback
import java.io.IOException
import java.util.Locale
import java.util.concurrent.TimeUnit
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response

/** Use this class to check if the JavaScript packager is running on the provided host. */
internal class PackagerStatusCheck {
internal class PackagerStatusCheck(private val client: OkHttpClient) {

private val client: OkHttpClient

constructor() {
client =
OkHttpClient.Builder()
.connectTimeout(HTTP_CONNECT_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS)
.build()
}

constructor(client: OkHttpClient) {
this.client = client
}
constructor() : this(DevSupportHttpClient.httpClient)

fun run(host: String, callback: PackagerStatusCallback) {
val statusURL = createPackagerStatusURL(host)
Expand Down Expand Up @@ -92,7 +79,6 @@ internal class PackagerStatusCheck {

private companion object {
private const val PACKAGER_OK_STATUS = "packager-status:running"
private const val HTTP_CONNECT_TIMEOUT_MS = 5_000
private const val PACKAGER_STATUS_URL_TEMPLATE = "http://%s/status"

private fun createPackagerStatusURL(host: String): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import android.widget.TextView
import com.facebook.common.logging.FLog
import com.facebook.react.R
import com.facebook.react.common.ReactConstants
import com.facebook.react.devsupport.inspector.DevSupportHttpClient
import com.facebook.react.devsupport.interfaces.DevSupportManager
import com.facebook.react.devsupport.interfaces.ErrorType
import com.facebook.react.devsupport.interfaces.RedBoxHandler
import com.facebook.react.devsupport.interfaces.StackFrame
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import org.json.JSONObject
Expand Down Expand Up @@ -165,7 +165,7 @@ internal class RedBoxContentView(
.query(null)
.build()
.toString()
val client = OkHttpClient()
val client = DevSupportHttpClient.httpClient
for (frame in stackFrames) {
val payload = stackFrameToJson(checkNotNull(frame)).toString()
val body: RequestBody = RequestBody.create(JSON, payload)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

@file:Suppress("DEPRECATION_ERROR") // Conflicting okhttp versions

package com.facebook.react.devsupport.inspector

import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
import okhttp3.Interceptor
import okhttp3.OkHttpClient

/**
* Shared [OkHttpClient] instances for devsupport networking. Uses a single connection pool and
* dispatcher across all dev support HTTP and WebSocket usage.
*/
internal object DevSupportHttpClient {
private val customHeaders = ConcurrentHashMap<String, String>()

private val headerInterceptor = Interceptor { chain ->
val builder = chain.request().newBuilder()
for ((name, value) in customHeaders) {
builder.header(name, value)
}
chain.proceed(builder.build())
}

/** Client for HTTP requests: connect=5s, write=disabled, read=disabled. */
val httpClient: OkHttpClient =
OkHttpClient.Builder()
.addInterceptor(headerInterceptor)
.connectTimeout(5, TimeUnit.SECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MINUTES)
.build()

/** Client for WebSocket connections: connect=10s, write=10s, read=disabled. */
val websocketClient: OkHttpClient =
httpClient
.newBuilder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build()

/** Add a custom header to be included in all requests made through both clients. */
fun addRequestHeader(name: String, value: String) {
customHeaders[name] = value
}

/** Remove a previously added custom header. */
fun removeRequestHeader(name: String) {
customHeaders.remove(name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,15 @@
package com.facebook.react.devsupport.inspector

import java.io.IOException
import java.util.concurrent.TimeUnit
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response

internal object InspectorNetworkHelper {
private lateinit var client: OkHttpClient

@JvmStatic
fun loadNetworkResource(url: String, listener: InspectorNetworkRequestListener) {
if (!::client.isInitialized) {
client =
OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
.build()
}

val request =
try {
Expand All @@ -40,7 +29,7 @@ internal object InspectorNetworkHelper {
}

// TODO(T196951523): Assign cancel function to listener
val call = client.newCall(request)
val call = DevSupportHttpClient.httpClient.newCall(request)

call.enqueue(
object : Callback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ package com.facebook.react.packagerconnection
import android.os.Handler
import android.os.Looper
import com.facebook.common.logging.FLog
import com.facebook.react.devsupport.inspector.DevSupportHttpClient
import java.io.IOException
import java.nio.channels.ClosedChannelException
import java.util.concurrent.TimeUnit
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.WebSocket
Expand All @@ -40,12 +39,7 @@ public class ReconnectingWebSocket(
}

private val handler = Handler(Looper.getMainLooper())
private val okHttpClient: OkHttpClient =
OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
.build()
private val okHttpClient = DevSupportHttpClient.websocketClient
private var closed = false
private var suppressConnectionErrors = false
private var webSocket: WebSocket? = null
Expand Down
Loading