Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{
min: number; // Starting port number
max: number; // Ending port number (inclusive)
retrieve?: number; // Number of ports needed
consecutive?: boolean;
doLog?: boolean;
},
host = TcpPortScanner.DefaultHost, cb = null): Promise {
let freePorts = [];
const busyPorts = [];
const needed = retrieve;
let error = null;
if (needed <= 0) {
return new Promise((resolve) => { resolve(freePorts); });
}
const functor = TcpPortScanner.shouldUseServerMethod(host) ? TcpPortScanner.isPortInUseEx : tcpPortUsed.tcpPortUsed;
for (let port = min; port <= max; port++) {
if (needed <= 0) {
return;
}
const startTime = process.hrtime();
await functor(port, host)
.then((inUse) => {
const endTime = process.hrtime(startTime);
if (inUse) {
busyPorts.push(port);
} else {
if (consecutive && (freePorts.length > 0) &&
(port !== (1 + freePorts[freePorts.length - 1]))) {
if (doLog) {
console.log('TcpPortHelper.finnd: Oops, reset for consecutive requirement');
}