tricache 0.7.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +89 -1371
  3. package/dashboards/prometheus-scrape.yml +18 -0
  4. package/dashboards/tricache-alerts.yaml +54 -0
  5. package/dashboards/tricache-grafana.json +548 -0
  6. package/dist/cache-service-DVvroyqA.d.ts +886 -0
  7. package/dist/cache-service-KI5ACF3F.js +12 -0
  8. package/dist/chunk-7RV5ZVBB.js +1023 -0
  9. package/dist/chunk-BQRURGGD.js +60 -0
  10. package/dist/chunk-IITRFWHY.js +196 -0
  11. package/dist/{chunk-X5AXHPVD.js → chunk-LJ5QJB5O.js} +90 -5
  12. package/dist/{chunk-EUGYR7KR.js → chunk-PL5J3ZNT.js} +3184 -514
  13. package/dist/chunk-QNU7B43J.js +687 -0
  14. package/dist/chunk-UGD4VEPD.js +138 -0
  15. package/dist/chunk-XZL45ABR.js +108 -0
  16. package/dist/cli.cjs +3122 -495
  17. package/dist/cli.d.ts +1 -0
  18. package/dist/cli.js +121 -5
  19. package/dist/dashboard/index.cjs +1065 -0
  20. package/dist/dashboard/index.d.ts +183 -0
  21. package/dist/dashboard/index.js +22 -0
  22. package/dist/drizzle/index.cjs +2846 -494
  23. package/dist/drizzle/index.d.ts +3 -1
  24. package/dist/drizzle/index.js +1 -1
  25. package/dist/edge/index.cjs +1559 -0
  26. package/dist/edge/index.d.ts +127 -0
  27. package/dist/edge/index.js +1109 -0
  28. package/dist/hono/index.cjs +7760 -0
  29. package/dist/hono/index.d.ts +91 -0
  30. package/dist/hono/index.js +185 -0
  31. package/dist/hono-VA9A1m0I.d.ts +371 -0
  32. package/dist/http/index.cjs +3281 -587
  33. package/dist/http/index.d.ts +85 -23
  34. package/dist/http/index.js +176 -78
  35. package/dist/index.cjs +7212 -2505
  36. package/dist/index.d.ts +547 -30
  37. package/dist/index.js +104 -5
  38. package/dist/murmur3-CMGw9PbV.d.ts +98 -0
  39. package/dist/nestjs/index.cjs +2787 -487
  40. package/dist/nestjs/index.d.ts +3 -1
  41. package/dist/nestjs/index.js +6 -3
  42. package/dist/next/index.cjs +2804 -501
  43. package/dist/next/index.d.ts +4 -2
  44. package/dist/next/index.js +14 -10
  45. package/dist/prisma/index.d.ts +3 -1
  46. package/dist/serialize-worker.cjs +5 -4
  47. package/dist/serialize-worker.js +1 -1
  48. package/dist/{cache-service-D8tIXmzb.d.ts → types-B_QkVwJ7.d.ts} +622 -662
  49. package/dist/utils-CEsU_Z0s.d.ts +27 -0
  50. package/package.json +48 -10
  51. package/src/adapters/auto-pipeliner.ts +206 -0
  52. package/src/adapters/node-redis.ts +370 -0
  53. package/src/cache-service.ts +1619 -415
  54. package/src/cli.ts +137 -2
  55. package/src/codec.ts +72 -0
  56. package/src/cross-region.ts +245 -0
  57. package/src/dashboard/handler.ts +392 -0
  58. package/src/dashboard/html.ts +594 -0
  59. package/src/dashboard/index.ts +195 -0
  60. package/src/dashboard/types.ts +96 -0
  61. package/src/disk-tier.ts +449 -47
  62. package/src/drizzle/index.ts +86 -86
  63. package/src/edge/adapters/cloudflare-do.ts +130 -0
  64. package/src/edge/adapters/cloudflare-kv.ts +115 -0
  65. package/src/edge/adapters/upstash.ts +143 -0
  66. package/src/edge/cache.ts +797 -0
  67. package/src/edge/crypto.ts +182 -0
  68. package/src/edge/hono.ts +284 -0
  69. package/src/edge/index.ts +37 -0
  70. package/src/edge/types.ts +230 -0
  71. package/src/edge/utils/base64.ts +63 -0
  72. package/src/edge/utils/murmur3.ts +179 -0
  73. package/src/encryption.ts +127 -5
  74. package/src/hono/index.ts +316 -0
  75. package/src/http/express.ts +195 -0
  76. package/src/http/fastify.ts +260 -0
  77. package/src/http/index.ts +44 -258
  78. package/src/http/utils.ts +92 -0
  79. package/src/index.ts +149 -47
  80. package/src/ipc-telemetry.ts +509 -0
  81. package/src/latency-watchdog.ts +282 -0
  82. package/src/nestjs/decorators.ts +161 -161
  83. package/src/nestjs/index.ts +23 -23
  84. package/src/nestjs/tricache.module.ts +104 -104
  85. package/src/nestjs/tricache.store.ts +97 -97
  86. package/src/nestjs/types.ts +79 -79
  87. package/src/next/cache-handler.ts +12 -9
  88. package/src/next/index.ts +37 -37
  89. package/src/next/isr-handler.ts +105 -105
  90. package/src/next/types.ts +130 -130
  91. package/src/remote-snapshot.ts +153 -0
  92. package/src/serialize-worker.ts +120 -120
  93. package/src/sigv4-snapshot-adapter.ts +260 -0
  94. package/src/smart-memory-cache.ts +91 -8
  95. package/src/types.ts +315 -2
  96. package/src/utils/cgroup.ts +100 -0
  97. package/src/utils/tracing.ts +54 -0
  98. package/src/wasm/bloom-filter-wasm.ts +109 -102
  99. package/src/worker-pool.ts +299 -299
  100. package/src/wtiny-lfu.ts +624 -0
  101. package/dist/cache-service-5GATUYAK.js +0 -7
  102. package/dist/chunk-UBSR5UYA.js +0 -0
@@ -1,33 +1,95 @@
1
- import { W as WrapOptions, C as CacheService } from '../cache-service-D8tIXmzb.js';
1
+ import { K as KeyDerivationOptions } from '../utils-CEsU_Z0s.js';
2
+ export { b as buildDeterministicKey, g as generateETag, s as shouldSkipCache } from '../utils-CEsU_Z0s.js';
3
+ import { C as CacheService } from '../cache-service-DVvroyqA.js';
4
+ import { W as WrapOptions } from '../types-B_QkVwJ7.js';
5
+ export { C as CachedEdgeHttpResponse, H as HonoEdgeCacheOptions, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoCache } from '../hono-VA9A1m0I.js';
6
+ import 'crypto';
2
7
 
8
+ interface ExpressCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOptions {
9
+ /** TriCache instance. If omitted, lazily resolves the default singleton via CacheService.create(). */
10
+ cache?: CacheService;
11
+ /** Whether to generate and evaluate weak ETags. Default: true. */
12
+ etag?: boolean;
13
+ /** Custom predicate to skip caching dynamically for this request (e.g. authenticated sessions). */
14
+ skipCache?: (req: any) => boolean;
15
+ /** Dynamic tags derivation from request. */
16
+ tags?: string[] | ((req: any) => string[]);
17
+ }
18
+ interface CachedHttpResponse {
19
+ body: unknown;
20
+ contentType?: string;
21
+ etag?: string;
22
+ status: number;
23
+ }
3
24
  /**
4
- * tricache/http — Universal HTTP Caching, ETag generation, and 304 Not Modified middleware
5
- * for Express, Fastify, and Hono.
25
+ * Creates an Express / Connect middleware that provides deterministic response caching,
26
+ * weak ETag generation, conditional 304 Not Modified short-circuiting, and Cache-Control bypass controls.
6
27
  *
7
- * Usage with Express:
8
- * import { expressCache } from 'tricache/http';
9
- * app.get('/api/users', expressCache({ cache, ttl: 300, tags: ['users'] }), handler);
28
+ * @example
29
+ * import express from 'express';
30
+ * import { CacheService } from 'tricache';
31
+ * import { createExpressMiddleware } from 'tricache/http';
10
32
  *
11
- * Usage with Hono:
12
- * import { honoCache } from 'tricache/http';
13
- * app.get('/api/users', honoCache({ cache, ttl: 300, tags: ['users'] }), handler);
33
+ * const app = express();
34
+ * const cache = CacheService.create();
35
+ *
36
+ * app.get(
37
+ * '/api/products',
38
+ * createExpressMiddleware({
39
+ * cache,
40
+ * ttl: 120,
41
+ * tags: ['products'],
42
+ * headerWhitelist: ['accept-language'],
43
+ * }),
44
+ * async (req, res) => {
45
+ * const data = await getProducts();
46
+ * res.json(data);
47
+ * }
48
+ * );
14
49
  */
50
+ declare function createExpressMiddleware(options?: ExpressCacheOptions): (req: any, res: any, next: any) => Promise<any>;
51
+ /** Alias matching legacy nomenclature */
52
+ declare const expressCache: typeof createExpressMiddleware;
15
53
 
16
- interface HttpCacheOptions extends WrapOptions {
17
- /** TriCache instance. If omitted, uses singleton CacheService.create(). */
54
+ interface FastifyCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOptions {
55
+ /** TriCache instance. If omitted, lazily resolves the default singleton via CacheService.create(). */
18
56
  cache?: CacheService;
19
- /** Custom key derivation function. Defaults to `${method}:${url}`. */
20
- keyGenerator?: (req: any) => string;
21
- /** Whether to emit weak ETags (default: true). */
57
+ /** Whether to generate and evaluate weak ETags. Default: true. */
22
58
  etag?: boolean;
59
+ /** Custom predicate to skip caching dynamically for this request. */
60
+ skipCache?: (req: any) => boolean;
61
+ /** Dynamic tags derivation from request. */
62
+ tags?: string[] | ((req: any) => string[]);
63
+ }
64
+ interface CachedFastifyResponse {
65
+ body: unknown;
66
+ contentType?: string;
67
+ etag?: string;
68
+ status: number;
23
69
  }
24
- /** Compute a weak ETag from a string, Buffer, or object */
25
- declare function generateETag(data: unknown): string;
26
- /** Express / Connect middleware */
27
- declare function expressCache(options?: HttpCacheOptions): (req: any, res: any, next: any) => Promise<any>;
28
- /** Hono / Web Standard middleware */
29
- declare function honoCache(options?: HttpCacheOptions): (c: any, next: any) => Promise<any>;
30
- /** Fastify preHandler hook */
31
- declare function fastifyCache(options?: HttpCacheOptions): (req: any, reply: any) => Promise<any>;
70
+ /**
71
+ * Creates a Fastify plugin that registers `onRequest` and `onSend` lifecycle hooks.
72
+ *
73
+ * Designed to bypass Fastify's route encapsulation barrier (`skip-override`),
74
+ * allowing caching across global and nested route scopes.
75
+ *
76
+ * @example
77
+ * import Fastify from 'fastify';
78
+ * import { fastifyCachePlugin } from 'tricache/http';
79
+ *
80
+ * const app = Fastify();
81
+ * await app.register(fastifyCachePlugin, {
82
+ * ttl: 300,
83
+ * tags: ['api'],
84
+ * });
85
+ */
86
+ declare function createFastifyPlugin(options?: FastifyCacheOptions): (fastify: any, pluginOpts?: FastifyCacheOptions) => Promise<void>;
87
+ /** Standard Fastify plugin export */
88
+ declare const fastifyCachePlugin: (fastify: any, pluginOpts?: FastifyCacheOptions) => Promise<void>;
89
+ /**
90
+ * Universal Fastify middleware: functions both as a Fastify plugin (`fastify.register(fastifyCache(options))`)
91
+ * and as a route preHandler hook (`fastify.get('/route', { preHandler: fastifyCache(options) })`).
92
+ */
93
+ declare function fastifyCache(options?: FastifyCacheOptions): (first: any, second?: any) => Promise<any>;
32
94
 
33
- export { type HttpCacheOptions, expressCache, fastifyCache, generateETag, honoCache };
95
+ export { type CachedFastifyResponse, type CachedHttpResponse, type ExpressCacheOptions, type FastifyCacheOptions, type ExpressCacheOptions as HttpCacheOptions, KeyDerivationOptions, createExpressMiddleware, createFastifyPlugin, expressCache, fastifyCache, fastifyCachePlugin };
@@ -1,23 +1,41 @@
1
- // src/http/index.ts
2
- import crypto from "crypto";
3
- function generateETag(data) {
4
- const str = typeof data === "string" ? data : Buffer.isBuffer(data) ? data.toString("utf8") : JSON.stringify(data);
5
- const hash = crypto.createHash("sha1").update(str).digest("hex").slice(0, 16);
6
- return `W/"${str.length.toString(16)}-${hash}"`;
7
- }
8
- function expressCache(options = {}) {
9
- const { cache, keyGenerator, etag = true, ttl = 300, swr, tags } = options;
1
+ import {
2
+ buildDeterministicKey,
3
+ generateETag,
4
+ shouldSkipCache
5
+ } from "../chunk-BQRURGGD.js";
6
+ import {
7
+ buildEdgeDeterministicKey,
8
+ computeEdgeETag,
9
+ honoEdgeCache
10
+ } from "../chunk-IITRFWHY.js";
11
+
12
+ // src/http/express.ts
13
+ function createExpressMiddleware(options = {}) {
14
+ const {
15
+ cache,
16
+ etag = true,
17
+ ttl = 300,
18
+ swr,
19
+ tags,
20
+ skipCache,
21
+ keyGenerator,
22
+ headerWhitelist
23
+ } = options;
10
24
  return async (req, res, next) => {
11
25
  if (req.method !== "GET" && req.method !== "HEAD") {
12
26
  return next();
13
27
  }
28
+ if (shouldSkipCache(req, skipCache)) {
29
+ return next();
30
+ }
14
31
  let activeCache = cache;
15
32
  if (!activeCache) {
16
- const { CacheService } = await import("../cache-service-5GATUYAK.js");
33
+ const { CacheService } = await import("../cache-service-KI5ACF3F.js");
17
34
  activeCache = CacheService.create();
18
35
  }
19
- const key = keyGenerator ? keyGenerator(req) : `http:${req.method}:${req.originalUrl || req.url}`;
20
- const ifNoneMatch = req.headers ? req.headers["if-none-match"] : void 0;
36
+ const key = buildDeterministicKey(req, { keyGenerator, headerWhitelist });
37
+ const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
38
+ const resolvedTags = typeof tags === "function" ? tags(req) : tags;
21
39
  try {
22
40
  const cached = await activeCache.get(
23
41
  key,
@@ -26,37 +44,47 @@ function expressCache(options = {}) {
26
44
  const origJson = res.json?.bind(res);
27
45
  const origSend = res.send?.bind(res);
28
46
  const origEnd = res.end?.bind(res);
29
- const statusOf = () => res.statusCode;
47
+ const getStatusCode = () => res.statusCode ?? 200;
30
48
  if (origJson) {
31
49
  res.json = (body) => {
50
+ const status = getStatusCode();
32
51
  const bodyEtag = etag ? generateETag(body) : void 0;
33
- resolve({ body, contentType: "application/json; charset=utf-8", etag: bodyEtag, status: statusOf() });
34
- if (ifNoneMatch && ifNoneMatch === bodyEtag) {
52
+ const contentType = res.getHeader?.("content-type") || "application/json; charset=utf-8";
53
+ resolve({ body, contentType, etag: bodyEtag, status });
54
+ if (ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
35
55
  res.status?.(304);
36
56
  origEnd?.();
37
57
  return;
38
58
  }
39
- if (bodyEtag && res.setHeader) res.setHeader("ETag", bodyEtag);
59
+ if (bodyEtag && res.setHeader) {
60
+ res.setHeader("ETag", bodyEtag);
61
+ }
40
62
  return origJson(body);
41
63
  };
42
64
  }
43
65
  if (origSend) {
44
66
  res.send = (body) => {
67
+ const status = getStatusCode();
45
68
  const bodyEtag = etag ? generateETag(body) : void 0;
46
- resolve({ body, contentType: res.getHeader ? res.getHeader("content-type") : void 0, etag: bodyEtag, status: statusOf() });
47
- if (ifNoneMatch && ifNoneMatch === bodyEtag) {
69
+ const contentType = res.getHeader?.("content-type");
70
+ resolve({ body, contentType, etag: bodyEtag, status });
71
+ if (ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
48
72
  res.status?.(304);
49
73
  origEnd?.();
50
74
  return;
51
75
  }
52
- if (bodyEtag && res.setHeader) res.setHeader("ETag", bodyEtag);
76
+ if (bodyEtag && res.setHeader) {
77
+ res.setHeader("ETag", bodyEtag);
78
+ }
53
79
  return origSend(body);
54
80
  };
55
81
  }
56
82
  if (origEnd) {
57
83
  res.end = (body) => {
84
+ const status = getStatusCode();
58
85
  const bodyEtag = etag && body != null ? generateETag(body) : void 0;
59
- resolve({ body, contentType: res.getHeader ? res.getHeader("content-type") : void 0, etag: bodyEtag, status: statusOf() });
86
+ const contentType = res.getHeader?.("content-type");
87
+ resolve({ body, contentType, etag: bodyEtag, status });
60
88
  return origEnd(body);
61
89
  };
62
90
  }
@@ -64,10 +92,9 @@ function expressCache(options = {}) {
64
92
  });
65
93
  },
66
94
  ttl,
67
- { swr, tags }
95
+ { swr, tags: resolvedTags }
68
96
  );
69
- const respStatus = cached.status;
70
- if (typeof respStatus === "number" && (respStatus < 200 || respStatus >= 300)) {
97
+ if (typeof cached.status === "number" && (cached.status < 200 || cached.status >= 300)) {
71
98
  await activeCache.delete(key).catch(() => {
72
99
  });
73
100
  return;
@@ -82,10 +109,15 @@ function expressCache(options = {}) {
82
109
  if (cached.contentType && res.setHeader) {
83
110
  res.setHeader("Content-Type", cached.contentType);
84
111
  }
112
+ if (cached.status && res.status) {
113
+ res.status(cached.status);
114
+ }
85
115
  if (typeof cached.body === "object" && cached.body !== null && res.json) {
86
116
  return res.json(cached.body);
87
117
  } else if (res.send) {
88
118
  return res.send(cached.body);
119
+ } else if (res.end) {
120
+ return res.end(cached.body);
89
121
  }
90
122
  }
91
123
  } catch (err) {
@@ -93,67 +125,114 @@ function expressCache(options = {}) {
93
125
  }
94
126
  };
95
127
  }
96
- function honoCache(options = {}) {
97
- const { cache, keyGenerator, etag = true, ttl = 300, swr, tags } = options;
98
- return async (c, next) => {
99
- if (c.req.method !== "GET" && c.req.method !== "HEAD") {
100
- return await next();
101
- }
102
- let activeCache = cache;
128
+ var expressCache = createExpressMiddleware;
129
+
130
+ // src/http/fastify.ts
131
+ function createFastifyPlugin(options = {}) {
132
+ const plugin = async function(fastify, pluginOpts = {}) {
133
+ const mergedOpts = { ...options, ...pluginOpts };
134
+ const etag = mergedOpts.etag ?? true;
135
+ const ttl = mergedOpts.ttl ?? 300;
136
+ let activeCache = mergedOpts.cache;
103
137
  if (!activeCache) {
104
- const { CacheService } = await import("../cache-service-5GATUYAK.js");
138
+ const { CacheService } = await import("../cache-service-KI5ACF3F.js");
105
139
  activeCache = CacheService.create();
106
140
  }
107
- const key = keyGenerator ? keyGenerator(c.req) : `http:${c.req.method}:${c.req.url}`;
108
- const ifNoneMatch = c.req.header ? c.req.header("if-none-match") : void 0;
109
- const cached = await activeCache.get(
110
- key,
111
- async () => {
112
- await next();
113
- const res = c.res;
114
- const text = await res.clone().text();
115
- const bodyEtag = etag ? generateETag(text) : void 0;
116
- return {
117
- body: text,
118
- contentType: res.headers.get("content-type") ?? "text/plain",
119
- etag: bodyEtag,
120
- status: typeof res.status === "number" ? res.status : void 0
121
- };
122
- },
123
- ttl,
124
- { swr, tags }
125
- );
126
- if (typeof cached.status === "number" && (cached.status < 200 || cached.status >= 300)) {
127
- await activeCache.delete(key).catch(() => {
141
+ fastify.addHook("onRequest", async (req, reply) => {
142
+ if (req.method !== "GET" && req.method !== "HEAD") {
143
+ return;
144
+ }
145
+ if (shouldSkipCache(req, mergedOpts.skipCache)) {
146
+ return;
147
+ }
148
+ const key = buildDeterministicKey(req, {
149
+ keyGenerator: mergedOpts.keyGenerator,
150
+ headerWhitelist: mergedOpts.headerWhitelist
128
151
  });
129
- return;
130
- }
131
- if (cached.etag && ifNoneMatch === cached.etag) {
132
- return c.body(null, 304, { ETag: cached.etag });
133
- }
134
- const headers = {};
135
- if (cached.etag) headers["ETag"] = cached.etag;
136
- if (cached.contentType) headers["Content-Type"] = cached.contentType;
137
- return c.body(cached.body, 200, headers);
152
+ const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
153
+ const cached = await activeCache.get(
154
+ key,
155
+ async () => null,
156
+ 0
157
+ );
158
+ if (cached && typeof cached === "object" && cached.body !== null) {
159
+ if (cached.etag) {
160
+ reply.header("ETag", cached.etag);
161
+ if (ifNoneMatch === cached.etag) {
162
+ reply.code(304);
163
+ return reply.send();
164
+ }
165
+ }
166
+ if (cached.contentType) {
167
+ reply.header("Content-Type", cached.contentType);
168
+ }
169
+ reply.code(cached.status ?? 200);
170
+ return reply.send(cached.body);
171
+ }
172
+ req._tricacheKey = key;
173
+ const resolvedTags = typeof mergedOpts.tags === "function" ? mergedOpts.tags(req) : mergedOpts.tags;
174
+ req._tricacheTags = resolvedTags;
175
+ });
176
+ fastify.addHook("onSend", async (req, reply, payload) => {
177
+ const key = req._tricacheKey;
178
+ if (!key) {
179
+ return payload;
180
+ }
181
+ const statusCode = reply.statusCode ?? reply.raw?.statusCode ?? 200;
182
+ if (statusCode >= 200 && statusCode < 300 && payload !== void 0 && payload !== null) {
183
+ const payloadEtag = etag ? generateETag(payload) : void 0;
184
+ const contentType = reply.getHeader("content-type") || (typeof payload === "object" ? "application/json" : "text/plain");
185
+ if (payloadEtag) {
186
+ reply.header("ETag", payloadEtag);
187
+ }
188
+ const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
189
+ if (ifNoneMatch && payloadEtag && ifNoneMatch === payloadEtag) {
190
+ reply.code(304);
191
+ return "";
192
+ }
193
+ const entry = {
194
+ body: payload,
195
+ contentType,
196
+ etag: payloadEtag,
197
+ status: statusCode
198
+ };
199
+ void activeCache.set(key, entry, mergedOpts.ttl ?? ttl, void 0, {
200
+ tags: req._tricacheTags
201
+ });
202
+ }
203
+ return payload;
204
+ });
138
205
  };
206
+ plugin[/* @__PURE__ */ Symbol.for("skip-override")] = true;
207
+ return plugin;
139
208
  }
209
+ var fastifyCachePlugin = createFastifyPlugin();
140
210
  function fastifyCache(options = {}) {
141
- const { cache, keyGenerator, etag = true, ttl = 300, tags } = options;
142
- return async (req, reply) => {
211
+ const plugin = createFastifyPlugin(options);
212
+ const dualHandler = async function(first, second) {
213
+ if (first && typeof first.addHook === "function") {
214
+ return plugin(first, second);
215
+ }
216
+ const req = first;
217
+ const reply = second;
143
218
  if (req.method !== "GET" && req.method !== "HEAD") return;
144
- let activeCache = cache;
219
+ if (shouldSkipCache(req, options.skipCache)) return;
220
+ let activeCache = options.cache;
145
221
  if (!activeCache) {
146
- const { CacheService } = await import("../cache-service-5GATUYAK.js");
222
+ const { CacheService } = await import("../cache-service-KI5ACF3F.js");
147
223
  activeCache = CacheService.create();
148
224
  }
149
- const key = keyGenerator ? keyGenerator(req) : `http:${req.method}:${req.url}`;
150
- const ifNoneMatch = req.headers ? req.headers["if-none-match"] : void 0;
225
+ const key = buildDeterministicKey(req, {
226
+ keyGenerator: options.keyGenerator,
227
+ headerWhitelist: options.headerWhitelist
228
+ });
229
+ const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
151
230
  const cached = await activeCache.get(
152
231
  key,
153
232
  async () => null,
154
233
  0
155
234
  );
156
- if (cached) {
235
+ if (cached && typeof cached === "object" && cached.body !== null) {
157
236
  if (cached.etag) {
158
237
  if (reply.header) reply.header("ETag", cached.etag);
159
238
  if (ifNoneMatch === cached.etag) {
@@ -166,19 +245,29 @@ function fastifyCache(options = {}) {
166
245
  if (cached.contentType && reply.header) {
167
246
  reply.header("Content-Type", cached.contentType);
168
247
  }
248
+ if (cached.status) {
249
+ if (reply.code) reply.code(cached.status);
250
+ else if (reply.status) reply.status(cached.status);
251
+ }
169
252
  if (reply.send) return reply.send(cached.body);
170
253
  return;
171
254
  }
172
255
  const origSend = reply.send?.bind(reply);
173
256
  if (origSend) {
174
- reply.send = function(payload) {
175
- if (payload !== void 0 && payload !== null) {
176
- const payloadEtag = etag ? generateETag(payload) : void 0;
177
- const contentType = reply.getHeader && reply.getHeader("content-type") || (typeof payload === "object" ? "application/json" : "text/plain");
178
- const respStatus = reply.statusCode ?? reply.raw?.statusCode;
179
- if (respStatus === void 0 || respStatus >= 200 && respStatus < 300) {
180
- activeCache.set(key, { body: payload, contentType, etag: payloadEtag }, ttl, void 0, { tags }).catch(() => {
181
- });
257
+ reply.send = function(resPayload) {
258
+ if (resPayload !== void 0 && resPayload !== null) {
259
+ const payloadEtag = options.etag !== false ? generateETag(resPayload) : void 0;
260
+ const contentType = reply.getHeader && reply.getHeader("content-type") || (typeof resPayload === "object" ? "application/json" : "text/plain");
261
+ const respStatus = reply.statusCode ?? reply.raw?.statusCode ?? 200;
262
+ if (respStatus >= 200 && respStatus < 300) {
263
+ const resolvedTags = typeof options.tags === "function" ? options.tags(req) : options.tags;
264
+ void activeCache.set(
265
+ key,
266
+ { body: resPayload, contentType, etag: payloadEtag, status: respStatus },
267
+ options.ttl ?? 300,
268
+ void 0,
269
+ { tags: resolvedTags }
270
+ );
182
271
  }
183
272
  if (ifNoneMatch && ifNoneMatch === payloadEtag) {
184
273
  if (reply.code) reply.code(304);
@@ -187,14 +276,23 @@ function fastifyCache(options = {}) {
187
276
  }
188
277
  if (payloadEtag && reply.header) reply.header("ETag", payloadEtag);
189
278
  }
190
- return origSend(payload);
279
+ return origSend(resPayload);
191
280
  };
192
281
  }
193
282
  };
283
+ dualHandler[/* @__PURE__ */ Symbol.for("skip-override")] = true;
284
+ return dualHandler;
194
285
  }
195
286
  export {
287
+ buildDeterministicKey,
288
+ buildEdgeDeterministicKey,
289
+ computeEdgeETag,
290
+ createExpressMiddleware,
291
+ createFastifyPlugin,
196
292
  expressCache,
197
293
  fastifyCache,
294
+ fastifyCachePlugin,
198
295
  generateETag,
199
- honoCache
296
+ honoEdgeCache as honoCache,
297
+ shouldSkipCache
200
298
  };