Skip to content

Commit

Permalink
feat(updater): allow passing headers & timeout in Update.download*
Browse files Browse the repository at this point in the history
…methods (tauri-apps#1661)

closes tauri-apps#1634
  • Loading branch information
amrbashir authored and Sir-Thom committed Oct 22, 2024
1 parent 65cb44f commit 9356155
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/updater-js-headers-download.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"updater-js": "patch"
---

Add a second argument in `Update.download` and `Update.donloadAndInstall` JS APIs to modify headers and timeout when downloading the update.
2 changes: 1 addition & 1 deletion plugins/updater/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions plugins/updater/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

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

/** Options used to check for updates */
/** Options used when checking for updates */
interface CheckOptions {
/**
* Request headers
*/
headers?: HeadersInit;
/**
* Timeout in seconds
* Timeout in milliseconds
*/
timeout?: number;
/**
Expand All @@ -24,6 +24,18 @@ interface CheckOptions {
target?: string;
}

/** Options used when downloading an update */
interface DownloadOptions {
/**
* Request headers
*/
headers?: HeadersInit;
/**
* Timeout in milliseconds
*/
timeout?: number;
}

interface UpdateMetadata {
rid: number;
available: boolean;
Expand Down Expand Up @@ -57,14 +69,18 @@ class Update extends Resource {
}

/** Download the updater package */
async download(onEvent?: (progress: DownloadEvent) => void): Promise<void> {
async download(
onEvent?: (progress: DownloadEvent) => void,
options?: DownloadOptions,
): Promise<void> {
const channel = new Channel<DownloadEvent>();
if (onEvent) {
channel.onmessage = onEvent;
}
const downloadedBytesRid = await invoke<number>("plugin:updater|download", {
onEvent: channel,
rid: this.rid,
...options,
});
this.downloadedBytes = new Resource(downloadedBytesRid);
}
Expand All @@ -87,6 +103,7 @@ class Update extends Resource {
/** Downloads the updater package and installs it */
async downloadAndInstall(
onEvent?: (progress: DownloadEvent) => void,
options?: DownloadOptions,
): Promise<void> {
const channel = new Channel<DownloadEvent>();
if (onEvent) {
Expand All @@ -95,6 +112,7 @@ class Update extends Resource {
await invoke("plugin:updater|download_and_install", {
onEvent: channel,
rid: this.rid,
...options,
});
}

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

export type { CheckOptions, DownloadEvent };
export type { CheckOptions, DownloadOptions, DownloadEvent };
export { check, Update };
39 changes: 37 additions & 2 deletions plugins/updater/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

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

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

use std::time::Duration;
use std::{str::FromStr, time::Duration};
use url::Url;

#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -53,7 +54,7 @@ pub(crate) async fn check<R: Runtime>(
}
}
if let Some(timeout) = timeout {
builder = builder.timeout(Duration::from_secs(timeout));
builder = builder.timeout(Duration::from_millis(timeout));
}
if let Some(ref proxy) = proxy {
let url = Url::parse(proxy.as_str())?;
Expand Down Expand Up @@ -83,8 +84,25 @@ pub(crate) async fn download<R: Runtime>(
webview: Webview<R>,
rid: ResourceId,
on_event: Channel<DownloadEvent>,
headers: Option<Vec<(String, String)>>,
timeout: Option<u64>,
) -> Result<ResourceId> {
let update = webview.resources_table().get::<Update>(rid)?;

let mut update = (*update).clone();

if let Some(headers) = headers {
let mut map = HeaderMap::new();
for (k, v) in headers {
map.append(HeaderName::from_str(&k)?, HeaderValue::from_str(&v)?);
}
update.headers = map;
}

if let Some(timeout) = timeout {
update.timeout = Some(Duration::from_millis(timeout));
}

let mut first_chunk = true;
let bytes = update
.download(
Expand All @@ -100,6 +118,7 @@ pub(crate) async fn download<R: Runtime>(
},
)
.await?;

Ok(webview.resources_table().add(DownloadedBytes(bytes)))
}

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

let mut update = (*update).clone();

if let Some(headers) = headers {
let mut map = HeaderMap::new();
for (k, v) in headers {
map.append(HeaderName::from_str(&k)?, HeaderValue::from_str(&v)?);
}
update.headers = map;
}

if let Some(timeout) = timeout {
update.timeout = Some(Duration::from_millis(timeout));
}

let mut first_chunk = true;

update
Expand Down
4 changes: 4 additions & 0 deletions plugins/updater/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub enum Error {
#[error(transparent)]
Http(#[from] http::Error),
#[error(transparent)]
InvalidHeaderValue(#[from] http::header::InvalidHeaderValue),
#[error(transparent)]
InvalidHeaderName(#[from] http::header::InvalidHeaderName),
#[error(transparent)]
Tauri(#[from] tauri::Error),
}

Expand Down
4 changes: 0 additions & 4 deletions plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,6 @@ impl Update {
"Accept",
HeaderValue::from_str("application/octet-stream").unwrap(),
);
headers.insert(
"User-Agent",
HeaderValue::from_str("tauri-updater").unwrap(),
);

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

0 comments on commit 9356155

Please sign in to comment.