Skip to content

Commit 43224c5

Browse files
feat(updater): add download and install js binding (#1330)
1 parent f3749e4 commit 43224c5

14 files changed

Lines changed: 197 additions & 40 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"updater": "patch"
3+
"updater-js": "patch"
4+
---
5+
6+
Add `Update.download` and `Update.install` functions to the JavaScript API

examples/api/src-tauri/gen/schemas/desktop-schema.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5927,27 +5927,55 @@
59275927
"updater:allow-check"
59285928
]
59295929
},
5930+
{
5931+
"description": "updater:allow-download -> Enables the download command without any pre-configured scope.",
5932+
"type": "string",
5933+
"enum": [
5934+
"updater:allow-download"
5935+
]
5936+
},
59305937
{
59315938
"description": "updater:allow-download-and-install -> Enables the download_and_install command without any pre-configured scope.",
59325939
"type": "string",
59335940
"enum": [
59345941
"updater:allow-download-and-install"
59355942
]
59365943
},
5944+
{
5945+
"description": "updater:allow-install -> Enables the install command without any pre-configured scope.",
5946+
"type": "string",
5947+
"enum": [
5948+
"updater:allow-install"
5949+
]
5950+
},
59375951
{
59385952
"description": "updater:deny-check -> Denies the check command without any pre-configured scope.",
59395953
"type": "string",
59405954
"enum": [
59415955
"updater:deny-check"
59425956
]
59435957
},
5958+
{
5959+
"description": "updater:deny-download -> Denies the download command without any pre-configured scope.",
5960+
"type": "string",
5961+
"enum": [
5962+
"updater:deny-download"
5963+
]
5964+
},
59445965
{
59455966
"description": "updater:deny-download-and-install -> Denies the download_and_install command without any pre-configured scope.",
59465967
"type": "string",
59475968
"enum": [
59485969
"updater:deny-download-and-install"
59495970
]
59505971
},
5972+
{
5973+
"description": "updater:deny-install -> Denies the install command without any pre-configured scope.",
5974+
"type": "string",
5975+
"enum": [
5976+
"updater:deny-install"
5977+
]
5978+
},
59515979
{
59525980
"description": "webview:default -> Default permissions for the plugin.",
59535981
"type": "string",

plugins/updater/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/updater/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
const COMMANDS: &[&str] = &["check", "download_and_install"];
5+
const COMMANDS: &[&str] = &["check", "download", "install", "download_and_install"];
66

77
fn main() {
88
tauri_plugin::Builder::new(COMMANDS)

plugins/updater/guest-js/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Update extends Resource {
4545
version: string;
4646
date?: string;
4747
body?: string;
48+
private downloadedBytes?: Resource;
4849

4950
constructor(metadata: UpdateMetadata) {
5051
super(metadata.rid);
@@ -55,6 +56,34 @@ class Update extends Resource {
5556
this.body = metadata.body;
5657
}
5758

59+
/** Download the updater package */
60+
async download(onEvent?: (progress: DownloadEvent) => void): Promise<void> {
61+
const channel = new Channel<DownloadEvent>();
62+
if (onEvent) {
63+
channel.onmessage = onEvent;
64+
}
65+
const downloadedBytesRid = await invoke<number>("plugin:updater|download", {
66+
onEvent: channel,
67+
rid: this.rid,
68+
});
69+
this.downloadedBytes = new Resource(downloadedBytesRid);
70+
}
71+
72+
/** Install downloaded updater package */
73+
async install(): Promise<void> {
74+
if (!this.downloadedBytes) {
75+
throw "Update.install called before Update.download";
76+
}
77+
78+
await invoke("plugin:updater|install", {
79+
updateRid: this.rid,
80+
bytesRid: this.downloadedBytes.rid,
81+
});
82+
83+
// Don't need to call close, we did it in rust side already
84+
this.downloadedBytes = undefined;
85+
}
86+
5887
/** Downloads the updater package and installs it */
5988
async downloadAndInstall(
6089
onEvent?: (progress: DownloadEvent) => void,
@@ -68,6 +97,11 @@ class Update extends Resource {
6897
rid: this.rid,
6998
});
7099
}
100+
101+
async close(): Promise<void> {
102+
await this.downloadedBytes?.close();
103+
await super.close();
104+
}
71105
}
72106

73107
/** Check for updates, resolves to `null` if no updates are available */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Automatically generated - DO NOT EDIT!
2+
3+
"$schema" = "../../schemas/schema.json"
4+
5+
[[permission]]
6+
identifier = "allow-download"
7+
description = "Enables the download command without any pre-configured scope."
8+
commands.allow = ["download"]
9+
10+
[[permission]]
11+
identifier = "deny-download"
12+
description = "Denies the download command without any pre-configured scope."
13+
commands.deny = ["download"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Automatically generated - DO NOT EDIT!
2+
3+
"$schema" = "../../schemas/schema.json"
4+
5+
[[permission]]
6+
identifier = "allow-install"
7+
description = "Enables the install command without any pre-configured scope."
8+
commands.allow = ["install"]
9+
10+
[[permission]]
11+
identifier = "deny-install"
12+
description = "Denies the install command without any pre-configured scope."
13+
commands.deny = ["install"]

plugins/updater/permissions/autogenerated/reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
|------|-----|
33
|`allow-check`|Enables the check command without any pre-configured scope.|
44
|`deny-check`|Denies the check command without any pre-configured scope.|
5+
|`allow-download`|Enables the download command without any pre-configured scope.|
6+
|`deny-download`|Denies the download command without any pre-configured scope.|
57
|`allow-download-and-install`|Enables the download_and_install command without any pre-configured scope.|
68
|`deny-download-and-install`|Denies the download_and_install command without any pre-configured scope.|
9+
|`allow-install`|Enables the install command without any pre-configured scope.|
10+
|`deny-install`|Denies the install command without any pre-configured scope.|
711
|`default`|Allows checking for new updates and installing them|
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
"$schema" = "schemas/schema.json"
22
[default]
33
description = "Allows checking for new updates and installing them"
4-
permissions = ["allow-check", "allow-download-and-install"]
4+
permissions = [
5+
"allow-check",
6+
"allow-download",
7+
"allow-install",
8+
"allow-download-and-install",
9+
]

plugins/updater/permissions/schemas/schema.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,20 @@
308308
"deny-check"
309309
]
310310
},
311+
{
312+
"description": "allow-download -> Enables the download command without any pre-configured scope.",
313+
"type": "string",
314+
"enum": [
315+
"allow-download"
316+
]
317+
},
318+
{
319+
"description": "deny-download -> Denies the download command without any pre-configured scope.",
320+
"type": "string",
321+
"enum": [
322+
"deny-download"
323+
]
324+
},
311325
{
312326
"description": "allow-download-and-install -> Enables the download_and_install command without any pre-configured scope.",
313327
"type": "string",
@@ -322,6 +336,20 @@
322336
"deny-download-and-install"
323337
]
324338
},
339+
{
340+
"description": "allow-install -> Enables the install command without any pre-configured scope.",
341+
"type": "string",
342+
"enum": [
343+
"allow-install"
344+
]
345+
},
346+
{
347+
"description": "deny-install -> Denies the install command without any pre-configured scope.",
348+
"type": "string",
349+
"enum": [
350+
"deny-install"
351+
]
352+
},
325353
{
326354
"description": "default -> Allows checking for new updates and installing them",
327355
"type": "string",

0 commit comments

Comments
 (0)