From 393b5d72499a50e2f260b8ce1fb7e29a55e71151 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Sun, 1 Sep 2024 22:43:45 +0200 Subject: [PATCH] fix(live-update): reset to the default bundle if the sync has not found a bundle --- .changeset/serious-walls-kiss.md | 5 +++ packages/live-update/README.md | 10 ++++-- .../plugins/liveupdate/LiveUpdate.java | 36 +++++++++++-------- .../classes/api/GetLatestBundleResponse.java | 11 ++++++ .../classes/results/SyncResult.java | 8 +++-- .../Api/GetLatestBundleResponse.swift | 4 +-- packages/live-update/src/definitions.ts | 10 +++++- 7 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 .changeset/serious-walls-kiss.md diff --git a/.changeset/serious-walls-kiss.md b/.changeset/serious-walls-kiss.md new file mode 100644 index 0000000..2550cc7 --- /dev/null +++ b/.changeset/serious-walls-kiss.md @@ -0,0 +1,5 @@ +--- +'@capawesome/capacitor-live-update': patch +--- + +fix: reset to the default bundle if the sync has not found a bundle diff --git a/packages/live-update/README.md b/packages/live-update/README.md index cb573fb..e056931 100644 --- a/packages/live-update/README.md +++ b/packages/live-update/README.md @@ -71,6 +71,7 @@ We recommend to declare [`CA92.1`](https://developer.apple.com/documentation/bun | **`autoDeleteBundles`** | boolean | Whether or not to automatically delete unused bundles. When enabled, the plugin will automatically delete unused bundles after calling `ready()`. | false | 5.0.0 | | **`defaultChannel`** | string | The default channel of the app. | | 6.3.0 | | **`enabled`** | boolean | Whether or not the plugin is enabled. | true | 5.0.0 | +| **`httpTimeout`** | number | The timeout in milliseconds for HTTP requests. | 60000 | 6.4.0 | | **`location`** | 'us' \| 'eu' | The location of the server to use when using [Capawesome Cloud](https://capawesome.io/cloud). | 'us' | 6.2.0 | | **`publicKey`** | string | The public key to verify the integrity of the bundle. The public key must be a PEM-encoded RSA public key. | | 6.1.0 | | **`readyTimeout`** | number | The timeout in milliseconds to wait for the app to be ready before resetting to the default bundle. | 10000 | 5.0.0 | @@ -88,6 +89,7 @@ In `capacitor.config.json`: "autoDeleteBundles": undefined, "defaultChannel": 'production', "enabled": undefined, + "httpTimeout": undefined, "location": 'eu', "publicKey": '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDodf1SD0OOn6hIlDuKBza0Ed0OqtwyVJwiyjmE9BJaZ7y8ZUfcF+SKmd0l2cDPM45XIg2tAFux5n29uoKyHwSt+6tCi5CJA5Z1/1eZruRRqABLonV77KS3HUtvOgqRLDnKSV89dYZkM++NwmzOPgIF422mvc+VukcVOBfc8/AHQIDAQAB-----END PUBLIC KEY-----', "readyTimeout": undefined, @@ -111,6 +113,7 @@ const config: CapacitorConfig = { autoDeleteBundles: undefined, defaultChannel: 'production', enabled: undefined, + httpTimeout: undefined, location: 'eu', publicKey: '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDodf1SD0OOn6hIlDuKBza0Ed0OqtwyVJwiyjmE9BJaZ7y8ZUfcF+SKmd0l2cDPM45XIg2tAFux5n29uoKyHwSt+6tCi5CJA5Z1/1eZruRRqABLonV77KS3HUtvOgqRLDnKSV89dYZkM++NwmzOPgIF422mvc+VukcVOBfc8/AHQIDAQAB-----END PUBLIC KEY-----', readyTimeout: undefined, @@ -620,9 +623,10 @@ Only available on Android and iOS. #### SyncResult -| Prop | Type | Description | Since | -| ------------------ | --------------------------- | ---------------------------------------------------------------------------------------------------------- | ----- | -| **`nextBundleId`** | string \| null | The identifier of the next bundle to use. If `null`, the app is up-to-date and no new bundle is available. | 5.0.0 | +| Prop | Type | Description | Since | +| --------------------- | --------------------------- | ----------------------------------------------------------------------------------------- | ----- | +| **`currentBundleId`** | string \| null | The unique identifier of the current bundle. If `null`, the default bundle is being used. | 7.0.0 | +| **`nextBundleId`** | string \| null | The identifier of the next bundle to use. If `null`, the default bundle is being used. | 5.0.0 | diff --git a/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/LiveUpdate.java b/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/LiveUpdate.java index d3255ec..5e3dfd3 100644 --- a/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/LiveUpdate.java +++ b/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/LiveUpdate.java @@ -221,33 +221,38 @@ public void error(Exception exception) { } // No update available Logger.debug(LiveUpdatePlugin.TAG, "No update available."); - SyncResult syncResult = new SyncResult(null); + String currentBundleId = getCurrentBundleId(); + SyncResult syncResult = new SyncResult(currentBundleId, currentBundleId); callback.success(syncResult); } @Override public void success(@NonNull GetLatestBundleResponse result) { - String latestBundleId = result.getBundleId(); + String currentBundleId = getCurrentBundleId(); + String nextBundleId = result.getBundleId(); String checksum = result.getChecksum(); String signature = result.getSignature(); String url = result.getUrl(); + // Check if a latest bundle is available + if (nextBundleId == null) { + Logger.debug(LiveUpdatePlugin.TAG, "No update available."); + setNextCapacitorServerPathToDefaultWebAssetDir(); + SyncResult syncResult = new SyncResult(currentBundleId, null); + callback.success(syncResult); + return; + } // Check if the bundle already exists - if (hasBundle(latestBundleId)) { - String nextBundleId = null; - String currentBundleId = getCurrentBundleId(); - if (!latestBundleId.equals(currentBundleId)) { - // Set the next bundle - setNextBundle(latestBundleId); - nextBundleId = latestBundleId; - } - SyncResult syncResult = new SyncResult(nextBundleId); + if (hasBundle(nextBundleId)) { + // Set the next bundle + setNextBundle(nextBundleId); + SyncResult syncResult = new SyncResult(currentBundleId, nextBundleId); callback.success(syncResult); return; } // Download and unzip the bundle downloadBundle( - latestBundleId, + nextBundleId, checksum, signature, url, @@ -255,8 +260,8 @@ public void success(@NonNull GetLatestBundleResponse result) { @Override public void success() { // Set the next bundle - setNextBundle(latestBundleId); - SyncResult syncResult = new SyncResult(latestBundleId); + setNextBundle(nextBundleId); + SyncResult syncResult = new SyncResult(currentBundleId, nextBundleId); callback.success(syncResult); } @@ -542,6 +547,9 @@ public void onResponse(@NonNull okhttp3.Call call, @NonNull okhttp3.Response res } GetLatestBundleResponse getLatestBundleResponse = new GetLatestBundleResponse(responseJson); callback.success(getLatestBundleResponse); + } else if (response.code() == 404) { + GetLatestBundleResponse getLatestBundleResponse = new GetLatestBundleResponse(); + callback.success(getLatestBundleResponse); } else { Exception exception = new Exception(responseBody.string()); Logger.error(LiveUpdatePlugin.TAG, exception.getMessage(), exception); diff --git a/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/classes/api/GetLatestBundleResponse.java b/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/classes/api/GetLatestBundleResponse.java index 8d13215..32c38bb 100644 --- a/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/classes/api/GetLatestBundleResponse.java +++ b/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/classes/api/GetLatestBundleResponse.java @@ -5,6 +5,7 @@ public class GetLatestBundleResponse { + @Nullable private String bundleId; @Nullable @@ -13,8 +14,16 @@ public class GetLatestBundleResponse { @Nullable private String signature; + @Nullable private String url; + public GetLatestBundleResponse() { + this.bundleId = null; + this.checksum = null; + this.signature = null; + this.url = null; + } + public GetLatestBundleResponse(JSONObject responseJson) { this.bundleId = responseJson.optString("bundleId"); String checksum = responseJson.optString("checksum", "null"); @@ -32,6 +41,7 @@ public GetLatestBundleResponse(JSONObject responseJson) { this.url = responseJson.optString("url"); } + @Nullable public String getBundleId() { return bundleId; } @@ -46,6 +56,7 @@ public String getSignature() { return signature; } + @Nullable public String getUrl() { return url; } diff --git a/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/classes/results/SyncResult.java b/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/classes/results/SyncResult.java index 354e5d4..75188ae 100644 --- a/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/classes/results/SyncResult.java +++ b/packages/live-update/android/src/main/java/io/capawesome/capacitorjs/plugins/liveupdate/classes/results/SyncResult.java @@ -7,15 +7,19 @@ public class SyncResult implements Result { + @Nullable + private final String currentBundleId; @Nullable private final String nextBundleId; - public SyncResult(@Nullable String nextBundleId) { - this.nextBundleId = nextBundleId; + public SyncResult(@Nullable String currentBundleId, @Nullable String nextBundleId) { + this.currentBundleId = currentBundleId; + this.nextBundleId = nextBundleId; } public JSObject toJSObject() { JSObject result = new JSObject(); + result.put("currentBundleId", currentBundleId == null ? JSONObject.NULL : currentBundleId); result.put("nextBundleId", nextBundleId == null ? JSONObject.NULL : nextBundleId); return result; } diff --git a/packages/live-update/ios/Plugin/Protocols/Api/GetLatestBundleResponse.swift b/packages/live-update/ios/Plugin/Protocols/Api/GetLatestBundleResponse.swift index b4303f1..6476054 100644 --- a/packages/live-update/ios/Plugin/Protocols/Api/GetLatestBundleResponse.swift +++ b/packages/live-update/ios/Plugin/Protocols/Api/GetLatestBundleResponse.swift @@ -1,6 +1,6 @@ struct GetLatestBundleResponse: Codable { - var bundleId: String + var bundleId: String? var checksum: String? var signature: String? - var url: String + var url: String? } diff --git a/packages/live-update/src/definitions.ts b/packages/live-update/src/definitions.ts index 1507596..fadc80c 100644 --- a/packages/live-update/src/definitions.ts +++ b/packages/live-update/src/definitions.ts @@ -411,10 +411,18 @@ export interface SetCustomIdOptions { * @since 5.0.0 */ export interface SyncResult { + /** + * The unique identifier of the current bundle. + * + * If `null`, the default bundle is being used. + * + * @since 7.0.0 + */ + currentBundleId: string | null; /** * The identifier of the next bundle to use. * - * If `null`, the app is up-to-date and no new bundle is available. + * If `null`, the default bundle is being used. * * @since 5.0.0 */