Downloader.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  13. return new (P || (P = Promise))(function (resolve, reject) {
  14. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  15. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  16. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  17. step((generator = generator.apply(thisArg, _arguments || [])).next());
  18. });
  19. };
  20. var __generator = (this && this.__generator) || function (thisArg, body) {
  21. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  22. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  23. function verb(n) { return function (v) { return step([n, v]); }; }
  24. function step(op) {
  25. if (f) throw new TypeError("Generator is already executing.");
  26. while (_) try {
  27. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  28. if (y = 0, t) op = [op[0] & 2, t.value];
  29. switch (op[0]) {
  30. case 0: case 1: t = op; break;
  31. case 4: _.label++; return { value: op[1], done: false };
  32. case 5: _.label++; y = op[1]; op = [0]; continue;
  33. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  34. default:
  35. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  36. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  37. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  38. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  39. if (t[2]) _.ops.pop();
  40. _.trys.pop(); continue;
  41. }
  42. op = body.call(thisArg, _);
  43. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  44. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  45. }
  46. };
  47. Object.defineProperty(exports, "__esModule", { value: true });
  48. var path_1 = require("path");
  49. var debug = require('debug')('build:downloader');
  50. var DownloaderBase_1 = require("./common/DownloaderBase");
  51. var util_1 = require("./util");
  52. var Downloader = /** @class */ (function (_super) {
  53. __extends(Downloader, _super);
  54. function Downloader(options) {
  55. var _this = _super.call(this) || this;
  56. _this.options = util_1.mergeOptions(Downloader.DEFAULT_OPTIONS, options);
  57. if (_this.options.destination !== _this.destination) {
  58. _this.setDestination(_this.options.destination);
  59. }
  60. if (process.env.NWJS_MIRROR) {
  61. _this.options.mirror = process.env.NWJS_MIRROR;
  62. }
  63. debug('in constructor', 'options', _this.options);
  64. return _this;
  65. }
  66. Downloader.prototype.fetch = function () {
  67. return __awaiter(this, void 0, void 0, function () {
  68. var _a, mirror, platform, arch, version, flavor, showProgress, partVersion, partFlavor, partPlatform, partArch, partExtension, url, filename, path, _b, _c, err_1;
  69. return __generator(this, function (_d) {
  70. switch (_d.label) {
  71. case 0:
  72. _a = this.options, mirror = _a.mirror, platform = _a.platform, arch = _a.arch, version = _a.version, flavor = _a.flavor, showProgress = _a.showProgress;
  73. return [4 /*yield*/, this.handleVersion(version)];
  74. case 1:
  75. partVersion = _d.sent();
  76. partFlavor = flavor == 'normal' ? '' : '-' + flavor;
  77. partPlatform = this.handlePlatform(platform);
  78. partArch = this.handleArch(arch);
  79. partExtension = this.extensionByPlatform(platform);
  80. url = mirror + "/" + partVersion + "/nwjs" + partFlavor + "-" + partVersion + "-" + partPlatform + "-" + partArch + "." + partExtension;
  81. filename = path_1.basename(url);
  82. path = path_1.resolve(this.destination, filename);
  83. debug('in fetch', 'url', url);
  84. debug('in fetch', 'filename', filename);
  85. debug('in fetch', 'path', path);
  86. _b = this.options.forceCaches;
  87. if (!_b) return [3 /*break*/, 3];
  88. return [4 /*yield*/, this.isFileExists(path)];
  89. case 2:
  90. _b = (_d.sent());
  91. _d.label = 3;
  92. case 3:
  93. if (_b) {
  94. return [2 /*return*/, path];
  95. }
  96. _d.label = 4;
  97. case 4:
  98. _d.trys.push([4, 8, , 9]);
  99. return [4 /*yield*/, this.isFileExists(path)];
  100. case 5:
  101. _c = (_d.sent());
  102. if (!_c) return [3 /*break*/, 7];
  103. return [4 /*yield*/, this.isFileSynced(url, path)];
  104. case 6:
  105. _c = (_d.sent());
  106. _d.label = 7;
  107. case 7:
  108. if (_c) {
  109. return [2 /*return*/, path];
  110. }
  111. return [3 /*break*/, 9];
  112. case 8:
  113. err_1 = _d.sent();
  114. debug('in fetch', 'err', err_1);
  115. if (err_1.code === 'ENOTFOUND' && this.options.useCaches) {
  116. console.info('DNS lookup fails, use local caches at this time.');
  117. return [2 /*return*/, path];
  118. }
  119. else if (err_1.code === 'EAI_AGAIN' && this.options.useCaches) {
  120. console.info('DNS lookup timeout, use local caches at this time.');
  121. return [2 /*return*/, path];
  122. }
  123. else {
  124. throw err_1;
  125. }
  126. return [3 /*break*/, 9];
  127. case 9: return [4 /*yield*/, this.download(url, filename, path, showProgress)];
  128. case 10:
  129. _d.sent();
  130. return [2 /*return*/, path];
  131. }
  132. });
  133. });
  134. };
  135. Downloader.prototype.handleVersion = function (version) {
  136. return __awaiter(this, void 0, void 0, function () {
  137. var _a, versions;
  138. return __generator(this, function (_b) {
  139. switch (_b.label) {
  140. case 0:
  141. _a = version;
  142. switch (_a) {
  143. case 'latest': return [3 /*break*/, 1];
  144. case 'stable': return [3 /*break*/, 1];
  145. case 'lts': return [3 /*break*/, 1];
  146. }
  147. return [3 /*break*/, 3];
  148. case 1: return [4 /*yield*/, this.getVersions()];
  149. case 2:
  150. versions = _b.sent();
  151. //debug('in handleVersion', 'versions', versions);
  152. return [2 /*return*/, versions[version]];
  153. case 3: return [2 /*return*/, version[0] == 'v' ? version : 'v' + version];
  154. }
  155. });
  156. });
  157. };
  158. Downloader.prototype.extensionByPlatform = function (platform) {
  159. switch (platform) {
  160. case 'win32':
  161. case 'win':
  162. return 'zip';
  163. case 'darwin':
  164. case 'osx':
  165. case 'mac':
  166. return 'zip';
  167. case 'linux':
  168. return 'tar.gz';
  169. default:
  170. throw new Error('ERROR_UNKNOWN_PLATFORM');
  171. }
  172. };
  173. Downloader.DEFAULT_OPTIONS = {
  174. platform: process.platform,
  175. arch: process.arch,
  176. version: '0.14.7',
  177. flavor: 'normal',
  178. mirror: 'https://dl.nwjs.io/',
  179. useCaches: true,
  180. showProgress: true,
  181. forceCaches: false,
  182. destination: DownloaderBase_1.DownloaderBase.DEFAULT_DESTINATION,
  183. };
  184. return Downloader;
  185. }(DownloaderBase_1.DownloaderBase));
  186. exports.Downloader = Downloader;
  187. //# sourceMappingURL=Downloader.js.map