-
Notifications
You must be signed in to change notification settings - Fork 241
/
updater.ts
181 lines (160 loc) · 7.37 KB
/
updater.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright 2016-2021 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { app, autoUpdater, ipcMain } from "electron";
import fs from "node:fs/promises";
import { getSquirrelExecutable } from "./squirrelhooks";
const UPDATE_POLL_INTERVAL_MS = 60 * 60 * 1000;
const INITIAL_UPDATE_DELAY_MS = 30 * 1000;
function installUpdate(): void {
// for some reason, quitAndInstall does not fire the
// before-quit event, so we need to set the flag here.
global.appQuitting = true;
autoUpdater.quitAndInstall();
}
// Workaround for Squirrel.Mac wedging auto-restart if latest check for update failed
// From https://github.com/vector-im/element-web/issues/12433#issuecomment-1508995119
async function safeCheckForUpdate(): Promise<void> {
if (process.platform === "darwin") {
const feedUrl = autoUpdater.getFeedURL();
// On Mac if the user has already downloaded an update but not installed it and
// we check again and no additional new update is available the app ends up in a
// bad state and doesn't restart after installing any updates that are downloaded.
// To avoid this we check manually whether an update is available and call the
// autoUpdater.checkForUpdates() when something new is there.
try {
const res = await global.fetch(feedUrl);
const { currentRelease } = await res.json();
const latestVersionDownloaded = latestUpdateDownloaded?.releaseName;
console.info(
`Latest version from release download: ${currentRelease} (current: ${app.getVersion()}, most recent downloaded ${latestVersionDownloaded}})`,
);
if (currentRelease === app.getVersion() || currentRelease === latestVersionDownloaded) {
ipcChannelSendUpdateStatus(false);
return;
}
} catch (err) {
console.error(`Error checking for updates ${feedUrl}`, err);
ipcChannelSendUpdateStatus(false);
return;
}
}
autoUpdater.checkForUpdates();
}
async function pollForUpdates(): Promise<void> {
try {
// If we've already got a new update downloaded, then stop trying to check for new ones, as according to the doc
// at https://github.com/electron/electron/blob/main/docs/api/auto-updater.md#autoupdatercheckforupdates
// we'll just keep re-downloading the same update.
// As a hunch, this might also be causing https://github.com/vector-im/element-web/issues/12433
// due to the update checks colliding with the pending install somehow
if (!latestUpdateDownloaded) {
await safeCheckForUpdate();
} else {
console.log("Skipping update check as download already present");
global.mainWindow?.webContents.send("update-downloaded", latestUpdateDownloaded);
}
} catch (e) {
console.log("Couldn't check for update", e);
}
}
export async function start(updateBaseUrl: string): Promise<void> {
if (!(await available(updateBaseUrl))) return;
if (updateBaseUrl.slice(-1) !== "/") {
updateBaseUrl = updateBaseUrl + "/";
}
try {
let url: string;
let serverType: "json" | undefined;
if (process.platform === "darwin") {
// On macOS it takes a JSON file with a map between versions and their URLs
url = `${updateBaseUrl}macos/releases.json`;
serverType = "json";
} else if (process.platform === "win32") {
// On windows it takes a base path and looks for files under that path.
url = `${updateBaseUrl}win32/${process.arch}/`;
} else {
// Squirrel / electron only supports auto-update on these two platforms.
// I'm not even going to try to guess which feed style they'd use if they
// implemented it on Linux, or if it would be different again.
return;
}
if (url) {
console.log(`Update URL: ${url}`);
autoUpdater.setFeedURL({ url, serverType });
// We check for updates ourselves rather than using 'updater' because we need to
// do it in the main process (and we don't really need to check every 10 minutes:
// every hour should be just fine for a desktop app)
// However, we still let the main window listen for the update events.
// We also wait a short time before checking for updates the first time because
// of squirrel on windows and it taking a small amount of time to release a
// lock file.
setTimeout(pollForUpdates, INITIAL_UPDATE_DELAY_MS);
setInterval(pollForUpdates, UPDATE_POLL_INTERVAL_MS);
}
} catch (err) {
// will fail if running in debug mode
console.log("Couldn't enable update checking", err);
}
}
async function available(updateBaseUrl?: string): Promise<boolean> {
if (process.platform === "linux") {
// Auto update is not supported on Linux
console.log("Auto update not supported on this platform");
return false;
}
if (process.platform === "win32") {
try {
await fs.access(getSquirrelExecutable());
} catch {
console.log("Squirrel not found, auto update not supported");
return false;
}
}
// Otherwise we're either on macOS or Windows with Squirrel
return !!updateBaseUrl;
}
ipcMain.on("install_update", installUpdate);
ipcMain.on("check_updates", pollForUpdates);
function ipcChannelSendUpdateStatus(status: boolean | string): void {
global.mainWindow?.webContents.send("check_updates", status);
}
interface ICachedUpdate {
releaseNotes: string;
releaseName: string;
releaseDate: Date;
updateURL: string;
}
// cache the latest update which has been downloaded as electron offers no api to read it
let latestUpdateDownloaded: ICachedUpdate | undefined;
autoUpdater
.on("update-available", function () {
ipcChannelSendUpdateStatus(true);
})
.on("update-not-available", function () {
if (latestUpdateDownloaded) {
// the only time we will get `update-not-available` if `latestUpdateDownloaded` is already set
// is if the user used the Manual Update check and there is no update newer than the one we
// have downloaded, so show it to them as the latest again.
global.mainWindow?.webContents.send("update-downloaded", latestUpdateDownloaded);
} else {
ipcChannelSendUpdateStatus(false);
}
})
.on("error", function (error) {
ipcChannelSendUpdateStatus(error.message);
});
autoUpdater.on("update-downloaded", (ev, releaseNotes, releaseName, releaseDate, updateURL) => {
// forward to renderer
latestUpdateDownloaded = { releaseNotes, releaseName, releaseDate, updateURL };
global.mainWindow?.webContents.send("update-downloaded", latestUpdateDownloaded);
});