native-update-react-native 1.0.0 → 1.1.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.
package/README.md CHANGED
@@ -5,7 +5,7 @@ runs on the same Native Update account and licence as the Capacitor, Android and
5
5
 
6
6
  | | |
7
7
  |---|---|
8
- | Version | `1.0.0` |
8
+ | Version | `1.1.0` |
9
9
  | React Native | 0.80 or newer (New Architecture, TurboModule) |
10
10
  | Android | minSdk 26 |
11
11
  | iOS | not yet: the package links on Android only for now |
@@ -38,3 +38,4 @@ dependencies {
38
38
  implementation "com.google.android.play:app-update-ktx:2.1.0"
39
39
  implementation "com.google.android.play:review:2.0.2"
40
40
  }
41
+
@@ -37,7 +37,7 @@ import java.io.File
37
37
  */
38
38
  public object NativeUpdate {
39
39
  /** This SDK's version, sent as `X-Sdk-Version`. Must equal the Gradle `VERSION_NAME` (checked by the build). */
40
- public const val SDK_VERSION: String = "1.0.0"
40
+ public const val SDK_VERSION: String = "1.1.0"
41
41
 
42
42
  private const val TAG = "NativeUpdate"
43
43
 
@@ -46,6 +46,8 @@ public object NativeUpdate {
46
46
  val config: NativeUpdateConfig,
47
47
  val clientSdk: String,
48
48
  val sdkVersion: String,
49
+ val accepted: Set<String>,
50
+ val runtimeKey: String?,
49
51
  val store: BundleStore,
50
52
  val client: UpdateClient,
51
53
  val binary: String,
@@ -68,7 +70,9 @@ public object NativeUpdate {
68
70
  * checks on the calling thread: one SHA-256 and signature check of the active bundle.
69
71
  *
70
72
  * Calling it again with an identical configuration does nothing; a different one is refused until [reset].
71
- * [clientSdk] and [sdkVersion] are for the Flutter and React Native wrappers; apps leave them out.
73
+ * [clientSdk], [sdkVersion], [acceptCodeUpdates] and [runtimeKey] are for the Flutter and React Native wrappers;
74
+ * apps leave them out. [acceptCodeUpdates] (Flutter only) also asks for `dart_aot` code patches built for the
75
+ * engine whose libflutter.so hashes to [runtimeKey].
72
76
  */
73
77
  @JvmStatic
74
78
  @JvmOverloads
@@ -78,9 +82,16 @@ public object NativeUpdate {
78
82
  config: NativeUpdateConfig,
79
83
  clientSdk: String = "android",
80
84
  sdkVersion: String = SDK_VERSION,
85
+ acceptCodeUpdates: Boolean = false,
86
+ runtimeKey: String? = null,
81
87
  ) {
88
+ val accepted = acceptedPayloads(clientSdk, acceptCodeUpdates && runtimeKey != null)
82
89
  state?.let { current ->
83
- if (current.config.fingerprint() == config.fingerprint() && current.clientSdk == clientSdk) return
90
+ if (current.config.fingerprint() == config.fingerprint() && current.clientSdk == clientSdk &&
91
+ current.accepted == accepted
92
+ ) {
93
+ return
94
+ }
84
95
  throw NativeUpdateException(
85
96
  ErrorCodes.VALIDATION_ERROR,
86
97
  "Configuration is locked after initialization; call reset() first",
@@ -92,13 +103,15 @@ public object NativeUpdate {
92
103
  throw NativeUpdateException(ErrorCodes.INSECURE_URL, "A plain-http test server is allowed only in debug builds")
93
104
  }
94
105
  val store = BundleStore(app)
95
- val binary = AppInfo.binaryIdentity(app)
106
+ val binary = AppInfo.binaryIdentity(app, perInstall = accepted.contains(PAYLOAD_DART_AOT))
96
107
  val fresh =
97
108
  State(
98
109
  context = app,
99
110
  config = config,
100
111
  clientSdk = clientSdk,
101
112
  sdkVersion = sdkVersion,
113
+ accepted = accepted,
114
+ runtimeKey = runtimeKey,
102
115
  store = store,
103
116
  client = UpdateClient(allowLoopbackHttp = debuggable),
104
117
  binary = binary,
@@ -159,12 +172,14 @@ public object NativeUpdate {
159
172
  certSha256 = AppInfo.signingCertSha256(current.context),
160
173
  clientSdk = current.clientSdk,
161
174
  sdkVersion = current.sdkVersion,
175
+ acceptPayload = current.accepted.sorted().joinToString(","),
176
+ runtimeKey = current.runtimeKey.takeIf { PAYLOAD_DART_AOT in current.accepted },
162
177
  )
163
178
  val result = current.client.check(current.config.serverUrl, current.config.channel, identity)
164
179
  if (result !is UpdateCheckResult.Available) return@withContext result
165
180
  val info = result.info
166
181
  when {
167
- info.payloadType != acceptedPayload(current.clientSdk) ->
182
+ info.payloadType !in current.accepted ->
168
183
  UpdateCheckResult.UpToDate("The latest release is not a payload this SDK can apply", "PAYLOAD_NOT_SUPPORTED")
169
184
  current.store.isRejected(info.bundleId) ->
170
185
  UpdateCheckResult.UpToDate("This release rolled back on this device", "ROLLED_BACK")
@@ -197,7 +212,7 @@ public object NativeUpdate {
197
212
  val zip = store.zipOf(info.bundleId)
198
213
  zip.delete()
199
214
  if (!partial.renameTo(zip)) throw NativeUpdateException(ErrorCodes.EXTRACTION_ERROR, "Bundle could not be stored")
200
- store.record(info.bundleId, info.version, info.checksum.lowercase(), info.signature)
215
+ store.record(info.bundleId, info.version, info.checksum.lowercase(), info.signature, info.payloadType)
201
216
  when (activation) {
202
217
  Activation.NEXT_START -> store.stage(info.bundleId, current.binary)
203
218
  Activation.NOW -> store.activate(info.bundleId, current.binary)
@@ -270,7 +285,25 @@ public object NativeUpdate {
270
285
  }
271
286
  }
272
287
 
273
- private fun acceptedPayload(clientSdk: String): String = if (clientSdk == "react-native") "web_bundle" else "assets"
288
+ /** Payload type of a Dart code patch (Flutter, sideloaded apps only). */
289
+ public const val PAYLOAD_DART_AOT: String = "dart_aot"
290
+
291
+ private fun acceptedPayloads(
292
+ clientSdk: String,
293
+ codeUpdates: Boolean,
294
+ ): Set<String> =
295
+ when {
296
+ clientSdk == "react-native" -> setOf("web_bundle")
297
+ clientSdk == "flutter" && codeUpdates -> setOf("assets", PAYLOAD_DART_AOT)
298
+ else -> setOf("assets")
299
+ }
300
+
301
+ /**
302
+ * Payload type of the bundle in use, or null while the app's own files run. Wrappers load code only from a
303
+ * bundle whose type is [PAYLOAD_DART_AOT]: an asset bundle that happens to contain a .so is never executed.
304
+ */
305
+ @JvmStatic
306
+ public fun activePayloadType(): String? = require().store.activeId?.let { require().store.payloadOf(it) }
274
307
 
275
308
  private fun require(): State =
276
309
  state ?: throw NativeUpdateException(ErrorCodes.NOT_INITIALIZED, "Call NativeUpdate.init() first")
@@ -11,12 +11,19 @@ internal object AppInfo {
11
11
  fun versionName(context: Context): String? =
12
12
  runCatching { packageInfo(context, 0).versionName }.getOrNull()
13
13
 
14
- /** `versionCode:versionName`; changes on every store or sideload update, which retires older OTA bundles. */
15
- fun binaryIdentity(context: Context): String =
14
+ /**
15
+ * `versionCode:versionName`; changes on every store or sideload update, which retires older OTA bundles.
16
+ * With [perInstall] the install time is added, so ANY reinstall retires them, even one that kept both version
17
+ * fields: a Dart code patch replaces the app's compiled code and must never outlive the APK it was built for.
18
+ */
19
+ fun binaryIdentity(
20
+ context: Context,
21
+ perInstall: Boolean = false,
22
+ ): String =
16
23
  runCatching {
17
24
  val info = packageInfo(context, 0)
18
25
  val code = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) info.longVersionCode else legacyCode(info)
19
- "$code:${info.versionName}"
26
+ if (perInstall) "$code:${info.versionName}:${info.lastUpdateTime}" else "$code:${info.versionName}"
20
27
  }.getOrDefault("unknown")
21
28
 
22
29
  fun isDebuggable(context: Context): Boolean = (context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
@@ -39,6 +39,9 @@ internal class BundleStore(
39
39
 
40
40
  fun signatureOf(id: String): String? = prefs.getString(KEY_SIGNATURE + id, null)
41
41
 
42
+ /** `assets`, `web_bundle` or `dart_aot`, as the server delivered it. Only a `dart_aot` bundle is ever run as code. */
43
+ fun payloadOf(id: String): String? = prefs.getString(KEY_PAYLOAD + id, null)
44
+
42
45
  fun directoryOf(id: String): File = File(bundlesDir, id)
43
46
 
44
47
  fun zipOf(id: String): File = File(bundlesDir, "$id.zip")
@@ -57,8 +60,10 @@ internal class BundleStore(
57
60
  version: String,
58
61
  checksum: String,
59
62
  signature: String?,
63
+ payloadType: String,
60
64
  ) {
61
65
  prefs.edit()
66
+ .putString(KEY_PAYLOAD + id, payloadType)
62
67
  .putString(KEY_VERSION + id, version)
63
68
  .putString(KEY_CHECKSUM + id, checksum)
64
69
  .apply { if (signature != null) putString(KEY_SIGNATURE + id, signature) else remove(KEY_SIGNATURE + id) }
@@ -170,7 +175,12 @@ internal class BundleStore(
170
175
  }
171
176
 
172
177
  private fun forget(id: String) {
173
- prefs.edit().remove(KEY_VERSION + id).remove(KEY_CHECKSUM + id).remove(KEY_SIGNATURE + id).commit()
178
+ prefs.edit()
179
+ .remove(KEY_VERSION + id)
180
+ .remove(KEY_CHECKSUM + id)
181
+ .remove(KEY_SIGNATURE + id)
182
+ .remove(KEY_PAYLOAD + id)
183
+ .commit()
174
184
  }
175
185
 
176
186
  private fun clearState() {
@@ -204,6 +214,7 @@ internal class BundleStore(
204
214
  const val KEY_VERSION = "version:"
205
215
  const val KEY_CHECKSUM = "checksum:"
206
216
  const val KEY_SIGNATURE = "signature:"
217
+ const val KEY_PAYLOAD = "payload:"
207
218
  const val MAX_UNCONFIRMED_STARTS = 2
208
219
  const val MAX_REJECTED = 20
209
220
  }
@@ -23,6 +23,10 @@ internal data class ClientIdentity(
23
23
  val certSha256: String?,
24
24
  val clientSdk: String,
25
25
  val sdkVersion: String,
26
+ /** Comma-separated payload types this client can apply (X-Accept-Payload). */
27
+ val acceptPayload: String,
28
+ /** Flutter code updates only: SHA-256 of the running libflutter.so (X-Runtime-Key). */
29
+ val runtimeKey: String? = null,
26
30
  )
27
31
 
28
32
  /**
@@ -48,6 +52,8 @@ internal class UpdateClient(
48
52
  put("X-Client-Sdk", identity.clientSdk)
49
53
  put("X-Sdk-Version", identity.sdkVersion)
50
54
  put("X-Android-Package", identity.packageName)
55
+ put("X-Accept-Payload", identity.acceptPayload)
56
+ identity.runtimeKey?.let { put("X-Runtime-Key", it) }
51
57
  identity.appVersion?.let { put("X-App-Version", it) }
52
58
  identity.certSha256?.let { put("X-Android-Cert-Sha256", it) }
53
59
  }
@@ -65,6 +71,8 @@ internal class UpdateClient(
65
71
  }
66
72
  } catch (failure: IOException) {
67
73
  throw NativeUpdateException(ErrorCodes.NETWORK_ERROR, "Update check failed: ${failure.javaClass.simpleName}", failure)
74
+ } catch (failure: SecurityException) {
75
+ throw NativeUpdateException(ErrorCodes.NETWORK_ERROR, "Update check blocked: the app lacks the INTERNET permission", failure)
68
76
  }
69
77
  return parseCheck(body)
70
78
  }
@@ -105,6 +113,9 @@ internal class UpdateClient(
105
113
  } catch (failure: IOException) {
106
114
  destination.delete()
107
115
  throw NativeUpdateException(ErrorCodes.DOWNLOAD_ERROR, "Download stopped: ${failure.javaClass.simpleName}", failure)
116
+ } catch (failure: SecurityException) {
117
+ destination.delete()
118
+ throw NativeUpdateException(ErrorCodes.DOWNLOAD_ERROR, "Download blocked: the app lacks the INTERNET permission", failure)
108
119
  } catch (failure: NativeUpdateException) {
109
120
  destination.delete()
110
121
  throw failure
@@ -21,7 +21,7 @@ import com.aoneahsan.nativeupdate.android.NativeUpdateConfig
21
21
  */
22
22
  object NativeUpdateReactNative {
23
23
  /** This package's version, sent as X-Sdk-Version. Kept equal to package.json by the package's version check. */
24
- const val VERSION: String = "1.0.0"
24
+ const val VERSION: String = "1.1.0"
25
25
 
26
26
  /** File name of the JavaScript bundle inside an update ZIP (Metro's default for Android). */
27
27
  const val BUNDLE_FILE: String = "index.android.bundle"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "native-update-react-native",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Signed over-the-air JavaScript bundle updates, Play in-app update and in-app review for React Native apps, on Native Update.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",