mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-26 00:21:59 +00:00
25 lines
637 B
TypeScript
25 lines
637 B
TypeScript
import net from "node:net";
|
|
|
|
export async function tryListenOnPort(params: {
|
|
port: number;
|
|
host?: string;
|
|
exclusive?: boolean;
|
|
}): Promise<void> {
|
|
const listenOptions: net.ListenOptions = { port: params.port };
|
|
if (params.host) {
|
|
listenOptions.host = params.host;
|
|
}
|
|
if (typeof params.exclusive === "boolean") {
|
|
listenOptions.exclusive = params.exclusive;
|
|
}
|
|
await new Promise<void>((resolve, reject) => {
|
|
const tester = net
|
|
.createServer()
|
|
.once("error", (err) => reject(err))
|
|
.once("listening", () => {
|
|
tester.close(() => resolve());
|
|
})
|
|
.listen(listenOptions);
|
|
});
|
|
}
|