@oxog/port-terminator
Version:
Cross-platform utility to terminate processes on ports with zero dependencies
73 lines • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PortScanner = void 0;
const process_finder_1 = require("./process-finder");
class PortScanner {
constructor() {
this.processFinder = new process_finder_1.ProcessFinder();
}
async scanPort(port, protocol) {
const processes = await this.processFinder.findByPort(port, protocol);
return {
port,
available: processes.length === 0,
processes,
};
}
async scanPorts(ports, protocol) {
const results = new Map();
await Promise.all(ports.map(async (port) => {
const result = await this.scanPort(port, protocol);
results.set(port, result);
}));
return results;
}
async scanPortRange(start, end, protocol) {
const ports = [];
for (let port = start; port <= end; port++) {
ports.push(port);
}
const scanResults = await this.scanPorts(ports, protocol);
const availablePorts = [];
const busyPorts = new Map();
for (const [port, result] of scanResults) {
if (result.available) {
availablePorts.push(port);
}
else {
busyPorts.set(port, result.processes);
}
}
return {
range: `${start}-${end}`,
availablePorts,
busyPorts,
};
}
async findAvailablePort(startPort = 3000, maxAttempts = 100, protocol) {
for (let i = 0; i < maxAttempts; i++) {
const port = startPort + i;
const isAvailable = await this.processFinder.isPortAvailable(port, protocol);
if (isAvailable) {
return port;
}
}
return null;
}
async findAvailablePorts(count, startPort = 3000, maxAttempts = 1000, protocol) {
const availablePorts = [];
let attempts = 0;
let currentPort = startPort;
while (availablePorts.length < count && attempts < maxAttempts) {
const isAvailable = await this.processFinder.isPortAvailable(currentPort, protocol);
if (isAvailable) {
availablePorts.push(currentPort);
}
currentPort++;
attempts++;
}
return availablePorts;
}
}
exports.PortScanner = PortScanner;
//# sourceMappingURL=port-scanner.js.map