Skip to content

Instantly share code, notes, and snippets.

@KR1470R
Created April 15, 2023 07:24
Show Gist options
  • Save KR1470R/0daca01f16577702856acbd989049117 to your computer and use it in GitHub Desktop.
Save KR1470R/0daca01f16577702856acbd989049117 to your computer and use it in GitHub Desktop.
Node.js get any available port
import net from "node:net";
const port_constraints = {
min: 1024,
max: 65535,
};
/**
* @param ip Host ip (default localhost)
* @param minPort Min available port (default 1024)
* @param maxPort Max available port (default 65535)
* @param amount Amount of available ports to get(default 1)
* @returns Promise<number>
*/
export default function findFreePort(
ip: string,
minPort: number = port_constraints.min,
maxPort: number = port_constraints.max,
amount = 1
) {
return new Promise<number[] | number>((resolve, reject) => {
[minPort, maxPort].forEach((p) => {
if (p < port_constraints.min || p > port_constraints.max)
throw new Error("OverPortError");
});
const res: number[] = [];
const probe = (
ip: string,
port: number,
cb: (port: number | null, nextPort?: number) => void
) => {
const s = net.createConnection({ port: port, host: ip });
s.on("connect", () => {
s.end();
cb(null, port + 1);
});
s.on("error", () => {
cb(port);
}); // can't connect, port is available
};
const onprobe = (port: number | null, nextPort?: number) => {
if (port) {
if (amount > 1) {
res.push(port);
if (res.length >= amount) resolve(res);
else setImmediate(() => probe(ip, port + 1, onprobe));
} else resolve(port);
} else {
if (nextPort! >= maxPort) reject("No available port");
else setImmediate(() => probe(ip, nextPort!, onprobe));
}
};
probe(ip, minPort, onprobe);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment