1// Windows region capture. Runs in a forked Node-mode child so the native
2// screenshot call cannot crash or stall the main process. One request per child.
3import type { RegionSnapShotRequest, RegionSnapShotResult } from "./RegionSnapShot.ts";
4
5process.once("disconnect", () => process.exit(0));
6
7async function capture() {
8 const imported = await import("@crowecawcaw/xa11y");
9 const xa11y = (imported as unknown as { readonly default?: typeof imported }).default ?? imported;
10 process.send?.("ready");
11 const request = await new Promise<RegionSnapShotRequest>((resolve) => {
12 process.once("message", resolve);
13 });
14 const shot = await xa11y.screenshot({ region: request.region });
15 const result: RegionSnapShotResult = {
16 type: "result",
17 width: shot.width,
18 height: shot.height,
19 png: shot.toPng().toString("base64"),
20 };
21 process.send?.(result);
22}
23
24void capture().catch((error: unknown) =>
25 process.send?.({
26 type: "error",
27 message: error instanceof Error ? error.message : "Windows window capture failed.",
28 } satisfies RegionSnapShotResult),
29);