openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
60 lines (59 loc) • 2.35 kB
JavaScript
import { r as defaultRuntime } from "./runtime-DlO3PTAd.mjs";
import { n as isErrno } from "./errno-FR4CRyya.mjs";
import "./errors-O33D01XW.mjs";
import { t as logDebug } from "./logger-DskmrSVa.mjs";
import { a as shouldLogVerbose, n as info, t as danger } from "./globals-DyT306Z_.mjs";
import { i as tryListenOnPort } from "./ports-probe-DMGtt9dg.mjs";
import { r as formatPortDiagnostics } from "./ports-format-B_qarAEO.mjs";
//#region src/infra/ports.ts
var PortInUseError = class extends Error {
constructor(port, details) {
super(`Port ${port} is already in use.`);
this.name = "PortInUseError";
this.port = port;
this.details = details;
}
};
async function describePortOwner(port) {
const { inspectPortUsage } = await import("./ports-inspect-BNlsMUJf.mjs");
const diagnostics = await inspectPortUsage(port);
if (diagnostics.listeners.length === 0) return;
return formatPortDiagnostics(diagnostics).join("\n");
}
/** Probes Node's wildcard bind by default; callers may scope checks to their owned interface. */
async function ensurePortAvailable(port, host, signal) {
try {
const probe = {
port,
...host ? { host } : {},
...signal ? { signal } : {}
};
await tryListenOnPort(probe);
} catch (err) {
if (isErrno(err) && err.code === "EADDRINUSE") throw new PortInUseError(port);
throw err;
}
}
async function handlePortError(err, port, context, runtime = defaultRuntime) {
if (err instanceof PortInUseError || isErrno(err) && err.code === "EADDRINUSE") {
const details = (err instanceof PortInUseError ? err.details : void 0) ?? await describePortOwner(port);
runtime.error(danger(`${context} failed: port ${port} is already in use.`));
if (details) {
runtime.error(info("Port listener details:"));
runtime.error(details);
}
runtime.error(info("Resolve by stopping the process using the port or passing --port <free-port>."));
runtime.exit(1);
}
runtime.error(danger(`${context} failed: ${String(err)}`));
if (shouldLogVerbose()) {
const stdout = err?.stdout;
const stderr = err?.stderr;
if (stdout?.trim()) logDebug(`stdout: ${stdout.trim()}`);
if (stderr?.trim()) logDebug(`stderr: ${stderr.trim()}`);
}
runtime.exit(1);
throw new Error("unreachable");
}
//#endregion
export { handlePortError as i, describePortOwner as n, ensurePortAvailable as r, PortInUseError as t };