mirror of
https://github.com/wukko/cobalt.git
synced 2025-04-30 06:24:25 +02:00
api/utils: retry getting redirecting url with fetch() if request() fails
This commit is contained in:
parent
a6240d0192
commit
f5df78ffec
@ -2,16 +2,25 @@ import { request } from 'undici';
|
|||||||
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
|
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
|
||||||
|
|
||||||
export async function getRedirectingURL(url, dispatcher, headers) {
|
export async function getRedirectingURL(url, dispatcher, headers) {
|
||||||
const location = await request(url, {
|
const params = {
|
||||||
dispatcher,
|
dispatcher,
|
||||||
method: 'HEAD',
|
method: 'HEAD',
|
||||||
headers: headers
|
headers,
|
||||||
}).then(r => {
|
redirect: 'manual'
|
||||||
|
};
|
||||||
|
|
||||||
|
let location = await request(url, params).then(r => {
|
||||||
if (redirectStatuses.has(r.statusCode) && r.headers['location']) {
|
if (redirectStatuses.has(r.statusCode) && r.headers['location']) {
|
||||||
return r.headers['location'];
|
return r.headers['location'];
|
||||||
}
|
}
|
||||||
}).catch(() => null);
|
}).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;
|
return location;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user