DownloadedUpdateHelper.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  42. /** @private **/
  43. class DownloadedUpdateHelper {
  44. constructor(cacheDir) {
  45. this.cacheDir = cacheDir;
  46. this._file = null;
  47. this._packageFile = null;
  48. this.versionInfo = null;
  49. this.fileInfo = null;
  50. }
  51. get file() {
  52. return this._file;
  53. }
  54. get packageFile() {
  55. return this._packageFile;
  56. }
  57. validateDownloadedPath(updateFile, versionInfo, fileInfo, logger) {
  58. var _this = this;
  59. return (0, _bluebirdLst().coroutine)(function* () {
  60. if (_this.versionInfo != null && _this.file === updateFile) {
  61. // update has already been downloaded from this running instance
  62. // check here only existence, not checksum
  63. return (0, _lodash().default)(_this.versionInfo, versionInfo) && (0, _lodash().default)(_this.fileInfo, fileInfo) && (yield (0, _fsExtraP().pathExists)(updateFile));
  64. } // update has already been downloaded from some previous app launch
  65. if (yield DownloadedUpdateHelper.isUpdateValid(updateFile, fileInfo, logger)) {
  66. logger.info(`Update has already been downloaded ${updateFile}).`);
  67. return true;
  68. }
  69. return false;
  70. })();
  71. }
  72. setDownloadedFile(downloadedFile, packageFile, versionInfo, fileInfo) {
  73. this._file = downloadedFile;
  74. this._packageFile = packageFile;
  75. this.versionInfo = versionInfo;
  76. this.fileInfo = fileInfo;
  77. }
  78. clear() {
  79. this._file = null;
  80. this._packageFile = null;
  81. this.versionInfo = null;
  82. this.fileInfo = null;
  83. }
  84. static isUpdateValid(updateFile, fileInfo, logger) {
  85. return (0, _bluebirdLst().coroutine)(function* () {
  86. if (!(yield (0, _fsExtraP().pathExists)(updateFile))) {
  87. logger.info("No cached update available");
  88. return false;
  89. }
  90. const sha512 = yield hashFile(updateFile);
  91. if (fileInfo.info.sha512 !== sha512) {
  92. logger.warn(`Sha512 checksum doesn't match the latest available update. New update must be downloaded. Cached: ${sha512}, expected: ${fileInfo.info.sha512}`);
  93. return false;
  94. }
  95. return true;
  96. })();
  97. }
  98. }
  99. exports.DownloadedUpdateHelper = DownloadedUpdateHelper;
  100. function hashFile(file, algorithm = "sha512", encoding = "base64", options) {
  101. return new Promise((resolve, reject) => {
  102. const hash = (0, _crypto().createHash)(algorithm);
  103. hash.on("error", reject).setEncoding(encoding);
  104. (0, _fs().createReadStream)(file, Object.assign({}, options, {
  105. highWaterMark: 1024 * 1024
  106. /* better to use more memory but hash faster */
  107. })).on("error", reject).on("end", () => {
  108. hash.end();
  109. resolve(hash.read());
  110. }).pipe(hash, {
  111. end: false
  112. });
  113. });
  114. }
  115. //# sourceMappingURL=DownloadedUpdateHelper.js.map