This commit is contained in:
2026-08-14 16:03:47 +01:00
parent fd53e3f4fb
commit dfdc3b2840
2463 changed files with 186456 additions and 111315 deletions
+35 -6
View File
@@ -1,4 +1,3 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -18,14 +17,43 @@ var __copyProps = (to, from, except, desc) => {
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var compress_exports = {};
__export(compress_exports, {
COMPRESSIBLE_CONTENT_TYPE_REGEX: () => import_compress.COMPRESSIBLE_CONTENT_TYPE_REGEX,
compress: () => compress
});
module.exports = __toCommonJS(compress_exports);
var import_accept = require("../../utils/accept");
var import_compress = require("../../utils/compress");
const ENCODING_TYPES = ["gzip", "deflate"];
const cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/i;
const selectEncoding = (header, candidates) => {
if (header === void 0) {
return void 0;
}
const accepts = (0, import_accept.parseAccept)(header);
const wildcardQ = accepts.find((a) => a.type === "*")?.q;
let best;
for (const enc of candidates) {
const explicit = accepts.find((a) => a.type.toLowerCase() === enc);
const q = explicit ? explicit.q : wildcardQ ?? 0;
if (q === 1) {
return enc;
} else if (q > 0 && (!best || q > best.q)) {
best = { encoding: enc, q };
}
}
return best?.encoding;
};
const compress = (options) => {
const threshold = options?.threshold ?? 1024;
const candidates = options?.encoding ? [options.encoding] : ENCODING_TYPES;
const contentTypeFilter = options?.contentTypeFilter ?? import_compress.COMPRESSIBLE_CONTENT_TYPE_REGEX;
const shouldCompress = typeof contentTypeFilter === "function" ? (res) => {
const type = res.headers.get("Content-Type");
return type && contentTypeFilter(type);
} : (res) => {
const type = res.headers.get("Content-Type");
return type && contentTypeFilter.test(type);
};
return async function compress2(ctx, next) {
await next();
const contentLength = ctx.res.headers.get("Content-Length");
@@ -38,7 +66,7 @@ const compress = (options) => {
return;
}
const accepted = ctx.req.header("Accept-Encoding");
const encoding = options?.encoding ?? ENCODING_TYPES.find((encoding2) => accepted?.includes(encoding2));
const encoding = selectEncoding(accepted, candidates);
if (!encoding || !ctx.res.body) {
return;
}
@@ -46,17 +74,18 @@ const compress = (options) => {
ctx.res = new Response(ctx.res.body.pipeThrough(stream), ctx.res);
ctx.res.headers.delete("Content-Length");
ctx.res.headers.set("Content-Encoding", encoding);
const etag = ctx.res.headers.get("ETag");
if (etag && !etag.startsWith("W/")) {
ctx.res.headers.set("ETag", `W/${etag}`);
}
};
};
const shouldCompress = (res) => {
const type = res.headers.get("Content-Type");
return type && import_compress.COMPRESSIBLE_CONTENT_TYPE_REGEX.test(type);
};
const shouldTransform = (res) => {
const cacheControl = res.headers.get("Cache-Control");
return !cacheControl || !cacheControlNoTransformRegExp.test(cacheControl);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
COMPRESSIBLE_CONTENT_TYPE_REGEX,
compress
});