Skip to content

Commit fbb8ea6

Browse files
committed
fix(updater): format Update.date to RFC 3339
1 parent 0bc5d58 commit fbb8ea6

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
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

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: 5 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,7 @@ type DownloadEvent =
5352
| { event: 'Finished' }
5453

5554
class Update extends Resource {
55+
// TODO: remove this field in v3
5656
available: boolean
5757
currentVersion: string
5858
version: string
@@ -63,7 +63,7 @@ class Update extends Resource {
6363

6464
constructor(metadata: UpdateMetadata) {
6565
super(metadata.rid)
66-
this.available = metadata.available
66+
this.available = true
6767
this.currentVersion = metadata.currentVersion
6868
this.version = metadata.version
6969
this.date = metadata.date
@@ -131,12 +131,10 @@ async function check(options?: CheckOptions): Promise<Update | null> {
131131
options.headers = Array.from(new Headers(options.headers).entries())
132132
}
133133

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

142140
export type { CheckOptions, DownloadOptions, DownloadEvent }

plugins/updater/src/commands.rs

Lines changed: 23 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,29 @@ 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+
.to_string();
76+
Some(formatted_date)
77+
} else {
78+
None
79+
};
80+
let metadata = Metadata {
81+
current_version: update.current_version.clone(),
82+
version: update.version.clone(),
83+
date: formatted_date,
84+
body: update.body.clone(),
85+
raw_json: update.raw_json.clone(),
86+
rid: webview.resources_table().add(update),
87+
};
88+
Ok(Some(metadata))
89+
} else {
90+
Ok(None)
8191
}
82-
83-
Ok(metadata)
8492
}
8593

8694
#[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,

0 commit comments

Comments
 (0)