Skip to content

Commit f8255e1

Browse files
authored
feat(updater): allow passing headers & timeout in Update.download* methods (#1661)
closes #1634
1 parent b9bcb2b commit f8255e1

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"updater-js": "patch"
3+
---
4+
5+
Add a second argument in `Update.download` and `Update.donloadAndInstall` JS APIs to modify headers and timeout when downloading the update.

plugins/updater/api-iife.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/updater/guest-js/index.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
import { invoke, Channel, Resource } from "@tauri-apps/api/core";
66

7-
/** Options used to check for updates */
7+
/** Options used when checking for updates */
88
interface CheckOptions {
99
/**
1010
* Request headers
1111
*/
1212
headers?: HeadersInit;
1313
/**
14-
* Timeout in seconds
14+
* Timeout in milliseconds
1515
*/
1616
timeout?: number;
1717
/**
@@ -24,6 +24,18 @@ interface CheckOptions {
2424
target?: string;
2525
}
2626

27+
/** Options used when downloading an update */
28+
interface DownloadOptions {
29+
/**
30+
* Request headers
31+
*/
32+
headers?: HeadersInit;
33+
/**
34+
* Timeout in milliseconds
35+
*/
36+
timeout?: number;
37+
}
38+
2739
interface UpdateMetadata {
2840
rid: number;
2941
available: boolean;
@@ -57,14 +69,18 @@ class Update extends Resource {
5769
}
5870

5971
/** Download the updater package */
60-
async download(onEvent?: (progress: DownloadEvent) => void): Promise<void> {
72+
async download(
73+
onEvent?: (progress: DownloadEvent) => void,
74+
options?: DownloadOptions,
75+
): Promise<void> {
6176
const channel = new Channel<DownloadEvent>();
6277
if (onEvent) {
6378
channel.onmessage = onEvent;
6479
}
6580
const downloadedBytesRid = await invoke<number>("plugin:updater|download", {
6681
onEvent: channel,
6782
rid: this.rid,
83+
...options,
6884
});
6985
this.downloadedBytes = new Resource(downloadedBytesRid);
7086
}
@@ -87,6 +103,7 @@ class Update extends Resource {
87103
/** Downloads the updater package and installs it */
88104
async downloadAndInstall(
89105
onEvent?: (progress: DownloadEvent) => void,
106+
options?: DownloadOptions,
90107
): Promise<void> {
91108
const channel = new Channel<DownloadEvent>();
92109
if (onEvent) {
@@ -95,6 +112,7 @@ class Update extends Resource {
95112
await invoke("plugin:updater|download_and_install", {
96113
onEvent: channel,
97114
rid: this.rid,
115+
...options,
98116
});
99117
}
100118

@@ -115,5 +133,5 @@ async function check(options?: CheckOptions): Promise<Update | null> {
115133
}).then((meta) => (meta.available ? new Update(meta) : null));
116134
}
117135

118-
export type { CheckOptions, DownloadEvent };
136+
export type { CheckOptions, DownloadOptions, DownloadEvent };
119137
export { check, Update };

plugins/updater/src/commands.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use crate::{Result, Update, UpdaterExt};
66

7+
use http::{HeaderMap, HeaderName, HeaderValue};
78
use serde::Serialize;
89
use tauri::{ipc::Channel, Manager, Resource, ResourceId, Runtime, Webview};
910

10-
use std::time::Duration;
11+
use std::{str::FromStr, time::Duration};
1112
use url::Url;
1213

1314
#[derive(Debug, Clone, Serialize)]
@@ -53,7 +54,7 @@ pub(crate) async fn check<R: Runtime>(
5354
}
5455
}
5556
if let Some(timeout) = timeout {
56-
builder = builder.timeout(Duration::from_secs(timeout));
57+
builder = builder.timeout(Duration::from_millis(timeout));
5758
}
5859
if let Some(ref proxy) = proxy {
5960
let url = Url::parse(proxy.as_str())?;
@@ -83,8 +84,25 @@ pub(crate) async fn download<R: Runtime>(
8384
webview: Webview<R>,
8485
rid: ResourceId,
8586
on_event: Channel<DownloadEvent>,
87+
headers: Option<Vec<(String, String)>>,
88+
timeout: Option<u64>,
8689
) -> Result<ResourceId> {
8790
let update = webview.resources_table().get::<Update>(rid)?;
91+
92+
let mut update = (*update).clone();
93+
94+
if let Some(headers) = headers {
95+
let mut map = HeaderMap::new();
96+
for (k, v) in headers {
97+
map.append(HeaderName::from_str(&k)?, HeaderValue::from_str(&v)?);
98+
}
99+
update.headers = map;
100+
}
101+
102+
if let Some(timeout) = timeout {
103+
update.timeout = Some(Duration::from_millis(timeout));
104+
}
105+
88106
let mut first_chunk = true;
89107
let bytes = update
90108
.download(
@@ -100,6 +118,7 @@ pub(crate) async fn download<R: Runtime>(
100118
},
101119
)
102120
.await?;
121+
103122
Ok(webview.resources_table().add(DownloadedBytes(bytes)))
104123
}
105124

@@ -123,9 +142,25 @@ pub(crate) async fn download_and_install<R: Runtime>(
123142
webview: Webview<R>,
124143
rid: ResourceId,
125144
on_event: Channel<DownloadEvent>,
145+
headers: Option<Vec<(String, String)>>,
146+
timeout: Option<u64>,
126147
) -> Result<()> {
127148
let update = webview.resources_table().get::<Update>(rid)?;
128149

150+
let mut update = (*update).clone();
151+
152+
if let Some(headers) = headers {
153+
let mut map = HeaderMap::new();
154+
for (k, v) in headers {
155+
map.append(HeaderName::from_str(&k)?, HeaderValue::from_str(&v)?);
156+
}
157+
update.headers = map;
158+
}
159+
160+
if let Some(timeout) = timeout {
161+
update.timeout = Some(Duration::from_millis(timeout));
162+
}
163+
129164
let mut first_chunk = true;
130165

131166
update

plugins/updater/src/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ pub enum Error {
6868
#[error(transparent)]
6969
Http(#[from] http::Error),
7070
#[error(transparent)]
71+
InvalidHeaderValue(#[from] http::header::InvalidHeaderValue),
72+
#[error(transparent)]
73+
InvalidHeaderName(#[from] http::header::InvalidHeaderName),
74+
#[error(transparent)]
7175
Tauri(#[from] tauri::Error),
7276
}
7377

plugins/updater/src/updater.rs

-4
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,6 @@ impl Update {
469469
"Accept",
470470
HeaderValue::from_str("application/octet-stream").unwrap(),
471471
);
472-
headers.insert(
473-
"User-Agent",
474-
HeaderValue::from_str("tauri-updater").unwrap(),
475-
);
476472

477473
let mut request = ClientBuilder::new().user_agent(UPDATER_USER_AGENT);
478474
if let Some(timeout) = self.timeout {

0 commit comments

Comments
 (0)