openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
61 lines (60 loc) • 2.56 kB
JavaScript
import { r as defaultRuntime } from "./runtime-CF2WjnNZ.js";
import { n as isErrno } from "./errno-CkbDOfLk.js";
import "./errors-Db3Ymjlb.js";
import { i as shouldLogVerbose, n as info, o as warn, t as danger } from "./globals-CTaGxEqj.js";
import { t as logDebug } from "./logger-DwECwNVZ.js";
import { r as tryListenOnPort } from "./ports-probe-funEe1wt.js";
import { r as formatPortDiagnostics } from "./ports-format-D-ReVpaa.js";
//#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-B3FdvO2m.js");
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 ?? await describePortOwner(port) : 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);
if (/openclaw|src\/index\.ts|dist\/index\.js/.test(details)) runtime.error(warn("It looks like another OpenClaw instance is already running. Stop it or pick a different port."));
}
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 };