Updates
With the updater feature, an Updater finds a newer version, downloads it, checks its signature, and installs it.
Name where new versions are announced
Updates are behind the updater feature of hypersig_desktop, which adds Updates, App::updates, and Updater. Updates::new is pub const fn new(version: &'static str, endpoint: &'static str, public_key: &'static str) -> Updates:
| Argument | What to pass |
|---|---|
version | The running version, env!("CARGO_PKG_VERSION") in the application's crate. |
endpoint | The URL asked for a newer version. It may hold {{target}}, {{arch}}, and {{current_version}}. |
public_key | The public key cargo packager signer generate wrote, the contents of the .pub file. |
Before each request, {{target}} becomes linux, windows, or macos, {{arch}} becomes x86_64, i686, aarch64, or armv7, and {{current_version}} becomes version, so https://updates.example.com/notes/{{target}}/{{arch}}/{{current_version}} asks https://updates.example.com/notes/macos/aarch64/1.0.0 on an Apple silicon Mac running 1.0.0. Updates::new and App::updates are const fn, so the whole setting fits in the const that names the application. include_str! of the .pub file gives a &'static str too.
Find, download, and install a version
App::updates turns the App into an App<Updates>, whose run hands the setup an Updater beside the Os, so only an application that named its updates has one to call. The setup puts it in the router's state, and a handler asks with updater.check(), async fn check(&self) -> Result<Option<Update>, Error>. It answers Ok(None) when the endpoint announces nothing newer than version. An Update gives version(), the announced version, and notes(), the release notes when the endpoint sent some. Update::download consumes it and returns a Downloaded once the package is downloaded and its signature verified, and Downloaded::install installs it. In the example below, one action runs all three steps when the button is clicked, and reports the result in a signal:
use hypersig_desktop::{App, Os, Updater, Updates};
/// The public key `cargo packager signer generate` printed.
const PUBLIC_KEY: &str = "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IEJFQjNFNjBDMjc0NTYxQTUKUldTbFlVVW5ET2F6dnJscS9QcVdGbVlLMTB6VWEzSzhndUlxbEtlL2NITk9ZVFV3dnk0TmpYaXQK";
const APP: App<Updates> = App::new("dev.example.notes").updates(Updates::new(
env!("CARGO_PKG_VERSION"),
"https://updates.example.com/notes/{{target}}/{{arch}}/{{current_version}}",
PUBLIC_KEY,
));
#[derive(Signals, Serialize, Deserialize)]
struct About {
status: String,
}
#[derive(Clone)]
struct AppState {
os: Os,
updater: Updater,
}
hypersig::routes! {
fn pages(Router<AppState>);
const ABOUT: Route = "/" => about;
action UPDATE: Action<About> = post "/update" => update;
}
async fn about(_: AppState, (): ()) -> Page<About> {
Page::new("About", About { status: String::new() })
.child(button().on_click(UPDATE.call()).text("Check for updates"))
.child(p().bind_text(About::status()))
}
async fn update(state: AppState, (): ()) -> Patches<About> {
let status = About::status();
let update = match state.updater.check().await {
Ok(Some(update)) => update,
Ok(None) => return status.patch("This is the newest version.".to_owned()).into(),
Err(error) => return status.patch(format!("Not checked: {error}")).into(),
};
let version = update.version().to_owned();
let downloaded = match update.download().await {
Ok(downloaded) => downloaded,
Err(error) => return status.patch(format!("Not downloaded: {error}")).into(),
};
match downloaded.install().await {
Ok(()) => status.patch(format!("Version {version} runs from the next start.")).into(),
Err(error) => status.patch(format!("Not installed: {error}")).into(),
}
}
fn main() -> Result<(), hypersig_desktop::Error> {
APP.run(&ABOUT.url(()), |os, updater| async move {
pages(Router::new()).hypersig_runtime().with_state(AppState { os, updater })
})
}The new version runs once the application starts again. On Windows, the installer replaces the running program and cargo-packager-updater 0.2.3 ends the process once it has started it, so install closes the application first, as Quit does: it returns Ok(()) at once, the windows close, the server finishes the requests it is answering, and the NSIS installer starts as the last thing App::run does. When the installer cannot start, App::run returns the error. No run has installed a package on Windows yet. On macOS the .app bundle, and on Linux the AppImage, are replaced on disk, and install returns while the application keeps running. In cargo-packager-updater 0.2.3's source, a .app in a folder the person cannot write to is moved into place after macOS asks for an administrator's password.
examples/update built as 0.1.0 finds a 0.2.0 AppImage that cargo-packager signed with a key made in the job, served on 127.0.0.1, and installs it over the file APPIMAGE names. On macOS and Windows, an update has not been installed by any run.What the endpoint answers
The endpoint answers 204 No Content when there is no newer version, which check returns as Ok(None). Otherwise it answers 200 OK with JSON naming the version and, for the system and architecture in the request's URL, the package's URL, the contents of its .sig file, and its format. This is the answer hypersig_desktop's tests serve to a version before 1.1.0, with the URL and the signature written out:
HTTP/1.1 200 OK
content-type: application/json
{
"version": "1.1.0",
"notes": "Faster saves.",
"url": "https://updates.example.com/notes/1.1.0/hypersig-notes.app.tar.gz",
"signature": "<the contents of hypersig-notes.app.tar.gz.sig>",
"format": "app"
}| System | format | url points at |
|---|---|---|
| macOS | app | the .app.tar.gz that signing made |
| Windows | nsis | the NSIS installer .exe |
| Linux | appimage | the AppImage |
The .dmg, the .deb, and the pacman package are not formats an update installs from. version may start with v, and notes and pub_date, an RFC 3339 date, may be left out. Instead of one answer per system, the endpoint may also answer every system at once under platforms, keyed macos-aarch64 and so on, as the cargo-packager-updater documentation describes. A file served as is can then be the endpoint.
When an update fails
Every failure of check, download, and install is an Error whose kind() is ErrorKind::Update, and its Display says what failed:
checkfails when the endpoint cannot be reached, and when it answers something other than204or the JSON above.downloadfails when the package cannot be downloaded, and when the public key does not verify its signature. A refused package is never installed, since only aDownloadedhasinstall.installfails when the package cannot be put in place.
Updates::new checks its arguments, and in a const a wrong one is a compile error. version is a semantic version such as 1.2.3. endpoint starts with https:// and a host; plain http is accepted only to 127.0.0.1 or localhost, for a test on the same machine. public_key is the base64 of a minisign public key: an untrusted comment: line, then the key. A key of that shape that does not verify the package still fails, at download. A test puts testing::FakeOs::updater in the state, whose check returns Ok(None), so a test of the handler sees the newest-version branch.
Certificates on Linux
On Linux, the first check sets SSL_CERT_FILE and SSL_CERT_DIR with std::env::set_var when they are unset, while other threads of the application may read the environment. Setting both before App::run avoids that write.
Next
Packaging builds and signs the packages the endpoint points at. Testing without a window calls handlers like update under cargo test.