@adea-ai/app-framework 0.25.0 → 0.26.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 (138) hide show
  1. package/README.md +157 -19
  2. package/dist/app/application.d.ts +11 -4
  3. package/dist/app/application.js +24 -4
  4. package/dist/app/create-application.d.ts +9 -0
  5. package/dist/app/create-application.js +214 -143
  6. package/dist/app/private-group.d.ts +14 -0
  7. package/dist/app/private-group.js +39 -0
  8. package/dist/app/service-registry.d.ts +8 -4
  9. package/dist/app/service-registry.js +43 -6
  10. package/dist/appearance/custom-theme-import.d.ts +5 -7
  11. package/dist/appearance/custom-theme-import.js +66 -18
  12. package/dist/appearance/custom-theme-library.d.ts +51 -3
  13. package/dist/appearance/custom-theme-library.js +262 -33
  14. package/dist/appearance/editor.d.ts +3 -1
  15. package/dist/appearance/editor.js +37 -2
  16. package/dist/appearance/theme-file-importer.d.ts +7 -3
  17. package/dist/appearance/theme-file-importer.js +27 -7
  18. package/dist/appearance/theme-library-action-fence.d.ts +11 -0
  19. package/dist/appearance/theme-library-action-fence.js +29 -0
  20. package/dist/cli/cef-helper-cli.js +0 -0
  21. package/dist/cli/create-application-cli.js +0 -0
  22. package/dist/cli/electrobun-shortcut-patch-cli.js +0 -0
  23. package/dist/cli/framework-update-cli.js +16 -3
  24. package/dist/cli/macos-signing-cli.js +0 -0
  25. package/dist/cli/release-assets-plan-cli.js +0 -0
  26. package/dist/cli/release-cut-cli.js +0 -0
  27. package/dist/cli/release-descriptor-staging-cli.js +0 -0
  28. package/dist/cli/release-finalize-cli.js +0 -0
  29. package/dist/cli/release-promote-cli.js +0 -0
  30. package/dist/cli/release-publish-cli.js +0 -0
  31. package/dist/cli/release-sign-cli.js +0 -0
  32. package/dist/cli/release-step-cli.js +0 -0
  33. package/dist/cli/toolchain-identity-cli.js +0 -0
  34. package/dist/cli/windows-runtime-patch-cli.js +0 -0
  35. package/dist/desktop/application-command-client.d.ts +11 -1
  36. package/dist/desktop/application-command-client.js +66 -3
  37. package/dist/desktop/channel-handshake.d.ts +2 -1
  38. package/dist/desktop/channel-handshake.js +21 -5
  39. package/dist/desktop/client-asset-stream.d.ts +10 -0
  40. package/dist/desktop/client-asset-stream.js +97 -0
  41. package/dist/desktop/client-assets.d.ts +3 -1
  42. package/dist/desktop/client-assets.js +41 -6
  43. package/dist/desktop/desktop-client.d.ts +15 -1
  44. package/dist/desktop/desktop-client.js +235 -109
  45. package/dist/desktop/desktop-gateway.d.ts +2 -0
  46. package/dist/desktop/desktop-gateway.js +134 -22
  47. package/dist/desktop/desktop-host-websocket.d.ts +10 -0
  48. package/dist/desktop/desktop-host-websocket.js +28 -0
  49. package/dist/desktop/desktop-host.d.ts +9 -0
  50. package/dist/desktop/desktop-host.js +70 -61
  51. package/dist/desktop/desktop-port-file.d.ts +2 -0
  52. package/dist/desktop/desktop-port-file.js +23 -0
  53. package/dist/desktop/desktop-stream.d.ts +5 -0
  54. package/dist/desktop/desktop-stream.js +83 -15
  55. package/dist/desktop/gateway-limits.d.ts +2 -0
  56. package/dist/desktop/gateway-limits.js +6 -0
  57. package/dist/desktop/open-url-channel.d.ts +5 -3
  58. package/dist/desktop/open-url-channel.js +18 -5
  59. package/dist/desktop/typed-array-intrinsics.d.ts +6 -0
  60. package/dist/desktop/typed-array-intrinsics.js +19 -0
  61. package/dist/framework-source.json +1 -1
  62. package/dist/installer/installed-payload.js +1 -1
  63. package/dist/installer/installer-ci-bootstrap.js +2 -1
  64. package/dist/installer/installer-command.d.ts +13 -0
  65. package/dist/installer/installer-command.js +47 -0
  66. package/dist/installer/installer-extraction.js +27 -61
  67. package/dist/installer/installer-identity.d.ts +19 -3
  68. package/dist/installer/installer-identity.js +58 -70
  69. package/dist/installer/installer-image-mount.d.ts +48 -0
  70. package/dist/installer/installer-image-mount.js +347 -0
  71. package/dist/installer/installer-layout.js +1 -1
  72. package/dist/installer/installer-process-cleanup.js +1 -1
  73. package/dist/installer/installer-renderer-evidence.js +6 -2
  74. package/dist/installer/upgrade-asset-download.js +30 -8
  75. package/dist/native/app-bundle.d.ts +2 -2
  76. package/dist/native/app-bundle.js +5 -4
  77. package/dist/native/cef-helper-download.d.ts +1 -0
  78. package/dist/native/cef-helper-download.js +35 -0
  79. package/dist/native/cef-helper.js +146 -61
  80. package/dist/native/exclusive-rename.d.ts +2 -0
  81. package/dist/native/exclusive-rename.js +52 -0
  82. package/dist/native/native-secret-store.js +5 -4
  83. package/dist/native/partitioned-window.d.ts +5 -2
  84. package/dist/native/partitioned-window.js +69 -9
  85. package/dist/native/sidecar-services.js +2 -2
  86. package/dist/native/sidecar.js +68 -25
  87. package/dist/native/window-bootstrap.js +2 -2
  88. package/dist/release/feed-state.js +2 -1
  89. package/dist/release/framework-update-lock.d.ts +2 -0
  90. package/dist/release/framework-update-lock.js +42 -0
  91. package/dist/release/framework-update.d.ts +6 -2
  92. package/dist/release/framework-update.js +165 -40
  93. package/dist/release/local-release.js +68 -39
  94. package/dist/release/release-config.js +1 -1
  95. package/dist/shared/discard-stream.d.ts +4 -0
  96. package/dist/shared/discard-stream.js +9 -0
  97. package/dist/shell/app-shell.js +59 -7
  98. package/dist/shell/app-utilities.js +2 -2
  99. package/dist/shell/application-menu.d.ts +13 -0
  100. package/dist/shell/application-menu.js +21 -8
  101. package/dist/update-helper.bundle.js +40 -4
  102. package/dist/updates/channel-update-manager.d.ts +2 -0
  103. package/dist/updates/channel-update-manager.js +15 -3
  104. package/dist/updates/desktop-updater.d.ts +2 -0
  105. package/dist/updates/desktop-updater.js +36 -7
  106. package/dist/updates/linux-process-state.d.ts +2 -0
  107. package/dist/updates/linux-process-state.js +13 -0
  108. package/dist/updates/release-location.js +5 -4
  109. package/dist/updates/update-adapter.js +5 -0
  110. package/dist/updates/update-arm.d.ts +2 -0
  111. package/dist/updates/update-arm.js +58 -7
  112. package/dist/updates/update-discovery.js +2 -1
  113. package/dist/updates/update-download-cleanup.d.ts +7 -0
  114. package/dist/updates/update-download-cleanup.js +21 -0
  115. package/dist/updates/update-download.js +41 -20
  116. package/dist/updates/update-exclusive-rename.d.ts +3 -2
  117. package/dist/updates/update-exclusive-rename.js +3 -50
  118. package/dist/updates/update-extraction.js +1 -1
  119. package/dist/updates/update-manager.d.ts +17 -1
  120. package/dist/updates/update-manager.js +27 -8
  121. package/dist/updates/update-process-fence.js +18 -3
  122. package/dist/views/appearance-surface.js +51 -15
  123. package/package.json +11 -6
  124. package/src/app/application.tsx +49 -7
  125. package/src/appearance/custom-theme-import.ts +67 -7
  126. package/src/appearance/custom-theme-library.ts +391 -33
  127. package/src/appearance/editor.ts +49 -10
  128. package/src/appearance/theme-file-importer.ts +43 -12
  129. package/src/appearance/theme-library-action-fence.ts +28 -0
  130. package/src/desktop/client-asset-stream.ts +118 -0
  131. package/src/desktop/client-assets.ts +55 -5
  132. package/src/shell/app-shell.tsx +71 -8
  133. package/src/shell/app-utilities.tsx +2 -2
  134. package/src/updates/update-adapter.ts +5 -0
  135. package/src/views/appearance-surface.tsx +61 -29
  136. package/templates/application/README.md +5 -1
  137. package/templates/application/scripts/app-version.ts +7 -3
  138. package/templates/application/src/bun/index.ts +2 -0
@@ -1,7 +1,7 @@
1
+ import { downloadCefHelperInput } from "./cef-helper-download.js";
1
2
  import { CEF_BRIDGE_PATCH_PATH, CEF_HELPER_SOURCES, ELECTROBUN_COMMIT, PINNED_CEF_BRIDGE_PATCH, applyElectrobunPatch } from "./electrobun-source-patch.js";
2
- import { chmod, lstat, mkdir, mkdtemp, readFile, readdir, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
3
+ import { chmod, lstat, mkdir, mkdtemp, open, readdir, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
3
4
  import { dirname, isAbsolute, join } from "node:path";
4
- import { createWriteStream } from "node:fs";
5
5
  import { spawnSync } from "node:child_process";
6
6
  import { createHash, randomUUID } from "node:crypto";
7
7
  import { availableParallelism, homedir } from "node:os";
@@ -13,6 +13,8 @@ var CHROMIUM_VERSION = "156.0.8078.17";
13
13
  var MACOS_DEPLOYMENT_TARGET = "14.0";
14
14
  /** Bump when the build recipe below changes, so cached helpers are rebuilt. */
15
15
  var RECIPE = 1;
16
+ /** Change when the on-disk cache format changes, independently of the build recipe. */
17
+ var CACHE_FORMAT = 2;
16
18
  /** Targets the packaged native probes exercise; any other CEF bundle fails closed. */
17
19
  var TARGETS = {
18
20
  "macos-arm64": {
@@ -54,7 +56,8 @@ function cefHelperCacheKey(os, arch) {
54
56
  CEF_VERSION,
55
57
  CHROMIUM_VERSION,
56
58
  pin.sdkSha256,
57
- RECIPE
59
+ RECIPE,
60
+ CACHE_FORMAT
58
61
  ]);
59
62
  return `cef-helper-${os}-${arch}-${sha256(Buffer.from(identity)).slice(0, 16)}`;
60
63
  }
@@ -68,60 +71,127 @@ function cefHelperCacheRoot(env) {
68
71
  const base = process.platform === "darwin" ? join(homedir(), "Library", "Caches") : process.platform === "win32" ? env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local") : env.XDG_CACHE_HOME ?? join(homedir(), ".cache");
69
72
  return join(base, "adea-app-framework");
70
73
  }
71
- /** Return the cached patched helper for this target, building it once from the pinned source. */
72
- async function ensurePatchedCefHelper(env, os, arch, build = buildPatchedCefHelper) {
73
- const pin = target(os, arch);
74
- const root = cefHelperCacheRoot(env);
75
- const entry = join(root, cefHelperCacheKey(os, arch));
76
- const output = join(entry, pin.platform === "win" ? "process_helper.exe" : "process_helper");
74
+ var CACHE_MANIFEST = "manifest.json";
75
+ var HASH_CHUNK_BYTES = 1048576;
76
+ function sameFile(left, right) {
77
+ return right.isFile() && left.dev === right.dev && left.ino === right.ino && left.size === right.size && left.mtimeMs === right.mtimeMs && left.ctimeMs === right.ctimeMs;
78
+ }
79
+ async function openRegular(path) {
80
+ const info = await lstat(path);
81
+ if (!info.isFile() || !Number.isSafeInteger(info.size) || info.size < 1) throw new Error("Invalid cached CEF helper");
82
+ const file = await open(path, "r");
77
83
  try {
78
- const info = await lstat(output);
79
- if (info.isFile() && !info.isSymbolicLink() && info.size > 0) return output;
80
- throw new Error("Invalid cached CEF helper");
84
+ if (!sameFile(info, await file.stat())) throw new Error("Invalid cached CEF helper");
85
+ return {
86
+ file,
87
+ info
88
+ };
81
89
  } catch (error) {
82
- if (error.code !== "ENOENT") throw error;
90
+ await file.close();
91
+ throw error;
83
92
  }
84
- await mkdir(root, { recursive: true });
85
- const work = await mkdtemp(join(root, ".build-"));
93
+ }
94
+ async function helperDigest(path, destination) {
95
+ const { file, info } = await openRegular(path);
96
+ const hash = createHash("sha256");
97
+ const buffer = Buffer.allocUnsafe(HASH_CHUNK_BYTES);
98
+ let offset = 0;
86
99
  try {
87
- const staged = join(work, "out");
88
- await mkdir(staged);
89
- await build(pin, work, join(staged, pin.platform === "win" ? "process_helper.exe" : "process_helper"));
90
- try {
91
- await rename(staged, entry);
92
- } catch (error) {
93
- if (![
94
- "EEXIST",
95
- "ENOTEMPTY",
96
- "EPERM"
97
- ].includes(error.code ?? "")) throw error;
98
- await stat(output);
100
+ while (offset < info.size) {
101
+ const { bytesRead } = await file.read(buffer, 0, Math.min(buffer.length, info.size - offset), offset);
102
+ if (!bytesRead) throw new Error("Invalid cached CEF helper");
103
+ hash.update(buffer.subarray(0, bytesRead));
104
+ if (destination) {
105
+ let written = 0;
106
+ while (written < bytesRead) {
107
+ const result = await destination.write(buffer, written, bytesRead - written);
108
+ if (!result.bytesWritten) throw new Error("CEF helper copy failed");
109
+ written += result.bytesWritten;
110
+ }
111
+ }
112
+ offset += bytesRead;
99
113
  }
100
- return output;
114
+ if (!sameFile(info, await file.stat())) throw new Error("Invalid cached CEF helper");
115
+ return {
116
+ size: offset,
117
+ sha256: hash.digest("hex")
118
+ };
101
119
  } finally {
102
- await rm(work, {
103
- recursive: true,
104
- force: true
105
- });
120
+ await file.close();
106
121
  }
107
122
  }
108
- async function download(url, path, maxBytes) {
109
- const response = await fetch(url, { signal: AbortSignal.timeout(9e5) });
110
- if (!response.ok || !response.body) throw new Error("CEF helper download failed");
111
- const hash = createHash("sha256");
112
- const file = createWriteStream(path, { flags: "wx" });
113
- let size = 0;
123
+ async function readManifest(path) {
124
+ const { file, info } = await openRegular(path);
114
125
  try {
115
- for await (const chunk of response.body) {
116
- size += chunk.byteLength;
117
- if (size > maxBytes) throw new Error("CEF helper download exceeded its size");
118
- hash.update(chunk);
119
- if (!file.write(chunk)) await new Promise((resolve) => file.once("drain", resolve));
126
+ if (info.size > 1024) throw new Error("Invalid cached CEF helper");
127
+ const bytes = Buffer.alloc(info.size);
128
+ let offset = 0;
129
+ while (offset < bytes.length) {
130
+ const { bytesRead } = await file.read(bytes, offset, bytes.length - offset, offset);
131
+ if (!bytesRead) throw new Error("Invalid cached CEF helper");
132
+ offset += bytesRead;
120
133
  }
134
+ if (!sameFile(info, await file.stat())) throw new Error("Invalid cached CEF helper");
135
+ return bytes.toString("utf8");
121
136
  } finally {
122
- await new Promise((resolve) => file.end(resolve));
137
+ await file.close();
123
138
  }
124
- return hash.digest("hex");
139
+ }
140
+ async function verifyCachedHelper(entry, output, key) {
141
+ try {
142
+ if (!(await lstat(entry)).isDirectory()) throw new Error("Invalid cached CEF helper");
143
+ const manifest = await readManifest(join(entry, CACHE_MANIFEST));
144
+ const actual = await helperDigest(output);
145
+ if (manifest !== JSON.stringify({
146
+ key,
147
+ ...actual
148
+ }) + "\n") throw new Error("Invalid cached CEF helper");
149
+ } catch (error) {
150
+ if (error.code === "ENOENT") throw new Error("Invalid cached CEF helper", { cause: error });
151
+ throw error;
152
+ }
153
+ }
154
+ /** Return the cached patched helper for this target, building it once from the pinned source. */
155
+ async function ensurePatchedCefHelper(env, os, arch, build = buildPatchedCefHelper) {
156
+ const pin = target(os, arch);
157
+ const root = cefHelperCacheRoot(env);
158
+ const key = cefHelperCacheKey(os, arch);
159
+ const entry = join(root, key);
160
+ const output = join(entry, pin.platform === "win" ? "process_helper.exe" : "process_helper");
161
+ try {
162
+ await lstat(entry);
163
+ } catch (error) {
164
+ if (error.code !== "ENOENT") throw error;
165
+ await mkdir(root, { recursive: true });
166
+ const work = await mkdtemp(join(root, ".build-"));
167
+ try {
168
+ const staged = join(work, "out");
169
+ await mkdir(staged);
170
+ const stagedOutput = join(staged, pin.platform === "win" ? "process_helper.exe" : "process_helper");
171
+ await build(pin, work, stagedOutput);
172
+ const digest = await helperDigest(stagedOutput);
173
+ await writeFile(join(staged, CACHE_MANIFEST), JSON.stringify({
174
+ key,
175
+ ...digest
176
+ }) + "\n", { flag: "wx" });
177
+ try {
178
+ await rename(staged, entry);
179
+ } catch (publishError) {
180
+ if (![
181
+ "EEXIST",
182
+ "ENOTEMPTY",
183
+ "EPERM"
184
+ ].includes(publishError.code ?? "")) throw publishError;
185
+ }
186
+ } finally {
187
+ await rm(work, {
188
+ recursive: true,
189
+ force: true
190
+ });
191
+ }
192
+ }
193
+ await verifyCachedHelper(entry, output, key);
194
+ return output;
125
195
  }
126
196
  function step(name, command, args, cwd) {
127
197
  const result = spawnSync(command, args, {
@@ -156,14 +226,14 @@ async function buildPatchedCefHelper(pin, work, output) {
156
226
  const source = join(work, "electrobun");
157
227
  for (const path of Object.values(CEF_HELPER_SOURCES)) {
158
228
  await mkdir(dirname(join(source, path)), { recursive: true });
159
- await download(`https://raw.githubusercontent.com/blackboardsh/electrobun/${ELECTROBUN_COMMIT}/${path}`, join(source, path), 65536);
229
+ await downloadCefHelperInput(`https://raw.githubusercontent.com/blackboardsh/electrobun/${ELECTROBUN_COMMIT}/${path}`, join(source, path), 65536);
160
230
  }
161
231
  step("source", "git", ["init", "-q"], source);
162
232
  applyElectrobunPatch(source, CEF_BRIDGE_PATCH_PATH, PINNED_CEF_BRIDGE_PATCH);
163
233
  const helperSource = join(source, CEF_HELPER_SOURCES[pin.platform]);
164
234
  const distribution = `cef_binary_${CEF_VERSION}+chromium-${CHROMIUM_VERSION}_${pin.sdk}_minimal`;
165
235
  const archive = join(work, "cef.tar.bz2");
166
- if (await download(`https://cef-builds.spotifycdn.com/${encodeURIComponent(distribution)}.tar.bz2`, archive, pin.sdkBytes) !== pin.sdkSha256 || (await stat(archive)).size !== pin.sdkBytes) throw new Error("Unexpected CEF distribution digest");
236
+ if (await downloadCefHelperInput(`https://cef-builds.spotifycdn.com/${encodeURIComponent(distribution)}.tar.bz2`, archive, pin.sdkBytes) !== pin.sdkSha256 || (await stat(archive)).size !== pin.sdkBytes) throw new Error("Unexpected CEF distribution digest");
167
237
  const cef = join(work, "cef");
168
238
  await mkdir(cef);
169
239
  const library = {
@@ -377,25 +447,40 @@ async function installPatchedCefHelpers(env, ensure = ensurePatchedCefHelper, st
377
447
  if (!cef) return 0;
378
448
  const pin = target(env.ELECTROBUN_OS, env.ELECTROBUN_ARCH);
379
449
  if (!helpers.length) throw new Error("Expected bundled CEF helpers");
380
- const patched = await readFile(await ensure(env, env.ELECTROBUN_OS, env.ELECTROBUN_ARCH));
381
- const patchedSha256 = sha256(patched);
450
+ const patchedPath = await ensure(env, env.ELECTROBUN_OS, env.ELECTROBUN_ARCH);
451
+ const patched = await helperDigest(patchedPath);
382
452
  const stock = [];
383
453
  for (const path of helpers) {
384
- const digest = sha256(await readFile(path));
385
- if (digest === (stockSha256 ?? pin.stockHelperSha256)) stock.push(path);
386
- else if (digest !== patchedSha256) throw new Error("Unexpected CEF helper digest");
454
+ const info = await lstat(path);
455
+ const digest = await helperDigest(path);
456
+ if (!sameFile(info, await lstat(path))) throw new Error("Unexpected CEF helper change");
457
+ if (digest.sha256 === (stockSha256 ?? pin.stockHelperSha256)) stock.push({
458
+ path,
459
+ info
460
+ });
461
+ else if (digest.sha256 !== patched.sha256) throw new Error("Unexpected CEF helper digest");
387
462
  }
388
- for (const path of stock) {
389
- const temp = `${path}.${randomUUID()}.tmp`;
390
- try {
391
- await writeFile(temp, patched, {
392
- flag: "wx",
393
- mode: (await stat(path)).mode & 511
463
+ const staged = [];
464
+ try {
465
+ for (const { path, info } of stock) {
466
+ const temp = `${path}.${randomUUID()}.tmp`;
467
+ const output = await open(temp, "wx", info.mode & 511);
468
+ staged.push({
469
+ path,
470
+ temp
394
471
  });
395
- await rename(temp, path);
396
- } finally {
397
- await rm(temp, { force: true });
472
+ try {
473
+ const copied = await helperDigest(patchedPath, output);
474
+ if (copied.size !== patched.size || copied.sha256 !== patched.sha256) throw new Error("Unexpected CEF helper digest");
475
+ } finally {
476
+ await output.close();
477
+ }
478
+ await chmod(temp, info.mode & 511);
398
479
  }
480
+ for (const { path, info } of stock) if (!sameFile(info, await lstat(path))) throw new Error("Unexpected CEF helper change");
481
+ for (const { path, temp } of staged) await rename(temp, path);
482
+ } finally {
483
+ for (const { temp } of staged) await rm(temp, { force: true });
399
484
  }
400
485
  return helpers.length;
401
486
  }
@@ -0,0 +1,2 @@
1
+ /** Rename without replacing an existing destination on supported desktop platforms. */
2
+ export declare function renameExclusive(source: string, destination: string): void;
@@ -0,0 +1,52 @@
1
+ import { isAbsolute, resolve } from "node:path";
2
+ import { FFIType, dlopen, ptr } from "bun:ffi";
3
+ // src/native/exclusive-rename.ts
4
+ /** Rename without replacing an existing destination on supported desktop platforms. */
5
+ function renameExclusive(source, destination) {
6
+ if (typeof source !== "string" || typeof destination !== "string" || !isAbsolute(source) || !isAbsolute(destination) || resolve(source) !== source || resolve(destination) !== destination || source === destination || source.includes("\0") || destination.includes("\0")) throw new Error("Invalid exclusive rename paths");
7
+ if (process.platform === "darwin") {
8
+ const library = dlopen("libSystem.B.dylib", { renamex_np: {
9
+ args: [
10
+ FFIType.ptr,
11
+ FFIType.ptr,
12
+ FFIType.u32
13
+ ],
14
+ returns: FFIType.i32
15
+ } });
16
+ try {
17
+ const from = Buffer.from(source + "\0"), to = Buffer.from(destination + "\0");
18
+ if (library.symbols.renamex_np(ptr(from), ptr(to), 4) !== 0) throw new Error("Exclusive rename failed");
19
+ } finally {
20
+ library.close();
21
+ }
22
+ } else if (process.platform === "linux") {
23
+ const library = dlopen("libc.so.6", { renameat2: {
24
+ args: [
25
+ FFIType.i32,
26
+ FFIType.ptr,
27
+ FFIType.i32,
28
+ FFIType.ptr,
29
+ FFIType.u32
30
+ ],
31
+ returns: FFIType.i32
32
+ } });
33
+ try {
34
+ const from = Buffer.from(source + "\0"), to = Buffer.from(destination + "\0");
35
+ if (library.symbols.renameat2(-100, ptr(from), -100, ptr(to), 1) !== 0) throw new Error("Exclusive rename failed");
36
+ } finally {
37
+ library.close();
38
+ }
39
+ } else if (process.platform === "win32") {
40
+ const library = dlopen("kernel32.dll", { MoveFileW: {
41
+ args: [FFIType.ptr, FFIType.ptr],
42
+ returns: FFIType.i32
43
+ } });
44
+ try {
45
+ const from = Buffer.from(source + "\0", "utf16le"), to = Buffer.from(destination + "\0", "utf16le");
46
+ if (library.symbols.MoveFileW(ptr(from), ptr(to)) === 0) throw new Error("Exclusive rename failed");
47
+ } finally {
48
+ library.close();
49
+ }
50
+ } else throw new Error("Unsupported exclusive rename platform");
51
+ }
52
+ export { renameExclusive };
@@ -2,8 +2,9 @@
2
2
  var identifier = (value) => typeof value === "string" && value.length > 0 && value.length <= 128 && /^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(value);
3
3
  /** Native credentials only. Apps choose the service namespace and own authorization and migration. */
4
4
  function createNativeSecretStore(options) {
5
+ const service = options.service;
5
6
  const maxBytes = options.maxBytes ?? 2048;
6
- if (!identifier(options.service) || !Number.isSafeInteger(maxBytes) || maxBytes < 1 || maxBytes > 2048) throw new Error("Invalid credential store configuration");
7
+ if (!identifier(service) || !Number.isSafeInteger(maxBytes) || maxBytes < 1 || maxBytes > 2048) throw new Error("Invalid credential store configuration");
7
8
  const backend = options.backend ?? Bun.secrets;
8
9
  if (!backend || typeof backend.get !== "function" || typeof backend.set !== "function" || typeof backend.delete !== "function") throw new Error("Credential store unavailable");
9
10
  let queue = Promise.resolve();
@@ -19,7 +20,7 @@ function createNativeSecretStore(options) {
19
20
  async function read(account) {
20
21
  try {
21
22
  const value = await backend.get({
22
- service: options.service,
23
+ service,
23
24
  name: account
24
25
  });
25
26
  if (value !== null && (typeof value !== "string" || value.includes("\0") || Buffer.byteLength(value, "utf8") > maxBytes)) throw new Error("Invalid native result");
@@ -39,7 +40,7 @@ function createNativeSecretStore(options) {
39
40
  return ordered(async () => {
40
41
  try {
41
42
  await backend.set({
42
- service: options.service,
43
+ service,
43
44
  name: key,
44
45
  value,
45
46
  persist: "local"
@@ -55,7 +56,7 @@ function createNativeSecretStore(options) {
55
56
  return ordered(async () => {
56
57
  try {
57
58
  const removed = await backend.delete({
58
- service: options.service,
59
+ service,
59
60
  name: key
60
61
  });
61
62
  if (typeof removed !== "boolean" || await read(key) !== null) throw new Error("Invalid native result");
@@ -24,8 +24,11 @@ export type PartitionedWindow = {
24
24
  * runs synchronously, this assigns the partition to the first view initialized
25
25
  * and restores `BrowserView.prototype.init` afterwards. It fails closed (and
26
26
  * closes the window) unless exactly one view was created and the window's view
27
- * reports the requested partition. Without a partition it only calls
28
- * `construct`, so ordinary windows keep Electrobun's default profile.
27
+ * reports the requested partition. Without a partition it calls `construct`
28
+ * with the default profile, unless a patch is active or the class could not be
29
+ * restored. An own accessor for `init` is unsupported. If restoration fails,
30
+ * this helper refuses that prototype and its descendants; it cannot repair a
31
+ * frozen prototype or control direct calls outside this helper.
29
32
  */
30
33
  export declare function constructPartitionedWindow<Window extends PartitionedWindow>(options: {
31
34
  BrowserView: PartitionableViewClass;
@@ -1,6 +1,18 @@
1
1
  // src/native/partitioned-window.ts
2
2
  var PARTITION = /^(?:persist:)?[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
3
3
  var constructing = false;
4
+ var unrestored = /* @__PURE__ */ new WeakSet();
5
+ function hasUnrestoredPrototype(prototype) {
6
+ const seen = /* @__PURE__ */ new Set();
7
+ let current = prototype;
8
+ while (current) {
9
+ if (unrestored.has(current) || seen.has(current)) return true;
10
+ seen.add(current);
11
+ current = Object.getPrototypeOf(current);
12
+ }
13
+ return false;
14
+ }
15
+ var sameDescriptor = (a, b) => a === void 0 || b === void 0 ? a === b : a.value === b.value && a.get === b.get && a.set === b.set && a.writable === b.writable && a.enumerable === b.enumerable && a.configurable === b.configurable;
4
16
  /**
5
17
  * Construct a `BrowserWindow` whose own webview uses `partition`.
6
18
  *
@@ -9,35 +21,83 @@ var constructing = false;
9
21
  * runs synchronously, this assigns the partition to the first view initialized
10
22
  * and restores `BrowserView.prototype.init` afterwards. It fails closed (and
11
23
  * closes the window) unless exactly one view was created and the window's view
12
- * reports the requested partition. Without a partition it only calls
13
- * `construct`, so ordinary windows keep Electrobun's default profile.
24
+ * reports the requested partition. Without a partition it calls `construct`
25
+ * with the default profile, unless a patch is active or the class could not be
26
+ * restored. An own accessor for `init` is unsupported. If restoration fails,
27
+ * this helper refuses that prototype and its descendants; it cannot repair a
28
+ * frozen prototype or control direct calls outside this helper.
14
29
  */
15
30
  function constructPartitionedWindow(options) {
16
31
  const { partition } = options;
32
+ const prototype = options.BrowserView.prototype;
33
+ if (hasUnrestoredPrototype(prototype)) throw new Error("Electrobun BrowserView could not be restored");
34
+ if (constructing) throw new Error("Partitioned windows must be constructed one at a time");
17
35
  if (partition === void 0 || partition === null) return options.construct();
18
36
  if (!PARTITION.test(partition)) throw new Error("Invalid browser partition");
19
- const prototype = options.BrowserView.prototype;
37
+ const descriptor = Object.getOwnPropertyDescriptor(prototype, "init");
38
+ if (descriptor && !("value" in descriptor)) throw new Error("Unsupported Electrobun BrowserView");
20
39
  const init = prototype.init;
21
40
  if (typeof init !== "function") throw new Error("Unsupported Electrobun BrowserView");
22
- if (constructing) throw new Error("Partitioned windows must be constructed one at a time");
23
41
  let views = 0;
24
42
  let window;
43
+ let setupFailed = false;
44
+ let setupError;
45
+ let restoreFailed = false;
46
+ let restoreError;
25
47
  constructing = true;
26
- prototype.init = function() {
48
+ const patched = function() {
27
49
  if (++views === 1) this.partition = partition;
28
50
  return init.call(this);
29
51
  };
30
52
  try {
53
+ const patchDescriptor = descriptor ? {
54
+ ...descriptor,
55
+ value: patched
56
+ } : {
57
+ value: patched,
58
+ writable: true,
59
+ enumerable: false,
60
+ configurable: true
61
+ };
62
+ Object.defineProperty(prototype, "init", patchDescriptor);
63
+ if (!sameDescriptor(Object.getOwnPropertyDescriptor(prototype, "init"), patchDescriptor)) throw new Error("Could not install Electrobun BrowserView patch");
31
64
  window = options.construct();
65
+ } catch (error) {
66
+ setupFailed = true;
67
+ setupError = error;
32
68
  } finally {
33
- prototype.init = init;
34
- constructing = false;
69
+ try {
70
+ if (!sameDescriptor(Object.getOwnPropertyDescriptor(prototype, "init"), descriptor)) {
71
+ if (descriptor) Object.defineProperty(prototype, "init", descriptor);
72
+ else Reflect.deleteProperty(prototype, "init");
73
+ }
74
+ if (!sameDescriptor(Object.getOwnPropertyDescriptor(prototype, "init"), descriptor)) {
75
+ restoreFailed = true;
76
+ restoreError = /* @__PURE__ */ new Error("BrowserView init did not restore");
77
+ }
78
+ } catch (error) {
79
+ restoreFailed = true;
80
+ restoreError = error;
81
+ } finally {
82
+ constructing = false;
83
+ }
35
84
  }
36
- if (views !== 1 || window.webview?.partition !== partition) {
85
+ if (restoreFailed) {
86
+ unrestored.add(prototype);
87
+ try {
88
+ window?.close();
89
+ } catch {}
90
+ throw new Error("Electrobun BrowserView could not be restored", { cause: restoreError });
91
+ }
92
+ if (setupFailed) throw setupError;
93
+ if (!window) throw new Error("Electrobun did not create a window");
94
+ try {
95
+ if (views !== 1 || window.webview?.partition !== partition) throw new Error("Electrobun did not create the window view in the requested partition");
96
+ } catch (error) {
37
97
  try {
38
98
  window.close();
39
99
  } catch {}
40
- throw new Error("Electrobun did not create the window view in the requested partition");
100
+ throw error;
41
101
  }
42
102
  return window;
43
103
  }
@@ -30,8 +30,8 @@ function registerSidecarServices(options) {
30
30
  for (const command of commands) unregister.push(services.register(prefix + command, {
31
31
  parse: (value) => parse(command, value),
32
32
  authorize: (principal) => authorize(principal, command),
33
- async run(_principal, args) {
34
- const reply = await sidecar.call(command, args);
33
+ async run(_principal, args, signal) {
34
+ const reply = await sidecar.call(command, args, signal ? { signal } : void 0);
35
35
  if (reply.ok) return reply.value;
36
36
  throw new ServiceError(reply.reason, reply.error);
37
37
  },