@@ -22,44 +22,53 @@ import {
22
22
ZstdCompressionStream ,
23
23
} from './streams'
24
24
25
+ function checkCompressEncodings ( encodings : CompressionEncoding [ ] ) {
26
+ const unsupportedEncoding : string | undefined = encodings . find (
27
+ ( enc ) => ! ACCEPTED_ENCODINGS . includes ( enc ) ,
28
+ )
29
+ if ( unsupportedEncoding ) {
30
+ throw new Error ( `Invalid compression encoding: ${ unsupportedEncoding } ` )
31
+ }
32
+ }
33
+
25
34
function checkResposeType ( c : Context ) {
26
- // skip no content
35
+ // NOTE: skip no content
27
36
if ( ! c . res . body ) {
28
37
throw Error
29
38
}
30
39
31
- // skip head request
40
+ // NOTE: skip head request
32
41
if ( c . req . method === 'HEAD' ) {
33
42
throw Error
34
43
}
35
44
}
36
45
37
46
function checkResponseCompressible ( c : Context , threshold : number ) {
38
- // skip already encoded
47
+ // NOTE: skip already encoded
39
48
if ( c . res . headers . has ( 'Content-Encoding' ) ) {
40
49
throw Error
41
50
}
42
51
43
52
const contentLength = Number ( c . res . headers . get ( 'Content-Length' ) )
44
53
45
- // skip small size content
54
+ // NOTE: skip small size content
46
55
if ( contentLength && contentLength < threshold ) {
47
56
throw Error
48
57
}
49
58
50
- // skip un-compressible content
59
+ // NOTE: skip un-compressible content
51
60
if ( ! shouldCompress ( c . res ) ) {
52
61
throw Error
53
62
}
54
63
55
- // skip un-transformable content
64
+ // NOTE: skip un-transformable content
56
65
if ( ! shouldTransform ( c . res ) ) {
57
66
throw Error
58
67
}
59
68
}
60
69
61
70
function checkResponseFilter ( c : Context , filter : CompressionFilter | null | undefined ) {
62
- // skip by filter callback result or already compressing runtimes
71
+ // NOTE: skip by callback result or if an already compressing runtime
63
72
if ( filter != null ) {
64
73
if ( ! filter ( c ) ) {
65
74
throw Error
@@ -75,7 +84,6 @@ function getAcceptedEncoding(c: Context, encodings: CompressionEncoding[]) {
75
84
if ( ! acceptedEncoding ) {
76
85
return
77
86
}
78
-
79
87
return encodings . find ( ( enc ) => acceptedEncoding . includes ( enc ) )
80
88
}
81
89
@@ -89,16 +97,19 @@ export function compress({
89
97
options = { } ,
90
98
filter,
91
99
} : CompressOptions = { } ) : MiddlewareHandler {
92
- // NOTE: uses `encoding` as the only compression scheme
100
+ // NOTE: use `encoding` as the only compression scheme
93
101
if ( encoding ) {
94
102
encodings = [ encoding ]
95
103
}
96
104
options = { ...options , level : zlibLevel }
97
105
106
+ // NOTE: fail if unsupported encodings
107
+ checkCompressEncodings ( encodings )
108
+
98
109
return async function compress ( c , next ) {
99
110
await next ( )
100
111
101
- // skip checks failed
112
+ // NOTE: skip if checks failed
102
113
try {
103
114
checkResposeType ( c )
104
115
checkResponseCompressible ( c , threshold )
@@ -109,7 +120,7 @@ export function compress({
109
120
110
121
const enc = getAcceptedEncoding ( c , encodings )
111
122
112
- // skip no accepted encoding
123
+ // NOTE: skip if no accepted encoding
113
124
if ( ! enc ) {
114
125
return
115
126
}
0 commit comments