-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressors.ts
48 lines (45 loc) · 1.23 KB
/
compressors.ts
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
38
39
40
41
42
43
44
45
46
47
48
import zlib from "node:zlib";
const compressors: Record<
string,
{
compress(buffer: Buffer, options?: any): Buffer;
decompress(buffer: Buffer, options?: any): Buffer;
}
> = {
brotli: {
compress(buffer: Buffer, options?: zlib.BrotliOptions): Buffer {
options ??= {};
options.params ??= {};
options.params[zlib.constants.BROTLI_PARAM_QUALITY] ??= 9;
return zlib.brotliCompressSync(buffer, options);
},
decompress(buffer: Buffer, options?: zlib.BrotliOptions): Buffer {
return zlib.brotliDecompressSync(buffer, options);
},
},
gzip: {
compress(buffer: Buffer, options?: zlib.ZlibOptions): Buffer {
return zlib.gzipSync(buffer, options);
},
decompress(buffer: Buffer, options?: zlib.ZlibOptions): Buffer {
return zlib.gunzipSync(buffer, options);
},
},
flate: {
compress(buffer: Buffer, options?: zlib.ZlibOptions): Buffer {
return zlib.deflateSync(buffer, options);
},
decompress(buffer: Buffer, options?: zlib.ZlibOptions): Buffer {
return zlib.inflateSync(buffer, options);
},
},
none: {
compress(buffer: Buffer): Buffer {
return buffer;
},
decompress(buffer: Buffer): Buffer {
return buffer;
},
},
};
export default compressors;