@capgo/capacitor-updater 8.53.0 → 8.54.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.
@@ -95,7 +95,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
95
95
  deinit {
96
96
  implementation.shutdown()
97
97
  }
98
- private let pluginVersion: String = "8.53.0"
98
+ private let pluginVersion: String = "8.54.0"
99
99
  private let launchStartedAtMs = Int64(Date().timeIntervalSince1970 * 1000)
100
100
  static let updateUrlDefault = "https://plugin.capgo.app/updates"
101
101
  static let statsUrlDefault = "https://plugin.capgo.app/stats"
@@ -315,6 +315,8 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
315
315
  appReadyTimeout = max(1000, getConfig().getInt("appReadyTimeout", 10000)) // Minimum 1 second
316
316
  implementation.timeout = Double(getConfig().getInt("responseTimeout", 20))
317
317
  implementation.allowHttpsToHttpRedirect = getConfig().getBoolean("allowHttpsToHttpRedirect", false)
318
+ implementation.httpsOnly = getConfig().getBoolean("httpsOnly", true)
319
+ implementation.builtinMinimum = getConfig().getBoolean("builtinMinimum", false)
318
320
  resetWhenUpdate = getConfig().getBoolean("resetWhenUpdate", true)
319
321
  shakeMenuEnabled = getConfig().getBoolean("shakeMenu", false)
320
322
  shakeChannelSelectorEnabled = getConfig().getBoolean("allowShakeChannelSelector", false)
@@ -381,6 +383,12 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
381
383
  logger.info("Loaded persisted channelUrl")
382
384
  }
383
385
  }
386
+ if implementation.httpsOnly {
387
+ for (label, value) in [("updateUrl", updateUrl), ("statsUrl", implementation.statsUrl), ("channelUrl", implementation.channelUrl)]
388
+ where !value.isEmpty && !Self.isHttpsUrl(value) {
389
+ logger.error("httpsOnly is enabled and \(label) \(value) is not https; requests to it will fail")
390
+ }
391
+ }
384
392
  implementation.restorePendingStats()
385
393
 
386
394
  let nativeBuildVersionChanged = self.hasNativeBuildVersionChanged()
@@ -1341,6 +1349,20 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
1341
1349
  }
1342
1350
  }
1343
1351
 
1352
+ static func isHttpsUrl(_ value: String) -> Bool {
1353
+ return URL(string: value)?.scheme?.lowercased() == "https"
1354
+ }
1355
+
1356
+ /// Rejects the call when `httpsOnly` is set and the URL is not https. Returns true when the URL is allowed.
1357
+ private func allowUrlScheme(_ url: String, call: CAPPluginCall, method: String) -> Bool {
1358
+ if implementation.httpsOnly && !Self.isHttpsUrl(url) {
1359
+ logger.error("\(method): httpsOnly is enabled and \(url) is not https")
1360
+ call.reject("httpsOnly is enabled and \(url) is not https")
1361
+ return false
1362
+ }
1363
+ return true
1364
+ }
1365
+
1344
1366
  @objc func setUpdateUrl(_ call: CAPPluginCall) {
1345
1367
  if !getConfig().getBoolean("allowModifyUrl", false) {
1346
1368
  logger.error("setUpdateUrl called without allowModifyUrl")
@@ -1352,6 +1374,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
1352
1374
  call.reject("setUpdateUrl called without url")
1353
1375
  return
1354
1376
  }
1377
+ guard allowUrlScheme(url, call: call, method: "setUpdateUrl") else {
1378
+ return
1379
+ }
1355
1380
  self.updateUrl = url
1356
1381
  if persistModifyUrl {
1357
1382
  UserDefaults.standard.set(url, forKey: updateUrlDefaultsKey)
@@ -1375,6 +1400,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
1375
1400
  call.reject("setStatsUrl called without url")
1376
1401
  return
1377
1402
  }
1403
+ guard allowUrlScheme(url, call: call, method: "setStatsUrl") else {
1404
+ return
1405
+ }
1378
1406
  self.implementation.statsUrl = url
1379
1407
  if persistModifyUrl {
1380
1408
  UserDefaults.standard.set(url, forKey: statsUrlDefaultsKey)
@@ -1394,6 +1422,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
1394
1422
  call.reject("setChannelUrl called without url")
1395
1423
  return
1396
1424
  }
1425
+ guard allowUrlScheme(url, call: call, method: "setChannelUrl") else {
1426
+ return
1427
+ }
1397
1428
  self.implementation.channelUrl = url
1398
1429
  if persistModifyUrl {
1399
1430
  UserDefaults.standard.set(url, forKey: channelUrlDefaultsKey)
@@ -4803,6 +4834,18 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
4803
4834
  )
4804
4835
  return
4805
4836
  }
4837
+ // builtinMinimum also covers an already-downloaded bundle reused without a new download.
4838
+ do {
4839
+ try self.implementation.requireVersionNotBelowBuiltin(latestVersionName)
4840
+ } catch {
4841
+ self.endBackGroundTaskWithNotif(
4842
+ msg: error.localizedDescription,
4843
+ latestVersionName: latestVersionName,
4844
+ current: current,
4845
+ plannedDirectUpdate: plannedDirectUpdate
4846
+ )
4847
+ return
4848
+ }
4806
4849
  var nextImpl = self.implementation.getBundleInfoByVersionName(version: latestVersionName)
4807
4850
  let needsDownload = nextImpl.map(Self.shouldRetryDownloadForExistingBundle) ?? true
4808
4851
  if needsDownload {
@@ -4939,10 +4982,11 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
4939
4982
  }
4940
4983
  if self.queueBundleForNextBackgroundInstall(next) {
4941
4984
  self.notifyListenersOnMain("updateAvailable", data: ["bundle": next.toJSON()])
4985
+ let installed = self.installNextIfInBackground()
4942
4986
  self.endBackGroundTaskWithNotif(
4943
- msg: "update downloaded, will install next background",
4987
+ msg: installed ? "update downloaded and installed in background" : "update downloaded, will install next background",
4944
4988
  latestVersionName: latestVersionName,
4945
- current: current,
4989
+ current: self.implementation.getCurrentBundle(),
4946
4990
  error: false,
4947
4991
  plannedDirectUpdate: plannedDirectUpdate
4948
4992
  )
@@ -4992,29 +5036,85 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
4992
5036
  }
4993
5037
  }
4994
5038
 
4995
- private func installNext() {
4996
- if self.shouldBlockAutoUpdateForPreviewSession() {
5039
+ /// A download that finishes while the app is in the background (silent push,
5040
+ /// background fetch, cold background launch) would otherwise wait for the next
5041
+ /// didEnterBackground, which a background launch never posts, so the user would
5042
+ /// open the old bundle. Install now; the notifyAppReady check stays deferred
5043
+ /// until the app reaches the foreground.
5044
+ @discardableResult
5045
+ func installNextIfInBackground() -> Bool {
5046
+ let install = {
5047
+ self.appInBackground && self.shouldInstallQueuedBundleWhileInBackground() && self.installNext()
5048
+ }
5049
+ if Thread.isMainThread {
5050
+ return install()
5051
+ }
5052
+ return DispatchQueue.main.sync(execute: install)
5053
+ }
5054
+
5055
+ /// Immediate background install applies to deferred (`atBackground`) updates only.
5056
+ /// Direct-update modes retry from `appMovedToForeground` when a reload failed earlier.
5057
+ private func shouldInstallQueuedBundleWhileInBackground() -> Bool {
5058
+ autoUpdateMode == Self.autoUpdateModeBackground
5059
+ }
5060
+
5061
+ /// Direct-update modes queue `next` when an immediate reload fails; retry once the app is active again.
5062
+ private func shouldInstallQueuedBundleOnForeground() -> Bool {
5063
+ switch directUpdateMode {
5064
+ case Self.autoUpdateModeAlways, Self.autoUpdateModeLaunch:
5065
+ return true
5066
+ default:
5067
+ return false
5068
+ }
5069
+ }
5070
+
5071
+ private func installQueuedBundleOnForegroundIfNeeded() {
5072
+ guard shouldInstallQueuedBundleOnForeground() else {
4997
5073
  return
4998
5074
  }
5075
+ let current = self.implementation.getCurrentBundle()
5076
+ guard let next = self.implementation.getNextBundle(),
5077
+ !next.isErrorStatus(),
5078
+ next.getVersionName() != current.getVersionName() else {
5079
+ return
5080
+ }
5081
+ _ = self.installNext()
5082
+ }
5083
+
5084
+ /// Returns true when the next bundle became the active WebView bundle.
5085
+ @discardableResult
5086
+ private func installNext() -> Bool {
5087
+ if self.shouldBlockAutoUpdateForPreviewSession() {
5088
+ return false
5089
+ }
4999
5090
  let delayUpdatePreferences = UserDefaults.standard.string(forKey: DelayUpdateUtils.DELAY_CONDITION_PREFERENCES) ?? "[]"
5000
5091
  let delayConditionList = DelayUpdateUtils.parseDelayConditions(json: delayUpdatePreferences)
5001
5092
  if !delayConditionList.isEmpty {
5002
5093
  logger.info("Update delayed until delay conditions met")
5003
- return
5094
+ return false
5004
5095
  }
5005
5096
  let current: BundleInfo = self.implementation.getCurrentBundle()
5006
5097
  let next: BundleInfo? = self.implementation.getNextBundle()
5007
5098
 
5008
5099
  if next != nil && !next!.isErrorStatus() && next!.getVersionName() != current.getVersionName() {
5009
5100
  logger.info("Next bundle is: \(next!.toString())")
5010
- if self.implementation.set(bundle: next!) && self._reload() {
5011
- logger.info("Updated to bundle: \(next!.toString())")
5012
- self.notifyBundleSet(next!)
5013
- _ = self.implementation.setNextBundle(next: Optional<String>.none)
5014
- } else {
5101
+ let resetState = self.implementation.captureResetState()
5102
+ guard self.implementation.set(bundle: next!) else {
5015
5103
  logger.error("Update to bundle: \(next!.toString()) Failed!")
5104
+ return false
5105
+ }
5106
+ guard self._reload() else {
5107
+ // Built-in `set` may reset fallback/next; restore the full pre-install state for retry.
5108
+ self.implementation.restoreResetState(resetState)
5109
+ logger.error("Update to bundle: \(next!.toString()) Failed! Reload failed, keeping it as next")
5110
+ return false
5016
5111
  }
5112
+ logger.info("Updated to bundle: \(next!.toString())")
5113
+ self.notifyBundleSet(next!)
5114
+ _ = self.implementation.setNextBundle(next: Optional<String>.none)
5115
+ return true
5017
5116
  }
5117
+ return false
5018
5118
  }
5019
5119
 
5020
5120
  @objc private func toJson(object: Any) -> String {
@@ -5035,6 +5135,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
5035
5135
  backgroundWork!.cancel()
5036
5136
  logger.info("Background Timer Task canceled, Activity resumed before timer completes")
5037
5137
  }
5138
+ self.installQueuedBundleOnForegroundIfNeeded()
5038
5139
  if self._isAutoUpdateEnabled() {
5039
5140
  // Check if download is already in progress (with timeout protection)
5040
5141
  if !isDownloadStuckOrTimedOut() {