forked from glamrock/cryptocat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
updater.js
306 lines (250 loc) · 8.72 KB
/
updater.js
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
296
297
298
299
300
301
302
303
304
305
306
// node module that checks if a newer version of Cryptocat is available
'use strict';
// requires
var EventEmitter = require('events').EventEmitter
var util = require('util')
var NodeWebkitUpdater = require('node-webkit-updater')
var verifySignature = require('./verifySignature')
var pkg = require('../package.json')
// private vars
var nodeWebkitUpdater = new NodeWebkitUpdater(pkg) // instance of node-webkit-updater
var copyPath, execPath // node-webkit-updater vars
var initialized = false // flag if updater has already been initialized once
var running = false // flag if updater is currently doing something.
var remoteManifest = false // manifest gets stored here privately if update was found
var downloadedFilename = false // filename gets stored here privately if update was downloaded
// borrowed from node-webkit-updater to detect platform (app/updater.js)!
// see https://github.com/edjafarov/node-webkit-updater/blob/master/app/updater.js
var platform = process.platform;
platform = /^win/.test(platform) ? 'win' : /^darwin/.test(platform) ? 'mac' : 'linux' + (process.arch === 'ia32' ? '32' : '64')
// -----------------------------------------------------------------------------
// Constructor of Updater
// -----------------------------------------------------------------------------
var Updater = function() {
this.args = []
}
util.inherits(Updater, EventEmitter)
// -----------------------------------------------------------------------------
// Private functions (module scoped)
// -----------------------------------------------------------------------------
// func gets passed as callback in last step of finishUpdate
// restarts application (needs gui reference to node-webkit to work)
function executeNewVersionAfterUpdateFinished(gui) {
if (process.platform === 'win32') {
// fix restart on windows (https://github.com/cryptocat/cryptocat/issues/566#issuecomment-61947221)
gui.Shell.openItem(execPath)
gui.App.quit()
} else {
nodeWebkitUpdater.run(execPath, null)
gui.App.quit()
}
}
// -----------------------------------------------------------------------------
// Public methods of Updater prototype
// -----------------------------------------------------------------------------
// initializes the updater object
// gui.App.argv (args the app was started with) must be handed over to initialize Updater
Updater.prototype.init = function(args) {
// immediately exit if we are already initialized
if (initialized) {
return false
}
initialized = true
this.args = args || this.args
if (args.length) {
this.finishUpdate()
} else {
this.checkUpdateAvailable()
}
}
Updater.prototype.checkUpdateAvailable = function() {
var self = this
// immediately exit if we are already running
if (running) {
return false
}
running = true
self.emit('checkingForUpdate')
nodeWebkitUpdater.checkNewVersion(function(error, newVersionExists, manifest) {
if (error) {
// e.g. error connecting to the remote .json file
self.emit('error', {
description: 'Error during checkUpdateAvailable (nodeWebkitUpdater.checkNewVersion).',
stack: error.stack,
retryCallback: self.checkUpdateAvailable
})
running = false
return false
}
if (newVersionExists === false) {
// we are running the most up to date version of cryptocat
self.emit('noUpdateAvailable', {
remoteVersion: manifest.version
})
running = false
return false
}
// if we are still here, then there is a newer version available!
// save the manifest (prerequisite for download to run)
remoteManifest = manifest
self.emit('updateAvailable', {
remoteVersion: manifest.version
})
running = false
})
}
Updater.prototype.downloadUpdate = function() {
var self = this
// immediately exit if we are already running (or privates manifest are not set!)
if (running || remoteManifest === false) {
return false
}
running = true
var sizeLoaded = 0
var fileData = nodeWebkitUpdater.download(function(error, filename) {
if (error) {
// e.g. connection error
self.emit('error', {
description: 'Error during downloadUpdate (nodeWebkitUpdater.download).',
stack: error.stack,
retryCallback: self.downloadUpdate
})
running = false
return false
}
// if we are still here, then the file was properly downloaded.
// let's verify it's dsa signature!
self.emit('verifyingSignature')
// try {
verifySignature.verifyFileSignatureAsync(filename, remoteManifest.packages[platform].dsa, function(val) {
if (val === false) {
// OMG, this update is not signed properly!
self.emit('error', {
description: 'Error: DSA signature is not valid!',
stack: 'filename: ' + filename + ' - remoteManifest: ' + remoteManifest,
retryCallback: self.downloadUpdate
})
running = false
return false
} else {
// clear to install!
// save the filename (prerequisite for unpacking and install to run)
downloadedFilename = filename
self.emit('downloadedUpdate', {
remoteVersion: remoteManifest.version,
filename: filename
})
running = false
}
})
// if (verifySignature.verifyFileSignature(filename, remoteManifest.packages[platform].dsa) === false) {
// // OMG, this update is not signed properly!
// self.emit('error', {
// description: 'Error: DSA signature is not valid!',
// stack: 'filename: ' + filename + ' - remoteManifest: ' + remoteManifest,
// retryCallback: self.downloadUpdate
// })
// running = false
// return false
// } else {
// // clear to install!
// // save the filename (prerequisite for unpacking and install to run)
// downloadedFilename = filename
// self.emit('downloadedUpdate', {
// remoteVersion: remoteManifest.version,
// filename: filename
// })
// running = false
// }
// } catch (e) {
// self.emit('error', {
// description: 'Error during verifyingSignature!',
// stack: e.stack,
// retryCallback: self.downloadUpdate
// })
// running = false
// return false
// }
}, remoteManifest)
// hock to the fileData (the progressing download to emit events on status)
fileData.on('data', function(chunk) {
sizeLoaded += chunk.length
self.emit('downloadProgress', {
loaded: sizeLoaded,
size: fileData['content-length'],
percentage: Math.floor(sizeLoaded / fileData['content-length'] * 100),
remoteVersion: remoteManifest.version
})
})
}
Updater.prototype.installUpdate = function() {
var self = this
// immediately exit if we are already running (or privates manifest and filename are not set!)
if (running || remoteManifest === false || downloadedFilename === false) {
return false
}
running = true
// let's unpack it...
nodeWebkitUpdater.unpack(downloadedFilename, function(error, newAppPath) {
if (error) {
// error during unpacking the file
self.emit('error', {
description: 'Error during installUpdate (nodeWebkitUpdater.unpack).',
stack: error.stack,
retryCallback: self.installUpdate
})
running = false
return false
}
self.emit('unpackingFinished', {
remoteVersion: remoteManifest.version,
filename: downloadedFilename
})
// running the installer
nodeWebkitUpdater.runInstaller(newAppPath, [nodeWebkitUpdater.getAppPath(), nodeWebkitUpdater.getAppExec()], {})
self.emit('preInstallationFinished')
// done, this application can now be closed.
running = false
}, remoteManifest)
}
Updater.prototype.finishUpdate = function() {
var self = this
// immediately exit if we are already running
if (running) {
return false
}
running = true
// The new app (in temp) will copy itself to the original folder, overwriting the old app.
// Replace old app, Run updated app from original location and close temp instance
copyPath = self.args[0]
execPath = self.args[1]
self.emit('installingUpdate')
nodeWebkitUpdater.install(copyPath, function(error) {
if (error) {
self.emit('error', {
description: 'Error during finishUpdate (nodeWebkitUpdater.install).',
stack: error.stack,
retryCallback: self.installUpdate
})
} else {
// Old version was successfully replaced, application can now be safely restarted.
// emit and pass callback reference to observer (who has gui object) to execute restart
self.emit('updateFinished', {
execute: executeNewVersionAfterUpdateFinished
})
}
running = false
})
}
Updater.prototype.getSavedRemoteVersion = function() {
if (remoteManifest !== false) {
return remoteManifest.version
}
console.error('Tried to get saved remote version without having updated version available!')
return false
}
// -----------------------------------------------------------------------------
// Module Export
// -----------------------------------------------------------------------------
var updater = new Updater()
module.exports = updater