Guess I'll have to maintain a list after all :)

This commit is contained in:
flash 2024-10-28 19:42:15 +00:00
parent 3b674f5e28
commit bf70ad32a1

View file

@ -139,6 +139,15 @@ const readableStreamToString = async (stream?: ReadableStream): string => {
return result; return result;
}; };
const isDomainSuffix = (known: string, user: string) => {
if(!known.startsWith('.'))
known = '.' + known;
if(!user.startsWith('.'))
user = '.' + user;
return user.endsWith(known);
};
const isAllowedOEmbedDomain = (domain: string): Boolean => { const isAllowedOEmbedDomain = (domain: string): Boolean => {
if(!domain.startsWith('.')) if(!domain.startsWith('.'))
domain = '.' + domain; domain = '.' + domain;
@ -373,9 +382,13 @@ const parseLinkHeader = (header: string) => {
const extractOEmbedData = async (response: Response, html, url: string, urlInfo: URL) => { const extractOEmbedData = async (response: Response, html, url: string, urlInfo: URL) => {
let oEmbedUrl: string = ''; let oEmbedUrl: string = '';
// idk how long i'll bother with this for // TODO: maintain a fucking list because its too difficult for services to just provide <link rel=alternate> tags
if(urlInfo.host === 'x.com' || urlInfo.host === 'twitter.com') if(isDomainSuffix('x.com', urlInfo.host) || isDomainSuffix('twitter.com', urlInfo.host))
oEmbedUrl = `https://publish.twitter.com/oembed?dnt=true&omit_script=true&url=${encodeURIComponent(url)}`; oEmbedUrl = `https://publish.twitter.com/oembed?dnt=true&omit_script=true&url=${encodeURIComponent(url)}`;
else if(isDomainSuffix('soundcloud.com', urlInfo.host))
oEmbedUrl = `https://soundcloud.com/oembed?format=json&url=${encodeURIComponent(url)}`;
else if(isDomainSuffix('tiktok.com', urlInfo.host))
oEmbedUrl = `https://www.tiktok.com/oembed?url=${encodeURIComponent(url)}`;
else if(html !== undefined) else if(html !== undefined)
oEmbedUrl = html('link[rel="alternate"][type="application/json+oembed"]').first()?.attr('href')?.trim() ?? ''; oEmbedUrl = html('link[rel="alternate"][type="application/json+oembed"]').first()?.attr('href')?.trim() ?? '';
@ -717,7 +730,7 @@ const requestHandler = async (req: Request): Response => {
const urlHash = encodeBase64Url( const urlHash = encodeBase64Url(
await crypto.subtle.digest('SHA-256', new TextEncoder().encode(urlParamRaw)) await crypto.subtle.digest('SHA-256', new TextEncoder().encode(urlParamRaw))
); );
const cacheKey = `uiharu:metadata:fv${formatVersion}:${urlHash}`; const cacheKey = `uiharu:${appVersion}:md:fv${formatVersion}:${urlHash}`;
const cacheInfo = await cache.get(cacheKey); const cacheInfo = await cache.get(cacheKey);
if(cacheInfo !== undefined) if(cacheInfo !== undefined)
return new Response( return new Response(