From 4966b2152eb213bac34cbcb0ff01c246b7746f5c Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Wed, 14 Dec 2022 15:35:15 -0600 Subject: Add post embeds (images and external links) --- src/lib/link-meta.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/lib/link-meta.ts') diff --git a/src/lib/link-meta.ts b/src/lib/link-meta.ts index c1739ae3b..53d5eed7c 100644 --- a/src/lib/link-meta.ts +++ b/src/lib/link-meta.ts @@ -22,9 +22,13 @@ export interface LinkMeta { url: string title?: string description?: string + image?: string } -export async function getLinkMeta(url: string): Promise { +export async function getLinkMeta( + url: string, + timeout = 5e3, +): Promise { if (isBskyAppUrl(url)) { // TODO this could be better url = convertBskyAppUrlIfNeeded(url) @@ -57,14 +61,20 @@ export async function getLinkMeta(url: string): Promise { } try { - const httpRes = await fetch(url) + const controller = new AbortController() + const to = setTimeout(() => controller.abort(), timeout || 5e3) + const httpRes = await fetch(url, { + headers: {accept: 'text/html'}, + signal: controller.signal, + }) const httpResBody = await httpRes.text() + clearTimeout(to) const httpResMeta = extractHtmlMeta(httpResBody) meta.title = httpResMeta.title ? he.decode(httpResMeta.title) : undefined meta.description = httpResMeta.description ? he.decode(httpResMeta.description) : undefined - // TODO meta.image = httpResMeta.image + meta.image = httpResMeta.image } catch (e) { // failed console.log(e) -- cgit 1.4.1