@adea-ai/app-framework 0.31.0 → 0.33.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 (111) hide show
  1. package/README.md +33 -15
  2. package/dist/app/create-application.js +4 -3
  3. package/dist/app/serial-queue.d.ts +5 -0
  4. package/dist/app/serial-queue.js +14 -0
  5. package/dist/app/value-guards.d.ts +15 -0
  6. package/dist/app/value-guards.js +29 -0
  7. package/dist/ci/toolchain-identity.js +1 -1
  8. package/dist/desktop/channel-proof.js +2 -1
  9. package/dist/desktop/client-asset-stream.js +6 -12
  10. package/dist/desktop/desktop-host.d.ts +25 -5
  11. package/dist/desktop/desktop-host.js +75 -20
  12. package/dist/framework-source.json +1 -1
  13. package/dist/installer/command-artifact.js +4 -3
  14. package/dist/installer/command-qualification-command.js +6 -5
  15. package/dist/installer/hosted-runner.d.ts +9 -2
  16. package/dist/installer/hosted-runner.js +18 -5
  17. package/dist/installer/installed-payload.js +20 -46
  18. package/dist/installer/installer-archive.d.ts +33 -0
  19. package/dist/installer/installer-archive.js +116 -0
  20. package/dist/installer/installer-ci-bootstrap.js +10 -6
  21. package/dist/installer/installer-extraction.js +54 -120
  22. package/dist/installer/installer-identity.js +33 -111
  23. package/dist/installer/installer-observation.js +3 -2
  24. package/dist/installer/installer-process-cleanup.js +7 -46
  25. package/dist/installer/installer-qualification-command.js +6 -5
  26. package/dist/installer/installer-upgrade-command.js +5 -5
  27. package/dist/installer/installer-worker-guard.js +2 -1
  28. package/dist/native/canonical-path.d.ts +5 -0
  29. package/dist/native/canonical-path.js +10 -0
  30. package/dist/native/cef-helper.js +5 -4
  31. package/dist/native/electrobun-source-patch.js +3 -2
  32. package/dist/native/{directory-exchange.d.ts → native-rename.d.ts} +2 -0
  33. package/dist/native/native-rename.js +126 -0
  34. package/dist/native/native-secret-store.js +2 -6
  35. package/dist/native/native-utilities.js +4 -4
  36. package/dist/native/posix-launcher-build.js +1 -1
  37. package/dist/native/posix-tree-removal.js +2 -2
  38. package/dist/native/windows-process.d.ts +9 -0
  39. package/dist/native/windows-process.js +60 -0
  40. package/dist/release/feed-release-cut.js +2 -1
  41. package/dist/release/framework-package-publish.js +1 -1
  42. package/dist/release/framework-update-lock.js +2 -1
  43. package/dist/release/github-cli.d.ts +18 -0
  44. package/dist/release/github-cli.js +13 -1
  45. package/dist/release/github-release-cut.js +3 -2
  46. package/dist/release/github-release-provenance.js +2 -1
  47. package/dist/release/github-release-publisher.js +2 -1
  48. package/dist/release/local-provenance.js +3 -2
  49. package/dist/release/local-release.js +6 -6
  50. package/dist/release/qualification-run.d.ts +25 -0
  51. package/dist/release/qualification-run.js +43 -0
  52. package/dist/release/release-artifact.js +3 -11
  53. package/dist/release/release-assets-plan.js +16 -6
  54. package/dist/release/release-config.js +14 -14
  55. package/dist/release/release-cut-command.js +14 -4
  56. package/dist/release/release-cut.js +3 -2
  57. package/dist/release/release-descriptor-staging.js +13 -27
  58. package/dist/release/release-finalize.js +2 -1
  59. package/dist/release/release-publication.js +3 -11
  60. package/dist/release/release-publish-command.js +15 -6
  61. package/dist/release/release-qualification.js +3 -2
  62. package/dist/release/retained-release-build.js +12 -12
  63. package/dist/update-helper.bundle.js +250 -178
  64. package/dist/updates/desktop-updater.js +53 -58
  65. package/dist/updates/packaged-launch-admission.js +3 -2
  66. package/dist/updates/packaged-release-config.js +6 -5
  67. package/dist/updates/packaged-update-startup.d.ts +25 -5
  68. package/dist/updates/packaged-update-startup.js +30 -5
  69. package/dist/updates/release-feed.js +9 -9
  70. package/dist/updates/signed-release.js +5 -5
  71. package/dist/updates/stable-file.d.ts +18 -0
  72. package/dist/updates/stable-file.js +65 -2
  73. package/dist/updates/update-adapter.d.ts +2 -13
  74. package/dist/updates/update-arm.js +13 -13
  75. package/dist/updates/update-channel-store.js +7 -9
  76. package/dist/updates/update-discovery.js +30 -34
  77. package/dist/updates/update-download-cleanup.js +2 -1
  78. package/dist/updates/update-download.js +2 -4
  79. package/dist/updates/update-extraction.js +1 -1
  80. package/dist/updates/update-generation-lease.js +4 -3
  81. package/dist/updates/update-handoff-plan.js +3 -2
  82. package/dist/updates/update-helper.js +8 -6
  83. package/dist/updates/update-instance-lease.js +3 -2
  84. package/dist/updates/update-launch-gate.js +12 -25
  85. package/dist/updates/update-launch.js +3 -2
  86. package/dist/updates/update-lock.js +3 -2
  87. package/dist/updates/update-marker.js +3 -2
  88. package/dist/updates/update-process-fence.js +10 -39
  89. package/dist/updates/update-process-liveness.js +2 -1
  90. package/dist/updates/update-resume.js +30 -27
  91. package/dist/updates/update-retention-marker.js +7 -5
  92. package/dist/updates/update-retention-plan.js +2 -2
  93. package/dist/updates/update-retirement.js +52 -52
  94. package/dist/updates/update-state-store.d.ts +2 -0
  95. package/dist/updates/update-state-store.js +34 -38
  96. package/dist/updates/update-state.d.ts +8 -3
  97. package/dist/updates/update-swap-stage.js +6 -5
  98. package/dist/updates/update-swap.d.ts +1 -1
  99. package/dist/updates/update-swap.js +8 -6
  100. package/package.json +4 -12
  101. package/src/desktop/client-asset-stream.ts +6 -12
  102. package/src/updates/update-adapter.ts +2 -14
  103. package/src/updates/update-state.ts +11 -2
  104. package/templates/application/src/bun/index.ts +13 -9
  105. package/dist/native/directory-exchange.js +0 -90
  106. package/dist/native/exclusive-rename.d.ts +0 -2
  107. package/dist/native/exclusive-rename.js +0 -52
  108. package/dist/release/retention-dry-run.d.ts +0 -46
  109. package/dist/release/retention-dry-run.js +0 -94
  110. package/dist/updates/update-exclusive-rename.d.ts +0 -3
  111. package/dist/updates/update-exclusive-rename.js +0 -5
@@ -1,14 +1,17 @@
1
+ import { errorCode } from "../app/value-guards.js";
2
+ import { isCanonicalPath } from "../native/canonical-path.js";
3
+ import { createSerialQueue } from "../app/serial-queue.js";
1
4
  import { isUpdateChannel } from "./release-version.js";
2
5
  import { openUnchangedFile, sameFile } from "./stable-file.js";
3
6
  import { lstat, open, realpath, rename, unlink } from "node:fs/promises";
4
- import { isAbsolute, join, resolve } from "node:path";
7
+ import { join } from "node:path";
5
8
  import { randomUUID } from "node:crypto";
6
9
  // src/updates/update-channel-store.ts
7
10
  /** Persist one app-owned update channel; the caller must supply a private state directory. */
8
11
  function createUpdateChannelStore(directory) {
9
- if (typeof directory !== "string" || !isAbsolute(directory) || resolve(directory) !== directory) throw new Error("Invalid update channel directory");
12
+ if (!isCanonicalPath(directory)) throw new Error("Invalid update channel directory");
10
13
  const path = join(directory, "update-channel.json");
11
- let queue = Promise.resolve();
14
+ const ordered = createSerialQueue();
12
15
  async function assertDirectory() {
13
16
  const info = await lstat(directory);
14
17
  if (!info.isDirectory() || info.isSymbolicLink() || await realpath(directory) !== directory) throw new Error("Invalid update channel directory");
@@ -19,7 +22,7 @@ function createUpdateChannelStore(directory) {
19
22
  try {
20
23
  info = await lstat(path);
21
24
  } catch (error) {
22
- if (error.code === "ENOENT") return "stable";
25
+ if (errorCode(error) === "ENOENT") return "stable";
23
26
  throw error;
24
27
  }
25
28
  if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > 4096) throw new Error("Invalid update channel file");
@@ -36,11 +39,6 @@ function createUpdateChannelStore(directory) {
36
39
  await file.close();
37
40
  }
38
41
  }
39
- function ordered(task) {
40
- const result = queue.then(task);
41
- queue = result.then(() => {}, () => {});
42
- return result;
43
- }
44
42
  return {
45
43
  read: () => ordered(read),
46
44
  save: (channel) => ordered(async () => {
@@ -15,43 +15,39 @@ async function discoverSignedRelease(options) {
15
15
  "daily",
16
16
  "dev"
17
17
  ].includes(trust.channel) || typeof trust.feed !== "string" || typeof discoveryName !== "string" || !/^[A-Za-z0-9][A-Za-z0-9._-]{0,199}\.json$/.test(discoveryName) || discoveryName === "release.json" || !Number.isSafeInteger(timeout) || timeout < 1 || timeout > 6e4) throw new Error("Invalid release discovery policy");
18
- const controller = new AbortController(), timer = setTimeout(() => controller.abort(), timeout);
19
- const signal = options.signal ? AbortSignal.any([controller.signal, options.signal]) : controller.signal;
18
+ const deadline = AbortSignal.timeout(timeout);
19
+ const signal = options.signal ? AbortSignal.any([deadline, options.signal]) : deadline;
20
20
  const fetcher = options.fetch ?? globalThis.fetch;
21
21
  const get = (path) => fetchReleaseLocation(`${trust.feed}${path}`, trust.feed, fetcher, signal);
22
- try {
23
- if (signal.aborted) throw new Error("Release discovery cancelled");
24
- let selected = null;
25
- if (olderThan === void 0) {
26
- selected = parseFeedRelease(await readReleaseJson(await get(`channels/${trust.channel}.json`), MAX_POINTER_BYTES));
27
- if (selected.channel !== trust.channel) throw new Error("Invalid channel pointer");
28
- } else {
29
- const kinds = trust.channel === "stable" ? ["stable"] : trust.channel === "dev" ? ["dev"] : ["daily", "stable"];
30
- const entry = parseReleaseLedger(await readReleaseJson(await get("index.json"), MAX_LEDGER_BYTES), new URL(trust.feed).pathname.split("/").at(-2)).releases.reduce((best, item) => kinds.includes(item.kind) && versionLessThan(item.tag, olderThan) && (!best || versionLessThan(best.tag, item.tag)) ? item : best, void 0);
31
- if (entry) {
32
- selected = parseFeedRelease(await readReleaseJson(await get(`${entry.tag}/release.json`), MAX_POINTER_BYTES));
33
- if (selected.tag !== entry.tag) throw new Error("Invalid feed release");
34
- if (!selected.assets.some((asset) => asset.name === discoveryName)) throw new Error("Selected previous release does not advertise this descriptor");
35
- }
36
- }
37
- const listed = selected?.assets.find((asset) => asset.name === discoveryName);
38
- if (!selected || !listed) throw new Error("No qualified channel descriptor is available");
39
- const { tag } = selected;
40
- const bytes = await readReleaseBytes(await get(`${tag}/${discoveryName}`), 65536);
41
- if (bytes.length !== listed.size || createHash("sha256").update(bytes).digest("hex") !== listed.sha256) throw new Error("Channel descriptor differs from its release");
42
- let signed;
43
- try {
44
- signed = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
45
- } catch {
46
- throw new Error("Invalid release feed response");
22
+ if (signal.aborted) throw new Error("Release discovery cancelled");
23
+ let selected = null;
24
+ if (olderThan === void 0) {
25
+ selected = parseFeedRelease(await readReleaseJson(await get(`channels/${trust.channel}.json`), MAX_POINTER_BYTES));
26
+ if (selected.channel !== trust.channel) throw new Error("Invalid channel pointer");
27
+ } else {
28
+ const kinds = trust.channel === "stable" ? ["stable"] : trust.channel === "dev" ? ["dev"] : ["daily", "stable"];
29
+ const entry = parseReleaseLedger(await readReleaseJson(await get("index.json"), MAX_LEDGER_BYTES), new URL(trust.feed).pathname.split("/").at(-2)).releases.reduce((best, item) => kinds.includes(item.kind) && versionLessThan(item.tag, olderThan) && (!best || versionLessThan(best.tag, item.tag)) ? item : best, void 0);
30
+ if (entry) {
31
+ selected = parseFeedRelease(await readReleaseJson(await get(`${entry.tag}/release.json`), MAX_POINTER_BYTES));
32
+ if (selected.tag !== entry.tag) throw new Error("Invalid feed release");
33
+ if (!selected.assets.some((asset) => asset.name === discoveryName)) throw new Error("Selected previous release does not advertise this descriptor");
47
34
  }
48
- const result = verifyReleaseDescriptor(signed, trust);
49
- if (!result.ok || result.descriptor.tag !== tag) throw new Error("Untrusted channel descriptor");
50
- const payload = selected.assets.find((asset) => asset.name === result.descriptor.asset.name);
51
- if (!payload || payload.size !== result.descriptor.asset.size || payload.sha256 !== result.descriptor.asset.sha256) throw new Error("Release payload is missing or differs from its descriptor");
52
- return result.descriptor;
53
- } finally {
54
- clearTimeout(timer);
55
35
  }
36
+ const listed = selected?.assets.find((asset) => asset.name === discoveryName);
37
+ if (!selected || !listed) throw new Error("No qualified channel descriptor is available");
38
+ const { tag } = selected;
39
+ const bytes = await readReleaseBytes(await get(`${tag}/${discoveryName}`), 65536);
40
+ if (bytes.length !== listed.size || createHash("sha256").update(bytes).digest("hex") !== listed.sha256) throw new Error("Channel descriptor differs from its release");
41
+ let signed;
42
+ try {
43
+ signed = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
44
+ } catch {
45
+ throw new Error("Invalid release feed response");
46
+ }
47
+ const result = verifyReleaseDescriptor(signed, trust);
48
+ if (!result.ok || result.descriptor.tag !== tag) throw new Error("Untrusted channel descriptor");
49
+ const payload = selected.assets.find((asset) => asset.name === result.descriptor.asset.name);
50
+ if (!payload || payload.size !== result.descriptor.asset.size || payload.sha256 !== result.descriptor.asset.sha256) throw new Error("Release payload is missing or differs from its descriptor");
51
+ return result.descriptor;
56
52
  }
57
53
  export { discoverSignedRelease };
@@ -1,3 +1,4 @@
1
+ import { errorCode } from "../app/value-guards.js";
1
2
  import { acquireUpdateDownloadLease, withUpdateDownloadRootLock } from "./update-download-lock.js";
2
3
  import { lstat, mkdir, mkdtemp, readdir, realpath, rm } from "node:fs/promises";
3
4
  import { basename, dirname, join, resolve } from "node:path";
@@ -101,7 +102,7 @@ async function removeAbandonedUpdateDownloads(root) {
101
102
  await lstat(directory);
102
103
  continue;
103
104
  } catch (error) {
104
- if (error.code !== "ENOENT") continue;
105
+ if (errorCode(error) !== "ENOENT") continue;
105
106
  }
106
107
  try {
107
108
  const lease = await acquireUpdateDownloadLease(directory, true);
@@ -8,9 +8,8 @@ async function downloadVerifiedRelease(options) {
8
8
  const { descriptor, maxBytes } = options;
9
9
  const timeout = options.timeoutMs ?? 12e4;
10
10
  if (!isVerifiedRelease(descriptor) || !Number.isSafeInteger(maxBytes) || maxBytes < 1 || descriptor.asset.size > maxBytes || !Number.isSafeInteger(timeout) || timeout < 1 || timeout > 6e5) throw new Error("Invalid verified download request");
11
- const controller = new AbortController();
12
- const timer = setTimeout(() => controller.abort(), timeout);
13
- const signal = options.signal ? AbortSignal.any([controller.signal, options.signal]) : controller.signal;
11
+ const deadline = AbortSignal.timeout(timeout);
12
+ const signal = options.signal ? AbortSignal.any([deadline, options.signal]) : deadline;
14
13
  const fetcher = options.fetch ?? globalThis.fetch;
15
14
  let temporary;
16
15
  let response;
@@ -83,7 +82,6 @@ async function downloadVerifiedRelease(options) {
83
82
  });
84
83
  throw error;
85
84
  } finally {
86
- clearTimeout(timer);
87
85
  if (response?.body && !response.bodyUsed && !response.body.locked) try {
88
86
  response.body.cancel().catch(() => {});
89
87
  } catch {}
@@ -5,8 +5,8 @@ import { safeArchivePath } from "../installer/archive-entry.js";
5
5
  import { isLinuxCefLibraryLink } from "../native/linux-cef-links.js";
6
6
  import { lstat, mkdir, mkdtemp, open, readFile, realpath, rm, symlink } from "node:fs/promises";
7
7
  import { isAbsolute, join } from "node:path";
8
- import { constants as constants$1 } from "node:fs";
9
8
  import { createHash } from "node:crypto";
9
+ import { constants as constants$1 } from "node:fs";
10
10
  import { x } from "tar";
11
11
  import { createZstdDecompress } from "node:zlib";
12
12
  import { Transform } from "node:stream";
@@ -1,10 +1,11 @@
1
+ import { isUuid } from "../app/value-guards.js";
2
+ import { isCanonicalPath } from "../native/canonical-path.js";
1
3
  import { tryAcquireNativeFileLock } from "./native-file-lock.js";
2
- import { basename, dirname, isAbsolute, join, resolve } from "node:path";
4
+ import { basename, dirname, join } from "node:path";
3
5
  // src/updates/update-generation-lease.ts
4
- var UUID = /^[0-9a-f]{8}-[0-9a-f-]{27}$/i;
5
6
  /** A persistent lock outside the retained tree; never remove or replace it. */
6
7
  function updateGenerationLeasePath(target, generation) {
7
- if (typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target || target.includes("\0") || typeof generation !== "string" || !UUID.test(generation)) throw new Error("Invalid update generation lease identity");
8
+ if (!isCanonicalPath(target) || typeof generation !== "string" || !isUuid(generation)) throw new Error("Invalid update generation lease identity");
8
9
  return join(dirname(target), `.${basename(target)}.framework-generation-${generation.toLowerCase()}.lock`);
9
10
  }
10
11
  /** Shared readers and future exclusive cleanup must use this same persistent lock. */
@@ -1,7 +1,8 @@
1
+ import { isCanonicalPath } from "../native/canonical-path.js";
1
2
  import { assertPortableAppName } from "../installer/installer-layout.js";
2
3
  import { isVerifiedReleaseForTrust } from "./signed-release.js";
3
4
  import { versionLessThan } from "./release-version.js";
4
- import { basename, isAbsolute, resolve } from "node:path";
5
+ import { basename } from "node:path";
5
6
  // src/updates/update-handoff-plan.ts
6
7
  /** Validate an explicitly approved update and its intended installation. This plan does not move files or launch a helper. */
7
8
  function planUpdateHandoff(options) {
@@ -15,7 +16,7 @@ function planUpdateHandoff(options) {
15
16
  };
16
17
  if (!expected[platform] || !["native", "cef"].includes(renderer) || !isVerifiedReleaseForTrust(descriptor, trust) || descriptor.platform !== expected[platform][0] || descriptor.arch !== expected[platform][1] || approved !== true || approvedVersion !== descriptor.version || typeof currentVersion !== "string" || currentVersion.length > 64 || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(currentVersion) || !versionLessThan(currentVersion, descriptor.version)) throw new Error("Invalid approved update");
17
18
  const name = platform === "macos-arm64" ? `${appName}.app` : "app";
18
- if (typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target || basename(target) !== name) throw new Error("Invalid update target");
19
+ if (!isCanonicalPath(target) || basename(target) !== name) throw new Error("Invalid update target");
19
20
  return Object.freeze({
20
21
  target,
21
22
  appName,
@@ -1,3 +1,5 @@
1
+ import { errorCode, isUuid } from "../app/value-guards.js";
2
+ import { isCanonicalPath } from "../native/canonical-path.js";
1
3
  import { acquireUpdateLock, readUpdateStageState, writeUpdateStageState, writeUpdateStageStateDurably } from "./update-state-store.js";
2
4
  import { readUpdateTreeDigest, readUpdateTreeIdentity, readUpdateTreeVersion, sameUpdateTreeDigest, sameUpdateTreeIdentity, swapUpdateTreeWhileLocked, syncUpdateTree, syncUpdateTreeParent, updateStagingFor } from "./update-swap.js";
3
5
  import { publishExclusiveUpdateMarker, publishExclusiveUpdateMarkerSync, readUpdateMarker } from "./update-marker.js";
@@ -5,7 +7,7 @@ import { launchInstalledApp } from "./update-launch.js";
5
7
  import { UpdateProcessFenceCancelledError, waitForUpdateProcesses } from "./update-process-fence.js";
6
8
  import { cancelUpdateLaunchGateBeforeTransition, publishUpdateLaunchGate, tryAcquireUpdateHelperLiveness, waitForUpdateLaunchGateWriter } from "./update-launch-gate.js";
7
9
  import { lstat, open, realpath } from "node:fs/promises";
8
- import { isAbsolute, join, resolve } from "node:path";
10
+ import { join } from "node:path";
9
11
  import { constants as constants$1 } from "node:fs";
10
12
  // src/updates/update-helper.ts
11
13
  /** Return whether a staging directory contains artifacts from a helper handoff. */
@@ -21,19 +23,19 @@ async function hasUpdateHelperArtifacts(staging) {
21
23
  await lstat(join(staging, name));
22
24
  return true;
23
25
  } catch (error) {
24
- if (error.code !== "ENOENT") throw error;
26
+ if (errorCode(error) !== "ENOENT") throw error;
25
27
  }
26
28
  return false;
27
29
  }
28
30
  /** Validate a helper request's shape. */
29
31
  function parseUpdateHelperRequest(value) {
30
32
  const data = value;
31
- if (typeof value !== "object" || value === null || Array.isArray(value) || data.schema !== 1 && data.schema !== 2 && data.schema !== 3 || typeof data.target !== "string" || !isAbsolute(data.target) || resolve(data.target) !== data.target || ![
33
+ if (typeof value !== "object" || value === null || Array.isArray(value) || data.schema !== 1 && data.schema !== 2 && data.schema !== 3 || !isCanonicalPath(data.target) || ![
32
34
  "macos-arm64",
33
35
  "windows-x64",
34
36
  "linux-x64",
35
37
  "linux-arm64"
36
- ].includes(data.platform) || typeof data.version !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(data.version) || !Array.isArray(data.processes) || data.schema === 2 && (typeof data.generation !== "string" || !/^[0-9a-f-]{36}$/i.test(data.generation) || data.helperOwner !== void 0 && (typeof data.helperOwner !== "string" || !/^[0-9a-f-]{36}$/i.test(data.helperOwner))) || data.schema === 3 && (typeof data.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(data.generation) || typeof data.helperOwner !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(data.helperOwner)) || !Number.isSafeInteger(data.processTimeoutMs) || data.processTimeoutMs < 1 || data.processTimeoutMs > 12e4) throw new Error("Invalid update helper request");
38
+ ].includes(data.platform) || typeof data.version !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(data.version) || !Array.isArray(data.processes) || data.schema === 2 && (typeof data.generation !== "string" || !/^[0-9a-f-]{36}$/i.test(data.generation) || data.helperOwner !== void 0 && (typeof data.helperOwner !== "string" || !/^[0-9a-f-]{36}$/i.test(data.helperOwner))) || data.schema === 3 && (typeof data.generation !== "string" || !isUuid(data.generation) || typeof data.helperOwner !== "string" || !isUuid(data.helperOwner)) || !Number.isSafeInteger(data.processTimeoutMs) || data.processTimeoutMs < 1 || data.processTimeoutMs > 12e4) throw new Error("Invalid update helper request");
37
39
  return Object.freeze(data);
38
40
  }
39
41
  async function readRequest(staging) {
@@ -69,7 +71,7 @@ async function updatePathExists(path) {
69
71
  await lstat(path);
70
72
  return true;
71
73
  } catch (error) {
72
- if (error.code === "ENOENT") return false;
74
+ if (errorCode(error) === "ENOENT") return false;
73
75
  throw error;
74
76
  }
75
77
  }
@@ -80,7 +82,7 @@ async function updatePathExists(path) {
80
82
  */
81
83
  async function runUpdateHelper(options) {
82
84
  if (options.target !== void 0 || options.generation !== void 0 || options.helperOwner !== void 0) {
83
- if (typeof options.target !== "string" || !isAbsolute(options.target) || resolve(options.target) !== options.target || options.target.includes("\0") || !isAbsolute(options.staging) || resolve(options.staging) !== options.staging || options.staging.includes("\0") || updateStagingFor(options.target) !== options.staging || typeof options.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(options.generation) || typeof options.helperOwner !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(options.helperOwner)) throw new Error("Invalid update helper invocation");
85
+ if (!isCanonicalPath(options.target) || !isCanonicalPath(options.staging) || updateStagingFor(options.target) !== options.staging || typeof options.generation !== "string" || !isUuid(options.generation) || typeof options.helperOwner !== "string" || !isUuid(options.helperOwner)) throw new Error("Invalid update helper invocation");
84
86
  const liveness = await tryAcquireUpdateHelperLiveness(options.target);
85
87
  if (!liveness) throw new Error("Update helper liveness is already owned");
86
88
  try {
@@ -1,9 +1,10 @@
1
+ import { isCanonicalPath } from "../native/canonical-path.js";
1
2
  import { tryAcquireNativeFileLock } from "./native-file-lock.js";
2
- import { basename, dirname, isAbsolute, join, resolve } from "node:path";
3
+ import { basename, dirname, join } from "node:path";
3
4
  // src/updates/update-instance-lease.ts
4
5
  /** Stable app-lifetime path, separate from the updater transition lock. */
5
6
  function updateInstanceLeasePath(target) {
6
- if (typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target || target.includes("\0")) throw new Error("Invalid update instance lease target");
7
+ if (!isCanonicalPath(target)) throw new Error("Invalid update instance lease target");
7
8
  return join(dirname(target), "." + basename(target) + ".framework-instance-lease.lock");
8
9
  }
9
10
  /**
@@ -1,12 +1,15 @@
1
+ import { errorCode, isUuid } from "../app/value-guards.js";
2
+ import { isCanonicalPath } from "../native/canonical-path.js";
3
+ import { readSmallStateFile } from "./stable-file.js";
1
4
  import { tryAcquireNativeFileLock } from "./native-file-lock.js";
2
5
  import { tryAcquireUpdateLock } from "./update-lock.js";
3
6
  import { acquireUpdateLock } from "./update-state-store.js";
4
7
  import { publishExclusiveUpdateMarker, readUpdateMarker } from "./update-marker.js";
5
8
  import { tryAcquireUpdateInstanceLease } from "./update-instance-lease.js";
6
- import { lstat, open, rename, unlink } from "node:fs/promises";
7
- import { basename, dirname, isAbsolute, join, resolve } from "node:path";
8
- import { constants as constants$1 } from "node:fs";
9
+ import { open, rename, unlink } from "node:fs/promises";
10
+ import { basename, dirname, join } from "node:path";
9
11
  import { randomUUID } from "node:crypto";
12
+ import { constants as constants$1 } from "node:fs";
10
13
  // src/updates/update-launch-gate.ts
11
14
  /**
12
15
  * Publish the schema-3 helper's cancellation request under the same
@@ -15,7 +18,7 @@ import { randomUUID } from "node:crypto";
15
18
  */
16
19
  async function publishUpdateLaunchGateCancellationRequest(target, staging, generation, helperOwner) {
17
20
  validateTarget(target);
18
- if (staging !== join(dirname(target), `.${basename(target)}.framework-update`) || !UUID.test(generation) || !UUID.test(helperOwner)) throw new Error("Invalid update launch gate cancellation owner");
21
+ if (staging !== join(dirname(target), `.${basename(target)}.framework-update`) || !isUuid(generation) || !isUuid(helperOwner)) throw new Error("Invalid update launch gate cancellation owner");
19
22
  const coordinator = await acquireUpdateLock(target);
20
23
  try {
21
24
  try {
@@ -26,7 +29,7 @@ async function publishUpdateLaunchGateCancellationRequest(target, staging, gener
26
29
  });
27
30
  return true;
28
31
  } catch (error) {
29
- if (error.code !== "EEXIST") throw error;
32
+ if (errorCode(error) !== "EEXIST") throw error;
30
33
  const existing = await readUpdateMarker(staging, "cancel.json").catch(() => void 0);
31
34
  return existing?.schema === 1 && existing.generation === generation && existing.helperOwner === helperOwner;
32
35
  }
@@ -73,11 +76,10 @@ var UpdateLaunchHandoffCleanupError = class extends Error {
73
76
  this.released = true;
74
77
  }
75
78
  };
76
- var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
77
79
  var VERSION = /^\d+\.\d+\.\d+(?:-dev\.\d+)?$/;
78
80
  var MAX_GATE_BYTES = 1024;
79
81
  function validateTarget(target) {
80
- if (typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target || target.includes("\0")) throw new Error("Invalid update launch gate target");
82
+ if (!isCanonicalPath(target)) throw new Error("Invalid update launch gate target");
81
83
  }
82
84
  async function syncDirectory(directoryPath) {
83
85
  const directory = await open(directoryPath, constants$1.O_RDONLY);
@@ -178,7 +180,7 @@ function helperLeaseState(lease, target, owner) {
178
180
  return state;
179
181
  }
180
182
  function validateGateInput(gate) {
181
- if (typeof gate !== "object" || gate === null || Object.keys(gate).length !== 4 || gate.schema !== 1 || typeof gate.generation !== "string" || !UUID.test(gate.generation) || typeof gate.version !== "string" || !VERSION.test(gate.version) || typeof gate.owner !== "string" || !UUID.test(gate.owner)) throw new Error("Invalid update launch gate");
183
+ if (typeof gate !== "object" || gate === null || Object.keys(gate).length !== 4 || gate.schema !== 1 || typeof gate.generation !== "string" || !isUuid(gate.generation) || typeof gate.version !== "string" || !VERSION.test(gate.version) || typeof gate.owner !== "string" || !isUuid(gate.owner)) throw new Error("Invalid update launch gate");
182
184
  return Object.freeze({
183
185
  schema: 1,
184
186
  generation: gate.generation,
@@ -199,23 +201,8 @@ function parseGate(value) {
199
201
  }
200
202
  /** Read the atomic gate record. Missing means open; malformed or non-file state fails closed. */
201
203
  async function readUpdateLaunchGate(target) {
202
- const path = updateLaunchGatePath(target);
203
- let info;
204
- try {
205
- info = await lstat(path);
206
- } catch (error) {
207
- if (error.code === "ENOENT") return void 0;
208
- throw error;
209
- }
210
- if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > MAX_GATE_BYTES) throw new Error("Invalid update launch gate");
211
- const file = await open(path, constants$1.O_RDONLY | (constants$1.O_NOFOLLOW ?? 0));
212
- try {
213
- const opened = await file.stat();
214
- if (!opened.isFile() || opened.size < 1 || opened.size > MAX_GATE_BYTES) throw new Error("Invalid update launch gate");
215
- return parseGate(JSON.parse(await file.readFile("utf8")));
216
- } finally {
217
- await file.close();
218
- }
204
+ const text = await readSmallStateFile(updateLaunchGatePath(target), MAX_GATE_BYTES, "Invalid update launch gate");
205
+ return text === void 0 ? void 0 : parseGate(JSON.parse(text));
219
206
  }
220
207
  function sameGate(left, right) {
221
208
  return left?.schema === right.schema && left.generation === right.generation && left.version === right.version && left.owner === right.owner;
@@ -1,10 +1,11 @@
1
+ import { isCanonicalPath } from "../native/canonical-path.js";
1
2
  import { INHERITED_LAUNCH_LEASE_ENV } from "./launch-environment.js";
2
- import { dirname, isAbsolute, join, resolve } from "node:path";
3
+ import { dirname, join } from "node:path";
3
4
  import { spawn } from "node:child_process";
4
5
  // src/updates/update-launch.ts
5
6
  /** The Electrobun launcher inside an installed app tree. */
6
7
  function updateLauncherPath(platform, target) {
7
- if (typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target) throw new Error("Invalid update target");
8
+ if (!isCanonicalPath(target)) throw new Error("Invalid update target");
8
9
  if (platform === "macos-arm64") return join(target, "Contents", "MacOS", "launcher");
9
10
  if (platform === "windows-x64") return join(target, "bin", "launcher.exe");
10
11
  if (platform === "linux-x64" || platform === "linux-arm64") return join(target, "bin", "launcher");
@@ -1,9 +1,10 @@
1
+ import { isCanonicalPath } from "../native/canonical-path.js";
1
2
  import { tryAcquireNativeFileLock } from "./native-file-lock.js";
2
- import { basename, dirname, isAbsolute, join, resolve } from "node:path";
3
+ import { basename, dirname, join } from "node:path";
3
4
  // src/updates/update-lock.ts
4
5
  /** Stable transition lock path. The file must never be removed or replaced. */
5
6
  function updateLockPath(target) {
6
- if (typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target || target.includes("\0")) throw new Error("Invalid update lock target");
7
+ if (!isCanonicalPath(target)) throw new Error("Invalid update lock target");
7
8
  return join(dirname(target), `.${basename(target)}.framework-update.lock`);
8
9
  }
9
10
  /**
@@ -1,7 +1,8 @@
1
+ import { errorCode } from "../app/value-guards.js";
1
2
  import { link, open, unlink, writeFile } from "node:fs/promises";
2
3
  import { join } from "node:path";
3
- import { closeSync, constants as constants$1, linkSync, openSync, unlinkSync, writeFileSync } from "node:fs";
4
4
  import { randomUUID } from "node:crypto";
5
+ import { closeSync, constants as constants$1, linkSync, openSync, unlinkSync, writeFileSync } from "node:fs";
5
6
  // src/updates/update-marker.ts
6
7
  function isUpdateHelperReadyMarker(value, generation, helperOwner) {
7
8
  return value?.schema === 1 && value.generation === generation && value.helperOwner === helperOwner;
@@ -46,7 +47,7 @@ async function readUpdateMarker(staging, name) {
46
47
  try {
47
48
  file = await open(path, constants$1.O_RDONLY | (constants$1.O_NOFOLLOW ?? 0));
48
49
  } catch (error) {
49
- if (error.code === "ENOENT") return void 0;
50
+ if (errorCode(error) === "ENOENT") return void 0;
50
51
  throw error;
51
52
  }
52
53
  try {
@@ -1,4 +1,6 @@
1
+ import { errorCode } from "../app/value-guards.js";
1
2
  import { linuxProcessIsExited } from "./linux-process-state.js";
3
+ import { windowsProcessImage } from "../native/windows-process.js";
2
4
  import { readFile, realpath } from "node:fs/promises";
3
5
  import { isAbsolute } from "node:path";
4
6
  import { FFIType, dlopen, ptr } from "bun:ffi";
@@ -14,7 +16,7 @@ function isProcessRunning(pid) {
14
16
  process.kill(pid, 0);
15
17
  return true;
16
18
  } catch (error) {
17
- if (error.code === "ESRCH") return false;
19
+ if (errorCode(error) === "ESRCH") return false;
18
20
  throw new Error("Cannot inspect update process", { cause: error });
19
21
  }
20
22
  }
@@ -26,7 +28,7 @@ async function hasVerifiedProcessExited(pid) {
26
28
  try {
27
29
  stat = await readFile(`/proc/${pid}/stat`, "utf8");
28
30
  } catch (error) {
29
- const code = error.code;
31
+ const code = errorCode(error);
30
32
  if ((code === "ENOENT" || code === "ESRCH") && !isProcessRunning(pid)) return true;
31
33
  throw new Error("Cannot inspect update process", { cause: error });
32
34
  }
@@ -57,45 +59,14 @@ async function executable(pid) {
57
59
  }
58
60
  }
59
61
  if (process.platform === "win32") {
60
- const library = dlopen("kernel32.dll", {
61
- OpenProcess: {
62
- args: [
63
- FFIType.u32,
64
- FFIType.i32,
65
- FFIType.u32
66
- ],
67
- returns: FFIType.ptr
68
- },
69
- QueryFullProcessImageNameW: {
70
- args: [
71
- FFIType.ptr,
72
- FFIType.u32,
73
- FFIType.ptr,
74
- FFIType.ptr
75
- ],
76
- returns: FFIType.i32
77
- },
78
- CloseHandle: {
79
- args: [FFIType.ptr],
80
- returns: FFIType.i32
81
- }
82
- });
62
+ let image;
83
63
  try {
84
- const handle = library.symbols.OpenProcess(4096, 0, pid);
85
- if (!handle) throw new Error("Cannot open update process");
86
- try {
87
- const buffer = Buffer.alloc(65536), length = Buffer.alloc(4);
88
- length.writeUInt32LE(32768);
89
- if (!library.symbols.QueryFullProcessImageNameW(handle, 0, ptr(buffer), ptr(length))) throw new Error("Cannot inspect update process");
90
- const count = length.readUInt32LE();
91
- if (!count || count >= 32768) throw new Error("Invalid update process path");
92
- return await realpath(buffer.subarray(0, count * 2).toString("utf16le"));
93
- } finally {
94
- library.symbols.CloseHandle(handle);
95
- }
96
- } finally {
97
- library.close();
64
+ image = windowsProcessImage(pid, { requireRunning: false });
65
+ } catch (error) {
66
+ throw new Error("Cannot inspect update process", { cause: error });
98
67
  }
68
+ if (image === "missing") throw new Error("Cannot open update process");
69
+ return realpath(image);
99
70
  }
100
71
  throw new Error("Unsupported update process platform");
101
72
  }
@@ -1,3 +1,4 @@
1
+ import { errorCode } from "../app/value-guards.js";
1
2
  // src/updates/update-process-liveness.ts
2
3
  /** Only ESRCH proves a recorded process is absent; inspection errors fail closed. */
3
4
  function updateProcessMayBeAlive(pid) {
@@ -5,7 +6,7 @@ function updateProcessMayBeAlive(pid) {
5
6
  process.kill(pid, 0);
6
7
  return true;
7
8
  } catch (error) {
8
- return error.code !== "ESRCH";
9
+ return errorCode(error) !== "ESRCH";
9
10
  }
10
11
  }
11
12
  export { updateProcessMayBeAlive };