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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
'use strict'
const arch = require('./arch')
const debug = require('debug')('electron-download')
const envPaths = require('env-paths')
const fs = require('fs-extra')
const rc = require('rc')
const nugget = require('nugget')
const os = require('os')
const path = require('path')
const pathExists = require('path-exists')
const semver = require('semver')
const sumchecker = require('sumchecker')
class ElectronDownloader {
constructor (opts) {
this.opts = Object.assign({ autoDownload: true }, opts)
if (this.opts.force && !this.opts.autoDownload) {
throw new Error('force and autoDownload options are incompatible for Electron Download')
}
this.npmrc = {}
try {
rc('npm', this.npmrc)
} catch (error) {
console.error(`Error reading npm configuration: ${error.message}`)
}
}
get baseUrl () {
if (this.version.indexOf('nightly') !== -1) {
return process.env.NPM_CONFIG_ELECTRON_NIGHTLY_MIRROR ||
process.env.npm_config_electron_nightly_mirror ||
process.env.npm_package_config_electron_nightly_mirror ||
process.env.ELECTRON_NIGHTLY_MIRROR ||
this.opts.nightly_mirror ||
'https://github.com/electron/nightlies/releases/download/v'
}
return process.env.NPM_CONFIG_ELECTRON_MIRROR ||
process.env.npm_config_electron_mirror ||
process.env.npm_package_config_electron_mirror ||
process.env.ELECTRON_MIRROR ||
this.opts.mirror ||
'https://github.com/electron/electron/releases/download/v'
}
get middleUrl () {
return process.env.NPM_CONFIG_ELECTRON_CUSTOM_DIR ||
process.env.npm_config_electron_custom_dir ||
process.env.npm_package_config_electron_custom_dir ||
process.env.ELECTRON_CUSTOM_DIR ||
this.opts.customDir ||
this.version
}
get urlSuffix () {
return process.env.NPM_CONFIG_ELECTRON_CUSTOM_FILENAME ||
process.env.npm_config_electron_custom_filename ||
process.env.npm_package_config_electron_custom_filename ||
process.env.ELECTRON_CUSTOM_FILENAME ||
this.opts.customFilename ||
this.filename
}
get arch () {
return this.opts.arch || arch.host(this.quiet)
}
get cache () {
const cacheLocation = this.opts.cache || process.env.ELECTRON_CACHE
if (cacheLocation) return cacheLocation
const oldCacheDirectory = path.join(os.homedir(), './.electron')
if (pathExists.sync(path.join(oldCacheDirectory, this.filename))) {
return oldCacheDirectory
}
// use passed argument or XDG environment variable fallback to OS default
return envPaths('electron', {suffix: ''}).cache
}
get cachedChecksum () {
return path.join(this.cache, `${this.checksumFilename}-${this.version}`)
}
get cachedZip () {
return path.join(this.cache, this.filename)
}
get checksumFilename () {
return 'SHASUMS256.txt'
}
get checksumUrl () {
return `${this.baseUrl}${this.middleUrl}/${this.checksumFilename}`
}
get filename () {
const type = `${this.platform}-${this.arch}`
const suffix = `v${this.version}-${type}`
if (this.chromedriver) {
// Chromedriver started using Electron's version in asset name in 1.7.0
if (semver.gte(this.version, '1.7.0')) {
return `chromedriver-${suffix}.zip`
} else {
return `chromedriver-v2.21-${type}.zip`
}
} else if (this.mksnapshot) {
return `mksnapshot-${suffix}.zip`
} else if (this.ffmpeg) {
return `ffmpeg-${suffix}.zip`
} else if (this.symbols) {
return `electron-${suffix}-symbols.zip`
} else if (this.dsym) {
return `electron-${suffix}-dsym.zip`
} else {
return `electron-${suffix}.zip`
}
}
get platform () {
return this.opts.platform || os.platform()
}
get proxy () {
let proxy
if (this.npmrc && this.npmrc.proxy) proxy = this.npmrc.proxy
if (this.npmrc && this.npmrc['https-proxy']) proxy = this.npmrc['https-proxy']
return proxy
}
get quiet () {
return this.opts.quiet || process.stdout.rows < 1
}
get strictSSL () {
let strictSSL = true
if (this.opts.strictSSL === false || this.npmrc['strict-ssl'] === false) {
strictSSL = false
}
return strictSSL
}
get force () {
return this.opts.force || false
}
get symbols () {
return this.opts.symbols || false
}
get dsym () {
return this.opts.dsym || false
}
get chromedriver () {
return this.opts.chromedriver || false
}
get mksnapshot () {
return this.opts.mksnapshot || false
}
get ffmpeg () {
return this.opts.ffmpeg || false
}
get url () {
return process.env.ELECTRON_DOWNLOAD_OVERRIDE_URL ||
`${this.baseUrl}${this.middleUrl}/${this.urlSuffix}`
}
get verifyChecksumNeeded () {
return !this.opts.disableChecksumSafetyCheck && semver.gte(this.version, '1.3.2')
}
get version () {
return this.opts.version
}
get headers () {
return this.opts.headers
}
checkForCachedChecksum (cb) {
pathExists(this.cachedChecksum)
.then(exists => {
if (exists && !this.force) {
this.verifyChecksum(cb)
} else {
this.downloadChecksum(cb)
}
})
}
checkForCachedZip (cb) {
pathExists(this.cachedZip).then(exists => {
if (exists && !this.force) {
debug('zip exists', this.cachedZip)
this.checkIfZipNeedsVerifying(cb)
} else if (this.opts.autoDownload) {
this.ensureCacheDir(cb)
} else {
cb(new Error(`File: "${this.cachedZip}" does not exist locally and autoDownload is false`))
}
})
}
checkIfZipNeedsVerifying (cb) {
if (this.verifyChecksumNeeded) {
debug('Verifying zip with checksum')
return this.checkForCachedChecksum(cb)
}
return cb(null, this.cachedZip)
}
createCacheDir (cb) {
fs.mkdirs(this.cache, (err) => {
if (err) {
if (err.code !== 'EACCES') return cb(err)
// try local folder if homedir is off limits (e.g. some linuxes return '/' as homedir)
const localCache = path.resolve('./.electron')
return fs.mkdirs(localCache, function (err) {
if (err) return cb(err)
cb(null, localCache)
})
}
cb(null, this.cache)
})
}
downloadChecksum (cb) {
this.downloadFile(this.checksumUrl, this.cachedChecksum, cb, this.verifyChecksum.bind(this))
}
downloadFile (url, cacheFilename, cb, onSuccess) {
const tempFileName = `tmp-${process.pid}-${(ElectronDownloader.tmpFileCounter++).toString(16)}-${path.basename(cacheFilename)}`
debug('downloading', url, 'to', this.cache)
const nuggetOpts = {
target: tempFileName,
dir: this.cache,
resume: true,
quiet: this.quiet,
strictSSL: this.strictSSL,
proxy: this.proxy,
headers: this.headers
}
nugget(url, nuggetOpts, (errors) => {
if (errors) {
// nugget returns an array of errors but we only need 1st because we only have 1 url
return this.handleDownloadError(cb, errors[0])
}
this.moveFileToCache(tempFileName, cacheFilename, cb, onSuccess)
})
}
downloadIfNotCached (cb) {
if (!this.version) return cb(new Error('must specify version'))
debug('info', {cache: this.cache, filename: this.filename, url: this.url})
this.checkForCachedZip(cb)
}
downloadZip (cb) {
this.downloadFile(this.url, this.cachedZip, cb, this.checkIfZipNeedsVerifying.bind(this))
}
ensureCacheDir (cb) {
debug('creating cache dir')
this.createCacheDir((err, actualCache) => {
if (err) return cb(err)
this.opts.cache = actualCache // in case cache dir changed
this.downloadZip(cb)
})
}
handleDownloadError (cb, error) {
if (error.message.indexOf('404') === -1) return cb(error)
if (this.symbols) {
error.message = `Failed to find Electron symbols v${this.version} for ${this.platform}-${this.arch} at ${this.url}`
} else {
error.message = `Failed to find Electron v${this.version} for ${this.platform}-${this.arch} at ${this.url}`
}
return cb(error)
}
moveFileToCache (filename, target, cb, onSuccess) {
const cache = this.cache
debug('moving', filename, 'from', cache, 'to', target)
fs.rename(path.join(cache, filename), target, (err) => {
if (err) {
fs.unlink(cache, cleanupError => {
try {
if (cleanupError) {
console.error(`Error deleting cache file: ${cleanupError.message}`)
}
} finally {
cb(err)
}
})
} else {
onSuccess(cb)
}
})
}
verifyChecksum (cb) {
const options = {}
if (semver.lt(this.version, '1.3.5')) {
options.defaultTextEncoding = 'binary'
}
const checker = new sumchecker.ChecksumValidator('sha256', this.cachedChecksum, options)
checker.validate(this.cache, this.filename).then(() => {
cb(null, this.cachedZip)
}, (err) => {
fs.unlink(this.cachedZip, (fsErr) => {
if (fsErr) return cb(fsErr)
cb(err)
})
})
}
}
ElectronDownloader.tmpFileCounter = 0
module.exports = function download (opts, cb) {
try {
const downloader = new ElectronDownloader(opts)
downloader.downloadIfNotCached(cb)
} catch (err) {
cb(err)
}
}
|