forked from trezor/trezor-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-updater.ts
276 lines (230 loc) · 9.49 KB
/
auto-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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { unlinkSync } from 'fs';
import {
autoUpdater,
CancellationToken,
UpdateInfo,
UpdateDownloadedEvent,
ProgressInfo,
} from 'electron-updater';
import { bytesToHumanReadable } from '@trezor/utils';
import { isFeatureFlagEnabled, isDevEnv } from '@suite-common/suite-utils';
import { app, ipcMain } from '../typed-electron';
import { b2t } from '../libs/utils';
import { verifySignature } from '../libs/update-checker';
import { getReleaseNotes } from '../libs/github';
import { Module } from './index';
// Runtime flags
const enableUpdater = app.commandLine.hasSwitch('enable-updater');
const disableUpdater = app.commandLine.hasSwitch('disable-updater');
const preReleaseFlag = app.commandLine.hasSwitch('pre-release');
const feedURL = app.commandLine.getSwitchValue('updater-url');
export const init: Module = ({ mainWindow, store }) => {
const { logger } = global;
if (!isFeatureFlagEnabled('DESKTOP_AUTO_UPDATER') && !enableUpdater) {
logger.info('auto-updater', 'Disabled via feature flag');
return;
}
if (isFeatureFlagEnabled('DESKTOP_AUTO_UPDATER') && disableUpdater) {
logger.info('auto-updater', 'Disabled via command line parameter');
return;
}
// If APPIMAGE is not set on Linux, the auto updater can't handle that
if (process.platform === 'linux' && process.env.APPIMAGE === undefined && !isDevEnv) {
logger.warn('auto-updater', 'APPIMAGE is not defined, skipping auto updater');
return;
}
let isManualCheck = false;
let updateCancellationToken: CancellationToken;
let errorHappened = false;
// Prevent downloading an update unless user explicitly asks for it.
autoUpdater.autoDownload = false;
const updateSettings = store.getUpdateSettings();
autoUpdater.allowPrerelease = preReleaseFlag || updateSettings.allowPrerelease;
autoUpdater.logger = null;
if (feedURL) {
autoUpdater.setFeedURL(feedURL);
logger.warn('auto-updater', [`Feed url: ${feedURL}`]);
}
logger.info(
'auto-updater',
`Is looking for pre-releases? (${b2t(autoUpdater.allowPrerelease)})`,
);
autoUpdater.on('checking-for-update', () => {
logger.info('auto-updater', 'Checking for update');
mainWindow.webContents.send('update/checking');
});
autoUpdater.on(
'update-available',
async ({ version, releaseDate, releaseNotes }: UpdateInfo) => {
let release;
try {
release = feedURL
? { prerelease: false, body: releaseNotes?.toString() }
: await getReleaseNotes(version);
} catch (error) {
logger.error('auto-updater', 'Fetching release notes failed!');
} finally {
logger.warn('auto-updater', [
'Update is available:',
`- Update version: ${version}`,
`- Prerelease: ${release?.prerelease}`,
`- Changelog: ${release?.body ? 'available' : 'unavailable'}`,
`- Release date: ${releaseDate}`,
`- Manual check: ${b2t(isManualCheck)}`,
]);
mainWindow.webContents.send('update/available', {
version,
releaseDate,
isManualCheck,
prerelease: release?.prerelease,
changelog: release?.body,
});
// Reset manual check flag
isManualCheck = false;
}
},
);
autoUpdater.on('update-not-available', ({ version, releaseDate }: UpdateInfo) => {
logger.info('auto-updater', [
'No new update is available:',
`- Last version: ${version}`,
`- Last release date: ${releaseDate}`,
`- Manual check: ${b2t(isManualCheck)}`,
]);
mainWindow.webContents.send('update/not-available', {
version,
releaseDate,
isManualCheck,
});
// Reset manual check flag
isManualCheck = false;
});
autoUpdater.on('error', (err: Error) => {
errorHappened = true;
logger.error('auto-updater', `An error happened: ${err.toString()}`);
mainWindow.webContents.send('update/error', err);
});
autoUpdater.on('download-progress', (progressObj: ProgressInfo) => {
logger.debug(
'auto-updater',
`Downloading ${progressObj.percent}% (${bytesToHumanReadable(
progressObj.transferred,
)}/${bytesToHumanReadable(progressObj.total)})`,
);
mainWindow.webContents.send('update/downloading', progressObj);
});
autoUpdater.on('update-downloaded', async (info: UpdateDownloadedEvent) => {
const { version, releaseDate, downloadedFile, releaseNotes } = info;
if (errorHappened) {
logger.info('auto-updater', 'An error happened. Stopping auto-update.');
return;
}
logger.info('auto-updater', [
'Update downloaded:',
`- Last version: ${version}`,
`- Last release date: ${releaseDate}`,
`- Downloaded file: ${downloadedFile}`,
`- Release notes: ${releaseNotes}`,
]);
mainWindow.webContents.send('update/downloading', { verifying: true });
try {
// check downloaded file
await verifySignature({
version,
downloadedFile,
feedURL,
});
logger.info('auto-updater', 'Signature of update file is valid');
mainWindow.webContents.send('update/downloaded', {
version,
releaseDate,
downloadedFile,
});
} catch (err) {
autoUpdater.autoInstallOnAppQuit = false;
unlinkSync(downloadedFile);
mainWindow.webContents.send('update/error', err);
logger.error('auto-updater', `Signature check of update file failed: ${err.message}`);
logger.info('auto-updater', `Unlink downloaded file ${downloadedFile}`);
}
logger.info(
'auto-updater',
`Is configured to auto update after app quit? ${autoUpdater.autoInstallOnAppQuit}`,
);
});
ipcMain.on('update/check', (_, isManual) => {
if (isManual) {
isManualCheck = true;
}
logger.info('auto-updater', `Update checking request (manual: ${b2t(isManualCheck)})`);
autoUpdater.checkForUpdates();
});
ipcMain.on('update/download', async () => {
logger.info('auto-updater', 'Download requested');
mainWindow.webContents.send('update/downloading', {
percent: 0,
bytesPerSecond: 0,
total: 0,
transferred: 0,
});
updateCancellationToken = new CancellationToken();
try {
await autoUpdater.downloadUpdate(updateCancellationToken);
logger.info('auto-updater', 'Update downloaded');
} catch {
logger.info('auto-updater', 'Update cancelled');
}
});
ipcMain.on('update/install', () => {
logger.info('auto-updater', 'Restart and update request');
setImmediate(() => {
// Removing listeners & closing window (https://github.com/electron-userland/electron-builder/issues/1604)
app.removeAllListeners('window-all-closed');
mainWindow.removeAllListeners('close');
mainWindow.close();
// Silent install on Windows to match on "Update on quit" and MacOS behavior
autoUpdater.quitAndInstall(true, true);
});
});
ipcMain.on('update/cancel', () => {
logger.info(
'auto-updater',
`Cancel update request (in progress: ${b2t(!!updateCancellationToken)})`,
);
if (updateCancellationToken) {
updateCancellationToken.cancel();
}
});
ipcMain.on('update/allow-prerelease', (_, value = true) => {
logger.info('auto-updater', `${value ? 'allow' : 'disable'} prerelease!`);
mainWindow.webContents.send('update/allow-prerelease', value);
const settings = store.getUpdateSettings();
store.setUpdateSettings({ ...settings, allowPrerelease: value });
autoUpdater.allowPrerelease = value;
});
// Enable feature on FE once it's ready
return () => {
// if there is savedCurrentVersion in store (it doesn't have to be there as it was added in later versions)
// and if it does not match current application version it means that application got updated and the new version
// is run for the first time.
const settings = store.getUpdateSettings();
const { savedCurrentVersion } = settings;
const currentVersion = app.getVersion();
logger.debug(
'auto-updater',
`Version of application before this launch: ${savedCurrentVersion}, current app version: ${currentVersion}`,
);
// save current app version so that after app is relaunched we can show info about transition to the new version
store.setUpdateSettings({
...updateSettings,
savedCurrentVersion: currentVersion,
});
return {
allowPrerelease: autoUpdater.allowPrerelease,
firstRun:
savedCurrentVersion && savedCurrentVersion !== currentVersion
? currentVersion
: undefined,
};
};
};