@gtkx/vitest 1.4.0 → 1.5.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.
package/README.md CHANGED
@@ -98,7 +98,7 @@ GTK4 is mature, and GtkBuilder XML can lay out a static interface, but nothing r
98
98
  - a React reconciler that exposes every GObject as a JSX element,
99
99
  - a CLI for scaffolding, development, and production builds,
100
100
  - a dev server with Fast Refresh that patches your running UI in place,
101
- - CSS-in-JS styling, React Spring animations, React Navigation stack, tab, drawer, and split view navigators, and high-level list, grid, and dialog components,
101
+ - CSS-in-JS styling, React Hook Form-powered Adwaita controls, gettext-backed react-i18next localization, React Spring animations, React Navigation stack, tab, drawer, and split view navigators, and high-level list, grid, and dialog components,
102
102
  - a Testing Library-style API for querying and driving your widgets in tests,
103
103
  - and a Model Context Protocol (MCP) server that exposes your live app to AI agents.
104
104
 
@@ -1 +1 @@
1
- {"version":3,"file":"headless-display.d.ts","sourceRoot":"","sources":["../src/headless-display.ts"],"names":[],"mappings":"AASA;;GAEG;AACH,KAAK,YAAY,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEtC;;GAEG;AACH,KAAK,eAAe,GAAG;IACnB,4EAA4E;IAC5E,IAAI,EAAE,MAAM,CAAC;IACb,4DAA4D;IAC5D,UAAU,EAAE,YAAY,CAAC;CAC5B,CAAC;AA4GF,QAAA,MAAM,mBAAmB;;;;;;;;;;;CAWxB,CAAC;AAEF,QAAA,MAAM,sBAAsB,GAAI,UAAU,OAAO,CAAC,eAAe,CAAC,KAAG,eAGnE,CAAC;AAqCH,QAAA,MAAM,mBAAmB,GAAI,QAAQ,eAAe,KAAG,OAAO,CAAC,eAAe,CAe7E,CAAC;AA4OF,QAAA,MAAM,oBAAoB,GAAU,SAAS,eAAe,KAAG,OAAO,CAAC,MAAM,IAAI,CAiDhF,CAAC;AAEF,OAAO,EACH,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,EACnB,oBAAoB,EACpB,KAAK,YAAY,EACjB,KAAK,eAAe,GACvB,CAAC"}
1
+ {"version":3,"file":"headless-display.d.ts","sourceRoot":"","sources":["../src/headless-display.ts"],"names":[],"mappings":"AASA;;GAEG;AACH,KAAK,YAAY,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEtC;;GAEG;AACH,KAAK,eAAe,GAAG;IACnB,4EAA4E;IAC5E,IAAI,EAAE,MAAM,CAAC;IACb,4DAA4D;IAC5D,UAAU,EAAE,YAAY,CAAC;CAC5B,CAAC;AAuGF,QAAA,MAAM,mBAAmB;;;;;;;;;;;CAWxB,CAAC;AAEF,QAAA,MAAM,sBAAsB,GAAI,UAAU,OAAO,CAAC,eAAe,CAAC,KAAG,eAGnE,CAAC;AAqCH,QAAA,MAAM,mBAAmB,GAAI,QAAQ,eAAe,KAAG,OAAO,CAAC,eAAe,CAe7E,CAAC;AAsOF,QAAA,MAAM,oBAAoB,GAAU,SAAS,eAAe,KAAG,OAAO,CAAC,MAAM,IAAI,CAmDhF,CAAC;AAEF,OAAO,EACH,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,EACnB,oBAAoB,EACpB,KAAK,YAAY,EACjB,KAAK,eAAe,GACvB,CAAC"}
@@ -135,17 +135,42 @@ const writeBusConfig = (busConfigPath, busSocketPath) => {
135
135
  "</busconfig>",
136
136
  ].join("\n"));
137
137
  };
138
- const captureStderr = (child) => {
139
- const stderr = child?.stderr ?? null;
138
+ const exitedMessage = (label, path, code, signal) => `${label} exited (code ${String(code)}, signal ${signal ?? "null"}) before ${path} appeared`;
139
+ const monitorChild = (child, label, path) => {
140
+ const stderr = child.stderr;
141
+ const subscribers = new Set();
140
142
  let log = "";
141
- stderr?.setEncoding("utf8");
142
- stderr?.on("data", (chunk) => {
143
+ let failure;
144
+ const onData = (chunk) => {
143
145
  log += chunk;
146
+ };
147
+ stderr?.setEncoding("utf8");
148
+ stderr?.on("data", onData);
149
+ const record = (cause) => {
150
+ failure ??= cause;
151
+ for (const notify of subscribers) {
152
+ notify(cause);
153
+ }
154
+ };
155
+ child.on("close", (code, signal) => {
156
+ record(`${exitedMessage(label, path, code, signal)}\n${log}`);
157
+ });
158
+ child.on("error", (cause) => {
159
+ record(`${label} failed to spawn before ${path} appeared: ${cause.message}\n${log}`);
144
160
  });
145
161
  return {
162
+ label,
163
+ path,
146
164
  read: () => log,
165
+ failure: () => failure,
166
+ subscribe: (notify) => {
167
+ subscribers.add(notify);
168
+ return () => {
169
+ subscribers.delete(notify);
170
+ };
171
+ },
147
172
  stop: () => {
148
- stderr?.removeAllListeners("data");
173
+ stderr?.removeListener("data", onData);
149
174
  stderr?.resume();
150
175
  if (stderr instanceof Socket) {
151
176
  stderr.unref();
@@ -153,15 +178,6 @@ const captureStderr = (child) => {
153
178
  },
154
179
  };
155
180
  };
156
- const trackChild = (child, handlers) => {
157
- child.on("close", handlers.exit);
158
- child.on("error", handlers.error);
159
- return () => {
160
- child.removeListener("close", handlers.exit);
161
- child.removeListener("error", handlers.error);
162
- };
163
- };
164
- const exitedMessage = (label, path, code, signal) => `${label} exited (code ${String(code)}, signal ${signal ?? "null"}) before ${path} appeared`;
165
181
  const runCleanups = (cleanups) => {
166
182
  for (const cleanup of cleanups) {
167
183
  cleanup();
@@ -173,60 +189,48 @@ const pollForPath = (path, onFound) => setInterval(() => {
173
189
  onFound();
174
190
  }
175
191
  }, 50);
176
- const stderrSuffix = (child, stderr) => child ? `\n${stderr.read()}` : "";
177
- const hasAlreadyAborted = (signal, onAbort, cleanups) => {
178
- if (signal === undefined) {
179
- return false;
180
- }
181
- if (signal.aborted) {
182
- return true;
192
+ const firstFailure = (monitors) => {
193
+ for (const monitor of monitors) {
194
+ const failure = monitor.failure();
195
+ if (failure !== undefined) {
196
+ return failure;
197
+ }
183
198
  }
184
- signal.addEventListener("abort", onAbort);
185
- cleanups.push(() => {
186
- signal.removeEventListener("abort", onAbort);
187
- });
188
- return false;
199
+ return undefined;
189
200
  };
190
- const watchForSocket = ({ path, options, resolve, reject }) => {
191
- const { label, timeout = 15_000, child, signal } = options;
192
- const stderr = captureStderr(child);
193
- const cleanups = [stderr.stop];
201
+ const watchForSocket = ({ options, resolve, reject }) => {
202
+ const { monitor, guards = [], timeout = 15_000 } = options;
203
+ const watched = [monitor, ...guards];
204
+ const cleanups = [monitor.stop];
194
205
  const stop = () => {
195
206
  runCleanups(cleanups);
196
207
  };
197
208
  const fail = (message) => {
198
209
  stop();
210
+ for (const guard of guards) {
211
+ guard.stop();
212
+ }
199
213
  reject(new Error(message));
200
214
  };
201
- const onExit = (code, terminationSignal) => {
202
- fail(`${exitedMessage(label, path, code, terminationSignal)}\n${stderr.read()}`);
203
- };
204
- const onError = (cause) => {
205
- fail(`${label} failed to spawn: ${cause.message}\n${stderr.read()}`);
206
- };
207
- const onAbort = () => {
208
- fail(`${label} startup aborted before ${path} appeared`);
209
- };
210
- const poll = pollForPath(path, () => {
215
+ const alreadyFailed = firstFailure(watched);
216
+ if (alreadyFailed !== undefined) {
217
+ fail(alreadyFailed);
218
+ return;
219
+ }
220
+ const poll = pollForPath(monitor.path, () => {
211
221
  stop();
212
222
  resolve();
213
223
  });
214
224
  const timer = setTimeout(() => {
215
- fail(`${label} did not become available within ${String(timeout)}ms${stderrSuffix(child, stderr)}`);
225
+ fail(`${monitor.label} did not become available within ${String(timeout)}ms\n${monitor.read()}`);
216
226
  }, timeout);
217
227
  cleanups.push(() => {
218
228
  clearInterval(poll);
219
229
  clearTimeout(timer);
220
- });
221
- if (child) {
222
- cleanups.push(trackChild(child, { exit: onExit, error: onError }));
223
- }
224
- if (hasAlreadyAborted(signal, onAbort, cleanups)) {
225
- onAbort();
226
- }
230
+ }, ...watched.map((entry) => entry.subscribe(fail)));
227
231
  };
228
- const waitForSocket = (path, options) => new Promise((resolve, reject) => {
229
- watchForSocket({ path, options, resolve, reject });
232
+ const waitForSocket = (options) => new Promise((resolve, reject) => {
233
+ watchForSocket({ options, resolve, reject });
230
234
  });
231
235
  const captureCompositorStderr = (child, logPath) => {
232
236
  const captured = [];
@@ -251,22 +255,9 @@ const watchCompositorExit = (child, capturedStderr) => {
251
255
  child.on("exit", report);
252
256
  return () => child.removeListener("exit", report);
253
257
  };
254
- const waitForDisplaySockets = async (sockets) => {
255
- const { compositor, compositorSocketPath, busChild, busSocketPath } = sockets;
256
- const abort = new AbortController();
257
- try {
258
- await Promise.all([
259
- waitForSocket(compositorSocketPath, {
260
- label: "Compositor",
261
- child: compositor.child,
262
- signal: abort.signal,
263
- }),
264
- waitForSocket(busSocketPath, { label: "D-Bus session bus", child: busChild, signal: abort.signal }),
265
- ]);
266
- }
267
- finally {
268
- abort.abort();
269
- }
258
+ const waitForDisplaySockets = async ({ compositorMonitor, busMonitor }) => {
259
+ await waitForSocket({ monitor: busMonitor, guards: [compositorMonitor] });
260
+ await waitForSocket({ monitor: compositorMonitor });
270
261
  };
271
262
  const killSpawned = (children) => {
272
263
  for (const child of children) {
@@ -302,13 +293,15 @@ const startHeadlessDisplay = async (options) => {
302
293
  });
303
294
  busChild.unref();
304
295
  spawned.push(busChild);
296
+ const busMonitor = monitorChild(busChild, "D-Bus session bus", busSocketPath);
305
297
  applyEnv(env, { DBUS_SESSION_BUS_ADDRESS: `unix:path=${busSocketPath}` });
306
298
  const compositor = startCompositor(runtimeDir, options, env);
307
299
  compositor.child.unref();
308
300
  spawned.push(compositor.child);
309
- applyEnv(env, { WAYLAND_DISPLAY: compositor.socket });
310
301
  const compositorSocketPath = join(runtimeDir, compositor.socket);
311
- await waitForDisplaySockets({ compositor, compositorSocketPath, busChild, busSocketPath });
302
+ const compositorMonitor = monitorChild(compositor.child, "Compositor", compositorSocketPath);
303
+ applyEnv(env, { WAYLAND_DISPLAY: compositor.socket });
304
+ await waitForDisplaySockets({ compositorMonitor, busMonitor });
312
305
  const stopVirtualSeat = await attachVirtualSeat(compositor, compositorSocketPath);
313
306
  const stopNotifications = await startNotificationService(`unix:path=${busSocketPath}`);
314
307
  const capturedStderr = captureCompositorStderr(compositor.child, join(runtimeDir, "compositor.stderr.log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jE,MAAM,qBAAqB,CAAC,EAAE,UAAU,EAAE,oBAAoB,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC,CAAC;QAC3F,MAAM,eAAe,GAAG,MAAM,iBAAiB,CAAC,UAAU,EAAE,oBAAoB,CAAC,CAAC;QAClF,MAAM,iBAAiB,GAAG,MAAM,wBAAwB,CAAC,aAAa,aAAa,EAAE,CAAC,CAAC;QACvF,MAAM,cAAc,GAAG,uBAAuB,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,uBAAuB,CAAC,CAAC,CAAC;QAC5G,MAAM,aAAa,GAAG,mBAAmB,CAAC,UAAU,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;QAE5E,OAAO,YAAY,CAAC;YAChB,aAAa;YACb,GAAG,EAAE;gBACD,WAAW,CAAC,OAAO,CAAC,CAAC;YACzB,CAAC;YACD,eAAe;YACf,iBAAiB;YACjB,aAAa;SAChB,CAAC,CAAC;IACP,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,WAAW,CAAC,OAAO,CAAC,CAAC;QACrB,aAAa,EAAE,CAAC;QAChB,MAAM,KAAK,CAAC;IAChB,CAAC;AACL,CAAC,CAAC;AAEF,OAAO,EACH,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,EACnB,oBAAoB,GAGvB,CAAC","sourcesContent":["import { resolveExecutable, spawnWithParentDeathSignal } from \"@gtkx/utils\";\nimport { type ChildProcess, spawnSync } from \"node:child_process\";\nimport { chmodSync, createWriteStream, existsSync, mkdtempSync, rmSync, writeFileSync } from \"node:fs\";\nimport { Socket } from \"node:net\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { startNotificationService } from \"./notification-service.ts\";\nimport { startVirtualSeat } from \"./virtual-seat.ts\";\n\n/**\n * Wayland compositors that can back a headless display.\n */\ntype CompositorId = \"sway\" | \"weston\";\n\n/**\n * Settings for the per-worker headless Wayland display.\n */\ntype HeadlessOptions = {\n /** Output resolution as a \"WIDTHxHEIGHT\" string, for example \"1024x768\". */\n size: string;\n /** Wayland compositor used to back the headless display. */\n compositor: CompositorId;\n};\n\ntype EnvSnapshot = Record<string, string | undefined>;\n\ntype CompositorDescriptor = {\n socket: string;\n env: Record<string, string>;\n requiresVirtualSeat: boolean;\n start: (runtimeDir: string, width: string, height: string) => ChildProcess;\n};\n\ntype SpawnedCompositor = {\n child: ChildProcess;\n socket: string;\n requiresVirtualSeat: boolean;\n};\n\ntype WaitForSocketOptions = {\n label: string;\n timeout?: number;\n child?: ChildProcess;\n signal?: AbortSignal;\n};\n\ntype StderrCapture = {\n read: () => string;\n stop: () => void;\n};\n\ntype ChildHandlers = {\n exit: (code: number | null, signal: NodeJS.Signals | null) => void;\n error: (cause: Error) => void;\n};\n\ntype DisplaySockets = {\n compositor: SpawnedCompositor;\n compositorSocketPath: string;\n busChild: ChildProcess;\n busSocketPath: string;\n};\n\ntype SocketWatch = {\n path: string;\n options: WaitForSocketOptions;\n resolve: () => void;\n reject: (error: Error) => void;\n};\n\nconst DEFAULT_HEADLESS_SIZE = \"1024x768\";\nconst DEFAULT_HEADLESS_COMPOSITOR: CompositorId = \"sway\";\n\nconst BUS_CONFIG_DOCTYPE =\n '<!DOCTYPE busconfig PUBLIC \"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN\" ' +\n '\"https://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd\">';\n\nconst hasWestonFakeSeat = createWestonFakeSeatProbe();\n\nconst compositorRegistry: Record<CompositorId, CompositorDescriptor> = {\n sway: {\n socket: \"wayland-1\",\n requiresVirtualSeat: true,\n env: {\n WLR_BACKENDS: \"headless\",\n WLR_RENDERER: \"pixman\",\n WLR_RENDERER_ALLOW_SOFTWARE: \"1\",\n WLR_LIBINPUT_NO_DEVICES: \"1\",\n WLR_HEADLESS_OUTPUTS: \"1\",\n },\n start: (runtimeDir, width, height) => {\n const configPath = join(runtimeDir, \"sway.conf\");\n\n writeFileSync(\n configPath,\n [\n \"xwayland disable\",\n \"default_border none\",\n \"default_floating_border none\",\n `output HEADLESS-1 resolution ${width}x${height}`,\n \"output HEADLESS-1 bg #000000 solid_color\",\n 'for_window [app_id=\".*\"] floating enable, border none',\n 'for_window [title=\".*\"] floating enable, border none',\n \"\",\n ].join(\"\\n\"),\n );\n\n return spawnWithParentDeathSignal(\"sway\", [\"-c\", configPath], { stdio: [\"ignore\", \"ignore\", \"pipe\"] });\n },\n },\n weston: {\n socket: \"wayland-0\",\n requiresVirtualSeat: false,\n env: {},\n start: (_runtimeDir, width, height) =>\n spawnWithParentDeathSignal(\n \"weston\",\n [\n \"--backend=headless\",\n \"--renderer=pixman\",\n ...(hasWestonFakeSeat() ? [\"--fake-seat\"] : []),\n `--width=${width}`,\n `--height=${height}`,\n \"--socket=wayland-0\",\n ],\n { stdio: [\"ignore\", \"ignore\", \"pipe\"] },\n ),\n },\n};\n\nconst STATIC_HEADLESS_ENV = {\n GDK_BACKEND: \"wayland\",\n GDK_DISABLE: \"vulkan\",\n GDK_DEBUG: \"no-vsync\",\n GSK_RENDERER: \"cairo\",\n GTK_A11Y: \"test\",\n LIBGL_ALWAYS_SOFTWARE: \"1\",\n GST_GL_WINDOW: \"none\",\n GSETTINGS_BACKEND: \"memory\",\n ALSOFT_DRIVERS: \"null\",\n ALSOFT_LOGLEVEL: \"0\",\n};\n\nconst resolveHeadlessOptions = (provided: Partial<HeadlessOptions>): HeadlessOptions => ({\n size: provided.size ?? DEFAULT_HEADLESS_SIZE,\n compositor: provided.compositor ?? DEFAULT_HEADLESS_COMPOSITOR,\n});\n\nconst applyEnv = (snapshot: EnvSnapshot, values: Record<string, string>): void => {\n for (const [name, value] of Object.entries(values)) {\n if (!Object.hasOwn(snapshot, name)) {\n snapshot[name] = process.env[name];\n }\n\n process.env[name] = value;\n }\n};\n\nconst restoreEnv = (snapshot: EnvSnapshot): void => {\n for (const [name, previous] of Object.entries(snapshot)) {\n if (previous === undefined) {\n Reflect.deleteProperty(process.env, name);\n } else {\n process.env[name] = previous;\n }\n }\n};\n\nfunction createWestonFakeSeatProbe(): () => boolean {\n let isSupported: boolean | undefined;\n\n return () => {\n if (isSupported === undefined) {\n const help = spawnSync(resolveExecutable(\"weston\"), [\"--help\"], { encoding: \"utf8\" });\n isSupported = `${help.stdout}${help.stderr}`.includes(\"--fake-seat\");\n }\n\n return isSupported;\n };\n}\n\nconst isCompositorId = (value: string): value is CompositorId => Object.hasOwn(compositorRegistry, value);\n\nconst readHeadlessOptions = (params: URLSearchParams): Partial<HeadlessOptions> => {\n const options: Partial<HeadlessOptions> = {};\n const size = params.get(\"size\");\n\n if (size !== null) {\n options.size = size;\n }\n\n const compositor = params.get(\"compositor\");\n\n if (compositor !== null && isCompositorId(compositor)) {\n options.compositor = compositor;\n }\n\n return options;\n};\n\nconst startCompositor = (runtimeDir: string, options: HeadlessOptions, env: EnvSnapshot): SpawnedCompositor => {\n const descriptor = compositorRegistry[options.compositor];\n const [width = \"\", height = \"\"] = options.size.split(\"x\", 2);\n applyEnv(env, descriptor.env);\n\n return {\n child: descriptor.start(runtimeDir, width, height),\n socket: descriptor.socket,\n requiresVirtualSeat: descriptor.requiresVirtualSeat,\n };\n};\n\nconst noVirtualSeat = (): void => undefined;\n\nconst attachVirtualSeat = (compositor: SpawnedCompositor, socketPath: string): Promise<() => void> =>\n compositor.requiresVirtualSeat ? startVirtualSeat(socketPath) : Promise.resolve(noVirtualSeat);\n\nconst writeBusConfig = (busConfigPath: string, busSocketPath: string): void => {\n writeFileSync(\n busConfigPath,\n [\n BUS_CONFIG_DOCTYPE,\n \"<busconfig>\",\n \" <type>session</type>\",\n ` <listen>unix:path=${busSocketPath}</listen>`,\n \" <auth>EXTERNAL</auth>\",\n ' <policy context=\"default\">',\n ' <allow send_destination=\"*\" eavesdrop=\"true\"/>',\n ' <allow eavesdrop=\"true\"/>',\n ' <allow own=\"*\"/>',\n \" </policy>\",\n \"</busconfig>\",\n ].join(\"\\n\"),\n );\n};\n\nconst captureStderr = (child: ChildProcess | undefined): StderrCapture => {\n const stderr = child?.stderr ?? null;\n let log = \"\";\n stderr?.setEncoding(\"utf8\");\n\n stderr?.on(\"data\", (chunk: string) => {\n log += chunk;\n });\n\n return {\n read: () => log,\n stop: () => {\n stderr?.removeAllListeners(\"data\");\n stderr?.resume();\n\n if (stderr instanceof Socket) {\n stderr.unref();\n }\n },\n };\n};\n\nconst trackChild = (child: ChildProcess, handlers: ChildHandlers): (() => void) => {\n child.on(\"close\", handlers.exit);\n child.on(\"error\", handlers.error);\n\n return () => {\n child.removeListener(\"close\", handlers.exit);\n child.removeListener(\"error\", handlers.error);\n };\n};\n\nconst exitedMessage = (label: string, path: string, code: number | null, signal: NodeJS.Signals | null): string =>\n `${label} exited (code ${String(code)}, signal ${signal ?? \"null\"}) before ${path} appeared`;\n\nconst runCleanups = (cleanups: (() => void)[]): void => {\n for (const cleanup of cleanups) {\n cleanup();\n }\n\n cleanups.length = 0;\n};\n\nconst pollForPath = (path: string, onFound: () => void): NodeJS.Timeout =>\n setInterval(() => {\n if (existsSync(path)) {\n onFound();\n }\n }, 50);\n\nconst stderrSuffix = (child: ChildProcess | undefined, stderr: StderrCapture): string =>\n child ? `\\n${stderr.read()}` : \"\";\n\nconst hasAlreadyAborted = (signal: AbortSignal | undefined, onAbort: () => void, cleanups: (() => void)[]): boolean => {\n if (signal === undefined) {\n return false;\n }\n\n if (signal.aborted) {\n return true;\n }\n\n signal.addEventListener(\"abort\", onAbort);\n\n cleanups.push(() => {\n signal.removeEventListener(\"abort\", onAbort);\n });\n\n return false;\n};\n\nconst watchForSocket = ({ path, options, resolve, reject }: SocketWatch): void => {\n const { label, timeout = 15_000, child, signal } = options;\n const stderr = captureStderr(child);\n const cleanups: (() => void)[] = [stderr.stop];\n\n const stop = (): void => {\n runCleanups(cleanups);\n };\n\n const fail = (message: string): void => {\n stop();\n reject(new Error(message));\n };\n\n const onExit = (code: number | null, terminationSignal: NodeJS.Signals | null): void => {\n fail(`${exitedMessage(label, path, code, terminationSignal)}\\n${stderr.read()}`);\n };\n\n const onError = (cause: Error): void => {\n fail(`${label} failed to spawn: ${cause.message}\\n${stderr.read()}`);\n };\n\n const onAbort = (): void => {\n fail(`${label} startup aborted before ${path} appeared`);\n };\n\n const poll = pollForPath(path, () => {\n stop();\n resolve();\n });\n\n const timer = setTimeout(() => {\n fail(`${label} did not become available within ${String(timeout)}ms${stderrSuffix(child, stderr)}`);\n }, timeout);\n\n cleanups.push(() => {\n clearInterval(poll);\n clearTimeout(timer);\n });\n\n if (child) {\n cleanups.push(trackChild(child, { exit: onExit, error: onError }));\n }\n\n if (hasAlreadyAborted(signal, onAbort, cleanups)) {\n onAbort();\n }\n};\n\nconst waitForSocket = (path: string, options: WaitForSocketOptions): Promise<void> =>\n new Promise((resolve, reject) => {\n watchForSocket({ path, options, resolve, reject });\n });\n\nconst captureCompositorStderr = (child: ChildProcess, logPath: string): string[] => {\n const captured: string[] = [];\n const stderr = child.stderr;\n\n if (stderr !== null) {\n stderr.setEncoding(\"utf8\");\n const logStream = createWriteStream(logPath);\n logStream.on(\"error\", (): void => undefined);\n\n stderr.on(\"data\", (chunk: string) => {\n captured.push(chunk);\n logStream.write(chunk);\n });\n }\n\n return captured;\n};\n\nconst compositorExitMessage = (\n code: number | null,\n signal: NodeJS.Signals | null,\n capturedStderr: string[],\n): string =>\n `[gtkx] the headless compositor exited (code ${String(code)}, signal ${signal ?? \"null\"}); ` +\n `every Wayland client in this worker has been severed.\\n${capturedStderr.join(\"\")}`;\n\nconst watchCompositorExit = (child: ChildProcess, capturedStderr: string[]): (() => void) => {\n const report = (code: number | null, signal: NodeJS.Signals | null): void => {\n process.stderr.write(compositorExitMessage(code, signal, capturedStderr));\n };\n\n child.on(\"exit\", report);\n\n return () => child.removeListener(\"exit\", report);\n};\n\nconst waitForDisplaySockets = async (sockets: DisplaySockets): Promise<void> => {\n const { compositor, compositorSocketPath, busChild, busSocketPath } = sockets;\n const abort = new AbortController();\n\n try {\n await Promise.all([\n waitForSocket(compositorSocketPath, {\n label: \"Compositor\",\n child: compositor.child,\n signal: abort.signal,\n }),\n waitForSocket(busSocketPath, { label: \"D-Bus session bus\", child: busChild, signal: abort.signal }),\n ]);\n } finally {\n abort.abort();\n }\n};\n\nconst killSpawned = (children: ChildProcess[]): void => {\n for (const child of children) {\n child.kill(\"SIGTERM\");\n }\n};\n\nconst makeTeardown = (stops: (() => void)[]): (() => void) => {\n let isTorndown = false;\n\n return (): void => {\n if (isTorndown) {\n return;\n }\n\n isTorndown = true;\n runCleanups(stops);\n };\n};\n\nconst startHeadlessDisplay = async (options: HeadlessOptions): Promise<() => void> => {\n const env: EnvSnapshot = {};\n const runtimeDir = mkdtempSync(join(tmpdir(), \"gtkx-xdg-\"));\n chmodSync(runtimeDir, 0o700);\n const spawned: ChildProcess[] = [];\n\n const removeRuntime = (): void => {\n restoreEnv(env);\n rmSync(runtimeDir, { recursive: true, force: true });\n };\n\n try {\n applyEnv(env, { XDG_RUNTIME_DIR: runtimeDir });\n const busConfigPath = join(runtimeDir, \"session.conf\");\n const busSocketPath = join(runtimeDir, \"bus\");\n writeBusConfig(busConfigPath, busSocketPath);\n\n const busChild = spawnWithParentDeathSignal(\"dbus-daemon\", [`--config-file=${busConfigPath}`], {\n stdio: [\"ignore\", \"ignore\", \"pipe\"],\n });\n\n busChild.unref();\n spawned.push(busChild);\n applyEnv(env, { DBUS_SESSION_BUS_ADDRESS: `unix:path=${busSocketPath}` });\n const compositor = startCompositor(runtimeDir, options, env);\n compositor.child.unref();\n spawned.push(compositor.child);\n applyEnv(env, { WAYLAND_DISPLAY: compositor.socket });\n const compositorSocketPath = join(runtimeDir, compositor.socket);\n await waitForDisplaySockets({ compositor, compositorSocketPath, busChild, busSocketPath });\n const stopVirtualSeat = await attachVirtualSeat(compositor, compositorSocketPath);\n const stopNotifications = await startNotificationService(`unix:path=${busSocketPath}`);\n const capturedStderr = captureCompositorStderr(compositor.child, join(runtimeDir, \"compositor.stderr.log\"));\n const stopExitWatch = watchCompositorExit(compositor.child, capturedStderr);\n\n return makeTeardown([\n stopExitWatch,\n () => {\n killSpawned(spawned);\n },\n stopVirtualSeat,\n stopNotifications,\n removeRuntime,\n ]);\n } catch (error) {\n killSpawned(spawned);\n removeRuntime();\n throw error;\n }\n};\n\nexport {\n STATIC_HEADLESS_ENV,\n resolveHeadlessOptions,\n readHeadlessOptions,\n startHeadlessDisplay,\n type CompositorId,\n type HeadlessOptions,\n};\n"]}
1
+ {"version":3,"file":"headless-display.js","sourceRoot":"","sources":["../src/headless-display.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAqB,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACvG,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AA0DrD,MAAM,qBAAqB,GAAG,UAAU,CAAC;AACzC,MAAM,2BAA2B,GAAiB,MAAM,CAAC;AAEzD,MAAM,kBAAkB,GACpB,mFAAmF;IACnF,iEAAiE,CAAC;AAEtE,MAAM,iBAAiB,GAAG,yBAAyB,EAAE,CAAC;AAEtD,MAAM,kBAAkB,GAA+C;IACnE,IAAI,EAAE;QACF,MAAM,EAAE,WAAW;QACnB,mBAAmB,EAAE,IAAI;QACzB,GAAG,EAAE;YACD,YAAY,EAAE,UAAU;YACxB,YAAY,EAAE,QAAQ;YACtB,2BAA2B,EAAE,GAAG;YAChC,uBAAuB,EAAE,GAAG;YAC5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AC,UAAU,EAAE,oBAAoB,CAAC,CAAC;QAClF,MAAM,iBAAiB,GAAG,MAAM,wBAAwB,CAAC,aAAa,aAAa,EAAE,CAAC,CAAC;QACvF,MAAM,cAAc,GAAG,uBAAuB,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,uBAAuB,CAAC,CAAC,CAAC;QAC5G,MAAM,aAAa,GAAG,mBAAmB,CAAC,UAAU,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;QAE5E,OAAO,YAAY,CAAC;YAChB,aAAa;YACb,GAAG,EAAE;gBACD,WAAW,CAAC,OAAO,CAAC,CAAC;YACzB,CAAC;YACD,eAAe;YACf,iBAAiB;YACjB,aAAa;SAChB,CAAC,CAAC;IACP,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,WAAW,CAAC,OAAO,CAAC,CAAC;QACrB,aAAa,EAAE,CAAC;QAChB,MAAM,KAAK,CAAC;IAChB,CAAC;AACL,CAAC,CAAC;AAEF,OAAO,EACH,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,EACnB,oBAAoB,GAGvB,CAAC","sourcesContent":["import { resolveExecutable, spawnWithParentDeathSignal } from \"@gtkx/utils\";\nimport { type ChildProcess, spawnSync } from \"node:child_process\";\nimport { chmodSync, createWriteStream, existsSync, mkdtempSync, rmSync, writeFileSync } from \"node:fs\";\nimport { Socket } from \"node:net\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { startNotificationService } from \"./notification-service.ts\";\nimport { startVirtualSeat } from \"./virtual-seat.ts\";\n\n/**\n * Wayland compositors that can back a headless display.\n */\ntype CompositorId = \"sway\" | \"weston\";\n\n/**\n * Settings for the per-worker headless Wayland display.\n */\ntype HeadlessOptions = {\n /** Output resolution as a \"WIDTHxHEIGHT\" string, for example \"1024x768\". */\n size: string;\n /** Wayland compositor used to back the headless display. */\n compositor: CompositorId;\n};\n\ntype EnvSnapshot = Record<string, string | undefined>;\n\ntype CompositorDescriptor = {\n socket: string;\n env: Record<string, string>;\n requiresVirtualSeat: boolean;\n start: (runtimeDir: string, width: string, height: string) => ChildProcess;\n};\n\ntype SpawnedCompositor = {\n child: ChildProcess;\n socket: string;\n requiresVirtualSeat: boolean;\n};\n\ntype ChildMonitor = {\n label: string;\n path: string;\n read: () => string;\n failure: () => string | undefined;\n subscribe: (notify: (failure: string) => void) => () => void;\n stop: () => void;\n};\n\ntype WaitForSocketOptions = {\n monitor: ChildMonitor;\n guards?: ChildMonitor[];\n timeout?: number;\n};\n\ntype DisplaySockets = {\n compositorMonitor: ChildMonitor;\n busMonitor: ChildMonitor;\n};\n\ntype SocketWatch = {\n options: WaitForSocketOptions;\n resolve: () => void;\n reject: (error: Error) => void;\n};\n\nconst DEFAULT_HEADLESS_SIZE = \"1024x768\";\nconst DEFAULT_HEADLESS_COMPOSITOR: CompositorId = \"sway\";\n\nconst BUS_CONFIG_DOCTYPE =\n '<!DOCTYPE busconfig PUBLIC \"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN\" ' +\n '\"https://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd\">';\n\nconst hasWestonFakeSeat = createWestonFakeSeatProbe();\n\nconst compositorRegistry: Record<CompositorId, CompositorDescriptor> = {\n sway: {\n socket: \"wayland-1\",\n requiresVirtualSeat: true,\n env: {\n WLR_BACKENDS: \"headless\",\n WLR_RENDERER: \"pixman\",\n WLR_RENDERER_ALLOW_SOFTWARE: \"1\",\n WLR_LIBINPUT_NO_DEVICES: \"1\",\n WLR_HEADLESS_OUTPUTS: \"1\",\n },\n start: (runtimeDir, width, height) => {\n const configPath = join(runtimeDir, \"sway.conf\");\n\n writeFileSync(\n configPath,\n [\n \"xwayland disable\",\n \"default_border none\",\n \"default_floating_border none\",\n `output HEADLESS-1 resolution ${width}x${height}`,\n \"output HEADLESS-1 bg #000000 solid_color\",\n 'for_window [app_id=\".*\"] floating enable, border none',\n 'for_window [title=\".*\"] floating enable, border none',\n \"\",\n ].join(\"\\n\"),\n );\n\n return spawnWithParentDeathSignal(\"sway\", [\"-c\", configPath], { stdio: [\"ignore\", \"ignore\", \"pipe\"] });\n },\n },\n weston: {\n socket: \"wayland-0\",\n requiresVirtualSeat: false,\n env: {},\n start: (_runtimeDir, width, height) =>\n spawnWithParentDeathSignal(\n \"weston\",\n [\n \"--backend=headless\",\n \"--renderer=pixman\",\n ...(hasWestonFakeSeat() ? [\"--fake-seat\"] : []),\n `--width=${width}`,\n `--height=${height}`,\n \"--socket=wayland-0\",\n ],\n { stdio: [\"ignore\", \"ignore\", \"pipe\"] },\n ),\n },\n};\n\nconst STATIC_HEADLESS_ENV = {\n GDK_BACKEND: \"wayland\",\n GDK_DISABLE: \"vulkan\",\n GDK_DEBUG: \"no-vsync\",\n GSK_RENDERER: \"cairo\",\n GTK_A11Y: \"test\",\n LIBGL_ALWAYS_SOFTWARE: \"1\",\n GST_GL_WINDOW: \"none\",\n GSETTINGS_BACKEND: \"memory\",\n ALSOFT_DRIVERS: \"null\",\n ALSOFT_LOGLEVEL: \"0\",\n};\n\nconst resolveHeadlessOptions = (provided: Partial<HeadlessOptions>): HeadlessOptions => ({\n size: provided.size ?? DEFAULT_HEADLESS_SIZE,\n compositor: provided.compositor ?? DEFAULT_HEADLESS_COMPOSITOR,\n});\n\nconst applyEnv = (snapshot: EnvSnapshot, values: Record<string, string>): void => {\n for (const [name, value] of Object.entries(values)) {\n if (!Object.hasOwn(snapshot, name)) {\n snapshot[name] = process.env[name];\n }\n\n process.env[name] = value;\n }\n};\n\nconst restoreEnv = (snapshot: EnvSnapshot): void => {\n for (const [name, previous] of Object.entries(snapshot)) {\n if (previous === undefined) {\n Reflect.deleteProperty(process.env, name);\n } else {\n process.env[name] = previous;\n }\n }\n};\n\nfunction createWestonFakeSeatProbe(): () => boolean {\n let isSupported: boolean | undefined;\n\n return () => {\n if (isSupported === undefined) {\n const help = spawnSync(resolveExecutable(\"weston\"), [\"--help\"], { encoding: \"utf8\" });\n isSupported = `${help.stdout}${help.stderr}`.includes(\"--fake-seat\");\n }\n\n return isSupported;\n };\n}\n\nconst isCompositorId = (value: string): value is CompositorId => Object.hasOwn(compositorRegistry, value);\n\nconst readHeadlessOptions = (params: URLSearchParams): Partial<HeadlessOptions> => {\n const options: Partial<HeadlessOptions> = {};\n const size = params.get(\"size\");\n\n if (size !== null) {\n options.size = size;\n }\n\n const compositor = params.get(\"compositor\");\n\n if (compositor !== null && isCompositorId(compositor)) {\n options.compositor = compositor;\n }\n\n return options;\n};\n\nconst startCompositor = (runtimeDir: string, options: HeadlessOptions, env: EnvSnapshot): SpawnedCompositor => {\n const descriptor = compositorRegistry[options.compositor];\n const [width = \"\", height = \"\"] = options.size.split(\"x\", 2);\n applyEnv(env, descriptor.env);\n\n return {\n child: descriptor.start(runtimeDir, width, height),\n socket: descriptor.socket,\n requiresVirtualSeat: descriptor.requiresVirtualSeat,\n };\n};\n\nconst noVirtualSeat = (): void => undefined;\n\nconst attachVirtualSeat = (compositor: SpawnedCompositor, socketPath: string): Promise<() => void> =>\n compositor.requiresVirtualSeat ? startVirtualSeat(socketPath) : Promise.resolve(noVirtualSeat);\n\nconst writeBusConfig = (busConfigPath: string, busSocketPath: string): void => {\n writeFileSync(\n busConfigPath,\n [\n BUS_CONFIG_DOCTYPE,\n \"<busconfig>\",\n \" <type>session</type>\",\n ` <listen>unix:path=${busSocketPath}</listen>`,\n \" <auth>EXTERNAL</auth>\",\n ' <policy context=\"default\">',\n ' <allow send_destination=\"*\" eavesdrop=\"true\"/>',\n ' <allow eavesdrop=\"true\"/>',\n ' <allow own=\"*\"/>',\n \" </policy>\",\n \"</busconfig>\",\n ].join(\"\\n\"),\n );\n};\n\nconst exitedMessage = (label: string, path: string, code: number | null, signal: NodeJS.Signals | null): string =>\n `${label} exited (code ${String(code)}, signal ${signal ?? \"null\"}) before ${path} appeared`;\n\nconst monitorChild = (child: ChildProcess, label: string, path: string): ChildMonitor => {\n const stderr = child.stderr;\n const subscribers: Set<(failure: string) => void> = new Set();\n let log = \"\";\n let failure: string | undefined;\n\n const onData = (chunk: string): void => {\n log += chunk;\n };\n\n stderr?.setEncoding(\"utf8\");\n stderr?.on(\"data\", onData);\n\n const record = (cause: string): void => {\n failure ??= cause;\n\n for (const notify of subscribers) {\n notify(cause);\n }\n };\n\n child.on(\"close\", (code: number | null, signal: NodeJS.Signals | null) => {\n record(`${exitedMessage(label, path, code, signal)}\\n${log}`);\n });\n\n child.on(\"error\", (cause: Error) => {\n record(`${label} failed to spawn before ${path} appeared: ${cause.message}\\n${log}`);\n });\n\n return {\n label,\n path,\n read: () => log,\n failure: () => failure,\n subscribe: (notify) => {\n subscribers.add(notify);\n\n return (): void => {\n subscribers.delete(notify);\n };\n },\n stop: () => {\n stderr?.removeListener(\"data\", onData);\n stderr?.resume();\n\n if (stderr instanceof Socket) {\n stderr.unref();\n }\n },\n };\n};\n\nconst runCleanups = (cleanups: (() => void)[]): void => {\n for (const cleanup of cleanups) {\n cleanup();\n }\n\n cleanups.length = 0;\n};\n\nconst pollForPath = (path: string, onFound: () => void): NodeJS.Timeout =>\n setInterval(() => {\n if (existsSync(path)) {\n onFound();\n }\n }, 50);\n\nconst firstFailure = (monitors: ChildMonitor[]): string | undefined => {\n for (const monitor of monitors) {\n const failure = monitor.failure();\n\n if (failure !== undefined) {\n return failure;\n }\n }\n\n return undefined;\n};\n\nconst watchForSocket = ({ options, resolve, reject }: SocketWatch): void => {\n const { monitor, guards = [], timeout = 15_000 } = options;\n const watched = [monitor, ...guards];\n const cleanups: (() => void)[] = [monitor.stop];\n\n const stop = (): void => {\n runCleanups(cleanups);\n };\n\n const fail = (message: string): void => {\n stop();\n\n for (const guard of guards) {\n guard.stop();\n }\n\n reject(new Error(message));\n };\n\n const alreadyFailed = firstFailure(watched);\n\n if (alreadyFailed !== undefined) {\n fail(alreadyFailed);\n\n return;\n }\n\n const poll = pollForPath(monitor.path, () => {\n stop();\n resolve();\n });\n\n const timer = setTimeout(() => {\n fail(`${monitor.label} did not become available within ${String(timeout)}ms\\n${monitor.read()}`);\n }, timeout);\n\n cleanups.push(\n () => {\n clearInterval(poll);\n clearTimeout(timer);\n },\n ...watched.map((entry) => entry.subscribe(fail)),\n );\n};\n\nconst waitForSocket = (options: WaitForSocketOptions): Promise<void> =>\n new Promise((resolve, reject) => {\n watchForSocket({ options, resolve, reject });\n });\n\nconst captureCompositorStderr = (child: ChildProcess, logPath: string): string[] => {\n const captured: string[] = [];\n const stderr = child.stderr;\n\n if (stderr !== null) {\n stderr.setEncoding(\"utf8\");\n const logStream = createWriteStream(logPath);\n logStream.on(\"error\", (): void => undefined);\n\n stderr.on(\"data\", (chunk: string) => {\n captured.push(chunk);\n logStream.write(chunk);\n });\n }\n\n return captured;\n};\n\nconst compositorExitMessage = (\n code: number | null,\n signal: NodeJS.Signals | null,\n capturedStderr: string[],\n): string =>\n `[gtkx] the headless compositor exited (code ${String(code)}, signal ${signal ?? \"null\"}); ` +\n `every Wayland client in this worker has been severed.\\n${capturedStderr.join(\"\")}`;\n\nconst watchCompositorExit = (child: ChildProcess, capturedStderr: string[]): (() => void) => {\n const report = (code: number | null, signal: NodeJS.Signals | null): void => {\n process.stderr.write(compositorExitMessage(code, signal, capturedStderr));\n };\n\n child.on(\"exit\", report);\n\n return () => child.removeListener(\"exit\", report);\n};\n\nconst waitForDisplaySockets = async ({ compositorMonitor, busMonitor }: DisplaySockets): Promise<void> => {\n await waitForSocket({ monitor: busMonitor, guards: [compositorMonitor] });\n await waitForSocket({ monitor: compositorMonitor });\n};\n\nconst killSpawned = (children: ChildProcess[]): void => {\n for (const child of children) {\n child.kill(\"SIGTERM\");\n }\n};\n\nconst makeTeardown = (stops: (() => void)[]): (() => void) => {\n let isTorndown = false;\n\n return (): void => {\n if (isTorndown) {\n return;\n }\n\n isTorndown = true;\n runCleanups(stops);\n };\n};\n\nconst startHeadlessDisplay = async (options: HeadlessOptions): Promise<() => void> => {\n const env: EnvSnapshot = {};\n const runtimeDir = mkdtempSync(join(tmpdir(), \"gtkx-xdg-\"));\n chmodSync(runtimeDir, 0o700);\n const spawned: ChildProcess[] = [];\n\n const removeRuntime = (): void => {\n restoreEnv(env);\n rmSync(runtimeDir, { recursive: true, force: true });\n };\n\n try {\n applyEnv(env, { XDG_RUNTIME_DIR: runtimeDir });\n const busConfigPath = join(runtimeDir, \"session.conf\");\n const busSocketPath = join(runtimeDir, \"bus\");\n writeBusConfig(busConfigPath, busSocketPath);\n\n const busChild = spawnWithParentDeathSignal(\"dbus-daemon\", [`--config-file=${busConfigPath}`], {\n stdio: [\"ignore\", \"ignore\", \"pipe\"],\n });\n\n busChild.unref();\n spawned.push(busChild);\n const busMonitor = monitorChild(busChild, \"D-Bus session bus\", busSocketPath);\n applyEnv(env, { DBUS_SESSION_BUS_ADDRESS: `unix:path=${busSocketPath}` });\n const compositor = startCompositor(runtimeDir, options, env);\n compositor.child.unref();\n spawned.push(compositor.child);\n const compositorSocketPath = join(runtimeDir, compositor.socket);\n const compositorMonitor = monitorChild(compositor.child, \"Compositor\", compositorSocketPath);\n applyEnv(env, { WAYLAND_DISPLAY: compositor.socket });\n await waitForDisplaySockets({ compositorMonitor, busMonitor });\n const stopVirtualSeat = await attachVirtualSeat(compositor, compositorSocketPath);\n const stopNotifications = await startNotificationService(`unix:path=${busSocketPath}`);\n const capturedStderr = captureCompositorStderr(compositor.child, join(runtimeDir, \"compositor.stderr.log\"));\n const stopExitWatch = watchCompositorExit(compositor.child, capturedStderr);\n\n return makeTeardown([\n stopExitWatch,\n () => {\n killSpawned(spawned);\n },\n stopVirtualSeat,\n stopNotifications,\n removeRuntime,\n ]);\n } catch (error) {\n killSpawned(spawned);\n removeRuntime();\n throw error;\n }\n};\n\nexport {\n STATIC_HEADLESS_ENV,\n resolveHeadlessOptions,\n readHeadlessOptions,\n startHeadlessDisplay,\n type CompositorId,\n type HeadlessOptions,\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gtkx/vitest",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Vitest plugin for GTK apps with headless Wayland isolation.",
5
5
  "keywords": [
6
6
  "gtkx",
@@ -52,8 +52,8 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@homebridge/dbus-native": "^0.7.9",
55
- "@gtkx/config": "1.4.0",
56
- "@gtkx/utils": "1.4.0"
55
+ "@gtkx/config": "1.5.0",
56
+ "@gtkx/utils": "1.5.0"
57
57
  },
58
58
  "devDependencies": {
59
59
  "vitest": "^4.1.11"
@@ -37,32 +37,27 @@ type SpawnedCompositor = {
37
37
  requiresVirtualSeat: boolean;
38
38
  };
39
39
 
40
- type WaitForSocketOptions = {
40
+ type ChildMonitor = {
41
41
  label: string;
42
- timeout?: number;
43
- child?: ChildProcess;
44
- signal?: AbortSignal;
45
- };
46
-
47
- type StderrCapture = {
42
+ path: string;
48
43
  read: () => string;
44
+ failure: () => string | undefined;
45
+ subscribe: (notify: (failure: string) => void) => () => void;
49
46
  stop: () => void;
50
47
  };
51
48
 
52
- type ChildHandlers = {
53
- exit: (code: number | null, signal: NodeJS.Signals | null) => void;
54
- error: (cause: Error) => void;
49
+ type WaitForSocketOptions = {
50
+ monitor: ChildMonitor;
51
+ guards?: ChildMonitor[];
52
+ timeout?: number;
55
53
  };
56
54
 
57
55
  type DisplaySockets = {
58
- compositor: SpawnedCompositor;
59
- compositorSocketPath: string;
60
- busChild: ChildProcess;
61
- busSocketPath: string;
56
+ compositorMonitor: ChildMonitor;
57
+ busMonitor: ChildMonitor;
62
58
  };
63
59
 
64
60
  type SocketWatch = {
65
- path: string;
66
61
  options: WaitForSocketOptions;
67
62
  resolve: () => void;
68
63
  reject: (error: Error) => void;
@@ -234,19 +229,52 @@ const writeBusConfig = (busConfigPath: string, busSocketPath: string): void => {
234
229
  );
235
230
  };
236
231
 
237
- const captureStderr = (child: ChildProcess | undefined): StderrCapture => {
238
- const stderr = child?.stderr ?? null;
232
+ const exitedMessage = (label: string, path: string, code: number | null, signal: NodeJS.Signals | null): string =>
233
+ `${label} exited (code ${String(code)}, signal ${signal ?? "null"}) before ${path} appeared`;
234
+
235
+ const monitorChild = (child: ChildProcess, label: string, path: string): ChildMonitor => {
236
+ const stderr = child.stderr;
237
+ const subscribers: Set<(failure: string) => void> = new Set();
239
238
  let log = "";
240
- stderr?.setEncoding("utf8");
239
+ let failure: string | undefined;
241
240
 
242
- stderr?.on("data", (chunk: string) => {
241
+ const onData = (chunk: string): void => {
243
242
  log += chunk;
243
+ };
244
+
245
+ stderr?.setEncoding("utf8");
246
+ stderr?.on("data", onData);
247
+
248
+ const record = (cause: string): void => {
249
+ failure ??= cause;
250
+
251
+ for (const notify of subscribers) {
252
+ notify(cause);
253
+ }
254
+ };
255
+
256
+ child.on("close", (code: number | null, signal: NodeJS.Signals | null) => {
257
+ record(`${exitedMessage(label, path, code, signal)}\n${log}`);
258
+ });
259
+
260
+ child.on("error", (cause: Error) => {
261
+ record(`${label} failed to spawn before ${path} appeared: ${cause.message}\n${log}`);
244
262
  });
245
263
 
246
264
  return {
265
+ label,
266
+ path,
247
267
  read: () => log,
268
+ failure: () => failure,
269
+ subscribe: (notify) => {
270
+ subscribers.add(notify);
271
+
272
+ return (): void => {
273
+ subscribers.delete(notify);
274
+ };
275
+ },
248
276
  stop: () => {
249
- stderr?.removeAllListeners("data");
277
+ stderr?.removeListener("data", onData);
250
278
  stderr?.resume();
251
279
 
252
280
  if (stderr instanceof Socket) {
@@ -256,19 +284,6 @@ const captureStderr = (child: ChildProcess | undefined): StderrCapture => {
256
284
  };
257
285
  };
258
286
 
259
- const trackChild = (child: ChildProcess, handlers: ChildHandlers): (() => void) => {
260
- child.on("close", handlers.exit);
261
- child.on("error", handlers.error);
262
-
263
- return () => {
264
- child.removeListener("close", handlers.exit);
265
- child.removeListener("error", handlers.error);
266
- };
267
- };
268
-
269
- const exitedMessage = (label: string, path: string, code: number | null, signal: NodeJS.Signals | null): string =>
270
- `${label} exited (code ${String(code)}, signal ${signal ?? "null"}) before ${path} appeared`;
271
-
272
287
  const runCleanups = (cleanups: (() => void)[]): void => {
273
288
  for (const cleanup of cleanups) {
274
289
  cleanup();
@@ -284,31 +299,22 @@ const pollForPath = (path: string, onFound: () => void): NodeJS.Timeout =>
284
299
  }
285
300
  }, 50);
286
301
 
287
- const stderrSuffix = (child: ChildProcess | undefined, stderr: StderrCapture): string =>
288
- child ? `\n${stderr.read()}` : "";
289
-
290
- const hasAlreadyAborted = (signal: AbortSignal | undefined, onAbort: () => void, cleanups: (() => void)[]): boolean => {
291
- if (signal === undefined) {
292
- return false;
293
- }
302
+ const firstFailure = (monitors: ChildMonitor[]): string | undefined => {
303
+ for (const monitor of monitors) {
304
+ const failure = monitor.failure();
294
305
 
295
- if (signal.aborted) {
296
- return true;
306
+ if (failure !== undefined) {
307
+ return failure;
308
+ }
297
309
  }
298
310
 
299
- signal.addEventListener("abort", onAbort);
300
-
301
- cleanups.push(() => {
302
- signal.removeEventListener("abort", onAbort);
303
- });
304
-
305
- return false;
311
+ return undefined;
306
312
  };
307
313
 
308
- const watchForSocket = ({ path, options, resolve, reject }: SocketWatch): void => {
309
- const { label, timeout = 15_000, child, signal } = options;
310
- const stderr = captureStderr(child);
311
- const cleanups: (() => void)[] = [stderr.stop];
314
+ const watchForSocket = ({ options, resolve, reject }: SocketWatch): void => {
315
+ const { monitor, guards = [], timeout = 15_000 } = options;
316
+ const watched = [monitor, ...guards];
317
+ const cleanups: (() => void)[] = [monitor.stop];
312
318
 
313
319
  const stop = (): void => {
314
320
  runCleanups(cleanups);
@@ -316,47 +322,43 @@ const watchForSocket = ({ path, options, resolve, reject }: SocketWatch): void =
316
322
 
317
323
  const fail = (message: string): void => {
318
324
  stop();
325
+
326
+ for (const guard of guards) {
327
+ guard.stop();
328
+ }
329
+
319
330
  reject(new Error(message));
320
331
  };
321
332
 
322
- const onExit = (code: number | null, terminationSignal: NodeJS.Signals | null): void => {
323
- fail(`${exitedMessage(label, path, code, terminationSignal)}\n${stderr.read()}`);
324
- };
333
+ const alreadyFailed = firstFailure(watched);
325
334
 
326
- const onError = (cause: Error): void => {
327
- fail(`${label} failed to spawn: ${cause.message}\n${stderr.read()}`);
328
- };
335
+ if (alreadyFailed !== undefined) {
336
+ fail(alreadyFailed);
329
337
 
330
- const onAbort = (): void => {
331
- fail(`${label} startup aborted before ${path} appeared`);
332
- };
338
+ return;
339
+ }
333
340
 
334
- const poll = pollForPath(path, () => {
341
+ const poll = pollForPath(monitor.path, () => {
335
342
  stop();
336
343
  resolve();
337
344
  });
338
345
 
339
346
  const timer = setTimeout(() => {
340
- fail(`${label} did not become available within ${String(timeout)}ms${stderrSuffix(child, stderr)}`);
347
+ fail(`${monitor.label} did not become available within ${String(timeout)}ms\n${monitor.read()}`);
341
348
  }, timeout);
342
349
 
343
- cleanups.push(() => {
344
- clearInterval(poll);
345
- clearTimeout(timer);
346
- });
347
-
348
- if (child) {
349
- cleanups.push(trackChild(child, { exit: onExit, error: onError }));
350
- }
351
-
352
- if (hasAlreadyAborted(signal, onAbort, cleanups)) {
353
- onAbort();
354
- }
350
+ cleanups.push(
351
+ () => {
352
+ clearInterval(poll);
353
+ clearTimeout(timer);
354
+ },
355
+ ...watched.map((entry) => entry.subscribe(fail)),
356
+ );
355
357
  };
356
358
 
357
- const waitForSocket = (path: string, options: WaitForSocketOptions): Promise<void> =>
359
+ const waitForSocket = (options: WaitForSocketOptions): Promise<void> =>
358
360
  new Promise((resolve, reject) => {
359
- watchForSocket({ path, options, resolve, reject });
361
+ watchForSocket({ options, resolve, reject });
360
362
  });
361
363
 
362
364
  const captureCompositorStderr = (child: ChildProcess, logPath: string): string[] => {
@@ -395,22 +397,9 @@ const watchCompositorExit = (child: ChildProcess, capturedStderr: string[]): (()
395
397
  return () => child.removeListener("exit", report);
396
398
  };
397
399
 
398
- const waitForDisplaySockets = async (sockets: DisplaySockets): Promise<void> => {
399
- const { compositor, compositorSocketPath, busChild, busSocketPath } = sockets;
400
- const abort = new AbortController();
401
-
402
- try {
403
- await Promise.all([
404
- waitForSocket(compositorSocketPath, {
405
- label: "Compositor",
406
- child: compositor.child,
407
- signal: abort.signal,
408
- }),
409
- waitForSocket(busSocketPath, { label: "D-Bus session bus", child: busChild, signal: abort.signal }),
410
- ]);
411
- } finally {
412
- abort.abort();
413
- }
400
+ const waitForDisplaySockets = async ({ compositorMonitor, busMonitor }: DisplaySockets): Promise<void> => {
401
+ await waitForSocket({ monitor: busMonitor, guards: [compositorMonitor] });
402
+ await waitForSocket({ monitor: compositorMonitor });
414
403
  };
415
404
 
416
405
  const killSpawned = (children: ChildProcess[]): void => {
@@ -455,13 +444,15 @@ const startHeadlessDisplay = async (options: HeadlessOptions): Promise<() => voi
455
444
 
456
445
  busChild.unref();
457
446
  spawned.push(busChild);
447
+ const busMonitor = monitorChild(busChild, "D-Bus session bus", busSocketPath);
458
448
  applyEnv(env, { DBUS_SESSION_BUS_ADDRESS: `unix:path=${busSocketPath}` });
459
449
  const compositor = startCompositor(runtimeDir, options, env);
460
450
  compositor.child.unref();
461
451
  spawned.push(compositor.child);
462
- applyEnv(env, { WAYLAND_DISPLAY: compositor.socket });
463
452
  const compositorSocketPath = join(runtimeDir, compositor.socket);
464
- await waitForDisplaySockets({ compositor, compositorSocketPath, busChild, busSocketPath });
453
+ const compositorMonitor = monitorChild(compositor.child, "Compositor", compositorSocketPath);
454
+ applyEnv(env, { WAYLAND_DISPLAY: compositor.socket });
455
+ await waitForDisplaySockets({ compositorMonitor, busMonitor });
465
456
  const stopVirtualSeat = await attachVirtualSeat(compositor, compositorSocketPath);
466
457
  const stopNotifications = await startNotificationService(`unix:path=${busSocketPath}`);
467
458
  const capturedStderr = captureCompositorStderr(compositor.child, join(runtimeDir, "compositor.stderr.log"));