peachsim 0.1.709 → 0.1.710

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 (83) hide show
  1. package/cli/commands/control.ts +55 -1
  2. package/cli/current-sim.ts +15 -1
  3. package/cli/drivers/playwright-sim-host.ts +14 -1
  4. package/cli/drivers/playwright.ts +36 -3
  5. package/cli/main.ts +8 -0
  6. package/cli/parent-pid.ts +91 -15
  7. package/cli/ws-bridge.ts +5 -0
  8. package/dist-lib/agent-daemon-client.cjs +1 -1
  9. package/dist-lib/agent-events.cjs +1 -1
  10. package/dist-lib/agent-identity.cjs +1 -1
  11. package/dist-lib/agent-sessions.cjs +1 -1
  12. package/dist-lib/attached-projects.cjs +1 -1
  13. package/dist-lib/auth/shared-session.cjs +1 -1
  14. package/dist-lib/backend-origin.cjs +1 -1
  15. package/dist-lib/beta.cjs +1 -1
  16. package/dist-lib/beta.mjs +1 -1
  17. package/dist-lib/bridge-constants.cjs +1 -1
  18. package/dist-lib/bridge-contract-input.cjs +1 -1
  19. package/dist-lib/bridge-contract-input.mjs +1 -1
  20. package/dist-lib/bridge-contract.cjs +1 -1
  21. package/dist-lib/bridge-contract.mjs +1 -1
  22. package/dist-lib/capture-contract.cjs +1 -1
  23. package/dist-lib/capture-contract.mjs +1 -1
  24. package/dist-lib/cli-constants.cjs +1 -1
  25. package/dist-lib/cloud-contract.cjs +1 -1
  26. package/dist-lib/cloud-contract.mjs +1 -1
  27. package/dist-lib/cloud-test-args.cjs +1 -1
  28. package/dist-lib/cloud-test-args.mjs +1 -1
  29. package/dist-lib/cloud.cjs +1 -1
  30. package/dist-lib/cloud.mjs +1 -1
  31. package/dist-lib/config.cjs +1 -1
  32. package/dist-lib/detox/index.cjs +78 -5
  33. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  34. package/dist-lib/home-paths.cjs +1 -1
  35. package/dist-lib/host/bridge-host.cjs +172 -30
  36. package/dist-lib/host/fetch-proxy-handler.cjs +45 -21
  37. package/dist-lib/host/fetch-proxy-overrides.cjs +2 -2
  38. package/dist-lib/host/fetch-proxy-overrides.mjs +2 -2
  39. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  40. package/dist-lib/host/websocket-proxy.cjs +1 -1
  41. package/dist-lib/index.cjs +78 -5
  42. package/dist-lib/jump-to-source-babel.cjs +1 -1
  43. package/dist-lib/jump-to-source-native.cjs +1 -1
  44. package/dist-lib/kotlin-host.cjs +1 -1
  45. package/dist-lib/menu.cjs +1 -1
  46. package/dist-lib/menu.mjs +1 -1
  47. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  48. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  49. package/dist-lib/metro-production-bundle.cjs +1 -1
  50. package/dist-lib/metro-production-bundle.mjs +1 -1
  51. package/dist-lib/metro.cjs +1 -1
  52. package/dist-lib/native-app-identity.cjs +1 -1
  53. package/dist-lib/native-app-identity.mjs +1 -1
  54. package/dist-lib/native-firebase-metadata.cjs +1 -1
  55. package/dist-lib/native-firebase-metadata.mjs +1 -1
  56. package/dist-lib/plist.cjs +1 -1
  57. package/dist-lib/plist.mjs +1 -1
  58. package/dist-lib/profiles.cjs +1 -1
  59. package/dist-lib/public-brand.cjs +1 -1
  60. package/dist-lib/react-native-host-modules.cjs +1 -1
  61. package/dist-lib/react-native-host-modules.mjs +1 -1
  62. package/dist-lib/render-mode.cjs +1 -1
  63. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  64. package/dist-lib/sdk.cjs +1 -1
  65. package/dist-lib/sdk.mjs +1 -1
  66. package/dist-lib/skills.cjs +331 -51
  67. package/dist-lib/swift-compile.cjs +8 -2
  68. package/dist-lib/swift-compile.mjs +8 -2
  69. package/dist-lib/swift-package.cjs +1 -1
  70. package/dist-lib/swift-package.mjs +1 -1
  71. package/dist-lib/swift-submit.cjs +2 -2
  72. package/dist-lib/swift-submit.mjs +2 -2
  73. package/dist-lib/swift-toolchain.cjs +1 -1
  74. package/dist-lib/swift.cjs +1 -1
  75. package/dist-lib/vite-swift.cjs +9 -3
  76. package/dist-lib/vite.cjs +1 -1
  77. package/package.json +2 -1
  78. package/src/host/bridge-host.ts +65 -0
  79. package/src/host/fetch-proxy-handler.ts +52 -23
  80. package/src/host/fetch-proxy-overrides.ts +5 -1
  81. package/src/host/fetch-proxy-transport.ts +82 -0
  82. package/src/swift-compile.ts +8 -2
  83. package/src/swift-submit.ts +2 -1
@@ -1,4 +1,4 @@
1
- /*! rnx v0.1.709 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
1
+ /*! rnx v0.1.710 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
2
2
  let __sootsim_import_meta_url = ''; try { __sootsim_import_meta_url = require('url').pathToFileURL(__filename).href; } catch {}
3
3
  "use strict";
4
4
  var __create = Object.create;
@@ -2244,6 +2244,7 @@ var import_fs = require("fs");
2244
2244
  var import_module = require("module");
2245
2245
  var cached = null;
2246
2246
  var cachedComm = null;
2247
+ var cachedPgid = null;
2247
2248
  function getParentPid(pid) {
2248
2249
  if (!cached) cached = build();
2249
2250
  try {
@@ -2260,6 +2261,14 @@ function getProcessComm(pid) {
2260
2261
  return null;
2261
2262
  }
2262
2263
  }
2264
+ function getProcessPgid(pid) {
2265
+ if (!cachedPgid) cachedPgid = buildPgid();
2266
+ try {
2267
+ return cachedPgid(pid);
2268
+ } catch {
2269
+ return null;
2270
+ }
2271
+ }
2263
2272
  function buildComm() {
2264
2273
  if (process.platform === "linux") {
2265
2274
  return (pid) => {
@@ -2293,6 +2302,31 @@ function build() {
2293
2302
  }
2294
2303
  return () => null;
2295
2304
  }
2305
+ function buildPgid() {
2306
+ if (process.platform === "linux") {
2307
+ return (pid) => {
2308
+ try {
2309
+ const raw = (0, import_fs.readFileSync)(`/proc/${pid}/stat`, "utf8");
2310
+ const end = raw.lastIndexOf(")");
2311
+ if (end < 0) return null;
2312
+ const rest = raw.slice(end + 1).trim().split(/\s+/);
2313
+ const pgid = Number(rest[2]);
2314
+ return Number.isFinite(pgid) && pgid > 0 ? pgid : null;
2315
+ } catch {
2316
+ return null;
2317
+ }
2318
+ };
2319
+ }
2320
+ if (process.platform === "darwin") {
2321
+ const ffi = tryBuildDarwinPgidFfi();
2322
+ if (ffi) return ffi;
2323
+ return (pid) => {
2324
+ const pgid = readProcessTable().get(pid)?.pgid;
2325
+ return pgid !== void 0 && pgid > 0 ? pgid : null;
2326
+ };
2327
+ }
2328
+ return () => null;
2329
+ }
2296
2330
  var PROCESS_TABLE_TTL_MS = 2e3;
2297
2331
  var processTable = { readAt: 0, rows: /* @__PURE__ */ new Map() };
2298
2332
  function readProcessTable() {
@@ -2302,18 +2336,19 @@ function readProcessTable() {
2302
2336
  }
2303
2337
  const rows = /* @__PURE__ */ new Map();
2304
2338
  try {
2305
- const out = (0, import_child_process.execFileSync)("ps", ["-eo", "pid=,ppid=,comm="], {
2339
+ const out = (0, import_child_process.execFileSync)("ps", ["-eo", "pid=,ppid=,pgid=,comm="], {
2306
2340
  encoding: "utf8",
2307
2341
  timeout: 5e3,
2308
2342
  maxBuffer: 8 * 1024 * 1024,
2309
2343
  stdio: ["ignore", "pipe", "ignore"]
2310
2344
  });
2311
2345
  for (const line of out.split("\n")) {
2312
- const parsed = line.trim().match(/^(\d+)\s+(\d+)\s+(.*)$/);
2346
+ const parsed = line.trim().match(/^(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/);
2313
2347
  if (!parsed) continue;
2314
2348
  rows.set(Number(parsed[1]), {
2315
2349
  ppid: Number(parsed[2]),
2316
- comm: (parsed[3].split("/").at(-1) ?? "").slice(0, PBI_COMM_LEN)
2350
+ pgid: Number(parsed[3]),
2351
+ comm: (parsed[4].split("/").at(-1) ?? "").slice(0, PBI_COMM_LEN)
2317
2352
  });
2318
2353
  }
2319
2354
  } catch {
@@ -2390,6 +2425,31 @@ function tryBuildDarwinCommFfi() {
2390
2425
  return null;
2391
2426
  }
2392
2427
  }
2428
+ var PBI_PGID_OFFSET = 100;
2429
+ function tryBuildDarwinPgidFfi() {
2430
+ if (!process.versions?.bun) return null;
2431
+ try {
2432
+ const r = (0, import_module.createRequire)(__sootsim_import_meta_url);
2433
+ const { dlopen, FFIType } = r("bun:ffi");
2434
+ const lib = dlopen("/usr/lib/libproc.dylib", {
2435
+ proc_pidinfo: {
2436
+ args: [FFIType.i32, FFIType.i32, FFIType.u64, FFIType.ptr, FFIType.i32],
2437
+ returns: FFIType.i32
2438
+ }
2439
+ });
2440
+ const buf = Buffer.alloc(PROC_BSDINFO_SIZE);
2441
+ return (pid) => {
2442
+ const ret = Number(
2443
+ lib.symbols.proc_pidinfo(pid, PROC_PIDTBSDINFO, 0n, buf, PROC_BSDINFO_SIZE)
2444
+ );
2445
+ if (ret <= 0) return null;
2446
+ const pgid = buf.readUInt32LE(PBI_PGID_OFFSET);
2447
+ return pgid > 0 ? pgid : null;
2448
+ };
2449
+ } catch {
2450
+ return null;
2451
+ }
2452
+ }
2393
2453
 
2394
2454
  // cli/secure-file.ts
2395
2455
  var import_node_crypto = require("node:crypto");
@@ -2484,6 +2544,15 @@ function getCliIdentity() {
2484
2544
  function getCliIdentityKey() {
2485
2545
  return getCliIdentity().key;
2486
2546
  }
2547
+ function getStableOwnerPid() {
2548
+ return findTopAncestor(process.ppid);
2549
+ }
2550
+ function getOwnerProcessGroupId() {
2551
+ const owner = getStableOwnerPid();
2552
+ if (!owner || owner <= 1) return null;
2553
+ const pgid = getProcessPgid(owner);
2554
+ return pgid !== null && pgid > 1 ? pgid : null;
2555
+ }
2487
2556
  function getCurrentSimPath() {
2488
2557
  if (process.env[CURRENT_SIM_PATH_ENV]) {
2489
2558
  return (0, import_path.resolve)(process.env[CURRENT_SIM_PATH_ENV]);
@@ -2604,7 +2673,11 @@ function createBridge(wsPort, opts = {}) {
2604
2673
  id: 0,
2605
2674
  cliIdentityKey: identity.key,
2606
2675
  cliIdentitySource: identity.source,
2607
- cliLabel: opts.cliLabel
2676
+ cliLabel: opts.cliLabel,
2677
+ // lets the bridge free this cli's leases once its owning
2678
+ // session is entirely gone, instead of blocking others
2679
+ // until the TTL burns out. null when ancestry is unreadable.
2680
+ cliOwnerPgid: getOwnerProcessGroupId()
2608
2681
  })
2609
2682
  );
2610
2683
  } catch {
@@ -1,4 +1,4 @@
1
- /*! rnx v0.1.709 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
1
+ /*! rnx v0.1.710 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
2
2
  let __sootsim_import_meta_url = ''; try { __sootsim_import_meta_url = require('url').pathToFileURL(__filename).href; } catch {}
3
3
  "use strict";
4
4
  var __defProp = Object.defineProperty;
@@ -1,4 +1,4 @@
1
- /*! rnx v0.1.709 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
1
+ /*! rnx v0.1.710 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
2
2
  let __sootsim_import_meta_url = ''; try { __sootsim_import_meta_url = require('url').pathToFileURL(__filename).href; } catch {}
3
3
  "use strict";
4
4
  var __create = Object.create;
@@ -1,4 +1,4 @@
1
- /*! rnx v0.1.709 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
1
+ /*! rnx v0.1.710 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
2
2
  let __sootsim_import_meta_url = ''; try { __sootsim_import_meta_url = require('url').pathToFileURL(__filename).href; } catch {}
3
3
  "use strict";
4
4
  var __create = Object.create;
@@ -4228,7 +4228,7 @@ function listMetroAssetDest(assetsPath) {
4228
4228
  return { directory, files, truncated };
4229
4229
  }
4230
4230
  function metroDescriptorHash(scaleBytes) {
4231
- const hasher = (0, import_node_crypto10.createHash)("md5");
4231
+ const hasher = (0, import_node_crypto11.createHash)("md5");
4232
4232
  for (const bytes of scaleBytes) hasher.update(bytes);
4233
4233
  return hasher.digest("hex");
4234
4234
  }
@@ -4257,7 +4257,7 @@ function distinctByContent(directory, options) {
4257
4257
  } catch {
4258
4258
  continue;
4259
4259
  }
4260
- const digest = (0, import_node_crypto10.createHash)("md5").update(bytes).digest("hex");
4260
+ const digest = (0, import_node_crypto11.createHash)("md5").update(bytes).digest("hex");
4261
4261
  if (!byContent.has(digest)) byContent.set(digest, relative2);
4262
4262
  }
4263
4263
  return [...byContent.values()];
@@ -4291,7 +4291,7 @@ function placeMetroDescriptor(listing, descriptor) {
4291
4291
  continue;
4292
4292
  }
4293
4293
  const identical = fileHashes ? bytes.every((file, index) => {
4294
- const digest = (0, import_node_crypto10.createHash)("md5").update(file).digest("hex");
4294
+ const digest = (0, import_node_crypto11.createHash)("md5").update(file).digest("hex");
4295
4295
  return descriptor.scales.some(
4296
4296
  (scale, declared) => scale === scales[index] && fileHashes[declared] === digest
4297
4297
  );
@@ -4526,7 +4526,7 @@ async function produceArtifact(bundlePath, maxBytes, command, assetPolicy) {
4526
4526
  }
4527
4527
  return {
4528
4528
  bytes,
4529
- sha256: (0, import_node_crypto10.createHash)("sha256").update(bytes).digest("hex"),
4529
+ sha256: (0, import_node_crypto11.createHash)("sha256").update(bytes).digest("hex"),
4530
4530
  receipt: artifact.receipt,
4531
4531
  assetsDirectory: prepared.dest?.directory ?? null,
4532
4532
  warnings
@@ -4631,11 +4631,11 @@ async function createCloudBox(options) {
4631
4631
  warnings: artifact.warnings
4632
4632
  };
4633
4633
  }
4634
- var import_node_crypto10, import_node_fs23, import_node_path23, DEFAULT_RNX_CLOUD_ORIGIN, MAX_RNX_CLOUD_BUNDLE_BYTES, REMOTE_COMMAND, MAX_ASSET_DEST_FILES, ACCOUNT_CHOICE_REFUSALS, PROJECT_ONLY_DIRECTORIES, MAX_PLACEMENT_COMBINATIONS;
4634
+ var import_node_crypto11, import_node_fs23, import_node_path23, DEFAULT_RNX_CLOUD_ORIGIN, MAX_RNX_CLOUD_BUNDLE_BYTES, REMOTE_COMMAND, MAX_ASSET_DEST_FILES, ACCOUNT_CHOICE_REFUSALS, PROJECT_ONLY_DIRECTORIES, MAX_PLACEMENT_COMBINATIONS;
4635
4635
  var init_cloud_client = __esm({
4636
4636
  "cli/cloud-client.ts"() {
4637
4637
  "use strict";
4638
- import_node_crypto10 = require("node:crypto");
4638
+ import_node_crypto11 = require("node:crypto");
4639
4639
  import_node_fs23 = require("node:fs");
4640
4640
  import_node_path23 = require("node:path");
4641
4641
  init_metroAssetArtifact();
@@ -6972,8 +6972,14 @@ function writeSwiftProject(layout, input, moduleRoot) {
6972
6972
  }
6973
6973
  ] : compileTargets(input);
6974
6974
  const files = /* @__PURE__ */ new Map();
6975
+ const modulePlan = (0, import_node_crypto3.createHash)("sha256").update(
6976
+ swiftModuleInputFiles(moduleRoot).join("\n")
6977
+ );
6978
+ if (!distribution) {
6979
+ modulePlan.update((0, import_node_fs7.readFileSync)(import_node_path7.default.join(moduleRoot, "Package.swift")));
6980
+ }
6975
6981
  const moduleFiles = `
6976
- // sootsim-swift files ${(0, import_node_crypto3.createHash)("sha256").update(swiftModuleInputFiles(moduleRoot).join("\n")).digest("hex").slice(0, 16)}
6982
+ // sootsim-swift files ${modulePlan.digest("hex").slice(0, 16)}
6977
6983
  `;
6978
6984
  if (targets.length === 0) {
6979
6985
  for (const [file, contents] of Object.entries(input.files)) {
@@ -8056,7 +8062,7 @@ var MAX_SWIFT_COMPILE_RESOURCE_TOTAL_BYTES = 8 * 1024 * 1024;
8056
8062
  var MAX_SWIFT_COMPILE_IMAGES = 128;
8057
8063
  var MAX_SWIFT_COMPILE_IMAGE_BYTES = 4 * 1024 * 1024;
8058
8064
  var MAX_SWIFT_COMPILE_IMAGE_TOTAL_BYTES = 16 * 1024 * 1024;
8059
- var SWIFT_HOST_ABI = 16;
8065
+ var SWIFT_HOST_ABI = 17;
8060
8066
  var POLL_INTERVAL_MS = 1500;
8061
8067
  function httpSwiftCompileSubmit(context) {
8062
8068
  const { assetReadToken, authorization, endpoint } = context;
@@ -10506,6 +10512,7 @@ function mapFrameworkToProjectFramework(fw) {
10506
10512
  // src/host/fetch-proxy-handler.ts
10507
10513
  var import_http2 = __toESM(require("http"), 1);
10508
10514
  var import_https = __toESM(require("https"), 1);
10515
+ var import_node_http2 = require("node:http2");
10509
10516
  var import_promises4 = require("stream/promises");
10510
10517
  init_backend_origin();
10511
10518
 
@@ -10584,7 +10591,7 @@ function buildFetchProxyUpstreamHeaders(requestHeaders, targetUrl) {
10584
10591
  for (const [rawKey, value] of requestHeaders) {
10585
10592
  const key = rawKey.toLowerCase();
10586
10593
  if (key === FETCH_PROXY_NATIVE_USER_AGENT_HEADER) nativeUserAgent = value;
10587
- if (STRIP_FETCH_PROXY_REQUEST_HEADERS.has(key) || key.startsWith("x-forwarded-")) {
10594
+ if (key.startsWith(":") || STRIP_FETCH_PROXY_REQUEST_HEADERS.has(key) || key.startsWith("x-forwarded-")) {
10588
10595
  continue;
10589
10596
  }
10590
10597
  headers[key] = value;
@@ -10613,10 +10620,7 @@ function fetchProxyAgentFor(targetUrl) {
10613
10620
  return targetUrl.protocol === "https:" ? fetchProxyHttpsAgent : fetchProxyHttpAgent;
10614
10621
  }
10615
10622
  var FETCH_PROXY_CORS_HEADERS = {
10616
- "access-control-allow-origin": "*",
10617
- "access-control-allow-methods": "GET,POST,PUT,DELETE,PATCH,OPTIONS",
10618
- "access-control-allow-headers": "*",
10619
- "access-control-expose-headers": "*",
10623
+ "access-control-allow-methods": "GET,HEAD,POST,PUT,DELETE,PATCH,OPTIONS",
10620
10624
  "access-control-max-age": "3600"
10621
10625
  };
10622
10626
  var APP_API_HEADER_REWRITES = /* @__PURE__ */ new Set([
@@ -10631,10 +10635,30 @@ function loopbackTlsOptions(targetUrl) {
10631
10635
  if (targetUrl.protocol !== "https:") return {};
10632
10636
  return isLoopbackHost(targetUrl.hostname) ? { rejectUnauthorized: false } : {};
10633
10637
  }
10634
- function applyFetchProxyCors(res) {
10638
+ function applyFetchProxyCors(req, res) {
10635
10639
  for (const [key, value] of Object.entries(FETCH_PROXY_CORS_HEADERS)) {
10636
10640
  res.setHeader(key, value);
10637
10641
  }
10642
+ res.setHeader("access-control-allow-origin", req.headers.origin ?? "*");
10643
+ res.setHeader("access-control-allow-credentials", "true");
10644
+ res.setHeader(
10645
+ "access-control-allow-headers",
10646
+ req.headers["access-control-request-headers"] ?? "*"
10647
+ );
10648
+ res.setHeader(
10649
+ "access-control-expose-headers",
10650
+ [
10651
+ ...Object.keys(res.getHeaders()).filter(
10652
+ (name2) => !name2.startsWith("access-control-")
10653
+ ),
10654
+ "x-sootsim-set-cookie"
10655
+ ].join(", ")
10656
+ );
10657
+ const vary = res.getHeader("vary");
10658
+ res.setHeader(
10659
+ "vary",
10660
+ vary ? `${Array.isArray(vary) ? vary.join(", ") : vary}, Origin` : "Origin"
10661
+ );
10638
10662
  }
10639
10663
  function buildFetchProxyHeaders(reqHeaders, targetUrl) {
10640
10664
  return buildFetchProxyUpstreamHeaders(nodeRequestHeaderEntries(reqHeaders), targetUrl);
@@ -10656,6 +10680,9 @@ function buildAppApiProxyHeaders(reqHeaders, targetUrl) {
10656
10680
  function isFetchProxyRequestUrl(rawUrl) {
10657
10681
  return rawUrl?.startsWith("/__fetch-proxy?") || rawUrl?.startsWith("/__proxy?") || false;
10658
10682
  }
10683
+ function isBundleProxyRequestUrl(rawUrl) {
10684
+ return rawUrl?.startsWith("/__bundle-proxy?") ?? false;
10685
+ }
10659
10686
  function isAppApiRequestUrl(rawUrl) {
10660
10687
  if (!rawUrl) return false;
10661
10688
  if (rawUrl.startsWith("/__app-api?")) return true;
@@ -10664,16 +10691,17 @@ function isAppApiRequestUrl(rawUrl) {
10664
10691
  }
10665
10692
  async function handleFetchProxyRequest(req, res) {
10666
10693
  if (req.method === "OPTIONS") {
10667
- applyFetchProxyCors(res);
10668
- res.writeHead(204);
10694
+ applyFetchProxyCors(req, res);
10695
+ res.statusCode = 204;
10669
10696
  res.end();
10670
10697
  return;
10671
10698
  }
10672
10699
  const params = new URLSearchParams((req.url || "").split("?")[1] || "");
10673
10700
  const targetUrl = params.get("url");
10674
10701
  if (!targetUrl) {
10675
- applyFetchProxyCors(res);
10676
- res.writeHead(400, { "Content-Type": "text/plain" });
10702
+ applyFetchProxyCors(req, res);
10703
+ res.statusCode = 400;
10704
+ res.setHeader("Content-Type", "text/plain");
10677
10705
  res.end("missing url param");
10678
10706
  return;
10679
10707
  }
@@ -10681,8 +10709,9 @@ async function handleFetchProxyRequest(req, res) {
10681
10709
  try {
10682
10710
  upstreamUrl = resolveFetchProxyTargetUrl(new URL(targetUrl));
10683
10711
  } catch {
10684
- applyFetchProxyCors(res);
10685
- res.writeHead(400, { "Content-Type": "text/plain" });
10712
+ applyFetchProxyCors(req, res);
10713
+ res.statusCode = 400;
10714
+ res.setHeader("Content-Type", "text/plain");
10686
10715
  res.end("invalid url param");
10687
10716
  return;
10688
10717
  }
@@ -10746,7 +10775,7 @@ async function handleFetchProxyRequest(req, res) {
10746
10775
  }
10747
10776
  res.setHeader(key, value);
10748
10777
  }
10749
- applyFetchProxyCors(res);
10778
+ applyFetchProxyCors(req, res);
10750
10779
  const setCookie = proxyRes.headers["set-cookie"] ?? [];
10751
10780
  if (setCookie.length > 0) {
10752
10781
  res.setHeader(
@@ -10783,15 +10812,17 @@ async function handleFetchProxyRequest(req, res) {
10783
10812
  }
10784
10813
  }
10785
10814
  }
10786
- req.socket.destroy();
10815
+ if (req instanceof import_node_http2.Http2ServerRequest) req.stream.destroy();
10816
+ else req.socket.destroy();
10787
10817
  }
10788
10818
  }
10789
10819
  async function handleBundleProxyRequest(req, res) {
10790
10820
  const params = new URLSearchParams((req.url || "").split("?")[1] || "");
10791
10821
  const target = params.get("url");
10792
10822
  if (!target) {
10793
- applyFetchProxyCors(res);
10794
- res.writeHead(400, { "Content-Type": "text/plain" });
10823
+ applyFetchProxyCors(req, res);
10824
+ res.statusCode = 400;
10825
+ res.setHeader("Content-Type", "text/plain");
10795
10826
  res.end("bundle-proxy: missing url query param");
10796
10827
  return;
10797
10828
  }
@@ -10799,14 +10830,16 @@ async function handleBundleProxyRequest(req, res) {
10799
10830
  try {
10800
10831
  targetUrl = new URL(target);
10801
10832
  } catch {
10802
- applyFetchProxyCors(res);
10803
- res.writeHead(400, { "Content-Type": "text/plain" });
10833
+ applyFetchProxyCors(req, res);
10834
+ res.statusCode = 400;
10835
+ res.setHeader("Content-Type", "text/plain");
10804
10836
  res.end("bundle-proxy: invalid url");
10805
10837
  return;
10806
10838
  }
10807
10839
  if (!isLoopbackHost(targetUrl.hostname)) {
10808
- applyFetchProxyCors(res);
10809
- res.writeHead(403, { "Content-Type": "text/plain" });
10840
+ applyFetchProxyCors(req, res);
10841
+ res.statusCode = 403;
10842
+ res.setHeader("Content-Type", "text/plain");
10810
10843
  res.end("bundle-proxy: only loopback targets allowed");
10811
10844
  return;
10812
10845
  }
@@ -10880,6 +10913,75 @@ function handleAppApiRequest(req, res) {
10880
10913
  return true;
10881
10914
  }
10882
10915
 
10916
+ // src/host/fetch-proxy-transport.ts
10917
+ var import_node_crypto10 = require("node:crypto");
10918
+ var import_node_http22 = require("node:http2");
10919
+ var import_x509 = require("@peculiar/x509");
10920
+ async function startFetchProxyTransport() {
10921
+ const algorithm = { name: "ECDSA", namedCurve: "P-256", hash: "SHA-256" };
10922
+ const keys = await crypto.subtle.generateKey(algorithm, true, ["sign", "verify"]);
10923
+ const certificate = await import_x509.X509CertificateGenerator.createSelfSigned(
10924
+ {
10925
+ serialNumber: (0, import_node_crypto10.randomBytes)(16).toString("hex"),
10926
+ name: "CN=localhost",
10927
+ notBefore: new Date(Date.now() - 6e4),
10928
+ notAfter: new Date(Date.now() + 365 * 24 * 60 * 6e4),
10929
+ signingAlgorithm: algorithm,
10930
+ keys,
10931
+ extensions: [
10932
+ new import_x509.SubjectAlternativeNameExtension([{ type: "ip", value: "127.0.0.1" }])
10933
+ ]
10934
+ },
10935
+ crypto
10936
+ );
10937
+ const privateKey = Buffer.from(await crypto.subtle.exportKey("pkcs8", keys.privateKey));
10938
+ const spki = (0, import_node_crypto10.createHash)("sha256").update(Buffer.from(await crypto.subtle.exportKey("spki", keys.publicKey))).digest("base64");
10939
+ const sessions = /* @__PURE__ */ new Set();
10940
+ const server = (0, import_node_http22.createSecureServer)(
10941
+ {
10942
+ cert: certificate.toString("pem"),
10943
+ key: `-----BEGIN PRIVATE KEY-----
10944
+ ${privateKey.toString("base64").match(/.{1,64}/g)?.join("\n")}
10945
+ -----END PRIVATE KEY-----
10946
+ `,
10947
+ settings: { maxConcurrentStreams: 256 }
10948
+ },
10949
+ (req, res) => {
10950
+ res.setHeader("Cross-Origin-Resource-Policy", "cross-origin");
10951
+ res.setHeader("Timing-Allow-Origin", "*");
10952
+ const handler = isBundleProxyRequestUrl(req.url) ? handleBundleProxyRequest : isFetchProxyRequestUrl(req.url) ? handleFetchProxyRequest : null;
10953
+ if (!handler) {
10954
+ res.writeHead(404);
10955
+ res.end();
10956
+ return;
10957
+ }
10958
+ void handler(req, res).catch(() => res.destroy());
10959
+ }
10960
+ );
10961
+ server.on("session", (session) => {
10962
+ sessions.add(session);
10963
+ session.once("close", () => sessions.delete(session));
10964
+ session.on("error", () => session.destroy());
10965
+ });
10966
+ await new Promise((resolve5, reject) => {
10967
+ server.once("error", reject);
10968
+ server.listen(0, "127.0.0.1", () => {
10969
+ server.off("error", reject);
10970
+ resolve5();
10971
+ });
10972
+ });
10973
+ const address = server.address();
10974
+ if (!address || typeof address === "string")
10975
+ throw new Error("fetch proxy has no address");
10976
+ return {
10977
+ descriptor: { origin: `https://127.0.0.1:${address.port}`, spki },
10978
+ async close() {
10979
+ for (const session of sessions) session.destroy();
10980
+ await new Promise((resolve5) => server.close(() => resolve5()));
10981
+ }
10982
+ };
10983
+ }
10984
+
10883
10985
  // src/host/open-url.ts
10884
10986
  var import_child_process2 = require("child_process");
10885
10987
  function buildOpenUrlCommand(url, options = {}) {
@@ -12029,6 +12131,16 @@ function handleWebSocketProxyUpgrade(req, socket, head, forwardOrigin = true) {
12029
12131
 
12030
12132
  // src/host/bridge-host.ts
12031
12133
  var SOOTSIM_CROSS_ORIGIN_EMBEDDER_POLICY = "require-corp";
12134
+ function isProcessGroupAlive(pgid) {
12135
+ if (!Number.isInteger(pgid) || pgid <= 1) return true;
12136
+ try {
12137
+ process.kill(-pgid, 0);
12138
+ return true;
12139
+ } catch (error) {
12140
+ const code = error && typeof error === "object" ? Reflect.get(error, "code") : null;
12141
+ return code !== "ESRCH";
12142
+ }
12143
+ }
12032
12144
  var WRITE_COMMAND_TYPES = /* @__PURE__ */ new Set([
12033
12145
  "tap",
12034
12146
  "keyboard",
@@ -12105,6 +12217,7 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
12105
12217
  openUrlHandler;
12106
12218
  httpServer = null;
12107
12219
  wss = null;
12220
+ fetchProxyTransport = null;
12108
12221
  nextCommandId = 1;
12109
12222
  nextSimNumber = 161;
12110
12223
  sims = /* @__PURE__ */ new Map();
@@ -12115,6 +12228,7 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
12115
12228
  cliLastCommandAt = /* @__PURE__ */ new Map();
12116
12229
  cliIdentityKeyBySocket = /* @__PURE__ */ new Map();
12117
12230
  cliLabelBySocket = /* @__PURE__ */ new Map();
12231
+ cliOwnerPgidBySocket = /* @__PURE__ */ new Map();
12118
12232
  restorableSims = /* @__PURE__ */ new Map();
12119
12233
  nextCliFallbackId = 1;
12120
12234
  cliIdleTimer = null;
@@ -12214,6 +12328,12 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
12214
12328
  `
12215
12329
  );
12216
12330
  }
12331
+ try {
12332
+ this.fetchProxyTransport = await startFetchProxyTransport();
12333
+ } catch (error) {
12334
+ await this.close();
12335
+ throw error;
12336
+ }
12217
12337
  this.afterBind();
12218
12338
  return boundPort;
12219
12339
  } catch (err) {
@@ -12508,6 +12628,7 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
12508
12628
  this.cliLastCommandAt.delete(ws);
12509
12629
  this.cliIdentityKeyBySocket.delete(ws);
12510
12630
  this.cliLabelBySocket.delete(ws);
12631
+ this.cliOwnerPgidBySocket.delete(ws);
12511
12632
  for (const [sentId2, entry] of this.cliBySentId) {
12512
12633
  if (entry.ws === ws) this.cliBySentId.delete(sentId2);
12513
12634
  }
@@ -12520,6 +12641,11 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
12520
12641
  if (typeof msg.cliLabel === "string" && msg.cliLabel.trim()) {
12521
12642
  this.cliLabelBySocket.set(ws, msg.cliLabel.trim());
12522
12643
  }
12644
+ if (typeof msg.cliOwnerPgid === "number" && Number.isInteger(msg.cliOwnerPgid) && msg.cliOwnerPgid > 1) {
12645
+ this.cliOwnerPgidBySocket.set(ws, msg.cliOwnerPgid);
12646
+ } else {
12647
+ this.cliOwnerPgidBySocket.delete(ws);
12648
+ }
12523
12649
  if (ws.readyState === import_ws.WebSocket.OPEN) {
12524
12650
  ws.send(
12525
12651
  JSON.stringify({
@@ -12670,6 +12796,7 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
12670
12796
  this.cliLastCommandAt.delete(ws);
12671
12797
  this.cliIdentityKeyBySocket.delete(ws);
12672
12798
  this.cliLabelBySocket.delete(ws);
12799
+ this.cliOwnerPgidBySocket.delete(ws);
12673
12800
  for (const [sentId, entry] of this.cliBySentId) {
12674
12801
  if (entry.ws === ws) this.cliBySentId.delete(sentId);
12675
12802
  }
@@ -13029,6 +13156,14 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
13029
13156
  * non-upgrade routes that don't match serve index.html (SPA behavior) so
13030
13157
  * electron's webContents can navigate freely inside the runtime. */
13031
13158
  handleHttpRequest(req, res) {
13159
+ if (req.url === "/__peach/fetch-transport") {
13160
+ res.writeHead(200, {
13161
+ "Content-Type": "application/json",
13162
+ "Cache-Control": "no-store"
13163
+ });
13164
+ res.end(JSON.stringify(this.fetchProxyTransport?.descriptor));
13165
+ return;
13166
+ }
13032
13167
  res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
13033
13168
  res.setHeader("Cross-Origin-Embedder-Policy", SOOTSIM_CROSS_ORIGIN_EMBEDDER_POLICY);
13034
13169
  res.setHeader("Cross-Origin-Resource-Policy", "cross-origin");
@@ -13591,6 +13726,8 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
13591
13726
  } catch {
13592
13727
  }
13593
13728
  }
13729
+ await this.fetchProxyTransport?.close();
13730
+ this.fetchProxyTransport = null;
13594
13731
  this.effectivePort = 0;
13595
13732
  this.startedAt = 0;
13596
13733
  this.agentHost.close();
@@ -13655,6 +13792,10 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
13655
13792
  sim.cliLease = void 0;
13656
13793
  return null;
13657
13794
  }
13795
+ if (lease.kind === "cli" && lease.ownerPgid !== void 0 && !isProcessGroupAlive(lease.ownerPgid)) {
13796
+ sim.cliLease = void 0;
13797
+ return null;
13798
+ }
13658
13799
  return lease;
13659
13800
  }
13660
13801
  tryAcquireLease(ws, sim, opts = {}) {
@@ -13697,7 +13838,8 @@ var SootSimBridgeHost = class _SootSimBridgeHost {
13697
13838
  kind: "cli",
13698
13839
  cliIdentityKey,
13699
13840
  cliLabel,
13700
- expiresAt: now + _SootSimBridgeHost.CLI_LEASE_TTL_MS
13841
+ expiresAt: now + _SootSimBridgeHost.CLI_LEASE_TTL_MS,
13842
+ ownerPgid: this.cliOwnerPgidBySocket.get(ws)
13701
13843
  };
13702
13844
  sim.cliLease = lease;
13703
13845
  return { granted: true, lease, bootedCount };