Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .changes/updater-add-on-before-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"updater": minor
"updater-js": minor
---

Add `UpdaterBuilder::configure_client` method on Rust side, to configure the `reqwest` client used to check and download the update.
2 changes: 1 addition & 1 deletion plugins/process/permissions/schemas/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,4 @@
]
}
}
}
}
28 changes: 28 additions & 0 deletions plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl RemoteRelease {
}

pub type OnBeforeExit = Arc<dyn Fn() + Send + Sync + 'static>;
pub type OnBeforeRequest = Arc<dyn Fn(ClientBuilder) -> ClientBuilder + Send + Sync + 'static>;
pub type VersionComparator = Arc<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>;
type MainThreadClosure = Box<dyn FnOnce() + Send + Sync + 'static>;
type RunOnMainThread =
Expand All @@ -114,6 +115,7 @@ pub struct UpdaterBuilder {
installer_args: Vec<OsString>,
current_exe_args: Vec<OsString>,
on_before_exit: Option<OnBeforeExit>,
configure_client: Option<OnBeforeRequest>,
}

impl UpdaterBuilder {
Expand All @@ -140,6 +142,7 @@ impl UpdaterBuilder {
timeout: None,
proxy: None,
on_before_exit: None,
configure_client: None,
}
}

Expand Down Expand Up @@ -239,6 +242,19 @@ impl UpdaterBuilder {
self
}

/// Allows you to modify the `reqwest` client builder before the HTTP request is sent.
///
/// Note that `reqwest` crate may be updated in minor releases of tauri-plugin-updater.
/// Therefore it's recommended to pin the plugin to at least a minor version when you're using `configure_client`.
///
pub fn configure_client<F: Fn(ClientBuilder) -> ClientBuilder + Send + Sync + 'static>(
mut self,
f: F,
) -> Self {
self.configure_client.replace(Arc::new(f));
self
}

pub fn build(self) -> Result<Updater> {
let endpoints = self
.endpoints
Expand Down Expand Up @@ -282,6 +298,7 @@ impl UpdaterBuilder {
headers: self.headers,
extract_path,
on_before_exit: self.on_before_exit,
configure_client: self.configure_client,
})
}
}
Expand Down Expand Up @@ -316,6 +333,7 @@ pub struct Updater {
headers: HeaderMap,
extract_path: PathBuf,
on_before_exit: Option<OnBeforeExit>,
configure_client: Option<OnBeforeRequest>,
#[allow(unused)]
installer_args: Vec<OsString>,
#[allow(unused)]
Expand Down Expand Up @@ -376,6 +394,11 @@ impl Updater {
let proxy = reqwest::Proxy::all(proxy.as_str())?;
request = request.proxy(proxy);
}

if let Some(ref configure_client) = self.configure_client {
request = configure_client(request);
}

let response = request
.build()?
.get(url)
Expand Down Expand Up @@ -440,6 +463,7 @@ impl Updater {
headers: self.headers.clone(),
installer_args: self.installer_args.clone(),
current_exe_args: self.current_exe_args.clone(),
configure_client: self.configure_client.clone(),
})
} else {
None
Expand Down Expand Up @@ -488,6 +512,7 @@ pub struct Update {
installer_args: Vec<OsString>,
#[allow(unused)]
current_exe_args: Vec<OsString>,
configure_client: Option<OnBeforeRequest>,
}

impl Resource for Update {}
Expand Down Expand Up @@ -516,6 +541,9 @@ impl Update {
let proxy = reqwest::Proxy::all(proxy.as_str())?;
request = request.proxy(proxy);
}
if let Some(ref configure_client) = self.configure_client {
request = configure_client(request);
}
let response = request
.build()?
.get(self.download_url.clone())
Expand Down
Loading