Follow redirects for soundcloud shortlinks (#8614)
* follow redirects for soundcloud shortlinks * clear timeout in `finally`
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import {BskyAgent} from '@atproto/api'
|
import {type BskyAgent} from '@atproto/api'
|
||||||
|
|
||||||
import {LINK_META_PROXY} from '#/lib/constants'
|
import {LINK_META_PROXY} from '#/lib/constants'
|
||||||
import {getGiphyMetaUri} from '#/lib/strings/embed-player'
|
import {getGiphyMetaUri} from '#/lib/strings/embed-player'
|
||||||
@@ -37,6 +37,7 @@ export async function getLinkMeta(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let urlp
|
let urlp
|
||||||
|
let shouldFollowRedirect = false
|
||||||
try {
|
try {
|
||||||
urlp = new URL(url)
|
urlp = new URL(url)
|
||||||
|
|
||||||
@@ -46,6 +47,9 @@ export async function getLinkMeta(
|
|||||||
url = giphyMetaUri
|
url = giphyMetaUri
|
||||||
urlp = new URL(url)
|
urlp = new URL(url)
|
||||||
}
|
}
|
||||||
|
// follow redirects for soundcloud shortlinks
|
||||||
|
// QUESTION - do we want to follow redirects in other cases? -sfn
|
||||||
|
shouldFollowRedirect = urlp.hostname === 'on.soundcloud.com'
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return {
|
return {
|
||||||
error: 'Invalid URL',
|
error: 'Invalid URL',
|
||||||
@@ -62,33 +66,35 @@ export async function getLinkMeta(
|
|||||||
return meta
|
return meta
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const controller = new AbortController()
|
||||||
const controller = new AbortController()
|
const to = setTimeout(() => controller.abort(), timeout || 5e3)
|
||||||
const to = setTimeout(() => controller.abort(), timeout || 5e3)
|
|
||||||
|
|
||||||
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${LINK_META_PROXY(agent.service.toString() || '')}${encodeURIComponent(
|
`${LINK_META_PROXY(agent.serviceUrl.toString() || '')}${encodeURIComponent(
|
||||||
url,
|
url,
|
||||||
)}`,
|
)}`,
|
||||||
{signal: controller.signal},
|
{signal: controller.signal},
|
||||||
)
|
)
|
||||||
|
|
||||||
const body = await response.json()
|
const body = await response.json()
|
||||||
clearTimeout(to)
|
|
||||||
|
|
||||||
const {description, error, image, title} = body
|
if (body.error !== '') {
|
||||||
|
throw new Error(body.error)
|
||||||
if (error !== '') {
|
|
||||||
throw new Error(error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
meta.description = description
|
meta.description = body.description
|
||||||
meta.image = image
|
meta.image = body.image
|
||||||
meta.title = title
|
meta.title = body.title
|
||||||
|
if (shouldFollowRedirect) {
|
||||||
|
meta.url = body.url
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// failed
|
// failed
|
||||||
console.error(e)
|
console.error(e)
|
||||||
meta.error = e instanceof Error ? e.toString() : 'Failed to fetch link'
|
meta.error = e instanceof Error ? e.toString() : 'Failed to fetch link'
|
||||||
|
} finally {
|
||||||
|
clearTimeout(to)
|
||||||
}
|
}
|
||||||
|
|
||||||
return meta
|
return meta
|
||||||
|
|||||||
Reference in New Issue
Block a user