formacao
This commit is contained in:
+40
-28
@@ -1,4 +1,3 @@
|
||||
"use strict";
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
@@ -22,31 +21,31 @@ __export(cache_exports, {
|
||||
});
|
||||
module.exports = __toCommonJS(cache_exports);
|
||||
const defaultCacheableStatusCodes = [200];
|
||||
const shouldSkipCache = (res) => {
|
||||
const vary = res.headers.get("Vary");
|
||||
if (vary && vary.includes("*")) {
|
||||
return true;
|
||||
const shouldSkipCacheControl = (cacheControl) => !!cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl);
|
||||
const parseVaryDirectives = (vary) => {
|
||||
if (vary == null) {
|
||||
return [];
|
||||
}
|
||||
const cacheControl = res.headers.get("Cache-Control");
|
||||
if (cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl)) {
|
||||
return true;
|
||||
}
|
||||
if (res.headers.has("Set-Cookie")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return (Array.isArray(vary) ? vary : vary.split(",")).map((directive) => directive.trim().toLowerCase()).filter(Boolean);
|
||||
};
|
||||
const shouldSkipCache = (res, optionsVaryDirectives, responseVary) => responseVary.length && (!optionsVaryDirectives || responseVary.some((name) => !optionsVaryDirectives.has(name))) || shouldSkipCacheControl(res.headers.get("Cache-Control")) || res.headers.has("Set-Cookie");
|
||||
const cache = (options) => {
|
||||
if (!globalThis.caches) {
|
||||
console.log("Cache Middleware is not enabled because caches is not defined.");
|
||||
if (options.onCacheNotAvailable === false) {
|
||||
} else if (options.onCacheNotAvailable) {
|
||||
options.onCacheNotAvailable();
|
||||
} else {
|
||||
console.log("Cache Middleware is not enabled because caches is not defined.");
|
||||
}
|
||||
return async (_c, next) => await next();
|
||||
}
|
||||
if (options.wait === void 0) {
|
||||
options.wait = false;
|
||||
}
|
||||
const cacheControlDirectives = options.cacheControl?.split(",").map((directive) => directive.toLowerCase());
|
||||
const varyDirectives = Array.isArray(options.vary) ? options.vary : options.vary?.split(",").map((directive) => directive.trim());
|
||||
if (options.vary?.includes("*")) {
|
||||
const optionsVaryList = parseVaryDirectives(options.vary);
|
||||
const varyDirectives = optionsVaryList.length ? new Set(optionsVaryList) : void 0;
|
||||
if (varyDirectives?.has("*")) {
|
||||
throw new Error(
|
||||
'Middleware vary configuration cannot include "*", as it disallows effective caching.'
|
||||
);
|
||||
@@ -54,7 +53,7 @@ const cache = (options) => {
|
||||
const cacheableStatusCodes = new Set(
|
||||
options.cacheableStatusCodes ?? defaultCacheableStatusCodes
|
||||
);
|
||||
const addHeader = (c) => {
|
||||
const addHeader = (c, responseVary) => {
|
||||
if (cacheControlDirectives) {
|
||||
const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0]) ?? [];
|
||||
for (const directive of cacheControlDirectives) {
|
||||
@@ -66,24 +65,36 @@ const cache = (options) => {
|
||||
}
|
||||
}
|
||||
if (varyDirectives) {
|
||||
const existingDirectives = c.res.headers.get("Vary")?.split(",").map((d) => d.trim()) ?? [];
|
||||
const vary = Array.from(
|
||||
new Set(
|
||||
[...existingDirectives, ...varyDirectives].map((directive) => directive.toLowerCase())
|
||||
)
|
||||
).sort();
|
||||
if (vary.includes("*")) {
|
||||
c.header("Vary", "*");
|
||||
if (responseVary.length === 0) {
|
||||
c.header("Vary", Array.from(varyDirectives).join(", "));
|
||||
} else {
|
||||
c.header("Vary", vary.join(", "));
|
||||
const merged = new Set(varyDirectives);
|
||||
for (const directive of responseVary) {
|
||||
merged.add(directive);
|
||||
}
|
||||
if (merged.has("*")) {
|
||||
c.header("Vary", "*");
|
||||
} else {
|
||||
c.header("Vary", Array.from(merged).join(", "));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
return async function cache2(c, next) {
|
||||
if (c.req.method !== "GET" || c.req.raw.headers.has("Authorization")) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
let key = c.req.url;
|
||||
if (options.keyGenerator) {
|
||||
key = await options.keyGenerator(c);
|
||||
}
|
||||
if (varyDirectives) {
|
||||
for (const directive of varyDirectives) {
|
||||
const value = c.req.raw.headers.get(directive) ?? "";
|
||||
key += `::${directive}=${encodeURIComponent(value)}`;
|
||||
}
|
||||
}
|
||||
const cacheName = typeof options.cacheName === "function" ? await options.cacheName(c) : options.cacheName;
|
||||
const cache3 = await caches.open(cacheName);
|
||||
const response = await cache3.match(key);
|
||||
@@ -94,8 +105,9 @@ const cache = (options) => {
|
||||
if (!cacheableStatusCodes.has(c.res.status)) {
|
||||
return;
|
||||
}
|
||||
addHeader(c);
|
||||
if (shouldSkipCache(c.res)) {
|
||||
const responseVary = parseVaryDirectives(c.res.headers.get("Vary"));
|
||||
addHeader(c, responseVary);
|
||||
if (shouldSkipCache(c.res, varyDirectives, responseVary)) {
|
||||
return;
|
||||
}
|
||||
const res = c.res.clone();
|
||||
|
||||
Reference in New Issue
Block a user