fix: harden port listener detection

This commit is contained in:
Peter Steinberger
2026-01-21 18:52:26 +00:00
parent 32550154f9
commit 403904ecd1
6 changed files with 139 additions and 21 deletions

35
src/infra/ports-lsof.ts Normal file
View File

@@ -0,0 +1,35 @@
import fs from "node:fs";
import fsPromises from "node:fs/promises";
const LSOF_CANDIDATES =
process.platform === "darwin"
? ["/usr/sbin/lsof", "/usr/bin/lsof"]
: ["/usr/bin/lsof", "/usr/sbin/lsof"];
async function canExecute(path: string): Promise<boolean> {
try {
await fsPromises.access(path, fs.constants.X_OK);
return true;
} catch {
return false;
}
}
export async function resolveLsofCommand(): Promise<string> {
for (const candidate of LSOF_CANDIDATES) {
if (await canExecute(candidate)) return candidate;
}
return "lsof";
}
export function resolveLsofCommandSync(): string {
for (const candidate of LSOF_CANDIDATES) {
try {
fs.accessSync(candidate, fs.constants.X_OK);
return candidate;
} catch {
// keep trying
}
}
return "lsof";
}