Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / dist / lib / Downloader.js
root on 7 May 2019 8 KB Initial commit
  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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
  28. if (y = 0, t) op = [0, 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 (process.env.NWJS_MIRROR) {
  58. _this.options.mirror = process.env.NWJS_MIRROR;
  59. }
  60. debug('in constructor', 'options', _this.options);
  61. return _this;
  62. }
  63. Downloader.prototype.fetch = function () {
  64. return __awaiter(this, void 0, void 0, function () {
  65. var _a, mirror, platform, arch, version, flavor, showProgress, partVersion, partFlavor, partPlatform, partArch, partExtension, url, filename, path, _b, err_1;
  66. return __generator(this, function (_c) {
  67. switch (_c.label) {
  68. case 0:
  69. _a = this.options, mirror = _a.mirror, platform = _a.platform, arch = _a.arch, version = _a.version, flavor = _a.flavor, showProgress = _a.showProgress;
  70. return [4 /*yield*/, this.handleVersion(version)];
  71. case 1:
  72. partVersion = _c.sent();
  73. partFlavor = flavor == 'normal' ? '' : '-' + flavor;
  74. partPlatform = this.handlePlatform(platform);
  75. partArch = this.handleArch(arch);
  76. partExtension = this.extensionByPlatform(platform);
  77. url = mirror + "/" + partVersion + "/nwjs" + partFlavor + "-" + partVersion + "-" + partPlatform + "-" + partArch + "." + partExtension;
  78. filename = path_1.basename(url);
  79. path = path_1.resolve(this.destination, filename);
  80. debug('in fetch', 'url', url);
  81. debug('in fetch', 'filename', filename);
  82. debug('in fetch', 'path', path);
  83. _c.label = 2;
  84. case 2:
  85. _c.trys.push([2, 6, , 7]);
  86. return [4 /*yield*/, this.isFileExists(path)];
  87. case 3:
  88. _b = (_c.sent());
  89. if (!_b) return [3 /*break*/, 5];
  90. return [4 /*yield*/, this.isFileSynced(url, path)];
  91. case 4:
  92. _b = (_c.sent());
  93. _c.label = 5;
  94. case 5:
  95. if (_b) {
  96. return [2 /*return*/, path];
  97. }
  98. return [3 /*break*/, 7];
  99. case 6:
  100. err_1 = _c.sent();
  101. debug('in fetch', 'err', err_1);
  102. if (err_1.code === 'ENOTFOUND' && this.options.useCaches) {
  103. console.info('DNS lookup fails, use local caches at this time.');
  104. return [2 /*return*/, path];
  105. }
  106. else if (err_1.code === 'EAI_AGAIN' && this.options.useCaches) {
  107. console.info('DNS lookup timeout, use local caches at this time.');
  108. return [2 /*return*/, path];
  109. }
  110. else {
  111. throw err_1;
  112. }
  113. return [3 /*break*/, 7];
  114. case 7: return [4 /*yield*/, this.download(url, filename, path, showProgress)];
  115. case 8:
  116. _c.sent();
  117. return [2 /*return*/, path];
  118. }
  119. });
  120. });
  121. };
  122. Downloader.prototype.handleVersion = function (version) {
  123. return __awaiter(this, void 0, void 0, function () {
  124. var _a, versions;
  125. return __generator(this, function (_b) {
  126. switch (_b.label) {
  127. case 0:
  128. _a = version;
  129. switch (_a) {
  130. case 'latest': return [3 /*break*/, 1];
  131. case 'stable': return [3 /*break*/, 1];
  132. case 'lts': return [3 /*break*/, 1];
  133. }
  134. return [3 /*break*/, 3];
  135. case 1: return [4 /*yield*/, this.getVersions()];
  136. case 2:
  137. versions = _b.sent();
  138. //debug('in handleVersion', 'versions', versions);
  139. return [2 /*return*/, versions[version]];
  140. case 3: return [2 /*return*/, version[0] == 'v' ? version : 'v' + version];
  141. }
  142. });
  143. });
  144. };
  145. Downloader.prototype.extensionByPlatform = function (platform) {
  146. switch (platform) {
  147. case 'win32':
  148. case 'win':
  149. return 'zip';
  150. case 'darwin':
  151. case 'osx':
  152. case 'mac':
  153. return 'zip';
  154. case 'linux':
  155. return 'tar.gz';
  156. default:
  157. throw new Error('ERROR_UNKNOWN_PLATFORM');
  158. }
  159. };
  160. Downloader.DEFAULT_OPTIONS = {
  161. platform: process.platform,
  162. arch: process.arch,
  163. version: '0.14.7',
  164. flavor: 'normal',
  165. mirror: 'https://dl.nwjs.io/',
  166. useCaches: true,
  167. showProgress: true,
  168. };
  169. return Downloader;
  170. }(DownloaderBase_1.DownloaderBase));
  171. exports.Downloader = Downloader;
  172. //# sourceMappingURL=Downloader.js.map
Buy Me A Coffee