@adea-ai/app-framework 0.30.0 → 0.32.0

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.
Files changed (148) hide show
  1. package/README.md +26 -11
  2. package/dist/app/application.js +4 -2
  3. package/dist/app/create-application.js +4 -3
  4. package/dist/app/serial-queue.d.ts +5 -0
  5. package/dist/app/serial-queue.js +14 -0
  6. package/dist/app/value-guards.d.ts +15 -0
  7. package/dist/app/value-guards.js +29 -0
  8. package/dist/appearance/appearance-host.js +6 -0
  9. package/dist/appearance/appearance-preferences.js +6 -2
  10. package/dist/appearance/composition.d.ts +1 -1
  11. package/dist/appearance/custom-theme-import.js +12 -19
  12. package/dist/appearance/custom-theme-library.d.ts +5 -4
  13. package/dist/appearance/custom-theme-library.js +20 -2
  14. package/dist/appearance/editor.js +4 -1
  15. package/dist/appearance/legacy-imported-theme-token-projection.d.ts +84 -0
  16. package/dist/appearance/legacy-imported-theme-token-projection.js +142 -0
  17. package/dist/appearance/theme-record-adapter.js +37 -9
  18. package/dist/ci/toolchain-identity.js +1 -1
  19. package/dist/desktop/channel-proof.js +2 -1
  20. package/dist/desktop/client-asset-stream.js +6 -12
  21. package/dist/desktop/desktop-host.d.ts +25 -5
  22. package/dist/desktop/desktop-host.js +75 -20
  23. package/dist/framework-source.json +1 -1
  24. package/dist/installer/command-artifact.js +4 -3
  25. package/dist/installer/command-qualification-command.js +6 -5
  26. package/dist/installer/hosted-runner.d.ts +9 -2
  27. package/dist/installer/hosted-runner.js +18 -5
  28. package/dist/installer/installed-payload.js +20 -46
  29. package/dist/installer/installer-archive.d.ts +33 -0
  30. package/dist/installer/installer-archive.js +116 -0
  31. package/dist/installer/installer-ci-bootstrap.js +10 -6
  32. package/dist/installer/installer-extraction.js +54 -120
  33. package/dist/installer/installer-identity.js +33 -111
  34. package/dist/installer/installer-observation.js +3 -2
  35. package/dist/installer/installer-process-cleanup.js +7 -46
  36. package/dist/installer/installer-qualification-command.js +6 -5
  37. package/dist/installer/installer-upgrade-command.js +5 -5
  38. package/dist/installer/installer-worker-guard.js +2 -1
  39. package/dist/native/canonical-path.d.ts +5 -0
  40. package/dist/native/canonical-path.js +10 -0
  41. package/dist/native/cef-helper.js +5 -4
  42. package/dist/native/electrobun-source-patch.js +3 -2
  43. package/dist/native/{directory-exchange.d.ts → native-rename.d.ts} +2 -0
  44. package/dist/native/native-rename.js +126 -0
  45. package/dist/native/native-secret-store.js +2 -6
  46. package/dist/native/native-utilities.js +4 -4
  47. package/dist/native/posix-launcher-build.js +1 -1
  48. package/dist/native/posix-tree-removal.js +2 -2
  49. package/dist/native/windows-process.d.ts +9 -0
  50. package/dist/native/windows-process.js +60 -0
  51. package/dist/release/feed-release-cut.js +2 -1
  52. package/dist/release/framework-package-publish.js +1 -1
  53. package/dist/release/framework-update-lock.js +2 -1
  54. package/dist/release/github-cli.d.ts +18 -0
  55. package/dist/release/github-cli.js +13 -1
  56. package/dist/release/github-release-cut.js +3 -2
  57. package/dist/release/github-release-provenance.js +2 -1
  58. package/dist/release/github-release-publisher.js +2 -1
  59. package/dist/release/local-provenance.js +3 -2
  60. package/dist/release/local-release.js +6 -6
  61. package/dist/release/qualification-run.d.ts +25 -0
  62. package/dist/release/qualification-run.js +43 -0
  63. package/dist/release/release-artifact.js +3 -11
  64. package/dist/release/release-assets-plan.js +16 -6
  65. package/dist/release/release-config.js +14 -14
  66. package/dist/release/release-cut-command.js +14 -4
  67. package/dist/release/release-cut.js +3 -2
  68. package/dist/release/release-descriptor-staging.js +13 -27
  69. package/dist/release/release-finalize.js +2 -1
  70. package/dist/release/release-publication.js +3 -11
  71. package/dist/release/release-publish-command.js +15 -6
  72. package/dist/release/release-qualification.js +3 -2
  73. package/dist/release/retained-release-build.js +12 -12
  74. package/dist/shell/app-shell.js +76 -48
  75. package/dist/shell/app-utilities.d.ts +5 -3
  76. package/dist/shell/app-utilities.js +15 -7
  77. package/dist/shell/help-shortcuts.d.ts +4 -0
  78. package/dist/shell/help-shortcuts.js +38 -7
  79. package/dist/shell/keyboard-target.d.ts +2 -0
  80. package/dist/shell/keyboard-target.js +54 -0
  81. package/dist/shell/settings-sections.d.ts +5 -0
  82. package/dist/shell/settings-shortcuts.d.ts +10 -0
  83. package/dist/shell/settings-shortcuts.js +67 -0
  84. package/dist/update-helper.bundle.js +250 -190
  85. package/dist/updates/desktop-updater.js +53 -58
  86. package/dist/updates/packaged-launch-admission.js +3 -2
  87. package/dist/updates/packaged-release-config.js +6 -5
  88. package/dist/updates/release-feed.js +9 -9
  89. package/dist/updates/signed-release.js +5 -5
  90. package/dist/updates/stable-file.d.ts +18 -0
  91. package/dist/updates/stable-file.js +54 -1
  92. package/dist/updates/update-adapter.d.ts +2 -13
  93. package/dist/updates/update-arm.js +7 -5
  94. package/dist/updates/update-channel-store.js +7 -9
  95. package/dist/updates/update-discovery.js +30 -34
  96. package/dist/updates/update-download-cleanup.js +2 -1
  97. package/dist/updates/update-download.js +2 -4
  98. package/dist/updates/update-extraction.js +1 -1
  99. package/dist/updates/update-generation-lease.js +4 -3
  100. package/dist/updates/update-handoff-plan.js +3 -2
  101. package/dist/updates/update-helper.js +8 -6
  102. package/dist/updates/update-instance-lease.js +3 -2
  103. package/dist/updates/update-launch-gate.js +12 -25
  104. package/dist/updates/update-launch.js +3 -2
  105. package/dist/updates/update-lock.js +3 -2
  106. package/dist/updates/update-marker.js +3 -2
  107. package/dist/updates/update-process-fence.js +10 -39
  108. package/dist/updates/update-process-liveness.js +2 -1
  109. package/dist/updates/update-resume.js +26 -22
  110. package/dist/updates/update-retention-marker.js +7 -5
  111. package/dist/updates/update-retention-plan.js +2 -2
  112. package/dist/updates/update-retirement.js +40 -39
  113. package/dist/updates/update-state-store.js +29 -37
  114. package/dist/updates/update-state.d.ts +8 -3
  115. package/dist/updates/update-swap-stage.js +6 -5
  116. package/dist/updates/update-swap.d.ts +1 -1
  117. package/dist/updates/update-swap.js +8 -6
  118. package/dist/views/app-help.js +2 -1
  119. package/dist/views/app-settings.js +5 -2
  120. package/dist/views/appearance-surface.js +33 -19
  121. package/package.json +7 -13
  122. package/src/app/application.tsx +3 -1
  123. package/src/appearance/appearance-host.tsx +6 -0
  124. package/src/appearance/appearance-preferences.ts +7 -1
  125. package/src/appearance/composition.ts +3 -1
  126. package/src/appearance/custom-theme-import.ts +21 -37
  127. package/src/appearance/custom-theme-library.ts +21 -5
  128. package/src/appearance/editor.ts +7 -6
  129. package/src/appearance/legacy-imported-theme-token-projection.ts +51 -0
  130. package/src/appearance/theme-record-adapter.ts +58 -11
  131. package/src/desktop/client-asset-stream.ts +6 -12
  132. package/src/shell/app-shell.tsx +88 -58
  133. package/src/shell/app-utilities.tsx +29 -17
  134. package/src/shell/help-shortcuts.ts +77 -6
  135. package/src/shell/keyboard-target.ts +74 -0
  136. package/src/shell/settings-sections.ts +5 -0
  137. package/src/shell/settings-shortcuts.ts +116 -0
  138. package/src/updates/update-adapter.ts +2 -14
  139. package/src/updates/update-state.ts +11 -2
  140. package/src/views/app-settings.tsx +5 -3
  141. package/src/views/appearance-surface.tsx +63 -27
  142. package/dist/native/directory-exchange.js +0 -90
  143. package/dist/native/exclusive-rename.d.ts +0 -2
  144. package/dist/native/exclusive-rename.js +0 -52
  145. package/dist/release/retention-dry-run.d.ts +0 -46
  146. package/dist/release/retention-dry-run.js +0 -94
  147. package/dist/updates/update-exclusive-rename.d.ts +0 -3
  148. package/dist/updates/update-exclusive-rename.js +0 -5
@@ -0,0 +1,33 @@
1
+ import type { Stats } from 'node:fs';
2
+ import type { InstallerPlatform } from './installer-layout';
3
+ /** Whether `path` lies strictly inside `root`, judged lexically. */
4
+ export declare function isWithin(root: string, path: string): boolean;
5
+ /** An archive member name without a leading `./`, with `/` separators. */
6
+ export declare function normalizeArchiveMember(name: string): string;
7
+ /** The one listed member named `expected` (case-insensitively on Windows), else `message`. */
8
+ export declare function findArchiveMember(entries: readonly string[], expected: string, platform: InstallerPlatform, message: string): string;
9
+ /** Fail with `message` unless `archive` is still the regular file `before` (lstat) described. */
10
+ export declare function assertArchiveUnchanged(archive: string, before: Stats, message: string): Promise<void>;
11
+ /**
12
+ * Stream one archive member through `tar -xOf` into a new private file, refusing
13
+ * members larger than `maximum`, and return its size. Installer stderr is drained
14
+ * and discarded; a failed or empty extraction rejects.
15
+ */
16
+ export declare function extractArchiveMember(options: {
17
+ tar: string;
18
+ archive: string;
19
+ member: string;
20
+ destination: string;
21
+ maximum: number;
22
+ env: Record<string, string>;
23
+ }): Promise<number>;
24
+ /** Require a regular, native executable for `platform` at `path`. */
25
+ export declare function assertInstallerExecutable(path: string, platform: InstallerPlatform): Promise<void>;
26
+ /**
27
+ * Require that a Linux self-extracting installer of `size` bytes ends with the
28
+ * Electrobun archive marker followed by exactly the release archive.
29
+ */
30
+ export declare function assertEmbeddedArchive(path: string, size: number, archive: {
31
+ size: number;
32
+ digest: string;
33
+ }): Promise<void>;
@@ -0,0 +1,116 @@
1
+ import { sameFile } from "../updates/stable-file.js";
2
+ import { assertNativeExecutable } from "./native-executable.js";
3
+ import { lstat, open } from "node:fs/promises";
4
+ import { isAbsolute, relative, sep } from "node:path";
5
+ import { createHash } from "node:crypto";
6
+ // src/installer/installer-archive.ts
7
+ var EMBEDDED_ARCHIVE_MARKER = Buffer.from("ELECTROBUN_ARCHIVE_V1");
8
+ /** Whether `path` lies strictly inside `root`, judged lexically. */
9
+ function isWithin(root, path) {
10
+ const rel = relative(root, path);
11
+ return rel !== "" && rel !== ".." && !rel.startsWith(".." + sep) && !isAbsolute(rel);
12
+ }
13
+ /** An archive member name without a leading `./`, with `/` separators. */
14
+ function normalizeArchiveMember(name) {
15
+ return name.replace(/^\.\//, "").replaceAll("\\", "/");
16
+ }
17
+ /** The one listed member named `expected` (case-insensitively on Windows), else `message`. */
18
+ function findArchiveMember(entries, expected, platform, message) {
19
+ const windows = platform === "windows-x64", wanted = windows ? expected.toLowerCase() : expected;
20
+ const found = entries.filter((entry) => {
21
+ const name = normalizeArchiveMember(entry);
22
+ return (windows ? name.toLowerCase() : name) === wanted;
23
+ });
24
+ if (found.length !== 1) throw new Error(message);
25
+ return found[0];
26
+ }
27
+ /** Fail with `message` unless `archive` is still the regular file `before` (lstat) described. */
28
+ async function assertArchiveUnchanged(archive, before, message) {
29
+ const after = await lstat(archive);
30
+ if (!after.isFile() || after.isSymbolicLink() || !sameFile(after, before)) throw new Error(message);
31
+ }
32
+ /**
33
+ * Stream one archive member through `tar -xOf` into a new private file, refusing
34
+ * members larger than `maximum`, and return its size. Installer stderr is drained
35
+ * and discarded; a failed or empty extraction rejects.
36
+ */
37
+ async function extractArchiveMember(options) {
38
+ const output = await open(options.destination, "wx", 384);
39
+ try {
40
+ const child = Bun.spawn({
41
+ cmd: [
42
+ options.tar,
43
+ "-xOf",
44
+ options.archive,
45
+ options.member
46
+ ],
47
+ env: options.env,
48
+ stdout: "pipe",
49
+ stderr: "pipe",
50
+ signal: AbortSignal.timeout(12e4)
51
+ });
52
+ const errors = (async () => {
53
+ for await (const _chunk of child.stderr);
54
+ })();
55
+ let count = 0;
56
+ try {
57
+ for await (const chunk of child.stdout) {
58
+ count += chunk.length;
59
+ if (!Number.isSafeInteger(count) || count > options.maximum) throw new Error("Installer member exceeds bound");
60
+ let offset = 0;
61
+ while (offset < chunk.length) {
62
+ const result = await output.write(chunk, offset, chunk.length - offset);
63
+ if (!result.bytesWritten) throw new Error("Cannot extract installer member");
64
+ offset += result.bytesWritten;
65
+ }
66
+ }
67
+ if (await child.exited !== 0 || count === 0) throw new Error("Cannot extract installer member");
68
+ await errors;
69
+ return count;
70
+ } catch (error) {
71
+ child.kill();
72
+ await child.exited;
73
+ await errors;
74
+ throw error;
75
+ }
76
+ } finally {
77
+ await output.close();
78
+ }
79
+ }
80
+ /** Require a regular, native executable for `platform` at `path`. */
81
+ async function assertInstallerExecutable(path, platform) {
82
+ const info = await lstat(path);
83
+ if (!info.isFile() || info.isSymbolicLink() || info.size < 4096) throw new Error("Invalid installer executable");
84
+ const file = await open(path, "r");
85
+ try {
86
+ await assertNativeExecutable(file, info.size, platform, "installer executable");
87
+ } finally {
88
+ await file.close();
89
+ }
90
+ }
91
+ /**
92
+ * Require that a Linux self-extracting installer of `size` bytes ends with the
93
+ * Electrobun archive marker followed by exactly the release archive.
94
+ */
95
+ async function assertEmbeddedArchive(path, size, archive) {
96
+ const marker = EMBEDDED_ARCHIVE_MARKER;
97
+ if (size <= archive.size + marker.length) throw new Error("Installer archive is incomplete");
98
+ const file = await open(path, "r");
99
+ try {
100
+ const position = size - archive.size - marker.length, actual = Buffer.alloc(marker.length);
101
+ if ((await file.read(actual, 0, actual.length, position)).bytesRead !== actual.length || !actual.equals(marker)) throw new Error("Installer archive marker is missing");
102
+ const hash = createHash("sha256");
103
+ let read = 0;
104
+ for await (const chunk of file.createReadStream({
105
+ start: position + marker.length,
106
+ autoClose: false
107
+ })) {
108
+ read += chunk.length;
109
+ hash.update(chunk);
110
+ }
111
+ if (read !== archive.size || "sha256:" + hash.digest("hex") !== archive.digest) throw new Error("Installer archive differs from release archive");
112
+ } finally {
113
+ await file.close();
114
+ }
115
+ }
116
+ export { assertArchiveUnchanged, assertEmbeddedArchive, assertInstallerExecutable, extractArchiveMember, findArchiveMember, isWithin, normalizeArchiveMember };
@@ -1,4 +1,5 @@
1
- import { isHostedRunner } from "./hosted-runner.js";
1
+ import { errorCode } from "../app/value-guards.js";
2
+ import { isHostedRunner, runnerTemp } from "./hosted-runner.js";
2
3
  import { lstat, mkdir, mkdtemp, readFile, realpath, rm, writeFile } from "node:fs/promises";
3
4
  import { dirname, isAbsolute, join, relative, sep } from "node:path";
4
5
  import { randomBytes } from "node:crypto";
@@ -6,6 +7,7 @@ import { fileURLToPath } from "node:url";
6
7
  // src/installer/installer-ci-bootstrap.ts
7
8
  /** The modules copied into the runner. The worker and preflight scripts import the rest. */
8
9
  var bootstrapModules = [
10
+ "app/value-guards.js",
9
11
  "installer/installer-ci-worker.js",
10
12
  "installer/installer-ci-preflight.js",
11
13
  "installer/installer-worker-guard.js",
@@ -18,8 +20,9 @@ var bootstrapModules = [
18
20
  ];
19
21
  /** Copy a bounded external preload into the current disposable runner's temp directory. */
20
22
  async function prepareInstallerCiBootstrap(env = process.env) {
21
- if (!isHostedRunner(env) || !env.RUNNER_TEMP || !isAbsolute(env.RUNNER_TEMP)) throw new Error("Installer bootstrap requires a disposable GitHub-hosted runner");
22
- const parent = await realpath(env.RUNNER_TEMP);
23
+ const temp = runnerTemp(env);
24
+ if (!isHostedRunner(env) || !temp) throw new Error("Installer bootstrap requires a disposable hosted runner");
25
+ const parent = await realpath(temp);
23
26
  if (/\s/.test(parent) || !(await lstat(parent)).isDirectory()) throw new Error("Runner temp cannot represent a Bun preload path");
24
27
  const source = dirname(dirname(fileURLToPath(import.meta.url)));
25
28
  const directory = await mkdtemp(join(parent, "framework-ci-"));
@@ -60,8 +63,9 @@ async function prepareInstallerCiBootstrap(env = process.env) {
60
63
  /** Exercise the externally prepared guard with the actual candidate Bun runtime. */
61
64
  async function preflightInstallerRuntime(options) {
62
65
  const { env, runtime, bootstrap } = options;
63
- if (!isHostedRunner(env) || !env.RUNNER_TEMP || !isAbsolute(env.RUNNER_TEMP) || !isAbsolute(runtime)) throw new Error("Runtime preflight requires a disposable hosted runner");
64
- const tempRoot = await realpath(env.RUNNER_TEMP), bootstrapRoot = await realpath(bootstrap.directory);
66
+ const temp = runnerTemp(env);
67
+ if (!isHostedRunner(env) || !temp || !isAbsolute(runtime)) throw new Error("Runtime preflight requires a disposable hosted runner");
68
+ const tempRoot = await realpath(temp), bootstrapRoot = await realpath(bootstrap.directory);
65
69
  const relativeBootstrap = relative(tempRoot, bootstrapRoot);
66
70
  if (!relativeBootstrap || relativeBootstrap === ".." || relativeBootstrap.startsWith(".." + sep) || isAbsolute(relativeBootstrap) || /\s/.test(bootstrapRoot) || bootstrap.worker !== join(bootstrapRoot, "installer/installer-ci-worker.js") || bootstrap.preflight !== join(bootstrapRoot, "installer/installer-ci-preflight.js") || bootstrap.bunOptions !== `--preload=${bootstrap.worker}`) throw new Error("Invalid installer bootstrap location");
67
71
  await runInstallerPreflight({
@@ -149,7 +153,7 @@ async function runInstallerPreflight(options) {
149
153
  await lstat(join(temporary, "unexpected"));
150
154
  throw new Error("Runtime preflight recorded unexpected activity");
151
155
  } catch (error) {
152
- if (error.code !== "ENOENT") throw error;
156
+ if (errorCode(error) !== "ENOENT") throw error;
153
157
  }
154
158
  } finally {
155
159
  await rm(temporary, {
@@ -1,11 +1,12 @@
1
+ import { runnerTemp } from "./hosted-runner.js";
1
2
  import { assertInstallerContext, assertInstallerLayout, assertPortableAppName } from "./installer-layout.js";
2
- import { assertNativeExecutable } from "./native-executable.js";
3
+ import { hashFileAt } from "../updates/stable-file.js";
4
+ import { assertEmbeddedArchive, assertInstallerExecutable, extractArchiveMember, findArchiveMember, isWithin } from "./installer-archive.js";
3
5
  import { cleanupInstallerDirectoryAfterFailure, defaultInstallerImageMountIo, withInstallerImageRetries } from "./installer-image-mount.js";
4
6
  import { runInstallerCommand } from "./installer-command.js";
5
7
  import { verifyInstallerPayload } from "./installer-identity.js";
6
- import { chmod, lstat, mkdir, mkdtemp, open, readdir, realpath } from "node:fs/promises";
7
- import { isAbsolute, join, relative, resolve, sep, win32 } from "node:path";
8
- import { createHash } from "node:crypto";
8
+ import { chmod, lstat, mkdir, mkdtemp, readdir, realpath } from "node:fs/promises";
9
+ import { join, resolve, win32 } from "node:path";
9
10
  // src/installer/installer-extraction.ts
10
11
  async function run(cmd, env) {
11
12
  return runInstallerCommand(cmd, env, {
@@ -14,106 +15,14 @@ async function run(cmd, env) {
14
15
  failureMessage: "Installer preparation command failed"
15
16
  });
16
17
  }
17
- async function extract(tar, archive, member, destination, maximum, env) {
18
- const output = await open(destination, "wx", 384);
19
- const spawn = () => Bun.spawn({
20
- cmd: [
21
- tar,
22
- "-xOf",
23
- archive,
24
- member
25
- ],
26
- env,
27
- stdout: "pipe",
28
- stderr: "pipe",
29
- signal: AbortSignal.timeout(12e4)
30
- });
31
- try {
32
- const child = spawn();
33
- const errors = (async () => {
34
- for await (const _chunk of child.stderr);
35
- })();
36
- let count = 0;
37
- try {
38
- for await (const chunk of child.stdout) {
39
- count += chunk.length;
40
- if (!Number.isSafeInteger(count) || count > maximum) throw new Error("Installer member exceeds bound");
41
- let offset = 0;
42
- while (offset < chunk.length) {
43
- const result = await output.write(chunk, offset, chunk.length - offset);
44
- if (!result.bytesWritten) throw new Error("Cannot copy installer member");
45
- offset += result.bytesWritten;
46
- }
47
- }
48
- if (await child.exited !== 0 || count === 0) throw new Error("Cannot copy installer member");
49
- await errors;
50
- } catch (error) {
51
- child.kill();
52
- await child.exited;
53
- await errors;
54
- throw error;
55
- }
56
- } finally {
57
- await output.close();
58
- }
59
- }
60
18
  async function digest(path) {
61
19
  const info = await lstat(path);
62
20
  if (!info.isFile() || info.isSymbolicLink() || !Number.isSafeInteger(info.size) || info.size < 1) throw new Error("Invalid extracted file");
63
- const file = await open(path, "r");
64
- try {
65
- const before = await file.stat();
66
- if (before.dev !== info.dev || before.ino !== info.ino || before.size !== info.size) throw new Error("Extracted file changed");
67
- const hash = createHash("sha256");
68
- let size = 0;
69
- for await (const chunk of file.createReadStream({ autoClose: false })) {
70
- size += chunk.length;
71
- if (size > before.size) throw new Error("Extracted file changed");
72
- hash.update(chunk);
73
- }
74
- const after = await file.stat();
75
- if (size !== before.size || after.size !== before.size || after.mtimeMs !== before.mtimeMs || after.ino !== before.ino || after.dev !== before.dev) throw new Error("Extracted file changed");
76
- return {
77
- size,
78
- digest: "sha256:" + hash.digest("hex")
79
- };
80
- } finally {
81
- await file.close();
82
- }
83
- }
84
- function normalizeArchiveMember(name) {
85
- return name.replace(/^\.\//, "").replaceAll("\\", "/");
86
- }
87
- async function assertExecutable(path, platform) {
88
- const info = await lstat(path);
89
- if (!info.isFile() || info.isSymbolicLink() || info.size < 4096) throw new Error("Invalid copied installer executable");
90
- const file = await open(path, "r");
91
- try {
92
- await assertNativeExecutable(file, info.size, platform, "installer executable");
93
- } finally {
94
- await file.close();
95
- }
96
- }
97
- async function assertLinuxEmbedded(path, archive) {
98
- const info = await lstat(path), marker = Buffer.from("ELECTROBUN_ARCHIVE_V1");
99
- if (info.size <= archive.size + marker.length) throw new Error("Copied installer archive is incomplete");
100
- const file = await open(path, "r");
101
- try {
102
- const position = info.size - archive.size - marker.length, actual = Buffer.alloc(marker.length);
103
- if ((await file.read(actual, 0, actual.length, position)).bytesRead !== actual.length || !actual.equals(marker)) throw new Error("Copied installer archive marker missing");
104
- const hash = createHash("sha256");
105
- for await (const chunk of file.createReadStream({
106
- start: position + marker.length,
107
- autoClose: false
108
- })) hash.update(chunk);
109
- if ("sha256:" + hash.digest("hex") !== archive.digest) throw new Error("Copied installer archive differs");
110
- } finally {
111
- await file.close();
112
- }
113
- }
114
- function within(root, value) {
115
- const path = relative(root, value);
116
- return path !== "" && path !== ".." && !path.startsWith(".." + sep) && !isAbsolute(path);
21
+ const { size, sha256 } = await hashFileAt(path, info, "Extracted file changed");
22
+ return {
23
+ size,
24
+ digest: "sha256:" + sha256
25
+ };
117
26
  }
118
27
  function cleanEnv(parent) {
119
28
  const result = {};
@@ -139,8 +48,9 @@ async function prepareInstallerExecution(options) {
139
48
  const env = options.env ?? process.env;
140
49
  assertInstallerContext(env, options.platform, process.platform, process.arch);
141
50
  assertPortableAppName(options.appName);
142
- if (!env.RUNNER_TEMP || !isAbsolute(env.RUNNER_TEMP)) throw new Error("Missing runner temp directory");
143
- const parent = await realpath(env.RUNNER_TEMP), sourceInstaller = resolve(options.installer), sourceArchive = resolve(options.archive);
51
+ const temp = runnerTemp(env);
52
+ if (!temp) throw new Error("Missing runner temp directory");
53
+ const parent = await realpath(temp), sourceInstaller = resolve(options.installer), sourceArchive = resolve(options.archive);
144
54
  if (!(await lstat(parent)).isDirectory()) throw new Error("Invalid runner temp directory");
145
55
  const verified = await verifyInstallerPayload({
146
56
  ...options,
@@ -165,8 +75,8 @@ async function prepareInstallerExecution(options) {
165
75
  work: async (mount) => {
166
76
  const appName = `${options.appName}.app`, apps = (await readdir(mount)).filter((name) => name.endsWith(".app"));
167
77
  if (apps.length !== 1 || apps[0] !== appName) throw new Error("Unexpected installer application");
168
- const sourceApp = await realpath(join(mount, appName));
169
- if (!within(await realpath(mount), sourceApp)) throw new Error("Installer application escapes image");
78
+ const sourceApp = await realpath(join(mount, appName)), mountRoot = await realpath(mount);
79
+ if (!isWithin(mountRoot, sourceApp)) throw new Error("Installer application escapes image");
170
80
  const app = join(incoming, appName);
171
81
  await run([
172
82
  "/usr/bin/ditto",
@@ -174,7 +84,7 @@ async function prepareInstallerExecution(options) {
174
84
  app
175
85
  ], clean);
176
86
  const actualApp = await realpath(app), resources = await realpath(join(actualApp, "Contents", "Resources"));
177
- if (!within(await realpath(incoming), actualApp) || !within(actualApp, resources)) throw new Error("Copied installer escapes profile");
87
+ if (!isWithin(await realpath(incoming), actualApp) || !isWithin(actualApp, resources)) throw new Error("Copied installer escapes profile");
178
88
  const plist = join(actualApp, "Contents", "Info.plist"), plistInfo = await lstat(plist);
179
89
  if (!plistInfo.isFile() || plistInfo.isSymbolicLink() || plistInfo.size < 1 || plistInfo.size > 1048576) throw new Error("Invalid installer property list");
180
90
  const name = (await run([
@@ -186,8 +96,8 @@ async function prepareInstallerExecution(options) {
186
96
  if (!name || name === "." || name === ".." || /[\\/]/.test(name)) throw new Error("Invalid installer executable");
187
97
  const appExecutable = join(actualApp, "Contents", "MacOS", name);
188
98
  const sourceExecutable = join(sourceApp, "Contents", "MacOS", name);
189
- if (!within(actualApp, await realpath(appExecutable)) || !within(sourceApp, await realpath(sourceExecutable))) throw new Error("Installer executable escapes application");
190
- await assertExecutable(appExecutable, options.platform);
99
+ if (!isWithin(actualApp, await realpath(appExecutable)) || !isWithin(sourceApp, await realpath(sourceExecutable))) throw new Error("Installer executable escapes application");
100
+ await assertInstallerExecutable(appExecutable, options.platform);
191
101
  if (JSON.stringify(await digest(sourceExecutable)) !== JSON.stringify(await digest(appExecutable))) throw new Error("Copied installer executable differs");
192
102
  const archives = (await readdir(resources)).filter((value) => value.endsWith(".tar.zst"));
193
103
  if (archives.length !== 1) throw new Error("Missing installer archive");
@@ -206,27 +116,51 @@ async function prepareInstallerExecution(options) {
206
116
  sourceInstaller
207
117
  ], clean)).split(/\r?\n/).filter(Boolean);
208
118
  assertInstallerLayout(options.platform, options.appName, names);
209
- const find = (name) => {
210
- const found = names.filter((value) => options.platform === "windows-x64" ? normalizeArchiveMember(value).toLowerCase() === name.toLowerCase() : normalizeArchiveMember(value) === name);
211
- if (found.length !== 1) throw new Error("Installer member missing or ambiguous");
212
- return found[0];
213
- };
119
+ const find = (name) => findArchiveMember(names, name, options.platform, "Installer member missing or ambiguous");
214
120
  if (options.platform === "windows-x64") {
215
121
  const subdirectory = join(incoming, ".installer");
216
122
  await mkdir(subdirectory);
217
123
  executable = join(incoming, `${options.appName}-Setup.exe`);
218
- await extract(tar, sourceInstaller, find(`${options.appName}-Setup.exe`), executable, 268435456, clean);
219
- await assertExecutable(executable, options.platform);
220
- await extract(tar, sourceInstaller, find(`.installer/${options.appName}-Setup.metadata.json`), join(subdirectory, `${options.appName}-Setup.metadata.json`), 1048576, clean);
124
+ await extractArchiveMember({
125
+ tar,
126
+ archive: sourceInstaller,
127
+ member: find(`${options.appName}-Setup.exe`),
128
+ destination: executable,
129
+ maximum: 268435456,
130
+ env: clean
131
+ });
132
+ await assertInstallerExecutable(executable, options.platform);
133
+ await extractArchiveMember({
134
+ tar,
135
+ archive: sourceInstaller,
136
+ member: find(`.installer/${options.appName}-Setup.metadata.json`),
137
+ destination: join(subdirectory, `${options.appName}-Setup.metadata.json`),
138
+ maximum: 1048576,
139
+ env: clean
140
+ });
221
141
  const embeddedPath = join(subdirectory, `${options.appName}-Setup.tar.zst`);
222
- await extract(tar, sourceInstaller, find(`.installer/${options.appName}-Setup.tar.zst`), embeddedPath, verified.archive.size, clean);
142
+ await extractArchiveMember({
143
+ tar,
144
+ archive: sourceInstaller,
145
+ member: find(`.installer/${options.appName}-Setup.tar.zst`),
146
+ destination: embeddedPath,
147
+ maximum: verified.archive.size,
148
+ env: clean
149
+ });
223
150
  const embedded = await digest(embeddedPath);
224
151
  if (embedded.size !== verified.archive.size || embedded.digest !== verified.archive.digest) throw new Error("Copied installer archive differs");
225
152
  } else {
226
153
  executable = join(incoming, "installer");
227
- await extract(tar, sourceInstaller, find("installer"), executable, verified.archive.size + 268435456, clean);
228
- await assertExecutable(executable, options.platform);
229
- await assertLinuxEmbedded(executable, verified.archive);
154
+ await extractArchiveMember({
155
+ tar,
156
+ archive: sourceInstaller,
157
+ member: find("installer"),
158
+ destination: executable,
159
+ maximum: verified.archive.size + 268435456,
160
+ env: clean
161
+ });
162
+ await assertInstallerExecutable(executable, options.platform);
163
+ await assertEmbeddedArchive(executable, (await lstat(executable)).size, verified.archive);
230
164
  await chmod(executable, 448);
231
165
  }
232
166
  }
@@ -1,36 +1,21 @@
1
1
  import { assertInstallerContext, assertInstallerHost, assertInstallerLayout, assertPortableAppName } from "./installer-layout.js";
2
- import { assertNativeExecutable } from "./native-executable.js";
2
+ import { hashFileAt } from "../updates/stable-file.js";
3
+ import { assertEmbeddedArchive, assertInstallerExecutable, extractArchiveMember, findArchiveMember, isWithin, normalizeArchiveMember } from "./installer-archive.js";
3
4
  import { InstallerImageLifecycleError, cleanupInstallerDirectoryAfterFailure, defaultInstallerImageMountIo, installerImageMountState, withInstallerImageRetries } from "./installer-image-mount.js";
4
5
  import { runInstallerCommand } from "./installer-command.js";
5
- import { lstat, mkdtemp, open, readdir, realpath } from "node:fs/promises";
6
- import { basename, dirname, isAbsolute, join, relative, resolve, sep, win32 } from "node:path";
7
- import { createHash } from "node:crypto";
6
+ import { lstat, mkdtemp, readdir, realpath } from "node:fs/promises";
7
+ import { basename, dirname, join, resolve, win32 } from "node:path";
8
8
  import { tmpdir } from "node:os";
9
9
  // src/installer/installer-identity.ts
10
10
  async function identity(path) {
11
11
  const info = await lstat(path);
12
12
  if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || !Number.isSafeInteger(info.size)) throw new Error("Invalid installer artifact");
13
- const file = await open(path, "r");
14
- try {
15
- const before = await file.stat();
16
- if (before.ino !== info.ino || before.dev !== info.dev || before.size !== info.size) throw new Error("Installer artifact changed");
17
- const hash = createHash("sha256");
18
- let size = 0;
19
- for await (const chunk of file.createReadStream({ autoClose: false })) {
20
- size += chunk.length;
21
- if (size > before.size) throw new Error("Installer artifact changed");
22
- hash.update(chunk);
23
- }
24
- const after = await file.stat();
25
- if (size !== before.size || after.size !== before.size || after.mtimeMs !== before.mtimeMs || after.ino !== before.ino || after.dev !== before.dev) throw new Error("Installer artifact changed");
26
- return {
27
- name: basename(path),
28
- size,
29
- digest: "sha256:" + hash.digest("hex")
30
- };
31
- } finally {
32
- await file.close();
33
- }
13
+ const { size, sha256 } = await hashFileAt(path, info, "Installer artifact changed");
14
+ return {
15
+ name: basename(path),
16
+ size,
17
+ digest: "sha256:" + sha256
18
+ };
34
19
  }
35
20
  function environment(parent) {
36
21
  const env = {};
@@ -103,68 +88,6 @@ async function command(args, env) {
103
88
  failureMessage: "Installer inspection command failed"
104
89
  });
105
90
  }
106
- async function extractMember(tar, installer, member, output, maximum, env) {
107
- const file = await open(output, "wx", 384);
108
- const spawn = () => Bun.spawn({
109
- cmd: [
110
- tar,
111
- "-xOf",
112
- installer,
113
- member
114
- ],
115
- env,
116
- stdout: "pipe",
117
- stderr: "pipe",
118
- signal: AbortSignal.timeout(12e4)
119
- });
120
- let child;
121
- try {
122
- child = spawn();
123
- } catch (error) {
124
- await file.close();
125
- throw error;
126
- }
127
- const errors = (async () => {
128
- for await (const _chunk of child.stderr);
129
- })();
130
- let count = 0;
131
- try {
132
- for await (const chunk of child.stdout) {
133
- count += chunk.length;
134
- if (!Number.isSafeInteger(count) || count > maximum) throw new Error("Installer member exceeds bound");
135
- let offset = 0;
136
- while (offset < chunk.length) {
137
- const result = await file.write(chunk, offset, chunk.length - offset);
138
- if (!result.bytesWritten) throw new Error("Cannot read installer member");
139
- offset += result.bytesWritten;
140
- }
141
- }
142
- if (await child.exited !== 0) throw new Error("Cannot read installer member");
143
- await errors;
144
- return count;
145
- } catch (error) {
146
- child.kill();
147
- await child.exited;
148
- await errors;
149
- throw error;
150
- } finally {
151
- await file.close();
152
- }
153
- }
154
- async function assertExecutable(path, platform) {
155
- const info = await lstat(path);
156
- if (!info.isFile() || info.isSymbolicLink() || info.size < 4096) throw new Error("Invalid installer executable");
157
- const file = await open(path, "r");
158
- try {
159
- await assertNativeExecutable(file, info.size, platform, "installer executable");
160
- } finally {
161
- await file.close();
162
- }
163
- }
164
- function within(root, path) {
165
- const rel = relative(root, path);
166
- return rel !== "" && rel !== ".." && !rel.startsWith(".." + sep) && !isAbsolute(rel);
167
- }
168
91
  /**
169
92
  * Inspect a shipped installer without executing it, on a matching disposable runner, or with
170
93
  * `local` on the maintainer's own matching machine for a local release (read-only inspection of
@@ -198,7 +121,7 @@ async function verifyInstallerPayload(options) {
198
121
  if (apps.length !== 1) throw new Error("Installer application is missing or ambiguous");
199
122
  const resources = join(mount, apps[0], "Contents", "Resources");
200
123
  const actualRoot = await realpath(mount), actualApp = await realpath(join(mount, apps[0])), actualResources = await realpath(resources);
201
- if (!within(actualRoot, actualApp) || !within(actualApp, actualResources)) throw new Error("Installer resources escape mounted image");
124
+ if (!isWithin(actualRoot, actualApp) || !isWithin(actualApp, actualResources)) throw new Error("Installer resources escape mounted image");
202
125
  const plist = join(actualApp, "Contents", "Info.plist"), plistInfo = await lstat(plist);
203
126
  if (!plistInfo.isFile() || plistInfo.isSymbolicLink() || plistInfo.size < 1 || plistInfo.size > 1048576) throw new Error("Invalid installer property list");
204
127
  const executable = (await command([
@@ -208,7 +131,7 @@ async function verifyInstallerPayload(options) {
208
131
  plist
209
132
  ], env)).trim();
210
133
  if (!executable || executable === "." || executable === ".." || /[\\/]/.test(executable)) throw new Error("Invalid installer executable name");
211
- await assertExecutable(join(actualApp, "Contents", "MacOS", executable), options.platform);
134
+ await assertInstallerExecutable(join(actualApp, "Contents", "MacOS", executable), options.platform);
212
135
  const archives = (await readdir(actualResources)).filter((name) => name.endsWith(".tar.zst"));
213
136
  if (archives.length !== 1) throw new Error("Installer archive is missing or ambiguous");
214
137
  const embedded = await identity(join(actualResources, archives[0]));
@@ -227,35 +150,34 @@ async function verifyInstallerPayload(options) {
227
150
  ], env)).split(/\r?\n/).filter(Boolean);
228
151
  assertInstallerLayout(options.platform, options.appName, names);
229
152
  const candidates = options.platform === "windows-x64" ? [`.installer/${options.appName}-Setup.tar.zst`] : ["installer"];
230
- const matches = names.filter((name) => candidates.includes(name.replace(/^\.\//, "").replaceAll("\\", "/")));
153
+ const matches = names.filter((name) => candidates.includes(normalizeArchiveMember(name)));
231
154
  if (matches.length !== 1) throw new Error("Installer archive is missing or ambiguous");
232
155
  const extracted = join(temporary, "member");
233
- const count = await extractMember(tar, installerPath, matches[0], extracted, before.archive.size + 268435456, env);
156
+ const count = await extractArchiveMember({
157
+ tar,
158
+ archive: installerPath,
159
+ member: matches[0],
160
+ destination: extracted,
161
+ maximum: before.archive.size + 268435456,
162
+ env
163
+ });
234
164
  if (options.platform === "windows-x64") {
235
- const setup = names.find((name) => name.replace(/^\.\//, "").replaceAll("\\", "/").toLowerCase() === `${options.appName}-Setup.exe`.toLowerCase());
236
- if (!setup) throw new Error("Installer executable is missing");
165
+ const setup = findArchiveMember(names, `${options.appName}-Setup.exe`, options.platform, "Installer executable is missing or ambiguous");
237
166
  const executable = join(temporary, "setup.exe");
238
- await extractMember(tar, installerPath, setup, executable, 268435456, env);
239
- await assertExecutable(executable, options.platform);
167
+ await extractArchiveMember({
168
+ tar,
169
+ archive: installerPath,
170
+ member: setup,
171
+ destination: executable,
172
+ maximum: 268435456,
173
+ env
174
+ });
175
+ await assertInstallerExecutable(executable, options.platform);
240
176
  const embedded = await identity(extracted);
241
177
  if (embedded.size !== before.archive.size || embedded.digest !== before.archive.digest) throw new Error("Installer archive differs from release archive");
242
178
  } else {
243
- await assertExecutable(extracted, options.platform);
244
- const marker = Buffer.from("ELECTROBUN_ARCHIVE_V1");
245
- if (count <= before.archive.size + marker.length) throw new Error("Installer archive is incomplete");
246
- const file = await open(extracted, "r");
247
- try {
248
- const position = count - before.archive.size - marker.length, actual = Buffer.alloc(marker.length);
249
- if ((await file.read(actual, 0, actual.length, position)).bytesRead !== actual.length || !actual.equals(marker)) throw new Error("Installer archive marker is missing");
250
- const hash = createHash("sha256");
251
- for await (const chunk of file.createReadStream({
252
- start: position + marker.length,
253
- autoClose: false
254
- })) hash.update(chunk);
255
- if ("sha256:" + hash.digest("hex") !== before.archive.digest) throw new Error("Installer archive differs from release archive");
256
- } finally {
257
- await file.close();
258
- }
179
+ await assertInstallerExecutable(extracted, options.platform);
180
+ await assertEmbeddedArchive(extracted, count, before.archive);
259
181
  }
260
182
  }
261
183
  const after = {
@@ -1,3 +1,4 @@
1
+ import { errorCode } from "../app/value-guards.js";
1
2
  import { assertInstallerContext, assertPortableAppName } from "./installer-layout.js";
2
3
  import { parseOfflinePolicy } from "../release/release-qualification.js";
3
4
  import { verifyInstallerPayload } from "./installer-identity.js";
@@ -69,7 +70,7 @@ async function exists(path) {
69
70
  await lstat(path);
70
71
  return true;
71
72
  } catch (error) {
72
- if (error.code === "ENOENT") return false;
73
+ if (errorCode(error) === "ENOENT") return false;
73
74
  throw error;
74
75
  }
75
76
  }
@@ -313,7 +314,7 @@ async function observeInstalledApp(options) {
313
314
  process.kill(guardedPid, 0);
314
315
  workerState = "present";
315
316
  } catch (error) {
316
- workerState = error.code === "ESRCH" ? "exited" : "unknown";
317
+ workerState = errorCode(error) === "ESRCH" ? "exited" : "unknown";
317
318
  }
318
319
  const appState = app ? app.exitCode ?? "running" : mac ? "not-spawned" : "installer-launched";
319
320
  throw new Error(`Installed renderer observation missing or unexpected activity observed (start=${run.start}, permitted=${granted}, observed=${observed}, unexpected=${unexpectedObserved}, workerPid=${workerState}, installerExit=${installer ? installer.exitCode ?? "running" : "none"}, appExit=${appState})`);