blob: fd605cf71cac27f531f244eb5a735e6c29f97e25 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compress = void 0;
const compress = (options) => {
return async (ctx, next) => {
await next();
const accepted = ctx.req.headers.get('Accept-Encoding');
const pattern = options?.encoding ?? /gzip|deflate/;
const match = accepted?.match(pattern);
if (!accepted || !match || !ctx.res.body) {
return;
}
const encoding = match[0];
const stream = new CompressionStream(encoding);
ctx.res = new Response(ctx.res.body.pipeThrough(stream), ctx.res.clone());
ctx.res.headers.set('Content-Encoding', encoding);
};
};
exports.compress = compress;
|