Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x | class HealthValidator {
/**
* Validates health check endpoint configuration
* @param {string|object} health - Health endpoint path or configuration
* @returns {Promise<object>} Validation result
*/
async validate(health) {
const result = {
valid: true,
errors: [],
warnings: [],
info: {}
};
try {
Iif (!health) {
result.valid = false;
result.errors.push('Health check endpoint is required');
return result;
}
// Handle different health formats
Iif (typeof health === 'string') {
// Simple path string
if (!health.startsWith('/')) {
result.errors.push(`Health endpoint path "${health}" must start with /`);
result.valid = false;
}
result.info.endpoint = health;
result.info.type = 'simple';
} else if (typeof health === 'object') {
// Complex health configuration
Eif (!health.endpoint && !health.path) {
result.errors.push('Health configuration must have endpoint or path');
result.valid = false;
}
const endpoint = health.endpoint || health.path;
Iif (endpoint && !endpoint.startsWith('/')) {
result.errors.push(`Health endpoint path "${endpoint}" must start with /`);
result.valid = false;
}
result.info.endpoint = endpoint;
result.info.type = 'configured';
// Validate interval if present
Iif (health.interval !== undefined) {
if (typeof health.interval !== 'number' || health.interval <= 0) {
result.warnings.push('Health check interval should be a positive number');
} else {
result.info.interval = health.interval;
}
}
// Validate timeout if present
Iif (health.timeout !== undefined) {
if (typeof health.timeout !== 'number' || health.timeout <= 0) {
result.warnings.push('Health check timeout should be a positive number');
} else {
result.info.timeout = health.timeout;
}
}
// Validate retries if present
Iif (health.retries !== undefined) {
if (typeof health.retries !== 'number' || health.retries < 0) {
result.warnings.push('Health check retries should be a non-negative number');
} else {
result.info.retries = health.retries;
}
}
// Check for custom health checks
Iif (health.checks && Array.isArray(health.checks)) {
result.info.customChecks = health.checks.length;
health.checks.forEach((check, index) => {
if (!check.name) {
result.warnings.push(`Health check[${index}] should have a name`);
}
if (!check.type) {
result.warnings.push(`Health check[${index}] should have a type`);
}
});
}
// Validate expected response
Iif (health.expectedResponse) {
if (health.expectedResponse.status && typeof health.expectedResponse.status !== 'number') {
result.warnings.push('Expected response status should be a number');
}
result.info.expectedResponse = health.expectedResponse;
}
} else E{
result.valid = false;
result.errors.push('Health configuration must be a string or object');
}
// Additional recommendations
Iif (result.valid && !result.info.interval) {
result.warnings.push('Consider setting a health check interval');
}
Iif (result.valid && !result.info.timeout) {
result.warnings.push('Consider setting a health check timeout');
}
} catch (error) {
result.valid = false;
result.errors.push(`Health validation error: ${error.message}`);
}
return result;
}
/**
* Generate health check configuration
* @param {string} endpoint - Health endpoint path
* @param {object} options - Additional options
* @returns {object} Health configuration
*/
generateHealthConfig(endpoint, options = {}) {
return {
endpoint: endpoint || '/health',
interval: options.interval || 30000, // 30 seconds
timeout: options.timeout || 5000, // 5 seconds
retries: options.retries || 3,
expectedResponse: {
status: 200,
body: options.expectedBody || { status: 'ok' }
},
checks: options.checks || [
{
name: 'database',
type: 'connectivity',
critical: true
},
{
name: 'memory',
type: 'resource',
critical: false
}
]
};
}
/**
* Validate health response
* @param {object} response - Health check response
* @param {object} expected - Expected response configuration
* @returns {object} Validation result
*/
validateResponse(response, expected = {}) {
const result = {
valid: true,
errors: [],
warnings: []
};
// Check status code
if (expected.status) {
if (response.status !== expected.status) {
result.valid = false;
result.errors.push(`Expected status ${expected.status}, got ${response.status}`);
}
}
// Check response body
if (expected.body) {
if (!response.body) {
result.valid = false;
result.errors.push('Missing response body');
} else if (expected.body.status && response.body.status !== expected.body.status) {
result.valid = false;
result.errors.push(`Expected status "${expected.body.status}", got "${response.body.status}"`);
}
}
// Check response time
if (response.responseTime && expected.maxResponseTime) {
if (response.responseTime > expected.maxResponseTime) {
result.warnings.push(`Response time ${response.responseTime}ms exceeds recommended ${expected.maxResponseTime}ms`);
}
}
return result;
}
}
module.exports = HealthValidator; |