index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. 'use strict'
  2. const arch = require('./arch')
  3. const debug = require('debug')('electron-download')
  4. const envPaths = require('env-paths')
  5. const fs = require('fs-extra')
  6. const rc = require('rc')
  7. const nugget = require('nugget')
  8. const os = require('os')
  9. const path = require('path')
  10. const pathExists = require('path-exists')
  11. const semver = require('semver')
  12. const sumchecker = require('sumchecker')
  13. class ElectronDownloader {
  14. constructor (opts) {
  15. this.opts = Object.assign({ autoDownload: true }, opts)
  16. if (this.opts.force && !this.opts.autoDownload) {
  17. throw new Error('force and autoDownload options are incompatible for Electron Download')
  18. }
  19. this.npmrc = {}
  20. try {
  21. rc('npm', this.npmrc)
  22. } catch (error) {
  23. console.error(`Error reading npm configuration: ${error.message}`)
  24. }
  25. }
  26. get baseUrl () {
  27. if (this.version.indexOf('nightly') !== -1) {
  28. return process.env.NPM_CONFIG_ELECTRON_NIGHTLY_MIRROR ||
  29. process.env.npm_config_electron_nightly_mirror ||
  30. process.env.npm_package_config_electron_nightly_mirror ||
  31. process.env.ELECTRON_NIGHTLY_MIRROR ||
  32. this.opts.nightly_mirror ||
  33. 'https://github.com/electron/nightlies/releases/download/v'
  34. }
  35. return process.env.NPM_CONFIG_ELECTRON_MIRROR ||
  36. process.env.npm_config_electron_mirror ||
  37. process.env.npm_package_config_electron_mirror ||
  38. process.env.ELECTRON_MIRROR ||
  39. this.opts.mirror ||
  40. 'https://github.com/electron/electron/releases/download/v'
  41. }
  42. get middleUrl () {
  43. return process.env.NPM_CONFIG_ELECTRON_CUSTOM_DIR ||
  44. process.env.npm_config_electron_custom_dir ||
  45. process.env.npm_package_config_electron_custom_dir ||
  46. process.env.ELECTRON_CUSTOM_DIR ||
  47. this.opts.customDir ||
  48. this.version
  49. }
  50. get urlSuffix () {
  51. return process.env.NPM_CONFIG_ELECTRON_CUSTOM_FILENAME ||
  52. process.env.npm_config_electron_custom_filename ||
  53. process.env.npm_package_config_electron_custom_filename ||
  54. process.env.ELECTRON_CUSTOM_FILENAME ||
  55. this.opts.customFilename ||
  56. this.filename
  57. }
  58. get arch () {
  59. return this.opts.arch || arch.host(this.quiet)
  60. }
  61. get cache () {
  62. const cacheLocation = this.opts.cache || process.env.ELECTRON_CACHE
  63. if (cacheLocation) return cacheLocation
  64. const oldCacheDirectory = path.join(os.homedir(), './.electron')
  65. if (pathExists.sync(path.join(oldCacheDirectory, this.filename))) {
  66. return oldCacheDirectory
  67. }
  68. // use passed argument or XDG environment variable fallback to OS default
  69. return envPaths('electron', {suffix: ''}).cache
  70. }
  71. get cachedChecksum () {
  72. return path.join(this.cache, `${this.checksumFilename}-${this.version}`)
  73. }
  74. get cachedZip () {
  75. return path.join(this.cache, this.filename)
  76. }
  77. get checksumFilename () {
  78. return 'SHASUMS256.txt'
  79. }
  80. get checksumUrl () {
  81. return `${this.baseUrl}${this.middleUrl}/${this.checksumFilename}`
  82. }
  83. get filename () {
  84. const type = `${this.platform}-${this.arch}`
  85. const suffix = `v${this.version}-${type}`
  86. if (this.chromedriver) {
  87. // Chromedriver started using Electron's version in asset name in 1.7.0
  88. if (semver.gte(this.version, '1.7.0')) {
  89. return `chromedriver-${suffix}.zip`
  90. } else {
  91. return `chromedriver-v2.21-${type}.zip`
  92. }
  93. } else if (this.mksnapshot) {
  94. return `mksnapshot-${suffix}.zip`
  95. } else if (this.ffmpeg) {
  96. return `ffmpeg-${suffix}.zip`
  97. } else if (this.symbols) {
  98. return `electron-${suffix}-symbols.zip`
  99. } else if (this.dsym) {
  100. return `electron-${suffix}-dsym.zip`
  101. } else {
  102. return `electron-${suffix}.zip`
  103. }
  104. }
  105. get platform () {
  106. return this.opts.platform || os.platform()
  107. }
  108. get proxy () {
  109. let proxy
  110. if (this.npmrc && this.npmrc.proxy) proxy = this.npmrc.proxy
  111. if (this.npmrc && this.npmrc['https-proxy']) proxy = this.npmrc['https-proxy']
  112. return proxy
  113. }
  114. get quiet () {
  115. return this.opts.quiet || process.stdout.rows < 1
  116. }
  117. get strictSSL () {
  118. let strictSSL = true
  119. if (this.opts.strictSSL === false || this.npmrc['strict-ssl'] === false) {
  120. strictSSL = false
  121. }
  122. return strictSSL
  123. }
  124. get force () {
  125. return this.opts.force || false
  126. }
  127. get symbols () {
  128. return this.opts.symbols || false
  129. }
  130. get dsym () {
  131. return this.opts.dsym || false
  132. }
  133. get chromedriver () {
  134. return this.opts.chromedriver || false
  135. }
  136. get mksnapshot () {
  137. return this.opts.mksnapshot || false
  138. }
  139. get ffmpeg () {
  140. return this.opts.ffmpeg || false
  141. }
  142. get url () {
  143. return process.env.ELECTRON_DOWNLOAD_OVERRIDE_URL ||
  144. `${this.baseUrl}${this.middleUrl}/${this.urlSuffix}`
  145. }
  146. get verifyChecksumNeeded () {
  147. return !this.opts.disableChecksumSafetyCheck && semver.gte(this.version, '1.3.2')
  148. }
  149. get version () {
  150. return this.opts.version
  151. }
  152. get headers () {
  153. return this.opts.headers
  154. }
  155. checkForCachedChecksum (cb) {
  156. pathExists(this.cachedChecksum)
  157. .then(exists => {
  158. if (exists && !this.force) {
  159. this.verifyChecksum(cb)
  160. } else {
  161. this.downloadChecksum(cb)
  162. }
  163. })
  164. }
  165. checkForCachedZip (cb) {
  166. pathExists(this.cachedZip).then(exists => {
  167. if (exists && !this.force) {
  168. debug('zip exists', this.cachedZip)
  169. this.checkIfZipNeedsVerifying(cb)
  170. } else if (this.opts.autoDownload) {
  171. this.ensureCacheDir(cb)
  172. } else {
  173. cb(new Error(`File: "${this.cachedZip}" does not exist locally and autoDownload is false`))
  174. }
  175. })
  176. }
  177. checkIfZipNeedsVerifying (cb) {
  178. if (this.verifyChecksumNeeded) {
  179. debug('Verifying zip with checksum')
  180. return this.checkForCachedChecksum(cb)
  181. }
  182. return cb(null, this.cachedZip)
  183. }
  184. createCacheDir (cb) {
  185. fs.mkdirs(this.cache, (err) => {
  186. if (err) {
  187. if (err.code !== 'EACCES') return cb(err)
  188. // try local folder if homedir is off limits (e.g. some linuxes return '/' as homedir)
  189. const localCache = path.resolve('./.electron')
  190. return fs.mkdirs(localCache, function (err) {
  191. if (err) return cb(err)
  192. cb(null, localCache)
  193. })
  194. }
  195. cb(null, this.cache)
  196. })
  197. }
  198. downloadChecksum (cb) {
  199. this.downloadFile(this.checksumUrl, this.cachedChecksum, cb, this.verifyChecksum.bind(this))
  200. }
  201. downloadFile (url, cacheFilename, cb, onSuccess) {
  202. const tempFileName = `tmp-${process.pid}-${(ElectronDownloader.tmpFileCounter++).toString(16)}-${path.basename(cacheFilename)}`
  203. debug('downloading', url, 'to', this.cache)
  204. const nuggetOpts = {
  205. target: tempFileName,
  206. dir: this.cache,
  207. resume: true,
  208. quiet: this.quiet,
  209. strictSSL: this.strictSSL,
  210. proxy: this.proxy,
  211. headers: this.headers
  212. }
  213. nugget(url, nuggetOpts, (errors) => {
  214. if (errors) {
  215. // nugget returns an array of errors but we only need 1st because we only have 1 url
  216. return this.handleDownloadError(cb, errors[0])
  217. }
  218. this.moveFileToCache(tempFileName, cacheFilename, cb, onSuccess)
  219. })
  220. }
  221. downloadIfNotCached (cb) {
  222. if (!this.version) return cb(new Error('must specify version'))
  223. debug('info', {cache: this.cache, filename: this.filename, url: this.url})
  224. this.checkForCachedZip(cb)
  225. }
  226. downloadZip (cb) {
  227. this.downloadFile(this.url, this.cachedZip, cb, this.checkIfZipNeedsVerifying.bind(this))
  228. }
  229. ensureCacheDir (cb) {
  230. debug('creating cache dir')
  231. this.createCacheDir((err, actualCache) => {
  232. if (err) return cb(err)
  233. this.opts.cache = actualCache // in case cache dir changed
  234. this.downloadZip(cb)
  235. })
  236. }
  237. handleDownloadError (cb, error) {
  238. if (error.message.indexOf('404') === -1) return cb(error)
  239. if (this.symbols) {
  240. error.message = `Failed to find Electron symbols v${this.version} for ${this.platform}-${this.arch} at ${this.url}`
  241. } else {
  242. error.message = `Failed to find Electron v${this.version} for ${this.platform}-${this.arch} at ${this.url}`
  243. }
  244. return cb(error)
  245. }
  246. moveFileToCache (filename, target, cb, onSuccess) {
  247. const cache = this.cache
  248. debug('moving', filename, 'from', cache, 'to', target)
  249. fs.rename(path.join(cache, filename), target, (err) => {
  250. if (err) {
  251. fs.unlink(cache, cleanupError => {
  252. try {
  253. if (cleanupError) {
  254. console.error(`Error deleting cache file: ${cleanupError.message}`)
  255. }
  256. } finally {
  257. cb(err)
  258. }
  259. })
  260. } else {
  261. onSuccess(cb)
  262. }
  263. })
  264. }
  265. verifyChecksum (cb) {
  266. const options = {}
  267. if (semver.lt(this.version, '1.3.5')) {
  268. options.defaultTextEncoding = 'binary'
  269. }
  270. const checker = new sumchecker.ChecksumValidator('sha256', this.cachedChecksum, options)
  271. checker.validate(this.cache, this.filename).then(() => {
  272. cb(null, this.cachedZip)
  273. }, (err) => {
  274. fs.unlink(this.cachedZip, (fsErr) => {
  275. if (fsErr) return cb(fsErr)
  276. cb(err)
  277. })
  278. })
  279. }
  280. }
  281. ElectronDownloader.tmpFileCounter = 0
  282. module.exports = function download (opts, cb) {
  283. try {
  284. const downloader = new ElectronDownloader(opts)
  285. downloader.downloadIfNotCached(cb)
  286. } catch (err) {
  287. cb(err)
  288. }
  289. }