index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // src/middleware/cache/index.ts
  2. var defaultCacheableStatusCodes = [200];
  3. var shouldSkipCache = (res) => {
  4. const vary = res.headers.get("Vary");
  5. if (vary && vary.includes("*")) {
  6. return true;
  7. }
  8. const cacheControl = res.headers.get("Cache-Control");
  9. if (cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl)) {
  10. return true;
  11. }
  12. if (res.headers.has("Set-Cookie")) {
  13. return true;
  14. }
  15. return false;
  16. };
  17. var cache = (options) => {
  18. if (!globalThis.caches) {
  19. console.log("Cache Middleware is not enabled because caches is not defined.");
  20. return async (_c, next) => await next();
  21. }
  22. if (options.wait === void 0) {
  23. options.wait = false;
  24. }
  25. const cacheControlDirectives = options.cacheControl?.split(",").map((directive) => directive.toLowerCase());
  26. const varyDirectives = Array.isArray(options.vary) ? options.vary : options.vary?.split(",").map((directive) => directive.trim());
  27. if (options.vary?.includes("*")) {
  28. throw new Error(
  29. 'Middleware vary configuration cannot include "*", as it disallows effective caching.'
  30. );
  31. }
  32. const cacheableStatusCodes = new Set(
  33. options.cacheableStatusCodes ?? defaultCacheableStatusCodes
  34. );
  35. const addHeader = (c) => {
  36. if (cacheControlDirectives) {
  37. const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0]) ?? [];
  38. for (const directive of cacheControlDirectives) {
  39. let [name, value] = directive.trim().split("=", 2);
  40. name = name.toLowerCase();
  41. if (!existingDirectives.includes(name)) {
  42. c.header("Cache-Control", `${name}${value ? `=${value}` : ""}`, { append: true });
  43. }
  44. }
  45. }
  46. if (varyDirectives) {
  47. const existingDirectives = c.res.headers.get("Vary")?.split(",").map((d) => d.trim()) ?? [];
  48. const vary = Array.from(
  49. new Set(
  50. [...existingDirectives, ...varyDirectives].map((directive) => directive.toLowerCase())
  51. )
  52. ).sort();
  53. if (vary.includes("*")) {
  54. c.header("Vary", "*");
  55. } else {
  56. c.header("Vary", vary.join(", "));
  57. }
  58. }
  59. };
  60. return async function cache2(c, next) {
  61. let key = c.req.url;
  62. if (options.keyGenerator) {
  63. key = await options.keyGenerator(c);
  64. }
  65. const cacheName = typeof options.cacheName === "function" ? await options.cacheName(c) : options.cacheName;
  66. const cache3 = await caches.open(cacheName);
  67. const response = await cache3.match(key);
  68. if (response) {
  69. return new Response(response.body, response);
  70. }
  71. await next();
  72. if (!cacheableStatusCodes.has(c.res.status)) {
  73. return;
  74. }
  75. addHeader(c);
  76. if (shouldSkipCache(c.res)) {
  77. return;
  78. }
  79. const res = c.res.clone();
  80. if (options.wait) {
  81. await cache3.put(key, res);
  82. } else {
  83. c.executionCtx.waitUntil(cache3.put(key, res));
  84. }
  85. };
  86. };
  87. export {
  88. cache
  89. };