All files / src/utils decompressBody.ts

93.75% Statements 15/16
100% Branches 10/10
100% Functions 1/1
93.75% Lines 15/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33      7x 1x     6x   6x 3x 3x     3x 1x   1x 1x     1x 1x       2x 1x     1x    
import { inflate, inflateRaw } from 'pako'
 
export function decompressBody(bodyBytes: ArrayBuffer, contentEncoding: string | null): string {
  if (!contentEncoding) {
    return new TextDecoder('utf-8').decode(bodyBytes)
  }
 
  const encoding = contentEncoding.trim().toLowerCase()
 
  if (encoding === 'gzip' || encoding === 'x-gzip') {
    const decompressed = inflate(new Uint8Array(bodyBytes))
    return new TextDecoder('utf-8').decode(decompressed)
  }
 
  if (encoding === 'deflate') {
    const compressed = new Uint8Array(bodyBytes)
 
    try {
      const decompressed = inflate(compressed)
      return new TextDecoder('utf-8').decode(decompressed)
    } catch {
      const decompressed = inflateRaw(compressed)
      return new TextDecoder('utf-8').decode(decompressed)
    }
  }
 
  if (encoding === 'identity') {
    return new TextDecoder('utf-8').decode(bodyBytes)
  }
 
  throw new Error(`Unsupported Content-Encoding: ${encoding}`)
}