import * as fs from 'fs'; import * as path from 'path'; import { fileURLToPath } from 'url'; import type { Certificates } from '../models/certificate-types.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); /** * Loads the default SSL certificates from the assets directory * @returns The certificate key pair */ export function loadDefaultCertificates(): Certificates { try { // Need to adjust path from /ts/certificate/utils to /assets/certs const certPath = path.join(__dirname, '..', '..', '..', 'assets', 'certs'); const privateKey = fs.readFileSync(path.join(certPath, 'key.pem'), 'utf8'); const publicKey = fs.readFileSync(path.join(certPath, 'cert.pem'), 'utf8'); if (!privateKey || !publicKey) { throw new Error('Failed to load default certificates'); } return { privateKey, publicKey }; } catch (error) { console.error('Error loading default certificates:', error); throw error; } } /** * Checks if a certificate file exists at the specified path * @param certPath Path to check for certificate * @returns True if the certificate exists, false otherwise */ export function certificateExists(certPath: string): boolean { return fs.existsSync(certPath); } /** * Ensures the certificate directory exists * @param dirPath Path to the certificate directory */ export function ensureCertificateDirectory(dirPath: string): void { if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }); } }