UNPKG

express-port-switcher

Version:

A smart port management tool that automatically finds and forwards to available ports when preferred ports are in use

40 lines 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PortManager = void 0; const events_1 = require("events"); const portUtils_1 = require("./portUtils"); class PortManager extends events_1.EventEmitter { constructor(options) { super(); this.activePorts = new Map(); this.defaultPort = (options === null || options === void 0 ? void 0 : options.defaultPort) || 3000; this.maxPortAttempts = (options === null || options === void 0 ? void 0 : options.maxPortAttempts) || 100; } static getInstance(options) { if (!PortManager.instance) { PortManager.instance = new PortManager(options); } return PortManager.instance; } async allocatePort(desiredPort) { const startPort = desiredPort || this.defaultPort; const port = await (0, portUtils_1.findNextAvailablePort)(startPort, this.maxPortAttempts); if (port) { this.activePorts.set(port, true); this.emit('port-allocated', port); return port; } return null; } releasePort(port) { if (this.activePorts.has(port)) { this.activePorts.delete(port); this.emit('port-released', port); } } getActivePorts() { return Array.from(this.activePorts.keys()); } } exports.PortManager = PortManager; //# sourceMappingURL=PortManager.js.map