File size: 1,122 Bytes
5c5b371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
import util from "util";
import zlib from "zlib";
import { PassThrough } from "stream";

const BUFFER_DECODER_MAP = {
  gzip: util.promisify(zlib.gunzip),
  deflate: util.promisify(zlib.inflate),
  br: util.promisify(zlib.brotliDecompress),
  text: (data: Buffer) => data,
};

const STREAM_DECODER_MAP = {
  gzip: zlib.createGunzip,
  deflate: zlib.createInflate,
  br: zlib.createBrotliDecompress,
  text: () => new PassThrough(),
};

type SupportedContentEncoding = keyof typeof BUFFER_DECODER_MAP;
const isSupportedContentEncoding = (
  encoding: string
): encoding is SupportedContentEncoding => encoding in BUFFER_DECODER_MAP;

export async function decompressBuffer(buf: Buffer, encoding: string = "text") {
  if (isSupportedContentEncoding(encoding)) {
    return (await BUFFER_DECODER_MAP[encoding](buf)).toString();
  }
  throw new Error(`Unsupported content-encoding: ${encoding}`);
}

export function getStreamDecompressor(encoding: string = "text") {
  if (isSupportedContentEncoding(encoding)) {
    return STREAM_DECODER_MAP[encoding]();
  }
  throw new Error(`Unsupported content-encoding: ${encoding}`);
}