generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression.js
More file actions
19 lines (18 loc) · 676 Bytes
/
compression.js
File metadata and controls
19 lines (18 loc) · 676 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
export function useCompression(format = 'deflate') {
return function(response, { request }) {
if (
response instanceof Response
&& response.body instanceof ReadableStream
&& ! response.headers.has('Content-Encoding')
&& response.headers.get('Content-Type').startsWith('text/')
&& request.headers.has('Accept-Encoding')
&& request.headers.get('Accept-Encoding').split(',').some(encoding => encoding.trim() === format)
) {
response.headers.set('Content-Encoding', format);
return new CompressionStream(format);
}
};
}
export const useGzip = useCompression('gzip');
export const useDeflate = useCompression('deflate');
export default useDeflate;