Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(live-update): reset to the default bundle if the sync has not found a bundle #281

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serious-walls-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capawesome/capacitor-live-update': patch
---

fix: reset to the default bundle if the sync has not found a bundle
10 changes: 7 additions & 3 deletions packages/live-update/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ We recommend to declare [`CA92.1`](https://developer.apple.com/documentation/bun
| **`autoDeleteBundles`** | <code>boolean</code> | Whether or not to automatically delete unused bundles. When enabled, the plugin will automatically delete unused bundles after calling `ready()`. | <code>false</code> | 5.0.0 |
| **`defaultChannel`** | <code>string</code> | The default channel of the app. | | 6.3.0 |
| **`enabled`** | <code>boolean</code> | Whether or not the plugin is enabled. | <code>true</code> | 5.0.0 |
| **`httpTimeout`** | <code>number</code> | The timeout in milliseconds for HTTP requests. | <code>60000</code> | 6.4.0 |
| **`location`** | <code>'us' \| 'eu'</code> | The location of the server to use when using [Capawesome Cloud](https://capawesome.io/cloud). | <code>'us'</code> | 6.2.0 |
| **`publicKey`** | <code>string</code> | 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`** | <code>number</code> | The timeout in milliseconds to wait for the app to be ready before resetting to the default bundle. | <code>10000</code> | 5.0.0 |
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -620,9 +623,10 @@ Only available on Android and iOS.

#### SyncResult

| Prop | Type | Description | Since |
| ------------------ | --------------------------- | ---------------------------------------------------------------------------------------------------------- | ----- |
| **`nextBundleId`** | <code>string \| null</code> | 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`** | <code>string \| null</code> | The unique identifier of the current bundle. If `null`, the default bundle is being used. | 7.0.0 |
| **`nextBundleId`** | <code>string \| null</code> | The identifier of the next bundle to use. If `null`, the default bundle is being used. | 5.0.0 |

</docgen-api>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,42 +221,47 @@ 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,
new EmptyCallback() {
@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);
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class GetLatestBundleResponse {

@Nullable
private String bundleId;

@Nullable
Expand All @@ -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");
Expand All @@ -32,6 +41,7 @@ public GetLatestBundleResponse(JSONObject responseJson) {
this.url = responseJson.optString("url");
}

@Nullable
public String getBundleId() {
return bundleId;
}
Expand All @@ -46,6 +56,7 @@ public String getSignature() {
return signature;
}

@Nullable
public String getUrl() {
return url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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?
}
10 changes: 9 additions & 1 deletion packages/live-update/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading