UNPKG

5.48 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.MacUpdater = void 0;
7
8function _builderUtilRuntime() {
9 const data = require("builder-util-runtime");
10
11 _builderUtilRuntime = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _fsExtra() {
19 const data = require("fs-extra");
20
21 _fsExtra = function () {
22 return data;
23 };
24
25 return data;
26}
27
28var _fs = require("fs");
29
30function _http() {
31 const data = require("http");
32
33 _http = function () {
34 return data;
35 };
36
37 return data;
38}
39
40function _AppUpdater() {
41 const data = require("./AppUpdater");
42
43 _AppUpdater = function () {
44 return data;
45 };
46
47 return data;
48}
49
50function _Provider() {
51 const data = require("./providers/Provider");
52
53 _Provider = function () {
54 return data;
55 };
56
57 return data;
58}
59
60class MacUpdater extends _AppUpdater().AppUpdater {
61 constructor(options, app) {
62 super(options, app);
63 this.nativeUpdater = require("electron").autoUpdater;
64 this.updateInfoForPendingUpdateDownloadedEvent = null;
65 this.nativeUpdater.on("error", it => {
66 this._logger.warn(it);
67
68 this.emit("error", it);
69 });
70 this.nativeUpdater.on("update-downloaded", () => {
71 const updateInfo = this.updateInfoForPendingUpdateDownloadedEvent;
72 this.updateInfoForPendingUpdateDownloadedEvent = null;
73 this.dispatchUpdateDownloaded(updateInfo);
74 });
75 }
76
77 doDownloadUpdate(downloadUpdateOptions) {
78 this.updateInfoForPendingUpdateDownloadedEvent = null;
79 const files = downloadUpdateOptions.updateInfoAndProvider.provider.resolveFiles(downloadUpdateOptions.updateInfoAndProvider.info);
80 const zipFileInfo = (0, _Provider().findFile)(files, "zip", ["pkg", "dmg"]);
81
82 if (zipFileInfo == null) {
83 throw (0, _builderUtilRuntime().newError)(`ZIP file not provided: ${(0, _builderUtilRuntime().safeStringifyJson)(files)}`, "ERR_UPDATER_ZIP_FILE_NOT_FOUND");
84 }
85
86 const server = (0, _http().createServer)();
87 server.on("close", () => {
88 this._logger.info(`Proxy server for native Squirrel.Mac is closed (was started to download ${zipFileInfo.url.href})`);
89 });
90
91 function getServerUrl() {
92 const address = server.address();
93 return `http://127.0.0.1:${address.port}`;
94 }
95
96 return this.executeDownload({
97 fileExtension: "zip",
98 fileInfo: zipFileInfo,
99 downloadUpdateOptions,
100 task: (destinationFile, downloadOptions) => {
101 return this.httpExecutor.download(zipFileInfo.url, destinationFile, downloadOptions);
102 },
103 done: async event => {
104 const downloadedFile = event.downloadedFile;
105 this.updateInfoForPendingUpdateDownloadedEvent = event;
106 let updateFileSize = zipFileInfo.info.size;
107
108 if (updateFileSize == null) {
109 updateFileSize = (await (0, _fsExtra().stat)(downloadedFile)).size;
110 }
111
112 return await new Promise((resolve, reject) => {
113 // insecure random is ok
114 const fileUrl = "/" + Date.now() + "-" + Math.floor(Math.random() * 9999) + ".zip";
115 server.on("request", (request, response) => {
116 const requestUrl = request.url;
117
118 this._logger.info(`${requestUrl} requested`);
119
120 if (requestUrl === "/") {
121 const data = Buffer.from(`{ "url": "${getServerUrl()}${fileUrl}" }`);
122 response.writeHead(200, {
123 "Content-Type": "application/json",
124 "Content-Length": data.length
125 });
126 response.end(data);
127 return;
128 }
129
130 if (!requestUrl.startsWith(fileUrl)) {
131 this._logger.warn(`${requestUrl} requested, but not supported`);
132
133 response.writeHead(404);
134 response.end();
135 return;
136 }
137
138 this._logger.info(`${fileUrl} requested by Squirrel.Mac, pipe ${downloadedFile}`);
139
140 let errorOccurred = false;
141 response.on("finish", () => {
142 try {
143 setImmediate(() => server.close());
144 } finally {
145 if (!errorOccurred) {
146 this.nativeUpdater.removeListener("error", reject);
147 resolve([]);
148 }
149 }
150 });
151 const readStream = (0, _fs.createReadStream)(downloadedFile);
152 readStream.on("error", error => {
153 try {
154 response.end();
155 } catch (e) {
156 this._logger.warn(`cannot end response: ${e}`);
157 }
158
159 errorOccurred = true;
160 this.nativeUpdater.removeListener("error", reject);
161 reject(new Error(`Cannot pipe "${downloadedFile}": ${error}`));
162 });
163 response.writeHead(200, {
164 "Content-Type": "application/zip",
165 "Content-Length": updateFileSize
166 });
167 readStream.pipe(response);
168 });
169 server.listen(0, "127.0.0.1", () => {
170 this.nativeUpdater.setFeedURL({
171 url: getServerUrl(),
172 headers: {
173 "Cache-Control": "no-cache"
174 }
175 });
176 this.nativeUpdater.once("error", reject);
177 this.nativeUpdater.checkForUpdates();
178 });
179 });
180 }
181 });
182 }
183
184 quitAndInstall() {
185 this.nativeUpdater.quitAndInstall();
186 }
187
188} exports.MacUpdater = MacUpdater;
189// __ts-babel@6.0.4
190//# sourceMappingURL=MacUpdater.js.map
\No newline at end of file