From 78d081a3dee00930197d9bda919f65dcf05dddd6 Mon Sep 17 00:00:00 2001 From: "ruuuuu.de" Date: Tue, 12 May 2026 16:09:05 -0400 Subject: [PATCH] Revert "Revert "Switch GIF picker source from Klipy to Giphy"" --- __tests__/lib/string.test.ts | 26 ++-- assets/images/giphy_attribution_dark.png | Bin 0 -> 1418 bytes assets/images/giphy_attribution_light.png | Bin 0 -> 1570 bytes .../Post/Embed/ExternalEmbed/index.tsx | 3 +- .../gifPicker/components/GifPickerHeader.tsx | 87 +++++------ .../gifPicker/components/PoweredByGiphy.tsx | 32 ++++ src/features/gifPicker/giphy.ts | 141 ++++++++++++++++++ src/features/gifPicker/queries.ts | 74 ++------- src/lib/api/resolve.ts | 63 +++++--- src/lib/constants.ts | 7 + src/lib/media/manip.web.ts | 14 +- src/lib/strings/embed-player.ts | 25 +++- 12 files changed, 317 insertions(+), 155 deletions(-) create mode 100644 assets/images/giphy_attribution_dark.png create mode 100644 assets/images/giphy_attribution_light.png create mode 100644 src/features/gifPicker/components/PoweredByGiphy.tsx create mode 100644 src/features/gifPicker/giphy.ts diff --git a/__tests__/lib/string.test.ts b/__tests__/lib/string.test.ts index 412dd5a8f..49cfbd277 100644 --- a/__tests__/lib/string.test.ts +++ b/__tests__/lib/string.test.ts @@ -664,7 +664,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, undefined, undefined, @@ -674,7 +674,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/39248209509382934029', - playerUri: 'https://i.giphy.com/media/39248209509382934029/200.webp', + playerUri: 'https://i.giphy.com/media/39248209509382934029/200.mp4', }, { type: 'giphy_gif', @@ -682,7 +682,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, { type: 'giphy_gif', @@ -690,7 +690,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, { type: 'giphy_gif', @@ -698,7 +698,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, { type: 'giphy_gif', @@ -706,7 +706,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, { type: 'giphy_gif', @@ -714,7 +714,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, { type: 'giphy_gif', @@ -722,7 +722,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, undefined, undefined, @@ -734,7 +734,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, { @@ -743,7 +743,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, { type: 'giphy_gif', @@ -751,7 +751,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, { type: 'giphy_gif', @@ -759,7 +759,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, { type: 'giphy_gif', @@ -767,7 +767,7 @@ describe('parseEmbedPlayerFromUrl', () => { isGif: true, hideDetails: true, metaUri: 'https://giphy.com/gifs/gifId', - playerUri: 'https://i.giphy.com/media/gifId/200.webp', + playerUri: 'https://i.giphy.com/media/gifId/200.mp4', }, undefined, diff --git a/assets/images/giphy_attribution_dark.png b/assets/images/giphy_attribution_dark.png new file mode 100644 index 0000000000000000000000000000000000000000..f979e8bed55a6eda4f7dba98eeb4ce6297f4f837 GIT binary patch literal 1418 zcme7R@N?(olHy`uVBq!ia0y~yU^v0Rz#zuK#=yYPnsfOe0|NtRfk$L90|U1(2s1Lw znj^u$z#v)T8c`CQpH@@&hxzcVO=Q@$1IgP{6Q8FxYH^p$TzL3Q z(Y5ON_htv1PI`Ldp6}|){5auAx!xr|pM`JBKli*?)+ji!T`}|@~hRVNRT2@?WyT<&w>h-P7xiTBD+WcVoi`!5UTyp|bO+;rY@#;KKAdOV-FZGuIbpZ>TZbZF|DC5?T-Zf%z| z4@T|!bLw-B)}r!5Zz9(oJ?XDI@v-28lO7l62_62?{Bce0%bODp`A7u3t77u#wEcJ3 zrH99C)5axXsqqXCZJHGphQ|GvdvF5x8O?^0Op*G`Hl?TUv+X8)w6?kZ$Z1-i!0f0! z8w$>xRD3R*{DD2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tsx index dac340bcf..bda926d2b 100644 --- a/src/components/Post/Embed/ExternalEmbed/index.tsx +++ b/src/components/Post/Embed/ExternalEmbed/index.tsx @@ -70,7 +70,8 @@ export const ExternalEmbed = ({ if ( embedPlayerParams?.source === 'tenor' || - embedPlayerParams?.source === 'klipy' + embedPlayerParams?.source === 'klipy' || + embedPlayerParams?.source === 'giphy' ) { const parsedAlt = parseAltFromGIFDescription(link.description) return ( diff --git a/src/features/gifPicker/components/GifPickerHeader.tsx b/src/features/gifPicker/components/GifPickerHeader.tsx index ee50cdb8e..f3435c083 100644 --- a/src/features/gifPicker/components/GifPickerHeader.tsx +++ b/src/features/gifPicker/components/GifPickerHeader.tsx @@ -7,6 +7,7 @@ import {Button, ButtonIcon} from '#/components/Button' import * as TextField from '#/components/forms/TextField' import {MagnifyingGlass_Stroke2_Corner0_Rounded as Search} from '#/components/icons/MagnifyingGlass' import {TimesLarge_Stroke2_Corner0_Rounded as X} from '#/components/icons/Times' +import {PoweredByGiphy} from '#/features/gifPicker/components/PoweredByGiphy' export function GifPickerHeader({ inputRef, @@ -25,54 +26,48 @@ export function GifPickerHeader({ const t = useTheme() return ( - - - - { - if (nativeEvent.key === 'Escape') { - onEscape() - } - }} - /> - {canClear && ( - - )} - + 'Accessibility label for the GIF search input inside the GIF picker dialog.', + })} + placeholder={l({ + message: 'Search GIFs', + comment: 'Placeholder text inside the GIF search input.', + })} + onChangeText={onChangeText} + returnKeyType="search" + inputRef={inputRef} + maxLength={50} + onKeyPress={({nativeEvent}) => { + if (nativeEvent.key === 'Escape') { + onEscape() + } + }} + /> + {canClear && ( + + )} + + + ) } diff --git a/src/features/gifPicker/components/PoweredByGiphy.tsx b/src/features/gifPicker/components/PoweredByGiphy.tsx new file mode 100644 index 000000000..f9d521f2c --- /dev/null +++ b/src/features/gifPicker/components/PoweredByGiphy.tsx @@ -0,0 +1,32 @@ +import {Image} from 'expo-image' +import {useLingui} from '@lingui/react/macro' + +import {atoms as a, useTheme} from '#/alf' + +const ASSET_DARK = require('../../../../assets/images/giphy_attribution_dark.png') +const ASSET_LIGHT = require('../../../../assets/images/giphy_attribution_light.png') + +const HEIGHT = 22 +const ASPECT_RATIO = 200 / 22 + +export function PoweredByGiphy() { + const t = useTheme() + const {t: l} = useLingui() + const isLight = t.name === 'light' + return ( + + ) +} diff --git a/src/features/gifPicker/giphy.ts b/src/features/gifPicker/giphy.ts new file mode 100644 index 000000000..950e42d0a --- /dev/null +++ b/src/features/gifPicker/giphy.ts @@ -0,0 +1,141 @@ +import {getLocales} from 'expo-localization' + +import {type Gif} from '#/features/gifPicker/types' + +type GiphyImage = { + url: string + width: string + height: string + size?: string + mp4?: string + mp4_size?: string + webp?: string + webp_size?: string +} + +type GiphyGif = { + id: string + url: string + title: string + alt_text?: string + import_datetime?: string + images: { + original: GiphyImage + fixed_height_small?: GiphyImage + preview_gif?: GiphyImage + } +} + +type GiphyResponse = { + data: GiphyGif[] + pagination: { + total_count: number + count: number + offset: number + } +} + +export type GiphyEndpoint = 'trending' | 'search' + +export type GiphyApiInput = { + q?: string + offset?: number +} + +export type GiphyApiResult = { + next: number | undefined + results: Gif[] +} + +const LIMIT = 30 +const RATING = 'g' + +export function createGiphyApi( + urlBuilder: (params: string) => string, +): (input: GiphyApiInput) => Promise { + return async input => { + const params = new URLSearchParams() + params.set('limit', String(LIMIT)) + params.set('rating', RATING) + + const locale = getLocales?.()?.[0] + if (locale?.languageCode) { + params.set('lang', locale.languageCode.toLowerCase()) + } + + if (typeof input.offset === 'number' && input.offset > 0) { + params.set('offset', String(input.offset)) + } + if (input.q !== undefined) { + params.set('q', input.q) + } + + const res = await fetch(urlBuilder(params.toString()), { + method: 'GET', + }) + if (!res.ok) { + throw new Error(`Failed to fetch Giphy API (status ${res.status})`) + } + const body = (await res.json()) as GiphyResponse + const nextOffset = body.pagination.offset + body.pagination.count + return { + next: nextOffset < body.pagination.total_count ? nextOffset : undefined, + results: body.data.map(toGif), + } + } +} + +function toGif(g: GiphyGif): Gif { + const original = g.images.original + const preview = + g.images.preview_gif ?? g.images.fixed_height_small ?? original + const tinygif = + g.images.fixed_height_small ?? g.images.preview_gif ?? original + const altText = g.alt_text || g.title + return { + created: parseImportDate(g.import_datetime), + hasaudio: false, + id: g.id, + media_formats: { + gif: toMediaObject(original), + preview: toMediaObject(preview), + tinygif: toMediaObject(tinygif), + ...(original.mp4 + ? { + mp4: { + url: original.mp4, + dims: dims(original), + duration: 0, + size: parseInt(original.mp4_size ?? '0', 10) || 0, + }, + } + : null), + }, + tags: [], + title: g.title, + content_description: altText, + itemurl: g.url, + hascaption: false, + flags: '', + url: g.url, + } +} + +function toMediaObject(img: GiphyImage) { + return { + url: img.url, + dims: dims(img), + duration: 0, + size: parseInt(img.size ?? '0', 10) || 0, + } +} + +function dims(img: GiphyImage): [number, number] { + return [parseInt(img.width, 10) || 0, parseInt(img.height, 10) || 0] +} + +function parseImportDate(s: string | undefined): number { + if (!s) return 0 + const t = Date.parse(s.replace(' ', 'T') + 'Z') + return Number.isNaN(t) ? 0 : Math.floor(t / 1000) +} diff --git a/src/features/gifPicker/queries.ts b/src/features/gifPicker/queries.ts index b319493c3..bc8785a65 100644 --- a/src/features/gifPicker/queries.ts +++ b/src/features/gifPicker/queries.ts @@ -1,22 +1,20 @@ -import {Platform} from 'react-native' -import {getLocales} from 'expo-localization' import {useInfiniteQuery} from '@tanstack/react-query' -import {GIF_KLIPY_FEATURED, GIF_KLIPY_SEARCH} from '#/lib/constants' -import {type Gif} from '#/features/gifPicker/types' +import {GIF_GIPHY_SEARCH, GIF_GIPHY_TRENDING} from '#/lib/constants' +import {createGiphyApi} from '#/features/gifPicker/giphy' -export const RQKEY_ROOT = 'klipy-gif-service' -export const RQKEY_FEATURED = [RQKEY_ROOT, 'featured'] +export const RQKEY_ROOT = 'giphy-gif-service' +export const RQKEY_FEATURED = [RQKEY_ROOT, 'trending'] export const RQKEY_SEARCH = (query: string) => [RQKEY_ROOT, 'search', query] -const getTrendingGifs = createKlipyApi(GIF_KLIPY_FEATURED) -const searchGifs = createKlipyApi<{q: string}>(GIF_KLIPY_SEARCH) +const getTrendingGifs = createGiphyApi(GIF_GIPHY_TRENDING) +const searchGifs = createGiphyApi(GIF_GIPHY_SEARCH) export function useFeaturedGifsQuery(options?: {enabled?: boolean}) { return useInfiniteQuery({ queryKey: RQKEY_FEATURED, - queryFn: ({pageParam}) => getTrendingGifs({pos: pageParam}), - initialPageParam: undefined as string | undefined, + queryFn: ({pageParam}) => getTrendingGifs({offset: pageParam}), + initialPageParam: 0, getNextPageParam: lastPage => lastPage.next, enabled: options?.enabled, }) @@ -28,61 +26,9 @@ export function useGifSearchQuery( ) { return useInfiniteQuery({ queryKey: RQKEY_SEARCH(query), - queryFn: ({pageParam}) => searchGifs({q: query, pos: pageParam}), - initialPageParam: undefined as string | undefined, + queryFn: ({pageParam}) => searchGifs({q: query, offset: pageParam}), + initialPageParam: 0, getNextPageParam: lastPage => lastPage.next, enabled: !!query && options?.enabled !== false, }) } - -function createKlipyApi( - urlFn: (params: string) => string, -): (input: Input & {pos?: string}) => Promise<{ - next: string - results: Gif[] -}> { - return async input => { - const params = new URLSearchParams() - - params.set( - 'client_key', - Platform.select({ - ios: 'bluesky-ios', - android: 'bluesky-android', - default: 'bluesky-web', - }), - ) - - // 30 is divisible by 2 and 3, so both 2 and 3 column layouts can be used - params.set('limit', '30') - - params.set('contentfilter', 'high') - - const locale = getLocales?.()?.[0] - - if (locale?.regionCode) { - params.set('locale', locale.regionCode.toLowerCase()) - } - - for (const [key, value] of Object.entries(input)) { - if (value !== undefined) { - params.set(key, String(value)) - } - } - - const res = await fetch(urlFn(params.toString()), { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - }, - }) - if (!res.ok) { - throw new Error(`Failed to fetch KLIPY API (status ${res.status})`) - } - const body: {next: string; results: Gif[]} = await res.json() - return { - next: body.next, - results: body.results, - } - } -} diff --git a/src/lib/api/resolve.ts b/src/lib/api/resolve.ts index 7a9c778cb..8b72a15b9 100644 --- a/src/lib/api/resolve.ts +++ b/src/lib/api/resolve.ts @@ -192,33 +192,56 @@ export async function resolveGif( gif: Gif, ): Promise { const gifUrl = gif.media_formats.gif.url - const params = new URLSearchParams() - params.set('hh', String(gif.media_formats.gif.dims[1])) - params.set('ww', String(gif.media_formats.gif.dims[0])) - - // For Klipy GIFs, embed video format slugs so parseKlipyGif can - // swap to the right format per platform at render time. Klipy uses - // different filename slugs per format (unlike Tenor where format is - // encoded in the URL ID), so this info must travel with the URL. - try { - const url = new URL(gifUrl) - if (url.hostname === 'static.klipy.com') { - const mp4Slug = getFileSlug(gif.media_formats.mp4?.url) - const webmSlug = getFileSlug(gif.media_formats.webm?.url) - if (mp4Slug) params.set('mp4', mp4Slug) - if (webmSlug) params.set('webm', webmSlug) - } - } catch {} - - const uri = `${gifUrl}?${params.toString()}` + const uri = buildGifUri(gif, gifUrl) const altText = gif.content_description || gif.title return { type: 'external', uri, title: altText, description: createGIFDescription(altText), - thumb: await imageToThumb(gif.media_formats.preview.url), + thumb: await tryImageToThumb(gif.media_formats.preview.url), + } +} + +async function tryImageToThumb( + uri: string, +): Promise { + return await Promise.race([ + imageToThumb(uri), + new Promise(resolve => + setTimeout(() => resolve(undefined), 8e3), + ), + ]) +} + +function buildGifUri(gif: Gif, gifUrl: string): string { + let url: URL + try { + url = new URL(gifUrl) + } catch { + return gifUrl } + + if (url.hostname === 'static.klipy.com') { + url.searchParams.set('hh', String(gif.media_formats.gif.dims[1])) + url.searchParams.set('ww', String(gif.media_formats.gif.dims[0])) + const mp4Slug = getFileSlug(gif.media_formats.mp4?.url) + const webmSlug = getFileSlug(gif.media_formats.webm?.url) + if (mp4Slug) url.searchParams.set('mp4', mp4Slug) + if (webmSlug) url.searchParams.set('webm', webmSlug) + return url.toString() + } + + if (/(^|\.)giphy\.com$/.test(url.hostname)) { + const cleaned = new URL(`https://media.giphy.com${url.pathname}`) + cleaned.searchParams.set('hh', String(gif.media_formats.gif.dims[1])) + cleaned.searchParams.set('ww', String(gif.media_formats.gif.dims[0])) + return cleaned.toString() + } + + url.searchParams.set('hh', String(gif.media_formats.gif.dims[1])) + url.searchParams.set('ww', String(gif.media_formats.gif.dims[0])) + return url.toString() } function getFileSlug(url: string | undefined): string | undefined { diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 7ab9e5f32..88bffb072 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -165,6 +165,13 @@ export const GIF_KLIPY_SEARCH = (params: string) => export const GIF_KLIPY_FEATURED = (params: string) => `${GIF_SERVICE}/klipy/v2/featured?${params}` +export const GIF_GIPHY_SERVICE = 'https://gifs.blacksky.community' + +export const GIF_GIPHY_SEARCH = (params: string) => + `${GIF_GIPHY_SERVICE}/v1/gifs/search?${params}` +export const GIF_GIPHY_TRENDING = (params: string) => + `${GIF_GIPHY_SERVICE}/v1/gifs/trending?${params}` + export const MAX_LABELERS = 20 export const VIDEO_SERVICE = 'https://video.blacksky.community' diff --git a/src/lib/media/manip.web.ts b/src/lib/media/manip.web.ts index 8fa2b6a4f..7bb9b5dda 100644 --- a/src/lib/media/manip.web.ts +++ b/src/lib/media/manip.web.ts @@ -29,12 +29,14 @@ export interface DownloadAndResizeOpts { export async function downloadAndResize(opts: DownloadAndResizeOpts) { const controller = new AbortController() const to = setTimeout(() => controller.abort(), opts.timeout || 5e3) - const res = await fetch(opts.uri) - const resBody = await res.blob() - clearTimeout(to) - - const dataUri = await blobToDataUri(resBody) - return await doResize(dataUri, opts) + try { + const res = await fetch(opts.uri, {signal: controller.signal}) + const resBody = await res.blob() + const dataUri = await blobToDataUri(resBody) + return await doResize(dataUri, opts) + } finally { + clearTimeout(to) + } } export async function shareImageModal(_opts: {uri: string}) { diff --git a/src/lib/strings/embed-player.ts b/src/lib/strings/embed-player.ts index 48e9f1c8c..1aefa8528 100644 --- a/src/lib/strings/embed-player.ts +++ b/src/lib/strings/embed-player.ts @@ -317,7 +317,7 @@ export function parseEmbedPlayerFromUrl( isGif: true, hideDetails: true, metaUri: `https://giphy.com/gifs/${gifId}`, - playerUri: `https://i.giphy.com/media/${gifId}/200.webp`, + playerUri: `https://i.giphy.com/media/${gifId}/200.mp4`, } } } @@ -330,6 +330,8 @@ export function parseEmbedPlayerFromUrl( const [__, media, trackingOrId, idOrFilename, filename] = urlp.pathname.split('/') + const dimensions = readGifDimensions(urlp) + if (media === 'media') { if (idOrFilename && gifFilenameRegex.test(idOrFilename)) { return { @@ -338,7 +340,8 @@ export function parseEmbedPlayerFromUrl( isGif: true, hideDetails: true, metaUri: `https://giphy.com/gifs/${trackingOrId}`, - playerUri: `https://i.giphy.com/media/${trackingOrId}/200.webp`, + playerUri: `https://i.giphy.com/media/${trackingOrId}/200.mp4`, + dimensions, } } else if (filename && gifFilenameRegex.test(filename)) { return { @@ -347,7 +350,8 @@ export function parseEmbedPlayerFromUrl( isGif: true, hideDetails: true, metaUri: `https://giphy.com/gifs/${idOrFilename}`, - playerUri: `https://i.giphy.com/media/${idOrFilename}/200.webp`, + playerUri: `https://i.giphy.com/media/${idOrFilename}/200.mp4`, + dimensions, } } } @@ -366,7 +370,7 @@ export function parseEmbedPlayerFromUrl( isGif: true, hideDetails: true, metaUri: `https://giphy.com/gifs/${gifId}`, - playerUri: `https://i.giphy.com/media/${gifId}/200.webp`, + playerUri: `https://i.giphy.com/media/${gifId}/200.mp4`, } } else if (mediaOrFilename) { const gifId = mediaOrFilename.split('.')[0] @@ -378,7 +382,7 @@ export function parseEmbedPlayerFromUrl( metaUri: `https://giphy.com/gifs/${gifId}`, playerUri: `https://i.giphy.com/media/${ mediaOrFilename.split('.')[0] - }/200.webp`, + }/200.mp4`, } } } @@ -593,6 +597,17 @@ export function getGiphyMetaUri(url: URL) { } } +function readGifDimensions( + urlp: URL, +): {height: number; width: number} | undefined { + const h = parseInt(urlp.searchParams.get('hh') ?? '', 10) + const w = parseInt(urlp.searchParams.get('ww') ?? '', 10) + if (Number.isFinite(h) && Number.isFinite(w) && h > 0 && w > 0) { + return {height: h, width: w} + } + return undefined +} + export function parseTenorGif(urlp: URL): | {success: false} | { -- knotmirror 0.0.1