Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions packages/sprixe-bridge/src/__tests__/mame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ class FakeProcess implements SpawnedProcessLike {
killed = false;
killSignal: NodeJS.Signals | undefined;
private exitHandler: ((code: number | null, signal: NodeJS.Signals | null) => void) | null = null;
private errorHandler: ((err: Error) => void) | null = null;

on(event: "exit", cb: (code: number | null, signal: NodeJS.Signals | null) => void): void {
if (event === "exit") this.exitHandler = cb;
on(event: "exit", cb: (code: number | null, signal: NodeJS.Signals | null) => void): void;
on(event: "error", cb: (err: Error) => void): void;
on(event: string, cb: (...args: unknown[]) => void): void {
if (event === "exit") this.exitHandler = cb as (c: number | null, s: NodeJS.Signals | null) => void;
if (event === "error") this.errorHandler = cb as (e: Error) => void;
}

kill(signal?: NodeJS.Signals): boolean {
Expand All @@ -20,6 +24,10 @@ class FakeProcess implements SpawnedProcessLike {
fireExit(code: number | null, signal: NodeJS.Signals | null = null): void {
this.exitHandler?.(code, signal);
}

fireError(err: Error): void {
this.errorHandler?.(err);
}
}

function makeSpawner(): { spawner: Spawner; instances: FakeProcess[]; cmds: { cmd: string; args: string[] }[] } {
Expand Down Expand Up @@ -93,6 +101,32 @@ describe("MameProcess", () => {
expect(mame.isRunning()).toBe(false);
});

it("translates async ENOENT 'error' events into spawn-error (not unhandled)", () => {
const { spawner, instances } = makeSpawner();
const mame = new MameProcess({ spawner });
const cb = vi.fn();
mame.onExit(cb);
mame.start({ gameId: "sf2", romPath: "/tmp/roms" });
// Node fires 'error' asynchronously when the binary is missing.
instances[0]!.fireError(new Error("spawn mame ENOENT"));
expect(cb).toHaveBeenCalledWith({
kind: "spawn-error",
error: expect.objectContaining({ message: "spawn mame ENOENT" }),
});
expect(mame.isRunning()).toBe(false);
});

it("ignores 'exit' that fires after an 'error' (Node sometimes emits both)", () => {
const { spawner, instances } = makeSpawner();
const mame = new MameProcess({ spawner });
const cb = vi.fn();
mame.onExit(cb);
mame.start({ gameId: "sf2", romPath: "/tmp/roms" });
instances[0]!.fireError(new Error("ENOENT"));
instances[0]!.fireExit(null);
expect(cb).toHaveBeenCalledTimes(1);
});

it("can relaunch after the previous instance exits", () => {
const { spawner, instances } = makeSpawner();
const mame = new MameProcess({ spawner });
Expand Down
8 changes: 7 additions & 1 deletion packages/sprixe-bridge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
*/

import { BridgeServer } from "./server.js";
import { MameProcess } from "./mame.js";

const port = Number(process.env.SPRIXE_BRIDGE_PORT ?? "7777");
const romDir = process.env.SPRIXE_BRIDGE_ROM_DIR ?? "/tmp/sprixe-roms";
// Debian ships MAME at /usr/games/mame which isn't in systemd's
// default PATH, so a bare "mame" spawn fails with ENOENT under
// systemd even though the binary is installed. Default to the
// canonical Debian path; override via env var on other distros.
const mameBin = process.env.SPRIXE_BRIDGE_MAME_BIN ?? "/usr/games/mame";

const server = new BridgeServer({ port, romDir });
const server = new BridgeServer({ port, romDir, mame: new MameProcess({ bin: mameBin }) });

await server.start();
console.log(`[sprixe-bridge] listening on http://127.0.0.1:${port} (romDir: ${romDir})`);
Expand Down
14 changes: 14 additions & 0 deletions packages/sprixe-bridge/src/mame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { spawn, type ChildProcess } from "node:child_process";
export interface SpawnedProcessLike {
pid?: number | undefined;
on(event: "exit", cb: (code: number | null, signal: NodeJS.Signals | null) => void): void;
on(event: "error", cb: (err: Error) => void): void;
kill(signal?: NodeJS.Signals): boolean;
}

Expand Down Expand Up @@ -81,7 +82,20 @@ export class MameProcess {
return;
}
this.current = proc;
// ENOENT (binary missing) is delivered as an 'error' event, not a
// synchronous throw — without a listener, Node escalates it to an
// unhandled exception and the whole bridge dies. Treat it the same
// way as a synchronous spawn failure: clear state, fire spawn-error.
let settled = false;
proc.on("error", (err) => {
if (settled) return;
settled = true;
this.current = null;
this.notifyExit({ kind: "spawn-error", error: err });
});
proc.on("exit", (code, signal) => {
if (settled) return;
settled = true;
this.current = null;
this.notifyExit({ kind: "exit", code, signal });
});
Expand Down
1 change: 1 addition & 0 deletions packages/sprixe-image/first-boot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ WorkingDirectory=/opt/sprixe/packages/sprixe-bridge
ExecStart=/usr/bin/node /opt/sprixe/packages/sprixe-bridge/dist/index.js
Environment=SPRIXE_BRIDGE_PORT=7777
Environment=SPRIXE_BRIDGE_ROM_DIR=/home/sprixe/sprixe-roms
Environment=SPRIXE_BRIDGE_MAME_BIN=/usr/games/mame
Restart=on-failure
RestartSec=5

Expand Down
Loading