diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index cb5e416..2368994 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -14,6 +14,7 @@ export const makeMessagesSocket = (config: SocketConfig) => { logger, linkPreviewImageThumbnailWidth, generateHighQualityLinkPreview, + options: axiosOptions, patchMessageBeforeSending, } = config const sock = makeGroupsSocket(config) @@ -623,12 +624,15 @@ export const makeMessagesSocket = (config: SocketConfig) => { text, { thumbnailWidth: linkPreviewImageThumbnailWidth, - timeoutMs: 3_000, + fetchOpts: { + timeout: 3_000, + ...axiosOptions || { } + }, + logger, uploadImage: generateHighQualityLinkPreview ? waUploadToServer : undefined }, - logger ), upload: waUploadToServer, mediaCache: config.mediaCache, diff --git a/src/Utils/link-preview.ts b/src/Utils/link-preview.ts index 2e4971b..e7e1c14 100644 --- a/src/Utils/link-preview.ts +++ b/src/Utils/link-preview.ts @@ -6,16 +6,25 @@ 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, timeoutMs }: URLGenerationOptions) => { - const stream = await getHttpStream(url, { timeout: timeoutMs }) +const getCompressedJpegThumbnail = async( + url: string, + { thumbnailWidth, fetchOpts }: URLGenerationOptions +) => { + const stream = await getHttpStream(url, fetchOpts) const result = await extractImageThumb(stream, thumbnailWidth) return result } export type URLGenerationOptions = { thumbnailWidth: number - timeoutMs: number + fetchOpts: { + /** Timeout in ms */ + timeout: number + proxyUrl?: string + headers?: { [key: string]: string } + } uploadImage?: WAMediaUploadFunction + logger?: Logger } /** @@ -26,8 +35,10 @@ export type URLGenerationOptions = { */ export const getUrlInfo = async( text: string, - opts: URLGenerationOptions = { thumbnailWidth: THUMBNAIL_WIDTH_PX, timeoutMs: 3000 }, - logger?: Logger + opts: URLGenerationOptions = { + thumbnailWidth: THUMBNAIL_WIDTH_PX, + fetchOpts: { timeout: 3000 } + }, ): Promise => { try { const { getLinkPreview } = await import('link-preview-js') @@ -36,7 +47,7 @@ export const getUrlInfo = async( previewLink = 'https://' + previewLink } - const info = await getLinkPreview(previewLink, { timeout: opts.timeoutMs }) + const info = await getLinkPreview(previewLink, opts.fetchOpts) if(info && 'title' in info) { const [image] = info.images @@ -63,7 +74,7 @@ export const getUrlInfo = async( ? (await getCompressedJpegThumbnail(image, opts)).buffer : undefined } catch(error) { - logger?.debug( + opts.logger?.debug( { err: error.stack, url: previewLink }, 'error in generating thumbnail' )