DownloadedUpdateHelper.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.DownloadedUpdateHelper = void 0;
  6. function _bluebirdLst() {
  7. const data = require("bluebird-lst");
  8. _bluebirdLst = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _crypto() {
  14. const data = require("crypto");
  15. _crypto = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _fs() {
  21. const data = require("fs");
  22. _fs = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _lodash() {
  28. const data = _interopRequireDefault(require("lodash.isequal"));
  29. _lodash = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _fsExtraP() {
  35. const data = require("fs-extra-p");
  36. _fsExtraP = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. var path = _interopRequireWildcard(require("path"));
  42. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  43. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  44. /** @private **/
  45. class DownloadedUpdateHelper {
  46. constructor(cacheDir) {
  47. this.cacheDir = cacheDir;
  48. this._file = null;
  49. this._packageFile = null;
  50. this.versionInfo = null;
  51. this.fileInfo = null;
  52. }
  53. get file() {
  54. return this._file;
  55. }
  56. get packageFile() {
  57. return this._packageFile;
  58. }
  59. validateDownloadedPath(updateFile, versionInfo, fileInfo, logger) {
  60. var _this = this;
  61. return (0, _bluebirdLst().coroutine)(function* () {
  62. if (_this.versionInfo != null && _this.file === updateFile && _this.fileInfo != null) {
  63. // update has already been downloaded from this running instance
  64. // check here only existence, not checksum
  65. if ((0, _lodash().default)(_this.versionInfo, versionInfo) && (0, _lodash().default)(_this.fileInfo.info, fileInfo.info) && (yield (0, _fsExtraP().pathExists)(updateFile))) {
  66. return updateFile;
  67. } else {
  68. return null;
  69. }
  70. } // update has already been downloaded from some previous app launch
  71. const cachedUpdateFile = yield _this.getValidCachedUpdateFile(fileInfo, logger);
  72. if (cachedUpdateFile == null) {
  73. return null;
  74. }
  75. logger.info(`Update has already been downloaded to ${updateFile}).`);
  76. return cachedUpdateFile;
  77. })();
  78. }
  79. setDownloadedFile(downloadedFile, packageFile, versionInfo, fileInfo) {
  80. this._file = downloadedFile;
  81. this._packageFile = packageFile;
  82. this.versionInfo = versionInfo;
  83. this.fileInfo = fileInfo;
  84. }
  85. cacheUpdateInfo(updateFileName) {
  86. var _this2 = this;
  87. return (0, _bluebirdLst().coroutine)(function* () {
  88. const data = {
  89. fileName: updateFileName,
  90. sha512: _this2.fileInfo.info.sha512
  91. };
  92. yield (0, _fsExtraP().outputJson)(path.join(_this2.cacheDir, "update-info.json"), data);
  93. })();
  94. }
  95. clear() {
  96. var _this3 = this;
  97. return (0, _bluebirdLst().coroutine)(function* () {
  98. _this3._file = null;
  99. _this3._packageFile = null;
  100. _this3.versionInfo = null;
  101. _this3.fileInfo = null;
  102. yield _this3.cleanCacheDir();
  103. })();
  104. }
  105. cleanCacheDir() {
  106. var _this4 = this;
  107. return (0, _bluebirdLst().coroutine)(function* () {
  108. try {
  109. // remove stale data
  110. yield (0, _fsExtraP().emptyDir)(_this4.cacheDir);
  111. } catch (ignore) {// ignore
  112. }
  113. })();
  114. }
  115. getValidCachedUpdateFile(fileInfo, logger) {
  116. var _this5 = this;
  117. return (0, _bluebirdLst().coroutine)(function* () {
  118. let cachedInfo;
  119. const updateInfoFile = path.join(_this5.cacheDir, "update-info.json");
  120. try {
  121. cachedInfo = yield (0, _fsExtraP().readJson)(updateInfoFile);
  122. } catch (e) {
  123. let message = `No cached update info available`;
  124. if (e.code !== "ENOENT") {
  125. yield _this5.cleanCacheDir();
  126. message += ` (error on read: ${e.message})`;
  127. }
  128. logger.info(message);
  129. return null;
  130. }
  131. if (cachedInfo.fileName == null) {
  132. logger.warn(`Cached update info is corrupted: no fileName, directory for cached update will be cleaned`);
  133. yield _this5.cleanCacheDir();
  134. return null;
  135. }
  136. if (fileInfo.info.sha512 !== cachedInfo.sha512) {
  137. logger.info(`Cached update sha512 checksum doesn't match the latest available update. New update must be downloaded. Cached: ${cachedInfo.sha512}, expected: ${fileInfo.info.sha512}. Directory for cached update will be cleaned`);
  138. yield _this5.cleanCacheDir();
  139. return null;
  140. }
  141. const updateFile = path.join(_this5.cacheDir, cachedInfo.fileName);
  142. if (!(yield (0, _fsExtraP().pathExists)(updateFile))) {
  143. logger.info("Cached update file doesn't exist, directory for cached update will be cleaned");
  144. yield _this5.cleanCacheDir();
  145. return null;
  146. }
  147. const sha512 = yield hashFile(updateFile);
  148. if (fileInfo.info.sha512 !== sha512) {
  149. logger.warn(`Sha512 checksum doesn't match the latest available update. New update must be downloaded. Cached: ${sha512}, expected: ${fileInfo.info.sha512}`);
  150. yield _this5.cleanCacheDir();
  151. return null;
  152. }
  153. return updateFile;
  154. })();
  155. }
  156. }
  157. exports.DownloadedUpdateHelper = DownloadedUpdateHelper;
  158. function hashFile(file, algorithm = "sha512", encoding = "base64", options) {
  159. return new Promise((resolve, reject) => {
  160. const hash = (0, _crypto().createHash)(algorithm);
  161. hash.on("error", reject).setEncoding(encoding);
  162. (0, _fs().createReadStream)(file, Object.assign({}, options, {
  163. highWaterMark: 1024 * 1024
  164. /* better to use more memory but hash faster */
  165. })).on("error", reject).on("end", () => {
  166. hash.end();
  167. resolve(hash.read());
  168. }).pipe(hash, {
  169. end: false
  170. });
  171. });
  172. }
  173. //# sourceMappingURL=DownloadedUpdateHelper.js.map