-
Notifications
You must be signed in to change notification settings - Fork 170
/
compress.ts
48 lines (42 loc) · 1.4 KB
/
compress.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
export const deno_x_brotli = 'https://deno.land/x/brotli@v0.1.4/mod.ts'
export const deno_x_flate = 'https://deno.land/x/denoflate@1.2.1/mod.ts'
class Compression {
#brotli: ((data: Uint8Array) => Uint8Array) | null = null
#gzip: ((data: Uint8Array) => Uint8Array) | null = null
enable: boolean = false
accept(acceptEncoding: string, contentType: string, contentLength: number): 'br' | 'gzip' | null {
const shouldCompress = this.enable && (
contentType.startsWith('text/') ||
contentType.startsWith('image/svg+xml') ||
/^application\/(javascript|json|xml|wasm)/i.test(contentType)
)
if (shouldCompress && contentLength > 1024) {
if (acceptEncoding.includes('br')) {
return 'br'
} else if (acceptEncoding.includes('gzip')) {
return 'gzip'
}
}
return null
}
async compress(
data: Uint8Array,
encoding: 'br' | 'gzip'
): Promise<Uint8Array> {
if (encoding === 'br') {
if (this.#brotli === null) {
const { compress } = await import(deno_x_brotli)
this.#brotli = compress
}
return this.#brotli!(data)
} else if (encoding === 'gzip') {
if (this.#gzip === null) {
const denoflate = await import(deno_x_flate)
this.#gzip = (data: Uint8Array) => denoflate.gzip(data, undefined)
}
return this.#gzip!(data)
}
return data
}
}
export default new Compression()