Everything in Rust on Servo 0.7 (trimmed: no WebXR, WebGPU, multiprocess): tabs and windows, address bar with suggestions, history, bookmarks and reading list, downloads with resume and external download managers, encrypted password vault with autofill, ad blocker (adblock-rust, 22 lists), VPN through Tor or a proxy and secure DNS via a local bridge proxy, picture in picture, Discord Rich Presence, media player and visualizer, spell check, site apps, user scripts and styles, import from other browsers, gamepads, Web Bluetooth, and every setting of the original Nacre in a plain egui interface. Not compiled yet; docs/HANDOFF.md has the first build checklist. Co-Authored-By: Claude Opus 5.5 <[email protected]>
98 lines
3.9 KiB
Rust
98 lines
3.9 KiB
Rust
//! Update check: the latest release of the repository set in Settings → About, at most once a day.
|
|
//! Forgejo / Gitea: <host>[/path]/api/v1/repos/<owner>/<repo>/releases/latest; github.com:
|
|
//! api.github.com/repos/<owner>/<repo>/releases/latest.
|
|
|
|
use std::time::Duration;
|
|
|
|
use serde_json::Value;
|
|
use url::Url;
|
|
|
|
use crate::net::fetch;
|
|
|
|
pub const DAY: u64 = 24 * 3600;
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct Release {
|
|
pub version: String,
|
|
pub page: String,
|
|
/// Download for this platform, "" if none matched.
|
|
pub asset: String,
|
|
pub notes: String,
|
|
}
|
|
|
|
/// "v0.4.1" -> (0, 4, 1)
|
|
pub fn parse_version(v: &str) -> (u32, u32, u32) {
|
|
let mut nums = v.split(|c: char| !c.is_ascii_digit()).filter(|s| !s.is_empty()).map(|s| s.parse().unwrap_or(0));
|
|
(nums.next().unwrap_or(0), nums.next().unwrap_or(0), nums.next().unwrap_or(0))
|
|
}
|
|
|
|
pub fn current_version() -> &'static str {
|
|
env!("CARGO_PKG_VERSION")
|
|
}
|
|
|
|
pub fn api_url(repo: &str) -> Option<String> {
|
|
let url = Url::parse(repo.trim().trim_end_matches('/').trim_end_matches(".git")).ok()?;
|
|
if !matches!(url.scheme(), "http" | "https") {
|
|
return None;
|
|
}
|
|
let parts: Vec<&str> = url.path_segments()?.filter(|p| !p.is_empty()).collect();
|
|
if parts.len() < 2 {
|
|
return None;
|
|
}
|
|
let (owner, name) = (parts[parts.len() - 2], parts[parts.len() - 1]);
|
|
let host = url.host_str()?;
|
|
if host == "github.com" || host == "www.github.com" {
|
|
return Some(format!("https://api.github.com/repos/{owner}/{name}/releases/latest"));
|
|
}
|
|
let base: String = parts[..parts.len() - 2].iter().map(|p| format!("/{p}")).collect();
|
|
let port = url.port().map(|p| format!(":{p}")).unwrap_or_default();
|
|
Some(format!("{}://{host}{port}{base}/api/v1/repos/{owner}/{name}/releases/latest", url.scheme()))
|
|
}
|
|
|
|
fn pick_asset(assets: &[Value]) -> String {
|
|
let patterns: &[&str] = if cfg!(windows) { &["setup", ".exe"] } else { &["linux", ".appimage"] };
|
|
let name_of = |a: &Value| a.get("name").and_then(Value::as_str).unwrap_or("").to_lowercase();
|
|
let link_of = |a: &Value| a.get("browser_download_url").and_then(Value::as_str).unwrap_or("").to_owned();
|
|
if cfg!(windows) {
|
|
assets.iter().find(|a| { let n = name_of(a); n.contains(patterns[0]) && n.ends_with(patterns[1]) }).map(link_of).unwrap_or_default()
|
|
} else {
|
|
assets
|
|
.iter()
|
|
.find(|a| { let n = name_of(a); n.contains("linux") && n.ends_with(".tar.gz") })
|
|
.or_else(|| assets.iter().find(|a| name_of(a).ends_with(".appimage")))
|
|
.map(link_of)
|
|
.unwrap_or_default()
|
|
}
|
|
}
|
|
|
|
/// Ask the server for the latest release. Blocking.
|
|
pub fn check(repo: &str) -> Result<Release, String> {
|
|
let api = api_url(repo).ok_or("The release repository address isn't valid.")?;
|
|
let json = fetch::get_json(&api, Duration::from_secs(20))?;
|
|
let version = json.get("tag_name").and_then(Value::as_str).unwrap_or("").trim_start_matches('v').to_owned();
|
|
if version.is_empty() {
|
|
return Err("No release found.".into());
|
|
}
|
|
Ok(Release {
|
|
page: json.get("html_url").and_then(Value::as_str).unwrap_or(repo).to_owned(),
|
|
asset: json.get("assets").and_then(Value::as_array).map(|a| pick_asset(a)).unwrap_or_default(),
|
|
notes: json.get("body").and_then(Value::as_str).unwrap_or("").to_owned(),
|
|
version,
|
|
})
|
|
}
|
|
|
|
pub fn is_newer(version: &str) -> bool {
|
|
parse_version(version) > parse_version(current_version())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn api_urls() {
|
|
assert_eq!(api_url("https://archlinux.tail7c4f5e.ts.net/git/astar76/Nacre").as_deref(), Some("https://archlinux.tail7c4f5e.ts.net/git/api/v1/repos/astar76/Nacre/releases/latest"));
|
|
assert_eq!(api_url("https://github.com/a/b.git").as_deref(), Some("https://api.github.com/repos/a/b/releases/latest"));
|
|
assert!(parse_version("v0.10.0") > parse_version("0.9.9"));
|
|
}
|
|
}
|