/**
* Truncates header metadata for a temporary solution with retry logic.
* @param {object} headers The headers object to truncate.
* @param {number} maxLength The maximum length of the header value.
* @param {number} maxRetries The maximum number of retry attempts.
* @returns {Promise<object>} A promise that resolves with the truncated headers object.
* Rejects if the truncation fails after maxRetries attempts.
*/
async function truncateHeaders(headers, maxLength, maxRetries) {
let retries = 0;
while (retries < maxRetries) {
try {
const truncatedHeaders = {};
for (const key in headers) {
if (headers.hasOwnProperty(key)) {
const value = headers[key];
if (typeof value === 'string' && value.length > maxLength) {
truncatedHeaders[key] = value.substring(0, maxLength);
} else {
truncatedHeaders[key] = value;
}
}
}
return truncatedHeaders; // Success!
} catch (error) {
retries++;
console.warn(`Truncation failed on attempt ${retries}: ${error.message}`);
// Optionally add a delay before retrying
await new Promise(resolve => setTimeout(resolve, 100)); // 100ms delay
}
}
throw new Error(`Truncation failed after ${maxRetries} attempts.`); // Failure after all retries
}
Add your comment