@phren/cli 0.3.57 → 0.3.58

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.
@@ -5,8 +5,12 @@ import path from "node:path";
5
5
  import { createHash, randomUUID } from "node:crypto";
6
6
  import { promisify } from "node:util";
7
7
  import { BridgeError, bridgeRoot } from "./protocol.js";
8
- const exec = promisify(execFile);
9
- const UDID = /^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/i;
8
+ const execFileAsync = promisify(execFile);
9
+ /** Every simulator child is killed outright at its timeout. SIGTERM, the
10
+ * default, let one that ignored it hold its promise, and with it a stream's
11
+ * slot, for good. */
12
+ const exec = (file, args, options) => execFileAsync(file, args, { ...options, killSignal: "SIGKILL", encoding: "utf8" });
13
+ export const SIMULATOR_UDID = /^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/i;
10
14
  /** The iOS simulators booted on this Mac, from `simctl`; none elsewhere. */
11
15
  export async function bootedSimulators() {
12
16
  if (process.platform !== "darwin")
@@ -24,7 +28,7 @@ export async function bootedSimulators() {
24
28
  // com.apple.CoreSimulator.SimRuntime.iOS-26-1 → iOS 26.1
25
29
  const runtime = runtimeID.replace(/^.*SimRuntime\./, "").replace(/-(\d+)-(\d+)$/, " $1.$2").replace(/-/g, " ");
26
30
  for (const device of devices ?? []) {
27
- if (device.state !== "Booted" || !device.udid || !UDID.test(device.udid))
31
+ if (device.state !== "Booted" || !device.udid || !SIMULATOR_UDID.test(device.udid))
28
32
  continue;
29
33
  list.push({ udid: device.udid.toUpperCase(), name: String(device.name ?? "Simulator").slice(0, 120), runtime });
30
34
  if (list.length >= 16)
@@ -35,7 +39,7 @@ export async function bootedSimulators() {
35
39
  }
36
40
  /** A PNG of one booted simulator's screen. */
37
41
  export async function simulatorScreenshot(udid) {
38
- if (!UDID.test(udid))
42
+ if (!SIMULATOR_UDID.test(udid))
39
43
  throw new BridgeError(400, "Invalid simulator identifier.");
40
44
  if (process.platform !== "darwin")
41
45
  throw new BridgeError(404, "Simulators run on macOS only.");
@@ -53,22 +57,24 @@ export async function simulatorScreenshot(udid) {
53
57
  await unlink(file).catch(() => undefined);
54
58
  }
55
59
  }
56
- /** The native helper (native/simtap.swift) that finds the Simulator window
57
- * through the Accessibility API and posts touches and keys with CGEvent.
58
- * AppleScript would need Automation consent, which a launchd agent can never
59
- * be prompted for; the helper needs one Accessibility grant. Its `prompt`
60
- * command puts it in the Accessibility list and shows the system dialog.
61
- * Compiled once per Hook version with swiftc, which any Mac with the
62
- * simulators has. */
63
- let building;
64
- function helper() {
65
- building ??= buildHelper().finally(() => { building = undefined; });
66
- return building;
60
+ const HELPER_LABEL = { simtap: "simulator input helper", simcast: "simulator stream helper" };
61
+ const building = new Map();
62
+ function helper(name) {
63
+ let build = building.get(name);
64
+ if (!build) {
65
+ build = buildHelper(name).finally(() => building.delete(name));
66
+ building.set(name, build);
67
+ }
68
+ return build;
69
+ }
70
+ async function helperSource(name) {
71
+ const bundled = name === "simtap" ? (typeof SIMTAP_SOURCE === "string" ? SIMTAP_SOURCE : undefined) : (typeof SIMCAST_SOURCE === "string" ? SIMCAST_SOURCE : undefined);
72
+ return bundled ?? await readFile(new URL(`./native/${name}.swift`, import.meta.url), "utf8");
67
73
  }
68
- async function buildHelper() {
74
+ async function buildHelper(name) {
69
75
  const dir = path.join(bridgeRoot(), "native");
70
- const binary = path.join(dir, "simtap");
71
- const code = typeof SIMTAP_SOURCE === "string" ? SIMTAP_SOURCE : await readFile(new URL("./native/simtap.swift", import.meta.url), "utf8");
76
+ const binary = path.join(dir, name);
77
+ const code = await helperSource(name);
72
78
  const digest = createHash("sha256").update(code).digest("hex");
73
79
  // Rebuilt only when the source changes: the Accessibility grant is tied
74
80
  // to this binary, and a needless rebuild would make macOS ask again.
@@ -76,12 +82,12 @@ async function buildHelper() {
76
82
  if (await stat(binary).catch(() => undefined) && saved?.sourceSha === digest)
77
83
  return binary;
78
84
  await mkdir(dir, { recursive: true, mode: 0o700 });
79
- const temporary = path.join(dir, `simtap.${randomUUID()}`), source = temporary + ".swift";
85
+ const temporary = path.join(dir, `${name}.${randomUUID()}`), source = temporary + ".swift";
80
86
  const sidecar = temporary + ".sha256";
81
87
  try {
82
88
  await writeFile(source, code, { mode: 0o600, flag: "wx" });
83
89
  await exec("/usr/bin/swiftc", ["-O", "-o", temporary, source], { timeout: 180_000 }).catch(() => {
84
- throw new BridgeError(409, "Couldn't build the simulator input helper (needs Xcode's swiftc).");
90
+ throw new BridgeError(409, `Couldn't build the ${HELPER_LABEL[name]} (needs Xcode's swiftc).`);
85
91
  });
86
92
  await chmod(temporary, 0o700);
87
93
  const binarySha = createHash("sha256").update(await readFile(temporary)).digest("hex");
@@ -100,13 +106,13 @@ function simtap(name, ...args) {
100
106
  inputQueue = task;
101
107
  return task;
102
108
  }
103
- /** The helper, hashed again right before it runs. */
104
- async function verifiedHelper() {
105
- const binary = await helper();
109
+ /** A helper, hashed again right before it runs. */
110
+ export async function verifiedHelper(name = "simtap") {
111
+ const binary = await helper(name);
106
112
  const saved = await readFile(binary + ".sha256", "utf8").then(JSON.parse).catch(() => undefined);
107
113
  const binarySha = createHash("sha256").update(await readFile(binary)).digest("hex");
108
114
  if (!saved || saved.binarySha !== binarySha)
109
- throw new BridgeError(409, "Simulator input helper integrity check failed.");
115
+ throw new BridgeError(409, `${HELPER_LABEL[name][0].toUpperCase()}${HELPER_LABEL[name].slice(1)} integrity check failed.`);
110
116
  return binary;
111
117
  }
112
118
  async function executeSimtap(name, ...args) {
@@ -148,7 +154,7 @@ const ACCESSIBILITY_PANE = "x-apple.systempreferences:com.apple.preference.secur
148
154
  // macOS virtual key codes.
149
155
  const KEY_H = "4", KEY_L = "37";
150
156
  export async function simulatorAct(udid, request) {
151
- if (!UDID.test(udid))
157
+ if (!SIMULATOR_UDID.test(udid))
152
158
  throw new BridgeError(400, "Invalid simulator identifier.");
153
159
  if (!["boot", "shutdown", "launch", "openurl", "home", "lock", "type", "tap", "screenshot-ready", "accessibility-settings"].includes(request.action))
154
160
  throw new BridgeError(400, "Unknown simulator action.");
@@ -216,7 +222,7 @@ async function deviceName(udid) {
216
222
  }
217
223
  /** The apps a simulator has, for the launcher. */
218
224
  export async function simulatorApps(udid) {
219
- if (!UDID.test(udid))
225
+ if (!SIMULATOR_UDID.test(udid))
220
226
  throw new BridgeError(400, "Invalid simulator identifier.");
221
227
  if (process.platform !== "darwin")
222
228
  return [];