UNPKG

portfree

Version:

A cross-platform CLI tool for managing processes running on specific ports

78 lines 2.6 kB
"use strict"; /** * Validation utilities for the FreePort CLI */ Object.defineProperty(exports, "__esModule", { value: true }); exports.validatePort = validatePort; exports.validatePid = validatePid; exports.validateProtocol = validateProtocol; exports.validateYesNo = validateYesNo; const errors_1 = require("../errors"); /** * Validate port number * @param port - Port value to validate * @returns Valid port number * @throws If port is invalid */ function validatePort(port) { // Convert to number if it's a string const portNum = typeof port === 'string' ? parseInt(port, 10) : port; // Check if it's a valid number if (!Number.isInteger(portNum) || isNaN(portNum)) { throw new errors_1.ValidationError(`Invalid port number: "${port}". Port must be a valid integer between 1 and 65535.`); } // Check port range (1-65535) if (portNum < 1 || portNum > 65535) { throw new errors_1.ValidationError(`Port ${portNum} is out of range. Port must be between 1 and 65535. Common ports: 80 (HTTP), 443 (HTTPS), 3000 (development), 8080 (web server).`); } return portNum; } /** * Validate process ID * @param pid - Process ID to validate * @returns Valid process ID * @throws If PID is invalid */ function validatePid(pid) { const pidNum = typeof pid === 'string' ? parseInt(pid, 10) : pid; if (!Number.isInteger(pidNum) || isNaN(pidNum) || pidNum <= 0) { throw new errors_1.ValidationError(`Invalid process ID: "${pid}". Process ID must be a positive integer (e.g., 1234).`); } return pidNum; } /** * Validate protocol * @param protocol - Protocol to validate * @returns Valid protocol * @throws If protocol is invalid */ function validateProtocol(protocol) { if (typeof protocol !== 'string') { throw new errors_1.ValidationError('Protocol must be a string'); } const upperProtocol = protocol.toUpperCase(); if (!['TCP', 'UDP'].includes(upperProtocol)) { throw new errors_1.ValidationError('Protocol must be either TCP or UDP'); } return upperProtocol; } /** * Validate user input for yes/no questions * @param input - User input to validate * @returns True for yes, false for no, null for invalid */ function validateYesNo(input) { if (typeof input !== 'string') { return null; } const normalized = input.toLowerCase().trim(); if (['y', 'yes'].includes(normalized)) { return true; } if (['n', 'no', ''].includes(normalized)) { return false; } // Invalid input return null; } //# sourceMappingURL=validator.js.map