This commit is contained in:
2025-05-21 18:52:04 +00:00
parent 645790d0c2
commit b6dd281a54
6 changed files with 446 additions and 43 deletions

View File

@ -21,13 +21,25 @@ export interface ICertificateData {
* @param str - Certificate string
* @returns Normalized certificate string
*/
function normalizeCertificate(str: string): string {
if (!str) {
function normalizeCertificate(str: string | Buffer): string {
// Handle different input types
let inputStr: string;
if (Buffer.isBuffer(str)) {
// Convert Buffer to string using utf8 encoding
inputStr = str.toString('utf8');
} else if (typeof str === 'string') {
inputStr = str;
} else {
throw new Error('Certificate must be a string or Buffer');
}
if (!inputStr) {
throw new Error('Empty certificate data');
}
// Remove any whitespace around the string
let normalizedStr = str.trim();
let normalizedStr = inputStr.trim();
// Make sure it has proper PEM format
if (!normalizedStr.includes('-----BEGIN ')) {
@ -72,18 +84,19 @@ function normalizeCertificate(str: string): string {
* @returns Certificate data with Buffer format
*/
export function loadCertificatesFromString(options: {
key: string;
cert: string;
ca?: string;
key: string | Buffer;
cert: string | Buffer;
ca?: string | Buffer;
}): ICertificateData {
try {
// Try to fix and normalize certificates
try {
// Normalize certificates (handles both string and Buffer inputs)
const key = normalizeCertificate(options.key);
const cert = normalizeCertificate(options.cert);
const ca = options.ca ? normalizeCertificate(options.ca) : undefined;
// Convert to Buffer with explicit utf8 encoding
// Convert normalized strings to Buffer with explicit utf8 encoding
const keyBuffer = Buffer.from(key, 'utf8');
const certBuffer = Buffer.from(cert, 'utf8');
const caBuffer = ca ? Buffer.from(ca, 'utf8') : undefined;