mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Expose DevSupportHttpClient as public API (#55778)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55778 Revert the `DevSupportRequestHeaders` extraction from D94354168 and instead make `DevSupportHttpClient` itself `public`. This gives external consumers (e.g. Expo Dev Launcher) direct access to the shared OkHttpClient instances and the `addRequestHeader`/`removeRequestHeader` methods without needing a separate intermediary singleton. Changes: - Remove `DevSupportRequestHeaders` class and its `request_headers` Buck target - Change `DevSupportHttpClient` from `internal` to `public` - Restore `addRequestHeader`/`removeRequestHeader` methods in `DevSupportHttpClient` - Revert `interfaces` Buck target to include all `interfaces/*.kt` files - Revert inspector Buck target to remove the `request_headers` dependency This looks breaking from the API prespective but `DevSupportRequestHeaders` was never shipped, so it's just an addittive change. Changelog: [Android][Added] - Make DevSupportHttpClient public for custom dev-support HTTP header registration Reviewed By: cipolleschi Differential Revision: D94515394 fbshipit-source-id: 709eb0e039f9b0a21d67b042dc9df0fc287720de
This commit is contained in:
1 parent
fc9940da1b
commit
58d94863f8
3 files changed
+32
-61
No files matched your search
@@ -2101,6 +2101,14 @@ public final class com/facebook/react/devsupport/StackTraceHelper$StackFrameImpl
|
||||
public fun toJSON ()Lorg/json/JSONObject;
|
||||
}
|
||||
|
||||
public final class com/facebook/react/devsupport/inspector/DevSupportHttpClient {
|
||||
public static final field INSTANCE Lcom/facebook/react/devsupport/inspector/DevSupportHttpClient;
|
||||
public static final fun addRequestHeader (Ljava/lang/String;Ljava/lang/String;)V
|
||||
public final fun getHttpClient ()Lokhttp3/OkHttpClient;
|
||||
public final fun getWebsocketClient ()Lokhttp3/OkHttpClient;
|
||||
public static final fun removeRequestHeader (Ljava/lang/String;)V
|
||||
}
|
||||
|
||||
public abstract interface class com/facebook/react/devsupport/interfaces/BundleLoadCallback {
|
||||
public fun onError (Ljava/lang/Exception;)V
|
||||
public abstract fun onSuccess ()V
|
||||
@@ -2184,13 +2192,6 @@ public abstract interface class com/facebook/react/devsupport/interfaces/DevSupp
|
||||
public abstract fun onResume ()V
|
||||
}
|
||||
|
||||
public final class com/facebook/react/devsupport/interfaces/DevSupportRequestHeaders {
|
||||
public static final field INSTANCE Lcom/facebook/react/devsupport/interfaces/DevSupportRequestHeaders;
|
||||
public static final fun addRequestHeader (Ljava/lang/String;Ljava/lang/String;)V
|
||||
public static final fun allHeaders ()Ljava/util/Map;
|
||||
public static final fun removeRequestHeader (Ljava/lang/String;)V
|
||||
}
|
||||
|
||||
public abstract interface class com/facebook/react/devsupport/interfaces/ErrorCustomizer {
|
||||
public abstract fun customizeErrorInfo (Landroid/util/Pair;)Landroid/util/Pair;
|
||||
}
|
||||
|
||||
+24
-16
@@ -9,33 +9,29 @@
|
||||
|
||||
package com.facebook.react.devsupport.inspector
|
||||
|
||||
import com.facebook.react.devsupport.interfaces.DevSupportRequestHeaders
|
||||
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. Injects custom request headers
|
||||
* registered via [DevSupportRequestHeaders] into every outgoing request.
|
||||
* dispatcher across all dev support HTTP and WebSocket usage. Supports injecting custom request
|
||||
* headers that are applied to every outgoing request via an OkHttp interceptor.
|
||||
*/
|
||||
internal object DevSupportHttpClient {
|
||||
public object DevSupportHttpClient {
|
||||
private val customHeaders = ConcurrentHashMap<String, String>()
|
||||
|
||||
private val headerInterceptor = Interceptor { chain ->
|
||||
val originalRequest = chain.request()
|
||||
val headers = DevSupportRequestHeaders.allHeaders()
|
||||
if (headers.isEmpty()) {
|
||||
chain.proceed(originalRequest)
|
||||
} else {
|
||||
val builder = originalRequest.newBuilder()
|
||||
for ((name, value) in headers) {
|
||||
builder.header(name, value)
|
||||
}
|
||||
chain.proceed(builder.build())
|
||||
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 =
|
||||
public val httpClient: OkHttpClient =
|
||||
OkHttpClient.Builder()
|
||||
.addInterceptor(headerInterceptor)
|
||||
.connectTimeout(5, TimeUnit.SECONDS)
|
||||
@@ -44,10 +40,22 @@ internal object DevSupportHttpClient {
|
||||
.build()
|
||||
|
||||
/** Client for WebSocket connections: connect=10s, write=10s, read=disabled. */
|
||||
val websocketClient: OkHttpClient =
|
||||
public 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. */
|
||||
@JvmStatic
|
||||
public fun addRequestHeader(name: String, value: String) {
|
||||
customHeaders[name] = value
|
||||
}
|
||||
|
||||
/** Remove a previously added custom header. */
|
||||
@JvmStatic
|
||||
public fun removeRequestHeader(name: String) {
|
||||
customHeaders.remove(name)
|
||||
}
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.facebook.react.devsupport.interfaces
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
/**
|
||||
* Thread-safe singleton for registering custom HTTP headers that will be applied to all React
|
||||
* Native dev-support network traffic (bundle fetches, packager status checks, inspector
|
||||
* connections, HMR WebSocket upgrades).
|
||||
*
|
||||
* Headers are stored globally and survive across React Host re-creations, so they can be set before
|
||||
* the first app load.
|
||||
*/
|
||||
public object DevSupportRequestHeaders {
|
||||
|
||||
private val headers = ConcurrentHashMap<String, String>()
|
||||
|
||||
/** Adds (or replaces) a custom request header. */
|
||||
@JvmStatic
|
||||
public fun addRequestHeader(name: String, value: String) {
|
||||
headers[name] = value
|
||||
}
|
||||
|
||||
/** Removes a previously added custom request header. */
|
||||
@JvmStatic
|
||||
public fun removeRequestHeader(name: String) {
|
||||
headers.remove(name)
|
||||
}
|
||||
|
||||
/** Returns a snapshot of all currently registered headers. */
|
||||
@JvmStatic public fun allHeaders(): Map<String, String> = HashMap(headers)
|
||||
}
|
||||
Reference in new issue
Block a user