From 4aec1a1771dcf289716ee8c33e39e78faa062579 Mon Sep 17 00:00:00 2001 From: David Vacca <515103+mdvacca@users.noreply.github.com> Date: Thu, 23 Jan 2025 13:15:56 -0800 Subject: [PATCH] Migrate PackagerStatusCheck to Kotlin (#48838) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48838 Migrate PackagerStatusCheck to Kotlin changelog: [internal] internal Reviewed By: tdn120, cortinico Differential Revision: D68467780 --- .../react/devsupport/PackagerStatusCheck.java | 102 ------------------ .../react/devsupport/PackagerStatusCheck.kt | 96 +++++++++++++++++ 2 files changed, 96 insertions(+), 102 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.java deleted file mode 100644 index 3e8a55d3c292f5..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.java +++ /dev/null @@ -1,102 +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; - -import com.facebook.common.logging.FLog; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.react.common.ReactConstants; -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; -import okhttp3.ResponseBody; - -/** Use this class to check if the JavaScript packager is running on the provided host. */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public class PackagerStatusCheck { - - private static final String PACKAGER_OK_STATUS = "packager-status:running"; - private static final int HTTP_CONNECT_TIMEOUT_MS = 5000; - private static final String PACKAGER_STATUS_URL_TEMPLATE = "http://%s/status"; - - private final OkHttpClient mClient; - - public PackagerStatusCheck() { - mClient = - new OkHttpClient.Builder() - .connectTimeout(HTTP_CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS) - .readTimeout(0, TimeUnit.MILLISECONDS) - .writeTimeout(0, TimeUnit.MILLISECONDS) - .build(); - } - - public PackagerStatusCheck(OkHttpClient client) { - mClient = client; - } - - public void run(String host, final PackagerStatusCallback callback) { - String statusURL = createPackagerStatusURL(host); - Request request = new Request.Builder().url(statusURL).build(); - - mClient - .newCall(request) - .enqueue( - new Callback() { - @Override - public void onFailure(Call call, IOException e) { - FLog.w( - ReactConstants.TAG, - "The packager does not seem to be running as we got an IOException requesting " - + "its status: " - + e.getMessage()); - callback.onPackagerStatusFetched(false); - } - - @Override - public void onResponse(Call call, Response response) throws IOException { - if (!response.isSuccessful()) { - FLog.e( - ReactConstants.TAG, - "Got non-success http code from packager when requesting status: " - + response.code()); - callback.onPackagerStatusFetched(false); - return; - } - ResponseBody body = response.body(); - if (body == null) { - FLog.e( - ReactConstants.TAG, - "Got null body response from packager when requesting status"); - callback.onPackagerStatusFetched(false); - return; - } - String bodyString = - body.string(); // cannot call body.string() twice, stored it into variable. - // https://github.com/square/okhttp/issues/1240#issuecomment-68142603 - if (!PACKAGER_OK_STATUS.equals(bodyString)) { - FLog.e( - ReactConstants.TAG, - "Got unexpected response from packager when requesting status: " - + bodyString); - callback.onPackagerStatusFetched(false); - return; - } - callback.onPackagerStatusFetched(true); - } - }); - } - - private static String createPackagerStatusURL(String host) { - return String.format(Locale.US, PACKAGER_STATUS_URL_TEMPLATE, host); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.kt new file mode 100644 index 00000000000000..a732e82631fcef --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.kt @@ -0,0 +1,96 @@ +/* + * 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 + +import com.facebook.common.logging.FLog +import com.facebook.react.common.ReactConstants +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. */ +public open class PackagerStatusCheck { + + private val client: OkHttpClient + + public constructor() { + client = + OkHttpClient.Builder() + .connectTimeout(HTTP_CONNECT_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS) + .readTimeout(0, TimeUnit.MILLISECONDS) + .writeTimeout(0, TimeUnit.MILLISECONDS) + .build() + } + + public constructor(client: OkHttpClient) { + this.client = client + } + + public open fun run(host: String, callback: PackagerStatusCallback): Unit { + val statusURL = createPackagerStatusURL(host) + val request = Request.Builder().url(statusURL).build() + + client + .newCall(request) + .enqueue( + object : Callback { + override fun onFailure(call: Call, e: IOException) { + FLog.w( + ReactConstants.TAG, + "The packager does not seem to be running as we got an IOException requesting its status: ${e.message}") + callback.onPackagerStatusFetched(false) + } + + override fun onResponse(call: Call, response: Response) { + if (!response.isSuccessful) { + FLog.e( + ReactConstants.TAG, + "Got non-success http code from packager when requesting status: ${response.code()}") + callback.onPackagerStatusFetched(false) + return + } + val body = response.body() + if (body == null) { + FLog.e( + ReactConstants.TAG, + "Got null body response from packager when requesting status") + callback.onPackagerStatusFetched(false) + return + } + val bodyString = + body.string() // cannot call body.string() twice, stored it into variable. + // https://github.com/square/okhttp/issues/1240#issuecomment-68142603 + if (PACKAGER_OK_STATUS != bodyString) { + FLog.e( + ReactConstants.TAG, + "Got unexpected response from packager when requesting status: ${bodyString}") + callback.onPackagerStatusFetched(false) + return + } + callback.onPackagerStatusFetched(true) + } + }) + } + + 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 = + String.format(Locale.US, PACKAGER_STATUS_URL_TEMPLATE, host) + } +}