Files
rea/src/cli/commandCancellation.ts
T
N0zoM1z0 904d03e987 feat(android): integrate static APK analysis with headless JADX (#738)
* feat(android): integrate static APK analysis with headless JADX

* test(android): include APK tools in explicit effects audit
2026-10-06 12:46:48 +00:00

33 lines
1.1 KiB
TypeScript

/** Process signal boundary injectable without emitting signals into a test runner. */
export interface CommandCancellationHost {
on(signal: "SIGINT" | "SIGTERM", listener: () => void): unknown;
off(signal: "SIGINT" | "SIGTERM", listener: () => void): unknown;
exitCode?: number | string | null | undefined;
}
/** Translate one-shot CLI interrupts into awaited cleanup and conventional exit codes. */
export const withCommandCancellation = async <Value>(
operation: (signal: AbortSignal) => Promise<Value>,
host: CommandCancellationHost = process,
): Promise<Value> => {
const controller = new AbortController();
let exitCode: number | undefined;
const interrupt = () => {
exitCode ??= 130;
controller.abort();
};
const terminate = () => {
exitCode ??= 143;
controller.abort();
};
host.on("SIGINT", interrupt);
host.on("SIGTERM", terminate);
try {
return await operation(controller.signal);
} finally {
host.off("SIGINT", interrupt);
host.off("SIGTERM", terminate);
if (exitCode !== undefined) host.exitCode = exitCode;
}
};