From faefcc9fd8c61f709d491649e255a7fcac82c09a Mon Sep 17 00:00:00 2001 From: Guilherme Oenning Date: Sun, 9 Mar 2025 01:53:16 +0000 Subject: [PATCH] feat(updater): add `configure_client` to `UpdaterBuilder` (#2430) --- .changes/updater-add-on-before-request.md | 6 +++++ plugins/updater/src/updater.rs | 28 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 .changes/updater-add-on-before-request.md diff --git a/.changes/updater-add-on-before-request.md b/.changes/updater-add-on-before-request.md new file mode 100644 index 00000000..f086d35e --- /dev/null +++ b/.changes/updater-add-on-before-request.md @@ -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. \ No newline at end of file diff --git a/plugins/updater/src/updater.rs b/plugins/updater/src/updater.rs index 37472b7c..8a4fbb04 100644 --- a/plugins/updater/src/updater.rs +++ b/plugins/updater/src/updater.rs @@ -96,6 +96,7 @@ impl RemoteRelease { } pub type OnBeforeExit = Arc; +pub type OnBeforeRequest = Arc ClientBuilder + Send + Sync + 'static>; pub type VersionComparator = Arc bool + Send + Sync>; type MainThreadClosure = Box; type RunOnMainThread = @@ -117,6 +118,7 @@ pub struct UpdaterBuilder { installer_args: Vec, current_exe_args: Vec, on_before_exit: Option, + configure_client: Option, } impl UpdaterBuilder { @@ -143,6 +145,7 @@ impl UpdaterBuilder { timeout: None, proxy: None, on_before_exit: None, + configure_client: None, } } @@ -242,6 +245,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 ClientBuilder + Send + Sync + 'static>( + mut self, + f: F, + ) -> Self { + self.configure_client.replace(Arc::new(f)); + self + } + pub fn build(self) -> Result { let endpoints = self .endpoints @@ -285,6 +301,7 @@ impl UpdaterBuilder { headers: self.headers, extract_path, on_before_exit: self.on_before_exit, + configure_client: self.configure_client, }) } } @@ -319,6 +336,7 @@ pub struct Updater { headers: HeaderMap, extract_path: PathBuf, on_before_exit: Option, + configure_client: Option, #[allow(unused)] installer_args: Vec, #[allow(unused)] @@ -382,6 +400,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) @@ -463,6 +486,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 @@ -511,6 +535,7 @@ pub struct Update { installer_args: Vec, #[allow(unused)] current_exe_args: Vec, + configure_client: Option, } impl Resource for Update {} @@ -539,6 +564,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())