The best way to control Claude Code, Codex and more from any computer or phone

main
patch 320 lines 16 KB
Raw
1 diff --git a/out/electronHttpExecutor.js b/out/electronHttpExecutor.js
2 index 0ae9e1126e1dc136977914ef1bfd46709fc9db1b..e6fb0b8aafda6ae0797d3127b5aa661b0d202ca5 100644
3 --- a/out/electronHttpExecutor.js
4 +++ b/out/electronHttpExecutor.js
5 @@ -3,7 +3,34 @@ Object.defineProperty(exports, "__esModule", { value: true });
6 exports.ElectronHttpExecutor = exports.NET_SESSION_NAME = void 0;
7 exports.getNetSession = getNetSession;
8 const builder_util_runtime_1 = require("builder-util-runtime");
9 +const fs_1 = require("fs");
10 +const stream_1 = require("stream");
11 exports.NET_SESSION_NAME = "electron-updater";
12 +// A stall on a slow link is retried, resuming after the bytes earlier attempts wrote.
13 +const DOWNLOAD_STALL_RETRIES = 3;
14 +function isDownloadStall(error) {
15 + return error != null && (error.message === "Request timed out" || error.message === "net::ERR_TIMED_OUT");
16 +}
17 +// doDownload turns any status >= 400 into this error before a response handler sees it.
18 +function isRangeNotSatisfiable(error) {
19 + return error != null && /", status 416: /.test(error.message);
20 +}
21 +function createDigest(options) {
22 + const sha512 = options.sha512;
23 + if (sha512 != null) {
24 + return new builder_util_runtime_1.DigestTransform(sha512, "sha512", sha512.length === 128 && !sha512.includes("+") && !sha512.includes("Z") && !sha512.includes("=") ? "hex" : "base64");
25 + }
26 + return options.sha2 == null ? null : new builder_util_runtime_1.DigestTransform(options.sha2, "sha256", "hex");
27 +}
28 +function verifyFile(destination, digest, cancellationToken) {
29 + return cancellationToken.createPromise((resolve, reject, onCancel) => {
30 + const controller = new AbortController();
31 + onCancel(() => controller.abort());
32 + stream_1.promises
33 + .pipeline((0, fs_1.createReadStream)(destination, { signal: controller.signal }), digest, new stream_1.Writable({ write: (chunk, encoding, next) => next() }))
34 + .then(() => resolve(), reject);
35 + });
36 +}
37 function getNetSession() {
38 return require("electron").session.fromPartition(exports.NET_SESSION_NAME, {
39 cache: false,
40 @@ -16,27 +43,133 @@ class ElectronHttpExecutor extends builder_util_runtime_1.HttpExecutor {
41 this.cachedSession = null;
42 }
43 async download(url, destination, options) {
44 - return await options.cancellationToken.createPromise((resolve, reject, onCancel) => {
45 + // Only bytes this call wrote are resumed: the differential downloader leaves its own output
46 + // at the same destination, and an attempt that fails before headers keeps the last count.
47 + const held = { bytes: 0 };
48 + for (let attempt = 0;; attempt++) {
49 + const offset = held.bytes;
50 + try {
51 + return await this.downloadFrom(url, destination, options, held);
52 + }
53 + catch (e) {
54 + // A 416 refuses the range; the digest decides whether the file already holds the release.
55 + const digest = offset > 0 && isRangeNotSatisfiable(e) ? createDigest(options) : null;
56 + if (digest != null) {
57 + await verifyFile(destination, digest, options.cancellationToken);
58 + return destination;
59 + }
60 + if (attempt >= DOWNLOAD_STALL_RETRIES || !isDownloadStall(e)) {
61 + throw e;
62 + }
63 + }
64 + await options.cancellationToken.createPromise((resolve, reject, onCancel) => {
65 + const timer = setTimeout(resolve, 2000 * 2 ** attempt);
66 + onCancel(() => clearTimeout(timer));
67 + });
68 + }
69 + }
70 + // Appends from `offset` when the server answers the Range with 206, then checks the digest over
71 + // the whole file so the bytes from earlier attempts are verified too. A 200 starts over.
72 + downloadFrom(url, destination, options, held) {
73 + const offset = held.bytes;
74 + const cancellationToken = options.cancellationToken;
75 + return new Promise((resolve, reject) => {
76 const requestOptions = {
77 - headers: options.headers || undefined,
78 + headers: offset > 0 ? { ...options.headers, Range: `bytes=${offset}-` } : options.headers || undefined,
79 redirect: "manual",
80 };
81 (0, builder_util_runtime_1.configureRequestUrl)(url, requestOptions);
82 (0, builder_util_runtime_1.configureRequestOptions)(requestOptions);
83 - this.doDownload(requestOptions, {
84 - destination,
85 - options,
86 - onCancel,
87 - callback: error => {
88 - if (error == null) {
89 - resolve(destination);
90 - }
91 - else {
92 - reject(error);
93 - }
94 - },
95 - responseHandler: null,
96 - }, 0);
97 + let fileOut = null;
98 + let upstream = null;
99 + let settled = false;
100 + const callback = error => {
101 + if (settled) {
102 + return;
103 + }
104 + settled = true;
105 + cancellationToken.removeListener("cancel", cancel);
106 + const settle = () => (error == null ? resolve(destination) : reject(error));
107 + if (fileOut == null || fileOut.closed) {
108 + settle();
109 + return;
110 + }
111 + // Let the bytes already handed to the file land, so a retry resumes after them.
112 + fileOut.once("close", settle);
113 + upstream.unpipe(fileOut);
114 + if (!fileOut.destroyed && !fileOut.writableEnded) {
115 + fileOut.end();
116 + }
117 + };
118 + // doDownload never calls onCancel, so a cancel settles here once the file is closed, and a
119 + // response that arrives after it is not written. The request runs on until its idle timeout.
120 + const cancel = () => callback(new builder_util_runtime_1.CancellationError());
121 + cancellationToken.onCancel(cancel);
122 + if (settled) {
123 + return;
124 + }
125 + try {
126 + this.doDownload(requestOptions, {
127 + destination,
128 + options,
129 + callback,
130 + responseHandler: response => {
131 + if (settled) {
132 + return;
133 + }
134 + const resumed = offset > 0 && response.statusCode === 206;
135 + const contentRange = (0, builder_util_runtime_1.safeGetHeader)(response, "content-range");
136 + if (resumed && (contentRange == null || !contentRange.startsWith(`bytes ${offset}-`))) {
137 + callback(new Error(`Cannot resume download at byte ${offset}, got Content-Range ${contentRange}`));
138 + return;
139 + }
140 + const sha2Header = (0, builder_util_runtime_1.safeGetHeader)(response, "X-Checksum-Sha2");
141 + if (sha2Header != null && options.sha2 != null && sha2Header !== options.sha2) {
142 + callback(new Error(`checksum mismatch: expected ${options.sha2} but got ${sha2Header} (X-Checksum-Sha2 header)`));
143 + return;
144 + }
145 + const start = resumed ? offset : 0;
146 + const streams = [];
147 + const contentLength = (0, builder_util_runtime_1.safeGetHeader)(response, "content-length");
148 + if (options.onProgress != null && contentLength != null) {
149 + const progress = new builder_util_runtime_1.ProgressCallbackTransform(start + parseInt(contentLength, 10), options.cancellationToken, options.onProgress);
150 + progress.transferred = start;
151 + streams.push(progress);
152 + }
153 + const digest = resumed ? null : createDigest(options);
154 + if (digest != null) {
155 + streams.push(digest);
156 + }
157 + fileOut = (0, fs_1.createWriteStream)(destination, { flags: resumed ? "a" : "w" });
158 + fileOut.once("close", () => {
159 + held.bytes = start + fileOut.bytesWritten;
160 + });
161 + upstream = response;
162 + for (const stream of streams) {
163 + stream.on("error", callback);
164 + upstream = upstream.pipe(stream);
165 + }
166 + fileOut.on("error", callback);
167 + upstream.pipe(fileOut);
168 + fileOut.on("finish", () => {
169 + if (settled) {
170 + return;
171 + }
172 + fileOut.once("close", () => {
173 + const fileDigest = resumed ? createDigest(options) : null;
174 + if (fileDigest == null) {
175 + callback(null);
176 + return;
177 + }
178 + verifyFile(destination, fileDigest, options.cancellationToken).then(() => callback(null), callback);
179 + });
180 + });
181 + },
182 + }, 0);
183 + }
184 + catch (error) {
185 + callback(error);
186 + }
187 });
188 }
189 createRequest(options, callback) {
190 @@ -61,6 +194,35 @@ class ElectronHttpExecutor extends builder_util_runtime_1.HttpExecutor {
191 }
192 return request;
193 }
194 + // Electron's net.ClientRequest never emits "socket", so the inherited socket idle timeout never
195 + // arms and a stalled request stays pending forever. Reset an idle timer on every chunk instead.
196 + addTimeOutHandler(request, callback, timeout) {
197 + let timer = null;
198 + const clear = () => {
199 + if (timer != null) {
200 + clearTimeout(timer);
201 + timer = null;
202 + }
203 + };
204 + const arm = () => {
205 + clear();
206 + timer = setTimeout(() => {
207 + timer = null;
208 + request.abort();
209 + callback(new Error("Request timed out"));
210 + }, timeout);
211 + };
212 + request.on("response", (response) => {
213 + arm();
214 + response.on("data", arm);
215 + response.on("end", clear);
216 + response.on("aborted", clear);
217 + response.on("error", clear);
218 + });
219 + request.on("abort", clear);
220 + request.on("error", clear);
221 + arm();
222 + }
223 addRedirectHandlers(request, options, reject, redirectCount, handler) {
224 request.on("redirect", (statusCode, method, redirectUrl) => {
225 // no way to modify request options, abort old and make a new one
226 diff --git a/out/providers/GitHubProvider.js b/out/providers/GitHubProvider.js
227 index e768d24e7a957caf32a4b8ca6c68cb50eb5865ee..708a1fa7d740192944f04259cde6d6bf79d58c53 100644
228 --- a/out/providers/GitHubProvider.js
229 +++ b/out/providers/GitHubProvider.js
230 @@ -107,6 +107,32 @@ class GitHubProvider extends BaseGitHubProvider {
231 catch (e) {
232 throw (0, builder_util_runtime_1.newError)(`Cannot parse releases feed: ${e.stack || e.message},\nXML:\n${feedXml}`, "ERR_UPDATER_INVALID_RELEASE_FEED");
233 }
234 + // The atom feed lists only the 10 newest git tags of any kind, in an order GitHub picks, so
235 + // the release workflow also records a prerelease channel's newest release at one fixed URL.
236 + // The newer of the two answers wins; the API scan is the last resort.
237 + let apiRelease = null;
238 + if (this.updater.allowPrerelease && this.updater.channel) {
239 + const feedTag = tag;
240 + const pointer = await this.getChannelPointerRelease(this.updater.channel, cancellationToken);
241 + if (pointer !== null && (tag == null || semver.gt(pointer.tag_name, tag))) {
242 + apiRelease = pointer;
243 + tag = pointer.tag_name;
244 + }
245 + // With no nightly in the feed, a pointer that offers nothing new may be stale, so the API
246 + // scan still runs. Its failure is fatal only when nothing else answered.
247 + if (feedTag == null && (tag == null || !semver.gt(tag, this.updater.currentVersion))) {
248 + const listed = await this.getLatestChannelRelease(this.updater.channel, cancellationToken).catch(e => {
249 + if (tag == null) {
250 + throw e;
251 + }
252 + return null;
253 + });
254 + if (listed !== null && (tag == null || semver.gt(listed.tag_name, tag))) {
255 + apiRelease = listed;
256 + tag = listed.tag_name;
257 + }
258 + }
259 + }
260 if (tag == null) {
261 throw (0, builder_util_runtime_1.newError)(`No published versions on GitHub`, "ERR_UPDATER_NO_PUBLISHED_VERSIONS");
262 }
263 @@ -144,6 +170,14 @@ class GitHubProvider extends BaseGitHubProvider {
264 }
265 }
266 const result = (0, Provider_1.parseUpdateInfo)(rawData, channelFile, channelFileUrl);
267 + if (apiRelease !== null) {
268 + if (result.releaseName == null) {
269 + result.releaseName = apiRelease.name || apiRelease.tag_name;
270 + }
271 + if (result.releaseNotes == null) {
272 + result.releaseNotes = apiRelease.body || "";
273 + }
274 + }
275 if (result.releaseName == null) {
276 result.releaseName = latestRelease.elementValueOrEmpty("title");
277 }
278 @@ -155,6 +189,42 @@ class GitHubProvider extends BaseGitHubProvider {
279 ...result,
280 };
281 }
282 + async getChannelPointerRelease(channel, cancellationToken) {
283 + try {
284 + const url = (0, util_1.newUrlFromBase)(this.getBaseDownloadPath("update-feed", `${channel}.json`), this.baseUrl);
285 + const rawData = await this.httpRequest(url, { accept: "application/json, */*" }, cancellationToken);
286 + const release = rawData == null ? null : JSON.parse(rawData);
287 + const releaseChannel = release == null || typeof release.tag_name !== "string" ? null : semver.prerelease(release.tag_name);
288 + return releaseChannel !== null && releaseChannel[0] === channel ? release : null;
289 + }
290 + catch (e) {
291 + // A fork, or a moment between two uploads, has no pointer. The feed still answers.
292 + return null;
293 + }
294 + }
295 + async getLatestChannelRelease(channel, cancellationToken) {
296 + for (let page = 1; page <= 5; page++) {
297 + const url = new url_1.URL(`${this.computeGithubBasePath(`/repos/${this.options.owner}/${this.options.repo}/releases`)}?per_page=100&page=${page}`, this.baseApiUrl);
298 + const rawData = await this.httpRequest(url, { Accept: "application/vnd.github+json" }, cancellationToken);
299 + const releases = rawData == null ? [] : JSON.parse(rawData);
300 + if (!Array.isArray(releases)) {
301 + return null;
302 + }
303 + for (const release of releases) {
304 + if (release == null || release.draft || typeof release.tag_name !== "string" || !semver.valid(release.tag_name)) {
305 + continue;
306 + }
307 + const releaseChannel = semver.prerelease(release.tag_name);
308 + if (releaseChannel !== null && releaseChannel[0] === channel) {
309 + return release;
310 + }
311 + }
312 + if (releases.length < 100) {
313 + return null;
314 + }
315 + }
316 + return null;
317 + }
318 async getLatestTagName(cancellationToken) {
319 const options = this.options;
320 // do not use API for GitHub to avoid limit, only for custom host or GitHub Enterprise