@capgo/capacitor-updater 8.52.1 → 8.53.1

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 (25) hide show
  1. package/README.md +198 -66
  2. package/android/build.gradle +2 -1
  3. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +414 -150
  4. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +693 -217
  5. package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +2 -1
  6. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +226 -105
  7. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +30 -10
  8. package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +324 -0
  9. package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +34 -0
  10. package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
  11. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +4 -4
  12. package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +1 -1
  13. package/dist/docs.json +378 -27
  14. package/dist/esm/definitions.d.ts +206 -7
  15. package/dist/esm/definitions.js.map +1 -1
  16. package/dist/plugin.cjs.js.map +1 -1
  17. package/dist/plugin.js.map +1 -1
  18. package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +104 -52
  19. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +360 -104
  20. package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +166 -10
  21. package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +2 -1
  22. package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +202 -90
  23. package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
  24. package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +5 -2
  25. package/package.json +7 -3
@@ -0,0 +1,183 @@
1
+ import Foundation
2
+ import UIKit
3
+
4
+ final class NativeUpdatePromptCoordinator {
5
+ static let lastNativeVersionDefaultsKey = "CapacitorUpdater.nativeUpdatePromptLastNativeVersion"
6
+ static let lastShownMsDefaultsKey = "CapacitorUpdater.nativeUpdatePromptLastShownMs"
7
+
8
+ private static var sessionShownNativeVersions = Set<String>()
9
+
10
+ static func resetSessionStateForTesting() {
11
+ sessionShownNativeVersions.removeAll()
12
+ }
13
+
14
+ private let enabled: Bool
15
+ private let onStoreCheck: Bool
16
+ private let title: String?
17
+ private let message: String?
18
+ private let updateButton: String?
19
+ private let laterButton: String?
20
+ private let countryFilter: String?
21
+ private let cooldownMs: TimeInterval
22
+ private weak var plugin: CapacitorUpdaterPlugin?
23
+
24
+ convenience init(plugin: CapacitorUpdaterPlugin) {
25
+ let config = plugin.getConfig()
26
+ self.init(
27
+ plugin: plugin,
28
+ enabled: config.getBoolean("nativeUpdatePrompt", false),
29
+ onStoreCheck: config.getBoolean("nativeUpdatePromptOnStoreCheck", false),
30
+ title: config.getString("nativeUpdatePromptTitle"),
31
+ message: config.getString("nativeUpdatePromptMessage"),
32
+ updateButton: config.getString("nativeUpdatePromptUpdateButton"),
33
+ laterButton: config.getString("nativeUpdatePromptLaterButton"),
34
+ countryFilter: config.getString("nativeUpdatePromptCountry"),
35
+ cooldownHours: config.getInt("nativeUpdatePromptCooldownHours", 24)
36
+ )
37
+ }
38
+
39
+ init(
40
+ plugin: CapacitorUpdaterPlugin?,
41
+ enabled: Bool = false,
42
+ onStoreCheck: Bool = false,
43
+ title: String? = nil,
44
+ message: String? = nil,
45
+ updateButton: String? = nil,
46
+ laterButton: String? = nil,
47
+ countryFilter: String? = nil,
48
+ cooldownHours: Int = 24
49
+ ) {
50
+ self.plugin = plugin
51
+ self.enabled = enabled
52
+ self.onStoreCheck = onStoreCheck
53
+ self.title = title
54
+ self.message = message
55
+ self.updateButton = updateButton
56
+ self.laterButton = laterButton
57
+ self.countryFilter = countryFilter
58
+ let normalizedCooldownHours = max(1, cooldownHours)
59
+ self.cooldownMs = TimeInterval(normalizedCooldownHours) * 60 * 60
60
+ }
61
+
62
+ var isOnStoreCheckEnabled: Bool {
63
+ enabled && onStoreCheck
64
+ }
65
+
66
+ static func isNativeStoreUpdateBlock(response: AppVersion) -> Bool {
67
+ if response.breaking == true {
68
+ return true
69
+ }
70
+ if response.message == "store_update_required" {
71
+ return true
72
+ }
73
+ let error = response.error ?? ""
74
+ return error.hasPrefix("disable_auto_update")
75
+ }
76
+
77
+ func maybeShowForUpdateBlock(response: AppVersion) {
78
+ guard enabled, Self.isNativeStoreUpdateBlock(response: response) else {
79
+ return
80
+ }
81
+ scheduleShow()
82
+ }
83
+
84
+ func maybeShowForStoreUpdateAvailable() {
85
+ guard isOnStoreCheckEnabled else {
86
+ return
87
+ }
88
+ scheduleShow()
89
+ }
90
+
91
+ private func scheduleShow() {
92
+ guard matchesCountryFilter() else {
93
+ return
94
+ }
95
+ guard let plugin = plugin else {
96
+ return
97
+ }
98
+ let nativeVersion = plugin.currentBuildVersionForNativeUpdatePrompt()
99
+ guard !nativeVersion.isEmpty else {
100
+ return
101
+ }
102
+ guard shouldShowForNativeVersion(nativeVersion) else {
103
+ return
104
+ }
105
+
106
+ DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(600)) {
107
+ self.showDialog(nativeVersion: nativeVersion)
108
+ }
109
+ }
110
+
111
+ func shouldShowForNativeVersion(_ nativeVersion: String) -> Bool {
112
+ if Self.sessionShownNativeVersions.contains(nativeVersion) {
113
+ return false
114
+ }
115
+ let lastVersion = UserDefaults.standard.string(forKey: Self.lastNativeVersionDefaultsKey) ?? ""
116
+ let lastShown = UserDefaults.standard.double(forKey: Self.lastShownMsDefaultsKey)
117
+ if nativeVersion == lastVersion, lastShown > 0 {
118
+ let elapsed = Date().timeIntervalSince1970 - lastShown
119
+ if elapsed < cooldownMs {
120
+ return false
121
+ }
122
+ }
123
+ return true
124
+ }
125
+
126
+ func matchesCountryFilter() -> Bool {
127
+ guard let filter = countryFilter?.trimmingCharacters(in: .whitespacesAndNewlines), !filter.isEmpty else {
128
+ return true
129
+ }
130
+ let deviceCountry = Self.currentDeviceRegionCode()
131
+ if deviceCountry.isEmpty {
132
+ return false
133
+ }
134
+ let normalizedDevice = deviceCountry.uppercased()
135
+ return filter
136
+ .split(separator: ",")
137
+ .map { $0.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() }
138
+ .contains(normalizedDevice)
139
+ }
140
+
141
+ private func showDialog(nativeVersion: String) {
142
+ guard let plugin = plugin else {
143
+ return
144
+ }
145
+ if let topVC = UIApplication.topViewController(plugin.bridge?.viewController),
146
+ topVC.isKind(of: UIAlertController.self) {
147
+ return
148
+ }
149
+
150
+ let dialogTitle = (title?.isEmpty == false ? title : nil) ?? "Update available"
151
+ let dialogMessage = (message?.isEmpty == false ? message : nil)
152
+ ?? "A new version of this app is available in the store. Update now to continue receiving live updates."
153
+ let positive = (updateButton?.isEmpty == false ? updateButton : nil) ?? "Update"
154
+ let negative = (laterButton?.isEmpty == false ? laterButton : nil) ?? "Not now"
155
+
156
+ let alert = UIAlertController(title: dialogTitle, message: dialogMessage, preferredStyle: .alert)
157
+ alert.addAction(UIAlertAction(title: negative, style: .cancel) { _ in
158
+ self.recordShown(nativeVersion: nativeVersion)
159
+ })
160
+ alert.addAction(UIAlertAction(title: positive, style: .default) { _ in
161
+ self.recordShown(nativeVersion: nativeVersion)
162
+ plugin.openAppStoreFromNativeUpdatePrompt()
163
+ })
164
+
165
+ if let topVC = UIApplication.topViewController(plugin.bridge?.viewController) {
166
+ topVC.present(alert, animated: true)
167
+ }
168
+ }
169
+
170
+ func recordShown(nativeVersion: String) {
171
+ Self.sessionShownNativeVersions.insert(nativeVersion)
172
+ UserDefaults.standard.set(nativeVersion, forKey: Self.lastNativeVersionDefaultsKey)
173
+ UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: Self.lastShownMsDefaultsKey)
174
+ UserDefaults.standard.synchronize()
175
+ }
176
+
177
+ private static func currentDeviceRegionCode() -> String {
178
+ if #available(iOS 16, *) {
179
+ return Locale.current.region?.identifier ?? ""
180
+ }
181
+ return Locale.current.regionCode ?? ""
182
+ }
183
+ }
@@ -779,10 +779,13 @@ extension UIWindow {
779
779
  }
780
780
  return
781
781
  }
782
- bundle = try updater.download(
782
+ // Same gate as download() and auto-update: the checksum is required and
783
+ // verified (decrypted when a public key is set) before extraction.
784
+ bundle = try updater.downloadVerified(
783
785
  url: downloadUrl,
784
786
  version: latest.version,
785
- sessionKey: latest.sessionKey ?? ""
787
+ sessionKey: latest.sessionKey ?? "",
788
+ expectedChecksum: latest.checksum
786
789
  )
787
790
  }
788
791
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-updater",
3
- "version": "8.52.1",
3
+ "version": "8.53.1",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Live update for capacitor apps",
6
6
  "main": "dist/plugin.cjs.js",
@@ -71,7 +71,7 @@
71
71
  "clean": "rimraf ./dist",
72
72
  "watch": "tsc --watch",
73
73
  "prepublishOnly": "bun run build",
74
- "check:wiring": "node scripts/check-capacitor-plugin-wiring.mjs",
74
+ "check:wiring": "node scripts/check-capacitor-plugin-wiring.mjs && bun scripts/test-cli-crypto-output-guard.mjs",
75
75
  "check:cap9-deprecated": "bun scripts/check-cap9-deprecated.mjs",
76
76
  "changelog:ai": "node scripts/generate-ai-changelog.mjs",
77
77
  "example:install": "cd example-app && bun install --frozen-lockfile",
@@ -79,7 +79,10 @@
79
79
  "native:contract:crypto": "bun run native:contract:crypto:ios && bun run native:contract:crypto:android",
80
80
  "native:contract:crypto:ios": "./scripts/test-ios.sh -only-testing:CapacitorUpdaterPluginTests/RsaContractTests",
81
81
  "native:contract:crypto:android": "cd android && ./gradlew testDebugUnitTest --tests ee.forgr.capacitor_updater.RsaContractTest && cd ..",
82
- "generate:rsa-contract": "bun scripts/generate-rsa-contract-fixtures.mjs"
82
+ "native:contract:cli:ios": "./scripts/test-ios.sh -only-testing:CapacitorUpdaterPluginTests/CliCryptoFixtureTests",
83
+ "native:contract:cli:android": "cd android && ./gradlew testDebugUnitTest --tests ee.forgr.capacitor_updater.CliCryptoFixtureTest && cd ..",
84
+ "generate:rsa-contract": "bun scripts/generate-rsa-contract-fixtures.mjs",
85
+ "generate:cli-crypto": "bun scripts/generate-cli-crypto-fixtures.mjs"
83
86
  },
84
87
  "devDependencies": {
85
88
  "@capacitor/android": "^8.5.0",
@@ -87,6 +90,7 @@
87
90
  "@capacitor/core": "^8.5.0",
88
91
  "@capacitor/docgen": "^0.3.0",
89
92
  "@capacitor/ios": "^8.5.0",
93
+ "@capgo/cli": "8.76.0",
90
94
  "@ionic/eslint-config": "^0.5.0",
91
95
  "@typescript-eslint/eslint-plugin": "^8.70.0",
92
96
  "@typescript-eslint/parser": "^8.70.0",