index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var cache_exports = {};
  19. __export(cache_exports, {
  20. cache: () => cache
  21. });
  22. module.exports = __toCommonJS(cache_exports);
  23. const defaultCacheableStatusCodes = [200];
  24. const shouldSkipCache = (res) => {
  25. const vary = res.headers.get("Vary");
  26. if (vary && vary.includes("*")) {
  27. return true;
  28. }
  29. const cacheControl = res.headers.get("Cache-Control");
  30. if (cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl)) {
  31. return true;
  32. }
  33. if (res.headers.has("Set-Cookie")) {
  34. return true;
  35. }
  36. return false;
  37. };
  38. const cache = (options) => {
  39. if (!globalThis.caches) {
  40. console.log("Cache Middleware is not enabled because caches is not defined.");
  41. return async (_c, next) => await next();
  42. }
  43. if (options.wait === void 0) {
  44. options.wait = false;
  45. }
  46. const cacheControlDirectives = options.cacheControl?.split(",").map((directive) => directive.toLowerCase());
  47. const varyDirectives = Array.isArray(options.vary) ? options.vary : options.vary?.split(",").map((directive) => directive.trim());
  48. if (options.vary?.includes("*")) {
  49. throw new Error(
  50. 'Middleware vary configuration cannot include "*", as it disallows effective caching.'
  51. );
  52. }
  53. const cacheableStatusCodes = new Set(
  54. options.cacheableStatusCodes ?? defaultCacheableStatusCodes
  55. );
  56. const addHeader = (c) => {
  57. if (cacheControlDirectives) {
  58. const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0]) ?? [];
  59. for (const directive of cacheControlDirectives) {
  60. let [name, value] = directive.trim().split("=", 2);
  61. name = name.toLowerCase();
  62. if (!existingDirectives.includes(name)) {
  63. c.header("Cache-Control", `${name}${value ? `=${value}` : ""}`, { append: true });
  64. }
  65. }
  66. }
  67. if (varyDirectives) {
  68. const existingDirectives = c.res.headers.get("Vary")?.split(",").map((d) => d.trim()) ?? [];
  69. const vary = Array.from(
  70. new Set(
  71. [...existingDirectives, ...varyDirectives].map((directive) => directive.toLowerCase())
  72. )
  73. ).sort();
  74. if (vary.includes("*")) {
  75. c.header("Vary", "*");
  76. } else {
  77. c.header("Vary", vary.join(", "));
  78. }
  79. }
  80. };
  81. return async function cache2(c, next) {
  82. let key = c.req.url;
  83. if (options.keyGenerator) {
  84. key = await options.keyGenerator(c);
  85. }
  86. const cacheName = typeof options.cacheName === "function" ? await options.cacheName(c) : options.cacheName;
  87. const cache3 = await caches.open(cacheName);
  88. const response = await cache3.match(key);
  89. if (response) {
  90. return new Response(response.body, response);
  91. }
  92. await next();
  93. if (!cacheableStatusCodes.has(c.res.status)) {
  94. return;
  95. }
  96. addHeader(c);
  97. if (shouldSkipCache(c.res)) {
  98. return;
  99. }
  100. const res = c.res.clone();
  101. if (options.wait) {
  102. await cache3.put(key, res);
  103. } else {
  104. c.executionCtx.waitUntil(cache3.put(key, res));
  105. }
  106. };
  107. };
  108. // Annotate the CommonJS export names for ESM import in node:
  109. 0 && (module.exports = {
  110. cache
  111. });