From 9f642a241673996cf486083f6d0f5f04e461e018 Mon Sep 17 00:00:00 2001 From: ashley Date: Tue, 26 Aug 2025 21:26:15 +0200 Subject: [PATCH] Update src/libpoketube/libpoketube-core.js --- src/libpoketube/libpoketube-core.js | 59 +++++++++++++++++++---------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/libpoketube/libpoketube-core.js b/src/libpoketube/libpoketube-core.js index acb2bf38..cd8c497e 100644 --- a/src/libpoketube/libpoketube-core.js +++ b/src/libpoketube/libpoketube-core.js @@ -56,30 +56,47 @@ class InnerTubePokeVidious { const headers = { "User-Agent": this.useragent, - }; + }; + const fetchWithRetry = async (url, options = {}, retries = 3) => { - for (let attempt = 0; attempt < retries; attempt++) { - try { - const res = await fetch(url, { - ...options, - headers: { - ...options.headers, - ...headers, - }, - }); - if (res.status === 500 && attempt < retries - 1) { - this.initError(`Retrying fetch for ${url}`, res.status); - continue; - } - return res; - } catch (err) { - this.initError(`Fetch error for ${url}`, err); - if (attempt === retries - 1) throw err; - } + let lastError; + + for (let attempt = 0; attempt < retries; attempt++) { + try { + const res = await fetch(url, { + ...options, + headers: { + ...options.headers, + ...headers, + }, + }); + + // If success, return immediately + if (res.ok) { + return res; } - return null; - }; + + // If error but retryable + if ((res.status >= 500 || res.status === 429) && attempt < retries - 1) { + this.initError(`Retrying fetch for ${url}`, res.status); + continue; + } + + // If non-retryable error, return response + return res; + } catch (err) { + lastError = err; + this.initError(`Fetch error for ${url}`, err); + + if (attempt < retries - 1) { + continue; + } else { + throw lastError; + } + } + } +}; try { const [invComments, videoInfo] = await Promise.all([