* Read and validate an SSL file (certificate, key, or CA) for SSL connections. * @param filePath - Path to the SSL file (PEM format) * @param label - Human-readable label for error messages (e.g. "CA certificate", "client certificate") * @returns Buffer containing the file data * @throws Error i
(filePath: string, label: string)
| 11 | * @throws Error if file doesn't exist, is empty, or cannot be read |
| 12 | */ |
| 13 | function readSSLFile(filePath: string, label: string): Buffer { |
| 14 | try { |
| 15 | // Check if file exists and is readable |
| 16 | if (!fs.existsSync(filePath)) { |
| 17 | throw new Error(`SSL ${label} file not found: ${filePath}`); |
| 18 | } |
| 19 | |
| 20 | // Read the file |
| 21 | const data = fs.readFileSync(filePath); |
| 22 | |
| 23 | // Basic validation - check it's not empty |
| 24 | if (data.length === 0) { |
| 25 | throw new Error(`SSL ${label} file is empty: ${filePath}`); |
| 26 | } |
| 27 | |
| 28 | return data; |
| 29 | } catch (error) { |
| 30 | if (error instanceof Error) { |
| 31 | // Re-throw our custom errors as-is |
| 32 | if (error.message.startsWith('SSL ')) { |
| 33 | throw error; |
| 34 | } |
| 35 | // Wrap other errors (like permission denied) |
| 36 | throw new Error(`Failed to read SSL ${label}: ${error.message}`); |
| 37 | } |
| 38 | throw error; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Read and validate CA certificate file for SSL connections. |
no outgoing calls
no test coverage detected