Skip to content

Commit

Permalink
feat(updater): Collect unknown response fields (#2325)
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianLars authored Jan 23, 2025
1 parent bda803f commit 0afc9b6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changes/updater-unknown-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
updater: minor
updater-js: minor
---

The `Update` struct/object will now contain a `raw_json`/`rawJson` property to be able to read parts of server's json response that are not handled by the plugin.
2 changes: 1 addition & 1 deletion plugins/updater/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions plugins/updater/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface UpdateMetadata {
version: string
date?: string
body?: string
rawJson: Record<string, unknown>
}

/** Updater download event */
Expand All @@ -57,6 +58,7 @@ class Update extends Resource {
version: string
date?: string
body?: string
rawJson: Record<string, unknown>
private downloadedBytes?: Resource

constructor(metadata: UpdateMetadata) {
Expand All @@ -66,6 +68,7 @@ class Update extends Resource {
this.version = metadata.version
this.date = metadata.date
this.body = metadata.body
this.rawJson = metadata.rawJson
}

/** Download the updater package */
Expand Down
2 changes: 2 additions & 0 deletions plugins/updater/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub(crate) struct Metadata {
version: String,
date: Option<String>,
body: Option<String>,
raw_json: serde_json::Value,
}

struct DownloadedBytes(pub Vec<u8>);
Expand Down Expand Up @@ -73,6 +74,7 @@ pub(crate) async fn check<R: Runtime>(
metadata.version.clone_from(&update.version);
metadata.date = update.date.map(|d| d.to_string());
metadata.body.clone_from(&update.body);
metadata.raw_json.clone_from(&update.raw_json);
metadata.rid = Some(webview.resources_table().add(update));
}

Expand Down
7 changes: 6 additions & 1 deletion plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ impl Updater {
}

let mut remote_release: Option<RemoteRelease> = None;
let mut raw_json: Option<serde_json::Value> = None;
let mut last_error: Option<Error> = None;
for url in &self.endpoints {
// replace {{current_version}}, {{target}} and {{arch}} in the provided URL
Expand Down Expand Up @@ -379,7 +380,8 @@ impl Updater {
return Ok(None);
};

match serde_json::from_value::<RemoteRelease>(res.json().await?)
raw_json = Some(res.json().await?);
match serde_json::from_value::<RemoteRelease>(raw_json.clone().unwrap())
.map_err(Into::into)
{
Ok(release) => {
Expand Down Expand Up @@ -421,6 +423,7 @@ impl Updater {
download_url: release.download_url(&self.json_target)?.to_owned(),
body: release.notes.clone(),
signature: release.signature(&self.json_target)?.to_owned(),
raw_json: raw_json.unwrap(),
timeout: self.timeout,
proxy: self.proxy.clone(),
headers: self.headers.clone(),
Expand Down Expand Up @@ -454,6 +457,8 @@ pub struct Update {
pub download_url: Url,
/// Signature announced
pub signature: String,
/// The raw version of server's JSON response. Useful if the response contains additional fields that the updater doesn't handle.
pub raw_json: serde_json::Value,
/// Request timeout
pub timeout: Option<Duration>,
/// Request proxy
Expand Down

0 comments on commit 0afc9b6

Please sign in to comment.