feat: complete pending roadmap items with doctor, hardening, and code quality

This commit is contained in:
Pandipipas
2026-02-21 19:27:11 +01:00
parent 710fea38c0
commit 2b0d627396
20 changed files with 1620 additions and 106 deletions
+65 -10
View File
@@ -1,5 +1,6 @@
import { ChildProcess, spawn, SpawnOptions } from "node:child_process";
import fs from "node:fs";
import net from "node:net";
import path from "node:path";
import { AppRuntimeConfig } from "../config/runtime-config";
@@ -25,10 +26,12 @@ type NodecgProcessManagerDeps = {
setTimer: (handler: () => void, timeoutMs: number) => unknown;
stdoutWrite: (chunk: string) => void;
stderrWrite: (chunk: string) => void;
probePortAvailable: (port: number) => Promise<boolean>;
hasReadWriteAccess: (candidatePath: string) => boolean;
};
export type NodecgProcessManager = {
startNodecgProcess: () => ChildProcess;
startNodecgProcess: () => Promise<ChildProcess>;
waitForNodecgReady: (startTime: number) => Promise<void>;
stopNodecgProcessGracefully: () => Promise<void>;
getProcess: () => ChildProcess | null;
@@ -47,8 +50,21 @@ export function createNodecgProcessManager({
let nodecgProcess: ChildProcess | null = null;
let stopNodecgPromise: Promise<void> | null = null;
const startNodecgProcess = (): ChildProcess => {
validateNodecgInstall(nodecgRootPath, appConfig.bundleName, resolvedDeps.pathExists);
const startNodecgProcess = async (): Promise<ChildProcess> => {
validateNodecgInstall(
nodecgRootPath,
appConfig.bundleName,
resolvedDeps.pathExists,
resolvedDeps.hasReadWriteAccess,
);
const portAsNumber = Number.parseInt(appConfig.nodecgPort, 10);
const isPortAvailable = await resolvedDeps.probePortAvailable(portAsNumber);
if (!isPortAvailable) {
throw new Error(
`El puerto ${appConfig.nodecgPort} ya está en uso. Cierra el proceso que lo ocupa o configura NODECG_PORT antes de iniciar.`,
);
}
const indexPath = path.join(nodecgRootPath, "index.js");
const child = resolvedDeps.spawnProcess(resolvedDeps.execPath, [indexPath], {
@@ -138,12 +154,15 @@ export function createNodecgProcessManager({
complete();
});
resolvedDeps.setTimer(() => {
if (processToStop.exitCode === null && processToStop.signalCode === null) {
log(`NodeCG did not exit after SIGTERM, forcing SIGKILL pid=${pid}`);
killNodecgProcessTree(pid, "SIGKILL", log, resolvedDeps);
}
}, Math.max(0, appConfig.nodecgKillTimeoutMs));
resolvedDeps.setTimer(
() => {
if (processToStop.exitCode === null && processToStop.signalCode === null) {
log(`NodeCG did not exit after SIGTERM, forcing SIGKILL pid=${pid}`);
killNodecgProcessTree(pid, "SIGKILL", log, resolvedDeps);
}
},
Math.max(0, appConfig.nodecgKillTimeoutMs),
);
});
return stopNodecgPromise;
@@ -169,10 +188,17 @@ function resolveDeps(deps?: Partial<NodecgProcessManagerDeps>): NodecgProcessMan
setTimer: deps?.setTimer ?? setTimeout,
stdoutWrite: deps?.stdoutWrite ?? ((chunk) => process.stdout.write(chunk)),
stderrWrite: deps?.stderrWrite ?? ((chunk) => process.stderr.write(chunk)),
probePortAvailable: deps?.probePortAvailable ?? probePortAvailable,
hasReadWriteAccess: deps?.hasReadWriteAccess ?? hasReadWriteAccess,
};
}
function validateNodecgInstall(nodecgRootPath: string, bundleName: string, pathExists: (candidatePath: string) => boolean): void {
function validateNodecgInstall(
nodecgRootPath: string,
bundleName: string,
pathExists: (candidatePath: string) => boolean,
hasReadWriteAccessToPath: (candidatePath: string) => boolean,
): void {
const indexPath = path.join(nodecgRootPath, "index.js");
const nodecgBootstrapPath = path.join(nodecgRootPath, "node_modules", "nodecg", "dist", "server", "bootstrap.js");
const bundlePath = path.join(nodecgRootPath, "bundles", bundleName);
@@ -181,6 +207,10 @@ function validateNodecgInstall(nodecgRootPath: string, bundleName: string, pathE
throw new Error(`No existe la carpeta NodeCG: ${nodecgRootPath}`);
}
if (!hasReadWriteAccessToPath(nodecgRootPath)) {
throw new Error(`Sin permisos de lectura/escritura sobre NodeCG: ${nodecgRootPath}`);
}
if (!pathExists(indexPath)) {
throw new Error(`No se encontró ${indexPath}. Copia una instalación completa de NodeCG en lib/nodecg.`);
}
@@ -207,6 +237,31 @@ function validateNodecgInstall(nodecgRootPath: string, bundleName: string, pathE
}
}
function hasReadWriteAccess(candidatePath: string): boolean {
try {
fs.accessSync(candidatePath, fs.constants.R_OK | fs.constants.W_OK);
return true;
} catch {
return false;
}
}
function probePortAvailable(port: number): Promise<boolean> {
return new Promise((resolve) => {
const server = net.createServer();
server.once("error", () => {
resolve(false);
});
server.listen(port, "127.0.0.1", () => {
server.close(() => {
resolve(true);
});
});
});
}
function killNodecgProcessTree(
pid: number,
signal: NodeJS.Signals,