api: support new xiaohongshu links, add fallbacks to getRedirectingURL

closes #1394
Co-authored-by: wukko <me@wukko.me>
This commit is contained in:
KwiatekMiki
2025-07-12 15:10:08 +02:00
committed by wukko
parent b9042a94e9
commit 3f785e7cbe
6 changed files with 33 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { request } from 'undici';
import { request } from "undici";
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
export async function getRedirectingURL(url, dispatcher, headers) {
@@ -8,18 +8,34 @@ export async function getRedirectingURL(url, dispatcher, headers) {
headers,
redirect: 'manual'
};
const getParams = {
...params,
method: 'GET',
};
let location = await request(url, params).then(r => {
const callback = (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);
/*
try request() with HEAD & GET,
then do the same with fetch
(fetch is required for shortened reddit links)
*/
let location = await request(url, params)
.then(callback).catch(() => null);
location ??= await request(url, getParams)
.then(callback).catch(() => null);
location ??= await fetch(url, params)
.then(callback).catch(() => null);
location ??= await fetch(url, getParams)
.then(callback).catch(() => null);
return location;
}