1. /**
  2. * Guards execution of URL lists for backward compatibility with rate limiting.
  3. *
  4. * @param {string[]} urls An array of URLs to execute.
  5. * @param {function} rateLimitFunction A function that checks if rate limiting allows execution.
  6. * Should return true if allowed, false otherwise.
  7. * @param {number} [limit=10] The rate limit.
  8. * @param {number} [interval=1000] The interval between requests (in milliseconds).
  9. * @returns {Promise<void>} A promise that resolves when all URLs have been executed or rejected if rate limiting fails.
  10. * @throws {Error} If urls is not an array or rateLimitFunction is not a function.
  11. */
  12. async function guardUrlExecution(urls, rateLimitFunction, limit = 10, interval = 1000) {
  13. if (!Array.isArray(urls)) {
  14. throw new Error("urls must be an array.");
  15. }
  16. if (typeof rateLimitFunction !== 'function') {
  17. throw new Error("rateLimitFunction must be a function.");
  18. }
  19. let executedCount = 0;
  20. for (const url of urls) {
  21. if (!rateLimitFunction(executedCount, limit, interval)) {
  22. console.log(`Rate limit exceeded. Skipping URL: ${url}`);
  23. return; // Stop execution if rate limit is exceeded
  24. }
  25. try {
  26. // Simulate URL execution (replace with your actual logic)
  27. await executeUrl(url); // Replace with your URL execution logic
  28. executedCount++;
  29. console.log(`Executed URL: ${url}`);
  30. } catch (error) {
  31. console.error(`Error executing URL ${url}:`, error);
  32. // Handle errors as needed (e.g., retry, log, etc.)
  33. }
  34. }
  35. }
  36. /**
  37. * Simulates executing a URL. Replace with your actual URL execution logic.
  38. * @param {string} url The URL to execute.
  39. * @returns {Promise<void>} A promise that resolves when the URL is executed.
  40. */
  41. async function executeUrl(url) {
  42. return new Promise((resolve) => {
  43. setTimeout(() => {
  44. resolve();
  45. }, 500); // Simulate some work
  46. });
  47. }

Add your comment