@turbopush/react-native-code-push 10.2.6 → 10.3.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -150,12 +150,20 @@ public class CodePushNativeModule extends BaseJavaModule {
150
150
  latestJSBundleLoader = JSBundleLoader.createFileLoader(latestJSBundleFile);
151
151
  }
152
152
 
153
- Field bundleLoaderField = reactHostDelegate.getClass().getDeclaredField("jsBundleLoader");
153
+ // ExpoReactHostDelegate (Kotlin) exposes jsBundleLoader as a computed property whose
154
+ // backing field is named "_jsBundleLoader". Setting it makes the getter return the new
155
+ // loader on the next reload. Fall back to the non-prefixed name for other delegates.
156
+ Field bundleLoaderField;
157
+ try {
158
+ bundleLoaderField = reactHostDelegate.getClass().getDeclaredField("_jsBundleLoader");
159
+ } catch (NoSuchFieldException ignored) {
160
+ bundleLoaderField = reactHostDelegate.getClass().getDeclaredField("jsBundleLoader");
161
+ }
154
162
  bundleLoaderField.setAccessible(true);
155
163
  bundleLoaderField.set(reactHostDelegate, latestJSBundleLoader);
156
164
 
157
165
  } catch (NoSuchFieldException nsfe) {
158
- CodePushUtils.log("Field 'jsBundleLoader' NOT FOUND on " + (reactHostDelegate != null ? reactHostDelegate.getClass().getName() : "null") + ". This is an EXPECTED and IGNORED failure with ExpoReactHostDelegate. Will rely on reactHost.reload(). Original log: Unable to set JSBundle of ReactHostDelegate - CodePush may not support this version of React Native");
166
+ CodePushUtils.log("Neither '_jsBundleLoader' nor 'jsBundleLoader' field found on " + (reactHostDelegate != null ? reactHostDelegate.getClass().getName() : "null") + ". Unable to update JS bundle for reload.");
159
167
  // DO NOT THROW for NoSuchFieldException.
160
168
  }catch (Exception e) {
161
169
  CodePushUtils.log("Unable to set JSBundle of ReactHostDelegate - CodePush may not support this version of React Native");
@@ -197,31 +205,26 @@ public class CodePushNativeModule extends BaseJavaModule {
197
205
 
198
206
 
199
207
  try {
200
- if (reactHost instanceof ReactHostImpl) {
201
- ReactHostDelegate delegate = getReactHostDelegate((ReactHostImpl) reactHost);
208
+ if (reactHost instanceof ReactHostImpl) {
209
+ ReactHostDelegate delegate = getReactHostDelegate((ReactHostImpl) reactHost);
202
210
  if (delegate != null) {
203
- // #2) Update the locally stored JS bundle file path
204
- setJSBundle(delegate, latestJSBundleFile);
211
+ // #2) Update the bundle loader on the delegate so the next reload uses it
212
+ setJSBundle(delegate, latestJSBundleFile);
205
213
  } else {
206
214
  CodePushUtils.log("Could not get ReactHostDelegate from ReactHostImpl.");
207
215
  }
208
216
  } else {
209
- CodePushUtils.log("ReactHost is not a direct ReactHostImpl instance (" + reactHost.getClass().getName() + "), skipping direct setJSBundle reflection attempt. This is expected with Expo.");
217
+ CodePushUtils.log("ReactHost is not ReactHostImpl (" + reactHost.getClass().getName() + ")");
210
218
  }
211
- } catch (ClassCastException cce) {
212
- CodePushUtils.log(new Exception("ClassCastException trying to get/use ReactHostDelegate. Skipping reflection call to setJSBundle. This is expected for Expo.", cce));
213
- }catch (Exception e) {
214
- // Catch any unexpected errors from the attempt to call setJSBundle, e.g., if getReactHostDelegate itself fails
215
- CodePushUtils.log("Exception during the reflective setJSBundle block: " + e.getMessage());
219
+ } catch (Exception e) {
220
+ CodePushUtils.log("Exception during setJSBundle: " + e.getMessage());
216
221
  }
217
222
 
218
- // #3) Get the context creation method
223
+ // #3) Trigger reload and initialize update state
219
224
  try {
220
225
  reactHost.reload("CodePush triggers reload");
221
226
  mCodePush.initializeUpdateAfterRestart();
222
227
  } catch (Exception e) {
223
- // The recreation method threw an unknown exception
224
- // so just simply fallback to restarting the Activity (if it exists)
225
228
  loadBundleLegacy();
226
229
  }
227
230
 
@@ -856,14 +859,23 @@ public class CodePushNativeModule extends BaseJavaModule {
856
859
  }
857
860
 
858
861
  public ReactHostDelegate getReactHostDelegate(ReactHostImpl reactHostImpl) {
862
+ // RN 0.76+ (used in Expo SDK 55+) names the field "reactHostDelegate" (Kotlin constructor
863
+ // property, no "m" prefix). Older versions used "mReactHostDelegate".
864
+ String[] candidateFields = {"reactHostDelegate", "mReactHostDelegate"};
859
865
  try {
860
866
  Class<?> clazz = reactHostImpl.getClass();
861
- Field field = clazz.getDeclaredField("mReactHostDelegate");
862
- field.setAccessible(true);
863
-
864
- // Get the value of the field for the provided instance
865
- return (ReactHostDelegate) field.get(reactHostImpl);
866
- } catch (NoSuchFieldException | IllegalAccessException e) {
867
+ for (String fieldName : candidateFields) {
868
+ try {
869
+ Field field = clazz.getDeclaredField(fieldName);
870
+ field.setAccessible(true);
871
+ return (ReactHostDelegate) field.get(reactHostImpl);
872
+ } catch (NoSuchFieldException ignored) {
873
+ // try next candidate
874
+ }
875
+ }
876
+ CodePushUtils.log("Could not find reactHostDelegate field on " + clazz.getName());
877
+ return null;
878
+ } catch (IllegalAccessException e) {
867
879
  e.printStackTrace();
868
880
  return null;
869
881
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turbopush/react-native-code-push",
3
- "version": "10.2.6",
3
+ "version": "10.3.0-rc.1",
4
4
  "description": "React Native plugin for the CodePush service",
5
5
  "main": "CodePush.js",
6
6
  "typings": "typings/react-native-code-push.d.ts",
@@ -1,7 +1,21 @@
1
1
  const packageJson = require("./package.json");
2
+ const { Platform } = require("react-native");
3
+
4
+ const { major, minor, patch } = Platform.constants.reactNativeVersion;
5
+ const rnVersion = `${major}.${minor}.${patch}`;
6
+
7
+ let expoVersion;
8
+ try {
9
+ expoVersion = require("expo/package.json").version;
10
+ } catch (_) { }
11
+
12
+ let turbopushExpoPluginVersion;
13
+ try {
14
+ turbopushExpoPluginVersion = require("@turbopush/turbopush-expo-plugin/package.json").version;
15
+ } catch (_) { }
2
16
 
3
17
  module.exports = {
4
- async request(verb, url, requestBody, callback) {
18
+ request(verb, url, requestBody, callback) {
5
19
  if (typeof requestBody === "function") {
6
20
  callback = requestBody;
7
21
  requestBody = null;
@@ -12,26 +26,32 @@ module.exports = {
12
26
  "Content-Type": "application/json",
13
27
  "X-CodePush-Plugin-Name": packageJson.name,
14
28
  "X-CodePush-Plugin-Version": packageJson.version,
15
- "X-CodePush-SDK-Version": packageJson.dependencies["code-push"]
29
+ "X-CodePush-SDK-Version": packageJson.dependencies["code-push"],
30
+ "X-React-Native-Version": rnVersion,
31
+ ...(expoVersion && { "X-Expo-Version": expoVersion }),
32
+ ...(turbopushExpoPluginVersion && { "X-Turbopush-Expo-Plugin-Version": turbopushExpoPluginVersion }),
16
33
  };
17
34
 
18
35
  if (requestBody && typeof requestBody === "object") {
19
36
  requestBody = JSON.stringify(requestBody);
20
37
  }
21
38
 
22
- try {
23
- const response = await fetch(url, {
24
- method: getHttpMethodName(verb),
25
- headers: headers,
26
- body: requestBody
27
- });
28
-
29
- const statusCode = response.status;
30
- const body = await response.text();
31
- callback(null, { statusCode, body });
32
- } catch (err) {
33
- callback(err);
34
- }
39
+ const xhr = new XMLHttpRequest();
40
+ xhr.open(getHttpMethodName(verb), url, true);
41
+
42
+ Object.entries(headers).forEach(([key, value]) => {
43
+ xhr.setRequestHeader(key, value);
44
+ });
45
+
46
+ xhr.onload = () => {
47
+ callback(null, { statusCode: xhr.status, body: xhr.responseText });
48
+ };
49
+
50
+ xhr.onerror = () => {
51
+ callback(new Error(`Network request failed for url ${url}`));
52
+ };
53
+
54
+ xhr.send(requestBody || null);
35
55
  }
36
56
  };
37
57