From f5df78ffec4c4b5b37aa73a302bd0535719032f7 Mon Sep 17 00:00:00 2001 From: jj Date: Wed, 2 Apr 2025 12:29:18 +0000 Subject: [PATCH] api/utils: retry getting redirecting url with fetch() if request() fails --- api/src/misc/utils.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/api/src/misc/utils.js b/api/src/misc/utils.js index a7c523a4..62bf6351 100644 --- a/api/src/misc/utils.js +++ b/api/src/misc/utils.js @@ -2,16 +2,25 @@ import { request } from 'undici'; const redirectStatuses = new Set([301, 302, 303, 307, 308]); export async function getRedirectingURL(url, dispatcher, headers) { - const location = await request(url, { + const params = { dispatcher, method: 'HEAD', - headers: headers - }).then(r => { + headers, + redirect: 'manual' + }; + + let location = await request(url, params).then(r => { if (redirectStatuses.has(r.statusCode) && r.headers['location']) { return r.headers['location']; } }).catch(() => null); + location ??= await fetch(url, params).then(r => { + if (redirectStatuses.has(r.status) && r.headers.has('location')) { + return r.headers.get('location'); + } + }).catch(() => null); + return location; }