everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
57 lines (55 loc) • 1.42 kB
JavaScript
import { Effect } from "effect";
import { execFile } from "node:child_process";
//#region src/infra/port-ownership.ts
let lsofCached = null;
const execFileText = (path, args) => new Promise((resolve, reject) => {
execFile(path, args, { timeout: 5e3 }, (error, stdout) => {
if (error && !stdout) reject(error);
else resolve(stdout);
});
});
const isValidPort = (port) => Number.isInteger(port) && port > 0 && port <= 65535;
const ownerOfPort = (port, options) => Effect.callback((resume) => {
(async () => {
if (!isValidPort(port)) {
resume(Effect.succeed(null));
return;
}
const lsof = options?.lsofPath ?? "lsof";
try {
const stdout = await execFileText(lsof, [
"-ti",
`:${port}`,
"-sTCP:LISTEN"
]);
const pid = Number.parseInt(stdout.trim().split("\n")[0] ?? "", 10);
if (!Number.isInteger(pid) || pid <= 0) {
resume(Effect.succeed(null));
return;
}
try {
const psOut = await execFileText(options?.psPath ?? "ps", [
"-o",
"command=",
"-p",
String(pid)
]);
resume(Effect.succeed({
pid,
command: psOut.trim() || "unknown"
}));
} catch {
resume(Effect.succeed({
pid,
command: "unknown"
}));
}
} catch (error) {
if (error.code === "ENOENT") lsofCached = false;
resume(Effect.succeed(null));
}
})();
});
//#endregion
export { ownerOfPort };
//# sourceMappingURL=port-ownership.mjs.map