Live video on the AT Protocol
1import electron from "electron";
2import ms from "ms";
3import {
4 IUpdateElectronAppOptions,
5 UpdateSourceType,
6} from "update-electron-app";
7import env from "./env";
8import * as build from "./version";
9
10const supportedPlatforms = ["darwin", "win32"];
11
12export default function () {
13 initElectronUpdater({
14 updateSource: {
15 type: UpdateSourceType.StaticStorage,
16 baseUrl: `${env().updateBaseUrl}/api/desktop-updates`,
17 },
18 notifyUser: true,
19 updateInterval: "10 minutes",
20 });
21}
22
23export function initElectronUpdater(opts: IUpdateElectronAppOptions) {
24 const { updateSource, updateInterval } = opts;
25
26 // exit early on unsupported platforms, e.g. `linux`
27 if (!supportedPlatforms.includes(process?.platform)) {
28 log(
29 `Electron's autoUpdater does not support the '${process.platform}' platform. Ref: https://www.electronjs.org/docs/latest/api/auto-updater#platform-notices`,
30 );
31 return;
32 }
33
34 const { app, autoUpdater, dialog } = electron;
35 let platform: string = process.platform;
36 let arch: string = process.arch;
37 if (platform === "win32") {
38 platform = "windows";
39 }
40 if (arch === "x64") {
41 arch = "amd64";
42 }
43 let feedURL: string;
44 let serverType: "default" | "json" = "default";
45 const electronVersion = app.getVersion();
46 switch (updateSource.type) {
47 case UpdateSourceType.StaticStorage: {
48 feedURL = `${updateSource.baseUrl}/${platform}/${arch}/${electronVersion}/${build.buildTime}`;
49
50 if (platform === "darwin") {
51 feedURL += "/RELEASES.json";
52 serverType = "json";
53 }
54 break;
55 }
56 }
57
58 const requestHeaders = {
59 "User-Agent": `streamplace-desktop/${build.version}`,
60 };
61
62 function log(...args: any[]) {
63 console.log(...args);
64 }
65
66 log("feedURL", feedURL);
67 log("requestHeaders", requestHeaders);
68 autoUpdater.setFeedURL({
69 url: feedURL,
70 headers: requestHeaders,
71 serverType,
72 });
73
74 autoUpdater.on("error", (err) => {
75 log("updater error");
76 log(err);
77 });
78
79 autoUpdater.on("checking-for-update", () => {
80 log("checking-for-update");
81 });
82
83 autoUpdater.on("update-available", () => {
84 log("update-available; downloading...");
85 });
86
87 autoUpdater.on("update-not-available", () => {
88 log("update-not-available");
89 });
90
91 if (opts.notifyUser) {
92 autoUpdater.on(
93 "update-downloaded",
94 (event, releaseNotes, releaseName, releaseDate, updateURL) => {
95 log("update-downloaded", [
96 event,
97 releaseNotes,
98 releaseName,
99 releaseDate,
100 updateURL,
101 ]);
102
103 const dialogOpts: Electron.MessageBoxOptions = {
104 type: "info",
105 buttons: ["Restart", "Later"],
106 title: "Application Update",
107 message: platform === "windows" ? releaseNotes : releaseName,
108 detail:
109 "A new version has been downloaded. Restart the application to apply the updates.",
110 };
111
112 dialog.showMessageBox(dialogOpts).then(({ response }) => {
113 if (response === 0) autoUpdater.quitAndInstall();
114 });
115 },
116 );
117 }
118
119 // check for updates right away and keep checking later
120 autoUpdater.checkForUpdates();
121 setInterval(() => {
122 autoUpdater.checkForUpdates();
123 }, ms(updateInterval));
124}