/**
 * Port validation utility
 * Addresses Issue #2: Command Injection Prevention
 *
 * Validates port numbers before using in execSync commands
 * to prevent command injection attacks.
 */
/**
 * Validate and normalize port number
 *
 * @param port - Port value (number, string, or unknown)
 * @returns Validated port number (1-65535)
 * @throws Error if port is invalid
 */
export function validatePort(port) {
    if (typeof port !== "number" && typeof port !== "string") {
        throw new Error(`Port must be a number or string, got ${typeof port}`);
    }
    const portNum = typeof port === "string" ? parseInt(port, 10) : port;
    if (isNaN(portNum)) {
        throw new Error(`Invalid port: ${port} (not a number)`);
    }
    if (!Number.isInteger(portNum)) {
        throw new Error(`Invalid port: ${portNum} (must be an integer)`);
    }
    if (portNum < 1 || portNum > 65535) {
        throw new Error(`Invalid port: ${portNum} (must be between 1 and 65535)`);
    }
    return portNum;
}
/**
 * Safe port validation that returns undefined on error
 * Useful for optional port values
 *
 * @param port - Port value
 * @returns Validated port or undefined if invalid
 */
export function validatePortSafe(port) {
    try {
        return validatePort(port);
    }
    catch {
        return undefined;
    }
}
