UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

222 lines 8.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FIELD_CONSTRAINTS = exports.REQUIRED_FIELDS = void 0; exports.validateAuth = validateAuth; exports.validateEnvironmentConfig = validateEnvironmentConfig; exports.validateTimeout = validateTimeout; exports.validateMaxRetries = validateMaxRetries; exports.validateBaseUrl = validateBaseUrl; exports.parseRawConfig = parseRawConfig; exports.validateAppConfig = validateAppConfig; exports.createConfigErrorMessage = createConfigErrorMessage; exports.applyDefaults = applyDefaults; const config_1 = require("../types/config"); const loader_1 = require("./loader"); exports.REQUIRED_FIELDS = { AUTH: ['appId', 'appSecret'], CORE: [], }; exports.FIELD_CONSTRAINTS = { timeout: { min: 1000, max: 300000 }, maxRetries: { min: 0, max: 10 }, baseUrl: { protocol: ['http:', 'https:'] }, }; function validateAuth(auth) { const errors = []; const missingFields = []; if (!auth.appId || !auth.appId.trim()) { missingFields.push('appId'); errors.push('PolyV App ID is required. Set via --appId or POLYV_APP_ID environment variable.'); } if (!auth.appSecret || !auth.appSecret.trim()) { missingFields.push('appSecret'); errors.push('PolyV App Secret is required. Set via --appSecret or POLYV_APP_SECRET environment variable.'); } if (auth.appId && auth.appId.trim()) { const trimmedAppId = auth.appId.trim(); if (trimmedAppId.length < 10) { errors.push('App ID appears to be invalid (too short). Please check your PolyV App ID.'); } } if (auth.appSecret && auth.appSecret.trim()) { const trimmedAppSecret = auth.appSecret.trim(); if (trimmedAppSecret.length < 16) { errors.push('App Secret appears to be invalid (too short). Please check your PolyV App Secret.'); } } if (auth.userId && auth.userId.trim()) { const trimmedUserId = auth.userId.trim(); if (!/^[a-zA-Z0-9]+$/.test(trimmedUserId)) { errors.push('User ID must be alphanumeric (letters and numbers only). Please provide a valid PolyV User ID.'); } if (trimmedUserId.length < 3) { errors.push('User ID appears to be invalid (too short). Please check your PolyV User ID.'); } } return { errors, missingFields }; } function validateEnvironmentConfig(environment) { const errors = []; if (!config_1.ENVIRONMENT_CONFIGS[environment]) { errors.push(`Invalid environment '${environment}'. Must be one of: development, production, test`); } return errors; } function validateTimeout(timeout) { const errors = []; const { min, max } = exports.FIELD_CONSTRAINTS.timeout; if (timeout < min) { errors.push(`Timeout must be at least ${min}ms (${min / 1000}s)`); } if (timeout > max) { errors.push(`Timeout must not exceed ${max}ms (${max / 1000}s)`); } return errors; } function validateMaxRetries(maxRetries) { const errors = []; const { min, max } = exports.FIELD_CONSTRAINTS.maxRetries; if (maxRetries < min) { errors.push(`Max retries must be at least ${min}`); } if (maxRetries > max) { errors.push(`Max retries must not exceed ${max}`); } return errors; } function validateBaseUrl(baseUrl) { const errors = []; try { const url = new URL(baseUrl); if (!exports.FIELD_CONSTRAINTS.baseUrl.protocol.includes(url.protocol)) { errors.push(`Base URL must use HTTP or HTTPS protocol, got: ${url.protocol}`); } if (!url.hostname) { errors.push('Base URL must have a valid hostname'); } } catch { errors.push(`Invalid base URL format: ${baseUrl}`); } return errors; } function parseRawConfig(rawConfig, environment) { const errors = []; const warnings = []; const envDefaults = config_1.ENVIRONMENT_CONFIGS[environment]; const debug = (0, loader_1.parseBoolean)(rawConfig.POLYV_DEBUG, envDefaults.debug); const timeout = (0, loader_1.parseInteger)(rawConfig.POLYV_TIMEOUT, envDefaults.timeout, exports.FIELD_CONSTRAINTS.timeout.min, exports.FIELD_CONSTRAINTS.timeout.max); if (rawConfig.POLYV_TIMEOUT && (0, loader_1.parseInteger)(rawConfig.POLYV_TIMEOUT, -1) === -1) { warnings.push(`Invalid timeout value '${rawConfig.POLYV_TIMEOUT}', using default: ${envDefaults.timeout}ms`); } const baseUrl = (0, loader_1.parseUrl)(rawConfig.POLYV_BASE_URL, envDefaults.baseUrl); if (rawConfig.POLYV_BASE_URL && baseUrl === envDefaults.baseUrl) { warnings.push(`Invalid base URL '${rawConfig.POLYV_BASE_URL}', using default: ${envDefaults.baseUrl}`); } const maxRetries = (0, loader_1.parseInteger)(rawConfig.POLYV_MAX_RETRIES, envDefaults.maxRetries, exports.FIELD_CONSTRAINTS.maxRetries.min, exports.FIELD_CONSTRAINTS.maxRetries.max); if (rawConfig.POLYV_MAX_RETRIES && (0, loader_1.parseInteger)(rawConfig.POLYV_MAX_RETRIES, -1) === -1) { warnings.push(`Invalid max retries value '${rawConfig.POLYV_MAX_RETRIES}', using default: ${envDefaults.maxRetries}`); } const configPath = rawConfig.POLYV_CONFIG_PATH?.trim(); const auth = {}; if (rawConfig.POLYV_APP_ID?.trim()) { auth.appId = rawConfig.POLYV_APP_ID.trim(); } if (rawConfig.POLYV_APP_SECRET?.trim()) { auth.appSecret = rawConfig.POLYV_APP_SECRET.trim(); } if (rawConfig.POLYV_USER_ID?.trim()) { auth.userId = rawConfig.POLYV_USER_ID.trim(); } const result = { environment, debug, timeout, baseUrl, maxRetries, auth, errors, warnings, }; if (configPath) { result.configPath = configPath; } return result; } function validateAppConfig(config) { const errors = []; const warnings = []; const missingFields = []; const authValidation = validateAuth(config.auth); errors.push(...authValidation.errors); missingFields.push(...authValidation.missingFields); const envErrors = validateEnvironmentConfig(config.environment); errors.push(...envErrors); const timeoutErrors = validateTimeout(config.timeout); errors.push(...timeoutErrors); const retriesErrors = validateMaxRetries(config.maxRetries); errors.push(...retriesErrors); const urlErrors = validateBaseUrl(config.baseUrl); errors.push(...urlErrors); if (config.environment === 'development' && !config.debug) { warnings.push('Debug mode is disabled in development environment. Consider enabling it for better development experience.'); } if (config.environment === 'production' && config.debug) { warnings.push('Debug mode is enabled in production environment. Consider disabling it for better performance.'); } if (config.timeout < 5000 && config.environment === 'production') { warnings.push('Timeout is set to less than 5 seconds in production. This might cause issues with slower network connections.'); } return { isValid: errors.length === 0 && missingFields.length === 0, errors, warnings, missingFields, }; } function createConfigErrorMessage(validation) { if (validation.isValid) { return 'Configuration is valid'; } let message = 'Configuration validation failed:\n\n'; if (validation.missingFields.length > 0) { message += '❌ Missing required fields:\n'; validation.missingFields.forEach(field => { message += ` • ${field}\n`; }); message += '\n'; } if (validation.errors.length > 0) { message += '❌ Configuration errors:\n'; validation.errors.forEach(error => { message += ` • ${error}\n`; }); message += '\n'; } if (validation.warnings.length > 0) { message += '⚠️ Configuration warnings:\n'; validation.warnings.forEach(warning => { message += ` • ${warning}\n`; }); message += '\n'; } message += 'Please fix the above issues before proceeding.\n'; message += '\nFor help with configuration, run: polyv-cli --help'; return message.trim(); } function applyDefaults(partial, environment) { const envDefaults = config_1.ENVIRONMENT_CONFIGS[environment]; const result = { environment: partial.environment || environment, debug: partial.debug !== undefined ? partial.debug : envDefaults.debug, timeout: partial.timeout || envDefaults.timeout, baseUrl: partial.baseUrl || envDefaults.baseUrl, maxRetries: partial.maxRetries !== undefined ? partial.maxRetries : envDefaults.maxRetries, auth: partial.auth, }; if (partial.configPath) { result.configPath = partial.configPath; } return result; } //# sourceMappingURL=validator.js.map