index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. var debug = require('debug')('electron-download');
  6. var fs = require('fs-extra');
  7. var homePath = require('home-path');
  8. var rc = require('rc');
  9. var nugget = require('nugget');
  10. var os = require('os');
  11. var path = require('path');
  12. var pathExists = require('path-exists');
  13. var semver = require('semver');
  14. var sumchecker = require('sumchecker');
  15. var ElectronDownloader = function () {
  16. function ElectronDownloader(opts) {
  17. _classCallCheck(this, ElectronDownloader);
  18. this.opts = opts;
  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. _createClass(ElectronDownloader, [{
  27. key: 'checkForCachedChecksum',
  28. value: function checkForCachedChecksum(cb) {
  29. var _this = this;
  30. pathExists(this.cachedChecksum).then(function (exists) {
  31. if (exists && !_this.force) {
  32. _this.verifyChecksum(cb);
  33. } else if (_this.tmpdir) {
  34. _this.downloadChecksum(cb);
  35. } else {
  36. _this.createTempDir(cb, function (callback) {
  37. _this.downloadChecksum(callback);
  38. });
  39. }
  40. });
  41. }
  42. }, {
  43. key: 'checkForCachedZip',
  44. value: function checkForCachedZip(cb) {
  45. var _this2 = this;
  46. pathExists(this.cachedZip).then(function (exists) {
  47. if (exists && !_this2.force) {
  48. debug('zip exists', _this2.cachedZip);
  49. _this2.checkIfZipNeedsVerifying(cb);
  50. } else {
  51. _this2.ensureCacheDir(cb);
  52. }
  53. });
  54. }
  55. }, {
  56. key: 'checkIfZipNeedsVerifying',
  57. value: function checkIfZipNeedsVerifying(cb) {
  58. if (this.verifyChecksumNeeded) {
  59. debug('Verifying zip with checksum');
  60. return this.checkForCachedChecksum(cb);
  61. }
  62. return cb(null, this.cachedZip);
  63. }
  64. }, {
  65. key: 'createCacheDir',
  66. value: function createCacheDir(cb) {
  67. var _this3 = this;
  68. fs.mkdirs(this.cache, function (err) {
  69. if (err) {
  70. var _ret = function () {
  71. if (err.code !== 'EACCES') return {
  72. v: cb(err)
  73. };
  74. // try local folder if homedir is off limits (e.g. some linuxes return '/' as homedir)
  75. var localCache = path.resolve('./.electron');
  76. return {
  77. v: fs.mkdirs(localCache, function (err) {
  78. if (err) return cb(err);
  79. cb(null, localCache);
  80. })
  81. };
  82. }();
  83. if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
  84. }
  85. cb(null, _this3.cache);
  86. });
  87. }
  88. }, {
  89. key: 'createTempDir',
  90. value: function createTempDir(cb, onSuccess) {
  91. this.tmpdir = path.join(os.tmpdir(), 'electron-tmp-download-' + process.pid + '-' + Date.now());
  92. fs.mkdirs(this.tmpdir, function (err) {
  93. if (err) return cb(err);
  94. onSuccess(cb);
  95. });
  96. }
  97. }, {
  98. key: 'downloadChecksum',
  99. value: function downloadChecksum(cb) {
  100. this.downloadFile(this.checksumUrl, this.checksumFilename, this.cachedChecksum, cb, this.verifyChecksum.bind(this));
  101. }
  102. }, {
  103. key: 'downloadFile',
  104. value: function downloadFile(url, filename, cacheFilename, cb, onSuccess) {
  105. var _this4 = this;
  106. debug('downloading', url, 'to', this.tmpdir);
  107. var nuggetOpts = {
  108. target: filename,
  109. dir: this.tmpdir,
  110. resume: true,
  111. quiet: this.quiet,
  112. strictSSL: this.strictSSL,
  113. proxy: this.proxy
  114. };
  115. nugget(url, nuggetOpts, function (errors) {
  116. if (errors) {
  117. // nugget returns an array of errors but we only need 1st because we only have 1 url
  118. return _this4.handleDownloadError(cb, errors[0]);
  119. }
  120. _this4.moveFileToCache(filename, cacheFilename, cb, onSuccess);
  121. });
  122. }
  123. }, {
  124. key: 'downloadIfNotCached',
  125. value: function downloadIfNotCached(cb) {
  126. if (!this.version) return cb(new Error('must specify version'));
  127. debug('info', { cache: this.cache, filename: this.filename, url: this.url });
  128. this.checkForCachedZip(cb);
  129. }
  130. }, {
  131. key: 'downloadZip',
  132. value: function downloadZip(cb) {
  133. this.downloadFile(this.url, this.filename, this.cachedZip, cb, this.checkIfZipNeedsVerifying.bind(this));
  134. }
  135. }, {
  136. key: 'ensureCacheDir',
  137. value: function ensureCacheDir(cb) {
  138. var _this5 = this;
  139. debug('creating cache/tmp dirs');
  140. this.createCacheDir(function (err, actualCache) {
  141. if (err) return cb(err);
  142. _this5.opts.cache = actualCache; // in case cache dir changed
  143. _this5.createTempDir(cb, _this5.downloadZip.bind(_this5));
  144. });
  145. }
  146. }, {
  147. key: 'handleDownloadError',
  148. value: function handleDownloadError(cb, error) {
  149. if (error.message.indexOf('404') === -1) return cb(error);
  150. if (this.symbols) {
  151. error.message = 'Failed to find Electron symbols v' + this.version + ' for ' + this.platform + '-' + this.arch + ' at ' + this.url;
  152. } else {
  153. error.message = 'Failed to find Electron v' + this.version + ' for ' + this.platform + '-' + this.arch + ' at ' + this.url;
  154. }
  155. return cb(error);
  156. }
  157. }, {
  158. key: 'moveFileToCache',
  159. value: function moveFileToCache(filename, target, cb, onSuccess) {
  160. var _this6 = this;
  161. debug('moving', filename, 'from', this.tmpdir, 'to', target);
  162. fs.unlink(target, function (err) {
  163. if (err != null && err.code !== 'ENOENT') return cb(err);
  164. fs.move(path.join(_this6.tmpdir, filename), target, function (err) {
  165. if (err) return cb(err);
  166. onSuccess(cb);
  167. });
  168. });
  169. }
  170. }, {
  171. key: 'verifyChecksum',
  172. value: function verifyChecksum(cb) {
  173. var _this7 = this;
  174. var options = {};
  175. if (semver.lt(this.version, '1.3.5')) {
  176. options.defaultTextEncoding = 'binary';
  177. }
  178. var checker = new sumchecker.ChecksumValidator('sha256', this.cachedChecksum, options);
  179. checker.validate(this.cache, this.filename).then(function () {
  180. cb(null, _this7.cachedZip);
  181. }, function (err) {
  182. fs.unlink(_this7.cachedZip, function (fsErr) {
  183. if (fsErr) return cb(fsErr);
  184. cb(err);
  185. });
  186. });
  187. }
  188. }, {
  189. key: 'baseUrl',
  190. get: function get() {
  191. return process.env.NPM_CONFIG_ELECTRON_MIRROR || process.env.npm_config_electron_mirror || process.env.ELECTRON_MIRROR || this.opts.mirror || 'https://github.com/electron/electron/releases/download/v';
  192. }
  193. }, {
  194. key: 'middleUrl',
  195. get: function get() {
  196. return process.env.ELECTRON_CUSTOM_DIR || this.opts.customDir || this.version;
  197. }
  198. }, {
  199. key: 'urlSuffix',
  200. get: function get() {
  201. return process.env.ELECTRON_CUSTOM_FILENAME || this.opts.customFilename || this.filename;
  202. }
  203. }, {
  204. key: 'arch',
  205. get: function get() {
  206. return this.opts.arch || os.arch();
  207. }
  208. }, {
  209. key: 'cache',
  210. get: function get() {
  211. return this.opts.cache || path.join(homePath(), './.electron');
  212. }
  213. }, {
  214. key: 'cachedChecksum',
  215. get: function get() {
  216. return path.join(this.cache, this.checksumFilename + '-' + this.version);
  217. }
  218. }, {
  219. key: 'cachedZip',
  220. get: function get() {
  221. return path.join(this.cache, this.filename);
  222. }
  223. }, {
  224. key: 'checksumFilename',
  225. get: function get() {
  226. return 'SHASUMS256.txt';
  227. }
  228. }, {
  229. key: 'checksumUrl',
  230. get: function get() {
  231. return '' + this.baseUrl + this.middleUrl + '/' + this.checksumFilename;
  232. }
  233. }, {
  234. key: 'filename',
  235. get: function get() {
  236. var type = this.platform + '-' + this.arch;
  237. var suffix = 'v' + this.version + '-' + type;
  238. if (this.chromedriver) {
  239. return 'chromedriver-v2.21-' + type + '.zip';
  240. } else if (this.mksnapshot) {
  241. return 'mksnapshot-' + suffix + '.zip';
  242. } else if (this.ffmpeg) {
  243. return 'ffmpeg-' + suffix + '.zip';
  244. } else if (this.symbols) {
  245. return 'electron-' + suffix + '-symbols.zip';
  246. } else if (this.dsym) {
  247. return 'electron-' + suffix + '-dsym.zip';
  248. } else {
  249. return 'electron-' + suffix + '.zip';
  250. }
  251. }
  252. }, {
  253. key: 'platform',
  254. get: function get() {
  255. return this.opts.platform || os.platform();
  256. }
  257. }, {
  258. key: 'proxy',
  259. get: function get() {
  260. var proxy = void 0;
  261. if (this.npmrc && this.npmrc.proxy) proxy = this.npmrc.proxy;
  262. if (this.npmrc && this.npmrc['https-proxy']) proxy = this.npmrc['https-proxy'];
  263. return proxy;
  264. }
  265. }, {
  266. key: 'quiet',
  267. get: function get() {
  268. return this.opts.quiet || process.stdout.rows < 1;
  269. }
  270. }, {
  271. key: 'strictSSL',
  272. get: function get() {
  273. var strictSSL = true;
  274. if (this.opts.strictSSL === false || this.npmrc['strict-ssl'] === false) {
  275. strictSSL = false;
  276. }
  277. return strictSSL;
  278. }
  279. }, {
  280. key: 'force',
  281. get: function get() {
  282. return this.opts.force || false;
  283. }
  284. }, {
  285. key: 'symbols',
  286. get: function get() {
  287. return this.opts.symbols || false;
  288. }
  289. }, {
  290. key: 'dsym',
  291. get: function get() {
  292. return this.opts.dsym || false;
  293. }
  294. }, {
  295. key: 'chromedriver',
  296. get: function get() {
  297. return this.opts.chromedriver || false;
  298. }
  299. }, {
  300. key: 'mksnapshot',
  301. get: function get() {
  302. return this.opts.mksnapshot || false;
  303. }
  304. }, {
  305. key: 'ffmpeg',
  306. get: function get() {
  307. return this.opts.ffmpeg || false;
  308. }
  309. }, {
  310. key: 'url',
  311. get: function get() {
  312. return '' + this.baseUrl + this.middleUrl + '/' + this.urlSuffix;
  313. }
  314. }, {
  315. key: 'verifyChecksumNeeded',
  316. get: function get() {
  317. return semver.gte(this.version, '1.3.2');
  318. }
  319. }, {
  320. key: 'version',
  321. get: function get() {
  322. return this.opts.version;
  323. }
  324. }]);
  325. return ElectronDownloader;
  326. }();
  327. module.exports = function download(opts, cb) {
  328. var downloader = new ElectronDownloader(opts);
  329. downloader.downloadIfNotCached(cb);
  330. };