Skip to content

Commit 2d731f8

Browse files
fix(updater): format Update.date to RFC 3339 (#2573)
* fix(updater): format `Update.date` to RFC 3339 * Messed up on argument in #2572 * Format * Update example * Avoid extra to_string * Deprecate `Update.available`
1 parent 0bc5d58 commit 2d731f8

File tree

7 files changed

+46
-27
lines changed

7 files changed

+46
-27
lines changed

.changes/updater-date-format.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"updater": "minor:bug"
3+
"updater-js": "minor:bug"
4+
---
5+
6+
Fix JS API `Update.date` not formatted to RFC 3339

examples/api/src/views/Updater.svelte

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
isChecking = true
1313
try {
1414
const update = await check()
15-
onMessage(`Should update: ${update.available}`)
16-
onMessage(update)
15+
if (update) {
16+
onMessage(`Should update: ${update.available}`)
17+
onMessage(update)
1718
18-
newUpdate = update
19+
newUpdate = update
20+
} else {
21+
onMessage('No update available')
22+
}
1923
} catch (e) {
2024
onMessage(e)
2125
} finally {

plugins/updater/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/updater/guest-js/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ interface DownloadOptions {
3838

3939
interface UpdateMetadata {
4040
rid: number
41-
available: boolean
4241
currentVersion: string
4342
version: string
4443
date?: string
@@ -53,6 +52,8 @@ type DownloadEvent =
5352
| { event: 'Finished' }
5453

5554
class Update extends Resource {
55+
// TODO: remove this field in v3
56+
/** @deprecated This is always true, check if the return value is `null` instead when using {@linkcode check} */
5657
available: boolean
5758
currentVersion: string
5859
version: string
@@ -63,7 +64,7 @@ class Update extends Resource {
6364

6465
constructor(metadata: UpdateMetadata) {
6566
super(metadata.rid)
66-
this.available = metadata.available
67+
this.available = true
6768
this.currentVersion = metadata.currentVersion
6869
this.version = metadata.version
6970
this.date = metadata.date
@@ -131,12 +132,10 @@ async function check(options?: CheckOptions): Promise<Update | null> {
131132
options.headers = Array.from(new Headers(options.headers).entries())
132133
}
133134

134-
return await invoke<UpdateMetadata>('plugin:updater|check', {
135+
const metadata = await invoke<UpdateMetadata | null>('plugin:updater|check', {
135136
...options
136-
}).then((meta) =>
137-
// TODO: Handle this in the rust side
138-
meta.available ? new Update(meta) : null
139-
)
137+
})
138+
return metadata ? new Update(metadata) : null
140139
}
141140

142141
export type { CheckOptions, DownloadOptions, DownloadEvent }

plugins/updater/src/commands.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ pub enum DownloadEvent {
2828
#[derive(Serialize, Default)]
2929
#[serde(rename_all = "camelCase")]
3030
pub(crate) struct Metadata {
31-
rid: Option<ResourceId>,
32-
available: bool,
31+
rid: ResourceId,
3332
current_version: String,
3433
version: String,
3534
date: Option<String>,
@@ -40,16 +39,14 @@ pub(crate) struct Metadata {
4039
struct DownloadedBytes(pub Vec<u8>);
4140
impl Resource for DownloadedBytes {}
4241

43-
// TODO: Align this with the result of `updater.check` to Result<Option<Metadata>>
44-
// and remove `available` instead of handling this in the js side
4542
#[tauri::command]
4643
pub(crate) async fn check<R: Runtime>(
4744
webview: Webview<R>,
4845
headers: Option<Vec<(String, String)>>,
4946
timeout: Option<u64>,
5047
proxy: Option<String>,
5148
target: Option<String>,
52-
) -> Result<Metadata> {
49+
) -> Result<Option<Metadata>> {
5350
let mut builder = webview.updater_builder();
5451
if let Some(headers) = headers {
5552
for (k, v) in headers {
@@ -69,18 +66,28 @@ pub(crate) async fn check<R: Runtime>(
6966

7067
let updater = builder.build()?;
7168
let update = updater.check().await?;
72-
let mut metadata = Metadata::default();
69+
7370
if let Some(update) = update {
74-
metadata.available = true;
75-
metadata.current_version.clone_from(&update.current_version);
76-
metadata.version.clone_from(&update.version);
77-
metadata.date = update.date.map(|d| d.to_string());
78-
metadata.body.clone_from(&update.body);
79-
metadata.raw_json.clone_from(&update.raw_json);
80-
metadata.rid = Some(webview.resources_table().add(update));
71+
let formatted_date = if let Some(date) = update.date {
72+
let formatted_date = date
73+
.format(&time::format_description::well_known::Rfc3339)
74+
.map_err(|_| crate::Error::FormatDate)?;
75+
Some(formatted_date)
76+
} else {
77+
None
78+
};
79+
let metadata = Metadata {
80+
current_version: update.current_version.clone(),
81+
version: update.version.clone(),
82+
date: formatted_date,
83+
body: update.body.clone(),
84+
raw_json: update.raw_json.clone(),
85+
rid: webview.resources_table().add(update),
86+
};
87+
Ok(Some(metadata))
88+
} else {
89+
Ok(None)
8190
}
82-
83-
Ok(metadata)
8491
}
8592

8693
#[tauri::command]

plugins/updater/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ pub enum Error {
7777
InvalidHeaderValue(#[from] http::header::InvalidHeaderValue),
7878
#[error(transparent)]
7979
InvalidHeaderName(#[from] http::header::InvalidHeaderName),
80+
#[error("Failed to format date")]
81+
FormatDate,
8082
/// The configured updater endpoint must use a secure protocol like `https`
8183
#[error("The configured updater endpoint must use a secure protocol like `https`.")]
8284
InsecureTransportProtocol,

plugins/updater/src/updater.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ impl UpdaterBuilder {
310310
I: IntoIterator<Item = S>,
311311
S: Into<OsString>,
312312
{
313-
self.installer_args.extend(args.into_iter().map(Into::into));
313+
self.current_exe_args
314+
.extend(args.into_iter().map(Into::into));
314315
self
315316
}
316317
}

0 commit comments

Comments
 (0)