diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-12-14 15:35:15 -0600 |
---|---|---|
committer | Paul Frazee <pfrazee@gmail.com> | 2022-12-14 15:35:15 -0600 |
commit | 4966b2152eb213bac34cbcb0ff01c246b7746f5c (patch) | |
tree | 5cc90408631c984018f1b5a4b06f428d3d31d4a5 /src/lib/link-meta.ts | |
parent | 345ec83f26e209929ca86b3885227e8508fb2cb8 (diff) | |
download | voidsky-4966b2152eb213bac34cbcb0ff01c246b7746f5c.tar.zst |
Add post embeds (images and external links)
Diffstat (limited to 'src/lib/link-meta.ts')
-rw-r--r-- | src/lib/link-meta.ts | 16 |
1 files changed, 13 insertions, 3 deletions
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<LinkMeta> { +export async function getLinkMeta( + url: string, + timeout = 5e3, +): Promise<LinkMeta> { if (isBskyAppUrl(url)) { // TODO this could be better url = convertBskyAppUrlIfNeeded(url) @@ -57,14 +61,20 @@ export async function getLinkMeta(url: string): Promise<LinkMeta> { } 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) |