Skip to content

Commit f5df78f

Browse files
committed
api/utils: retry getting redirecting url with fetch() if request() fails
1 parent a6240d0 commit f5df78f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

api/src/misc/utils.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ import { request } from 'undici';
22
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
33

44
export async function getRedirectingURL(url, dispatcher, headers) {
5-
const location = await request(url, {
5+
const params = {
66
dispatcher,
77
method: 'HEAD',
8-
headers: headers
9-
}).then(r => {
8+
headers,
9+
redirect: 'manual'
10+
};
11+
12+
let location = await request(url, params).then(r => {
1013
if (redirectStatuses.has(r.statusCode) && r.headers['location']) {
1114
return r.headers['location'];
1215
}
1316
}).catch(() => null);
1417

18+
location ??= await fetch(url, params).then(r => {
19+
if (redirectStatuses.has(r.status) && r.headers.has('location')) {
20+
return r.headers.get('location');
21+
}
22+
}).catch(() => null);
23+
1524
return location;
1625
}
1726

0 commit comments

Comments
 (0)