Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - electron-updater installs synchronously while the app quits.
2import * as NodeCrypto from "node:crypto";
3import * as NodeFS from "node:fs";
4import * as NodePath from "node:path";
5
6import type { AppImageUpdater } from "electron-updater";
7
8function syncPath(path: string): void {
9 const fd = NodeFS.openSync(path, "r");
10 try {
11 NodeFS.fsyncSync(fd);
12 } finally {
13 NodeFS.closeSync(fd);
14 }
15}
16
17function verifyAndSyncStage(path: string, sourceSize: number, sha512: string): void {
18 const fd = NodeFS.openSync(path, "r");
19 try {
20 if (NodeFS.fstatSync(fd).size !== sourceSize) {
21 throw new Error("The copied AppImage update has an invalid size.");
22 }
23 const hash = NodeCrypto.createHash("sha512");
24 const buffer = Buffer.allocUnsafe(1024 * 1024);
25 let read: number;
26 while ((read = NodeFS.readSync(fd, buffer, 0, buffer.length, null)) > 0) {
27 hash.update(buffer.subarray(0, read));
28 }
29 if (hash.digest("base64") !== sha512) {
30 throw new Error("The copied AppImage update has an invalid SHA-512 digest.");
31 }
32 NodeFS.fchmodSync(fd, 0o755);
33 NodeFS.fsyncSync(fd);
34 } finally {
35 NodeFS.closeSync(fd);
36 }
37}
38
39export function installAppImageUpdate(input: {
40 readonly appImageFile: string;
41 readonly installerPath: string;
42 readonly sha512: string;
43 readonly size?: number | undefined;
44 readonly logger?: NonNullable<AppImageUpdater["logger"]> | undefined;
45}): string {
46 const installedName = NodePath.basename(input.appImageFile);
47 // Match electron-updater: retain custom names, but advance versioned filenames.
48 const destination = /\d+\.\d+\.\d+/.test(installedName)
49 ? NodePath.join(NodePath.dirname(input.appImageFile), NodePath.basename(input.installerPath))
50 : input.appImageFile;
51 const sourceSize = NodeFS.statSync(input.installerPath).size;
52 if (sourceSize === 0 || (input.size !== undefined && sourceSize !== input.size)) {
53 throw new Error("The downloaded AppImage update has an invalid size.");
54 }
55
56 const bestEffort = (operation: () => void): boolean => {
57 try {
58 operation();
59 return true;
60 } catch (error) {
61 try {
62 input.logger?.warn(`AppImage update cleanup or directory sync failed: ${String(error)}`);
63 } catch {
64 // Logging must not turn a committed replacement into an installation failure.
65 }
66 return false;
67 }
68 };
69 const removeObsolete = (path: string) =>
70 bestEffort(() => {
71 const file = NodeFS.statSync(path, { bigint: true });
72 const installed = NodeFS.statSync(destination, { bigint: true });
73 // realpath covers symlinks; device/inode identity also covers bind mounts.
74 if (
75 NodeFS.realpathSync(path) === NodeFS.realpathSync(destination) ||
76 (file.dev === installed.dev && file.ino === installed.ino)
77 ) {
78 return;
79 }
80 NodeFS.rmSync(path, { force: true });
81 });
82
83 // A private stage on the destination filesystem allows an atomic rename. Its
84 // fixed-length name also accommodates AppImage names near the filename limit.
85 const stageDirectory = NodeFS.mkdtempSync(
86 NodePath.join(NodePath.dirname(destination), ".t3code-update-"),
87 );
88 const staged = NodePath.join(stageDirectory, "update.AppImage");
89 try {
90 NodeFS.copyFileSync(input.installerPath, staged, NodeFS.constants.COPYFILE_EXCL);
91 verifyAndSyncStage(staged, sourceSize, input.sha512);
92 NodeFS.renameSync(staged, destination);
93 } finally {
94 bestEffort(() => NodeFS.rmSync(stageDirectory, { recursive: true, force: true }));
95 }
96
97 // Until the rename is durable, the old AppImage and cached download are the only recovery copies.
98 if (!bestEffort(() => syncPath(NodePath.dirname(destination)))) return destination;
99 if (destination !== input.appImageFile) removeObsolete(input.appImageFile);
100 removeObsolete(input.installerPath);
101 return destination;
102}