1. /**
  2. * Truncates header metadata for a temporary solution with retry logic.
  3. * @param {object} headers The headers object to truncate.
  4. * @param {number} maxLength The maximum length of the header value.
  5. * @param {number} maxRetries The maximum number of retry attempts.
  6. * @returns {Promise<object>} A promise that resolves with the truncated headers object.
  7. * Rejects if the truncation fails after maxRetries attempts.
  8. */
  9. async function truncateHeaders(headers, maxLength, maxRetries) {
  10. let retries = 0;
  11. while (retries < maxRetries) {
  12. try {
  13. const truncatedHeaders = {};
  14. for (const key in headers) {
  15. if (headers.hasOwnProperty(key)) {
  16. const value = headers[key];
  17. if (typeof value === 'string' && value.length > maxLength) {
  18. truncatedHeaders[key] = value.substring(0, maxLength);
  19. } else {
  20. truncatedHeaders[key] = value;
  21. }
  22. }
  23. }
  24. return truncatedHeaders; // Success!
  25. } catch (error) {
  26. retries++;
  27. console.warn(`Truncation failed on attempt ${retries}: ${error.message}`);
  28. // Optionally add a delay before retrying
  29. await new Promise(resolve => setTimeout(resolve, 100)); // 100ms delay
  30. }
  31. }
  32. throw new Error(`Truncation failed after ${maxRetries} attempts.`); // Failure after all retries
  33. }

Add your comment