@gfean/react-native-bundle-drop 0.6.0 → 0.8.0

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.
Files changed (86) hide show
  1. package/BundleDrop.podspec +96 -0
  2. package/README.md +249 -9
  3. package/android/build.gradle +72 -5
  4. package/android/src/main/java/com/bundledrop/BundleDropModule.kt +137 -13
  5. package/android/src/main/java/com/bundledrop/BundleDropNativePaths.kt +43 -13
  6. package/android/src/main/java/com/bundledrop/BundleDropOtaResolver.kt +35 -0
  7. package/android/src/main/java/com/bundledrop/BundleDropStartupRecovery.kt +1117 -0
  8. package/ios/BundleDropBridge.m +27 -0
  9. package/ios/BundleDropLocator.swift +31 -15
  10. package/ios/BundleDropModule.swift +106 -3
  11. package/ios/BundleDropOtaResolver.swift +137 -29
  12. package/ios/BundleDropStartupRecovery.swift +1108 -0
  13. package/ios/BundleDropStartupRecoveryAdapter.swift +301 -0
  14. package/lib/CLI/cli.js +9 -5
  15. package/lib/CLI/scripts/doctor.js +28 -9
  16. package/lib/CLI/scripts/expo/configure-expo.js +2 -1
  17. package/lib/CLI/scripts/init-config.d.ts +2 -2
  18. package/lib/CLI/scripts/init-config.js +23 -14
  19. package/lib/CLI/scripts/login-cli.js +2 -3
  20. package/lib/CLI/scripts/native/write-runtime-identity.d.ts +19 -0
  21. package/lib/CLI/scripts/native/write-runtime-identity.js +92 -0
  22. package/lib/CLI/scripts/sight-artifacts.d.ts +9 -1
  23. package/lib/CLI/scripts/sight-artifacts.js +20 -8
  24. package/lib/CLI/scripts/sight-assets.d.ts +15 -0
  25. package/lib/CLI/scripts/sight-assets.js +71 -0
  26. package/lib/CLI/scripts/sight-cli.d.ts +3 -0
  27. package/lib/CLI/scripts/sight-cli.js +50 -4
  28. package/lib/CLI/scripts/sight-compare/assets.d.ts +6 -0
  29. package/lib/CLI/scripts/sight-compare/assets.js +73 -0
  30. package/lib/CLI/scripts/sight-compare/dependencies.d.ts +22 -0
  31. package/lib/CLI/scripts/sight-compare/dependencies.js +289 -0
  32. package/lib/CLI/scripts/sight-compare/git.d.ts +22 -0
  33. package/lib/CLI/scripts/sight-compare/git.js +116 -0
  34. package/lib/CLI/scripts/sight-compare/inspect-project.d.ts +37 -0
  35. package/lib/CLI/scripts/sight-compare/inspect-project.js +191 -0
  36. package/lib/CLI/scripts/sight-compare/metadata.d.ts +4 -0
  37. package/lib/CLI/scripts/sight-compare/metadata.js +39 -0
  38. package/lib/CLI/scripts/sight-compare/process.d.ts +22 -0
  39. package/lib/CLI/scripts/sight-compare/process.js +221 -0
  40. package/lib/CLI/scripts/sight-compare/run.d.ts +3 -0
  41. package/lib/CLI/scripts/sight-compare/run.js +290 -0
  42. package/lib/CLI/scripts/sight-compare/snapshot.d.ts +26 -0
  43. package/lib/CLI/scripts/sight-compare/snapshot.js +279 -0
  44. package/lib/CLI/scripts/sight-compare/types.d.ts +87 -0
  45. package/lib/CLI/scripts/sight-compare/types.js +2 -0
  46. package/lib/CLI/scripts/sight-ota.d.ts +18 -0
  47. package/lib/CLI/scripts/sight-ota.js +260 -0
  48. package/lib/CLI/scripts/sight-session.d.ts +9 -2
  49. package/lib/CLI/scripts/sight-session.js +58 -20
  50. package/lib/CLI/scripts/upload-cli.d.ts +12 -2
  51. package/lib/CLI/scripts/upload-cli.js +74 -11
  52. package/lib/CLI/serverUrl.d.ts +3 -0
  53. package/lib/CLI/serverUrl.js +31 -0
  54. package/lib/bundleInfo.d.ts +2 -0
  55. package/lib/bundleInfo.js +16 -1
  56. package/lib/context.js +12 -2
  57. package/lib/fs/bundlePointer.d.ts +1 -17
  58. package/lib/fs/bundlePointer.js +5 -93
  59. package/lib/install/bundleInstallShared.js +4 -4
  60. package/lib/manager/downloadAndInstall.js +25 -22
  61. package/lib/manager/reporting.d.ts +8 -1
  62. package/lib/manager/reporting.js +17 -22
  63. package/lib/manager/rollbackState.d.ts +18 -54
  64. package/lib/manager/rollbackState.js +115 -216
  65. package/lib/manager/updateCheck.js +9 -3
  66. package/lib/manager/updateState.d.ts +2 -1
  67. package/lib/manager/updateState.js +59 -6
  68. package/lib/metro.d.ts +1 -1
  69. package/lib/metro.js +4 -3
  70. package/lib/native/bundleDropNative.d.ts +62 -0
  71. package/lib/native/bundleDropNative.js +266 -1
  72. package/lib/patch-engine/installFromPatchSet.js +2 -2
  73. package/lib/runtime/service.js +29 -71
  74. package/lib/runtime-delivery/bootstrapConfig.d.ts +29 -11
  75. package/lib/runtime-delivery/bootstrapConfig.js +83 -22
  76. package/lib/runtime-delivery/manifestState.d.ts +3 -1
  77. package/lib/runtime-delivery/manifestState.js +22 -1
  78. package/lib/runtime-delivery/manifestVerifier.js +7 -1
  79. package/lib/scripts/bundle.d.ts +12 -2
  80. package/lib/scripts/bundle.js +74 -21
  81. package/lib/scripts/download-bundle.d.ts +1 -0
  82. package/lib/scripts/download-bundle.js +2 -2
  83. package/lib/scripts/resolveModule.d.ts +2 -0
  84. package/lib/scripts/resolveModule.js +5 -0
  85. package/package.json +5 -2
  86. package/plugin/xcodeBuildPhase.js +2 -1
@@ -6,22 +6,19 @@ import android.util.Log
6
6
  import com.facebook.react.bridge.*
7
7
  import com.facebook.react.modules.network.OkHttpClientProvider
8
8
  import java.io.File
9
+ import org.json.JSONArray
10
+ import org.json.JSONObject
9
11
 
10
12
  class BundleDropModule(reactContext: ReactApplicationContext) :
11
13
  ReactContextBaseJavaModule(reactContext) {
12
14
 
13
- private var downloadedBundlePath: String? = null
14
-
15
15
  companion object {
16
- var latestBundlePath: String? = null
17
- private set
18
-
19
16
  fun getDownloadedBundlePath(context: Context): String? =
20
- BundleDropNativePaths.getDownloadedBundlePath(context)
17
+ BundleDropNativePaths.getDownloadedBundlePathPassive(context)
21
18
 
22
19
  @JvmStatic
23
20
  fun resolveJSBundleFile(context: Context, fallback: String?): String? {
24
- val path = getDownloadedBundlePath(context)
21
+ val path = BundleDropNativePaths.getDownloadedBundlePath(context)
25
22
  return if (!path.isNullOrEmpty()) path else fallback
26
23
  }
27
24
  }
@@ -30,10 +27,7 @@ class BundleDropModule(reactContext: ReactApplicationContext) :
30
27
 
31
28
  @ReactMethod
32
29
  fun getDownloadedBundlePath(promise: Promise) {
33
- val path = getDownloadedBundlePath(reactApplicationContext)
34
- downloadedBundlePath = path
35
- latestBundlePath = path
36
- promise.resolve(path)
30
+ promise.resolve(getDownloadedBundlePath(reactApplicationContext))
37
31
  }
38
32
 
39
33
  @ReactMethod
@@ -69,6 +63,95 @@ class BundleDropModule(reactContext: ReactApplicationContext) :
69
63
  }
70
64
  }
71
65
 
66
+ @ReactMethod
67
+ fun activateStartupCandidate(
68
+ hash: String,
69
+ maxCrashCount: Double,
70
+ healthCheckMode: String,
71
+ healthyAfterSec: Double,
72
+ promise: Promise,
73
+ ) {
74
+ try {
75
+ require(maxCrashCount.isFinite() && maxCrashCount >= 0 && maxCrashCount % 1.0 == 0.0) {
76
+ "maxCrashCount must be a non-negative integer"
77
+ }
78
+ require(maxCrashCount <= Int.MAX_VALUE) { "maxCrashCount is outside the supported range" }
79
+ val result = BundleDropStartupRecovery.controller(reactApplicationContext)
80
+ .activateCandidate(hash, maxCrashCount.toInt(), healthCheckMode, healthyAfterSec)
81
+ promise.resolve(Arguments.createMap().apply {
82
+ putString("hash", result.hash)
83
+ putString("bundlePath", result.bundlePath)
84
+ })
85
+ } catch (error: Exception) {
86
+ promise.reject("ERR_STARTUP_RECOVERY_ACTIVATE", error.message, error)
87
+ }
88
+ }
89
+
90
+ @ReactMethod
91
+ fun markStartupHealthy(hash: String, attemptId: String, promise: Promise) {
92
+ try {
93
+ val marked = BundleDropStartupRecovery.controller(reactApplicationContext)
94
+ .markHealthy(hash, attemptId)
95
+ promise.resolve(marked)
96
+ } catch (error: Exception) {
97
+ promise.reject("ERR_STARTUP_RECOVERY_HEALTH", error.message, error)
98
+ }
99
+ }
100
+
101
+ @ReactMethod
102
+ fun getStartupRecoveryState(promise: Promise) {
103
+ try {
104
+ val state = BundleDropStartupRecovery.controller(reactApplicationContext).snapshot()
105
+ promise.resolve(jsonObjectToWritableMap(state))
106
+ } catch (error: Exception) {
107
+ promise.reject("ERR_STARTUP_RECOVERY_STATE", error.message, error)
108
+ }
109
+ }
110
+
111
+ @ReactMethod
112
+ fun setStartupRecoveryRevokedHashes(hashes: ReadableArray, promise: Promise) {
113
+ try {
114
+ val values = buildSet {
115
+ for (index in 0 until hashes.size()) {
116
+ val hash = hashes.getString(index)
117
+ ?: throw IllegalArgumentException("Revoked bundle hashes must be strings")
118
+ add(hash)
119
+ }
120
+ }
121
+ BundleDropStartupRecovery.controller(reactApplicationContext).setRevokedHashes(values)
122
+ promise.resolve(true)
123
+ } catch (error: Exception) {
124
+ promise.reject("ERR_STARTUP_RECOVERY_REVOKE", error.message, error)
125
+ }
126
+ }
127
+
128
+ @ReactMethod
129
+ fun acknowledgeStartupRecovery(eventId: String, promise: Promise) {
130
+ try {
131
+ promise.resolve(
132
+ BundleDropStartupRecovery.controller(reactApplicationContext)
133
+ .acknowledgeRecovery(eventId),
134
+ )
135
+ } catch (error: Exception) {
136
+ promise.reject("ERR_STARTUP_RECOVERY_ACK", error.message, error)
137
+ }
138
+ }
139
+
140
+ @ReactMethod
141
+ fun rollbackStartupBundle(forceEmbedded: Boolean, promise: Promise) {
142
+ try {
143
+ val result = BundleDropStartupRecovery.controller(reactApplicationContext)
144
+ .rollbackStartupBundle(forceEmbedded)
145
+ promise.resolve(Arguments.createMap().apply {
146
+ putBoolean("rolledBack", result.rolledBack)
147
+ putBoolean("toEmbedded", result.toEmbedded)
148
+ result.hash?.let { putString("hash", it) }
149
+ })
150
+ } catch (error: Exception) {
151
+ promise.reject("ERR_STARTUP_RECOVERY_ROLLBACK", error.message, error)
152
+ }
153
+ }
154
+
72
155
  @ReactMethod(isBlockingSynchronousMethod = true)
73
156
  fun getImageManifestSync(): String? {
74
157
  return readImageManifestRaw()
@@ -332,12 +415,53 @@ class BundleDropModule(reactContext: ReactApplicationContext) :
332
415
 
333
416
  override fun getConstants(): MutableMap<String, Any> {
334
417
  val map = mutableMapOf<String, Any>()
335
- downloadedBundlePath?.let {
336
- map["downloadedBundlePath"] = it
418
+ map["startupRecoveryProtocolVersion"] = BundleDropStartupRecoveryController.PROTOCOL_VERSION
419
+ @Suppress("UNCHECKED_CAST")
420
+ (map as MutableMap<String, Any?>)["startupRecoverySelectedHash"] =
421
+ BundleDropStartupRecovery.startupSelectedHash()
422
+ BundleDropStartupRecovery.startupAttempt()?.let { (hash, attemptId) ->
423
+ map["startupRecoveryAttemptHash"] = hash
424
+ map["startupRecoveryAttemptId"] = attemptId
337
425
  }
338
426
  val context = reactApplicationContext
339
427
  map["DocumentDirectoryPath"] = context.filesDir.absolutePath
340
428
  map["LibraryDirectoryPath"] = context.filesDir.absolutePath
341
429
  return map
342
430
  }
431
+
432
+ private fun jsonObjectToWritableMap(json: JSONObject): WritableMap {
433
+ val map = Arguments.createMap()
434
+ val keys = json.keys()
435
+ while (keys.hasNext()) {
436
+ val key = keys.next()
437
+ putJsonValue(map, key, json.opt(key))
438
+ }
439
+ return map
440
+ }
441
+
442
+ private fun jsonArrayToWritableArray(json: JSONArray): WritableArray {
443
+ val array = Arguments.createArray()
444
+ for (index in 0 until json.length()) {
445
+ when (val value = json.opt(index)) {
446
+ null, JSONObject.NULL -> array.pushNull()
447
+ is JSONObject -> array.pushMap(jsonObjectToWritableMap(value))
448
+ is JSONArray -> array.pushArray(jsonArrayToWritableArray(value))
449
+ is Boolean -> array.pushBoolean(value)
450
+ is Number -> array.pushDouble(value.toDouble())
451
+ else -> array.pushString(value.toString())
452
+ }
453
+ }
454
+ return array
455
+ }
456
+
457
+ private fun putJsonValue(map: WritableMap, key: String, value: Any?) {
458
+ when (value) {
459
+ null, JSONObject.NULL -> map.putNull(key)
460
+ is JSONObject -> map.putMap(key, jsonObjectToWritableMap(value))
461
+ is JSONArray -> map.putArray(key, jsonArrayToWritableArray(value))
462
+ is Boolean -> map.putBoolean(key, value)
463
+ is Number -> map.putDouble(key, value.toDouble())
464
+ else -> map.putString(key, value.toString())
465
+ }
466
+ }
343
467
  }
@@ -8,8 +8,8 @@ import org.json.JSONObject
8
8
  /**
9
9
  * Resolves the on-disk OTA JS bundle path (no React / bridge types).
10
10
  *
11
- * Shared by [BundleDropModule] so cold-start `resolveJSBundleFile`, the JS bridge
12
- * `getDownloadedBundlePath`, and tests use the same logic — including the OTA-disabled gate.
11
+ * Cold-start resolution records a launch attempt, while bridge lookups are passive and never
12
+ * change the attempt selected for the current React runtime.
13
13
  */
14
14
  object BundleDropNativePaths {
15
15
  private const val KEY_BINARY_VERSION = "binary_version"
@@ -40,6 +40,9 @@ object BundleDropNativePaths {
40
40
  return binaryVersionKey(versionName, versionCode, runtimeVersion)
41
41
  }
42
42
 
43
+ internal fun currentBinaryIdentity(context: Context): String =
44
+ getBinaryVersionKey(context, readEmbeddedRuntimeVersion(context))
45
+
43
46
  internal fun readEmbeddedRuntimeVersion(context: Context): String? {
44
47
  return try {
45
48
  context.assets.open(BUILD_IDENTITY_ASSET).bufferedReader().use { reader ->
@@ -64,11 +67,37 @@ object BundleDropNativePaths {
64
67
 
65
68
  @JvmStatic
66
69
  fun getDownloadedBundlePath(context: Context): String? {
67
- if (!BundleDropOtaPrefs.isOtaEnabled(context)) return null
68
- return getDownloadedBundlePath(context, readEmbeddedRuntimeVersion(context))
70
+ if (!BundleDropOtaPrefs.isOtaEnabled(context)) {
71
+ BundleDropStartupRecovery.clearStartupSelection()
72
+ return null
73
+ }
74
+ resolveForBinary(context, readEmbeddedRuntimeVersion(context))
75
+ val selection = BundleDropStartupRecovery.selectForStartup(context)
76
+ logResolvedPath(selection.bundlePath)
77
+ return selection.bundlePath
69
78
  }
70
79
 
71
80
  internal fun getDownloadedBundlePath(context: Context, runtimeVersion: String?): String? {
81
+ if (!BundleDropOtaPrefs.isOtaEnabled(context)) {
82
+ BundleDropStartupRecovery.clearStartupSelection()
83
+ return null
84
+ }
85
+ resolveForBinary(context, runtimeVersion)
86
+ val selection = BundleDropStartupRecovery.selectForStartup(context)
87
+ logResolvedPath(selection.bundlePath)
88
+ return selection.bundlePath
89
+ }
90
+
91
+ internal fun getDownloadedBundlePathPassive(context: Context): String? {
92
+ if (!BundleDropOtaPrefs.isOtaEnabled(context)) return null
93
+ val resolved = resolveForBinary(context, readEmbeddedRuntimeVersion(context))
94
+ if (resolved == null) return null
95
+ val path = BundleDropStartupRecovery.controller(context).resolvePassive()
96
+ logResolvedPath(path)
97
+ return path
98
+ }
99
+
100
+ private fun resolveForBinary(context: Context, runtimeVersion: String?): String? {
72
101
  val bundleDropRoot = File(context.filesDir, "bundle-drop")
73
102
  val result = BundleDropOtaResolver.resolve(
74
103
  bundleDropRoot = bundleDropRoot,
@@ -79,16 +108,17 @@ object BundleDropNativePaths {
79
108
 
80
109
  result.storedVersion?.let { setStoredBinaryVersion(context, it) }
81
110
 
82
- if (result.bundlePath == null) {
83
- if (result.clearedOta) {
84
- Log.d("BundleDrop", "Binary updated, clearing OTA bundle")
85
- } else {
86
- Log.d("BundleDrop", "📦 No OTA bundle found.")
87
- }
88
- } else {
89
- Log.d("BundleDrop", "🔁 Using OTA bundle at: ${result.bundlePath}")
111
+ if (result.clearedOta) {
112
+ Log.d("BundleDrop", "Binary updated, clearing OTA bundle")
90
113
  }
91
-
92
114
  return result.bundlePath
93
115
  }
116
+
117
+ private fun logResolvedPath(path: String?) {
118
+ if (path == null) {
119
+ Log.d("BundleDrop", "📦 No OTA bundle found.")
120
+ } else {
121
+ Log.d("BundleDrop", "🔁 Using OTA bundle at: $path")
122
+ }
123
+ }
94
124
  }
@@ -43,6 +43,39 @@ object BundleDropOtaResolver {
43
43
  }
44
44
  }
45
45
 
46
+ /** Verify an installed bundle independently of the active pointer. */
47
+ internal fun readBundleForHash(bundleDropRoot: File, hash: String): String? {
48
+ if (!bundleHashPattern.matches(hash)) return null
49
+ return try {
50
+ val bundleDir = File(File(bundleDropRoot, "bundles"), hash)
51
+ val bundleFile = File(bundleDir, "main.jsbundle")
52
+ val manifestFile = File(bundleDir, "bundle-manifest.json")
53
+ if (!bundleFile.exists() || !manifestFile.exists()) return null
54
+ val manifest = JSONObject(manifestFile.readText())
55
+ if (
56
+ manifest.optInt("manifestVersion", -1) != 1 ||
57
+ manifest.optString("bundleHash", "") != hash ||
58
+ !verifyBundleDir(bundleDir, manifest, hash)
59
+ ) {
60
+ return null
61
+ }
62
+ bundleFile.absolutePath
63
+ } catch (_: Exception) {
64
+ null
65
+ }
66
+ }
67
+
68
+ internal fun readBundleRuntimeVersion(bundleDropRoot: File, hash: String): String? {
69
+ readBundleForHash(bundleDropRoot, hash) ?: return null
70
+ return try {
71
+ JSONObject(File(File(File(bundleDropRoot, "bundles"), hash), "bundle-manifest.json").readText())
72
+ .optString("runtimeVersion", "")
73
+ .takeIf(String::isNotEmpty)
74
+ } catch (_: Exception) {
75
+ null
76
+ }
77
+ }
78
+
46
79
  private fun verifyBundleDir(bundleDir: File, manifest: JSONObject, expectedHash: String): Boolean {
47
80
  val files = manifest.optJSONArray("files") ?: return false
48
81
  if (manifest.optString("platform", "") != "android") {
@@ -184,6 +217,7 @@ object BundleDropOtaResolver {
184
217
  File(bundleDropRoot, "current.json"),
185
218
  File(bundleDropRoot, "previous.json"),
186
219
  File(bundleDropRoot, "state.json"),
220
+ File(bundleDropRoot, BundleDropStartupRecoveryController.RECOVERY_LEDGER),
187
221
  File(filesDir, "bundle-info.json"),
188
222
  )
189
223
  filesToClear.forEach {
@@ -196,6 +230,7 @@ object BundleDropOtaResolver {
196
230
  File(bundleDropRoot, "current.json"),
197
231
  File(bundleDropRoot, "previous.json"),
198
232
  File(bundleDropRoot, "state.json"),
233
+ File(bundleDropRoot, BundleDropStartupRecoveryController.RECOVERY_LEDGER),
199
234
  File(filesDir, "bundle-info.json"),
200
235
  ).any { it.exists() }
201
236
  }