mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Refactor DevServerHelper to separate checking if packager running
Summary: Changelog: [Internal] Separate the functionality of the isPackagerRunning() function into a new class PackagerStatusCheck with the intention of being able to use this without needing a DevServerHelper Reviewed By: makovkastar, ShikaSD Differential Revision: D29933318 fbshipit-source-id: d708bb987b08634015d6ee6b6c8989faba416c5a
This commit is contained in:
committed by
Facebook GitHub Bot
parent
6d4fff2e5c
commit
3081db28a7
@@ -41,7 +41,6 @@ import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okio.Okio;
|
||||
import okio.Sink;
|
||||
import org.json.JSONArray;
|
||||
@@ -63,8 +62,6 @@ import org.json.JSONObject;
|
||||
public class DevServerHelper {
|
||||
public static final String RELOAD_APP_EXTRA_JS_PROXY = "jsproxy";
|
||||
|
||||
private static final String PACKAGER_OK_STATUS = "packager-status:running";
|
||||
|
||||
private static final int HTTP_CONNECT_TIMEOUT_MS = 5000;
|
||||
|
||||
private static final String DEBUGGER_MSG_DISABLE = "{ \"id\":1,\"method\":\"Debugger.disable\" }";
|
||||
@@ -113,6 +110,7 @@ public class DevServerHelper {
|
||||
private final DevInternalSettings mSettings;
|
||||
private final OkHttpClient mClient;
|
||||
private final BundleDownloader mBundleDownloader;
|
||||
private final PackagerStatusCheck mPackagerStatusCheck;
|
||||
private final String mPackageName;
|
||||
|
||||
private @Nullable JSPackagerClient mPackagerClient;
|
||||
@@ -132,7 +130,7 @@ public class DevServerHelper {
|
||||
.writeTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.build();
|
||||
mBundleDownloader = new BundleDownloader(mClient);
|
||||
|
||||
mPackagerStatusCheck = new PackagerStatusCheck(mClient);
|
||||
mPackageName = packageName;
|
||||
}
|
||||
|
||||
@@ -477,60 +475,13 @@ public class DevServerHelper {
|
||||
}
|
||||
|
||||
public void isPackagerRunning(final PackagerStatusCallback callback) {
|
||||
String statusURL =
|
||||
createPackagerStatusURL(mSettings.getPackagerConnectionSettings().getDebugServerHost());
|
||||
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, "http://%s/status", host);
|
||||
String host = mSettings.getPackagerConnectionSettings().getDebugServerHost();
|
||||
if (host == null) {
|
||||
FLog.w(ReactConstants.TAG, "No packager host configured.");
|
||||
callback.onPackagerStatusFetched(false);
|
||||
} else {
|
||||
mPackagerStatusCheck.run(host, callback);
|
||||
}
|
||||
}
|
||||
|
||||
private String createLaunchJSDevtoolsCommandUrl() {
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its 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.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. */
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user