From 67339001a8fc3aa5ed68b58be69f6147cb33fc5a Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Mon, 20 Dec 2021 17:45:57 +0100 Subject: [PATCH] Fix getHtmlDocument for compressed documents --- ts/linkPreviews/linkPreviewFetch.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/ts/linkPreviews/linkPreviewFetch.ts b/ts/linkPreviews/linkPreviewFetch.ts index 87f15a127..de7476777 100644 --- a/ts/linkPreviews/linkPreviewFetch.ts +++ b/ts/linkPreviews/linkPreviewFetch.ts @@ -283,14 +283,12 @@ const parseHtmlBytes = ( const getHtmlDocument = async ( body: AsyncIterable, - contentLength: number, httpCharset: string | null, abortSignal: AbortSignal ): Promise => { let result: HTMLDocument = emptyHtmlDocument(); - const maxHtmlBytesToLoad = Math.min(contentLength, MAX_HTML_BYTES_TO_LOAD); - const buffer = new Uint8Array(maxHtmlBytesToLoad); + const buffer = new Uint8Array(MAX_HTML_BYTES_TO_LOAD); let bytesLoadedSoFar = 0; try { @@ -307,16 +305,13 @@ const getHtmlDocument = async ( chunk = Buffer.from(chunk, httpCharset || 'utf8'); } - const truncatedChunk = chunk.slice( - 0, - maxHtmlBytesToLoad - bytesLoadedSoFar - ); + const truncatedChunk = chunk.slice(0, buffer.length - bytesLoadedSoFar); buffer.set(truncatedChunk, bytesLoadedSoFar); bytesLoadedSoFar += truncatedChunk.byteLength; result = parseHtmlBytes(buffer.slice(0, bytesLoadedSoFar), httpCharset); - const hasLoadedMaxBytes = bytesLoadedSoFar >= maxHtmlBytesToLoad; + const hasLoadedMaxBytes = bytesLoadedSoFar >= buffer.length; if (hasLoadedMaxBytes) { break; } @@ -495,7 +490,6 @@ export async function fetchLinkPreviewMetadata( const document = await getHtmlDocument( response.body, - contentLength, contentType.charset, abortSignal );