Skip to content

Commit 530871e

Browse files
fix(desktop): only reuse the cached file the updater would download
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 8fd4e0d commit 530871e

2 files changed

Lines changed: 57 additions & 23 deletions

File tree

‎apps/desktop/src/updates/DesktopUpdates.test.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ describe("DesktopUpdates", () => {
256256
cachedDownloadSha512: "sha-1.2.4",
257257
// electron-updater reports update-available while the check is running.
258258
checkForUpdates: Effect.sync(() =>
259-
harness.emit("update-available", { version: "1.2.4", files: [{ sha512: "sha-1.2.4" }] }),
259+
harness.emit("update-available", {
260+
version: "1.2.4",
261+
files: [{ url: "T3-Code-1.2.4.zip", sha512: "sha-1.2.4" }],
262+
}),
260263
).pipe(Effect.andThen(Deferred.await(releaseCheck))),
261264
downloadUpdate: Effect.sync(() =>
262265
harness.emit("update-downloaded", { version: "1.2.4" }),
@@ -294,7 +297,10 @@ describe("DesktopUpdates", () => {
294297
const updates = yield* DesktopUpdates.DesktopUpdates;
295298
yield* updates.configure;
296299

297-
harness.emit("update-available", { version: "1.2.5", files: [{ sha512: "sha-1.2.5" }] });
300+
harness.emit("update-available", {
301+
version: "1.2.5",
302+
files: [{ url: "T3-Code-1.2.5.zip", sha512: "sha-1.2.5" }],
303+
});
298304
yield* flushCallbacks;
299305

300306
assert.equal(harness.downloadCount(), 0);
@@ -305,16 +311,23 @@ describe("DesktopUpdates", () => {
305311
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
306312
});
307313

308-
it.effect("ignores a cached download of another package type", () => {
309-
// The harness caches update.zip; a Windows updater would replace it with an .exe.
310-
const harness = makeHarness({ cachedDownloadSha512: "sha-1.2.4", platform: "win32" });
314+
it.effect("ignores a cached download this platform's updater would not pick", () => {
315+
// The x64 Mac updater picks the x64 zip, so a cached arm64 zip would be
316+
// discarded and downloaded over the network again.
317+
const harness = makeHarness({ cachedDownloadSha512: "sha-arm64" });
311318

312319
return Effect.scoped(
313320
Effect.gen(function* () {
314321
const updates = yield* DesktopUpdates.DesktopUpdates;
315322
yield* updates.configure;
316323

317-
harness.emit("update-available", { version: "1.2.4", files: [{ sha512: "sha-1.2.4" }] });
324+
harness.emit("update-available", {
325+
version: "1.2.4",
326+
files: [
327+
{ url: "T3-Code-1.2.4-arm64.zip", sha512: "sha-arm64" },
328+
{ url: "T3-Code-1.2.4.zip", sha512: "sha-x64" },
329+
],
330+
});
318331
yield* flushCallbacks;
319332

320333
assert.equal(harness.downloadCount(), 0);

‎apps/desktop/src/updates/DesktopUpdates.ts‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ const UpdateInfo = Schema.Struct({
6969
files: Schema.optional(Schema.Unknown),
7070
});
7171

72-
// Only the sha512s are read, to recognize a download an earlier launch finished.
73-
const ReleaseFiles = Schema.Array(Schema.Struct({ sha512: Schema.String }));
72+
// Read to recognize a download an earlier launch finished.
73+
const ReleaseFile = Schema.Struct({ url: Schema.String, sha512: Schema.String });
74+
type ReleaseFile = typeof ReleaseFile.Type;
75+
const ReleaseFiles = Schema.Array(ReleaseFile);
7476

7577
// electron-updater's pending/update-info.json.
7678
const CachedDownloadInfo = Schema.fromJsonString(
@@ -284,6 +286,33 @@ function isArm64HostRunningIntelBuild(runtimeInfo: DesktopRuntimeInfo): boolean
284286
return runtimeInfo.hostArch === "arm64" && runtimeInfo.appArch === "x64";
285287
}
286288

289+
/** The release file electron-updater's platform updater will download, mirroring
290+
MacUpdater.filterFilesForArch and Provider.findFile. A cached download only
291+
counts if it is this file: the updater discards any other and fetches anew. */
292+
function selectUpdateFile(
293+
files: ReadonlyArray<ReleaseFile>,
294+
platform: NodeJS.Platform,
295+
isDebPackage: boolean,
296+
runtimeInfo: DesktopRuntimeInfo,
297+
): ReleaseFile | undefined {
298+
let candidates = files;
299+
if (platform === "darwin") {
300+
const isArm64File = (file: ReleaseFile) => file.url.includes("arm64");
301+
const preferArm64 = runtimeInfo.hostArch === "arm64" && files.some(isArm64File);
302+
candidates = files.filter((file) => isArm64File(file) === preferArm64);
303+
}
304+
const extension =
305+
platform === "darwin"
306+
? ".zip"
307+
: platform === "win32"
308+
? ".exe"
309+
: isDebPackage
310+
? ".deb"
311+
: ".appimage";
312+
const packageFiles = candidates.filter((file) => file.url.toLowerCase().endsWith(extension));
313+
return packageFiles.find((file) => file.url.includes(runtimeInfo.appArch)) ?? packageFiles[0];
314+
}
315+
287316
/** @public Service construction is part of the canonical Effect module API. */
288317
export const make = Effect.gen(function* () {
289318
const config = yield* DesktopConfig.DesktopConfig;
@@ -754,25 +783,12 @@ export const make = Effect.gen(function* () {
754783
);
755784
}).pipe(Effect.withSpan("desktop.updates.startPollers"));
756785

757-
// The package type this install's updater downloads. A cache left by another
758-
// install type (an AppImage next to a .deb) would be discarded and replaced
759-
// with a fresh network download, so it must not count as a match.
760-
const updatePackageExtension =
761-
environment.platform === "darwin"
762-
? ".zip"
763-
: environment.platform === "win32"
764-
? ".exe"
765-
: isDebPackage
766-
? ".deb"
767-
: ".AppImage";
768-
769786
const cachedDownloadSha512 = Effect.gen(function* () {
770787
const pendingDirectory = yield* electronUpdater.pendingDownloadDirectory;
771788
if (Option.isNone(pendingDirectory)) return Option.none<string>();
772789
const info = yield* fileSystem
773790
.readFileString(environment.path.join(pendingDirectory.value, "update-info.json"))
774791
.pipe(Effect.flatMap(decodeCachedDownloadInfo));
775-
if (!info.fileName.endsWith(updatePackageExtension)) return Option.none<string>();
776792
const fileExists = yield* fileSystem.exists(
777793
environment.path.join(pendingDirectory.value, info.fileName),
778794
);
@@ -789,8 +805,13 @@ export const make = Effect.gen(function* () {
789805
) {
790806
const cachedSha512 = yield* cachedDownloadSha512;
791807
if (Option.isNone(cachedSha512)) return;
792-
const releaseFiles = Option.getOrElse(decodeReleaseFiles(files), () => []);
793-
if (!releaseFiles.some((file) => file.sha512 === cachedSha512.value)) return;
808+
const updateFile = selectUpdateFile(
809+
Option.getOrElse(decodeReleaseFiles(files), () => []),
810+
environment.platform,
811+
isDebPackage,
812+
environment.runtimeInfo,
813+
);
814+
if (updateFile?.sha512 !== cachedSha512.value) return;
794815

795816
// update-available arrives while its check still holds the action reservation.
796817
yield* awaitIdleUpdateAction;

0 commit comments

Comments
 (0)