akanjs 3.0.0-beta.26 → 3.0.0-beta.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base/baseEnv.ts +19 -5
- package/client/csrTypes.ts +2 -0
- package/client/device.ts +28 -12
- package/client/index.ts +1 -0
- package/client/native/accessibility.ts +1 -0
- package/client/native/app-state.ts +1 -0
- package/client/native/app.ts +1 -0
- package/client/native/appearance.ts +1 -0
- package/client/native/auth-session.ts +1 -0
- package/client/native/autostart.ts +1 -0
- package/client/native/badge.ts +1 -0
- package/client/native/biometric.ts +1 -0
- package/client/native/browser.ts +1 -0
- package/client/native/camera.ts +1 -0
- package/client/native/clipboard.ts +1 -0
- package/client/native/contacts.ts +1 -0
- package/client/native/device.ts +1 -0
- package/client/native/dialog.ts +1 -0
- package/client/native/dock.ts +1 -0
- package/client/native/file-picker.ts +1 -0
- package/client/native/filesystem.ts +1 -0
- package/client/native/geolocation.ts +1 -0
- package/client/native/global-shortcut.ts +1 -0
- package/client/native/haptics.ts +1 -0
- package/client/native/http.ts +1 -0
- package/client/native/iap.ts +1 -0
- package/client/native/keep-awake.ts +1 -0
- package/client/native/keyboard.ts +1 -0
- package/client/native/local-notifications.ts +1 -0
- package/client/native/menu.ts +1 -0
- package/client/native/network.ts +1 -0
- package/client/native/opener.ts +1 -0
- package/client/native/preferences.ts +1 -0
- package/client/native/push.ts +1 -0
- package/client/native/screen-orientation.ts +1 -0
- package/client/native/screen.ts +1 -0
- package/client/native/secure-storage.ts +1 -0
- package/client/native/share.ts +1 -0
- package/client/native/single-instance.ts +1 -0
- package/client/native/splash-screen.ts +1 -0
- package/client/native/sqlite.ts +1 -0
- package/client/native/toast.ts +1 -0
- package/client/native/tray.ts +1 -0
- package/client/native/updates.ts +1 -0
- package/client/native/volume.ts +1 -0
- package/client/native/window-state.ts +1 -0
- package/client/native/window.ts +1 -0
- package/client/native.ts +31 -1
- package/client/resolveServerUrl.ts +11 -0
- package/client/router.ts +8 -0
- package/common/Logger.ts +26 -2
- package/common/RouteLayering.ts +52 -0
- package/common/index.ts +1 -0
- package/index.ts +115 -6
- package/native/desktop.ts +5 -0
- package/package.json +11 -1
- package/server/akanApp.ts +23 -3
- package/server/akanAppHeaders.ts +4 -1
- package/server/akanServer.ts +27 -7
- package/server/index.ts +1 -0
- package/server/lifecycle/orphanGroup.ts +21 -0
- package/server/nativeFile.ts +107 -0
- package/server/ops/opsRoute.ts +3 -2
- package/server/routeTreeBuilder.ts +14 -39
- package/server/routing/apiRouter.ts +26 -3
- package/server/routing/hostAllowlist.ts +24 -0
- package/server/selfExec.ts +25 -0
- package/server/types.tsx +3 -1
- package/server/webRouter.ts +7 -4
- package/service/ipcTypes.ts +4 -0
- package/signal/CrossSiteGuard.ts +7 -0
- package/test/signalTestRuntime.ts +8 -2
- package/types/base/baseEnv.d.ts +1 -0
- package/types/client/csrTypes.d.ts +2 -0
- package/types/client/index.d.ts +1 -0
- package/types/client/native/accessibility.d.ts +1 -0
- package/types/client/native/app-state.d.ts +1 -0
- package/types/client/native/app.d.ts +1 -0
- package/types/client/native/appearance.d.ts +1 -0
- package/types/client/native/auth-session.d.ts +1 -0
- package/types/client/native/autostart.d.ts +1 -0
- package/types/client/native/badge.d.ts +1 -0
- package/types/client/native/biometric.d.ts +1 -0
- package/types/client/native/browser.d.ts +1 -0
- package/types/client/native/camera.d.ts +1 -0
- package/types/client/native/clipboard.d.ts +1 -0
- package/types/client/native/contacts.d.ts +1 -0
- package/types/client/native/device.d.ts +1 -0
- package/types/client/native/dialog.d.ts +1 -0
- package/types/client/native/dock.d.ts +1 -0
- package/types/client/native/file-picker.d.ts +1 -0
- package/types/client/native/filesystem.d.ts +1 -0
- package/types/client/native/geolocation.d.ts +1 -0
- package/types/client/native/global-shortcut.d.ts +1 -0
- package/types/client/native/haptics.d.ts +1 -0
- package/types/client/native/http.d.ts +1 -0
- package/types/client/native/iap.d.ts +1 -0
- package/types/client/native/keep-awake.d.ts +1 -0
- package/types/client/native/keyboard.d.ts +1 -0
- package/types/client/native/local-notifications.d.ts +1 -0
- package/types/client/native/menu.d.ts +1 -0
- package/types/client/native/network.d.ts +1 -0
- package/types/client/native/opener.d.ts +1 -0
- package/types/client/native/preferences.d.ts +1 -0
- package/types/client/native/push.d.ts +1 -0
- package/types/client/native/screen-orientation.d.ts +1 -0
- package/types/client/native/screen.d.ts +1 -0
- package/types/client/native/secure-storage.d.ts +1 -0
- package/types/client/native/share.d.ts +1 -0
- package/types/client/native/single-instance.d.ts +1 -0
- package/types/client/native/splash-screen.d.ts +1 -0
- package/types/client/native/sqlite.d.ts +1 -0
- package/types/client/native/toast.d.ts +1 -0
- package/types/client/native/tray.d.ts +1 -0
- package/types/client/native/updates.d.ts +1 -0
- package/types/client/native/volume.d.ts +1 -0
- package/types/client/native/window-state.d.ts +1 -0
- package/types/client/native/window.d.ts +1 -0
- package/types/client/native.d.ts +14 -2
- package/types/client/resolveServerUrl.d.ts +1 -0
- package/types/client/router.d.ts +3 -0
- package/types/common/RouteLayering.d.ts +28 -0
- package/types/common/index.d.ts +1 -0
- package/types/index.d.ts +112 -6
- package/types/native/desktop.d.ts +3 -0
- package/types/server/akanAppHeaders.d.ts +2 -0
- package/types/server/index.d.ts +1 -0
- package/types/server/lifecycle/orphanGroup.d.ts +7 -0
- package/types/server/nativeFile.d.ts +10 -0
- package/types/server/routing/apiRouter.d.ts +3 -1
- package/types/server/routing/hostAllowlist.d.ts +8 -0
- package/types/server/selfExec.d.ts +8 -0
- package/types/server/types.d.ts +3 -1
- package/types/service/ipcTypes.d.ts +16 -0
- package/types/signal/CrossSiteGuard.d.ts +1 -0
- package/types/ui/recipe/buttonRecipe.d.ts +2 -2
- package/types/vendor/@akanjs/native/packages/cli/src/api.d.ts +57 -4
- package/types/vendor/@akanjs/native/packages/cli/src/config.d.ts +48 -0
- package/types/vendor/@akanjs/native/packages/cli/src/lib/desktop-server.d.ts +8 -0
- package/types/vendor/@akanjs/native/packages/cli/src/lib/prepare.d.ts +8 -2
- package/types/vendor/@akanjs/native/packages/cli/src/lib/project.d.ts +10 -0
- package/types/vendor/@akanjs/native/packages/cli/src/lib/publish.d.ts +24 -0
- package/types/vendor/@akanjs/native/packages/cli/src/lib/updates.d.ts +24 -0
- package/types/vendor/@akanjs/native/packages/cli/src/platforms/desktop-update.d.ts +4 -1
- package/types/vendor/@akanjs/native/packages/cli/src/platforms/macos.d.ts +5 -0
- package/types/vendor/@akanjs/native/packages/cli/src/platforms/windows-installer.d.ts +38 -0
- package/types/vendor/@akanjs/native/packages/desktop/src/dispatcher.d.ts +8 -1
- package/types/vendor/@akanjs/native/packages/desktop/src/grants.d.ts +11 -0
- package/types/vendor/@akanjs/native/packages/desktop/src/main.d.ts +11 -0
- package/types/vendor/@akanjs/native/packages/desktop/src/paths.d.ts +13 -2
- package/types/vendor/@akanjs/native/packages/desktop/src/plugin.d.ts +27 -0
- package/types/vendor/@akanjs/native/packages/desktop/src/relaunch.d.ts +21 -0
- package/types/vendor/@akanjs/native/packages/desktop/src/server.d.ts +79 -0
- package/types/vendor/@akanjs/native/plugins/app/src/index.d.ts +5 -0
- package/types/vendor/@akanjs/native/plugins/file-picker/src/index.d.ts +20 -1
- package/types/vendor/@akanjs/native/plugins/push/src/index.d.ts +9 -0
- package/types/vendor/@akanjs/native/plugins/updates/src/desktop.d.ts +28 -0
- package/types/vendor/@akanjs/native/plugins/volume/src/common.d.ts +10 -0
- package/types/vendor/@akanjs/native/plugins/volume/src/desktop.d.ts +18 -0
- package/types/vendor/@akanjs/native/plugins/volume/src/index.d.ts +28 -0
- package/types/vendor/@akanjs/native/plugins/volume/src.d.ts +1 -0
- package/types/webkit/csrFrameDump.d.ts +42 -0
- package/types/webkit/nativeNavigation.d.ts +1 -0
- package/types/webkit/nativePurchase.d.ts +6 -2
- package/types/webkit/nativeUpdates.d.ts +8 -0
- package/ui/Agent/Attach.tsx +6 -2
- package/ui/CsrImage.tsx +3 -2
- package/ui/System/CSR.tsx +4 -0
- package/vendor/@akanjs/native/native/android/src/com/akanjs/runtime/AkanNativeActivity.kt +5 -1
- package/vendor/@akanjs/native/native/android/src/com/akanjs/runtime/AkanNativeBridge.kt +19 -0
- package/vendor/@akanjs/native/native/desktop/Cargo.toml +1 -1
- package/vendor/@akanjs/native/native/desktop/src/lib.rs +161 -25
- package/vendor/@akanjs/native/native/desktop/src/volume.rs +184 -0
- package/vendor/@akanjs/native/native/desktop/src/win/mod.rs +3 -1
- package/vendor/@akanjs/native/native/desktop/src/win/volume.rs +71 -0
- package/vendor/@akanjs/native/native/ios/Sources/AkanNativeShell.swift +12 -2
- package/vendor/@akanjs/native/packages/cli/src/api.ts +142 -12
- package/vendor/@akanjs/native/packages/cli/src/commands/build.ts +5 -3
- package/vendor/@akanjs/native/packages/cli/src/commands/update.ts +10 -71
- package/vendor/@akanjs/native/packages/cli/src/config.ts +41 -0
- package/vendor/@akanjs/native/packages/cli/src/lib/configkeys.ts +9 -2
- package/vendor/@akanjs/native/packages/cli/src/lib/desktop-server.ts +58 -0
- package/vendor/@akanjs/native/packages/cli/src/lib/hmr.ts +2 -0
- package/vendor/@akanjs/native/packages/cli/src/lib/prepare.ts +10 -2
- package/vendor/@akanjs/native/packages/cli/src/lib/project.ts +38 -2
- package/vendor/@akanjs/native/packages/cli/src/lib/publish.ts +104 -0
- package/vendor/@akanjs/native/packages/cli/src/lib/updates.ts +85 -5
- package/vendor/@akanjs/native/packages/cli/src/platforms/android.ts +1 -0
- package/vendor/@akanjs/native/packages/cli/src/platforms/desktop-update.ts +6 -4
- package/vendor/@akanjs/native/packages/cli/src/platforms/desktop.ts +16 -0
- package/vendor/@akanjs/native/packages/cli/src/platforms/macos.ts +44 -1
- package/vendor/@akanjs/native/packages/cli/src/platforms/windows-installer.ts +436 -0
- package/vendor/@akanjs/native/packages/cli/src/platforms/windows.ts +2 -0
- package/vendor/@akanjs/native/packages/core/src/runtime.ts +26 -5
- package/vendor/@akanjs/native/packages/desktop/src/dispatcher.ts +20 -0
- package/vendor/@akanjs/native/packages/desktop/src/grants.ts +47 -0
- package/vendor/@akanjs/native/packages/desktop/src/host.ts +102 -14
- package/vendor/@akanjs/native/packages/desktop/src/main.ts +59 -11
- package/vendor/@akanjs/native/packages/desktop/src/paths.ts +38 -4
- package/vendor/@akanjs/native/packages/desktop/src/plugin.ts +28 -0
- package/vendor/@akanjs/native/packages/desktop/src/relaunch.ts +159 -0
- package/vendor/@akanjs/native/packages/desktop/src/server.ts +493 -0
- package/vendor/@akanjs/native/plugins/app/android/AppPlugin.kt +14 -0
- package/vendor/@akanjs/native/plugins/app/native-plugin.json +2 -2
- package/vendor/@akanjs/native/plugins/app/src/desktop.ts +5 -0
- package/vendor/@akanjs/native/plugins/app/src/index.ts +6 -1
- package/vendor/@akanjs/native/plugins/app/src/web.ts +3 -0
- package/vendor/@akanjs/native/plugins/file-picker/src/desktop.ts +39 -2
- package/vendor/@akanjs/native/plugins/file-picker/src/index.ts +36 -1
- package/vendor/@akanjs/native/plugins/push/android/PushPlugin.kt +9 -1
- package/vendor/@akanjs/native/plugins/push/ios/PushPlugin.swift +4 -0
- package/vendor/@akanjs/native/plugins/push/src/index.ts +10 -0
- package/vendor/@akanjs/native/plugins/updates/src/desktop.ts +464 -452
- package/vendor/@akanjs/native/plugins/volume/android/VolumePlugin.kt +107 -0
- package/vendor/@akanjs/native/plugins/volume/native-plugin.json +14 -0
- package/vendor/@akanjs/native/plugins/volume/src/common.ts +28 -0
- package/vendor/@akanjs/native/plugins/volume/src/desktop.ts +185 -0
- package/vendor/@akanjs/native/plugins/volume/src/index.ts +32 -0
- package/webkit/CsrRouteTable.ts +15 -35
- package/webkit/RenderLayer.tsx +71 -6
- package/webkit/bootCsr.tsx +11 -8
- package/webkit/csrFrameDump.ts +67 -0
- package/webkit/nativeNavigation.ts +17 -0
- package/webkit/nativePurchase.ts +12 -2
- package/webkit/nativeUpdates.ts +55 -0
- package/webkit/useCsrValues.ts +27 -0
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
import { spawn } from "node:child_process";
|
|
4
3
|
import { createHash, createPublicKey, verify } from "node:crypto";
|
|
5
4
|
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
6
5
|
import { basename, dirname, join } from "node:path";
|
|
@@ -8,9 +7,10 @@ import { AkanNativeError } from "../../../packages/core/src/index.ts";
|
|
|
8
7
|
import { isBundleId } from "../../../packages/core/src/kernel.ts";
|
|
9
8
|
import { applyDelta } from "../../../packages/desktop/src/delta.ts";
|
|
10
9
|
import { type DesktopContext, type DesktopPlugin, defineDesktopPlugin } from "../../../packages/desktop/src/plugin.ts";
|
|
10
|
+
import { relaunchAfterExit } from "../../../packages/desktop/src/relaunch.ts";
|
|
11
11
|
import type { UpdateCheck, UpdateState, UpdatesApi, UpdatesEvents } from "./index.ts";
|
|
12
12
|
|
|
13
|
-
interface
|
|
13
|
+
export interface UpdatesConfig {
|
|
14
14
|
app: string;
|
|
15
15
|
platform: string;
|
|
16
16
|
nativeApi: string | null;
|
|
@@ -34,8 +34,11 @@ interface Entry {
|
|
|
34
34
|
interface State {
|
|
35
35
|
current?: Entry;
|
|
36
36
|
pending?: Entry & { staged: string };
|
|
37
|
-
/**
|
|
38
|
-
|
|
37
|
+
/**
|
|
38
|
+
* `pid`: the process running the trial, so a second instance of the app leaves it alone. `from` (Windows, where a
|
|
39
|
+
* helper swaps after the app exited): the build that applied it, which is still what runs when the swap failed.
|
|
40
|
+
*/
|
|
41
|
+
trial?: Entry & { previous: string; attempts: number; pid?: number; from?: number };
|
|
39
42
|
failed: string[];
|
|
40
43
|
rolledBack?: string;
|
|
41
44
|
}
|
|
@@ -55,6 +58,13 @@ interface Manifest {
|
|
|
55
58
|
patches?: { from: string; url: string; sha256: string; size: number }[];
|
|
56
59
|
}
|
|
57
60
|
|
|
61
|
+
/**
|
|
62
|
+
* How long a new release's carried server may take to answer ready before its trial clock starts anyway: a first
|
|
63
|
+
* run scans every file with a cold transpiler cache and migrates the databases, on a PC that may be slow. The
|
|
64
|
+
* launcher's own wait (READY_TIMEOUT, 8 s) only decides when the window opens.
|
|
65
|
+
*/
|
|
66
|
+
export const SERVER_BOOT_ALLOWANCE = 120_000;
|
|
67
|
+
|
|
58
68
|
const MAX_MANIFEST = 1 << 20;
|
|
59
69
|
const MAX_SIGNATURE = 1024;
|
|
60
70
|
const sha256 = (data: Uint8Array) => createHash("sha256").update(data).digest("hex");
|
|
@@ -69,490 +79,492 @@ function alive(pid: number): boolean {
|
|
|
69
79
|
}
|
|
70
80
|
}
|
|
71
81
|
|
|
72
|
-
const mac = process.platform === "darwin";
|
|
73
|
-
const windows = process.platform === "win32";
|
|
74
|
-
|
|
75
82
|
/**
|
|
76
83
|
* The running app, from the executable path: macOS <App>.app/Contents/MacOS/<exe>, Windows and
|
|
77
84
|
* Linux the folder of <exe> with resources/ (packages/desktop/src/ffi.ts resolvePaths).
|
|
78
85
|
*/
|
|
79
|
-
function appPath(): string | null {
|
|
86
|
+
function appPath(mac: boolean): string | null {
|
|
80
87
|
const exeDir = dirname(process.execPath);
|
|
81
88
|
if (!mac) return existsSync(join(exeDir, "resources", "boot.json")) ? exeDir : null;
|
|
82
89
|
const app = dirname(dirname(exeDir));
|
|
83
90
|
return basename(exeDir) === "MacOS" && app.endsWith(".app") ? app : null;
|
|
84
91
|
}
|
|
85
92
|
|
|
86
|
-
const resourcesOf = (app: string) => (mac ? join(app, "Contents", "Resources") : join(app, "resources"));
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
93
|
+
const resourcesOf = (app: string, mac: boolean) => (mac ? join(app, "Contents", "Resources") : join(app, "resources"));
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Windows: the version Settings > Apps shows for an app the installer put here (windows-installer.ts writes the key
|
|
97
|
+
* and puts the uninstaller beside the folder). A copy of the app elsewhere leaves the installed one's entry alone.
|
|
98
|
+
* Best effort: the update stands anyway.
|
|
99
|
+
*/
|
|
100
|
+
async function recordInstalledVersion(appId: string, app: string | null, version: string): Promise<void> {
|
|
101
|
+
if (!app || !version || !existsSync(`${app}.uninstall.exe`)) return;
|
|
102
|
+
const key = `HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\${appId}`;
|
|
103
|
+
|
|
104
|
+
const reg = async (args: string[]) =>
|
|
105
|
+
(await Bun.spawn(["reg.exe", ...args], { stdin: "ignore", stdout: "ignore", stderr: "ignore", windowsHide: true })
|
|
106
|
+
.exited) === 0;
|
|
107
|
+
try {
|
|
108
|
+
if (await reg(["query", key, "/v", "InstallLocation"]))
|
|
109
|
+
await reg(["add", key, "/v", "DisplayVersion", "/t", "REG_SZ", "/d", version, "/f"]);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.warn("[akan-native] updates: cannot record the installed version", error);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
90
114
|
|
|
91
|
-
function readConfig():
|
|
92
|
-
const
|
|
93
|
-
const path = app ? join(resourcesOf(app), "updates.json") : "";
|
|
115
|
+
function readConfig(app: string | null, mac: boolean): UpdatesConfig | null {
|
|
116
|
+
const path = app ? join(resourcesOf(app, mac), "updates.json") : "";
|
|
94
117
|
if (!app || !existsSync(path)) return null;
|
|
95
|
-
const c = JSON.parse(readFileSync(path, "utf8")) as
|
|
118
|
+
const c = JSON.parse(readFileSync(path, "utf8")) as UpdatesConfig;
|
|
96
119
|
return typeof c.url === "string" && typeof c.publicKey === "string" ? c : null;
|
|
97
120
|
}
|
|
98
121
|
|
|
99
|
-
export
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
try {
|
|
110
|
-
const s = JSON.parse(readFileSync(statePath(), "utf8")) as State;
|
|
111
|
-
return { ...s, failed: Array.isArray(s.failed) ? s.failed : [] };
|
|
112
|
-
} catch {
|
|
113
|
-
return { failed: [] };
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
const writeState = (state: State) => {
|
|
117
|
-
mkdirSync(dir, { recursive: true });
|
|
118
|
-
const tmp = `${statePath()}.tmp`;
|
|
119
|
-
writeFileSync(tmp, JSON.stringify({ ...state, failed: state.failed.slice(-20) }));
|
|
120
|
-
renameSync(tmp, statePath());
|
|
121
|
-
};
|
|
122
|
-
const need = (): Config => {
|
|
123
|
-
if (!config)
|
|
124
|
-
throw new AkanNativeError(
|
|
125
|
-
"UNSUPPORTED",
|
|
126
|
-
"updates are not configured (akan-native.config.ts `updates`, a built app)",
|
|
127
|
-
);
|
|
128
|
-
return config;
|
|
129
|
-
};
|
|
130
|
-
/** The running app is this release (installed by an update), not another build. */
|
|
131
|
-
const runningIs = (entry: Entry | undefined) => !!entry && !!config && entry.build === config.embeddedSequence;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Once this process has exited (single-instance, locks): renames `moves` in order and starts
|
|
135
|
-
* `app` with this environment. macOS and Linux rename a running app's folder right away, so only
|
|
136
|
-
* the start waits; Windows refuses that, so a helper does the renames too (and on a failed
|
|
137
|
-
* rename puts back what it moved, so there is always an app to start).
|
|
138
|
-
*/
|
|
139
|
-
const moveThenRelaunch = (moves: [string, string][], app: string): Promise<void> => {
|
|
140
|
-
const exe = executableIn(app);
|
|
141
|
-
if (!windows) {
|
|
142
|
-
for (const [from, to] of moves) renameSync(from, to);
|
|
143
|
-
const child = spawn(
|
|
144
|
-
"/bin/sh",
|
|
145
|
-
["-c", 'while kill -0 "$AKAN_NATIVE_PARENT" 2>/dev/null; do sleep 0.2; done; exec "$AKAN_NATIVE_EXE"'],
|
|
146
|
-
{
|
|
147
|
-
detached: true,
|
|
148
|
-
stdio: "inherit",
|
|
149
|
-
env: { ...process.env, AKAN_NATIVE_PARENT: String(process.pid), AKAN_NATIVE_EXE: exe },
|
|
150
|
-
},
|
|
151
|
-
);
|
|
152
|
-
child.unref();
|
|
153
|
-
return Promise.resolve();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const script = `
|
|
157
|
-
$ErrorActionPreference = 'Stop'
|
|
158
|
-
$ProgressPreference = 'SilentlyContinue'
|
|
159
|
-
Wait-Process -Id $env:AKAN_NATIVE_PARENT -ErrorAction SilentlyContinue
|
|
160
|
-
$moves = $env:AKAN_NATIVE_MOVES | ConvertFrom-Json
|
|
161
|
-
$done = @()
|
|
162
|
-
try {
|
|
163
|
-
foreach ($m in $moves) {
|
|
164
|
-
for ($i = 0; ; $i++) { try { Move-Item -LiteralPath $m[0] -Destination $m[1]; break } catch { if ($i -ge 40) { throw }; Start-Sleep -Milliseconds 250 } }
|
|
165
|
-
$done = ,$m + $done
|
|
166
|
-
}
|
|
167
|
-
} catch {
|
|
168
|
-
foreach ($m in $done) { try { Move-Item -LiteralPath $m[1] -Destination $m[0] } catch {} }
|
|
122
|
+
export interface DesktopUpdatesOptions {
|
|
123
|
+
platform?: NodeJS.Platform;
|
|
124
|
+
/** The installed app; the running one by default. */
|
|
125
|
+
app?: string | null;
|
|
126
|
+
/** Its updates.json; read from `app` by default. */
|
|
127
|
+
config?: UpdatesConfig | null;
|
|
128
|
+
/** The executable's file name inside an app; the running one's by default. */
|
|
129
|
+
exeName?: string;
|
|
130
|
+
relaunch?(exe: string, moves: [string, string][]): Promise<void>;
|
|
131
|
+
serverBootAllowance?: number;
|
|
169
132
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
[trial.previous, app],
|
|
231
|
-
],
|
|
232
|
-
app,
|
|
233
|
-
);
|
|
234
|
-
quit(0);
|
|
235
|
-
} else writeState(state);
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* At most `limit` bytes (the manifest and its signature are read before they are verified, so a
|
|
240
|
-
* bad server or a changed response could otherwise send anything; a file has its signed size).
|
|
241
|
-
*/
|
|
242
|
-
const fetchBytes = async (url: string, limit: number, ctx?: DesktopContext): Promise<Uint8Array> => {
|
|
133
|
+
|
|
134
|
+
export function createDesktopUpdates(options: DesktopUpdatesOptions = {}): DesktopPlugin<UpdatesApi, UpdatesEvents> {
|
|
135
|
+
const platform = options.platform ?? process.platform;
|
|
136
|
+
const mac = platform === "darwin";
|
|
137
|
+
const windows = platform === "win32";
|
|
138
|
+
const exeName = options.exeName ?? basename(process.execPath);
|
|
139
|
+
const installed = options.app !== undefined ? options.app : appPath(mac);
|
|
140
|
+
const config = options.config !== undefined ? options.config : readConfig(installed, mac);
|
|
141
|
+
const relaunch = options.relaunch ?? ((exe, moves) => relaunchAfterExit(exe, moves));
|
|
142
|
+
const allowance = options.serverBootAllowance ?? SERVER_BOOT_ALLOWANCE;
|
|
143
|
+
/** The executable inside an app with the running one's name. */
|
|
144
|
+
const executableIn = (app: string) => (mac ? join(app, "Contents", "MacOS", exeName) : join(app, exeName));
|
|
145
|
+
const resources = (app: string) => resourcesOf(app, mac);
|
|
146
|
+
let dir = "";
|
|
147
|
+
let onTrial = false;
|
|
148
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
149
|
+
let downloading = false;
|
|
150
|
+
let serverAnswered: Promise<boolean> = Promise.resolve(true);
|
|
151
|
+
|
|
152
|
+
const statePath = () => join(dir, "state.json");
|
|
153
|
+
const readState = (): State => {
|
|
154
|
+
try {
|
|
155
|
+
const s = JSON.parse(readFileSync(statePath(), "utf8")) as State;
|
|
156
|
+
return { ...s, failed: Array.isArray(s.failed) ? s.failed : [] };
|
|
157
|
+
} catch {
|
|
158
|
+
return { failed: [] };
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const writeState = (state: State) => {
|
|
162
|
+
mkdirSync(dir, { recursive: true });
|
|
163
|
+
const tmp = `${statePath()}.tmp`;
|
|
164
|
+
writeFileSync(tmp, JSON.stringify({ ...state, failed: state.failed.slice(-20) }));
|
|
165
|
+
renameSync(tmp, statePath());
|
|
166
|
+
};
|
|
167
|
+
const need = (): UpdatesConfig => {
|
|
168
|
+
if (!config)
|
|
169
|
+
throw new AkanNativeError(
|
|
170
|
+
"UNSUPPORTED",
|
|
171
|
+
"updates are not configured (akan-native.config.ts `updates`, a built app)",
|
|
172
|
+
);
|
|
173
|
+
return config;
|
|
174
|
+
};
|
|
175
|
+
/** The running app is this release (installed by an update), not another build. */
|
|
176
|
+
const runningIs = (entry: Entry | undefined) => !!entry && !!config && entry.build === config.embeddedSequence;
|
|
177
|
+
|
|
178
|
+
const moveThenRelaunch = (moves: [string, string][], app: string) => relaunch(executableIn(app), moves);
|
|
179
|
+
|
|
180
|
+
/** Puts .previous back, marks the trial failed and relaunches the old app. */
|
|
181
|
+
const rollBack = async (state: State, quit: (code: number) => void, why: string): Promise<void> => {
|
|
182
|
+
const trial = state.trial;
|
|
183
|
+
if (!trial || !installed) return;
|
|
184
|
+
const app = installed;
|
|
185
|
+
console.warn(`[akan-native] updates: ${trial.bundle} ${why}; rolling back`);
|
|
186
|
+
state.trial = undefined;
|
|
187
|
+
state.failed.push(trial.bundle);
|
|
188
|
+
state.rolledBack = trial.bundle;
|
|
189
|
+
if (existsSync(trial.previous)) {
|
|
190
|
+
const failed = `${app}.failed-${trial.bundle}`;
|
|
191
|
+
rmSync(failed, { recursive: true, force: true });
|
|
192
|
+
writeState(state);
|
|
243
193
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
let received = 0;
|
|
251
|
-
for await (const chunk of response.body ?? []) {
|
|
252
|
-
chunks.push(chunk);
|
|
253
|
-
received += chunk.length;
|
|
254
|
-
if (received > limit) {
|
|
255
|
-
void response.body?.cancel().catch(() => {});
|
|
256
|
-
throw tooBig();
|
|
257
|
-
}
|
|
258
|
-
ctx?.emit("progress", { received, total: limit });
|
|
259
|
-
}
|
|
260
|
-
const out = new Uint8Array(received);
|
|
261
|
-
let at = 0;
|
|
262
|
-
for (const c of chunks) out.set(c, at), (at += c.length);
|
|
263
|
-
return out;
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
const releases = (c: Config) => `${c.url}/${c.platform}-${process.arch}`;
|
|
267
|
-
|
|
268
|
-
const fetchRelease = async (c: Config): Promise<Manifest> => {
|
|
269
|
-
const base = `${releases(c)}/${c.channel}.json`;
|
|
270
|
-
const manifest = await fetchBytes(base, MAX_MANIFEST);
|
|
271
|
-
const signature = Buffer.from(
|
|
272
|
-
new TextDecoder().decode(await fetchBytes(`${base}.sig`, MAX_SIGNATURE)).trim(),
|
|
273
|
-
"base64",
|
|
194
|
+
await moveThenRelaunch(
|
|
195
|
+
[
|
|
196
|
+
[app, failed],
|
|
197
|
+
[trial.previous, app],
|
|
198
|
+
],
|
|
199
|
+
app,
|
|
274
200
|
);
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
201
|
+
quit(0);
|
|
202
|
+
} else writeState(state);
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* At most `limit` bytes (the manifest and its signature are read before they are verified, so a
|
|
207
|
+
* bad server or a changed response could otherwise send anything; a file has its signed size).
|
|
208
|
+
*/
|
|
209
|
+
const fetchBytes = async (url: string, limit: number, ctx?: DesktopContext): Promise<Uint8Array> => {
|
|
210
|
+
|
|
211
|
+
const response = await fetch(url, { cache: "no-store", ...(ctx?.signal ? { signal: ctx.signal } : {}) });
|
|
212
|
+
if (response.status === 404) throw new AkanNativeError("NOT_FOUND", `${url} not found`);
|
|
213
|
+
if (!response.ok) throw new AkanNativeError("INTERNAL", `${url}: HTTP ${response.status}`);
|
|
214
|
+
const tooBig = () => new AkanNativeError("INTERNAL", `${url} is larger than ${limit} bytes`);
|
|
215
|
+
if (Number(response.headers.get("content-length") ?? 0) > limit) throw tooBig();
|
|
216
|
+
const chunks: Uint8Array[] = [];
|
|
217
|
+
let received = 0;
|
|
218
|
+
for await (const chunk of response.body ?? []) {
|
|
219
|
+
chunks.push(chunk);
|
|
220
|
+
received += chunk.length;
|
|
221
|
+
if (received > limit) {
|
|
222
|
+
void response.body?.cancel().catch(() => {});
|
|
223
|
+
throw tooBig();
|
|
293
224
|
}
|
|
225
|
+
ctx?.emit("progress", { received, total: limit });
|
|
226
|
+
}
|
|
227
|
+
const out = new Uint8Array(received);
|
|
228
|
+
let at = 0;
|
|
229
|
+
for (const c of chunks) out.set(c, at), (at += c.length);
|
|
230
|
+
return out;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const releases = (c: UpdatesConfig) => `${c.url}/${c.platform}-${process.arch}`;
|
|
234
|
+
|
|
235
|
+
const fetchRelease = async (c: UpdatesConfig): Promise<Manifest> => {
|
|
236
|
+
const base = `${releases(c)}/${c.channel}.json`;
|
|
237
|
+
const manifest = await fetchBytes(base, MAX_MANIFEST);
|
|
238
|
+
const signature = Buffer.from(
|
|
239
|
+
new TextDecoder().decode(await fetchBytes(`${base}.sig`, MAX_SIGNATURE)).trim(),
|
|
240
|
+
"base64",
|
|
241
|
+
);
|
|
242
|
+
const spki = Buffer.concat([Buffer.from("302a300506032b6570032100", "hex"), Buffer.from(c.publicKey, "base64")]);
|
|
243
|
+
if (!verify(null, manifest, createPublicKey({ key: spki, format: "der", type: "spki" }), signature)) {
|
|
244
|
+
throw new AkanNativeError("INTERNAL", "the release signature does not match updates.publicKey");
|
|
245
|
+
}
|
|
246
|
+
const m = JSON.parse(new TextDecoder().decode(manifest)) as Manifest;
|
|
247
|
+
const hex = (s: unknown) => typeof s === "string" && /^[0-9a-f]{64}$/.test(s);
|
|
248
|
+
if (
|
|
249
|
+
m.schema !== 1 ||
|
|
250
|
+
m.kind !== "app" ||
|
|
251
|
+
m.app !== c.app ||
|
|
252
|
+
m.platform !== c.platform ||
|
|
253
|
+
m.arch !== process.arch ||
|
|
254
|
+
m.channel !== c.channel
|
|
255
|
+
) {
|
|
256
|
+
throw new AkanNativeError("INTERNAL", "invalid release manifest: made for another app, platform, CPU or channel");
|
|
257
|
+
}
|
|
258
|
+
if (
|
|
259
|
+
typeof m.bundle !== "string" ||
|
|
260
|
+
!isBundleId(m.bundle) ||
|
|
261
|
+
!(m.sequence > 0) ||
|
|
262
|
+
typeof m.build !== "number" ||
|
|
263
|
+
!m.archive ||
|
|
264
|
+
!hex(m.archive.sha256) ||
|
|
265
|
+
!hex(m.archive.gzSha256)
|
|
266
|
+
) {
|
|
267
|
+
throw new AkanNativeError("INTERNAL", "invalid release manifest");
|
|
268
|
+
}
|
|
269
|
+
return m;
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const running = (state: State) => (runningIs(state.current) ? state.current!.sequence : config!.embeddedSequence);
|
|
273
|
+
const isNewer = (m: Manifest, state: State) => m.sequence > running(state) && !state.failed.includes(m.bundle);
|
|
274
|
+
const patchFor = (m: Manifest, state: State) => {
|
|
275
|
+
const tar = runningIs(state.current) ? state.current!.tar : undefined;
|
|
276
|
+
return tar && existsSync(join(dir, "app", `${tar}.tar`)) ? m.patches?.find((p) => p.from === tar) : undefined;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
/** The carried server's first answer, or false once the release's boot allowance ran out. */
|
|
280
|
+
const serverVerdict = (ctx: DesktopContext): Promise<boolean> => {
|
|
281
|
+
const server = ctx.server;
|
|
282
|
+
if (!server) return Promise.resolve(true);
|
|
283
|
+
return new Promise((resolve) => {
|
|
284
|
+
const timer = setTimeout(() => {
|
|
285
|
+
console.warn(`[akan-native] updates: the app's server did not answer ready within ${allowance / 1000} s`);
|
|
286
|
+
resolve(false);
|
|
287
|
+
}, allowance);
|
|
288
|
+
void server.ready.then((ready) => {
|
|
289
|
+
clearTimeout(timer);
|
|
290
|
+
resolve(ready);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const setup = (ctx: DesktopContext) => {
|
|
296
|
+
|
|
297
|
+
dir = join(ctx.appLocalDataDir, "akan-native-updates");
|
|
298
|
+
const app = installed;
|
|
299
|
+
if (!config || !app) return;
|
|
300
|
+
const state = readState();
|
|
301
|
+
|
|
302
|
+
if (state.pending && state.pending.sequence <= running(state)) {
|
|
303
|
+
console.warn(`[akan-native] updates: ${state.pending.bundle} is not newer than this app; dropping it`);
|
|
304
|
+
state.pending = undefined;
|
|
305
|
+
writeState(state);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
for (const name of readdirSync(dirname(app))) {
|
|
309
|
+
const path = join(dirname(app), name);
|
|
294
310
|
if (
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
!(m.sequence > 0) ||
|
|
298
|
-
typeof m.build !== "number" ||
|
|
299
|
-
!m.archive ||
|
|
300
|
-
!hex(m.archive.sha256) ||
|
|
301
|
-
!hex(m.archive.gzSha256)
|
|
311
|
+
name.startsWith(`${basename(app)}.failed-`) ||
|
|
312
|
+
(name.startsWith(`${basename(app)}.update-`) && path !== dirname(state.pending?.staged ?? ""))
|
|
302
313
|
) {
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
return m;
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
const running = (state: State) => (runningIs(state.current) ? state.current!.sequence : config!.embeddedSequence);
|
|
309
|
-
const isNewer = (m: Manifest, state: State) => m.sequence > running(state) && !state.failed.includes(m.bundle);
|
|
310
|
-
const patchFor = (m: Manifest, state: State) => {
|
|
311
|
-
const tar = runningIs(state.current) ? state.current!.tar : undefined;
|
|
312
|
-
return tar && existsSync(join(dir, "app", `${tar}.tar`)) ? m.patches?.find((p) => p.from === tar) : undefined;
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
const setup = (ctx: DesktopContext) => {
|
|
316
|
-
dir = join(ctx.appDataDir, "akan-native-updates");
|
|
317
|
-
const app = appPath();
|
|
318
|
-
if (!config || !app) return;
|
|
319
|
-
|
|
320
|
-
const state = readState();
|
|
321
|
-
for (const name of readdirSync(dirname(app))) {
|
|
322
|
-
const path = join(dirname(app), name);
|
|
323
|
-
if (
|
|
324
|
-
name.startsWith(`${basename(app)}.failed-`) ||
|
|
325
|
-
(name.startsWith(`${basename(app)}.update-`) && path !== dirname(state.pending?.staged ?? ""))
|
|
326
|
-
) {
|
|
327
|
-
rmSync(path, { recursive: true, force: true });
|
|
328
|
-
}
|
|
314
|
+
rmSync(path, { recursive: true, force: true });
|
|
329
315
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
} else {
|
|
338
|
-
state.trial.pid = process.pid;
|
|
339
|
-
writeState(state);
|
|
340
|
-
onTrial = true;
|
|
341
|
-
ctx.onNativeEvent("pageLoad", (e) => {
|
|
342
|
-
if (e.event !== "finished" || (e.window ?? 1) !== 1 || !onTrial) return;
|
|
343
|
-
clearTimeout(timer);
|
|
344
|
-
timer = setTimeout(() => {
|
|
345
|
-
if (onTrial) void rollBack(readState(), (code) => ctx.quit(code), "did not call notifyReady() in time");
|
|
346
|
-
}, config.readyTimeout);
|
|
347
|
-
});
|
|
316
|
+
}
|
|
317
|
+
if (state.trial) {
|
|
318
|
+
if (!runningIs(state.trial)) {
|
|
319
|
+
|
|
320
|
+
if (state.trial.from === config.embeddedSequence) {
|
|
321
|
+
console.warn(`[akan-native] updates: ${state.trial.bundle} could not be moved into place`);
|
|
322
|
+
state.failed.push(state.trial.bundle);
|
|
348
323
|
}
|
|
324
|
+
state.trial = undefined;
|
|
325
|
+
writeState(state);
|
|
326
|
+
} else if (state.trial.pid !== undefined && state.trial.pid !== process.pid && alive(state.trial.pid)) {
|
|
327
|
+
} else if (++state.trial.attempts > 1) {
|
|
328
|
+
return rollBack(state, (code) => ctx.launch.exit(code), "was started again without notifyReady()");
|
|
329
|
+
} else {
|
|
330
|
+
state.trial.pid = process.pid;
|
|
331
|
+
writeState(state);
|
|
332
|
+
onTrial = true;
|
|
333
|
+
const startClock = () => {
|
|
334
|
+
clearTimeout(timer);
|
|
335
|
+
timer = setTimeout(() => {
|
|
336
|
+
if (onTrial) void rollBack(readState(), (code) => ctx.quit(code), "did not call notifyReady() in time");
|
|
337
|
+
}, config.readyTimeout);
|
|
338
|
+
};
|
|
339
|
+
serverAnswered = serverVerdict(ctx);
|
|
340
|
+
ctx.onNativeEvent("pageLoad", (e) => {
|
|
341
|
+
if (e.event !== "finished" || (e.window ?? 1) !== 1 || !onTrial) return;
|
|
342
|
+
|
|
343
|
+
void serverAnswered.then(() => onTrial && startClock());
|
|
344
|
+
});
|
|
349
345
|
}
|
|
350
|
-
}
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const plugin: DesktopPlugin<UpdatesApi, UpdatesEvents> = {
|
|
350
|
+
id: "updates",
|
|
351
|
+
setup,
|
|
352
|
+
|
|
353
|
+
events: { progress: () => () => {} },
|
|
354
|
+
methods: {
|
|
355
|
+
async getState() {
|
|
356
|
+
const c = need();
|
|
357
|
+
const state = readState();
|
|
358
|
+
const current = runningIs(state.trial) ? state.trial : runningIs(state.current) ? state.current : undefined;
|
|
359
|
+
return {
|
|
360
|
+
bundle: current?.bundle ?? null,
|
|
361
|
+
sequence: current?.sequence ?? 0,
|
|
362
|
+
pending: state.pending?.bundle ?? null,
|
|
363
|
+
trial: onTrial,
|
|
364
|
+
rolledBack: state.rolledBack ?? null,
|
|
365
|
+
channel: c.channel,
|
|
366
|
+
nativeApi: c.nativeApi,
|
|
367
|
+
} satisfies UpdateState;
|
|
368
|
+
},
|
|
351
369
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
async check() {
|
|
374
|
-
const c = need();
|
|
370
|
+
async check() {
|
|
371
|
+
const c = need();
|
|
372
|
+
const m = await fetchRelease(c);
|
|
373
|
+
const state = readState();
|
|
374
|
+
const available = isNewer(m, state);
|
|
375
|
+
return {
|
|
376
|
+
available,
|
|
377
|
+
bundle: m.bundle,
|
|
378
|
+
version: m.version,
|
|
379
|
+
sequence: m.sequence,
|
|
380
|
+
downloadSize: available ? (patchFor(m, state)?.size ?? m.archive!.size) : null,
|
|
381
|
+
} satisfies UpdateCheck;
|
|
382
|
+
},
|
|
383
|
+
|
|
384
|
+
async download(_args, ctx) {
|
|
385
|
+
const c = need();
|
|
386
|
+
const app = installed as string;
|
|
387
|
+
if (downloading) throw new AkanNativeError("CANCELLED", "a download is already running");
|
|
388
|
+
downloading = true;
|
|
389
|
+
try {
|
|
375
390
|
const m = await fetchRelease(c);
|
|
376
391
|
const state = readState();
|
|
377
|
-
|
|
378
|
-
return {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
const app = appPath()!;
|
|
390
|
-
if (downloading) throw new AkanNativeError("CANCELLED", "a download is already running");
|
|
391
|
-
downloading = true;
|
|
392
|
-
try {
|
|
393
|
-
const m = await fetchRelease(c);
|
|
394
|
-
const state = readState();
|
|
395
|
-
if (!isNewer(m, state)) throw new AkanNativeError("NOT_FOUND", "no newer release for this app");
|
|
396
|
-
if (state.pending?.bundle === m.bundle && existsSync(state.pending.staged)) return { bundle: m.bundle };
|
|
397
|
-
|
|
398
|
-
let tar: Uint8Array | null = null;
|
|
399
|
-
const patch = patchFor(m, state);
|
|
400
|
-
if (patch) {
|
|
401
|
-
try {
|
|
402
|
-
const gz = await fetchBytes(`${releases(c)}/${patch.url}`, patch.size, ctx);
|
|
403
|
-
if (sha256(gz) !== patch.sha256) throw new Error("delta hash");
|
|
404
|
-
tar = applyDelta(
|
|
405
|
-
readFileSync(join(dir, "app", `${patch.from}.tar`)),
|
|
406
|
-
Bun.gunzipSync(gz as Uint8Array<ArrayBuffer>),
|
|
407
|
-
);
|
|
408
|
-
} catch (error) {
|
|
409
|
-
console.warn("[akan-native] updates: delta failed, downloading the whole release", error);
|
|
410
|
-
tar = null;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
if (!tar) {
|
|
414
|
-
const gz = await fetchBytes(`${releases(c)}/${m.archive!.url}`, m.archive!.size, ctx);
|
|
415
|
-
if (sha256(gz) !== m.archive!.gzSha256)
|
|
416
|
-
throw new AkanNativeError("INTERNAL", "the release archive does not match its hash");
|
|
417
|
-
tar = Bun.gunzipSync(gz as Uint8Array<ArrayBuffer>);
|
|
418
|
-
}
|
|
419
|
-
if (sha256(tar) !== m.archive!.sha256)
|
|
420
|
-
throw new AkanNativeError("INTERNAL", "the release does not match its hash");
|
|
421
|
-
|
|
422
|
-
const staging = `${app}.update-${m.bundle}`;
|
|
423
|
-
rmSync(staging, { recursive: true, force: true });
|
|
424
|
-
mkdirSync(staging, { recursive: true });
|
|
425
|
-
const tarFile = join(staging, "release.tar");
|
|
426
|
-
writeFileSync(tarFile, tar);
|
|
427
|
-
const run = async (cmd: string[]) => {
|
|
428
|
-
const p = Bun.spawn(cmd, { stdout: "pipe", stderr: "pipe" });
|
|
429
|
-
const [err, code] = await Promise.all([new Response(p.stderr).text(), p.exited]);
|
|
430
|
-
if (code !== 0) throw new AkanNativeError("INTERNAL", `${basename(cmd[0]!)} failed: ${err.trim()}`);
|
|
431
|
-
};
|
|
432
|
-
|
|
433
|
-
await run([windows ? "tar.exe" : "/usr/bin/tar", "-xf", tarFile, "-C", staging]);
|
|
434
|
-
rmSync(tarFile);
|
|
435
|
-
const staged = join(staging, basename(app));
|
|
436
|
-
if (!existsSync(executableIn(staged)))
|
|
437
|
-
throw new AkanNativeError(
|
|
438
|
-
"INTERNAL",
|
|
439
|
-
`the release has no ${basename(app)} with ${basename(process.execPath)}`,
|
|
392
|
+
if (!isNewer(m, state)) throw new AkanNativeError("NOT_FOUND", "no newer release for this app");
|
|
393
|
+
if (state.pending?.bundle === m.bundle && existsSync(state.pending.staged)) return { bundle: m.bundle };
|
|
394
|
+
|
|
395
|
+
let tar: Uint8Array | null = null;
|
|
396
|
+
const patch = patchFor(m, state);
|
|
397
|
+
if (patch) {
|
|
398
|
+
try {
|
|
399
|
+
const gz = await fetchBytes(`${releases(c)}/${patch.url}`, patch.size, ctx);
|
|
400
|
+
if (sha256(gz) !== patch.sha256) throw new Error("delta hash");
|
|
401
|
+
tar = applyDelta(
|
|
402
|
+
readFileSync(join(dir, "app", `${patch.from}.tar`)),
|
|
403
|
+
Bun.gunzipSync(gz as Uint8Array<ArrayBuffer>),
|
|
440
404
|
);
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
"/usr/bin/plutil",
|
|
445
|
-
"-extract",
|
|
446
|
-
"CFBundleIdentifier",
|
|
447
|
-
"raw",
|
|
448
|
-
"-o",
|
|
449
|
-
"-",
|
|
450
|
-
join(staged, "Contents", "Info.plist"),
|
|
451
|
-
])
|
|
452
|
-
.stdout.toString()
|
|
453
|
-
.trim();
|
|
454
|
-
} else {
|
|
455
|
-
try {
|
|
456
|
-
id =
|
|
457
|
-
(
|
|
458
|
-
JSON.parse(readFileSync(join(resourcesOf(staged), "boot.json"), "utf8")) as {
|
|
459
|
-
app?: { id?: string };
|
|
460
|
-
}
|
|
461
|
-
).app?.id ?? "";
|
|
462
|
-
} catch {}
|
|
405
|
+
} catch (error) {
|
|
406
|
+
console.warn("[akan-native] updates: delta failed, downloading the whole release", error);
|
|
407
|
+
tar = null;
|
|
463
408
|
}
|
|
464
|
-
if (id !== c.app)
|
|
465
|
-
throw new AkanNativeError("INTERNAL", `the release is ${id || "not an akan-native app"}, not ${c.app}`);
|
|
466
|
-
|
|
467
|
-
if (mac) await run(["/usr/bin/codesign", "--verify", "--deep", "--strict", staged]);
|
|
468
|
-
|
|
469
|
-
mkdirSync(join(dir, "app"), { recursive: true });
|
|
470
|
-
for (const name of readdirSync(join(dir, "app"))) rmSync(join(dir, "app", name), { force: true });
|
|
471
|
-
writeFileSync(join(dir, "app", `${m.archive!.sha256}.tar`), tar);
|
|
472
|
-
state.pending = {
|
|
473
|
-
bundle: m.bundle,
|
|
474
|
-
sequence: m.sequence,
|
|
475
|
-
version: m.version,
|
|
476
|
-
tar: m.archive!.sha256,
|
|
477
|
-
build: m.build!,
|
|
478
|
-
staged,
|
|
479
|
-
};
|
|
480
|
-
writeState(state);
|
|
481
|
-
return { bundle: m.bundle };
|
|
482
|
-
} finally {
|
|
483
|
-
downloading = false;
|
|
484
409
|
}
|
|
485
|
-
|
|
410
|
+
if (!tar) {
|
|
411
|
+
const gz = await fetchBytes(`${releases(c)}/${m.archive!.url}`, m.archive!.size, ctx);
|
|
412
|
+
if (sha256(gz) !== m.archive!.gzSha256)
|
|
413
|
+
throw new AkanNativeError("INTERNAL", "the release archive does not match its hash");
|
|
414
|
+
tar = Bun.gunzipSync(gz as Uint8Array<ArrayBuffer>);
|
|
415
|
+
}
|
|
416
|
+
if (sha256(tar) !== m.archive!.sha256)
|
|
417
|
+
throw new AkanNativeError("INTERNAL", "the release does not match its hash");
|
|
418
|
+
|
|
419
|
+
const staging = `${app}.update-${m.bundle}`;
|
|
420
|
+
rmSync(staging, { recursive: true, force: true });
|
|
421
|
+
mkdirSync(staging, { recursive: true });
|
|
422
|
+
const tarFile = join(staging, "release.tar");
|
|
423
|
+
writeFileSync(tarFile, tar);
|
|
424
|
+
const run = async (cmd: string[]) => {
|
|
425
|
+
const p = Bun.spawn(cmd, { stdout: "pipe", stderr: "pipe" });
|
|
426
|
+
const [err, code] = await Promise.all([new Response(p.stderr).text(), p.exited]);
|
|
427
|
+
if (code !== 0) throw new AkanNativeError("INTERNAL", `${basename(cmd[0]!)} failed: ${err.trim()}`);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
await run([process.platform === "win32" ? "tar.exe" : "/usr/bin/tar", "-xf", tarFile, "-C", staging]);
|
|
431
|
+
rmSync(tarFile);
|
|
432
|
+
|
|
433
|
+
const unpacked = readdirSync(staging);
|
|
434
|
+
const staged = unpacked.length === 1 ? join(staging, unpacked[0] as string) : "";
|
|
435
|
+
if (!staged || !existsSync(executableIn(staged)))
|
|
436
|
+
throw new AkanNativeError("INTERNAL", `the release is not one app with ${exeName}`);
|
|
437
|
+
let id = "";
|
|
438
|
+
if (mac) {
|
|
439
|
+
id = Bun.spawnSync([
|
|
440
|
+
"/usr/bin/plutil",
|
|
441
|
+
"-extract",
|
|
442
|
+
"CFBundleIdentifier",
|
|
443
|
+
"raw",
|
|
444
|
+
"-o",
|
|
445
|
+
"-",
|
|
446
|
+
join(staged, "Contents", "Info.plist"),
|
|
447
|
+
])
|
|
448
|
+
.stdout.toString()
|
|
449
|
+
.trim();
|
|
450
|
+
} else {
|
|
451
|
+
try {
|
|
452
|
+
id =
|
|
453
|
+
(
|
|
454
|
+
JSON.parse(readFileSync(join(resources(staged), "boot.json"), "utf8")) as {
|
|
455
|
+
app?: { id?: string };
|
|
456
|
+
}
|
|
457
|
+
).app?.id ?? "";
|
|
458
|
+
} catch {}
|
|
459
|
+
}
|
|
460
|
+
if (id !== c.app)
|
|
461
|
+
throw new AkanNativeError("INTERNAL", `the release is ${id || "not an akan-native app"}, not ${c.app}`);
|
|
486
462
|
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
const
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
throw new AkanNativeError("NOT_FOUND", "no downloaded update to apply");
|
|
494
|
-
const previous = `${app}.previous`;
|
|
495
|
-
rmSync(previous, { recursive: true, force: true });
|
|
496
|
-
const { staged: _staged, ...entry } = pending;
|
|
497
|
-
if (windows) {
|
|
498
|
-
|
|
499
|
-
state.trial = { ...entry, previous, attempts: 0 };
|
|
500
|
-
state.pending = undefined;
|
|
463
|
+
if (mac) await run(["/usr/bin/codesign", "--verify", "--deep", "--strict", staged]);
|
|
464
|
+
|
|
465
|
+
const carries = (root: string) => existsSync(join(resources(root), "server.json"));
|
|
466
|
+
if (carries(staged) !== carries(app)) {
|
|
467
|
+
rmSync(staging, { recursive: true, force: true });
|
|
468
|
+
state.failed.push(m.bundle);
|
|
501
469
|
writeState(state);
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
[pending.staged, app],
|
|
506
|
-
],
|
|
507
|
-
app,
|
|
470
|
+
throw new AkanNativeError(
|
|
471
|
+
"NOT_ALLOWED",
|
|
472
|
+
`the release ${carries(app) ? "carries no server, and this app does" : "carries a server, and this app does not"}; install it instead`,
|
|
508
473
|
);
|
|
509
|
-
}
|
|
474
|
+
}
|
|
510
475
|
|
|
511
|
-
|
|
512
|
-
|
|
476
|
+
mkdirSync(join(dir, "app"), { recursive: true });
|
|
477
|
+
for (const name of readdirSync(join(dir, "app"))) rmSync(join(dir, "app", name), { force: true });
|
|
478
|
+
writeFileSync(join(dir, "app", `${m.archive!.sha256}.tar`), tar);
|
|
479
|
+
state.pending = {
|
|
480
|
+
bundle: m.bundle,
|
|
481
|
+
sequence: m.sequence,
|
|
482
|
+
version: m.version,
|
|
483
|
+
tar: m.archive!.sha256,
|
|
484
|
+
build: m.build!,
|
|
485
|
+
staged,
|
|
486
|
+
};
|
|
487
|
+
writeState(state);
|
|
488
|
+
return { bundle: m.bundle };
|
|
489
|
+
} finally {
|
|
490
|
+
downloading = false;
|
|
491
|
+
}
|
|
492
|
+
},
|
|
493
|
+
|
|
494
|
+
async apply(_args, ctx) {
|
|
495
|
+
const c = need();
|
|
496
|
+
const app = installed as string;
|
|
497
|
+
const state = readState();
|
|
498
|
+
const pending = state.pending;
|
|
499
|
+
if (!pending || !existsSync(pending.staged) || pending.sequence <= running(state))
|
|
500
|
+
throw new AkanNativeError("NOT_FOUND", "no downloaded update to apply");
|
|
501
|
+
const previous = `${app}.previous`;
|
|
502
|
+
rmSync(previous, { recursive: true, force: true });
|
|
503
|
+
const { staged: _staged, ...entry } = pending;
|
|
504
|
+
if (windows) {
|
|
505
|
+
|
|
506
|
+
state.trial = { ...entry, previous, attempts: 0, from: c.embeddedSequence };
|
|
507
|
+
state.pending = undefined;
|
|
508
|
+
writeState(state);
|
|
509
|
+
await moveThenRelaunch(
|
|
510
|
+
[
|
|
511
|
+
[app, previous],
|
|
512
|
+
[pending.staged, app],
|
|
513
|
+
],
|
|
514
|
+
app,
|
|
515
|
+
);
|
|
516
|
+
} else {
|
|
517
|
+
|
|
518
|
+
state.trial = { ...entry, previous, attempts: 0 };
|
|
519
|
+
state.pending = undefined;
|
|
520
|
+
writeState(state);
|
|
521
|
+
let moved = false;
|
|
522
|
+
try {
|
|
523
|
+
renameSync(app, previous);
|
|
524
|
+
moved = true;
|
|
525
|
+
renameSync(pending.staged, app);
|
|
526
|
+
} catch (error) {
|
|
527
|
+
if (moved) renameSync(previous, app);
|
|
528
|
+
state.trial = undefined;
|
|
529
|
+
state.pending = pending;
|
|
513
530
|
writeState(state);
|
|
514
|
-
|
|
515
|
-
try {
|
|
516
|
-
renameSync(app, previous);
|
|
517
|
-
moved = true;
|
|
518
|
-
renameSync(pending.staged, app);
|
|
519
|
-
} catch (error) {
|
|
520
|
-
if (moved) renameSync(previous, app);
|
|
521
|
-
state.trial = undefined;
|
|
522
|
-
state.pending = pending;
|
|
523
|
-
writeState(state);
|
|
524
|
-
throw new AkanNativeError("INTERNAL", `could not move the update into place: ${error}`);
|
|
525
|
-
}
|
|
526
|
-
rmSync(dirname(pending.staged), { recursive: true, force: true });
|
|
527
|
-
await moveThenRelaunch([], app);
|
|
531
|
+
throw new AkanNativeError("INTERNAL", `could not move the update into place: ${error}`);
|
|
528
532
|
}
|
|
529
|
-
|
|
530
|
-
|
|
533
|
+
rmSync(dirname(pending.staged), { recursive: true, force: true });
|
|
534
|
+
await moveThenRelaunch([], app);
|
|
535
|
+
}
|
|
536
|
+
setTimeout(() => ctx.quit(0), 50);
|
|
537
|
+
},
|
|
531
538
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
539
|
+
async notifyReady() {
|
|
540
|
+
const c = need();
|
|
541
|
+
if (!onTrial) return;
|
|
542
|
+
|
|
543
|
+
if (!(await serverAnswered)) return;
|
|
544
|
+
if (!onTrial) return;
|
|
545
|
+
clearTimeout(timer);
|
|
546
|
+
onTrial = false;
|
|
547
|
+
const state = readState();
|
|
548
|
+
const trial = state.trial;
|
|
549
|
+
if (!trial) return;
|
|
550
|
+
const { previous, attempts: _attempts, ...entry } = trial;
|
|
551
|
+
state.current = entry;
|
|
552
|
+
state.trial = undefined;
|
|
553
|
+
state.rolledBack = undefined;
|
|
554
|
+
writeState(state);
|
|
555
|
+
rmSync(previous, { recursive: true, force: true });
|
|
556
|
+
if (windows) void recordInstalledVersion(c.app, installed, entry.version);
|
|
557
|
+
},
|
|
547
558
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
},
|
|
559
|
+
async reset() {
|
|
560
|
+
need();
|
|
561
|
+
const state = readState();
|
|
562
|
+
if (state.pending) rmSync(dirname(state.pending.staged), { recursive: true, force: true });
|
|
563
|
+
writeState({ failed: [], ...(state.current ? { current: state.current } : {}) });
|
|
554
564
|
},
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
565
|
+
},
|
|
566
|
+
};
|
|
567
|
+
return plugin;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
export default defineDesktopPlugin<UpdatesApi, UpdatesEvents>(createDesktopUpdates());
|