mirror of
https://github.com/lone-cloud/prism
synced 2026-06-03 08:43:10 -07:00
38 lines
946 B
TypeScript
38 lines
946 B
TypeScript
export function maybeCompress(
|
|
request: Request,
|
|
body: string,
|
|
contentType = 'text/html; charset=utf-8',
|
|
): Response {
|
|
const acceptEncoding = request.headers.get('accept-encoding') || '';
|
|
const bodyBytes = new TextEncoder().encode(body);
|
|
|
|
if (bodyBytes.length < 1024) {
|
|
return new Response(body, {
|
|
headers: { 'Content-Type': contentType },
|
|
});
|
|
}
|
|
|
|
if (acceptEncoding.includes('gzip')) {
|
|
const compressed = Bun.gzipSync(bodyBytes);
|
|
return new Response(compressed, {
|
|
headers: {
|
|
'Content-Type': contentType,
|
|
'Content-Encoding': 'gzip',
|
|
},
|
|
});
|
|
}
|
|
|
|
if (acceptEncoding.includes('deflate')) {
|
|
const compressed = Bun.deflateSync(bodyBytes);
|
|
return new Response(compressed, {
|
|
headers: {
|
|
'Content-Type': contentType,
|
|
'Content-Encoding': 'deflate',
|
|
},
|
|
});
|
|
}
|
|
|
|
return new Response(body, {
|
|
headers: { 'Content-Type': contentType },
|
|
});
|
|
}
|