@capgo/capacitor-updater 6.51.19 → 6.51.23

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.
@@ -96,7 +96,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
96
96
  deinit {
97
97
  implementation.shutdown()
98
98
  }
99
- private let pluginVersion: String = "6.51.19"
99
+ private let pluginVersion: String = "6.51.23"
100
100
  private let launchStartedAtMs = Int64(Date().timeIntervalSince1970 * 1000)
101
101
  static let updateUrlDefault = "https://plugin.capgo.app/updates"
102
102
  static let statsUrlDefault = "https://plugin.capgo.app/stats"
@@ -582,8 +582,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
582
582
  var dest: URL
583
583
  if BundleInfo.ID_BUILTIN == id {
584
584
  dest = Bundle.main.resourceURL!.appendingPathComponent("public")
585
+ } else if let bundleDir = try? self.implementation.getBundleDirectory(id: id) {
586
+ dest = bundleDir
585
587
  } else {
586
- dest = self.implementation.getBundleDirectory(id: id)
588
+ dest = Bundle.main.resourceURL!.appendingPathComponent("public")
587
589
  }
588
590
 
589
591
  if !FileManager.default.fileExists(atPath: dest.path) {
@@ -1609,7 +1611,19 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
1609
1611
  throw makePreviewError("Invalid download URL")
1610
1612
  }
1611
1613
 
1614
+ if !self.implementation.publicKey.isEmpty && !CryptoCipher.isValidSessionKey(sessionKey) {
1615
+ self.logger.error("Public key present but no valid session key provided")
1616
+ self.implementation.sendStats(action: "session_key_required", versionName: version)
1617
+ throw makePreviewError("Session key required when public key is present")
1618
+ }
1619
+
1612
1620
  var checksum = rawChecksum
1621
+ if manifestEntries == nil && checksum.isEmpty {
1622
+ self.logger.error("No checksum provided")
1623
+ self.implementation.sendStats(action: "checksum_required", versionName: version)
1624
+ throw makePreviewError("Checksum required")
1625
+ }
1626
+
1613
1627
  let next: BundleInfo
1614
1628
  if let manifestEntries = manifestEntries {
1615
1629
  next = try self.implementation.downloadManifest(manifest: manifestEntries, version: version, sessionKey: sessionKey)
@@ -1617,29 +1631,20 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
1617
1631
  next = try self.implementation.download(url: url, version: version, sessionKey: sessionKey)
1618
1632
  }
1619
1633
 
1620
- if self.implementation.publicKey != "" && checksum == "" {
1621
- self.logger.error("Public key present but no checksum provided")
1622
- self.implementation.sendStats(action: "checksum_required", versionName: next.getVersionName())
1623
- let id = next.getId()
1624
- let resDel = self.implementation.delete(id: id)
1625
- if !resDel {
1626
- self.logger.error("Delete failed, id \(id) doesn't exist")
1627
- }
1628
- throw ObjectSavableError.checksum
1629
- }
1630
-
1631
- checksum = try CryptoCipher.decryptChecksum(checksum: checksum, publicKey: self.implementation.publicKey)
1632
- CryptoCipher.logChecksumInfo(label: "Bundle checksum", hexChecksum: next.getChecksum())
1633
- CryptoCipher.logChecksumInfo(label: "Expected checksum", hexChecksum: checksum)
1634
- if (checksum != "" || self.implementation.publicKey != "") && next.getChecksum() != checksum {
1635
- self.logger.error("Error checksum \(next.getChecksum()) \(checksum)")
1636
- self.implementation.sendStats(action: "checksum_fail", versionName: next.getVersionName())
1637
- let id = next.getId()
1638
- let resDel = self.implementation.delete(id: id)
1639
- if !resDel {
1640
- self.logger.error("Delete failed, id \(id) doesn't exist")
1634
+ if manifestEntries == nil {
1635
+ checksum = try CryptoCipher.decryptChecksum(checksum: checksum, publicKey: self.implementation.publicKey)
1636
+ CryptoCipher.logChecksumInfo(label: "Bundle checksum", hexChecksum: next.getChecksum())
1637
+ CryptoCipher.logChecksumInfo(label: "Expected checksum", hexChecksum: checksum)
1638
+ if next.getChecksum() != checksum {
1639
+ self.logger.error("Error checksum \(next.getChecksum()) \(checksum)")
1640
+ self.implementation.sendStats(action: "checksum_fail", versionName: next.getVersionName())
1641
+ let id = next.getId()
1642
+ let resDel = self.implementation.delete(id: id)
1643
+ if !resDel {
1644
+ self.logger.error("Delete failed, id \(id) doesn't exist")
1645
+ }
1646
+ throw ObjectSavableError.checksum
1641
1647
  }
1642
- throw ObjectSavableError.checksum
1643
1648
  }
1644
1649
 
1645
1650
  self.logger.info("Good checksum \(next.getChecksum()) \(checksum)")
@@ -1691,8 +1696,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
1691
1696
  let id = self.implementation.getCurrentBundleId()
1692
1697
  if BundleInfo.ID_BUILTIN == id {
1693
1698
  return Bundle.main.resourceURL!.appendingPathComponent("public")
1699
+ } else if let bundleDir = try? self.implementation.getBundleDirectory(id: id) {
1700
+ return bundleDir
1694
1701
  } else {
1695
- return self.implementation.getBundleDirectory(id: id)
1702
+ return Bundle.main.resourceURL!.appendingPathComponent("public")
1696
1703
  }
1697
1704
  }
1698
1705
 
@@ -3496,13 +3503,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
3496
3503
  }
3497
3504
 
3498
3505
  private func performHideSplashscreen() {
3499
- self.cancelSplashscreenTimeout()
3500
3506
  self.removeSplashscreenLoader()
3501
3507
  self.splashscreenInvocationToken += 1
3502
- self.invokeSplashscreenMethod(
3503
- methodName: "hide",
3504
- callbackId: "autoHideSplashscreen",
3505
- options: self.splashscreenOptions(methodName: "hide"),
3508
+ self.invokeSplashscreenAction(
3509
+ .hide,
3506
3510
  retriesRemaining: self.splashscreenMaxRetries,
3507
3511
  requestToken: self.splashscreenInvocationToken
3508
3512
  )
@@ -3522,10 +3526,8 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
3522
3526
  self.cancelSplashscreenTimeout()
3523
3527
  self.autoSplashscreenTimedOut = false
3524
3528
  self.splashscreenInvocationToken += 1
3525
- self.invokeSplashscreenMethod(
3526
- methodName: "show",
3527
- callbackId: "autoShowSplashscreen",
3528
- options: self.splashscreenOptions(methodName: "show"),
3529
+ self.invokeSplashscreenAction(
3530
+ .show,
3529
3531
  retriesRemaining: self.splashscreenMaxRetries,
3530
3532
  requestToken: self.splashscreenInvocationToken
3531
3533
  )
@@ -3535,7 +3537,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
3535
3537
  }
3536
3538
 
3537
3539
  private func splashscreenOptions(methodName: String) -> [String: Any] {
3538
- methodName == "show" ? ["autoHide": false] : [:]
3540
+ methodName == "show" ? ["autoHide": false, "fadeInDuration": 0] : [:]
3539
3541
  }
3540
3542
 
3541
3543
  private func splashscreenCompletedMessage(methodName: String) -> String {
@@ -3554,20 +3556,59 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
3554
3556
  self.splashscreenInvocationToken += 1
3555
3557
  }
3556
3558
 
3557
- private func makeSplashscreenCall(callbackId: String, options: [String: Any], methodName: String) -> CAPPluginCall {
3558
- CAPPluginCall(callbackId: callbackId, methodName: methodName, options: options, success: { [weak self] (_, _) in
3559
- guard let self = self else { return }
3560
- self.logger.info(self.splashscreenCompletedMessage(methodName: methodName))
3561
- }, error: { [weak self] (_) in
3562
- guard let self = self else { return }
3563
- self.logger.error("Failed to auto-\(methodName) splashscreen")
3564
- })
3559
+ private enum SplashscreenAction {
3560
+ case show
3561
+ case hide
3562
+
3563
+ var methodName: String {
3564
+ switch self {
3565
+ case .show:
3566
+ return "show"
3567
+ case .hide:
3568
+ return "hide"
3569
+ }
3570
+ }
3571
+
3572
+ var unavailableMessageVerb: String {
3573
+ switch self {
3574
+ case .show:
3575
+ return "showing"
3576
+ case .hide:
3577
+ return "hiding"
3578
+ }
3579
+ }
3580
+ }
3581
+
3582
+ private func splashscreenOptionsJSON(methodName: String) -> String {
3583
+ let options = self.splashscreenOptions(methodName: methodName)
3584
+ guard !options.isEmpty,
3585
+ let data = try? JSONSerialization.data(withJSONObject: options),
3586
+ let json = String(data: data, encoding: .utf8) else {
3587
+ return "{}"
3588
+ }
3589
+ return json
3565
3590
  }
3566
3591
 
3567
- private func invokeSplashscreenMethod(
3568
- methodName: String,
3569
- callbackId: String,
3570
- options: [String: Any],
3592
+ private func splashscreenPluginExposesMethod(_ methodName: String, on plugin: CAPPlugin) -> Bool {
3593
+ guard let bridgedPlugin = plugin as? CAPBridgedPlugin else {
3594
+ return false
3595
+ }
3596
+ return bridgedPlugin.pluginMethods.contains { $0.name == methodName }
3597
+ }
3598
+
3599
+ private func splashscreenBridgeScript(for action: SplashscreenAction) -> String {
3600
+ let optionsJSON = self.splashscreenOptionsJSON(methodName: action.methodName)
3601
+ return """
3602
+ var cap = window.Capacitor;
3603
+ if (!cap || typeof cap.nativePromise !== 'function') {
3604
+ throw new Error('Capacitor bridge not ready');
3605
+ }
3606
+ return await cap.nativePromise('\(self.splashscreenPluginName)', '\(action.methodName)', \(optionsJSON));
3607
+ """
3608
+ }
3609
+
3610
+ private func invokeSplashscreenAction(
3611
+ _ action: SplashscreenAction,
3571
3612
  retriesRemaining: Int,
3572
3613
  requestToken: Int
3573
3614
  ) {
@@ -3576,22 +3617,18 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
3576
3617
  }
3577
3618
 
3578
3619
  guard let bridge = self.bridge else {
3579
- self.retrySplashscreenMethod(
3580
- methodName: methodName,
3581
- callbackId: callbackId,
3582
- options: options,
3620
+ self.retrySplashscreenAction(
3621
+ action,
3583
3622
  retriesRemaining: retriesRemaining,
3584
3623
  requestToken: requestToken,
3585
- message: "Bridge not available for \(methodName == "show" ? "showing" : "hiding") splashscreen with autoSplashscreen"
3624
+ message: "Bridge not available for \(action.unavailableMessageVerb) splashscreen with autoSplashscreen"
3586
3625
  )
3587
3626
  return
3588
3627
  }
3589
3628
 
3590
3629
  guard let splashScreenPlugin = bridge.plugin(withName: self.splashscreenPluginName) else {
3591
- self.retrySplashscreenMethod(
3592
- methodName: methodName,
3593
- callbackId: callbackId,
3594
- options: options,
3630
+ self.retrySplashscreenAction(
3631
+ action,
3595
3632
  retriesRemaining: retriesRemaining,
3596
3633
  requestToken: requestToken,
3597
3634
  message: "autoSplashscreen: SplashScreen plugin not found. Install @capacitor/splash-screen plugin."
@@ -3599,37 +3636,111 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
3599
3636
  return
3600
3637
  }
3601
3638
 
3602
- let selector = NSSelectorFromString("\(methodName):")
3603
- guard splashScreenPlugin.responds(to: selector) else {
3604
- self.retrySplashscreenMethod(
3605
- methodName: methodName,
3606
- callbackId: callbackId,
3607
- options: options,
3639
+ guard self.splashscreenPluginExposesMethod(action.methodName, on: splashScreenPlugin) else {
3640
+ self.retrySplashscreenAction(
3641
+ action,
3642
+ retriesRemaining: retriesRemaining,
3643
+ requestToken: requestToken,
3644
+ message: "autoSplashscreen: SplashScreen plugin does not expose \(action.methodName)(). Make sure @capacitor/splash-screen plugin is properly installed."
3645
+ )
3646
+ return
3647
+ }
3648
+
3649
+ guard let webView = bridge.webView else {
3650
+ self.retrySplashscreenAction(
3651
+ action,
3608
3652
  retriesRemaining: retriesRemaining,
3609
3653
  requestToken: requestToken,
3610
- message: "autoSplashscreen: SplashScreen plugin does not respond to \(methodName): method. Make sure @capacitor/splash-screen plugin is properly installed."
3654
+ message: "WebView not available for \(action.unavailableMessageVerb) splashscreen with autoSplashscreen"
3611
3655
  )
3612
3656
  return
3613
3657
  }
3614
3658
 
3615
- let call = self.makeSplashscreenCall(callbackId: callbackId, options: options, methodName: methodName)
3616
- _ = splashScreenPlugin.perform(selector, with: call)
3617
- self.logger.info("Called SplashScreen \(methodName) method")
3659
+ let script = self.splashscreenBridgeScript(for: action)
3660
+ let runInvocation = { [weak self] in
3661
+ guard let self = self, requestToken == self.splashscreenInvocationToken else {
3662
+ return
3663
+ }
3664
+ let handleSplashSuccess = { [weak self] in
3665
+ guard let self = self, requestToken == self.splashscreenInvocationToken else {
3666
+ return
3667
+ }
3668
+ if action == .hide {
3669
+ self.cancelSplashscreenTimeout()
3670
+ }
3671
+ self.logger.info("Called SplashScreen \(action.methodName) method")
3672
+ self.logger.info(self.splashscreenCompletedMessage(methodName: action.methodName))
3673
+ }
3674
+ let handleSplashFailure = { [weak self] (message: String) in
3675
+ guard let self = self, requestToken == self.splashscreenInvocationToken else {
3676
+ return
3677
+ }
3678
+ self.retrySplashscreenAction(
3679
+ action,
3680
+ retriesRemaining: retriesRemaining,
3681
+ requestToken: requestToken,
3682
+ message: message
3683
+ )
3684
+ }
3685
+ // callAsyncJavaScript(_:arguments:in:in:completionHandler:) requires iOS 14+;
3686
+ // Cap 5/6 LTS still declare iOS 13.4 so keep an evaluateJavaScript fallback.
3687
+ if #available(iOS 14.0, *) {
3688
+ webView.callAsyncJavaScript(
3689
+ script,
3690
+ arguments: [:],
3691
+ in: nil,
3692
+ in: .page
3693
+ ) { (result: Result<Any, Error>) in
3694
+ if case .failure(let error) = result {
3695
+ handleSplashFailure(
3696
+ "Failed to invoke SplashScreen \(action.methodName) via Capacitor bridge: \(error.localizedDescription)"
3697
+ )
3698
+ return
3699
+ }
3700
+ handleSplashSuccess()
3701
+ }
3702
+ } else {
3703
+ webView.evaluateJavaScript(script) { _, error in
3704
+ if let error = error {
3705
+ handleSplashFailure(
3706
+ "Failed to invoke SplashScreen \(action.methodName) via Capacitor bridge: \(error.localizedDescription)"
3707
+ )
3708
+ return
3709
+ }
3710
+ handleSplashSuccess()
3711
+ }
3712
+ }
3713
+ }
3714
+
3715
+ if Thread.isMainThread {
3716
+ runInvocation()
3717
+ } else {
3718
+ DispatchQueue.main.async(execute: runInvocation)
3719
+ }
3618
3720
  }
3619
3721
 
3620
- private func retrySplashscreenMethod(
3621
- methodName: String,
3622
- callbackId: String,
3623
- options: [String: Any],
3722
+ private func retrySplashscreenAction(
3723
+ _ action: SplashscreenAction,
3624
3724
  retriesRemaining: Int,
3625
3725
  requestToken: Int,
3626
3726
  message: String
3627
3727
  ) {
3628
3728
  guard retriesRemaining > 0 else {
3629
- if methodName == "show" {
3729
+ if action == .show {
3630
3730
  self.logger.warn(message)
3631
- } else {
3632
- self.logger.error(message)
3731
+ return
3732
+ }
3733
+
3734
+ self.logger.error("\(message). Scheduling another hide retry.")
3735
+ DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(self.splashscreenRetryDelayMilliseconds)) { [weak self] in
3736
+ guard let self = self, requestToken == self.splashscreenInvocationToken else {
3737
+ return
3738
+ }
3739
+ self.invokeSplashscreenAction(
3740
+ action,
3741
+ retriesRemaining: self.splashscreenMaxRetries,
3742
+ requestToken: requestToken
3743
+ )
3633
3744
  }
3634
3745
  return
3635
3746
  }
@@ -3639,10 +3750,8 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
3639
3750
  guard let self = self, requestToken == self.splashscreenInvocationToken else {
3640
3751
  return
3641
3752
  }
3642
- self.invokeSplashscreenMethod(
3643
- methodName: methodName,
3644
- callbackId: callbackId,
3645
- options: options,
3753
+ self.invokeSplashscreenAction(
3754
+ action,
3646
3755
  retriesRemaining: retriesRemaining - 1,
3647
3756
  requestToken: requestToken
3648
3757
  )
@@ -4095,6 +4204,22 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
4095
4204
  self.updateUrl = updateUrl
4096
4205
  }
4097
4206
 
4207
+ func downloadBundleForTesting(
4208
+ urlString: String,
4209
+ version: String,
4210
+ sessionKey: String,
4211
+ checksum: String,
4212
+ manifestEntries: [ManifestEntry]?
4213
+ ) throws -> BundleInfo {
4214
+ try downloadBundle(
4215
+ urlString: urlString,
4216
+ version: version,
4217
+ sessionKey: sessionKey,
4218
+ checksum: checksum,
4219
+ manifestEntries: manifestEntries
4220
+ )
4221
+ }
4222
+
4098
4223
  func setAutoUpdateModeForTesting(_ autoUpdateMode: String) {
4099
4224
  self.autoUpdateMode = Self.normalizedAutoUpdateMode(autoUpdateMode)
4100
4225
  self.autoUpdate = Self.isAutoUpdateModeEnabled(self.autoUpdateMode)
@@ -4430,6 +4555,17 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
4430
4555
  if latestVersionName != "" && current.getVersionName() != latestVersionName {
4431
4556
  do {
4432
4557
  self.logger.info("New bundle: \(latestVersionName) found. Current is: \(current.getVersionName()). \(messageUpdate)")
4558
+ if !self.implementation.publicKey.isEmpty && !CryptoCipher.isValidSessionKey(sessionKey) {
4559
+ self.logger.error("Public key present but no valid session key provided")
4560
+ self.implementation.sendStats(action: "session_key_required", versionName: latestVersionName)
4561
+ self.endBackGroundTaskWithNotif(
4562
+ msg: "Session key required when public key is present",
4563
+ latestVersionName: latestVersionName,
4564
+ current: current,
4565
+ plannedDirectUpdate: plannedDirectUpdate
4566
+ )
4567
+ return
4568
+ }
4433
4569
  var nextImpl = self.implementation.getBundleInfoByVersionName(version: latestVersionName)
4434
4570
  let needsDownload = nextImpl.map(Self.shouldRetryDownloadForExistingBundle) ?? true
4435
4571
  if needsDownload {
@@ -4447,6 +4583,17 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
4447
4583
  if res.manifest != nil {
4448
4584
  nextImpl = try self.implementation.downloadManifest(manifest: res.manifest!, version: latestVersionName, sessionKey: sessionKey, link: res.link, comment: res.comment)
4449
4585
  } else {
4586
+ if res.checksum.isEmpty {
4587
+ self.logger.error("No checksum provided")
4588
+ self.implementation.sendStats(action: "checksum_required", versionName: latestVersionName)
4589
+ self.endBackGroundTaskWithNotif(
4590
+ msg: "Checksum required",
4591
+ latestVersionName: latestVersionName,
4592
+ current: current,
4593
+ plannedDirectUpdate: plannedDirectUpdate
4594
+ )
4595
+ return
4596
+ }
4450
4597
  nextImpl = try self.implementation.download(url: downloadUrl, version: latestVersionName, sessionKey: sessionKey, link: res.link, comment: res.comment)
4451
4598
  }
4452
4599
  }
@@ -4475,24 +4622,26 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
4475
4622
  self.endBackGroundTask()
4476
4623
  return
4477
4624
  }
4478
- res.checksum = try CryptoCipher.decryptChecksum(checksum: res.checksum, publicKey: self.implementation.publicKey)
4479
- CryptoCipher.logChecksumInfo(label: "Bundle checksum", hexChecksum: next.getChecksum())
4480
- CryptoCipher.logChecksumInfo(label: "Expected checksum", hexChecksum: res.checksum)
4481
- if res.checksum != "" && next.getChecksum() != res.checksum && res.manifest == nil {
4482
- self.logger.error("Error checksum \(next.getChecksum()) \(res.checksum)")
4483
- self.implementation.sendStats(action: "checksum_fail", versionName: next.getVersionName())
4484
- let id = next.getId()
4485
- let resDel = self.implementation.delete(id: id)
4486
- if !resDel {
4487
- self.logger.error("Delete failed, id \(id) doesn't exist")
4625
+ if res.manifest == nil {
4626
+ res.checksum = try CryptoCipher.decryptChecksum(checksum: res.checksum, publicKey: self.implementation.publicKey)
4627
+ CryptoCipher.logChecksumInfo(label: "Bundle checksum", hexChecksum: next.getChecksum())
4628
+ CryptoCipher.logChecksumInfo(label: "Expected checksum", hexChecksum: res.checksum)
4629
+ if next.getChecksum() != res.checksum {
4630
+ self.logger.error("Error checksum \(next.getChecksum()) \(res.checksum)")
4631
+ self.implementation.sendStats(action: "checksum_fail", versionName: next.getVersionName())
4632
+ let id = next.getId()
4633
+ let resDel = self.implementation.delete(id: id)
4634
+ if !resDel {
4635
+ self.logger.error("Delete failed, id \(id) doesn't exist")
4636
+ }
4637
+ self.endBackGroundTaskWithNotif(
4638
+ msg: "Error checksum",
4639
+ latestVersionName: latestVersionName,
4640
+ current: current,
4641
+ plannedDirectUpdate: plannedDirectUpdate
4642
+ )
4643
+ return
4488
4644
  }
4489
- self.endBackGroundTaskWithNotif(
4490
- msg: "Error checksum",
4491
- latestVersionName: latestVersionName,
4492
- current: current,
4493
- plannedDirectUpdate: plannedDirectUpdate
4494
- )
4495
- return
4496
4645
  }
4497
4646
  if self.shouldBlockAutoUpdateForPreviewSession() {
4498
4647
  self.clearDownloadInProgressState()