From 68a6f34cdea486613fcafa26ad77ebc097f5dd64 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Fri, 22 Apr 2022 20:05:17 +0530 Subject: [PATCH] feat: add timeoutMs to link preview --- src/Socket/messages-send.ts | 5 ++++- src/Utils/link-preview.ts | 11 ++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index bb254d3..463333a 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -485,7 +485,10 @@ export const makeMessagesSocket = (config: SocketConfig) => { logger, userJid, // multi-device does not have this yet - getUrlInfo: text => getUrlInfo(text, { thumbnailWidth: linkPreviewImageThumbnailWidth }), + getUrlInfo: text => getUrlInfo( + text, + { thumbnailWidth: linkPreviewImageThumbnailWidth, timeoutMs: 3_000 } + ), upload: waUploadToServer, mediaCache: config.mediaCache, ...options, diff --git a/src/Utils/link-preview.ts b/src/Utils/link-preview.ts index f84116d..2bc0205 100644 --- a/src/Utils/link-preview.ts +++ b/src/Utils/link-preview.ts @@ -4,14 +4,15 @@ import { extractImageThumb, getHttpStream } from './messages-media' const THUMBNAIL_WIDTH_PX = 192 /** Fetches an image and generates a thumbnail for it */ -const getCompressedJpegThumbnail = async(url: string, thumbnailWidth: number) => { - const stream = await getHttpStream(url) +const getCompressedJpegThumbnail = async(url: string, { thumbnailWidth, timeoutMs }: URLGenerationOptions) => { + const stream = await getHttpStream(url, { timeout: timeoutMs }) const result = await extractImageThumb(stream, thumbnailWidth) return result } export type URLGenerationOptions = { thumbnailWidth: number + timeoutMs: number } /** @@ -22,17 +23,17 @@ export type URLGenerationOptions = { */ export const getUrlInfo = async( text: string, - opts: URLGenerationOptions = { thumbnailWidth: THUMBNAIL_WIDTH_PX } + opts: URLGenerationOptions = { thumbnailWidth: THUMBNAIL_WIDTH_PX, timeoutMs: 3000 } ): Promise => { try { const { getLinkPreview } = await import('link-preview-js') - const info = await getLinkPreview(text) + const info = await getLinkPreview(text, { timeout: opts.timeoutMs }) if(info && 'title' in info) { const [image] = info.images const jpegThumbnail = image - ? await getCompressedJpegThumbnail(image, opts.thumbnailWidth) + ? await getCompressedJpegThumbnail(image, opts) : undefined return {