@capgo/capacitor-updater 9.0.0 → 9.0.2

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 (109) hide show
  1. package/CapgoCapacitorUpdater.podspec +3 -6
  2. package/Package.swift +30 -0
  3. package/README.md +2406 -291
  4. package/android/build.gradle +37 -17
  5. package/android/consumer-rules.pro +48 -0
  6. package/android/proguard-rules.pro +23 -0
  7. package/android/src/main/AndroidManifest.xml +0 -1
  8. package/android/src/main/java/ee/forgr/capacitor_updater/AndroidAppExitReporter.java +92 -0
  9. package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +115 -0
  10. package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +227 -195
  11. package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +24 -23
  12. package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
  13. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +7927 -1243
  14. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +5785 -0
  15. package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +564 -121
  16. package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +44 -0
  17. package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +43 -49
  18. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
  19. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +427 -0
  20. package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +230 -0
  21. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +2041 -118
  22. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +301 -0
  23. package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +569 -0
  24. package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +436 -0
  25. package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +604 -0
  26. package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +79 -0
  27. package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +419 -0
  28. package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
  29. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
  30. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +963 -0
  31. package/android/src/main/java/ee/forgr/capacitor_updater/StatsBatchFailureDiagnostics.java +40 -0
  32. package/android/src/main/java/ee/forgr/capacitor_updater/ThreeFingerPinchDetector.java +323 -0
  33. package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
  34. package/dist/docs.json +4431 -840
  35. package/dist/esm/definitions.d.ts +2641 -225
  36. package/dist/esm/definitions.js +103 -1
  37. package/dist/esm/definitions.js.map +1 -1
  38. package/dist/esm/history.d.ts +1 -0
  39. package/dist/esm/history.js +283 -0
  40. package/dist/esm/history.js.map +1 -0
  41. package/dist/esm/index.d.ts +3 -2
  42. package/dist/esm/index.js +5 -4
  43. package/dist/esm/index.js.map +1 -1
  44. package/dist/esm/web.d.ts +53 -42
  45. package/dist/esm/web.js +180 -37
  46. package/dist/esm/web.js.map +1 -1
  47. package/dist/plugin.cjs.js +570 -37
  48. package/dist/plugin.cjs.js.map +1 -1
  49. package/dist/plugin.js +570 -37
  50. package/dist/plugin.js.map +1 -1
  51. package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +262 -0
  52. package/ios/Sources/CapacitorUpdaterPlugin/AppHealthTracker.swift +82 -0
  53. package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +185 -0
  54. package/ios/Sources/CapacitorUpdaterPlugin/BundleStatus.swift +127 -0
  55. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +6813 -0
  56. package/ios/Sources/CapacitorUpdaterPlugin/CapgoRawRsa.swift +36 -0
  57. package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +200 -0
  58. package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +5099 -0
  59. package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +387 -0
  60. package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +343 -0
  61. package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
  62. package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +452 -0
  63. package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +380 -0
  64. package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
  65. package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +96 -0
  66. package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +97 -0
  67. package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +876 -0
  68. package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +0 -2
  69. package/ios/Sources/CapacitorUpdaterPlugin/WKWebView+CapgoObjcBridge.swift +20 -0
  70. package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +304 -0
  71. package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
  72. package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
  73. package/ios/Tests/CapacitorUpdaterPluginTests/BuiltinHashesTests.swift +210 -0
  74. package/ios/Tests/CapacitorUpdaterPluginTests/BundleHardeningTests.swift +292 -0
  75. package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +6533 -0
  76. package/ios/Tests/CapacitorUpdaterPluginTests/CapgoSemanticVersionTests.swift +64 -0
  77. package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +156 -0
  78. package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +314 -0
  79. package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +653 -0
  80. package/ios/Tests/CapacitorUpdaterPluginTests/InstallPerfTests.swift +174 -0
  81. package/ios/Tests/CapacitorUpdaterPluginTests/ListChannelsTests.swift +74 -0
  82. package/ios/Tests/CapacitorUpdaterPluginTests/LoggerSecurityTests.swift +149 -0
  83. package/ios/Tests/CapacitorUpdaterPluginTests/NativeContractTests.swift +160 -0
  84. package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +1109 -0
  85. package/ios/Tests/CapacitorUpdaterPluginTests/PopulateDeltaCacheTests.swift +573 -0
  86. package/ios/Tests/CapacitorUpdaterPluginTests/RedirectPolicyTests.swift +63 -0
  87. package/ios/Tests/CapacitorUpdaterPluginTests/RsaContractTests.swift +281 -0
  88. package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +196 -0
  89. package/ios/Tests/CapacitorUpdaterPluginTests/TestZipWriter.swift +235 -0
  90. package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +470 -0
  91. package/ios/Tests/CapacitorUpdaterPluginTests/WebKitOverlayGuardTests.swift +32 -0
  92. package/ios/Tests/CapacitorUpdaterPluginTests/ZipArchiveReaderTests.swift +302 -0
  93. package/ios/Tests/CapacitorUpdaterPluginTests/ZipBundleExtractionTests.swift +122 -0
  94. package/package.json +80 -43
  95. package/scripts/builtin-hashes-lib.mjs +148 -0
  96. package/scripts/capacitor-builtin-hashes-hook.mjs +65 -0
  97. package/scripts/resolve-real-path.mjs +25 -0
  98. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1130
  99. package/ios/Plugin/BundleInfo.swift +0 -113
  100. package/ios/Plugin/BundleStatus.swift +0 -48
  101. package/ios/Plugin/CapacitorUpdater.swift +0 -858
  102. package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
  103. package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
  104. package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -677
  105. package/ios/Plugin/CryptoCipher.swift +0 -240
  106. /package/{LICENCE → LICENSE} +0 -0
  107. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
  108. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
  109. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
@@ -0,0 +1,419 @@
1
+ package ee.forgr.capacitor_updater;
2
+
3
+ import android.util.ArrayMap;
4
+ import android.util.Log;
5
+ import androidx.annotation.NonNull;
6
+ import androidx.annotation.Nullable;
7
+ import com.getcapacitor.*;
8
+ import java.nio.ByteBuffer;
9
+ import java.nio.CharBuffer;
10
+ import java.nio.charset.CharsetEncoder;
11
+ import java.nio.charset.CoderResult;
12
+ import java.nio.charset.CodingErrorAction;
13
+ import java.nio.charset.StandardCharsets;
14
+ import java.util.ArrayList;
15
+ import java.util.Arrays;
16
+ import java.util.Date;
17
+ import java.util.Locale;
18
+ import java.util.Map;
19
+ import org.jetbrains.annotations.Contract;
20
+ import org.json.JSONObject;
21
+
22
+ public class Logger {
23
+
24
+ static final int MAX_WEBVIEW_LOG_PAYLOAD_CHARS = 4096;
25
+
26
+ private Bridge bridge;
27
+
28
+ public enum LogLevel {
29
+ silent,
30
+ error,
31
+ warn,
32
+ info,
33
+ debug
34
+ }
35
+
36
+ public static class Options {
37
+
38
+ LogLevel level;
39
+ Map<String, String> labels;
40
+ boolean useSystemLog;
41
+
42
+ Options() {
43
+ level = LogLevel.info;
44
+ labels = null;
45
+ useSystemLog = true; // Default to true for backward compatibility
46
+ }
47
+
48
+ Options(@NonNull Options other) {
49
+ level = other.level;
50
+ labels = other.labels;
51
+ useSystemLog = other.useSystemLog;
52
+ }
53
+
54
+ Options(LogLevel level) {
55
+ this.level = level;
56
+ this.labels = null;
57
+ useSystemLog = true;
58
+ }
59
+
60
+ Options(Map<String, String> labels) {
61
+ this.level = LogLevel.info;
62
+ this.labels = labels;
63
+ useSystemLog = true;
64
+ }
65
+
66
+ Options(LogLevel level, Map<String, String> labels) {
67
+ this.level = level;
68
+ this.labels = labels;
69
+ useSystemLog = true;
70
+ }
71
+
72
+ Options(LogLevel level, Map<String, String> labels, boolean useSystemLog) {
73
+ this.level = level;
74
+ this.labels = labels;
75
+ this.useSystemLog = useSystemLog;
76
+ }
77
+
78
+ Options(boolean useSystemLog) {
79
+ this.level = LogLevel.info;
80
+ this.labels = null;
81
+ this.useSystemLog = useSystemLog;
82
+ }
83
+ }
84
+
85
+ public LogLevel level;
86
+ private final Map<LogLevel, String> labels = new ArrayMap<>();
87
+ private String tag;
88
+ private final ArrayMap<String, Long> timers = new ArrayMap<>();
89
+ private final String kDefaultTimerLabel = "default";
90
+ private boolean useSystemLog;
91
+
92
+ public void setBridge(Bridge bridge) {
93
+ this.bridge = bridge;
94
+ }
95
+
96
+ public Logger(String tag) {
97
+ super();
98
+ init(tag, new Options());
99
+ }
100
+
101
+ public Logger(String tag, Options options) {
102
+ super();
103
+ init(tag, options);
104
+ }
105
+
106
+ private void init(String tag, @NonNull Options options) {
107
+ this.level = options.level;
108
+ this.useSystemLog = options.useSystemLog;
109
+ this.labels.putAll(
110
+ Map.of(LogLevel.silent, "", LogLevel.error, "🔴", LogLevel.warn, "🟠", LogLevel.info, "🟢", LogLevel.debug, "\uD83D\uDD0E")
111
+ );
112
+
113
+ if (options.labels != null) {
114
+ setLabels(options.labels);
115
+ }
116
+
117
+ this.tag = tag;
118
+ }
119
+
120
+ @Nullable
121
+ public Object getLevelWithName(String name) {
122
+ try {
123
+ return LogLevel.valueOf(name);
124
+ } catch (IllegalArgumentException ex) {
125
+ return null;
126
+ }
127
+ }
128
+
129
+ public String getLevelName() {
130
+ return level.name();
131
+ }
132
+
133
+ public void setLevelName(String name) {
134
+ try {
135
+ level = LogLevel.valueOf(name);
136
+ } catch (IllegalArgumentException e) {
137
+ // Ignore it
138
+ }
139
+ }
140
+
141
+ public Map<String, String> getLabels() {
142
+ ArrayMap<String, String> result = new ArrayMap<>();
143
+
144
+ for (Map.Entry<LogLevel, String> entry : labels.entrySet()) {
145
+ result.put(entry.getKey().name(), entry.getValue());
146
+ }
147
+
148
+ return result;
149
+ }
150
+
151
+ public void setLabels(@NonNull Map<String, String> labels) {
152
+ for (Map.Entry<String, String> entry : labels.entrySet()) {
153
+ Object level = getLevelWithName(entry.getKey());
154
+
155
+ if (level != null) {
156
+ this.labels.put((LogLevel) level, entry.getValue());
157
+ }
158
+ }
159
+ }
160
+
161
+ public String getTag() {
162
+ return tag;
163
+ }
164
+
165
+ public void setTag(@NonNull String tag) {
166
+ if (!tag.isEmpty()) {
167
+ this.tag = tag;
168
+ }
169
+ }
170
+
171
+ public void error(String message) {
172
+ logWithTagAtLevel(LogLevel.error, "", tag, message);
173
+ }
174
+
175
+ public void warn(String message) {
176
+ logWithTagAtLevel(LogLevel.warn, "", tag, message);
177
+ }
178
+
179
+ public void info(String message) {
180
+ logWithTagAtLevel(LogLevel.info, "", tag, message);
181
+ }
182
+
183
+ public void log(String message) {
184
+ info(message);
185
+ }
186
+
187
+ public void debug(String message) {
188
+ logWithTagAtLevel(LogLevel.debug, "", tag, message);
189
+ }
190
+
191
+ public void logAtLevel(LogLevel level, String message) {
192
+ logWithTagAtLevel(level, "", tag, message);
193
+ }
194
+
195
+ public void logAtLevel(String level, String message) {
196
+ logWithTagAtLevel(resolveLevelName(level), "", tag, message);
197
+ }
198
+
199
+ public void logWithTagAtLevel(LogLevel level, String tag, String message) {
200
+ logWithTagAtLevel(level, "", tag, message);
201
+ }
202
+
203
+ public void logWithTagAtLevel(String level, String tag, String message) {
204
+ logWithTagAtLevel(resolveLevelName(level), "", tag, message);
205
+ }
206
+
207
+ private LogLevel resolveLevelName(String name) {
208
+ Object level = getLevelWithName(name);
209
+
210
+ if (level != null) {
211
+ return (LogLevel) level;
212
+ }
213
+
214
+ return LogLevel.info;
215
+ }
216
+
217
+ public void logWithTagAtLevel(LogLevel level, String label, String tag, String message) {
218
+ if (this.level.compareTo(level) < 0) {
219
+ return;
220
+ }
221
+
222
+ if (label.isEmpty()) {
223
+ label = labels.get(level);
224
+ }
225
+
226
+ // If the label is ASCII, surround it with []
227
+ String format;
228
+
229
+ if (label != null) {
230
+ format = label.charAt(0) <= 127 ? "[%s]: %s" : "%s %s";
231
+ } else {
232
+ label = "";
233
+ format = "%s%s";
234
+ }
235
+
236
+ String formattedMessage = String.format(format, label, message);
237
+
238
+ if (tag.isEmpty()) {
239
+ tag = this.tag;
240
+ }
241
+
242
+ // Log to Android system log if enabled
243
+ if (useSystemLog) {
244
+ switch (level) {
245
+ case error:
246
+ Log.e(tag, formattedMessage);
247
+ break;
248
+ case warn:
249
+ Log.w(tag, formattedMessage);
250
+ break;
251
+ case info:
252
+ Log.i(tag, formattedMessage);
253
+ break;
254
+ case debug:
255
+ Log.d(tag, formattedMessage);
256
+ break;
257
+ }
258
+ }
259
+
260
+ // Send to JavaScript if webView is available
261
+ if (bridge != null && bridge.getWebView() != null) {
262
+ String script = buildWebViewConsoleScript(level, tag, formattedMessage);
263
+ if (script != null) {
264
+ bridge.eval(script, null);
265
+ }
266
+ }
267
+ }
268
+
269
+ @Nullable
270
+ static String consoleMethodForLevel(@NonNull LogLevel level) {
271
+ switch (level) {
272
+ case error:
273
+ return "error";
274
+ case warn:
275
+ return "warn";
276
+ case info:
277
+ return "info";
278
+ case debug:
279
+ return "debug";
280
+ default:
281
+ return null;
282
+ }
283
+ }
284
+
285
+ @NonNull
286
+ static String toJsStringLiteral(@NonNull String value) {
287
+ return JSONObject.quote(value);
288
+ }
289
+
290
+ @NonNull
291
+ static String capWebViewLogPayload(@NonNull String payload) {
292
+ CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder()
293
+ .onMalformedInput(CodingErrorAction.REPLACE)
294
+ .onUnmappableCharacter(CodingErrorAction.REPLACE);
295
+ ByteBuffer bytes = ByteBuffer.allocate(MAX_WEBVIEW_LOG_PAYLOAD_CHARS);
296
+ CharBuffer chars = CharBuffer.wrap(payload);
297
+ CoderResult result = encoder.encode(chars, bytes, true);
298
+ if (result.isUnderflow()) {
299
+ result = encoder.flush(bytes);
300
+ }
301
+ if (result.isUnderflow()) {
302
+ return payload;
303
+ }
304
+
305
+ return new String(bytes.array(), 0, bytes.position(), StandardCharsets.UTF_8) + "...";
306
+ }
307
+
308
+ @Nullable
309
+ static String buildWebViewConsoleScript(@NonNull LogLevel level, @NonNull String tag, @NonNull String formattedMessage) {
310
+ String consoleMethod = consoleMethodForLevel(level);
311
+ if (consoleMethod == null) {
312
+ return null;
313
+ }
314
+
315
+ String payload = capWebViewLogPayload("[" + tag + "] " + formattedMessage);
316
+ return "console." + consoleMethod + "(" + toJsStringLiteral(payload) + ")";
317
+ }
318
+
319
+ public void dir(Object value) {
320
+ String message;
321
+
322
+ if (value != null) {
323
+ if (value.getClass().isArray()) {
324
+ Object[] arr = (Object[]) value;
325
+ message = Arrays.deepToString(arr);
326
+ } else {
327
+ try {
328
+ message = value.toString();
329
+ } catch (Exception ex) {
330
+ message = String.format("<%s>", ex.getMessage());
331
+ }
332
+ }
333
+ } else {
334
+ message = "null";
335
+ }
336
+
337
+ info(message);
338
+ }
339
+
340
+ public void time() {
341
+ time(kDefaultTimerLabel);
342
+ }
343
+
344
+ public void time(String label) {
345
+ timers.put(label, new Date().getTime());
346
+ }
347
+
348
+ public void timeLog() {
349
+ timeLog(kDefaultTimerLabel);
350
+ }
351
+
352
+ public void timeLog(String label) {
353
+ label = resolveTimerLabel(label);
354
+
355
+ if (timers.containsKey(label)) {
356
+ Long start = timers.get(label);
357
+
358
+ // This will always be true
359
+ if (start != null) {
360
+ long now = new Date().getTime();
361
+ long diff = now - start;
362
+ info(String.format(Locale.getDefault(), "%s: %s", label, formatMilliseconds(diff)));
363
+ }
364
+ } else {
365
+ warn(String.format("timer '%s' does not exist", label));
366
+ }
367
+ }
368
+
369
+ public void timeEnd() {
370
+ timeEnd(kDefaultTimerLabel);
371
+ }
372
+
373
+ public void timeEnd(String label) {
374
+ label = resolveTimerLabel(label);
375
+ timeLog(label);
376
+ timers.remove(label);
377
+ }
378
+
379
+ @Contract(pure = true)
380
+ private String resolveTimerLabel(@NonNull String label) {
381
+ return label.isEmpty() ? kDefaultTimerLabel : label;
382
+ }
383
+
384
+ @NonNull
385
+ private String formatMilliseconds(long milliseconds) {
386
+ long seconds = Math.floorDiv(milliseconds, 1000);
387
+ long minutes = Math.floorDiv(seconds, 60);
388
+ long hours = Math.floorDiv(minutes, 60);
389
+ long millis = milliseconds % 1000;
390
+
391
+ if (seconds < 1) {
392
+ return String.format(Locale.getDefault(), "%dms", milliseconds);
393
+ }
394
+
395
+ if (minutes < 1) {
396
+ return String.format(Locale.getDefault(), "%d.%ds", seconds, millis);
397
+ }
398
+
399
+ seconds = seconds % 60;
400
+
401
+ if (hours < 1) {
402
+ return String.format(Locale.getDefault(), "%d:%02d.%03d (min:sec.ms)", minutes, seconds, millis);
403
+ }
404
+
405
+ minutes = minutes % 60;
406
+ return String.format(Locale.getDefault(), "%d:%02d:%02d (hr:min:sec)", hours, minutes, seconds);
407
+ }
408
+
409
+ public void trace() {
410
+ StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
411
+ ArrayList<String> stack = new ArrayList<>();
412
+
413
+ for (StackTraceElement element : stackTrace) {
414
+ stack.add(element.toString());
415
+ }
416
+
417
+ info("trace\n" + String.join("\n", stack));
418
+ }
419
+ }