OpenblockDesktopUpdater.js
11.4 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import {app} from 'electron';
import {autoUpdater, CancellationToken} from 'electron-updater';
import log from 'electron-log';
import bytes from 'bytes';
import path from 'path';
import fetch from 'electron-fetch';
import formatMessage from 'format-message';
import parseReleaseMessage from 'openblock-parse-release-message';
import {UPDATE_TARGET, UPDATE_MODAL_STATE} from 'openblock-gui/src/lib/update-state.js';
import {AbortController} from 'node-abort-controller';
class OpenblockDesktopUpdater {
constructor (webContents, resourceServer) {
this._webContents = webContents;
this._resourceServer = resourceServer;
autoUpdater.autoDownload = false;
const appPath = app.getAppPath();
if (appPath.search(/main/g) !== -1) {
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
autoUpdater.updateConfigPath = path.join(appPath, '../win-unpacked/resources/app-update.yml');
}
this.updaterState = null;
this.updateTarget = null;
this.abortController = null;
this.cancellationToken = null;
}
removeAllAutoUpdaterListeners () {
autoUpdater.removeAllListeners('error');
autoUpdater.removeAllListeners('update-available');
autoUpdater.removeAllListeners('update-not-available');
}
reportUpdateState (state) {
this._webContents.send('setUpdate', state);
}
applicationAvailable (info) {
this.updateTarget = UPDATE_TARGET.application;
if (this.isCN) {
const url = `https://openblock.sgp1.digitaloceanspaces.com/desktop/latestRelease.json`;
fetch(url)
.then(res => res.json())
.then(data => {
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.applicationUpdateAvailable,
info: {
version: info.version,
message: parseReleaseMessage(data.body)
}
});
})
.catch(err => {
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.error,
info: {
message: err.message
}
});
});
} else {
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.applicationUpdateAvailable,
info: {
version: info.version,
message: parseReleaseMessage(info.releaseNotes, {html: true})
}
});
}
}
resourceAvailable (info) {
this.updateTarget = UPDATE_TARGET.resource;
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.resourceUpdateAvailable,
info: {
version: info.latestVersion,
message: info.message
}
});
}
checkUpdateAtStartup () {
autoUpdater.on('error', err => {
this.removeAllAutoUpdaterListeners();
console.warn(`Error while checking for application update: ${err}`);
});
autoUpdater.once('update-available', applicationUpdateInfo => {
this.removeAllAutoUpdaterListeners();
this.applicationAvailable(applicationUpdateInfo);
});
autoUpdater.once('update-not-available', () => {
this.removeAllAutoUpdaterListeners();
this._resourceServer.checkUpdate()
.then(resourceUpdateInfo => {
if (resourceUpdateInfo.updateble) {
this.resourceAvailable(resourceUpdateInfo);
}
})
.catch(err => {
console.warn(`Error while checking for resource update: ${err}`);
});
});
autoUpdater.checkForUpdates();
}
reqeustCheckUpdate () {
autoUpdater.on('error', err => {
this.removeAllAutoUpdaterListeners();
if (err.message === 'net::ERR_INTERNET_DISCONNECTED') {
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.error,
info: {
message: formatMessage({
id: 'index.internetDisconnectedError',
default: 'Internet disconnected, please verify your internet connection and try again.',
description: 'Error message of internet disconnected'
})
}
});
} else {
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.error,
info: {
message: err.message
}
});
}
});
autoUpdater.once('update-available', applicationUpdateInfo => {
this.updaterState = UPDATE_MODAL_STATE.applicationUpdateAvailable;
this.removeAllAutoUpdaterListeners();
this.applicationAvailable(applicationUpdateInfo);
});
autoUpdater.once('update-not-available', () => {
this.removeAllAutoUpdaterListeners();
this.abortController = new AbortController();
this._resourceServer.checkUpdate({signal: this.abortController.signal})
.then(resourceUpdateInfo => {
if (resourceUpdateInfo.updateble) {
this.updaterState = UPDATE_MODAL_STATE.resourceUpdateAvailable;
this.resourceAvailable(resourceUpdateInfo);
} else {
this.reportUpdateState({phase: 'latest'});
}
})
.catch(err => {
this.reportUpdateState({phase: 'error', message: err});
});
this.updaterState = UPDATE_MODAL_STATE.checkingResource;
});
autoUpdater.checkForUpdates();
this.updaterState = UPDATE_MODAL_STATE.checkingApplication;
}
reqeustUpdate () {
if (this.updateTarget === UPDATE_TARGET.application) {
this.cancellationToken = new CancellationToken();
autoUpdater.downloadUpdate(this.cancellationToken);
this.updaterState = UPDATE_MODAL_STATE.applicationDownloading;
const PROGRESS_BASE_VALUE = 0;
const PROGRESS_DOWNLOADING_PROGRESS_VALUE = 0.1;
const PROGRESS_STEP_INTERVAL = 0.5; // 0.5s
const PROGRESS_STEP_TIMEOUT = 20; // 20s
const PROGRESS_STEP_VALUE = (PROGRESS_DOWNLOADING_PROGRESS_VALUE - PROGRESS_BASE_VALUE) /
(PROGRESS_STEP_TIMEOUT / PROGRESS_STEP_INTERVAL);
let downloadInProgress = false;
const stepProgressBar = progress => {
this.startDownloadTimeout = setTimeout(() => {
if (!downloadInProgress && progress <= PROGRESS_DOWNLOADING_PROGRESS_VALUE) {
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.applicationDownloading,
info: {
progress: progress
}
});
stepProgressBar(progress + PROGRESS_STEP_VALUE);
} else {
this.startDownloadTimeout = null;
}
}, PROGRESS_STEP_INTERVAL * 1000);
};
// After start downloading, it takes a while for download-progress event to trigger,
// report a progress that grows slowly over time let user know the downloading is started and running.
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.applicationDownloading,
info: {
progress: PROGRESS_BASE_VALUE
}
});
stepProgressBar(PROGRESS_BASE_VALUE);
return new Promise((resolve, reject) => {
autoUpdater.on('error', err => reject(err));
autoUpdater.on('download-progress', progressObj => {
downloadInProgress = true;
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.applicationDownloading,
info: {
progress: ((progressObj.percent * (1 - PROGRESS_DOWNLOADING_PROGRESS_VALUE)) +
(PROGRESS_DOWNLOADING_PROGRESS_VALUE * 100)) / 100,
state: {
speed: `${bytes(progressObj.bytesPerSecond)}/s`,
total: bytes(progressObj.total),
done: bytes(progressObj.transferred)
}
}
});
});
autoUpdater.on('update-downloaded', () => {
this.reportUpdateState({phase: UPDATE_MODAL_STATE.applicationDownloadFinish});
setTimeout(() => {
console.log(`INFO: App will quit and install after 3 seconds`);
autoUpdater.quitAndInstall();
}, 1000 * 3);
});
});
}
const reportResourceUpdateState = res => {
if (this.updaterState !== UPDATE_MODAL_STATE.abort) {
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.resourceUpdating,
info: {
phase: res.phase,
progress: res.progress,
state: res.state
}
});
}
};
this.abortController = new AbortController();
this.updaterState = UPDATE_MODAL_STATE.resourceUpdating;
return this._resourceServer.update({
signal: this.abortController.signal,
callback: reportResourceUpdateState
})
.then(() => {
this.reportUpdateState({phase: UPDATE_MODAL_STATE.resourceUpdatFinish});
return Promise.resolve();
})
.catch(err => {
if (!err.stack.startsWith('AbortError')) {
this.reportUpdateState({
phase: UPDATE_MODAL_STATE.error,
info: {
message: err.message
}
});
}
return Promise.reject(err);
});
}
abortUpdate () {
if (this.updaterState === UPDATE_MODAL_STATE.checkingResource ||
this.updaterState === UPDATE_MODAL_STATE.resourceUpdating) {
this.updaterState = UPDATE_MODAL_STATE.abort;
this.abortController.abort();
} else if (this.updaterState === UPDATE_MODAL_STATE.checkingApplication) {
this.removeAllAutoUpdaterListeners();
} else if (this.updaterState === UPDATE_MODAL_STATE.applicationDownloading) {
this.removeAllAutoUpdaterListeners();
this.cancellationToken.cancel();
if (this.startDownloadTimeout) {
clearTimeout(this.startDownloadTimeout);
}
}
if (this.updaterState !== UPDATE_MODAL_STATE.abort) {
this.updaterState = null;
}
}
}
export default OpenblockDesktopUpdater;