From df645b65e241d0bb84220b59bbe68a2ac226a9b1 Mon Sep 17 00:00:00 2001 From: Fioren <102145692+FiorenMas@users.noreply.github.com> Date: Fri, 7 Jun 2024 13:58:20 +0700 Subject: [PATCH] fix Cloudflare rate limiting --- lib/api.js | 10 ---------- lib/helpers.js | 50 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/api.js b/lib/api.js index 882ea1b5..5b87a0c6 100644 --- a/lib/api.js +++ b/lib/api.js @@ -49,11 +49,6 @@ export const createZeroTrustListsOneByOne = async (items) => { totalListNumber--; listNumber++; console.log(`Created "${listName}" list - ${totalListNumber} left`); - /* - Sleep for 4 seconds to work around an undocumented Cloudflare API rate limit. - This should be removed once the issue is fixed on their side. - */ - await new Promise((r) => setTimeout(r, 4 * 1000)); } catch (err) { console.error(`Could not create "${listName}" - ${err.toString()}`); throw err; @@ -111,11 +106,6 @@ export const deleteZeroTrustListsOneByOne = async (lists) => { await deleteZeroTrustList(id); totalListNumber--; console.log(`Deleted ${name} list - ${totalListNumber} left`); - /* - Sleep for 4 seconds to work around an undocumented Cloudflare API rate limit. - This should be removed once the issue is fixed on their side. - */ - await new Promise((r) => setTimeout(r, 4 * 1000)); } catch (err) { console.error(`Could not delete ${name} - ${err.toString()}`); throw err; diff --git a/lib/helpers.js b/lib/helpers.js index d9921096..ec7e30d2 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -96,23 +96,41 @@ const request = async (url, options) => { "X-Auth-Key": API_KEY, }; - const response = await fetch(url, { - ...options, - headers: { - ...options.headers, - ...headers, - }, - }); - - const data = await response.json(); - - if (!response.ok) { - // Send a message to the Discord webhook if it exists - await notifyWebhook(`An HTTP error has occurred (${response.status}) while making a web request. Please check the logs for further details.`); - throw new Error(`HTTP error! Status: ${response.status} - ${ 'errors' in data ? data.errors[0].message : JSON.stringify(data) }`); - } + /* + Due to the undocumented Cloudflare API rate limit, the request will be retried until Cloudflare accepts it. + */ + const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms)); + + let response; + let data; + let attempts = 0; - return data; + while(attempts < 50) { + try { + response = await fetch(url, { + ...options, + headers: { + ...options.headers, + ...headers, + }, + }); + + data = await response.json(); + + if (!response.ok) { + throw new Error('Response not OK'); + } + return data; + } catch (error) { + attempts++; + if(attempts >= 50) { + // Send a message to the Discord webhook if it exists + await notifyWebhook(`An HTTP error has occurred (${response.status}) while making a web request. Please check the logs for further details.`); + throw new Error(`HTTP error! Status: ${response.status} - ${ 'errors' in data ? data.errors[0].message : JSON.stringify(data) }`); + } + await wait(5000); + } + } }; /**