@hot-updater/react-native 1.0.0-rc.4 → 1.0.0-rc.40

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 (306) hide show
  1. package/android/build.gradle +13 -2
  2. package/android/proguard-rules.pro +8 -2
  3. package/android/src/bridge/BridgeReactInstance.kt +38 -0
  4. package/android/src/bridgeless/BridgeReactInstance.kt +24 -0
  5. package/android/src/main/java/com/hotupdater/ActivityStartGate.kt +94 -0
  6. package/android/src/main/java/com/hotupdater/BuiltInAssetResolver.kt +286 -0
  7. package/android/src/main/java/com/hotupdater/BundleFileStorageService.kt +1064 -844
  8. package/android/src/main/java/com/hotupdater/BundleMetadata.kt +50 -11
  9. package/android/src/main/java/com/hotupdater/FileManagerService.kt +45 -0
  10. package/android/src/main/java/com/hotupdater/HotUpdaterException.kt +287 -22
  11. package/android/src/main/java/com/hotupdater/HotUpdaterImpl.kt +46 -30
  12. package/android/src/main/java/com/hotupdater/HotUpdaterRecoveryManager.kt +75 -192
  13. package/android/src/main/java/com/hotupdater/HotUpdaterRecoveryState.kt +64 -0
  14. package/android/src/main/java/com/hotupdater/InstallIdentityService.kt +37 -0
  15. package/android/src/main/java/com/hotupdater/KeyValueStorageService.kt +86 -0
  16. package/android/src/main/java/com/hotupdater/NetworkState.kt +31 -0
  17. package/android/src/main/java/com/hotupdater/NoBackupStorage.kt +17 -0
  18. package/android/src/main/java/com/hotupdater/OkHttpDownloadService.kt +127 -50
  19. package/android/src/main/java/com/hotupdater/ReactNativeValueConverters.kt +34 -0
  20. package/android/src/main/java/com/hotupdater/TarArchiveInputStream.kt +214 -375
  21. package/android/src/main/java/com/hotupdater/TarBrArchiveExtractor.kt +119 -0
  22. package/android/src/newarch/HotUpdater.kt +7 -9
  23. package/android/src/newarch/HotUpdaterModule.kt +109 -84
  24. package/android/src/newarch/JavaScriptExceptionHooks.kt +48 -0
  25. package/android/src/newarch/ReactIntegrationManager.kt +6 -32
  26. package/android/src/oldarch/HotUpdater.kt +7 -9
  27. package/android/src/oldarch/HotUpdaterModule.kt +98 -74
  28. package/android/src/oldarch/HotUpdaterSpec.kt +4 -6
  29. package/android/src/oldarch/JavaScriptExceptionHooks.kt +71 -0
  30. package/ios/HotUpdater/Internal/BrotliFileDecompressor.swift +167 -0
  31. package/ios/HotUpdater/Internal/BuiltInAssetResolver.swift +196 -0
  32. package/ios/HotUpdater/Internal/BundleFileStorageService.swift +1032 -1116
  33. package/ios/HotUpdater/Internal/BundleMetadata.swift +61 -54
  34. package/ios/HotUpdater/Internal/FileManagerService.swift +48 -0
  35. package/ios/HotUpdater/Internal/FileUtilities.swift +64 -0
  36. package/ios/HotUpdater/Internal/HotUpdater-Bridging-Header.h +0 -1
  37. package/ios/HotUpdater/Internal/HotUpdater.mm +32 -67
  38. package/ios/HotUpdater/Internal/HotUpdaterImpl.swift +229 -73
  39. package/ios/HotUpdater/Internal/KeyValueStorageService.swift +71 -0
  40. package/ios/HotUpdater/Internal/NotificationExtension.swift +0 -2
  41. package/ios/HotUpdater/Internal/TarArchiveExtractor.swift +182 -311
  42. package/ios/HotUpdater/Internal/URLSessionDownloadService.swift +119 -71
  43. package/ios/HotUpdater/Internal/UpdateFailure.swift +390 -0
  44. package/ios/HotUpdater/Recovery/HotUpdaterRecovery.h +16 -0
  45. package/ios/HotUpdater/Recovery/HotUpdaterRecovery.mm +83 -0
  46. package/ios/HotUpdater/Recovery/HotUpdaterRecoveryHooks.mm +49 -0
  47. package/lib/commonjs/appReady.js +102 -0
  48. package/lib/commonjs/appReady.js.map +1 -0
  49. package/lib/commonjs/checkForUpdate.js +120 -68
  50. package/lib/commonjs/checkForUpdate.js.map +1 -1
  51. package/lib/commonjs/clientPlugin.js +13 -0
  52. package/lib/commonjs/clientPlugin.js.map +1 -0
  53. package/lib/commonjs/error.js +4 -13
  54. package/lib/commonjs/error.js.map +1 -1
  55. package/lib/commonjs/fetchJSON.js +16 -27
  56. package/lib/commonjs/fetchJSON.js.map +1 -1
  57. package/lib/commonjs/fetchUpdateResponse.js +60 -0
  58. package/lib/commonjs/fetchUpdateResponse.js.map +1 -0
  59. package/lib/commonjs/httpClient.js +63 -94
  60. package/lib/commonjs/httpClient.js.map +1 -1
  61. package/lib/commonjs/index.js +71 -18
  62. package/lib/commonjs/index.js.map +1 -1
  63. package/lib/commonjs/native.js +74 -60
  64. package/lib/commonjs/native.js.map +1 -1
  65. package/lib/commonjs/pluginHost.js +45 -0
  66. package/lib/commonjs/pluginHost.js.map +1 -0
  67. package/lib/commonjs/releaseCatalogCache.js +36 -87
  68. package/lib/commonjs/releaseCatalogCache.js.map +1 -1
  69. package/lib/commonjs/sdkVersion.js +1 -1
  70. package/lib/commonjs/sdkVersion.js.map +1 -1
  71. package/lib/commonjs/specs/NativeHotUpdater.js.map +1 -1
  72. package/lib/commonjs/store.js +3 -10
  73. package/lib/commonjs/store.js.map +1 -1
  74. package/lib/commonjs/updateError.js +145 -0
  75. package/lib/commonjs/updateError.js.map +1 -0
  76. package/lib/commonjs/wrap.js +43 -26
  77. package/lib/commonjs/wrap.js.map +1 -1
  78. package/lib/module/appReady.js +95 -0
  79. package/lib/module/appReady.js.map +1 -0
  80. package/lib/module/checkForUpdate.js +113 -63
  81. package/lib/module/checkForUpdate.js.map +1 -1
  82. package/lib/module/clientPlugin.js +4 -0
  83. package/lib/module/clientPlugin.js.map +1 -0
  84. package/lib/module/error.js +4 -13
  85. package/lib/module/error.js.map +1 -1
  86. package/lib/module/fetchJSON.js +14 -26
  87. package/lib/module/fetchJSON.js.map +1 -1
  88. package/lib/module/fetchUpdateResponse.js +55 -0
  89. package/lib/module/fetchUpdateResponse.js.map +1 -0
  90. package/lib/module/httpClient.js +62 -93
  91. package/lib/module/httpClient.js.map +1 -1
  92. package/lib/module/index.js +58 -20
  93. package/lib/module/index.js.map +1 -1
  94. package/lib/module/native.js +67 -53
  95. package/lib/module/native.js.map +1 -1
  96. package/lib/module/pluginHost.js +36 -0
  97. package/lib/module/pluginHost.js.map +1 -0
  98. package/lib/module/releaseCatalogCache.js +32 -82
  99. package/lib/module/releaseCatalogCache.js.map +1 -1
  100. package/lib/module/sdkVersion.js +1 -1
  101. package/lib/module/sdkVersion.js.map +1 -1
  102. package/lib/module/specs/NativeHotUpdater.js.map +1 -1
  103. package/lib/module/store.js +3 -10
  104. package/lib/module/store.js.map +1 -1
  105. package/lib/module/updateError.js +139 -0
  106. package/lib/module/updateError.js.map +1 -0
  107. package/lib/module/wrap.js +43 -26
  108. package/lib/module/wrap.js.map +1 -1
  109. package/lib/typescript/commonjs/appReady.d.ts +21 -0
  110. package/lib/typescript/commonjs/appReady.d.ts.map +1 -0
  111. package/lib/typescript/commonjs/checkForUpdate.d.ts +16 -4
  112. package/lib/typescript/commonjs/checkForUpdate.d.ts.map +1 -1
  113. package/lib/typescript/commonjs/clientPlugin.d.ts +2 -0
  114. package/lib/typescript/commonjs/clientPlugin.d.ts.map +1 -0
  115. package/lib/typescript/commonjs/error.d.ts +4 -13
  116. package/lib/typescript/commonjs/error.d.ts.map +1 -1
  117. package/lib/typescript/commonjs/fetchJSON.d.ts +5 -5
  118. package/lib/typescript/commonjs/fetchJSON.d.ts.map +1 -1
  119. package/lib/typescript/commonjs/fetchUpdateResponse.d.ts +14 -0
  120. package/lib/typescript/commonjs/fetchUpdateResponse.d.ts.map +1 -0
  121. package/lib/typescript/commonjs/httpClient.d.ts +4 -33
  122. package/lib/typescript/commonjs/httpClient.d.ts.map +1 -1
  123. package/lib/typescript/commonjs/index.d.ts +4 -8
  124. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  125. package/lib/typescript/commonjs/native.d.ts +29 -22
  126. package/lib/typescript/commonjs/native.d.ts.map +1 -1
  127. package/lib/typescript/commonjs/pluginHost.d.ts +4 -0
  128. package/lib/typescript/commonjs/pluginHost.d.ts.map +1 -0
  129. package/lib/typescript/commonjs/releaseCatalogCache.d.ts +8 -14
  130. package/lib/typescript/commonjs/releaseCatalogCache.d.ts.map +1 -1
  131. package/lib/typescript/commonjs/sdkVersion.d.ts +1 -1
  132. package/lib/typescript/commonjs/sdkVersion.d.ts.map +1 -1
  133. package/lib/typescript/commonjs/specs/NativeHotUpdater.d.ts +35 -47
  134. package/lib/typescript/commonjs/specs/NativeHotUpdater.d.ts.map +1 -1
  135. package/lib/typescript/commonjs/store.d.ts +1 -3
  136. package/lib/typescript/commonjs/store.d.ts.map +1 -1
  137. package/lib/typescript/commonjs/types.d.ts +1 -1
  138. package/lib/typescript/commonjs/types.d.ts.map +1 -1
  139. package/lib/typescript/commonjs/updateError.d.ts +26 -0
  140. package/lib/typescript/commonjs/updateError.d.ts.map +1 -0
  141. package/lib/typescript/commonjs/wrap.d.ts.map +1 -1
  142. package/lib/typescript/commonjs/wrap.types.d.ts +6 -7
  143. package/lib/typescript/commonjs/wrap.types.d.ts.map +1 -1
  144. package/lib/typescript/module/appReady.d.ts +21 -0
  145. package/lib/typescript/module/appReady.d.ts.map +1 -0
  146. package/lib/typescript/module/checkForUpdate.d.ts +16 -4
  147. package/lib/typescript/module/checkForUpdate.d.ts.map +1 -1
  148. package/lib/typescript/module/clientPlugin.d.ts +2 -0
  149. package/lib/typescript/module/clientPlugin.d.ts.map +1 -0
  150. package/lib/typescript/module/error.d.ts +4 -13
  151. package/lib/typescript/module/error.d.ts.map +1 -1
  152. package/lib/typescript/module/fetchJSON.d.ts +5 -5
  153. package/lib/typescript/module/fetchJSON.d.ts.map +1 -1
  154. package/lib/typescript/module/fetchUpdateResponse.d.ts +14 -0
  155. package/lib/typescript/module/fetchUpdateResponse.d.ts.map +1 -0
  156. package/lib/typescript/module/httpClient.d.ts +4 -33
  157. package/lib/typescript/module/httpClient.d.ts.map +1 -1
  158. package/lib/typescript/module/index.d.ts +4 -8
  159. package/lib/typescript/module/index.d.ts.map +1 -1
  160. package/lib/typescript/module/native.d.ts +29 -22
  161. package/lib/typescript/module/native.d.ts.map +1 -1
  162. package/lib/typescript/module/pluginHost.d.ts +4 -0
  163. package/lib/typescript/module/pluginHost.d.ts.map +1 -0
  164. package/lib/typescript/module/releaseCatalogCache.d.ts +8 -14
  165. package/lib/typescript/module/releaseCatalogCache.d.ts.map +1 -1
  166. package/lib/typescript/module/sdkVersion.d.ts +1 -1
  167. package/lib/typescript/module/sdkVersion.d.ts.map +1 -1
  168. package/lib/typescript/module/specs/NativeHotUpdater.d.ts +35 -47
  169. package/lib/typescript/module/specs/NativeHotUpdater.d.ts.map +1 -1
  170. package/lib/typescript/module/store.d.ts +1 -3
  171. package/lib/typescript/module/store.d.ts.map +1 -1
  172. package/lib/typescript/module/types.d.ts +1 -1
  173. package/lib/typescript/module/types.d.ts.map +1 -1
  174. package/lib/typescript/module/updateError.d.ts +26 -0
  175. package/lib/typescript/module/updateError.d.ts.map +1 -0
  176. package/lib/typescript/module/wrap.d.ts.map +1 -1
  177. package/lib/typescript/module/wrap.types.d.ts +6 -7
  178. package/lib/typescript/module/wrap.types.d.ts.map +1 -1
  179. package/package.json +7 -4
  180. package/src/appReady.ts +141 -0
  181. package/src/checkForUpdate.ts +145 -86
  182. package/src/clientPlugin.ts +18 -0
  183. package/src/error.ts +4 -14
  184. package/src/fetchJSON.ts +14 -30
  185. package/src/fetchUpdateResponse.ts +60 -0
  186. package/src/httpClient.ts +88 -159
  187. package/src/index.ts +90 -24
  188. package/src/native.ts +98 -102
  189. package/src/pluginHost.ts +47 -0
  190. package/src/releaseCatalogCache.ts +43 -172
  191. package/src/sdkVersion.ts +1 -1
  192. package/src/specs/NativeHotUpdater.ts +34 -48
  193. package/src/store.ts +2 -23
  194. package/src/types.ts +1 -1
  195. package/src/updateError.ts +167 -0
  196. package/src/wrap.tsx +57 -26
  197. package/src/wrap.types.ts +6 -7
  198. package/android/src/main/java/com/hotupdater/DecompressService.kt +0 -89
  199. package/android/src/main/java/com/hotupdater/DecompressionStrategy.kt +0 -26
  200. package/android/src/main/java/com/hotupdater/TarBrDecompressionStrategy.kt +0 -117
  201. package/android/src/main/java/com/hotupdater/TarGzDecompressionStrategy.kt +0 -117
  202. package/android/src/main/java/com/hotupdater/ZipDecompressionStrategy.kt +0 -192
  203. package/android/src/test/java/com/hotupdater/BsdiffPatchTest.kt +0 -46
  204. package/android/src/test/java/com/hotupdater/BundleFileStorageServiceTest.kt +0 -799
  205. package/android/src/test/java/com/hotupdater/HotUpdaterImplTest.kt +0 -273
  206. package/android/src/test/java/com/hotupdater/NativeConfigUtilsTest.kt +0 -20
  207. package/android/src/test/java/com/hotupdater/OkHttpDownloadServiceTest.kt +0 -111
  208. package/android/src/test/java/com/hotupdater/RelativePathResolverTest.kt +0 -41
  209. package/android/src/test/java/com/hotupdater/ReleaseCatalogCacheServiceTest.kt +0 -118
  210. package/android/src/test/java/com/hotupdater/TarArchiveInputStreamTest.kt +0 -68
  211. package/ios/HotUpdater/Internal/ArchiveExtractionUtilities.swift +0 -178
  212. package/ios/HotUpdater/Internal/DecompressService.swift +0 -112
  213. package/ios/HotUpdater/Internal/DecompressionStrategy.swift +0 -22
  214. package/ios/HotUpdater/Internal/StreamingTarArchiveExtractor.swift +0 -370
  215. package/ios/HotUpdater/Internal/TarBrDecompressionStrategy.swift +0 -39
  216. package/ios/HotUpdater/Internal/TarGzDecompressionStrategy.swift +0 -59
  217. package/ios/HotUpdater/Internal/ZipArchiveExtractor.swift +0 -482
  218. package/ios/HotUpdater/Internal/ZipDecompressionStrategy.swift +0 -56
  219. package/lib/commonjs/catalogCacheNative.spec.js +0 -31
  220. package/lib/commonjs/catalogCacheNative.spec.js.map +0 -1
  221. package/lib/commonjs/checkForUpdate.releaseCatalog.spec.js +0 -616
  222. package/lib/commonjs/checkForUpdate.releaseCatalog.spec.js.map +0 -1
  223. package/lib/commonjs/httpClient.spec.js +0 -102
  224. package/lib/commonjs/httpClient.spec.js.map +0 -1
  225. package/lib/commonjs/index.spec.js +0 -302
  226. package/lib/commonjs/index.spec.js.map +0 -1
  227. package/lib/commonjs/iosInterop.spec.js +0 -18
  228. package/lib/commonjs/iosInterop.spec.js.map +0 -1
  229. package/lib/commonjs/native.spec.js +0 -883
  230. package/lib/commonjs/native.spec.js.map +0 -1
  231. package/lib/commonjs/notifyAppReadyInsights.boundary.spec.js +0 -218
  232. package/lib/commonjs/notifyAppReadyInsights.boundary.spec.js.map +0 -1
  233. package/lib/commonjs/notifyAppReadyInsights.js +0 -133
  234. package/lib/commonjs/notifyAppReadyInsights.js.map +0 -1
  235. package/lib/commonjs/notifyAppReadyInsights.spec.js +0 -246
  236. package/lib/commonjs/notifyAppReadyInsights.spec.js.map +0 -1
  237. package/lib/commonjs/notifyAppReadyInsights.test-utils.js +0 -24
  238. package/lib/commonjs/notifyAppReadyInsights.test-utils.js.map +0 -1
  239. package/lib/commonjs/notifyAppReadyInsights.transport.spec.js +0 -117
  240. package/lib/commonjs/notifyAppReadyInsights.transport.spec.js.map +0 -1
  241. package/lib/commonjs/releaseCatalogCache.spec.js +0 -195
  242. package/lib/commonjs/releaseCatalogCache.spec.js.map +0 -1
  243. package/lib/commonjs/releaseCatalogCacheNativeContract.spec.js +0 -64
  244. package/lib/commonjs/releaseCatalogCacheNativeContract.spec.js.map +0 -1
  245. package/lib/commonjs/store.notification.spec.js +0 -76
  246. package/lib/commonjs/store.notification.spec.js.map +0 -1
  247. package/lib/commonjs/store.spec.js +0 -316
  248. package/lib/commonjs/store.spec.js.map +0 -1
  249. package/lib/commonjs/wrap.spec.js +0 -329
  250. package/lib/commonjs/wrap.spec.js.map +0 -1
  251. package/lib/module/catalogCacheNative.spec.js +0 -31
  252. package/lib/module/catalogCacheNative.spec.js.map +0 -1
  253. package/lib/module/checkForUpdate.releaseCatalog.spec.js +0 -615
  254. package/lib/module/checkForUpdate.releaseCatalog.spec.js.map +0 -1
  255. package/lib/module/httpClient.spec.js +0 -102
  256. package/lib/module/httpClient.spec.js.map +0 -1
  257. package/lib/module/index.spec.js +0 -301
  258. package/lib/module/index.spec.js.map +0 -1
  259. package/lib/module/iosInterop.spec.js +0 -18
  260. package/lib/module/iosInterop.spec.js.map +0 -1
  261. package/lib/module/native.spec.js +0 -882
  262. package/lib/module/native.spec.js.map +0 -1
  263. package/lib/module/notifyAppReadyInsights.boundary.spec.js +0 -217
  264. package/lib/module/notifyAppReadyInsights.boundary.spec.js.map +0 -1
  265. package/lib/module/notifyAppReadyInsights.js +0 -128
  266. package/lib/module/notifyAppReadyInsights.js.map +0 -1
  267. package/lib/module/notifyAppReadyInsights.spec.js +0 -245
  268. package/lib/module/notifyAppReadyInsights.spec.js.map +0 -1
  269. package/lib/module/notifyAppReadyInsights.test-utils.js +0 -18
  270. package/lib/module/notifyAppReadyInsights.test-utils.js.map +0 -1
  271. package/lib/module/notifyAppReadyInsights.transport.spec.js +0 -116
  272. package/lib/module/notifyAppReadyInsights.transport.spec.js.map +0 -1
  273. package/lib/module/releaseCatalogCache.spec.js +0 -195
  274. package/lib/module/releaseCatalogCache.spec.js.map +0 -1
  275. package/lib/module/releaseCatalogCacheNativeContract.spec.js +0 -64
  276. package/lib/module/releaseCatalogCacheNativeContract.spec.js.map +0 -1
  277. package/lib/module/store.notification.spec.js +0 -77
  278. package/lib/module/store.notification.spec.js.map +0 -1
  279. package/lib/module/store.spec.js +0 -315
  280. package/lib/module/store.spec.js.map +0 -1
  281. package/lib/module/wrap.spec.js +0 -330
  282. package/lib/module/wrap.spec.js.map +0 -1
  283. package/lib/typescript/commonjs/notifyAppReadyInsights.d.ts +0 -13
  284. package/lib/typescript/commonjs/notifyAppReadyInsights.d.ts.map +0 -1
  285. package/lib/typescript/commonjs/notifyAppReadyInsights.test-utils.d.ts +0 -8
  286. package/lib/typescript/commonjs/notifyAppReadyInsights.test-utils.d.ts.map +0 -1
  287. package/lib/typescript/module/notifyAppReadyInsights.d.ts +0 -13
  288. package/lib/typescript/module/notifyAppReadyInsights.d.ts.map +0 -1
  289. package/lib/typescript/module/notifyAppReadyInsights.test-utils.d.ts +0 -8
  290. package/lib/typescript/module/notifyAppReadyInsights.test-utils.d.ts.map +0 -1
  291. package/src/catalogCacheNative.spec.ts +0 -63
  292. package/src/checkForUpdate.releaseCatalog.spec.ts +0 -612
  293. package/src/httpClient.spec.ts +0 -132
  294. package/src/index.spec.ts +0 -337
  295. package/src/iosInterop.spec.ts +0 -30
  296. package/src/native.spec.ts +0 -945
  297. package/src/notifyAppReadyInsights.boundary.spec.ts +0 -234
  298. package/src/notifyAppReadyInsights.spec.ts +0 -243
  299. package/src/notifyAppReadyInsights.test-utils.ts +0 -31
  300. package/src/notifyAppReadyInsights.transport.spec.ts +0 -130
  301. package/src/notifyAppReadyInsights.ts +0 -194
  302. package/src/releaseCatalogCache.spec.ts +0 -282
  303. package/src/releaseCatalogCacheNativeContract.spec.ts +0 -80
  304. package/src/store.notification.spec.ts +0 -92
  305. package/src/store.spec.ts +0 -372
  306. package/src/wrap.spec.ts +0 -406
@@ -0,0 +1,86 @@
1
+ package com.hotupdater
2
+
3
+ import android.content.Context
4
+ import android.util.Log
5
+ import androidx.core.util.AtomicFile
6
+ import org.json.JSONObject
7
+ import java.io.File
8
+ import java.io.FileNotFoundException
9
+ import java.io.FileOutputStream
10
+
11
+ /**
12
+ * The persistent string key-value store behind `getStorageItem` and
13
+ * `setStorageItem`, which client plugins use through JS. Keys are opaque
14
+ * here; JS scopes them per plugin.
15
+ *
16
+ * Values live as one JSON object in a file outside device backups. The file
17
+ * is read once, on first use, and rewritten atomically on every set. A
18
+ * missing, unreadable, or corrupt file reads as empty and is replaced by the
19
+ * next set. A set that cannot be persisted still applies for this process.
20
+ */
21
+ class KeyValueStorageService(
22
+ private val file: File,
23
+ ) {
24
+ companion object {
25
+ private const val TAG = "KeyValueStorage"
26
+ const val FILENAME = "storage.json"
27
+
28
+ /** The store kept in `noBackupFilesDir/hot-updater/storage.json`. */
29
+ fun create(context: Context): KeyValueStorageService = KeyValueStorageService(NoBackupStorage.file(context, FILENAME))
30
+ }
31
+
32
+ private var values: MutableMap<String, String>? = null
33
+
34
+ @Synchronized
35
+ fun getItem(key: String): String? = loadValues()[key]
36
+
37
+ /** Stores [value] under [key], or removes [key] when [value] is null. */
38
+ @Synchronized
39
+ fun setItem(
40
+ key: String,
41
+ value: String?,
42
+ ) {
43
+ val values = loadValues()
44
+ if (value == null) {
45
+ values.remove(key)
46
+ } else {
47
+ values[key] = value
48
+ }
49
+ write(values)
50
+ }
51
+
52
+ private fun loadValues(): MutableMap<String, String> = values ?: read().also { values = it }
53
+
54
+ private fun read(): MutableMap<String, String> =
55
+ try {
56
+ val json =
57
+ AtomicFile(file).openRead().use { input ->
58
+ JSONObject(String(input.readBytes(), Charsets.UTF_8))
59
+ }
60
+ val stored = linkedMapOf<String, String>()
61
+ json.keys().forEach { key ->
62
+ (json.opt(key) as? String)?.let { stored[key] = it }
63
+ }
64
+ stored
65
+ } catch (_: FileNotFoundException) {
66
+ linkedMapOf()
67
+ } catch (e: Exception) {
68
+ // The message of a parse error can quote stored values, so it is not logged.
69
+ Log.w(TAG, "Ignoring unreadable storage file (${e.javaClass.simpleName})")
70
+ linkedMapOf()
71
+ }
72
+
73
+ private fun write(values: Map<String, String>) {
74
+ val atomicFile = AtomicFile(file)
75
+ var output: FileOutputStream? = null
76
+ try {
77
+ file.parentFile?.mkdirs()
78
+ output = atomicFile.startWrite()
79
+ output.write(JSONObject(values).toString().toByteArray(Charsets.UTF_8))
80
+ atomicFile.finishWrite(output)
81
+ } catch (e: Exception) {
82
+ output?.let(atomicFile::failWrite)
83
+ Log.w(TAG, "Failed to persist storage", e)
84
+ }
85
+ }
86
+ }
@@ -0,0 +1,31 @@
1
+ package com.hotupdater
2
+
3
+ import android.Manifest
4
+ import android.content.Context
5
+ import android.content.pm.PackageManager
6
+ import android.net.ConnectivityManager
7
+ import android.os.Build
8
+
9
+ internal object NetworkState {
10
+ /**
11
+ * True when the device reports no active network. False when it has one,
12
+ * or when that cannot be told, as when the app lacks
13
+ * `ACCESS_NETWORK_STATE`.
14
+ */
15
+ fun hasNoActiveNetwork(context: Context): Boolean =
16
+ try {
17
+ val permission = context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)
18
+ val connectivity = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
19
+ when {
20
+ permission != PackageManager.PERMISSION_GRANTED || connectivity == null -> false
21
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> connectivity.activeNetwork == null
22
+ else -> hasNoConnectedNetworkLegacy(connectivity)
23
+ }
24
+ } catch (_: Exception) {
25
+ false
26
+ }
27
+
28
+ @Suppress("DEPRECATION")
29
+ private fun hasNoConnectedNetworkLegacy(connectivity: ConnectivityManager): Boolean =
30
+ connectivity.activeNetworkInfo?.isConnected != true
31
+ }
@@ -0,0 +1,17 @@
1
+ package com.hotupdater
2
+
3
+ import android.content.Context
4
+ import java.io.File
5
+
6
+ /**
7
+ * SDK state that must never be restored onto another installation lives in
8
+ * `noBackupFilesDir/hot-updater`, which Android Auto Backup skips.
9
+ */
10
+ internal object NoBackupStorage {
11
+ private const val DIRECTORY_NAME = "hot-updater"
12
+
13
+ fun file(
14
+ context: Context,
15
+ name: String,
16
+ ): File = File(File(context.noBackupFilesDir, DIRECTORY_NAME), name)
17
+ }
@@ -1,6 +1,7 @@
1
1
  package com.hotupdater
2
2
 
3
3
  import android.util.Log
4
+ import kotlinx.coroutines.CancellationException
4
5
  import kotlinx.coroutines.Dispatchers
5
6
  import kotlinx.coroutines.delay
6
7
  import kotlinx.coroutines.withContext
@@ -15,10 +16,11 @@ import okio.Source
15
16
  import okio.buffer
16
17
  import java.io.File
17
18
  import java.io.IOException
18
- import java.net.SocketTimeoutException
19
+ import java.net.MalformedURLException
19
20
  import java.net.URL
20
- import java.net.UnknownHostException
21
21
  import java.util.concurrent.TimeUnit
22
+ import javax.net.ssl.SSLHandshakeException
23
+ import javax.net.ssl.SSLPeerUnverifiedException
22
24
 
23
25
  /**
24
26
  * Exception for incomplete downloads with size information
@@ -28,6 +30,47 @@ class IncompleteDownloadException(
28
30
  val actualSize: Long,
29
31
  ) : IOException("Download incomplete: received $actualSize bytes, expected $expectedSize bytes")
30
32
 
33
+ /**
34
+ * The server answered with a non-2xx status. [originCode] is the storage
35
+ * origin's XML error `<Code>` (S3, R2, or GCS style) when the body names one.
36
+ */
37
+ class HttpStatusException(
38
+ val statusCode: Int,
39
+ statusMessage: String,
40
+ val originCode: String? = null,
41
+ ) : Exception("HTTP error $statusCode: $statusMessage")
42
+
43
+ /** How much of an error body is read to find a storage origin's error code. */
44
+ internal const val MAX_ORIGIN_ERROR_BODY_BYTES = 4 * 1024L
45
+
46
+ private val ORIGIN_ERROR_CODE = Regex("<Error(?:\\s[^>]*)?>(?:(?!</Error>).)*?<Code>([^<]*)</Code>", RegexOption.DOT_MATCHES_ALL)
47
+ private val ORIGIN_CODE_VALUE = Regex("[A-Za-z0-9._-]{1,64}")
48
+
49
+ /**
50
+ * Reads the `<Code>` of a storage origin's XML error, as in
51
+ * `<Error><Code>AccessDenied</Code>...`, from at most the first 4 KB of
52
+ * [body]. Only a 1 to 64 character code of letters, digits, `.`, `_`, and `-`
53
+ * is kept, so no key, resource, or message text is ever returned.
54
+ */
55
+ internal fun readOriginErrorCode(body: ResponseBody?): String? =
56
+ try {
57
+ body?.source()?.let { source ->
58
+ source.request(MAX_ORIGIN_ERROR_BODY_BYTES)
59
+ val prefix = source.buffer.readByteArray(minOf(source.buffer.size, MAX_ORIGIN_ERROR_BODY_BYTES))
60
+ parseOriginErrorCode(String(prefix, Charsets.UTF_8))
61
+ }
62
+ } catch (_: Exception) {
63
+ null
64
+ }
65
+
66
+ internal fun parseOriginErrorCode(body: String): String? =
67
+ ORIGIN_ERROR_CODE
68
+ .find(body)
69
+ ?.groupValues
70
+ ?.get(1)
71
+ ?.trim()
72
+ ?.takeIf { ORIGIN_CODE_VALUE.matches(it) }
73
+
31
74
  /**
32
75
  * Result wrapper for download operations
33
76
  */
@@ -65,6 +108,13 @@ interface DownloadService {
65
108
  fileSizeCallback: ((Long) -> Unit)? = null,
66
109
  progressCallback: (DownloadProgress) -> Unit,
67
110
  ): DownloadResult
111
+
112
+ suspend fun downloadFileOnce(
113
+ fileUrl: URL,
114
+ destination: File,
115
+ fileSizeCallback: ((Long) -> Unit)? = null,
116
+ progressCallback: (DownloadProgress) -> Unit,
117
+ ): DownloadResult = downloadFile(fileUrl, destination, fileSizeCallback, progressCallback)
68
118
  }
69
119
 
70
120
  /**
@@ -127,13 +177,33 @@ private class ProgressResponseBody(
127
177
  }
128
178
  }
129
179
 
180
+ /**
181
+ * Whether a failed download may succeed if tried again: a network failure or
182
+ * timeout, a body that ended early, or a 408, 429, or 5xx answer. Another 4xx,
183
+ * a TLS certificate failure, a canceled call, and a local storage failure
184
+ * would fail the same way again.
185
+ */
186
+ internal fun isRetryableDownloadError(error: Exception): Boolean =
187
+ when (error) {
188
+ is HttpStatusException -> error.statusCode == 408 || error.statusCode == 429 || error.statusCode >= 500
189
+ is LocalStorageException -> false
190
+ is SSLHandshakeException, is SSLPeerUnverifiedException -> false
191
+ is MalformedURLException -> false
192
+ is IOException -> error.message != "Canceled"
193
+ else -> false
194
+ }
195
+
130
196
  /**
131
197
  * OkHttp-based implementation of DownloadService with resume support
132
198
  */
133
- class OkHttpDownloadService : DownloadService {
199
+ class OkHttpDownloadService internal constructor(
200
+ private val initialRetryDelayMs: Long,
201
+ ) : DownloadService {
202
+ constructor() : this(INITIAL_RETRY_DELAY_MS)
203
+
134
204
  companion object {
135
205
  private const val TAG = "OkHttpDownloadService"
136
- private const val MAX_RETRIES = 3
206
+ internal const val MAX_ATTEMPTS = 3
137
207
  private const val INITIAL_RETRY_DELAY_MS = 1000L
138
208
  private const val TIMEOUT_SECONDS = 30L
139
209
  }
@@ -146,6 +216,12 @@ class OkHttpDownloadService : DownloadService {
146
216
  .writeTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
147
217
  .build()
148
218
 
219
+ /**
220
+ * Downloads [fileUrl], trying again after a retryable failure (see
221
+ * [isRetryableDownloadError]) up to [MAX_ATTEMPTS] attempts in all, 1 and
222
+ * then 2 seconds apart. The last attempt's result is returned, so a
223
+ * failure keeps what classified it.
224
+ */
149
225
  override suspend fun downloadFile(
150
226
  fileUrl: URL,
151
227
  destination: File,
@@ -153,38 +229,51 @@ class OkHttpDownloadService : DownloadService {
153
229
  progressCallback: (DownloadProgress) -> Unit,
154
230
  ): DownloadResult =
155
231
  withContext(Dispatchers.IO) {
156
- var attempt = 0
157
- var lastException: Exception? = null
158
-
159
- while (attempt < MAX_RETRIES) {
160
- try {
161
- return@withContext attemptDownload(
162
- fileUrl,
163
- destination,
164
- fileSizeCallback,
165
- progressCallback,
166
- )
167
- } catch (e: Exception) {
168
- lastException = e
169
- attempt++
170
-
171
- if (attempt < MAX_RETRIES && isRetryableException(e)) {
172
- val delayMs = INITIAL_RETRY_DELAY_MS * (1 shl (attempt - 1))
173
- Log.d(
174
- TAG,
175
- "Download failed (attempt $attempt/$MAX_RETRIES): ${e.message}. Retrying in ${delayMs}ms...",
176
- )
177
- delay(delayMs)
178
- } else {
179
- Log.d(TAG, "Download failed: ${e.message}")
180
- break
181
- }
182
- }
232
+ var attempt = 1
233
+ var result = attemptOrError(fileUrl, destination, fileSizeCallback, progressCallback)
234
+ while (
235
+ result is DownloadResult.Error &&
236
+ attempt < MAX_ATTEMPTS &&
237
+ isRetryableDownloadError(result.exception)
238
+ ) {
239
+ val delayMs = initialRetryDelayMs * (1 shl (attempt - 1))
240
+ Log.d(
241
+ TAG,
242
+ "Download failed (attempt $attempt/$MAX_ATTEMPTS): ${result.exception.message}. Retrying in ${delayMs}ms...",
243
+ )
244
+ delay(delayMs)
245
+ attempt++
246
+ result = attemptOrError(fileUrl, destination, fileSizeCallback, progressCallback)
183
247
  }
248
+ if (result is DownloadResult.Error) {
249
+ Log.d(TAG, "Download failed (attempt $attempt/$MAX_ATTEMPTS): ${result.exception.message}")
250
+ }
251
+ result
252
+ }
184
253
 
185
- DownloadResult.Error(lastException ?: Exception("Download failed after $MAX_RETRIES attempts"))
254
+ /** One attempt; an exception a callback threw, such as the disk space check's, is its error. */
255
+ private suspend fun attemptOrError(
256
+ fileUrl: URL,
257
+ destination: File,
258
+ fileSizeCallback: ((Long) -> Unit)?,
259
+ progressCallback: (DownloadProgress) -> Unit,
260
+ ): DownloadResult =
261
+ try {
262
+ attemptDownload(fileUrl, destination, fileSizeCallback, progressCallback)
263
+ } catch (error: CancellationException) {
264
+ throw error
265
+ } catch (error: Exception) {
266
+ DownloadResult.Error(error)
186
267
  }
187
268
 
269
+ /** One attempt with no retry, for a download with a fallback, such as the archive's per-file downloads. */
270
+ override suspend fun downloadFileOnce(
271
+ fileUrl: URL,
272
+ destination: File,
273
+ fileSizeCallback: ((Long) -> Unit)?,
274
+ progressCallback: (DownloadProgress) -> Unit,
275
+ ): DownloadResult = attemptOrError(fileUrl, destination, fileSizeCallback, progressCallback)
276
+
188
277
  private suspend fun attemptDownload(
189
278
  fileUrl: URL,
190
279
  destination: File,
@@ -212,10 +301,10 @@ class OkHttpDownloadService : DownloadService {
212
301
  }
213
302
 
214
303
  if (!response.isSuccessful) {
215
- val errorMsg = "HTTP error ${response.code}: ${response.message}"
216
- Log.d(TAG, errorMsg)
304
+ val error = HttpStatusException(response.code, response.message, readOriginErrorCode(response.body))
305
+ Log.d(TAG, "HTTP error ${response.code}: ${response.message}")
217
306
  response.close()
218
- return@withContext DownloadResult.Error(Exception(errorMsg))
307
+ return@withContext DownloadResult.Error(error)
219
308
  }
220
309
 
221
310
  val body = response.body
@@ -242,9 +331,10 @@ class OkHttpDownloadService : DownloadService {
242
331
  progressCallback.invoke(progress)
243
332
  }
244
333
 
245
- // Write to file
334
+ // Write to file. A local write failure surfaces as LocalStorageException,
335
+ // so it is not mistaken for a network failure.
246
336
  progressBody.source().use { source ->
247
- destination.outputStream().use { output ->
337
+ LocalStorageOutputStream.open(destination).use { output ->
248
338
  val buffer = ByteArray(8 * 1024)
249
339
  var bytesRead: Int
250
340
 
@@ -291,17 +381,4 @@ class OkHttpDownloadService : DownloadService {
291
381
  DownloadResult.Error(e)
292
382
  }
293
383
  }
294
-
295
- /**
296
- * Check if exception is retryable
297
- */
298
- private fun isRetryableException(e: Exception): Boolean =
299
- when (e) {
300
- is SocketTimeoutException,
301
- is UnknownHostException,
302
- is IOException,
303
- -> true
304
-
305
- else -> false
306
- }
307
384
  }
@@ -1,10 +1,44 @@
1
1
  package com.hotupdater
2
2
 
3
+ import com.facebook.react.bridge.Arguments
4
+ import com.facebook.react.bridge.Promise
3
5
  import com.facebook.react.bridge.WritableArray
4
6
  import com.facebook.react.bridge.WritableMap
5
7
  import com.facebook.react.bridge.WritableNativeArray
6
8
  import com.facebook.react.bridge.WritableNativeMap
7
9
 
10
+ /** The value an `updateBundle` promise resolves to: `{ delivery, patchFallback }`. */
11
+ internal fun UpdateBundleResult.toWritableMap(): WritableMap =
12
+ Arguments.createMap().apply {
13
+ putString("delivery", delivery.value)
14
+ putBoolean("patchFallback", patchFallback)
15
+ }
16
+
17
+ /**
18
+ * Rejects an `updateBundle` promise with the error's code and message. An
19
+ * update failure also carries its classification as the rejection's
20
+ * `userInfo`: `{ stage, reason, httpStatus? }`.
21
+ */
22
+ internal fun Promise.rejectUpdateBundle(error: HotUpdaterException) {
23
+ val userInfo = error.failure?.toUserInfo()
24
+ if (userInfo == null) {
25
+ reject(error.code, error.message)
26
+ return
27
+ }
28
+ reject(
29
+ error.code,
30
+ error.message,
31
+ Arguments.createMap().apply {
32
+ userInfo.forEach { (key, value) ->
33
+ when (value) {
34
+ is Int -> putInt(key, value)
35
+ else -> putString(key, value.toString())
36
+ }
37
+ }
38
+ },
39
+ )
40
+ }
41
+
8
42
  internal fun Map<String, Any?>.toWritableNativeMap(): WritableNativeMap {
9
43
  val result = WritableNativeMap()
10
44
  forEach { (key, value) ->