Newer
Older
CVSS_3.0_GUI / dist / CVSS_3.0_Calc-1.0.0-linux-x64 / node_modules / nwjs-builder-phoenix / dist / lib / common / DownloaderBase.js
root on 7 May 2019 9 KB Initial commit
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (_) try {
  17. if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [0, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. Object.defineProperty(exports, "__esModule", { value: true });
  38. var path_1 = require("path");
  39. var request = require("request");
  40. var ProgressBar = require("progress");
  41. var fs_extra_1 = require("fs-extra");
  42. var debug = require('debug')('build:downloader');
  43. var progress = require('request-progress');
  44. var Event_1 = require("./Event");
  45. var util_1 = require("../util");
  46. var DIR_CACHES = path_1.resolve(path_1.dirname(module.filename), '..', '..', '..', 'caches');
  47. fs_extra_1.ensureDirSync(DIR_CACHES);
  48. var DownloaderBase = /** @class */ (function () {
  49. function DownloaderBase() {
  50. this.onProgress = new Event_1.Event('progress');
  51. this.destination = DIR_CACHES;
  52. }
  53. DownloaderBase.prototype.fetchAndExtract = function () {
  54. return __awaiter(this, void 0, void 0, function () {
  55. var archive, dest;
  56. return __generator(this, function (_a) {
  57. switch (_a.label) {
  58. case 0: return [4 /*yield*/, this.fetch()];
  59. case 1:
  60. archive = _a.sent();
  61. dest = archive + "-extracted";
  62. return [4 /*yield*/, util_1.extractGeneric(archive, dest)];
  63. case 2:
  64. _a.sent();
  65. return [2 /*return*/, dest];
  66. }
  67. });
  68. });
  69. };
  70. DownloaderBase.prototype.getVersions = function () {
  71. return new Promise(function (resolve, reject) {
  72. request('https://nwjs.io/versions.json', function (err, res, body) {
  73. if (err) {
  74. return reject(err);
  75. }
  76. var json = JSON.parse(body);
  77. resolve(json);
  78. });
  79. });
  80. };
  81. DownloaderBase.prototype.setDestination = function (destination) {
  82. this.destination = destination;
  83. };
  84. DownloaderBase.prototype.handlePlatform = function (platform) {
  85. switch (platform) {
  86. case 'win32':
  87. case 'win':
  88. return 'win';
  89. case 'darwin':
  90. case 'osx':
  91. case 'mac':
  92. return 'osx';
  93. case 'linux':
  94. return 'linux';
  95. default:
  96. throw new Error('ERROR_UNKNOWN_PLATFORM');
  97. }
  98. };
  99. DownloaderBase.prototype.handleArch = function (arch) {
  100. switch (arch) {
  101. case 'x86':
  102. case 'ia32':
  103. return 'ia32';
  104. case 'x64':
  105. return 'x64';
  106. default:
  107. throw new Error('ERROR_UNKNOWN_PLATFORM');
  108. }
  109. };
  110. DownloaderBase.prototype.getLocalSize = function (path) {
  111. return fs_extra_1.lstat(path)
  112. .then(function (stat) { return stat.size; });
  113. };
  114. DownloaderBase.prototype.getRemoteSize = function (url) {
  115. return new Promise(function (resolve, reject) {
  116. request.head(url)
  117. .on('error', reject)
  118. .on('response', function (res) { return resolve(parseInt((res.headers['content-length']), 10)); });
  119. });
  120. };
  121. DownloaderBase.prototype.isFileExists = function (path) {
  122. return new Promise(function (resolve, reject) {
  123. fs_extra_1.exists(path, resolve);
  124. });
  125. };
  126. DownloaderBase.prototype.isFileSynced = function (url, path) {
  127. return __awaiter(this, void 0, void 0, function () {
  128. var localSize, remoteSize;
  129. return __generator(this, function (_a) {
  130. switch (_a.label) {
  131. case 0: return [4 /*yield*/, this.getLocalSize(path)];
  132. case 1:
  133. localSize = _a.sent();
  134. return [4 /*yield*/, this.getRemoteSize(url)];
  135. case 2:
  136. remoteSize = _a.sent();
  137. debug('in isFileSynced', 'localSize', localSize);
  138. debug('in isFileSynced', 'remoteSize', remoteSize);
  139. return [2 /*return*/, localSize == remoteSize];
  140. }
  141. });
  142. });
  143. };
  144. DownloaderBase.prototype.download = function (url, filename, path, showProgress) {
  145. return __awaiter(this, void 0, void 0, function () {
  146. var _this = this;
  147. var bar, onProgress;
  148. return __generator(this, function (_a) {
  149. switch (_a.label) {
  150. case 0:
  151. bar = null;
  152. onProgress = function (state) {
  153. if (!state.size.total) {
  154. return;
  155. }
  156. if (!bar) {
  157. bar = new ProgressBar('[:bar] :speedKB/s :etas', {
  158. width: 50,
  159. total: state.size.total,
  160. });
  161. console.info('');
  162. }
  163. bar.update(state.size.transferred / state.size.total, {
  164. speed: (state.speed / 1000).toFixed(2),
  165. });
  166. };
  167. if (showProgress) {
  168. this.onProgress.subscribe(onProgress);
  169. }
  170. debug('in download', 'start downloading', filename);
  171. return [4 /*yield*/, new Promise(function (resolve, reject) {
  172. progress(request(url, {
  173. encoding: null,
  174. }, function (err, res, data) {
  175. if (err) {
  176. return reject(err);
  177. }
  178. if (res.statusCode != 200) {
  179. var e = new Error("ERROR_STATUS_CODE statusCode = " + res.statusCode);
  180. return reject(e);
  181. }
  182. fs_extra_1.writeFile(path, data, function (err) { return err ? reject(err) : resolve(); });
  183. }))
  184. .on('progress', function (state) {
  185. _this.onProgress.trigger(state);
  186. });
  187. })];
  188. case 1:
  189. _a.sent();
  190. debug('in fetch', 'end downloading', filename);
  191. if (showProgress) {
  192. this.onProgress.unsubscribe(onProgress);
  193. if (bar) {
  194. console.info('');
  195. bar.terminate();
  196. }
  197. }
  198. return [2 /*return*/, path];
  199. }
  200. });
  201. });
  202. };
  203. return DownloaderBase;
  204. }());
  205. exports.DownloaderBase = DownloaderBase;
  206. //# sourceMappingURL=DownloaderBase.js.map
Buy Me A Coffee