// Port - generate a random valid port number that has not been used yet
// Get an unused port in the user range (2000 - 10000)
const ports = [];
const limit = 1000;
const random = () => 1024 + parseInt(Math.random() * 48151);
// Keep a count of how many times we tried to find a port to avoid infinite loop
const randPort = (i = 0) => {
const port = random();
// If "i" is already taken try again
Iif (port in ports) {
return randPort(i + 1);
}
// Add it to the list of ports already being used and return it
ports.push(port);
return port;
}
module.exports = randPort;
|