Desktop host: offline read cache (SQLite), job ids for progress and cancel, parallel live portfolio, project init, verified ffmpeg / Chrome for Testing downloads into app data
This commit is contained in:
parent
0ace432e87
commit
95b7cf7ced
21 changed files with 1444 additions and 58 deletions
89
apps/desktop/src-tauri/Cargo.lock
generated
89
apps/desktop/src-tauri/Cargo.lock
generated
|
|
@ -1090,6 +1090,18 @@ dependencies = [
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fallible-iterator"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fallible-streaming-iterator"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastrand"
|
name = "fastrand"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
|
|
@ -1577,12 +1589,30 @@ version = "0.14.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.16.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
|
||||||
|
dependencies = [
|
||||||
|
"foldhash",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.17.1"
|
version = "0.17.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashlink"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "824e001ac4f3012dd16a264bec811403a67ca9deb6c102fc5049b32c4574b35f"
|
||||||
|
dependencies = [
|
||||||
|
"hashbrown 0.16.1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
|
|
@ -2219,6 +2249,17 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libsqlite3-sys"
|
||||||
|
version = "0.36.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "95b4103cffefa72eb8428cb6b47d6627161e51c2739fc5e3b734584157bc642a"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"pkg-config",
|
||||||
|
"vcpkg",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.12.1"
|
version = "0.12.1"
|
||||||
|
|
@ -3337,6 +3378,31 @@ dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rsqlite-vfs"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c"
|
||||||
|
dependencies = [
|
||||||
|
"hashbrown 0.16.1",
|
||||||
|
"thiserror 2.0.21",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rusqlite"
|
||||||
|
version = "0.38.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1c93dd1c9683b438c392c492109cb702b8090b2bfc8fed6f6e4eb4523f17af3"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.13.2",
|
||||||
|
"fallible-iterator",
|
||||||
|
"fallible-streaming-iterator",
|
||||||
|
"hashlink",
|
||||||
|
"libsqlite3-sys",
|
||||||
|
"smallvec",
|
||||||
|
"sqlite-wasm-rs",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust-ini"
|
name = "rust-ini"
|
||||||
version = "0.21.3"
|
version = "0.21.3"
|
||||||
|
|
@ -3918,6 +3984,18 @@ dependencies = [
|
||||||
"system-deps",
|
"system-deps",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sqlite-wasm-rs"
|
||||||
|
version = "0.5.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc3efc0da82635d7e1ced0053bbbfa8c7ab9645d0bf36ceb4f7127bb85315d75"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"js-sys",
|
||||||
|
"rsqlite-vfs",
|
||||||
|
"wasm-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "stable_deref_trait"
|
name = "stable_deref_trait"
|
||||||
version = "1.2.1"
|
version = "1.2.1"
|
||||||
|
|
@ -3938,6 +4016,9 @@ dependencies = [
|
||||||
"getrandom 0.3.4",
|
"getrandom 0.3.4",
|
||||||
"hmac",
|
"hmac",
|
||||||
"keyring",
|
"keyring",
|
||||||
|
"reqwest",
|
||||||
|
"rusqlite",
|
||||||
|
"rustls",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sha2",
|
"sha2",
|
||||||
|
|
@ -3955,6 +4036,7 @@ dependencies = [
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"tokio",
|
"tokio",
|
||||||
"url",
|
"url",
|
||||||
|
"zip",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -5040,6 +5122,12 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vcpkg"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version-compare"
|
name = "version-compare"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
|
|
@ -6078,6 +6166,7 @@ checksum = "caa8cd6af31c3b31c6631b8f483848b91589021b28fffe50adada48d4f4d2ed1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arbitrary",
|
"arbitrary",
|
||||||
"crc32fast",
|
"crc32fast",
|
||||||
|
"flate2",
|
||||||
"indexmap 2.14.2",
|
"indexmap 2.14.2",
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,12 @@ base64 = "0.22"
|
||||||
getrandom = "0.3"
|
getrandom = "0.3"
|
||||||
url = "2"
|
url = "2"
|
||||||
tokio = { version = "1", features = ["sync", "time"] }
|
tokio = { version = "1", features = ["sync", "time"] }
|
||||||
|
# Offline cache of the last store reads (app data cache.sqlite); SQLite compiled in, nothing system-wide.
|
||||||
|
rusqlite = { version = "0.38", features = ["bundled"] }
|
||||||
|
# Dependencies screen downloads (ffmpeg, Chrome for Testing): the same HTTP stack the updater already brings.
|
||||||
|
reqwest = { version = "0.13", default-features = false, features = ["rustls-no-provider", "system-proxy"] }
|
||||||
|
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
|
||||||
|
zip = { version = "4", default-features = false, features = ["deflate-flate2-zlib-rs"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
{
|
{
|
||||||
"$schema": "../gen/schemas/desktop-schema.json",
|
"$schema": "../gen/schemas/desktop-schema.json",
|
||||||
"identifier": "main-window",
|
"identifier": "main-window",
|
||||||
"description": "The main window with the app's own bundled pages only (no remote content). It may listen to app events (sidecar progress, deep links) and call the storebound app commands; every command re-checks the window label and page origin in Rust. No plugin API is granted to the webview: the sidecar is spawned from Rust with fixed arguments, dialogs, the opener, notifications, the updater and process control are driven from Rust only, and credentials never reach the page.",
|
"description": "The main window with the app's own bundled pages only (no remote content). It may listen to app events (sidecar progress, tool downloads, deep links) and call the storebound app commands; every command re-checks the window label and page origin in Rust. No plugin API is granted to the webview: the sidecar is spawned from Rust with fixed arguments, dialogs, the opener, notifications, the updater and process control are driven from Rust only, and credentials never reach the page.",
|
||||||
"windows": ["main"],
|
"windows": [
|
||||||
|
"main"
|
||||||
|
],
|
||||||
"local": true,
|
"local": true,
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"core:event:allow-listen",
|
"core:event:allow-listen",
|
||||||
|
|
@ -10,9 +12,13 @@
|
||||||
"allow-app-info",
|
"allow-app-info",
|
||||||
"allow-sidecar-status",
|
"allow-sidecar-status",
|
||||||
"allow-sidecar-call",
|
"allow-sidecar-call",
|
||||||
|
"allow-cache-get",
|
||||||
|
"allow-cache-clear",
|
||||||
|
"allow-job-cancel",
|
||||||
"allow-write-with-confirm",
|
"allow-write-with-confirm",
|
||||||
"allow-projects-list",
|
"allow-projects-list",
|
||||||
"allow-project-add",
|
"allow-project-add",
|
||||||
|
"allow-project-init",
|
||||||
"allow-project-remove",
|
"allow-project-remove",
|
||||||
"allow-project-set-accounts",
|
"allow-project-set-accounts",
|
||||||
"allow-accounts-list",
|
"allow-accounts-list",
|
||||||
|
|
@ -20,6 +26,9 @@
|
||||||
"allow-account-add-apple",
|
"allow-account-add-apple",
|
||||||
"allow-account-remove",
|
"allow-account-remove",
|
||||||
"allow-portfolio-status",
|
"allow-portfolio-status",
|
||||||
"allow-open-external"
|
"allow-open-external",
|
||||||
|
"allow-tools-info",
|
||||||
|
"allow-tools-install",
|
||||||
|
"allow-tools-uninstall"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Automatically generated - DO NOT EDIT!
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "allow-cache-clear"
|
||||||
|
description = "Enables the cache_clear command without any pre-configured scope."
|
||||||
|
commands.allow = ["cache_clear"]
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "deny-cache-clear"
|
||||||
|
description = "Denies the cache_clear command without any pre-configured scope."
|
||||||
|
commands.deny = ["cache_clear"]
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Automatically generated - DO NOT EDIT!
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "allow-cache-get"
|
||||||
|
description = "Enables the cache_get command without any pre-configured scope."
|
||||||
|
commands.allow = ["cache_get"]
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "deny-cache-get"
|
||||||
|
description = "Denies the cache_get command without any pre-configured scope."
|
||||||
|
commands.deny = ["cache_get"]
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Automatically generated - DO NOT EDIT!
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "allow-job-cancel"
|
||||||
|
description = "Enables the job_cancel command without any pre-configured scope."
|
||||||
|
commands.allow = ["job_cancel"]
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "deny-job-cancel"
|
||||||
|
description = "Denies the job_cancel command without any pre-configured scope."
|
||||||
|
commands.deny = ["job_cancel"]
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Automatically generated - DO NOT EDIT!
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "allow-project-init"
|
||||||
|
description = "Enables the project_init command without any pre-configured scope."
|
||||||
|
commands.allow = ["project_init"]
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "deny-project-init"
|
||||||
|
description = "Denies the project_init command without any pre-configured scope."
|
||||||
|
commands.deny = ["project_init"]
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Automatically generated - DO NOT EDIT!
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "allow-tools-info"
|
||||||
|
description = "Enables the tools_info command without any pre-configured scope."
|
||||||
|
commands.allow = ["tools_info"]
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "deny-tools-info"
|
||||||
|
description = "Denies the tools_info command without any pre-configured scope."
|
||||||
|
commands.deny = ["tools_info"]
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Automatically generated - DO NOT EDIT!
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "allow-tools-install"
|
||||||
|
description = "Enables the tools_install command without any pre-configured scope."
|
||||||
|
commands.allow = ["tools_install"]
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "deny-tools-install"
|
||||||
|
description = "Denies the tools_install command without any pre-configured scope."
|
||||||
|
commands.deny = ["tools_install"]
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Automatically generated - DO NOT EDIT!
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "allow-tools-uninstall"
|
||||||
|
description = "Enables the tools_uninstall command without any pre-configured scope."
|
||||||
|
commands.allow = ["tools_uninstall"]
|
||||||
|
|
||||||
|
[[permission]]
|
||||||
|
identifier = "deny-tools-uninstall"
|
||||||
|
description = "Denies the tools_uninstall command without any pre-configured scope."
|
||||||
|
commands.deny = ["tools_uninstall"]
|
||||||
102
apps/desktop/src-tauri/src/cache.rs
Normal file
102
apps/desktop/src-tauri/src/cache.rs
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
//! Offline cache of the last successful reads (app data `cache.sqlite`): store status, health, reviews, TestFlight,
|
||||||
|
//! catalog parity, listings, icons. The window shows a cached value with its "last read" time while nothing newer is
|
||||||
|
//! there, and says "unknown" when there is none; a cached value is never shown as fresh. Secrets are never cached
|
||||||
|
//! (results only, never params), and a project's rows go when the project is removed.
|
||||||
|
use std::path::Path;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use rusqlite::{params, Connection, OptionalExtension};
|
||||||
|
use serde::Serialize;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
pub const FILE: &str = "cache.sqlite";
|
||||||
|
/// Results above this size are not kept (logs of a long render, say); the window reads them live.
|
||||||
|
pub const MAX_VALUE_BYTES: usize = 4 * 1024 * 1024;
|
||||||
|
|
||||||
|
pub struct Cache {
|
||||||
|
db: Mutex<Connection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||||
|
pub struct Entry {
|
||||||
|
pub value: Value,
|
||||||
|
/// When it was read (RFC 3339 UTC).
|
||||||
|
pub at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cache key: project id, method and the canonical arguments (the same canonical JSON as confirm tokens).
|
||||||
|
pub fn key(project: &str, method: &str, args: &Value) -> String {
|
||||||
|
let a = crate::confirm::canonical(args).unwrap_or_else(|_| args.to_string());
|
||||||
|
format!("{project}\u{1f}{method}\u{1f}{a}")
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Cache {
|
||||||
|
pub fn open(dir: &Path) -> Result<Cache, String> {
|
||||||
|
std::fs::create_dir_all(dir).map_err(|e| e.to_string())?;
|
||||||
|
Cache::with(Connection::open(dir.join(FILE)).map_err(|e| e.to_string())?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn in_memory() -> Result<Cache, String> {
|
||||||
|
Cache::with(Connection::open_in_memory().map_err(|e| e.to_string())?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with(db: Connection) -> Result<Cache, String> {
|
||||||
|
db.execute_batch(
|
||||||
|
"PRAGMA journal_mode=WAL;
|
||||||
|
CREATE TABLE IF NOT EXISTS reads (
|
||||||
|
key TEXT PRIMARY KEY,
|
||||||
|
project TEXT NOT NULL,
|
||||||
|
method TEXT NOT NULL,
|
||||||
|
value TEXT NOT NULL,
|
||||||
|
at TEXT NOT NULL
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS reads_project ON reads(project);",
|
||||||
|
)
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
Ok(Cache { db: Mutex::new(db) })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn put(&self, project: &str, method: &str, args: &Value, value: &Value, at: &str) -> Result<(), String> {
|
||||||
|
let text = value.to_string();
|
||||||
|
if text.len() > MAX_VALUE_BYTES {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let db = self.db.lock().map_err(|_| "cache poisoned")?;
|
||||||
|
db.execute(
|
||||||
|
"INSERT INTO reads(key, project, method, value, at) VALUES (?1, ?2, ?3, ?4, ?5)
|
||||||
|
ON CONFLICT(key) DO UPDATE SET value = excluded.value, at = excluded.at",
|
||||||
|
params![key(project, method, args), project, method, text, at],
|
||||||
|
)
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, project: &str, method: &str, args: &Value) -> Result<Option<Entry>, String> {
|
||||||
|
let db = self.db.lock().map_err(|_| "cache poisoned")?;
|
||||||
|
let row: Option<(String, String)> = db
|
||||||
|
.query_row("SELECT value, at FROM reads WHERE key = ?1", params![key(project, method, args)], |r| Ok((r.get(0)?, r.get(1)?)))
|
||||||
|
.optional()
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
Ok(row.and_then(|(v, at)| serde_json::from_str(&v).ok().map(|value| Entry { value, at })))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Every cached method of a project with its time (for the "last read" stamps).
|
||||||
|
pub fn stamps(&self, project: &str) -> Result<Vec<(String, String)>, String> {
|
||||||
|
let db = self.db.lock().map_err(|_| "cache poisoned")?;
|
||||||
|
let mut st = db.prepare("SELECT method, MAX(at) FROM reads WHERE project = ?1 GROUP BY method ORDER BY method").map_err(|e| e.to_string())?;
|
||||||
|
let rows = st.query_map(params![project], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))).map_err(|e| e.to_string())?;
|
||||||
|
rows.collect::<Result<Vec<_>, _>>().map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn forget_project(&self, project: &str) -> Result<(), String> {
|
||||||
|
let db = self.db.lock().map_err(|_| "cache poisoned")?;
|
||||||
|
db.execute("DELETE FROM reads WHERE project = ?1", params![project]).map_err(|e| e.to_string())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&self) -> Result<(), String> {
|
||||||
|
let db = self.db.lock().map_err(|_| "cache poisoned")?;
|
||||||
|
db.execute("DELETE FROM reads", []).map_err(|e| e.to_string())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,17 +2,20 @@
|
||||||
//! the webview: account files are picked in native dialogs and read here, and credentials go from the keychain
|
//! the webview: account files are picked in native dialogs and read here, and credentials go from the keychain
|
||||||
//! straight into the sidecar call.
|
//! straight into the sidecar call.
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json::{json, Map, Value};
|
use serde_json::{json, Map, Value};
|
||||||
use tauri::{AppHandle, Manager, State, WebviewWindow};
|
use tauri::{AppHandle, Emitter, Manager, State, WebviewWindow};
|
||||||
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};
|
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};
|
||||||
use tauri_plugin_opener::OpenerExt;
|
use tauri_plugin_opener::OpenerExt;
|
||||||
|
|
||||||
use crate::accounts::{self, Account, AccountKind, Secret, SecretStore};
|
use crate::accounts::{self, Account, AccountKind, Secret, SecretStore};
|
||||||
|
use crate::cache::{Cache, Entry};
|
||||||
|
use crate::downloads;
|
||||||
use crate::policy::{self, Kind};
|
use crate::policy::{self, Kind};
|
||||||
use crate::sidecar::{Fail, Sidecar};
|
use crate::sidecar::{valid_job, Fail, Progress, Sidecar};
|
||||||
use crate::store::{self, Project, State as Saved};
|
use crate::store::{self, Project, State as Saved};
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
|
|
@ -20,6 +23,8 @@ pub struct AppState {
|
||||||
pub data_dir: PathBuf,
|
pub data_dir: PathBuf,
|
||||||
pub saved: Mutex<Saved>,
|
pub saved: Mutex<Saved>,
|
||||||
pub secrets: Box<dyn SecretStore>,
|
pub secrets: Box<dyn SecretStore>,
|
||||||
|
pub cache: Cache,
|
||||||
|
pub downloads: downloads::Jobs,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Res<T> = Result<T, Fail>;
|
type Res<T> = Result<T, Fail>;
|
||||||
|
|
@ -37,6 +42,13 @@ fn persist(s: &AppState, st: &Saved) -> Res<()> {
|
||||||
store::save(&s.data_dir, st).map_err(Fail::host)
|
store::save(&s.data_dir, st).map_err(Fail::host)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn job_of(job: Option<String>) -> Res<Option<String>> {
|
||||||
|
match job {
|
||||||
|
Some(j) if !valid_job(&j) => Err(Fail::host("bad job id")),
|
||||||
|
j => Ok(j),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Project folder + credentials of its mapped accounts, as sidecar params.
|
/// Project folder + credentials of its mapped accounts, as sidecar params.
|
||||||
fn project_params(s: &AppState, project_id: &str) -> Res<(Project, Map<String, Value>)> {
|
fn project_params(s: &AppState, project_id: &str) -> Res<(Project, Map<String, Value>)> {
|
||||||
let p = saved(s)?.projects.iter().find(|p| p.id == project_id).cloned().ok_or_else(|| Fail::host("unknown project"))?;
|
let p = saved(s)?.projects.iter().find(|p| p.id == project_id).cloned().ok_or_else(|| Fail::host("unknown project"))?;
|
||||||
|
|
@ -81,12 +93,16 @@ pub async fn sidecar_status(window: WebviewWindow, app: AppHandle, state: State<
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A read (or dry-run write) through the sidecar. Writes from here are always dry runs (policy::sanitize_js_call).
|
/// A read (or dry-run write) through the sidecar. Writes from here are always dry runs (policy::sanitize_js_call).
|
||||||
|
/// Successful reads of a project are kept in the offline cache (results only, never the credentials).
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn sidecar_call(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, method: String, project_id: Option<String>, params: Option<Value>) -> Res<Value> {
|
pub async fn sidecar_call(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, method: String, project_id: Option<String>, params: Option<Value>, job: Option<String>) -> Res<Value> {
|
||||||
guard(&window)?;
|
guard(&window)?;
|
||||||
|
let job = job_of(job)?;
|
||||||
let r = state.sidecar.get(&app).await?;
|
let r = state.sidecar.get(&app).await?;
|
||||||
let kind = r.kind(&method);
|
let kind = r.kind(&method);
|
||||||
let mut p = policy::sanitize_js_call(&method, kind, ¶ms.unwrap_or(Value::Null)).map_err(Fail::host)?;
|
let mut p = policy::sanitize_js_call(&method, kind, ¶ms.unwrap_or(Value::Null)).map_err(Fail::host)?;
|
||||||
|
let args = p.get("args").cloned().unwrap_or_else(|| json!({}));
|
||||||
|
let mut cache_project = None;
|
||||||
if matches!(kind, Some(Kind::Read | Kind::Write)) {
|
if matches!(kind, Some(Kind::Read | Kind::Write)) {
|
||||||
let id = project_id.ok_or_else(|| Fail::host(format!("{method} needs a project")))?;
|
let id = project_id.ok_or_else(|| Fail::host(format!("{method} needs a project")))?;
|
||||||
let (proj, creds) = project_params(&state, &id)?;
|
let (proj, creds) = project_params(&state, &id)?;
|
||||||
|
|
@ -95,8 +111,46 @@ pub async fn sidecar_call(window: WebviewWindow, app: AppHandle, state: State<'_
|
||||||
if !creds.is_empty() && method != "project.inspect" {
|
if !creds.is_empty() && method != "project.inspect" {
|
||||||
o.insert("credentials".into(), Value::Object(creds));
|
o.insert("credentials".into(), Value::Object(creds));
|
||||||
}
|
}
|
||||||
|
if kind == Some(Kind::Read) {
|
||||||
|
cache_project = Some(id);
|
||||||
}
|
}
|
||||||
r.call(&method, p).await
|
}
|
||||||
|
let res = r.call_job(&method, p, job.as_deref()).await?;
|
||||||
|
if let Some(id) = cache_project {
|
||||||
|
// only results that read something: a failed store read must not hide the last good one
|
||||||
|
if res["ok"] == Value::Bool(true) && !res["data"].is_null() {
|
||||||
|
let _ = state.cache.put(&id, &method, &args, &res, &store::now_iso());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The last cached result of a read (`project_id` "*" for the portfolio), with its time; null when never read.
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn cache_get(window: WebviewWindow, state: State<'_, AppState>, method: String, project_id: String, args: Option<Value>) -> Res<Option<Entry>> {
|
||||||
|
guard(&window)?;
|
||||||
|
state.cache.get(&project_id, &method, &args.unwrap_or_else(|| json!({}))).map_err(Fail::host)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn cache_clear(window: WebviewWindow, state: State<'_, AppState>) -> Res<()> {
|
||||||
|
guard(&window)?;
|
||||||
|
state.cache.clear().map_err(Fail::host)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stops a running job: its sidecar requests (`$/cancelRequest`) and a running download.
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn job_cancel(window: WebviewWindow, state: State<'_, AppState>, job: String) -> Res<bool> {
|
||||||
|
guard(&window)?;
|
||||||
|
if !valid_job(&job) {
|
||||||
|
return Err(Fail::host("bad job id"));
|
||||||
|
}
|
||||||
|
let mut hit = state.downloads.cancel(&job);
|
||||||
|
if let Some(r) = state.sidecar.current().await {
|
||||||
|
hit |= r.cancel_job(&job) > 0;
|
||||||
|
hit |= r.cancel_job(&format!("{job}:p")) > 0;
|
||||||
|
}
|
||||||
|
Ok(hit)
|
||||||
}
|
}
|
||||||
|
|
||||||
const DIALOG_LINES: usize = 28;
|
const DIALOG_LINES: usize = 28;
|
||||||
|
|
@ -135,14 +189,32 @@ pub fn dialog_summary(title: &str, description: &str, project: &str, network: bo
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Native Apply / Cancel dialog; true on Apply.
|
||||||
|
async fn confirm_dialog(window: &WebviewWindow, title: String, text: String) -> Res<bool> {
|
||||||
|
let w = window.clone();
|
||||||
|
tauri::async_runtime::spawn_blocking(move || {
|
||||||
|
w.app_handle()
|
||||||
|
.dialog()
|
||||||
|
.message(text)
|
||||||
|
.title(title)
|
||||||
|
.kind(MessageDialogKind::Warning)
|
||||||
|
.buttons(MessageDialogButtons::OkCancelCustom("Apply".into(), "Cancel".into()))
|
||||||
|
.parent(&w)
|
||||||
|
.blocking_show()
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(|e| Fail::host(e.to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
/// Dry run, then a native dialog with the summary and the dry run, then (only on Apply) the real run with a confirm
|
/// Dry run, then a native dialog with the summary and the dry run, then (only on Apply) the real run with a confirm
|
||||||
/// token minted here for exactly this method, project and arguments.
|
/// token minted here for exactly this method, project and arguments.
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, method: String, project_id: String, args: Option<Value>) -> Res<Value> {
|
pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, method: String, project_id: String, args: Option<Value>, job: Option<String>) -> Res<Value> {
|
||||||
guard(&window)?;
|
guard(&window)?;
|
||||||
|
let job = job_of(job)?;
|
||||||
let r = state.sidecar.get(&app).await?;
|
let r = state.sidecar.get(&app).await?;
|
||||||
if r.kind(&method) != Some(Kind::Write) {
|
if r.kind(&method) != Some(Kind::Write) || method == "project.init" {
|
||||||
return Err(Fail::host(format!("{method} is not a write method")));
|
return Err(Fail::host(format!("{method} is not a write method of an app folder")));
|
||||||
}
|
}
|
||||||
let d = r.descriptor(&method).cloned().ok_or_else(|| Fail::host("unknown method"))?;
|
let d = r.descriptor(&method).cloned().ok_or_else(|| Fail::host("unknown method"))?;
|
||||||
let args = match args.unwrap_or(json!({})) {
|
let args = match args.unwrap_or(json!({})) {
|
||||||
|
|
@ -154,7 +226,7 @@ pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: St
|
||||||
if !creds.is_empty() {
|
if !creds.is_empty() {
|
||||||
dry_params["credentials"] = Value::Object(creds.clone());
|
dry_params["credentials"] = Value::Object(creds.clone());
|
||||||
}
|
}
|
||||||
let dry = r.call(&method, dry_params).await?;
|
let dry = r.call_job(&method, dry_params, job.as_deref()).await?;
|
||||||
if dry["ok"] != Value::Bool(true) {
|
if dry["ok"] != Value::Bool(true) {
|
||||||
return Ok(json!({ "confirmed": false, "stage": "dry-run", "dryRun": dry }));
|
return Ok(json!({ "confirmed": false, "stage": "dry-run", "dryRun": dry }));
|
||||||
}
|
}
|
||||||
|
|
@ -165,22 +237,7 @@ pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: St
|
||||||
dialog_excerpt(dry["output"].as_str().unwrap_or_default())
|
dialog_excerpt(dry["output"].as_str().unwrap_or_default())
|
||||||
);
|
);
|
||||||
let home = app.path().home_dir().ok().map(|h| h.to_string_lossy().to_string());
|
let home = app.path().home_dir().ok().map(|h| h.to_string_lossy().to_string());
|
||||||
let text = tilde(&text, home.as_deref());
|
if !confirm_dialog(&window, format!("Apply: {}", d.title), tilde(&text, home.as_deref())).await? {
|
||||||
let w = window.clone();
|
|
||||||
let title = format!("Apply: {}", d.title);
|
|
||||||
let yes = tauri::async_runtime::spawn_blocking(move || {
|
|
||||||
w.app_handle()
|
|
||||||
.dialog()
|
|
||||||
.message(text)
|
|
||||||
.title(title)
|
|
||||||
.kind(MessageDialogKind::Warning)
|
|
||||||
.buttons(MessageDialogButtons::OkCancelCustom("Apply".into(), "Cancel".into()))
|
|
||||||
.parent(&w)
|
|
||||||
.blocking_show()
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
.map_err(|e| Fail::host(e.to_string()))?;
|
|
||||||
if !yes {
|
|
||||||
return Ok(json!({ "confirmed": false, "stage": "dialog", "dryRun": dry }));
|
return Ok(json!({ "confirmed": false, "stage": "dialog", "dryRun": dry }));
|
||||||
}
|
}
|
||||||
let bound = json!({ "project": proj.path, "args": args });
|
let bound = json!({ "project": proj.path, "args": args });
|
||||||
|
|
@ -189,7 +246,7 @@ pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: St
|
||||||
if !creds.is_empty() {
|
if !creds.is_empty() {
|
||||||
real["credentials"] = Value::Object(creds);
|
real["credentials"] = Value::Object(creds);
|
||||||
}
|
}
|
||||||
let result = r.call(&method, real).await?;
|
let result = r.call_job(&method, real, job.as_deref()).await?;
|
||||||
Ok(json!({ "confirmed": true, "stage": "done", "dryRun": dry, "result": result }))
|
Ok(json!({ "confirmed": true, "stage": "done", "dryRun": dry, "result": result }))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -205,25 +262,23 @@ pub fn has_config(dir: &Path) -> bool {
|
||||||
CONFIG_NAMES.iter().any(|n| dir.join(n).is_file())
|
CONFIG_NAMES.iter().any(|n| dir.join(n).is_file())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Native folder picker; the folder must contain storebound.config.ts and load (project.inspect). None = cancelled.
|
async fn pick_folder(window: &WebviewWindow, title: &'static str) -> Res<Option<PathBuf>> {
|
||||||
#[tauri::command]
|
|
||||||
pub async fn project_add(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>) -> Res<Option<Project>> {
|
|
||||||
guard(&window)?;
|
|
||||||
let w = window.clone();
|
let w = window.clone();
|
||||||
let picked = tauri::async_runtime::spawn_blocking(move || w.app_handle().dialog().file().set_title("Add a game folder (with storebound.config.ts)").set_parent(&w).blocking_pick_folder())
|
let picked = tauri::async_runtime::spawn_blocking(move || w.app_handle().dialog().file().set_title(title).set_parent(&w).blocking_pick_folder())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Fail::host(e.to_string()))?;
|
.map_err(|e| Fail::host(e.to_string()))?;
|
||||||
let Some(picked) = picked else { return Ok(None) };
|
let Some(picked) = picked else { return Ok(None) };
|
||||||
let dir = picked.into_path().map_err(|e| Fail::host(e.to_string()))?;
|
let dir = picked.into_path().map_err(|e| Fail::host(e.to_string()))?;
|
||||||
let dir = std::fs::canonicalize(&dir).unwrap_or(dir);
|
Ok(Some(std::fs::canonicalize(&dir).unwrap_or(dir)))
|
||||||
if !has_config(&dir) {
|
|
||||||
return Err(Fail::host(format!("no storebound.config.ts in {}", dir.display())));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a folder that has a config (inspected by the sidecar) to the list.
|
||||||
|
async fn add_folder(app: &AppHandle, state: &AppState, dir: &Path) -> Res<Project> {
|
||||||
let path = dir.to_string_lossy().to_string();
|
let path = dir.to_string_lossy().to_string();
|
||||||
if saved(&state)?.projects.iter().any(|p| p.path == path) {
|
if saved(state)?.projects.iter().any(|p| p.path == path) {
|
||||||
return Err(Fail::host("this folder is already in the list"));
|
return Err(Fail::host("this folder is already in the list"));
|
||||||
}
|
}
|
||||||
let r = state.sidecar.get(&app).await?;
|
let r = state.sidecar.get(app).await?;
|
||||||
let summary = r.call("project.inspect", json!({ "project": path })).await?;
|
let summary = r.call("project.inspect", json!({ "project": path })).await?;
|
||||||
let project = Project {
|
let project = Project {
|
||||||
id: store::new_id()?,
|
id: store::new_id()?,
|
||||||
|
|
@ -233,10 +288,58 @@ pub async fn project_add(window: WebviewWindow, app: AppHandle, state: State<'_,
|
||||||
apple_account: None,
|
apple_account: None,
|
||||||
added: store::now_iso(),
|
added: store::now_iso(),
|
||||||
};
|
};
|
||||||
let mut st = saved(&state)?;
|
let mut st = saved(state)?;
|
||||||
st.projects.push(project.clone());
|
st.projects.push(project.clone());
|
||||||
persist(&state, &st)?;
|
persist(state, &st)?;
|
||||||
Ok(Some(project))
|
Ok(project)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Native folder picker; the folder must contain storebound.config.ts and load (project.inspect). None = cancelled.
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn project_add(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>) -> Res<Option<Project>> {
|
||||||
|
guard(&window)?;
|
||||||
|
let Some(dir) = pick_folder(&window, "Add an app folder (with storebound.config.ts)").await? else { return Ok(None) };
|
||||||
|
if !has_config(&dir) {
|
||||||
|
return Err(Fail::host(format!("no storebound.config.ts in {}", dir.display())));
|
||||||
|
}
|
||||||
|
add_folder(&app, &state, &dir).await.map(Some)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const PROFILES: [&str; 3] = ["app", "game", "subscription-app"];
|
||||||
|
|
||||||
|
/// `storebound init` for a new app folder: native folder picker, the dry run in a native dialog, then the templates
|
||||||
|
/// for `profile` are written (existing files kept) and the folder is added. None = cancelled at any step.
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn project_init(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, profile: String) -> Res<Option<Project>> {
|
||||||
|
guard(&window)?;
|
||||||
|
if !PROFILES.contains(&profile.as_str()) {
|
||||||
|
return Err(Fail::host("profile: app, game or subscription-app"));
|
||||||
|
}
|
||||||
|
let Some(dir) = pick_folder(&window, "Choose the app's folder (storebound files go into it)").await? else { return Ok(None) };
|
||||||
|
if has_config(&dir) {
|
||||||
|
return Err(Fail::host("this folder already has a storebound config: add it instead"));
|
||||||
|
}
|
||||||
|
let path = dir.to_string_lossy().to_string();
|
||||||
|
let args = json!({ "profile": profile });
|
||||||
|
let r = state.sidecar.get(&app).await?;
|
||||||
|
let dry = r.call("project.init", json!({ "project": path, "args": args, "dryRun": true })).await?;
|
||||||
|
if dry["ok"] != Value::Bool(true) {
|
||||||
|
return Err(Fail::host(format!("init dry run failed: {}", dialog_excerpt(dry["output"].as_str().unwrap_or_default()))));
|
||||||
|
}
|
||||||
|
let home = app.path().home_dir().ok().map(|h| h.to_string_lossy().to_string());
|
||||||
|
let text = format!(
|
||||||
|
"Start an app folder · {profile} profile\n\n{path}\n\nCopies the storebound templates into this folder; existing files are kept. Every answer in them is a starting point to edit.\n\n{}",
|
||||||
|
dialog_excerpt(dry["output"].as_str().unwrap_or_default())
|
||||||
|
);
|
||||||
|
if !confirm_dialog(&window, "Apply: Start an app folder".into(), tilde(&text, home.as_deref())).await? {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
let token = r.mint("project.init", &json!({ "project": path, "args": args })).map_err(Fail::host)?;
|
||||||
|
let res = r.call("project.init", json!({ "project": path, "args": args, "dryRun": false, "confirmToken": token })).await?;
|
||||||
|
if res["ok"] != Value::Bool(true) {
|
||||||
|
return Err(Fail::host(format!("init failed: {}", dialog_excerpt(res["output"].as_str().unwrap_or_default()))));
|
||||||
|
}
|
||||||
|
add_folder(&app, &state, &dir).await.map(Some)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
|
|
@ -244,6 +347,7 @@ pub fn project_remove(window: WebviewWindow, state: State<'_, AppState>, id: Str
|
||||||
guard(&window)?;
|
guard(&window)?;
|
||||||
let mut st = saved(&state)?;
|
let mut st = saved(&state)?;
|
||||||
st.projects.retain(|p| p.id != id);
|
st.projects.retain(|p| p.id != id);
|
||||||
|
let _ = state.cache.forget_project(&id);
|
||||||
persist(&state, &st)
|
persist(&state, &st)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -342,10 +446,16 @@ pub fn account_remove(window: WebviewWindow, state: State<'_, AppState>, id: Str
|
||||||
persist(&state, &st)
|
persist(&state, &st)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `portfolio.status` over every project. Offline unless `live`; live runs each project with its own accounts.
|
/// Store reads run at most this many apps at once (each is two status reads).
|
||||||
|
const LIVE_PARALLEL: usize = 4;
|
||||||
|
|
||||||
|
/// `portfolio.status` over every project. Offline unless `live`; live reads every app with its own accounts, several
|
||||||
|
/// at once, and sends one progress line per finished app (job progress n / total). Each live app result is cached;
|
||||||
|
/// the whole live result too (project "*"), so the portfolio shows the last read when offline.
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn portfolio_status(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, live: Option<bool>) -> Res<Value> {
|
pub async fn portfolio_status(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, live: Option<bool>, job: Option<String>) -> Res<Value> {
|
||||||
guard(&window)?;
|
guard(&window)?;
|
||||||
|
let job = job_of(job)?;
|
||||||
let projects = saved(&state)?.projects.clone();
|
let projects = saved(&state)?.projects.clone();
|
||||||
if projects.is_empty() {
|
if projects.is_empty() {
|
||||||
return Ok(json!({ "apps": [], "at": store::now_iso() }));
|
return Ok(json!({ "apps": [], "at": store::now_iso() }));
|
||||||
|
|
@ -353,15 +463,37 @@ pub async fn portfolio_status(window: WebviewWindow, app: AppHandle, state: Stat
|
||||||
let r = state.sidecar.get(&app).await?;
|
let r = state.sidecar.get(&app).await?;
|
||||||
if live != Some(true) {
|
if live != Some(true) {
|
||||||
let paths: Vec<&str> = projects.iter().map(|p| p.path.as_str()).collect();
|
let paths: Vec<&str> = projects.iter().map(|p| p.path.as_str()).collect();
|
||||||
return r.call("portfolio.status", json!({ "projects": paths })).await;
|
return r.call_job("portfolio.status", json!({ "projects": paths }), job.as_deref()).await;
|
||||||
}
|
}
|
||||||
let mut all = Vec::new();
|
let total = projects.len();
|
||||||
|
let sem = Arc::new(tokio::sync::Semaphore::new(LIVE_PARALLEL));
|
||||||
|
let done = Arc::new(std::sync::atomic::AtomicUsize::new(0));
|
||||||
|
let mut handles = Vec::new();
|
||||||
for p in &projects {
|
for p in &projects {
|
||||||
let (_, creds) = project_params(&state, &p.id)?;
|
let (_, creds) = project_params(&state, &p.id)?;
|
||||||
let res = r.call("portfolio.status", json!({ "projects": [p.path], "live": true, "credentials": creds })).await?;
|
let (r, sem, done, app, job, path, id) = (r.clone(), sem.clone(), done.clone(), app.clone(), job.clone(), p.path.clone(), p.id.clone());
|
||||||
all.extend(res["apps"].as_array().cloned().unwrap_or_default());
|
handles.push(tauri::async_runtime::spawn(async move {
|
||||||
|
let _permit = sem.acquire_owned().await.ok();
|
||||||
|
let sub = job.as_ref().map(|j| format!("{j}:p"));
|
||||||
|
let res = r.call_job("portfolio.status", json!({ "projects": [path], "live": true, "credentials": creds }), sub.as_deref()).await;
|
||||||
|
let n = done.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1;
|
||||||
|
let _ = app.emit_to(policy::MAIN, "sidecar-progress", Progress { params: json!({ "requestId": 0, "n": n, "total": total, "project": path }), job: job.clone() });
|
||||||
|
(id, res)
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
Ok(json!({ "apps": all, "at": store::now_iso() }))
|
let mut all = Vec::new();
|
||||||
|
let at = store::now_iso();
|
||||||
|
for h in handles {
|
||||||
|
let (id, res) = h.await.map_err(|e| Fail::host(e.to_string()))?;
|
||||||
|
let res = res?;
|
||||||
|
for a in res["apps"].as_array().cloned().unwrap_or_default() {
|
||||||
|
let _ = state.cache.put(&id, "portfolio.app", &json!({}), &a, &at);
|
||||||
|
all.push(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let out = json!({ "apps": all, "at": at });
|
||||||
|
let _ = state.cache.put("*", "portfolio.status", &json!({ "live": true }), &out, &at);
|
||||||
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
|
|
@ -373,3 +505,39 @@ pub fn open_external(window: WebviewWindow, app: AppHandle, url: String) -> Res<
|
||||||
}
|
}
|
||||||
app.opener().open_url(u.as_str(), None::<&str>).map_err(|e| Fail::host(e.to_string()))
|
app.opener().open_url(u.as_str(), None::<&str>).map_err(|e| Fail::host(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// What the Dependencies screen can download on this platform, and what is installed.
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn tools_info(window: WebviewWindow, state: State<'_, AppState>) -> Res<Vec<downloads::DownloadInfo>> {
|
||||||
|
guard(&window)?;
|
||||||
|
downloads::info(&state.data_dir).map_err(Fail::host)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Downloads, verifies and installs ffmpeg or Chrome for Testing into the app data folder (progress: `tool-download`
|
||||||
|
/// events with the job id; job_cancel stops it). The sidecar restarts afterwards so it sees the new tool.
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn tools_install(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, tool: String, job: String) -> Res<String> {
|
||||||
|
guard(&window)?;
|
||||||
|
if !valid_job(&job) || !matches!(tool.as_str(), "ffmpeg" | "chrome") {
|
||||||
|
return Err(Fail::host("bad tool or job"));
|
||||||
|
}
|
||||||
|
let flag = state.downloads.start(&job).map_err(Fail::host)?;
|
||||||
|
let handle = app.clone();
|
||||||
|
let res = downloads::install(&state.data_dir, &tool, &job, flag, move |e| {
|
||||||
|
let _ = handle.emit_to(policy::MAIN, "tool-download", e);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
state.downloads.finish(&job);
|
||||||
|
if res.is_ok() {
|
||||||
|
state.sidecar.shutdown().await;
|
||||||
|
}
|
||||||
|
res.map_err(Fail::host)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn tools_uninstall(window: WebviewWindow, state: State<'_, AppState>, tool: String) -> Res<()> {
|
||||||
|
guard(&window)?;
|
||||||
|
downloads::uninstall(&state.data_dir, &tool).map_err(Fail::host)?;
|
||||||
|
state.sidecar.shutdown().await;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,13 @@ pub const COMMANDS: &[&str] = &[
|
||||||
"app_info",
|
"app_info",
|
||||||
"sidecar_status",
|
"sidecar_status",
|
||||||
"sidecar_call",
|
"sidecar_call",
|
||||||
|
"cache_get",
|
||||||
|
"cache_clear",
|
||||||
|
"job_cancel",
|
||||||
"write_with_confirm",
|
"write_with_confirm",
|
||||||
"projects_list",
|
"projects_list",
|
||||||
"project_add",
|
"project_add",
|
||||||
|
"project_init",
|
||||||
"project_remove",
|
"project_remove",
|
||||||
"project_set_accounts",
|
"project_set_accounts",
|
||||||
"accounts_list",
|
"accounts_list",
|
||||||
|
|
@ -15,4 +19,7 @@ pub const COMMANDS: &[&str] = &[
|
||||||
"account_remove",
|
"account_remove",
|
||||||
"portfolio_status",
|
"portfolio_status",
|
||||||
"open_external",
|
"open_external",
|
||||||
|
"tools_info",
|
||||||
|
"tools_install",
|
||||||
|
"tools_uninstall",
|
||||||
];
|
];
|
||||||
|
|
|
||||||
454
apps/desktop/src-tauri/src/downloads.rs
Normal file
454
apps/desktop/src-tauri/src/downloads.rs
Normal file
|
|
@ -0,0 +1,454 @@
|
||||||
|
//! One-click downloads of the optional local tools (Dependencies screen) into the app data folder, never system-wide
|
||||||
|
//! and never bundled with the installer (docs/desktop.md):
|
||||||
|
//!
|
||||||
|
//! - **ffmpeg + ffprobe**: the exact archives of `tools-manifest.json` (URL, SHA-256, size; written by
|
||||||
|
//! tools/tools-manifest.ts per desktop release). The SHA-256 is checked while downloading; a mismatch deletes the file.
|
||||||
|
//! - **Chrome for Testing**: Google's official JSON endpoint (Stable channel), archive from storage.googleapis.com. No
|
||||||
|
//! hashes are published, so the unpacked browser must carry Google's code signature (macOS team EQHXZ8M8AV,
|
||||||
|
//! Windows Authenticode by Google LLC); on Linux the version banner is checked.
|
||||||
|
//!
|
||||||
|
//! Everything happens in a staging folder; only a verified result replaces the installed copy. Progress goes to the
|
||||||
|
//! window as `tool-download` events with the job id; cancel removes the staging folder. Redirects may only lead to
|
||||||
|
//! the hosts in DOWNLOAD_HOSTS, over HTTPS.
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fs;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
pub const MANIFEST_JSON: &str = include_str!("../tools-manifest.json");
|
||||||
|
|
||||||
|
/// Hosts a download (or one of its redirects) may touch.
|
||||||
|
pub const DOWNLOAD_HOSTS: &[&str] = &[
|
||||||
|
"ffmpeg.martin-riedl.de",
|
||||||
|
"github.com",
|
||||||
|
"objects.githubusercontent.com",
|
||||||
|
"release-assets.githubusercontent.com",
|
||||||
|
"googlechromelabs.github.io",
|
||||||
|
"storage.googleapis.com",
|
||||||
|
];
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct FileMap {
|
||||||
|
pub from: String,
|
||||||
|
pub to: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct Archive {
|
||||||
|
pub url: String,
|
||||||
|
pub sha256: String,
|
||||||
|
pub size: u64,
|
||||||
|
pub kind: String,
|
||||||
|
pub files: Vec<FileMap>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct Ffmpeg {
|
||||||
|
pub version: String,
|
||||||
|
pub license: String,
|
||||||
|
pub license_url: String,
|
||||||
|
pub sources: HashMap<String, String>,
|
||||||
|
pub targets: HashMap<String, Vec<Archive>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct Chrome {
|
||||||
|
pub endpoint: String,
|
||||||
|
pub channel: String,
|
||||||
|
pub platforms: HashMap<String, String>,
|
||||||
|
pub mac_team_id: String,
|
||||||
|
pub windows_signer: String,
|
||||||
|
pub license: String,
|
||||||
|
pub license_url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct Manifest {
|
||||||
|
pub generated: String,
|
||||||
|
pub ffmpeg: Ffmpeg,
|
||||||
|
pub chrome: Chrome,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn manifest() -> Result<Manifest, String> {
|
||||||
|
serde_json::from_str(MANIFEST_JSON).map_err(|e| format!("tools manifest: {e}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rust target triple of this build (the manifest's keys).
|
||||||
|
pub fn target_triple() -> &'static str {
|
||||||
|
if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
|
||||||
|
"aarch64-apple-darwin"
|
||||||
|
} else if cfg!(target_os = "macos") {
|
||||||
|
"x86_64-apple-darwin"
|
||||||
|
} else if cfg!(all(target_os = "linux", target_arch = "aarch64")) {
|
||||||
|
"aarch64-unknown-linux-gnu"
|
||||||
|
} else if cfg!(target_os = "linux") {
|
||||||
|
"x86_64-unknown-linux-gnu"
|
||||||
|
} else {
|
||||||
|
"x86_64-pc-windows-msvc"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn download_allowed(u: &Url) -> bool {
|
||||||
|
u.scheme() == "https" && u.username().is_empty() && u.password().is_none() && u.port().is_none() && u.host_str().is_some_and(|h| DOWNLOAD_HOSTS.contains(&h))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hex(b: &[u8]) -> String {
|
||||||
|
crate::confirm::hex(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What the Dependencies screen shows next to the Download buttons.
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct DownloadInfo {
|
||||||
|
pub tool: &'static str,
|
||||||
|
pub available: bool,
|
||||||
|
pub version: Option<String>,
|
||||||
|
pub size: Option<u64>,
|
||||||
|
pub license: String,
|
||||||
|
pub license_url: String,
|
||||||
|
pub source: String,
|
||||||
|
pub installed: bool,
|
||||||
|
pub folder: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn info(data_dir: &Path) -> Result<Vec<DownloadInfo>, String> {
|
||||||
|
let m = manifest()?;
|
||||||
|
let t = target_triple();
|
||||||
|
let ff = m.ffmpeg.targets.get(t);
|
||||||
|
let os = if t.contains("apple") { "macos" } else if t.contains("windows") { "windows" } else { "linux" };
|
||||||
|
let exe = if cfg!(windows) { ".exe" } else { "" };
|
||||||
|
Ok(vec![
|
||||||
|
DownloadInfo {
|
||||||
|
tool: "ffmpeg",
|
||||||
|
available: ff.is_some(),
|
||||||
|
version: Some(m.ffmpeg.version.clone()),
|
||||||
|
size: ff.map(|a| a.iter().map(|x| x.size).sum()),
|
||||||
|
license: m.ffmpeg.license.clone(),
|
||||||
|
license_url: m.ffmpeg.license_url.clone(),
|
||||||
|
source: m.ffmpeg.sources.get(os).cloned().unwrap_or_default(),
|
||||||
|
installed: data_dir.join("tools").join("bin").join(format!("ffmpeg{exe}")).is_file(),
|
||||||
|
folder: "tools/bin".into(),
|
||||||
|
},
|
||||||
|
DownloadInfo {
|
||||||
|
tool: "chrome",
|
||||||
|
available: m.chrome.platforms.contains_key(t),
|
||||||
|
version: None,
|
||||||
|
size: None,
|
||||||
|
license: m.chrome.license.clone(),
|
||||||
|
license_url: m.chrome.license_url.clone(),
|
||||||
|
source: "https://googlechromelabs.github.io/chrome-for-testing/".into(),
|
||||||
|
installed: data_dir.join("tools").join("chrome").join(crate::policy::chrome_executable()).is_file(),
|
||||||
|
folder: "tools/chrome".into(),
|
||||||
|
},
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Progress of a download job, sent to the window as `tool-download`.
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub struct Event {
|
||||||
|
pub job: String,
|
||||||
|
pub tool: String,
|
||||||
|
/// resolve, download, verify, unpack, install, done, error, cancelled
|
||||||
|
pub stage: &'static str,
|
||||||
|
pub received: u64,
|
||||||
|
pub total: Option<u64>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub message: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Running download jobs and their cancel flags.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Jobs {
|
||||||
|
flags: Mutex<HashMap<String, Arc<AtomicBool>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Jobs {
|
||||||
|
pub fn start(&self, job: &str) -> Result<Arc<AtomicBool>, String> {
|
||||||
|
let mut m = self.flags.lock().map_err(|_| "jobs poisoned")?;
|
||||||
|
if m.contains_key(job) {
|
||||||
|
return Err("this download is already running".into());
|
||||||
|
}
|
||||||
|
let f = Arc::new(AtomicBool::new(false));
|
||||||
|
m.insert(job.to_string(), f.clone());
|
||||||
|
Ok(f)
|
||||||
|
}
|
||||||
|
pub fn finish(&self, job: &str) {
|
||||||
|
if let Ok(mut m) = self.flags.lock() {
|
||||||
|
m.remove(job);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn cancel(&self, job: &str) -> bool {
|
||||||
|
self.flags.lock().ok().and_then(|m| m.get(job).map(|f| f.store(true, Ordering::SeqCst))).is_some()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The Chrome for Testing download of a platform from the endpoint JSON: (version, url).
|
||||||
|
pub fn chrome_download(endpoint_json: &serde_json::Value, channel: &str, platform: &str) -> Result<(String, Url), String> {
|
||||||
|
let ch = &endpoint_json["channels"][channel];
|
||||||
|
let version = ch["version"].as_str().ok_or("Chrome for Testing: no version in the endpoint answer")?.to_string();
|
||||||
|
let url = ch["downloads"]["chrome"]
|
||||||
|
.as_array()
|
||||||
|
.and_then(|a| a.iter().find(|d| d["platform"].as_str() == Some(platform)))
|
||||||
|
.and_then(|d| d["url"].as_str())
|
||||||
|
.ok_or_else(|| format!("Chrome for Testing: no download for {platform}"))?;
|
||||||
|
let u = Url::parse(url).map_err(|e| e.to_string())?;
|
||||||
|
if !download_allowed(&u) {
|
||||||
|
return Err(format!("Chrome for Testing: unexpected download host {}", u.host_str().unwrap_or("?")));
|
||||||
|
}
|
||||||
|
Ok((version, u))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn client() -> Result<reqwest::Client, String> {
|
||||||
|
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||||
|
reqwest::Client::builder()
|
||||||
|
.user_agent(concat!("storebound-desktop/", env!("CARGO_PKG_VERSION")))
|
||||||
|
.redirect(reqwest::redirect::Policy::custom(|a| {
|
||||||
|
if a.previous().len() > 8 {
|
||||||
|
a.error("too many redirects")
|
||||||
|
} else if download_allowed(a.url()) {
|
||||||
|
a.follow()
|
||||||
|
} else {
|
||||||
|
a.error("redirect to a host outside the download list")
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.connect_timeout(std::time::Duration::from_secs(20))
|
||||||
|
.build()
|
||||||
|
.map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Streams `url` into `dest`, returning its SHA-256 (hex). `progress(received_in_this_file)` after each chunk.
|
||||||
|
async fn fetch(c: &reqwest::Client, url: &Url, dest: &Path, cancel: &AtomicBool, mut progress: impl FnMut(u64)) -> Result<String, String> {
|
||||||
|
if !download_allowed(url) {
|
||||||
|
return Err(format!("not an allowed download host: {}", url.host_str().unwrap_or("?")));
|
||||||
|
}
|
||||||
|
let mut resp = c.get(url.clone()).send().await.map_err(|e| format!("download: {e}"))?;
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("download: {} answered {}", url.host_str().unwrap_or("?"), resp.status()));
|
||||||
|
}
|
||||||
|
let mut f = fs::File::create(dest).map_err(|e| e.to_string())?;
|
||||||
|
let mut h = Sha256::new();
|
||||||
|
let mut got: u64 = 0;
|
||||||
|
while let Some(chunk) = resp.chunk().await.map_err(|e| format!("download: {e}"))? {
|
||||||
|
if cancel.load(Ordering::SeqCst) {
|
||||||
|
return Err("cancelled".into());
|
||||||
|
}
|
||||||
|
h.update(&chunk);
|
||||||
|
f.write_all(&chunk).map_err(|e| e.to_string())?;
|
||||||
|
got += chunk.len() as u64;
|
||||||
|
progress(got);
|
||||||
|
}
|
||||||
|
f.flush().map_err(|e| e.to_string())?;
|
||||||
|
Ok(hex(&h.finalize()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unpacks an archive into `dir`. macOS: ditto (keeps the app bundles' symlinks and signatures intact); tar.xz: the
|
||||||
|
/// system tar; other zips: the zip crate, which refuses entries outside `dir`.
|
||||||
|
pub fn unpack(archive: &Path, kind: &str, dir: &Path) -> Result<(), String> {
|
||||||
|
fs::create_dir_all(dir).map_err(|e| e.to_string())?;
|
||||||
|
let run = |cmd: &mut std::process::Command| -> Result<(), String> {
|
||||||
|
let out = cmd.output().map_err(|e| e.to_string())?;
|
||||||
|
if out.status.success() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(String::from_utf8_lossy(&out.stderr).trim().chars().take(300).collect())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match kind {
|
||||||
|
"tar.xz" => run(std::process::Command::new("tar").arg("-xJf").arg(archive).arg("-C").arg(dir)),
|
||||||
|
"zip" if cfg!(target_os = "macos") => run(std::process::Command::new("/usr/bin/ditto").arg("-x").arg("-k").arg(archive).arg(dir)),
|
||||||
|
"zip" => {
|
||||||
|
let f = fs::File::open(archive).map_err(|e| e.to_string())?;
|
||||||
|
let mut z = zip::ZipArchive::new(f).map_err(|e| e.to_string())?;
|
||||||
|
z.extract(dir).map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
k => Err(format!("unknown archive kind {k}")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `rel` inside `base`, refusing absolute paths and `..`.
|
||||||
|
pub fn inside(base: &Path, rel: &str) -> Result<PathBuf, String> {
|
||||||
|
let p = Path::new(rel);
|
||||||
|
if p.is_absolute() || p.components().any(|c| !matches!(c, std::path::Component::Normal(_))) {
|
||||||
|
return Err(format!("unsafe path in the manifest: {rel}"));
|
||||||
|
}
|
||||||
|
Ok(base.join(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn first_line(exe: &Path, arg: &str) -> Option<String> {
|
||||||
|
let out = std::process::Command::new(exe).arg(arg).output().ok()?;
|
||||||
|
let text = if out.stdout.is_empty() { out.stderr } else { out.stdout };
|
||||||
|
String::from_utf8_lossy(&text).lines().next().map(|s| s.trim().to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn make_executable(p: &Path) -> Result<(), String> {
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
fs::set_permissions(p, fs::Permissions::from_mode(0o755)).map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
fn make_executable(_: &Path) -> Result<(), String> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Google's code signature on the unpacked Chrome for Testing.
|
||||||
|
fn verify_chrome(root: &Path, exe: &Path, c: &Chrome) -> Result<(), String> {
|
||||||
|
if cfg!(target_os = "macos") {
|
||||||
|
let app = root.join("Google Chrome for Testing.app");
|
||||||
|
let v = std::process::Command::new("/usr/bin/codesign").args(["--verify", "--deep", "--strict"]).arg(&app).output().map_err(|e| e.to_string())?;
|
||||||
|
if !v.status.success() {
|
||||||
|
return Err(format!("code signature invalid: {}", String::from_utf8_lossy(&v.stderr).trim()));
|
||||||
|
}
|
||||||
|
let d = std::process::Command::new("/usr/bin/codesign").args(["-dv", "--verbose=2"]).arg(&app).output().map_err(|e| e.to_string())?;
|
||||||
|
let text = String::from_utf8_lossy(&d.stderr);
|
||||||
|
if !text.lines().any(|l| l.trim() == format!("TeamIdentifier={}", c.mac_team_id)) {
|
||||||
|
return Err("not signed by Google (team identifier differs)".into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
} else if cfg!(windows) {
|
||||||
|
let script = format!(
|
||||||
|
"$s = Get-AuthenticodeSignature -LiteralPath '{}'; if ($s.Status -eq 'Valid' -and $s.SignerCertificate.Subject -like '*O={}*') {{ exit 0 }} else {{ exit 1 }}",
|
||||||
|
exe.display().to_string().replace('\'', "''"),
|
||||||
|
c.windows_signer
|
||||||
|
);
|
||||||
|
let st = std::process::Command::new("powershell").args(["-NoProfile", "-NonInteractive", "-Command", &script]).status().map_err(|e| e.to_string())?;
|
||||||
|
if st.success() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(format!("Authenticode signature by {} not found", c.windows_signer))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match first_line(exe, "--version") {
|
||||||
|
Some(l) if l.starts_with("Google Chrome for Testing") => Ok(()),
|
||||||
|
other => Err(format!("unexpected version banner: {}", other.unwrap_or_default())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Installs `tool` ("ffmpeg" or "chrome") into `<data_dir>/tools`. `emit` gets every progress event.
|
||||||
|
pub async fn install(data_dir: &Path, tool: &str, job: &str, cancel: Arc<AtomicBool>, emit: impl Fn(Event) + Send + Sync) -> Result<String, String> {
|
||||||
|
let m = manifest()?;
|
||||||
|
let tools = data_dir.join("tools");
|
||||||
|
let staging = tools.join(format!(".staging-{job}"));
|
||||||
|
let _ = fs::remove_dir_all(&staging);
|
||||||
|
fs::create_dir_all(&staging).map_err(|e| e.to_string())?;
|
||||||
|
let ev = |stage: &'static str, received: u64, total: Option<u64>, message: Option<String>| emit(Event { job: job.into(), tool: tool.into(), stage, received, total, message });
|
||||||
|
let res = async {
|
||||||
|
let c = client()?;
|
||||||
|
match tool {
|
||||||
|
"ffmpeg" => {
|
||||||
|
let archives = m.ffmpeg.targets.get(target_triple()).ok_or("no ffmpeg build for this platform in the manifest")?;
|
||||||
|
let total: u64 = archives.iter().map(|a| a.size).sum();
|
||||||
|
let mut done: u64 = 0;
|
||||||
|
let bin = staging.join("bin");
|
||||||
|
fs::create_dir_all(&bin).map_err(|e| e.to_string())?;
|
||||||
|
for (i, a) in archives.iter().enumerate() {
|
||||||
|
let url = Url::parse(&a.url).map_err(|e| e.to_string())?;
|
||||||
|
let file = staging.join(format!("a{i}.{}", a.kind));
|
||||||
|
let sum = fetch(&c, &url, &file, &cancel, |n| ev("download", done + n, Some(total), None)).await?;
|
||||||
|
ev("verify", done + a.size, Some(total), None);
|
||||||
|
if !sum.eq_ignore_ascii_case(&a.sha256) {
|
||||||
|
let _ = fs::remove_file(&file);
|
||||||
|
return Err(format!("checksum mismatch for {} (deleted)", url.path()));
|
||||||
|
}
|
||||||
|
done += a.size;
|
||||||
|
ev("unpack", done, Some(total), None);
|
||||||
|
let out = staging.join(format!("u{i}"));
|
||||||
|
unpack(&file, &a.kind, &out)?;
|
||||||
|
for f in &a.files {
|
||||||
|
let src = inside(&out, &f.from)?;
|
||||||
|
let dst = inside(&bin, &f.to)?;
|
||||||
|
if !src.is_file() {
|
||||||
|
return Err(format!("{} missing in the archive", f.from));
|
||||||
|
}
|
||||||
|
fs::rename(&src, &dst).or_else(|_| fs::copy(&src, &dst).map(|_| ())).map_err(|e| e.to_string())?;
|
||||||
|
make_executable(&dst)?;
|
||||||
|
}
|
||||||
|
let _ = fs::remove_file(&file);
|
||||||
|
}
|
||||||
|
let exe = if cfg!(windows) { "ffmpeg.exe" } else { "ffmpeg" };
|
||||||
|
let banner = first_line(&bin.join(exe), "-version").unwrap_or_default();
|
||||||
|
if !banner.starts_with("ffmpeg version") {
|
||||||
|
return Err(format!("the downloaded ffmpeg does not run: {banner}"));
|
||||||
|
}
|
||||||
|
ev("install", total, Some(total), None);
|
||||||
|
let dest = tools.join("bin");
|
||||||
|
fs::create_dir_all(&dest).map_err(|e| e.to_string())?;
|
||||||
|
for e in fs::read_dir(&bin).map_err(|e| e.to_string())? {
|
||||||
|
let e = e.map_err(|e| e.to_string())?;
|
||||||
|
let target = dest.join(e.file_name());
|
||||||
|
let _ = fs::remove_file(&target);
|
||||||
|
fs::rename(e.path(), &target).map_err(|e| e.to_string())?;
|
||||||
|
}
|
||||||
|
Ok(banner)
|
||||||
|
}
|
||||||
|
"chrome" => {
|
||||||
|
let platform = m.chrome.platforms.get(target_triple()).ok_or("Chrome for Testing has no build for this platform")?;
|
||||||
|
ev("resolve", 0, None, None);
|
||||||
|
let endpoint = Url::parse(&m.chrome.endpoint).map_err(|e| e.to_string())?;
|
||||||
|
if !download_allowed(&endpoint) {
|
||||||
|
return Err("endpoint host not allowed".into());
|
||||||
|
}
|
||||||
|
let json: serde_json::Value = c.get(endpoint).send().await.map_err(|e| e.to_string())?.json().await.map_err(|e| e.to_string())?;
|
||||||
|
let (version, url) = chrome_download(&json, &m.chrome.channel, platform)?;
|
||||||
|
let head = c.head(url.clone()).send().await.ok();
|
||||||
|
let total = head.and_then(|h| h.content_length());
|
||||||
|
let file = staging.join("chrome.zip");
|
||||||
|
fetch(&c, &url, &file, &cancel, |n| ev("download", n, total, Some(version.clone()))).await?;
|
||||||
|
ev("unpack", total.unwrap_or(0), total, None);
|
||||||
|
let out = staging.join("u");
|
||||||
|
unpack(&file, "zip", &out)?;
|
||||||
|
let _ = fs::remove_file(&file);
|
||||||
|
let root = out.join(format!("chrome-{platform}"));
|
||||||
|
let exe = root.join(crate::policy::chrome_executable());
|
||||||
|
if !exe.is_file() {
|
||||||
|
return Err("Chrome for Testing archive has an unexpected layout".into());
|
||||||
|
}
|
||||||
|
ev("verify", total.unwrap_or(0), total, None);
|
||||||
|
verify_chrome(&root, &exe, &m.chrome)?;
|
||||||
|
ev("install", total.unwrap_or(0), total, None);
|
||||||
|
let dest = tools.join("chrome");
|
||||||
|
let _ = fs::remove_dir_all(&dest);
|
||||||
|
fs::rename(&root, &dest).map_err(|e| e.to_string())?;
|
||||||
|
Ok(format!("Chrome for Testing {version}"))
|
||||||
|
}
|
||||||
|
other => Err(format!("unknown tool {other}")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.await;
|
||||||
|
let _ = fs::remove_dir_all(&staging);
|
||||||
|
match &res {
|
||||||
|
Ok(v) => ev("done", 0, None, Some(v.clone())),
|
||||||
|
Err(e) if e == "cancelled" => ev("cancelled", 0, None, None),
|
||||||
|
Err(e) => ev("error", 0, None, Some(e.clone())),
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes an app-managed tool.
|
||||||
|
pub fn uninstall(data_dir: &Path, tool: &str) -> Result<(), String> {
|
||||||
|
let tools = data_dir.join("tools");
|
||||||
|
match tool {
|
||||||
|
"ffmpeg" => {
|
||||||
|
for n in ["ffmpeg", "ffprobe", "ffmpeg.exe", "ffprobe.exe"] {
|
||||||
|
let _ = fs::remove_file(tools.join("bin").join(n));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
"chrome" => {
|
||||||
|
let p = tools.join("chrome");
|
||||||
|
if p.exists() {
|
||||||
|
fs::remove_dir_all(p).map_err(|e| e.to_string())?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
other => Err(format!("unknown tool {other}")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
//! storebound desktop: a Tauri shell around the storebound CLI, which runs as a sidecar (`storebound serve --stdio`).
|
//! storebound desktop: a Tauri shell around the storebound CLI, which runs as a sidecar (`storebound serve --stdio`).
|
||||||
//! The GUI never talks to a store itself; every store call is a storebound command.
|
//! The GUI never talks to a store itself; every store call is a storebound command.
|
||||||
pub mod accounts;
|
pub mod accounts;
|
||||||
|
pub mod cache;
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod confirm;
|
pub mod confirm;
|
||||||
|
pub mod downloads;
|
||||||
pub mod policy;
|
pub mod policy;
|
||||||
pub mod sidecar;
|
pub mod sidecar;
|
||||||
pub mod store;
|
pub mod store;
|
||||||
|
|
@ -91,6 +93,32 @@ fn handle_deep_link<R: Runtime>(app: &AppHandle<R>, urls: &[url::Url]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Debug builds only: STOREBOUND_DEV_PROJECTS (folders separated by the OS path separator) seeds the app folder list
|
||||||
|
/// once, for screenshots and manual checks with the demo apps. Release builds ignore it.
|
||||||
|
fn dev_seed(data_dir: &std::path::Path) {
|
||||||
|
if !cfg!(debug_assertions) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let Some(v) = std::env::var_os("STOREBOUND_DEV_PROJECTS") else { return };
|
||||||
|
let mut st = store::load(data_dir);
|
||||||
|
let mut changed = false;
|
||||||
|
for dir in std::env::split_paths(&v) {
|
||||||
|
let dir = std::fs::canonicalize(&dir).unwrap_or(dir);
|
||||||
|
let path = dir.to_string_lossy().to_string();
|
||||||
|
if !commands::has_config(&dir) || st.projects.iter().any(|p| p.path == path) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let name = dir.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
|
||||||
|
if let Ok(id) = store::new_id() {
|
||||||
|
st.projects.push(store::Project { id, path, name, play_account: None, apple_account: None, added: store::now_iso() });
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
let _ = store::save(data_dir, &st);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
linux_webkit_env();
|
linux_webkit_env();
|
||||||
let app = tauri::Builder::default()
|
let app = tauri::Builder::default()
|
||||||
|
|
@ -106,11 +134,15 @@ pub fn run() {
|
||||||
.plugin(tauri_plugin_process::init())
|
.plugin(tauri_plugin_process::init())
|
||||||
.setup(|app| {
|
.setup(|app| {
|
||||||
let data_dir = app.path().app_data_dir()?;
|
let data_dir = app.path().app_data_dir()?;
|
||||||
|
dev_seed(&data_dir);
|
||||||
|
let cache = cache::Cache::open(&data_dir).or_else(|_| cache::Cache::in_memory()).map_err(std::io::Error::other)?;
|
||||||
app.manage(commands::AppState {
|
app.manage(commands::AppState {
|
||||||
sidecar: sidecar::Sidecar::default(),
|
sidecar: sidecar::Sidecar::default(),
|
||||||
saved: Mutex::new(store::load(&data_dir)),
|
saved: Mutex::new(store::load(&data_dir)),
|
||||||
data_dir,
|
data_dir,
|
||||||
secrets: Box::new(accounts::Keychain),
|
secrets: Box::new(accounts::Keychain),
|
||||||
|
cache,
|
||||||
|
downloads: downloads::Jobs::default(),
|
||||||
});
|
});
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
app.set_menu(menu(app.handle())?)?;
|
app.set_menu(menu(app.handle())?)?;
|
||||||
|
|
@ -144,9 +176,13 @@ pub fn run() {
|
||||||
commands::app_info,
|
commands::app_info,
|
||||||
commands::sidecar_status,
|
commands::sidecar_status,
|
||||||
commands::sidecar_call,
|
commands::sidecar_call,
|
||||||
|
commands::cache_get,
|
||||||
|
commands::cache_clear,
|
||||||
|
commands::job_cancel,
|
||||||
commands::write_with_confirm,
|
commands::write_with_confirm,
|
||||||
commands::projects_list,
|
commands::projects_list,
|
||||||
commands::project_add,
|
commands::project_add,
|
||||||
|
commands::project_init,
|
||||||
commands::project_remove,
|
commands::project_remove,
|
||||||
commands::project_set_accounts,
|
commands::project_set_accounts,
|
||||||
commands::accounts_list,
|
commands::accounts_list,
|
||||||
|
|
@ -155,6 +191,9 @@ pub fn run() {
|
||||||
commands::account_remove,
|
commands::account_remove,
|
||||||
commands::portfolio_status,
|
commands::portfolio_status,
|
||||||
commands::open_external,
|
commands::open_external,
|
||||||
|
commands::tools_info,
|
||||||
|
commands::tools_install,
|
||||||
|
commands::tools_uninstall,
|
||||||
])
|
])
|
||||||
.build(tauri::generate_context!())
|
.build(tauri::generate_context!())
|
||||||
.expect("storebound desktop failed to start");
|
.expect("storebound desktop failed to start");
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@
|
||||||
//! plugin to the webview), with the fixed arguments in policy::SIDECAR_ARGS. One process per app run, started on
|
//! plugin to the webview), with the fixed arguments in policy::SIDECAR_ARGS. One process per app run, started on
|
||||||
//! first use and again after it stops. Its first request is `session.hello` with a fresh 32-byte secret, so only this
|
//! first use and again after it stops. Its first request is `session.hello` with a fresh 32-byte secret, so only this
|
||||||
//! process can mint the confirm tokens the sidecar accepts for real writes.
|
//! process can mint the confirm tokens the sidecar accepts for real writes.
|
||||||
|
//!
|
||||||
|
//! Jobs: the webview may tag a call with a short job id. Progress notifications of that request then carry the job id
|
||||||
|
//! (so the right toast or screen shows them), and `cancel_job` sends `$/cancelRequest` for every request of the job.
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
@ -54,11 +57,19 @@ pub struct Descriptor {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pending = Arc<Mutex<HashMap<u64, oneshot::Sender<Result<Value, Fail>>>>>;
|
type Pending = Arc<Mutex<HashMap<u64, oneshot::Sender<Result<Value, Fail>>>>>;
|
||||||
|
/// Request id -> the webview's job id.
|
||||||
|
type Jobs = Arc<Mutex<HashMap<u64, String>>>;
|
||||||
|
|
||||||
|
/// Job ids come from the webview and are echoed back in events: short and plain only.
|
||||||
|
pub fn valid_job(job: &str) -> bool {
|
||||||
|
!job.is_empty() && job.len() <= 64 && job.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b':' | b'.'))
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Running {
|
pub struct Running {
|
||||||
child: Mutex<Option<CommandChild>>,
|
child: Mutex<Option<CommandChild>>,
|
||||||
secret: [u8; 32],
|
secret: [u8; 32],
|
||||||
pending: Pending,
|
pending: Pending,
|
||||||
|
jobs: Jobs,
|
||||||
next: AtomicU64,
|
next: AtomicU64,
|
||||||
alive: Arc<AtomicBool>,
|
alive: Arc<AtomicBool>,
|
||||||
pub version: String,
|
pub version: String,
|
||||||
|
|
@ -95,9 +106,36 @@ impl Running {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn call(&self, method: &str, params: Value) -> Result<Value, Fail> {
|
pub async fn call(&self, method: &str, params: Value) -> Result<Value, Fail> {
|
||||||
|
self.call_job(method, params, None).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A call whose progress events carry `job`, and which `cancel_job` can stop.
|
||||||
|
pub async fn call_job(&self, method: &str, params: Value, job: Option<&str>) -> Result<Value, Fail> {
|
||||||
let id = self.next.fetch_add(1, Ordering::SeqCst);
|
let id = self.next.fetch_add(1, Ordering::SeqCst);
|
||||||
let rx = self.send(id, method, ¶ms)?;
|
if let Some(j) = job {
|
||||||
rx.await.map_err(|_| Fail::host("the storebound sidecar stopped"))?
|
if let Ok(mut m) = self.jobs.lock() {
|
||||||
|
m.insert(id, j.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let res = match self.send(id, method, ¶ms) {
|
||||||
|
Ok(rx) => rx.await.map_err(|_| Fail::host("the storebound sidecar stopped")).and_then(|r| r),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
};
|
||||||
|
if job.is_some() {
|
||||||
|
if let Ok(mut m) = self.jobs.lock() {
|
||||||
|
m.remove(&id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cancels every running request of a job (`$/cancelRequest`; they answer -32800). Returns how many.
|
||||||
|
pub fn cancel_job(&self, job: &str) -> usize {
|
||||||
|
let ids: Vec<u64> = self.jobs.lock().map(|m| m.iter().filter(|(_, j)| j.as_str() == job).map(|(id, _)| *id).collect()).unwrap_or_default();
|
||||||
|
for id in &ids {
|
||||||
|
self.notify("$/cancelRequest", json!({ "id": id }));
|
||||||
|
}
|
||||||
|
ids.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn notify(&self, method: &str, params: Value) {
|
pub fn notify(&self, method: &str, params: Value) {
|
||||||
|
|
@ -134,8 +172,10 @@ pub struct Sidecar {
|
||||||
|
|
||||||
/// Progress of a running request, forwarded to the main window as `sidecar-progress`.
|
/// Progress of a running request, forwarded to the main window as `sidecar-progress`.
|
||||||
#[derive(Clone, Serialize)]
|
#[derive(Clone, Serialize)]
|
||||||
struct Progress {
|
pub struct Progress {
|
||||||
params: Value,
|
pub params: Value,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub job: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sidecar {
|
impl Sidecar {
|
||||||
|
|
@ -152,6 +192,12 @@ impl Sidecar {
|
||||||
Ok(r)
|
Ok(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The running sidecar, if there is one (never starts it).
|
||||||
|
pub async fn current(&self) -> Option<Arc<Running>> {
|
||||||
|
self.current.lock().await.as_ref().filter(|r| r.alive()).cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stops the sidecar; the next call starts a fresh one (after a tool install, so it sees the new tool paths).
|
||||||
pub async fn shutdown(&self) {
|
pub async fn shutdown(&self) {
|
||||||
if let Some(r) = self.current.lock().await.take() {
|
if let Some(r) = self.current.lock().await.take() {
|
||||||
r.kill();
|
r.kill();
|
||||||
|
|
@ -196,9 +242,10 @@ async fn start<R: Runtime>(app: &AppHandle<R>) -> Result<Running, Fail> {
|
||||||
.set_process_group(true);
|
.set_process_group(true);
|
||||||
let (mut rx, child) = cmd.spawn().map_err(|e| Fail::host(format!("sidecar did not start: {e}")))?;
|
let (mut rx, child) = cmd.spawn().map_err(|e| Fail::host(format!("sidecar did not start: {e}")))?;
|
||||||
let pending: Pending = Arc::new(Mutex::new(HashMap::new()));
|
let pending: Pending = Arc::new(Mutex::new(HashMap::new()));
|
||||||
|
let jobs: Jobs = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let alive = Arc::new(AtomicBool::new(true));
|
let alive = Arc::new(AtomicBool::new(true));
|
||||||
|
|
||||||
let (p, a, handle) = (pending.clone(), alive.clone(), app.clone());
|
let (p, a, handle, jb) = (pending.clone(), alive.clone(), app.clone(), jobs.clone());
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
let mut out: Vec<u8> = Vec::new();
|
let mut out: Vec<u8> = Vec::new();
|
||||||
let mut err: Vec<u8> = Vec::new();
|
let mut err: Vec<u8> = Vec::new();
|
||||||
|
|
@ -208,7 +255,7 @@ async fn start<R: Runtime>(app: &AppHandle<R>) -> Result<Running, Fail> {
|
||||||
out.extend_from_slice(&bytes);
|
out.extend_from_slice(&bytes);
|
||||||
while let Some(nl) = out.iter().position(|b| *b == b'\n') {
|
while let Some(nl) = out.iter().position(|b| *b == b'\n') {
|
||||||
let line: Vec<u8> = out.drain(..=nl).collect();
|
let line: Vec<u8> = out.drain(..=nl).collect();
|
||||||
route(&handle, &p, &line);
|
route(&handle, &p, &jb, &line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CommandEvent::Stderr(bytes) => {
|
CommandEvent::Stderr(bytes) => {
|
||||||
|
|
@ -237,6 +284,7 @@ async fn start<R: Runtime>(app: &AppHandle<R>) -> Result<Running, Fail> {
|
||||||
child: Mutex::new(Some(child)),
|
child: Mutex::new(Some(child)),
|
||||||
secret,
|
secret,
|
||||||
pending,
|
pending,
|
||||||
|
jobs,
|
||||||
next: AtomicU64::new(1),
|
next: AtomicU64::new(1),
|
||||||
alive,
|
alive,
|
||||||
version: String::new(),
|
version: String::new(),
|
||||||
|
|
@ -274,7 +322,7 @@ async fn start<R: Runtime>(app: &AppHandle<R>) -> Result<Running, Fail> {
|
||||||
Ok(running)
|
Ok(running)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn route<R: Runtime>(app: &AppHandle<R>, pending: &Pending, line: &[u8]) {
|
fn route<R: Runtime>(app: &AppHandle<R>, pending: &Pending, jobs: &Jobs, line: &[u8]) {
|
||||||
let Ok(msg) = serde_json::from_slice::<Value>(line) else { return };
|
let Ok(msg) = serde_json::from_slice::<Value>(line) else { return };
|
||||||
if let Some(id) = msg.get("id").and_then(Value::as_u64) {
|
if let Some(id) = msg.get("id").and_then(Value::as_u64) {
|
||||||
let tx = pending.lock().ok().and_then(|mut m| m.remove(&id));
|
let tx = pending.lock().ok().and_then(|mut m| m.remove(&id));
|
||||||
|
|
@ -290,6 +338,7 @@ fn route<R: Runtime>(app: &AppHandle<R>, pending: &Pending, line: &[u8]) {
|
||||||
let _ = tx.send(res);
|
let _ = tx.send(res);
|
||||||
}
|
}
|
||||||
} else if msg.get("method").and_then(Value::as_str) == Some("progress") {
|
} else if msg.get("method").and_then(Value::as_str) == Some("progress") {
|
||||||
let _ = app.emit_to(policy::MAIN, "sidecar-progress", Progress { params: msg["params"].clone() });
|
let job = msg["params"]["requestId"].as_u64().and_then(|id| jobs.lock().ok().and_then(|m| m.get(&id).cloned()));
|
||||||
|
let _ = app.emit_to(policy::MAIN, "sidecar-progress", Progress { params: msg["params"].clone(), job });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
137
apps/desktop/src-tauri/tests/d2.rs
Normal file
137
apps/desktop/src-tauri/tests/d2.rs
Normal file
|
|
@ -0,0 +1,137 @@
|
||||||
|
//! Desktop D2: offline cache, tool download manifest and rules, job ids.
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use serde_json::json;
|
||||||
|
use storebound_desktop::cache::{key, Cache};
|
||||||
|
use storebound_desktop::downloads::{self, chrome_download, download_allowed, inside, manifest, unpack};
|
||||||
|
use storebound_desktop::sidecar::valid_job;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cache_keeps_the_last_read_per_project_method_and_args() {
|
||||||
|
let c = Cache::in_memory().unwrap();
|
||||||
|
assert_eq!(c.get("p1", "health", &json!({})).unwrap(), None);
|
||||||
|
c.put("p1", "health", &json!({}), &json!({ "ok": true, "n": 1 }), "2026-10-10T10:00:00Z").unwrap();
|
||||||
|
c.put("p1", "health", &json!({}), &json!({ "ok": true, "n": 2 }), "2026-10-10T11:00:00Z").unwrap();
|
||||||
|
c.put("p1", "reviews.list", &json!({ "days": 14, "store": "play" }), &json!([1]), "2026-10-10T09:00:00Z").unwrap();
|
||||||
|
c.put("p2", "health", &json!({}), &json!({ "n": 9 }), "2026-10-10T08:00:00Z").unwrap();
|
||||||
|
let e = c.get("p1", "health", &json!({})).unwrap().unwrap();
|
||||||
|
assert_eq!((e.value["n"].clone(), e.at.as_str()), (json!(2), "2026-10-10T11:00:00Z"));
|
||||||
|
// argument order does not matter (canonical JSON), values do
|
||||||
|
assert!(c.get("p1", "reviews.list", &json!({ "store": "play", "days": 14 })).unwrap().is_some());
|
||||||
|
assert!(c.get("p1", "reviews.list", &json!({ "store": "apple", "days": 14 })).unwrap().is_none());
|
||||||
|
assert_eq!(key("a", "m", &json!({ "b": 1, "a": 2 })), key("a", "m", &json!({ "a": 2, "b": 1 })));
|
||||||
|
assert_eq!(c.stamps("p1").unwrap().len(), 2);
|
||||||
|
c.forget_project("p1").unwrap();
|
||||||
|
assert!(c.get("p1", "health", &json!({})).unwrap().is_none());
|
||||||
|
assert!(c.get("p2", "health", &json!({})).unwrap().is_some());
|
||||||
|
c.clear().unwrap();
|
||||||
|
assert!(c.get("p2", "health", &json!({})).unwrap().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cache_survives_a_reopen() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
Cache::open(dir.path()).unwrap().put("p", "m", &json!({}), &json!({ "x": 1 }), "t").unwrap();
|
||||||
|
assert_eq!(Cache::open(dir.path()).unwrap().get("p", "m", &json!({})).unwrap().unwrap().value, json!({ "x": 1 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn manifest_pins_every_desktop_target_with_hashes() {
|
||||||
|
let m = manifest().unwrap();
|
||||||
|
for t in ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc"] {
|
||||||
|
let list = m.ffmpeg.targets.get(t).unwrap_or_else(|| panic!("no ffmpeg for {t}"));
|
||||||
|
let mut outs = vec![];
|
||||||
|
for a in list {
|
||||||
|
let u = Url::parse(&a.url).unwrap();
|
||||||
|
assert!(download_allowed(&u), "{}", a.url);
|
||||||
|
assert!(!a.url.contains("/latest/"), "pinned URLs only: {}", a.url);
|
||||||
|
assert_eq!(a.sha256.len(), 64);
|
||||||
|
assert!(a.sha256.chars().all(|c| c.is_ascii_hexdigit()));
|
||||||
|
assert!(a.size > 1_000_000);
|
||||||
|
assert!(a.kind == "zip" || a.kind == "tar.xz");
|
||||||
|
for f in &a.files {
|
||||||
|
inside(std::path::Path::new("/x"), &f.from).unwrap();
|
||||||
|
outs.push(f.to.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outs.sort();
|
||||||
|
let exe = if t.contains("windows") { ".exe" } else { "" };
|
||||||
|
assert_eq!(outs, vec![format!("ffmpeg{exe}"), format!("ffprobe{exe}")], "{t}");
|
||||||
|
}
|
||||||
|
assert!(download_allowed(&Url::parse(&m.chrome.endpoint).unwrap()));
|
||||||
|
assert_eq!(m.chrome.mac_team_id, "EQHXZ8M8AV");
|
||||||
|
assert!(m.ffmpeg.license.contains("GPL"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn downloads_only_over_https_from_the_listed_hosts() {
|
||||||
|
for ok in ["https://ffmpeg.martin-riedl.de/download/x.zip", "https://github.com/BtbN/FFmpeg-Builds/releases/download/a/b.zip", "https://storage.googleapis.com/chrome-for-testing-public/1/mac-arm64/chrome-mac-arm64.zip"] {
|
||||||
|
assert!(download_allowed(&Url::parse(ok).unwrap()), "{ok}");
|
||||||
|
}
|
||||||
|
for bad in ["http://ffmpeg.martin-riedl.de/x.zip", "https://evil.test/x.zip", "https://github.com.evil.test/x", "https://storage.googleapis.com:8443/x", "file:///tmp/x.zip"] {
|
||||||
|
assert!(!download_allowed(&Url::parse(bad).unwrap()), "{bad}");
|
||||||
|
}
|
||||||
|
assert!(!download_allowed(&Url::parse(&format!("https://{}@github.com/x", "u")).unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn chrome_for_testing_endpoint_answer() {
|
||||||
|
let j = json!({ "channels": { "Stable": { "version": "141.0.7390.54", "downloads": { "chrome": [
|
||||||
|
{ "platform": "linux64", "url": "https://storage.googleapis.com/chrome-for-testing-public/141.0.7390.54/linux64/chrome-linux64.zip" },
|
||||||
|
{ "platform": "mac-arm64", "url": "https://storage.googleapis.com/chrome-for-testing-public/141.0.7390.54/mac-arm64/chrome-mac-arm64.zip" }
|
||||||
|
] } } } });
|
||||||
|
let (v, u) = chrome_download(&j, "Stable", "mac-arm64").unwrap();
|
||||||
|
assert_eq!(v, "141.0.7390.54");
|
||||||
|
assert!(u.as_str().ends_with("chrome-mac-arm64.zip"));
|
||||||
|
assert!(chrome_download(&j, "Stable", "win64").is_err());
|
||||||
|
let evil = json!({ "channels": { "Stable": { "version": "1", "downloads": { "chrome": [{ "platform": "win64", "url": "https://evil.test/chrome.zip" }] } } } });
|
||||||
|
assert!(chrome_download(&evil, "Stable", "win64").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn archive_paths_stay_inside() {
|
||||||
|
let base = std::path::Path::new("/data/tools/.staging");
|
||||||
|
assert!(inside(base, "ffmpeg-n9/bin/ffmpeg").is_ok());
|
||||||
|
for bad in ["../ffmpeg", "/usr/bin/ffmpeg", "a/../../b", "./x"] {
|
||||||
|
assert!(inside(base, bad).is_err(), "{bad}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zip_unpack_refuses_entries_outside_the_folder() {
|
||||||
|
if cfg!(target_os = "macos") {
|
||||||
|
return; // macOS unpacks with ditto, which never writes outside the target folder
|
||||||
|
}
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let zip_path = dir.path().join("evil.zip");
|
||||||
|
{
|
||||||
|
let f = std::fs::File::create(&zip_path).unwrap();
|
||||||
|
let mut z = zip::ZipWriter::new(f);
|
||||||
|
z.start_file("../escape.txt", zip::write::SimpleFileOptions::default()).unwrap();
|
||||||
|
z.write_all(b"x").unwrap();
|
||||||
|
z.finish().unwrap();
|
||||||
|
}
|
||||||
|
let out = dir.path().join("out");
|
||||||
|
let _ = unpack(&zip_path, "zip", &out);
|
||||||
|
assert!(!dir.path().join("escape.txt").exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn download_info_lists_both_tools() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let info = downloads::info(dir.path()).unwrap();
|
||||||
|
assert_eq!(info.iter().map(|i| i.tool).collect::<Vec<_>>(), vec!["ffmpeg", "chrome"]);
|
||||||
|
assert!(info.iter().all(|i| !i.installed));
|
||||||
|
assert!(info[0].available && info[0].size.unwrap_or(0) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn job_ids_are_short_and_plain() {
|
||||||
|
for ok in ["portfolio-1", "dl:ffmpeg:3", "a.b_c"] {
|
||||||
|
assert!(valid_job(ok), "{ok}");
|
||||||
|
}
|
||||||
|
for bad in ["", "a b", "x\"y", "<script>", &"x".repeat(65)] {
|
||||||
|
assert!(!valid_job(bad), "{bad}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -173,6 +173,15 @@ fn sidecar_starts_with_fixed_arguments_from_one_place() {
|
||||||
assert!(src.contains(".args(policy::SIDECAR_ARGS)"));
|
assert!(src.contains(".args(policy::SIDECAR_ARGS)"));
|
||||||
}
|
}
|
||||||
assert!(!src.contains(".command("), "{}: no other programs are started", p.display());
|
assert!(!src.contains(".command("), "{}: no other programs are started", p.display());
|
||||||
|
// other programs: only the download checks (unpack, signature, version banner), with fixed program names
|
||||||
|
if src.contains("Command::new(") {
|
||||||
|
assert!(p.ends_with("downloads.rs"), "{} starts a program", p.display());
|
||||||
|
for (i, _) in src.match_indices("Command::new(") {
|
||||||
|
let arg = &src[i + "Command::new(".len()..];
|
||||||
|
let ok = ["\"tar\")", "\"/usr/bin/ditto\")", "\"/usr/bin/codesign\")", "\"powershell\")", "exe)"];
|
||||||
|
assert!(ok.iter().any(|o| arg.starts_with(o)), "unexpected program: {}", &arg[..arg.find(')').unwrap_or(20).min(40)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
sites += n;
|
sites += n;
|
||||||
}
|
}
|
||||||
assert_eq!(sites, 1);
|
assert_eq!(sites, 1);
|
||||||
|
|
|
||||||
135
apps/desktop/src-tauri/tools-manifest.json
Normal file
135
apps/desktop/src-tauri/tools-manifest.json
Normal file
|
|
@ -0,0 +1,135 @@
|
||||||
|
{
|
||||||
|
"generated": "2026-10-10",
|
||||||
|
"ffmpeg": {
|
||||||
|
"version": "9.0.2",
|
||||||
|
"license": "GPL-3.0-or-later (ffmpeg built with GPL components)",
|
||||||
|
"licenseUrl": "https://ffmpeg.org/legal.html",
|
||||||
|
"sources": {
|
||||||
|
"macos": "https://ffmpeg.martin-riedl.de/",
|
||||||
|
"linux": "https://github.com/BtbN/FFmpeg-Builds",
|
||||||
|
"windows": "https://github.com/BtbN/FFmpeg-Builds"
|
||||||
|
},
|
||||||
|
"targets": {
|
||||||
|
"aarch64-apple-darwin": [
|
||||||
|
{
|
||||||
|
"url": "https://ffmpeg.martin-riedl.de/download/macos/arm64/1789931890_9.0.2/ffmpeg.zip",
|
||||||
|
"sha256": "c8ed4c4e6978a03c485edbfe4e0a5dc2380f8a30bba5150531b31b094492d924",
|
||||||
|
"size": 28395699,
|
||||||
|
"kind": "zip",
|
||||||
|
"files": [
|
||||||
|
{
|
||||||
|
"from": "ffmpeg",
|
||||||
|
"to": "ffmpeg"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://ffmpeg.martin-riedl.de/download/macos/arm64/1789931890_9.0.2/ffprobe.zip",
|
||||||
|
"sha256": "fcbe839537485eaee7a7a8bc5cbc0f90d53617e80943e8a5b2e31cb851197ea6",
|
||||||
|
"size": 28317701,
|
||||||
|
"kind": "zip",
|
||||||
|
"files": [
|
||||||
|
{
|
||||||
|
"from": "ffprobe",
|
||||||
|
"to": "ffprobe"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"x86_64-apple-darwin": [
|
||||||
|
{
|
||||||
|
"url": "https://ffmpeg.martin-riedl.de/download/macos/amd64/1789931006_9.0.2/ffmpeg.zip",
|
||||||
|
"sha256": "7c6b4125b191cbf773832dc51f424cf2b6bb7da43007d1e066f95909e47cacd4",
|
||||||
|
"size": 33816391,
|
||||||
|
"kind": "zip",
|
||||||
|
"files": [
|
||||||
|
{
|
||||||
|
"from": "ffmpeg",
|
||||||
|
"to": "ffmpeg"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://ffmpeg.martin-riedl.de/download/macos/amd64/1789931006_9.0.2/ffprobe.zip",
|
||||||
|
"sha256": "2322438ed2f6319a691291b247d09c69dcaa3a982460d1f269a7e1af335cfdfd",
|
||||||
|
"size": 33719233,
|
||||||
|
"kind": "zip",
|
||||||
|
"files": [
|
||||||
|
{
|
||||||
|
"from": "ffprobe",
|
||||||
|
"to": "ffprobe"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"x86_64-unknown-linux-gnu": [
|
||||||
|
{
|
||||||
|
"url": "https://github.com/BtbN/FFmpeg-Builds/releases/download/autobuild-2026-10-10-13-04/ffmpeg-n9.0.2-25-g67b60c310b-linux64-gpl-9.0.tar.xz",
|
||||||
|
"sha256": "7e898ca0a18e8620c0caa9af9a728bc01be006398dd5db5a99d53f14de010a9a",
|
||||||
|
"size": 151141436,
|
||||||
|
"kind": "tar.xz",
|
||||||
|
"files": [
|
||||||
|
{
|
||||||
|
"from": "ffmpeg-n9.0.2-25-g67b60c310b-linux64-gpl-9.0/bin/ffmpeg",
|
||||||
|
"to": "ffmpeg"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"from": "ffmpeg-n9.0.2-25-g67b60c310b-linux64-gpl-9.0/bin/ffprobe",
|
||||||
|
"to": "ffprobe"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"aarch64-unknown-linux-gnu": [
|
||||||
|
{
|
||||||
|
"url": "https://github.com/BtbN/FFmpeg-Builds/releases/download/autobuild-2026-10-10-13-04/ffmpeg-n9.0.2-25-g67b60c310b-linuxarm64-gpl-9.0.tar.xz",
|
||||||
|
"sha256": "8f897563d9a39eb5616ab043f29f9018358d1f8c15d4b0d30df0dba71f3a931f",
|
||||||
|
"size": 127540920,
|
||||||
|
"kind": "tar.xz",
|
||||||
|
"files": [
|
||||||
|
{
|
||||||
|
"from": "ffmpeg-n9.0.2-25-g67b60c310b-linuxarm64-gpl-9.0/bin/ffmpeg",
|
||||||
|
"to": "ffmpeg"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"from": "ffmpeg-n9.0.2-25-g67b60c310b-linuxarm64-gpl-9.0/bin/ffprobe",
|
||||||
|
"to": "ffprobe"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"x86_64-pc-windows-msvc": [
|
||||||
|
{
|
||||||
|
"url": "https://github.com/BtbN/FFmpeg-Builds/releases/download/autobuild-2026-10-10-13-04/ffmpeg-n9.0.2-25-g67b60c310b-win64-gpl-9.0.zip",
|
||||||
|
"sha256": "1b196e14bd13b76c0d33411f8ef4511f8708a459f682b58ca4c0285a793138cf",
|
||||||
|
"size": 193972697,
|
||||||
|
"kind": "zip",
|
||||||
|
"files": [
|
||||||
|
{
|
||||||
|
"from": "ffmpeg-n9.0.2-25-g67b60c310b-win64-gpl-9.0/bin/ffmpeg.exe",
|
||||||
|
"to": "ffmpeg.exe"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"from": "ffmpeg-n9.0.2-25-g67b60c310b-win64-gpl-9.0/bin/ffprobe.exe",
|
||||||
|
"to": "ffprobe.exe"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"chrome": {
|
||||||
|
"endpoint": "https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json",
|
||||||
|
"channel": "Stable",
|
||||||
|
"platforms": {
|
||||||
|
"aarch64-apple-darwin": "mac-arm64",
|
||||||
|
"x86_64-apple-darwin": "mac-x64",
|
||||||
|
"x86_64-unknown-linux-gnu": "linux64",
|
||||||
|
"x86_64-pc-windows-msvc": "win64"
|
||||||
|
},
|
||||||
|
"macTeamId": "EQHXZ8M8AV",
|
||||||
|
"windowsSigner": "Google LLC",
|
||||||
|
"license": "Chromium BSD-style license and third-party notices; Google Chrome for Testing terms",
|
||||||
|
"licenseUrl": "https://github.com/GoogleChromeLabs/chrome-for-testing#readme"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,7 +16,9 @@ export const RULES: Rule[] = [
|
||||||
{ id: 'private_key-json', re: new RegExp('"private' + '_key"\\s*:\\s*"-----BEGIN', 'g') },
|
{ id: 'private_key-json', re: new RegExp('"private' + '_key"\\s*:\\s*"-----BEGIN', 'g') },
|
||||||
{ id: 'asc-key-file', re: new RegExp('AuthKey' + '_[A-Z0-9]{10}\\.p8', 'g') },
|
{ id: 'asc-key-file', re: new RegExp('AuthKey' + '_[A-Z0-9]{10}\\.p8', 'g') },
|
||||||
{ id: 'apple-id-like', re: /\b(?=[A-Z0-9]{10}\b)(?=[A-Z0-9]*\d)(?=[A-Z0-9]*[A-Z])[A-Z0-9]{10}\b/g,
|
{ id: 'apple-id-like', re: /\b(?=[A-Z0-9]{10}\b)(?=[A-Z0-9]*\d)(?=[A-Z0-9]*[A-Z])[A-Z0-9]{10}\b/g,
|
||||||
files: /^(templates\/|.*\.config\.(ts|js|json)$|.*\.json$|.*\.toml$|.*\.plist$)/ },
|
files: /^(templates\/|.*\.config\.(ts|js|json)$|.*\.json$|.*\.toml$|.*\.plist$)/,
|
||||||
|
// Google's public code signing team, which the desktop app checks Chrome for Testing against
|
||||||
|
allow: (_m, line) => /"macTeamId":/.test(line) },
|
||||||
{ id: 'issuer-uuid', re: /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/g,
|
{ id: 'issuer-uuid', re: /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/g,
|
||||||
allow: m => /^0{8}-0{4}-0{4}-0{4}-0{12}$/.test(m) },
|
allow: m => /^0{8}-0{4}-0{4}-0{4}-0{12}$/.test(m) },
|
||||||
{ id: 'ip-address', re: /(?<![\w.\-]|[A-Za-z]\/)(?:\d{1,3}\.){3}\d{1,3}(?![\w.\-])/g, allow: m => ['127.0.0.1', '0.0.0.0'].includes(m) },
|
{ id: 'ip-address', re: /(?<![\w.\-]|[A-Za-z]\/)(?:\d{1,3}\.){3}\d{1,3}(?![\w.\-])/g, allow: m => ['127.0.0.1', '0.0.0.0'].includes(m) },
|
||||||
|
|
|
||||||
103
tools/tools-manifest.ts
Normal file
103
tools/tools-manifest.ts
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
#!/usr/bin/env bun
|
||||||
|
// Writes apps/desktop/src-tauri/tools-manifest.json: the exact ffmpeg archives the desktop app may download (URL,
|
||||||
|
// SHA-256, size, files inside), per Rust target triple, plus the Chrome for Testing endpoint and the signers its
|
||||||
|
// downloads must carry. Run before each desktop release (the BtbN autobuilds are pruned after a while):
|
||||||
|
// bun tools/tools-manifest.ts resolve, verify, write
|
||||||
|
// bun tools/tools-manifest.ts --check only check that the manifest parses and every URL still answers
|
||||||
|
// Trust: macOS archives (ffmpeg.martin-riedl.de) are downloaded and hashed here, and that hash must equal the
|
||||||
|
// publisher's .sha256 file. Linux / Windows archives (github.com/BtbN/FFmpeg-Builds, pinned autobuild tag) use the
|
||||||
|
// SHA-256 digest GitHub computes for every release asset, which must equal the publisher's checksums.sha256. So every
|
||||||
|
// pinned hash has two independent sources, and the app checks it again after its own download (docs/desktop.md).
|
||||||
|
import { writeFileSync } from 'node:fs';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
|
||||||
|
export const MANIFEST = join(import.meta.dir, '..', 'apps/desktop/src-tauri/tools-manifest.json');
|
||||||
|
const FFMPEG_MAJOR = '9.0';
|
||||||
|
|
||||||
|
export interface Archive { url: string; sha256: string; size: number; kind: 'zip' | 'tar.xz'; files: { from: string; to: string }[] }
|
||||||
|
export interface ToolsManifest {
|
||||||
|
generated: string;
|
||||||
|
ffmpeg: { version: string; license: string; licenseUrl: string; sources: Record<string, string>; targets: Record<string, Archive[]> };
|
||||||
|
chrome: { endpoint: string; channel: string; platforms: Record<string, string>; macTeamId: string; windowsSigner: string; license: string; licenseUrl: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
const sha256 = (b: ArrayBuffer): string => new Bun.CryptoHasher('sha256').update(new Uint8Array(b)).digest('hex');
|
||||||
|
|
||||||
|
async function riedl(arch: 'arm64' | 'amd64', tool: 'ffmpeg' | 'ffprobe'): Promise<{ archive: Archive; version: string }> {
|
||||||
|
const r = await fetch(`https://ffmpeg.martin-riedl.de/redirect/latest/macos/${arch}/release/${tool}.zip`, { redirect: 'manual' });
|
||||||
|
const loc = r.headers.get('location');
|
||||||
|
if (r.status !== 307 && r.status !== 302 || !loc) throw new Error(`martin-riedl ${arch} ${tool}: no redirect (${r.status})`);
|
||||||
|
const url = new URL(loc, 'https://ffmpeg.martin-riedl.de').toString();
|
||||||
|
const version = /_(\d+(?:\.\d+)+)\//.exec(url)?.[1] ?? '?';
|
||||||
|
const published = (await (await fetch(`${url}.sha256`)).text()).trim().split(/\s+/)[0]!;
|
||||||
|
console.log(`download ${url}`);
|
||||||
|
const body = await (await fetch(url)).arrayBuffer();
|
||||||
|
const got = sha256(body);
|
||||||
|
if (got !== published) throw new Error(`${url}: sha256 ${got} != published ${published}`);
|
||||||
|
return { version, archive: { url, sha256: got, size: body.byteLength, kind: 'zip', files: [{ from: tool, to: tool }] } };
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GhAsset { name: string; size: number; digest?: string; browser_download_url: string }
|
||||||
|
|
||||||
|
async function btbn(): Promise<Record<string, Archive[]>> {
|
||||||
|
const rels = await (await fetch('https://api.github.com/repos/BtbN/FFmpeg-Builds/releases?per_page=5', { headers: { accept: 'application/vnd.github+json' } })).json() as { tag_name: string; assets: GhAsset[] }[];
|
||||||
|
const rel = rels.find(r => r.tag_name.startsWith('autobuild-'));
|
||||||
|
if (!rel) throw new Error('BtbN: no autobuild release');
|
||||||
|
const sums = await (await fetch(rel.assets.find(a => a.name === 'checksums.sha256')!.browser_download_url)).text();
|
||||||
|
const out: Record<string, Archive[]> = {};
|
||||||
|
const pick = (os: string, triple: string, ext: 'tar.xz' | 'zip', exe: string) => {
|
||||||
|
const re = new RegExp(`^ffmpeg-n${FFMPEG_MAJOR.replace('.', '\\.')}[^-]*-\\d+-g[0-9a-f]+-${os}-gpl-${FFMPEG_MAJOR.replace('.', '\\.')}\\.${ext.replace('.', '\\.')}$`);
|
||||||
|
const a = rel.assets.find(x => re.test(x.name));
|
||||||
|
if (!a?.digest?.startsWith('sha256:')) throw new Error(`BtbN ${os}: no asset with a digest in ${rel.tag_name}`);
|
||||||
|
const digest = a.digest.slice(7);
|
||||||
|
const line = sums.split('\n').find(l => l.trim().endsWith(a.name));
|
||||||
|
if (!line || line.split(/\s+/)[0] !== digest) throw new Error(`BtbN ${a.name}: GitHub digest and checksums.sha256 differ`);
|
||||||
|
const top = a.name.replace(/\.(tar\.xz|zip)$/, '');
|
||||||
|
out[triple] = [{ url: a.browser_download_url, sha256: digest, size: a.size, kind: ext, files: [{ from: `${top}/bin/ffmpeg${exe}`, to: `ffmpeg${exe}` }, { from: `${top}/bin/ffprobe${exe}`, to: `ffprobe${exe}` }] }];
|
||||||
|
};
|
||||||
|
pick('linux64', 'x86_64-unknown-linux-gnu', 'tar.xz', '');
|
||||||
|
pick('linuxarm64', 'aarch64-unknown-linux-gnu', 'tar.xz', '');
|
||||||
|
pick('win64', 'x86_64-pc-windows-msvc', 'zip', '.exe');
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function build(): Promise<ToolsManifest> {
|
||||||
|
const targets: Record<string, Archive[]> = {};
|
||||||
|
let version = '?';
|
||||||
|
for (const [arch, triple] of [['arm64', 'aarch64-apple-darwin'], ['amd64', 'x86_64-apple-darwin']] as const) {
|
||||||
|
const ff = await riedl(arch, 'ffmpeg'), fp = await riedl(arch, 'ffprobe');
|
||||||
|
version = ff.version;
|
||||||
|
targets[triple] = [ff.archive, fp.archive];
|
||||||
|
}
|
||||||
|
Object.assign(targets, await btbn());
|
||||||
|
return {
|
||||||
|
generated: new Date().toISOString().slice(0, 10),
|
||||||
|
ffmpeg: {
|
||||||
|
version, license: 'GPL-3.0-or-later (ffmpeg built with GPL components)', licenseUrl: 'https://ffmpeg.org/legal.html',
|
||||||
|
sources: { macos: 'https://ffmpeg.martin-riedl.de/', linux: 'https://github.com/BtbN/FFmpeg-Builds', windows: 'https://github.com/BtbN/FFmpeg-Builds' },
|
||||||
|
targets,
|
||||||
|
},
|
||||||
|
chrome: {
|
||||||
|
endpoint: 'https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json', channel: 'Stable',
|
||||||
|
platforms: { 'aarch64-apple-darwin': 'mac-arm64', 'x86_64-apple-darwin': 'mac-x64', 'x86_64-unknown-linux-gnu': 'linux64', 'x86_64-pc-windows-msvc': 'win64' },
|
||||||
|
macTeamId: 'EQHXZ8M8AV', windowsSigner: 'Google LLC',
|
||||||
|
license: 'Chromium BSD-style license and third-party notices; Google Chrome for Testing terms', licenseUrl: 'https://github.com/GoogleChromeLabs/chrome-for-testing#readme',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (import.meta.main) {
|
||||||
|
if (process.argv.includes('--check')) {
|
||||||
|
const m = await Bun.file(MANIFEST).json() as ToolsManifest;
|
||||||
|
let bad = 0;
|
||||||
|
for (const [t, list] of Object.entries(m.ffmpeg.targets)) for (const a of list) {
|
||||||
|
const r = await fetch(a.url, { method: 'HEAD' });
|
||||||
|
console.log(`${r.ok ? 'ok ' : 'BAD'} ${t} ${a.url}`);
|
||||||
|
if (!r.ok) bad++;
|
||||||
|
}
|
||||||
|
process.exit(bad ? 1 : 0);
|
||||||
|
}
|
||||||
|
const m = await build();
|
||||||
|
writeFileSync(MANIFEST, JSON.stringify(m, null, 2) + '\n');
|
||||||
|
console.log(`wrote ${MANIFEST} (ffmpeg ${m.ffmpeg.version}, ${Object.keys(m.ffmpeg.targets).length} targets)`);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue