GenericProvider.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.GenericProvider = undefined;
  6. var _bluebirdLst;
  7. function _load_bluebirdLst() {
  8. return _bluebirdLst = require("bluebird-lst");
  9. }
  10. exports.validateUpdateInfo = validateUpdateInfo;
  11. var _api;
  12. function _load_api() {
  13. return _api = require("./api");
  14. }
  15. var _url;
  16. function _load_url() {
  17. return _url = _interopRequireWildcard(require("url"));
  18. }
  19. var _path = _interopRequireWildcard(require("path"));
  20. var _electronBuilderHttp;
  21. function _load_electronBuilderHttp() {
  22. return _electronBuilderHttp = require("electron-builder-http");
  23. }
  24. var _CancellationToken;
  25. function _load_CancellationToken() {
  26. return _CancellationToken = require("electron-builder-http/out/CancellationToken");
  27. }
  28. 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  29. class GenericProvider extends (_api || _load_api()).Provider {
  30. constructor(configuration) {
  31. super();
  32. this.configuration = configuration;
  33. this.baseUrl = (_url || _load_url()).parse(this.configuration.url);
  34. this.channel = this.configuration.channel ? (0, (_api || _load_api()).getCustomChannelName)(this.configuration.channel) : (0, (_api || _load_api()).getDefaultChannelName)();
  35. }
  36. getLatestVersion() {
  37. var _this = this;
  38. return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
  39. let result = null;
  40. const channelFile = (0, (_api || _load_api()).getChannelFilename)(_this.channel);
  41. const pathname = _path.posix.resolve(_this.baseUrl.pathname || "/", channelFile);
  42. try {
  43. const options = {
  44. hostname: _this.baseUrl.hostname,
  45. path: `${pathname}${_this.baseUrl.search || ""}`,
  46. protocol: _this.baseUrl.protocol,
  47. headers: _this.requestHeaders || undefined
  48. };
  49. if (_this.baseUrl.port != null) {
  50. options.port = parseInt(_this.baseUrl.port, 10);
  51. }
  52. result = yield (0, (_electronBuilderHttp || _load_electronBuilderHttp()).request)(options, new (_CancellationToken || _load_CancellationToken()).CancellationToken());
  53. } catch (e) {
  54. if (e instanceof (_electronBuilderHttp || _load_electronBuilderHttp()).HttpError && e.response.statusCode === 404) {
  55. throw new Error(`Cannot find channel "${channelFile}" update info: ${e.stack || e.message}`);
  56. }
  57. throw e;
  58. }
  59. validateUpdateInfo(result);
  60. if ((0, (_api || _load_api()).getCurrentPlatform)() === "darwin") {
  61. result.releaseJsonUrl = (_url || _load_url()).format(Object.assign({}, _this.baseUrl, { pathname: pathname }));
  62. }
  63. return result;
  64. })();
  65. }
  66. getUpdateFile(versionInfo) {
  67. var _this2 = this;
  68. return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
  69. if ((0, (_api || _load_api()).getCurrentPlatform)() === "darwin") {
  70. return versionInfo;
  71. }
  72. return {
  73. name: _path.posix.basename(versionInfo.path),
  74. url: (_url || _load_url()).format(Object.assign({}, _this2.baseUrl, { pathname: _path.posix.resolve(_this2.baseUrl.pathname || "/", versionInfo.path) })),
  75. sha2: versionInfo.sha2
  76. };
  77. })();
  78. }
  79. }
  80. exports.GenericProvider = GenericProvider; // sha2 is required only for windows because on macOS update is verified by Squirrel.Mac
  81. function validateUpdateInfo(info) {
  82. if ((0, (_api || _load_api()).getCurrentPlatform)() === "darwin") {
  83. if (info.url == null) {
  84. throw new Error("Update info doesn't contain url");
  85. }
  86. return;
  87. }
  88. if (info.sha2 == null) {
  89. throw new Error(`Update info doesn't contain sha2 checksum: ${JSON.stringify(info, null, 2)}`);
  90. }
  91. if (info.path == null) {
  92. throw new Error(`Update info doesn't contain file path: ${JSON.stringify(info, null, 2)}`);
  93. }
  94. }
  95. //# sourceMappingURL=GenericProvider.js.map