diff --git a/dashboard/src/api/logViewer.ts b/dashboard/src/api/logViewer.ts index 4b2d3a5c5..c06aebdd1 100644 --- a/dashboard/src/api/logViewer.ts +++ b/dashboard/src/api/logViewer.ts @@ -16,33 +16,29 @@ const STALE_DURATION_MS = minutesToMilliseconds(60); type FetchAndDecompressLogsResponse = { content: string; }; + +const isGzipBytes = (data: Uint8Array): boolean => + // eslint-disable-next-line no-magic-numbers + data.length >= 2 && data[0] === 0x1f && data[1] === 0x8b; + async function fetchAndDecompressLog( url: string, ): Promise { const proxyUrl = `/api/proxy/?url=${encodeURIComponent(url)}`; - const urlPathname = new URL(url).pathname; - const isGzipped = urlPathname.endsWith('.gz'); try { - if (isGzipped) { - const response = await RequestData.get(proxyUrl, { - responseType: 'arraybuffer', - }); - - const uint8ArrayResponse = new Uint8Array(response); - const decompressedData = pako.inflate(uint8ArrayResponse); - const textDecoder = new TextDecoder('utf-8'); - const decompressedText = textDecoder.decode(decompressedData); - - return { content: decompressedText }; - } else { - // For non-gzipped files, request as text - const response = await RequestData.get(proxyUrl, { - responseType: 'text', - }); - - return { content: response }; + const response = await RequestData.get(proxyUrl, { + responseType: 'arraybuffer', + }); + + let byteResponse = new Uint8Array(response); + const textDecoder = new TextDecoder('utf-8'); + + while (isGzipBytes(byteResponse)) { + byteResponse = pako.inflate(byteResponse); } + + return { content: textDecoder.decode(byteResponse) }; } catch (error) { console.error(error);