1. /**
  2. * Paginate session cookies for dry-run scenarios with a timeout.
  3. *
  4. * @param {object} cookies - An object containing session cookies.
  5. * @param {number} pageSize - The number of cookies to display per page.
  6. * @param {number} [timeout=5000] - Timeout in milliseconds for the operation.
  7. * @returns {object} - An object containing the paginated cookies, total pages, and current page.
  8. */
  9. function paginateSessionCookies(cookies, pageSize, timeout = 5000) {
  10. const cookieArray = Object.values(cookies); // Extract cookie values
  11. const totalCookies = cookieArray.length;
  12. const totalPages = Math.ceil(totalCookies / pageSize);
  13. const currentPage = 1;
  14. // Simulate a timeout using setTimeout for dry-run purposes. Remove for actual execution.
  15. let timeoutId;
  16. // Safeguard against potential infinite loops if timeout is excessively long.
  17. const maxTimeout = 10000;
  18. if (timeout > maxTimeout) {
  19. timeout = maxTimeout;
  20. }
  21. const executeWithTimeout = (callback) => {
  22. timeoutId = setTimeout(callback, timeout);
  23. };
  24. //The main part of pagination logic
  25. const startIndex = (currentPage - 1) * pageSize;
  26. const endIndex = startIndex + pageSize;
  27. const pageCookies = cookieArray.slice(startIndex, endIndex);
  28. return {
  29. pageCookies: pageCookies,
  30. totalPages: totalPages,
  31. currentPage: currentPage,
  32. executeWithTimeout: executeWithTimeout, // Expose the timeout function for dry-run simulation.
  33. clearTimeout: () => clearTimeout(timeoutId) // Added a clear timeout function to prevent memory leaks.
  34. };
  35. }

Add your comment