tricache 0.7.0 → 0.8.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.
- package/LICENSE +21 -21
- package/README.md +144 -1428
- package/bin/tricache.js +8 -8
- package/dashboards/tricache-alerts.yaml +54 -0
- package/dashboards/tricache-grafana.json +548 -0
- package/dist/cache-service-5MZXFIGK.js +12 -0
- package/dist/cache-service-CyLdpo77.d.ts +871 -0
- package/dist/{chunk-3XQUQBOZ.js → chunk-6PQ5MOWR.js} +2950 -523
- package/dist/chunk-7RV5ZVBB.js +1023 -0
- package/dist/{chunk-PFB5FMEY.js → chunk-LJ5QJB5O.js} +116 -20
- package/dist/chunk-QNU7B43J.js +687 -0
- package/dist/chunk-UGD4VEPD.js +138 -0
- package/dist/chunk-XZL45ABR.js +108 -0
- package/dist/chunk-ZTSWCFYO.js +135 -0
- package/dist/cli.cjs +2894 -518
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +103 -5
- package/dist/dashboard/index.cjs +1065 -0
- package/dist/dashboard/index.d.ts +183 -0
- package/dist/dashboard/index.js +22 -0
- package/dist/drizzle/index.cjs +2648 -508
- package/dist/drizzle/index.d.ts +3 -1
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +1498 -0
- package/dist/edge/index.d.ts +127 -0
- package/dist/edge/index.js +1109 -0
- package/dist/hono-DQRS1Xd7.d.ts +370 -0
- package/dist/http/index.cjs +3038 -589
- package/dist/http/index.d.ts +109 -23
- package/dist/http/index.js +238 -62
- package/dist/index.cjs +6962 -2487
- package/dist/index.d.ts +547 -30
- package/dist/index.js +104 -5
- package/dist/murmur3-CMGw9PbV.d.ts +98 -0
- package/dist/nestjs/index.cjs +2586 -498
- package/dist/nestjs/index.d.ts +3 -1
- package/dist/nestjs/index.js +6 -3
- package/dist/next/index.cjs +2633 -519
- package/dist/next/index.d.ts +13 -7
- package/dist/next/index.js +45 -18
- package/dist/prisma/index.cjs +2 -1
- package/dist/prisma/index.d.ts +3 -1
- package/dist/prisma/index.js +2 -1
- package/dist/serialize-worker.cjs +31 -19
- package/dist/serialize-worker.js +1 -1
- package/dist/{cache-service-BblHuF3i.d.ts → types-B6krw64X.d.ts} +605 -646
- package/package.json +37 -7
- package/src/adapters/auto-pipeliner.ts +206 -0
- package/src/adapters/node-redis.ts +370 -0
- package/src/cache-service.ts +4231 -3226
- package/src/cli.ts +261 -143
- package/src/codec.ts +72 -0
- package/src/compression.ts +56 -63
- package/src/cross-region.ts +245 -0
- package/src/dashboard/handler.ts +392 -0
- package/src/dashboard/html.ts +594 -0
- package/src/dashboard/index.ts +195 -0
- package/src/dashboard/types.ts +96 -0
- package/src/disk-tier.ts +1223 -821
- package/src/drizzle/index.ts +86 -86
- package/src/edge/adapters/cloudflare-do.ts +130 -0
- package/src/edge/adapters/cloudflare-kv.ts +115 -0
- package/src/edge/adapters/upstash.ts +143 -0
- package/src/edge/cache.ts +797 -0
- package/src/edge/crypto.ts +182 -0
- package/src/edge/hono.ts +203 -0
- package/src/edge/index.ts +37 -0
- package/src/edge/types.ts +230 -0
- package/src/edge/utils/base64.ts +63 -0
- package/src/edge/utils/murmur3.ts +179 -0
- package/src/encryption.ts +635 -493
- package/src/http/express.ts +195 -0
- package/src/http/fastify.ts +260 -0
- package/src/http/index.ts +46 -223
- package/src/http/utils.ts +92 -0
- package/src/index.ts +149 -47
- package/src/ipc-telemetry.ts +481 -0
- package/src/latency-watchdog.ts +282 -0
- package/src/nestjs/decorators.ts +161 -161
- package/src/nestjs/index.ts +23 -23
- package/src/nestjs/tricache.module.ts +104 -104
- package/src/nestjs/tricache.store.ts +97 -97
- package/src/nestjs/types.ts +79 -79
- package/src/next/cache-handler.ts +271 -244
- package/src/next/index.ts +37 -37
- package/src/next/isr-handler.ts +105 -105
- package/src/next/types.ts +130 -130
- package/src/prisma/index.ts +154 -151
- package/src/remote-snapshot.ts +153 -0
- package/src/serialize-worker.ts +120 -120
- package/src/sigv4-snapshot-adapter.ts +260 -0
- package/src/smart-memory-cache.ts +1109 -1024
- package/src/types.ts +1199 -902
- package/src/utils/cgroup.ts +100 -0
- package/src/utils/tracing.ts +54 -0
- package/src/wasm/bloom-filter-wasm.ts +109 -102
- package/src/worker-pool.ts +299 -299
- package/src/wtiny-lfu.ts +624 -0
- package/dist/cache-service-AJASH5FX.js +0 -7
- package/dist/chunk-UBSR5UYA.js +0 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import type { CacheService } from '../cache-service.js';
|
|
2
|
+
import type { WrapOptions } from '../types.js';
|
|
3
|
+
import {
|
|
4
|
+
buildDeterministicKey,
|
|
5
|
+
generateETag,
|
|
6
|
+
shouldSkipCache,
|
|
7
|
+
type KeyDerivationOptions,
|
|
8
|
+
} from './utils.js';
|
|
9
|
+
|
|
10
|
+
export interface ExpressCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOptions {
|
|
11
|
+
/** TriCache instance. If omitted, lazily resolves the default singleton via CacheService.create(). */
|
|
12
|
+
cache?: CacheService;
|
|
13
|
+
/** Whether to generate and evaluate weak ETags. Default: true. */
|
|
14
|
+
etag?: boolean;
|
|
15
|
+
/** Custom predicate to skip caching dynamically for this request (e.g. authenticated sessions). */
|
|
16
|
+
skipCache?: (req: any) => boolean;
|
|
17
|
+
/** Dynamic tags derivation from request. */
|
|
18
|
+
tags?: string[] | ((req: any) => string[]);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface CachedHttpResponse {
|
|
22
|
+
body: unknown;
|
|
23
|
+
contentType?: string;
|
|
24
|
+
etag?: string;
|
|
25
|
+
status: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Creates an Express / Connect middleware that provides deterministic response caching,
|
|
30
|
+
* weak ETag generation, conditional 304 Not Modified short-circuiting, and Cache-Control bypass controls.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* import express from 'express';
|
|
34
|
+
* import { CacheService } from 'tricache';
|
|
35
|
+
* import { createExpressMiddleware } from 'tricache/http';
|
|
36
|
+
*
|
|
37
|
+
* const app = express();
|
|
38
|
+
* const cache = CacheService.create();
|
|
39
|
+
*
|
|
40
|
+
* app.get(
|
|
41
|
+
* '/api/products',
|
|
42
|
+
* createExpressMiddleware({
|
|
43
|
+
* cache,
|
|
44
|
+
* ttl: 120,
|
|
45
|
+
* tags: ['products'],
|
|
46
|
+
* headerWhitelist: ['accept-language'],
|
|
47
|
+
* }),
|
|
48
|
+
* async (req, res) => {
|
|
49
|
+
* const data = await getProducts();
|
|
50
|
+
* res.json(data);
|
|
51
|
+
* }
|
|
52
|
+
* );
|
|
53
|
+
*/
|
|
54
|
+
export function createExpressMiddleware(options: ExpressCacheOptions = {}) {
|
|
55
|
+
const {
|
|
56
|
+
cache,
|
|
57
|
+
etag = true,
|
|
58
|
+
ttl = 300,
|
|
59
|
+
swr,
|
|
60
|
+
tags,
|
|
61
|
+
skipCache,
|
|
62
|
+
keyGenerator,
|
|
63
|
+
headerWhitelist,
|
|
64
|
+
} = options;
|
|
65
|
+
|
|
66
|
+
return async (req: any, res: any, next: any) => {
|
|
67
|
+
// Only cache safe, idempotent HTTP read methods
|
|
68
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
69
|
+
return next();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (shouldSkipCache(req, skipCache)) {
|
|
73
|
+
return next();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let activeCache = cache;
|
|
77
|
+
if (!activeCache) {
|
|
78
|
+
const { CacheService } = await import('../cache-service.js');
|
|
79
|
+
activeCache = CacheService.create();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const key = buildDeterministicKey(req, { keyGenerator, headerWhitelist });
|
|
83
|
+
const ifNoneMatch = req.headers ? (req.headers['if-none-match'] || req.headers['If-None-Match']) : undefined;
|
|
84
|
+
|
|
85
|
+
const resolvedTags = typeof tags === 'function' ? tags(req) : tags;
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
const cached = await activeCache.get<CachedHttpResponse>(
|
|
89
|
+
key,
|
|
90
|
+
async () => {
|
|
91
|
+
return new Promise<CachedHttpResponse>((resolve) => {
|
|
92
|
+
const origJson = res.json?.bind(res);
|
|
93
|
+
const origSend = res.send?.bind(res);
|
|
94
|
+
const origEnd = res.end?.bind(res);
|
|
95
|
+
|
|
96
|
+
const getStatusCode = () => (res.statusCode ?? 200);
|
|
97
|
+
|
|
98
|
+
if (origJson) {
|
|
99
|
+
res.json = (body: any) => {
|
|
100
|
+
const status = getStatusCode();
|
|
101
|
+
const bodyEtag = etag ? generateETag(body) : undefined;
|
|
102
|
+
const contentType = res.getHeader?.('content-type') || 'application/json; charset=utf-8';
|
|
103
|
+
|
|
104
|
+
resolve({ body, contentType, etag: bodyEtag, status });
|
|
105
|
+
|
|
106
|
+
if (ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
|
|
107
|
+
res.status?.(304);
|
|
108
|
+
origEnd?.();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (bodyEtag && res.setHeader) {
|
|
113
|
+
res.setHeader('ETag', bodyEtag);
|
|
114
|
+
}
|
|
115
|
+
return origJson(body);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (origSend) {
|
|
120
|
+
res.send = (body: any) => {
|
|
121
|
+
const status = getStatusCode();
|
|
122
|
+
const bodyEtag = etag ? generateETag(body) : undefined;
|
|
123
|
+
const contentType = res.getHeader?.('content-type') as string | undefined;
|
|
124
|
+
|
|
125
|
+
resolve({ body, contentType, etag: bodyEtag, status });
|
|
126
|
+
|
|
127
|
+
if (ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
|
|
128
|
+
res.status?.(304);
|
|
129
|
+
origEnd?.();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (bodyEtag && res.setHeader) {
|
|
134
|
+
res.setHeader('ETag', bodyEtag);
|
|
135
|
+
}
|
|
136
|
+
return origSend(body);
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (origEnd) {
|
|
141
|
+
res.end = (body?: unknown) => {
|
|
142
|
+
const status = getStatusCode();
|
|
143
|
+
const bodyEtag = etag && body != null ? generateETag(body) : undefined;
|
|
144
|
+
const contentType = res.getHeader?.('content-type') as string | undefined;
|
|
145
|
+
|
|
146
|
+
resolve({ body, contentType, etag: bodyEtag, status });
|
|
147
|
+
return origEnd(body as never);
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
next();
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
ttl,
|
|
155
|
+
{ swr, tags: resolvedTags }
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
// Status gate: only cache 2xx successful responses
|
|
159
|
+
if (typeof cached.status === 'number' && (cached.status < 200 || cached.status >= 300)) {
|
|
160
|
+
await activeCache.delete(key).catch(() => {});
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!res.headersSent) {
|
|
165
|
+
if (cached.etag) {
|
|
166
|
+
if (res.setHeader) res.setHeader('ETag', cached.etag);
|
|
167
|
+
if (ifNoneMatch === cached.etag) {
|
|
168
|
+
return res.status(304).end();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (cached.contentType && res.setHeader) {
|
|
173
|
+
res.setHeader('Content-Type', cached.contentType);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (cached.status && res.status) {
|
|
177
|
+
res.status(cached.status);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (typeof cached.body === 'object' && cached.body !== null && res.json) {
|
|
181
|
+
return res.json(cached.body);
|
|
182
|
+
} else if (res.send) {
|
|
183
|
+
return res.send(cached.body);
|
|
184
|
+
} else if (res.end) {
|
|
185
|
+
return res.end(cached.body);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
} catch (err) {
|
|
189
|
+
next(err);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Alias matching legacy nomenclature */
|
|
195
|
+
export const expressCache = createExpressMiddleware;
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import type { CacheService } from '../cache-service.js';
|
|
2
|
+
import type { WrapOptions } from '../types.js';
|
|
3
|
+
import {
|
|
4
|
+
buildDeterministicKey,
|
|
5
|
+
generateETag,
|
|
6
|
+
shouldSkipCache,
|
|
7
|
+
type KeyDerivationOptions,
|
|
8
|
+
} from './utils.js';
|
|
9
|
+
|
|
10
|
+
export interface FastifyCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOptions {
|
|
11
|
+
/** TriCache instance. If omitted, lazily resolves the default singleton via CacheService.create(). */
|
|
12
|
+
cache?: CacheService;
|
|
13
|
+
/** Whether to generate and evaluate weak ETags. Default: true. */
|
|
14
|
+
etag?: boolean;
|
|
15
|
+
/** Custom predicate to skip caching dynamically for this request. */
|
|
16
|
+
skipCache?: (req: any) => boolean;
|
|
17
|
+
/** Dynamic tags derivation from request. */
|
|
18
|
+
tags?: string[] | ((req: any) => string[]);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface CachedFastifyResponse {
|
|
22
|
+
body: unknown;
|
|
23
|
+
contentType?: string;
|
|
24
|
+
etag?: string;
|
|
25
|
+
status: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Creates a Fastify plugin that registers `onRequest` and `onSend` lifecycle hooks.
|
|
30
|
+
*
|
|
31
|
+
* Designed to bypass Fastify's route encapsulation barrier (`skip-override`),
|
|
32
|
+
* allowing caching across global and nested route scopes.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* import Fastify from 'fastify';
|
|
36
|
+
* import { fastifyCachePlugin } from 'tricache/http';
|
|
37
|
+
*
|
|
38
|
+
* const app = Fastify();
|
|
39
|
+
* await app.register(fastifyCachePlugin, {
|
|
40
|
+
* ttl: 300,
|
|
41
|
+
* tags: ['api'],
|
|
42
|
+
* });
|
|
43
|
+
*/
|
|
44
|
+
export function createFastifyPlugin(options: FastifyCacheOptions = {}) {
|
|
45
|
+
const plugin = async function (fastify: any, pluginOpts: FastifyCacheOptions = {}) {
|
|
46
|
+
const mergedOpts = { ...options, ...pluginOpts };
|
|
47
|
+
const etag = mergedOpts.etag ?? true;
|
|
48
|
+
const ttl = mergedOpts.ttl ?? 300;
|
|
49
|
+
let activeCache = mergedOpts.cache;
|
|
50
|
+
|
|
51
|
+
if (!activeCache) {
|
|
52
|
+
const { CacheService } = await import('../cache-service.js');
|
|
53
|
+
activeCache = CacheService.create();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
fastify.addHook('onRequest', async (req: any, reply: any) => {
|
|
57
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (shouldSkipCache(req, mergedOpts.skipCache)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const key = buildDeterministicKey(req, {
|
|
66
|
+
keyGenerator: mergedOpts.keyGenerator,
|
|
67
|
+
headerWhitelist: mergedOpts.headerWhitelist,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const ifNoneMatch = req.headers
|
|
71
|
+
? (req.headers['if-none-match'] || req.headers['If-None-Match'])
|
|
72
|
+
: undefined;
|
|
73
|
+
|
|
74
|
+
// Fast probe in cache
|
|
75
|
+
const cached = await activeCache!.get<CachedFastifyResponse | null>(
|
|
76
|
+
key,
|
|
77
|
+
async () => null,
|
|
78
|
+
0
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
if (cached && typeof cached === 'object' && cached.body !== null) {
|
|
82
|
+
if (cached.etag) {
|
|
83
|
+
reply.header('ETag', cached.etag);
|
|
84
|
+
if (ifNoneMatch === cached.etag) {
|
|
85
|
+
reply.code(304);
|
|
86
|
+
return reply.send();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (cached.contentType) {
|
|
91
|
+
reply.header('Content-Type', cached.contentType);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
reply.code(cached.status ?? 200);
|
|
95
|
+
return reply.send(cached.body);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Mark request for response capture in onSend
|
|
99
|
+
req._tricacheKey = key;
|
|
100
|
+
const resolvedTags = typeof mergedOpts.tags === 'function'
|
|
101
|
+
? mergedOpts.tags(req)
|
|
102
|
+
: mergedOpts.tags;
|
|
103
|
+
req._tricacheTags = resolvedTags;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
fastify.addHook('onSend', async (req: any, reply: any, payload: unknown) => {
|
|
107
|
+
const key = req._tricacheKey;
|
|
108
|
+
if (!key) {
|
|
109
|
+
return payload;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const statusCode = reply.statusCode ?? reply.raw?.statusCode ?? 200;
|
|
113
|
+
|
|
114
|
+
// Status gate: only cache 2xx successful responses
|
|
115
|
+
if (statusCode >= 200 && statusCode < 300 && payload !== undefined && payload !== null) {
|
|
116
|
+
const payloadEtag = etag ? generateETag(payload) : undefined;
|
|
117
|
+
const contentType = reply.getHeader('content-type')
|
|
118
|
+
|| (typeof payload === 'object' ? 'application/json' : 'text/plain');
|
|
119
|
+
|
|
120
|
+
if (payloadEtag) {
|
|
121
|
+
reply.header('ETag', payloadEtag);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const ifNoneMatch = req.headers
|
|
125
|
+
? (req.headers['if-none-match'] || req.headers['If-None-Match'])
|
|
126
|
+
: undefined;
|
|
127
|
+
|
|
128
|
+
if (ifNoneMatch && payloadEtag && ifNoneMatch === payloadEtag) {
|
|
129
|
+
reply.code(304);
|
|
130
|
+
return '';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Asynchronously persist to cache without delaying current onSend pipeline
|
|
134
|
+
const entry: CachedFastifyResponse = {
|
|
135
|
+
body: payload,
|
|
136
|
+
contentType,
|
|
137
|
+
etag: payloadEtag,
|
|
138
|
+
status: statusCode,
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
void activeCache!.set(key, entry, mergedOpts.ttl ?? ttl, undefined, {
|
|
142
|
+
tags: req._tricacheTags,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return payload;
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// Attach Fastify skip-override symbol so plugin is not scoped to child contexts
|
|
151
|
+
(plugin as any)[Symbol.for('skip-override')] = true;
|
|
152
|
+
return plugin;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Standard Fastify plugin export */
|
|
156
|
+
export const fastifyCachePlugin = createFastifyPlugin();
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Universal Fastify middleware: functions both as a Fastify plugin (`fastify.register(fastifyCache(options))`)
|
|
160
|
+
* and as a route preHandler hook (`fastify.get('/route', { preHandler: fastifyCache(options) })`).
|
|
161
|
+
*/
|
|
162
|
+
export function fastifyCache(options: FastifyCacheOptions = {}) {
|
|
163
|
+
const plugin = createFastifyPlugin(options);
|
|
164
|
+
|
|
165
|
+
const dualHandler = async function (first: any, second?: any) {
|
|
166
|
+
// If invoked as a Fastify plugin: (fastify, pluginOpts)
|
|
167
|
+
if (first && typeof first.addHook === 'function') {
|
|
168
|
+
return plugin(first, second);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Otherwise invoked as a route preHandler: (req, reply)
|
|
172
|
+
const req = first;
|
|
173
|
+
const reply = second;
|
|
174
|
+
|
|
175
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') return;
|
|
176
|
+
if (shouldSkipCache(req, options.skipCache)) return;
|
|
177
|
+
|
|
178
|
+
let activeCache = options.cache;
|
|
179
|
+
if (!activeCache) {
|
|
180
|
+
const { CacheService } = await import('../cache-service.js');
|
|
181
|
+
activeCache = CacheService.create();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const key = buildDeterministicKey(req, {
|
|
185
|
+
keyGenerator: options.keyGenerator,
|
|
186
|
+
headerWhitelist: options.headerWhitelist,
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const ifNoneMatch = req.headers
|
|
190
|
+
? (req.headers['if-none-match'] || req.headers['If-None-Match'])
|
|
191
|
+
: undefined;
|
|
192
|
+
|
|
193
|
+
// Fast-path probe
|
|
194
|
+
const cached = await activeCache.get<CachedFastifyResponse | null>(
|
|
195
|
+
key,
|
|
196
|
+
async () => null,
|
|
197
|
+
0
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
if (cached && typeof cached === 'object' && cached.body !== null) {
|
|
201
|
+
if (cached.etag) {
|
|
202
|
+
if (reply.header) reply.header('ETag', cached.etag);
|
|
203
|
+
if (ifNoneMatch === cached.etag) {
|
|
204
|
+
if (reply.code) reply.code(304);
|
|
205
|
+
else if (reply.status) reply.status(304);
|
|
206
|
+
if (reply.send) return reply.send();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (cached.contentType && reply.header) {
|
|
212
|
+
reply.header('Content-Type', cached.contentType);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (cached.status) {
|
|
216
|
+
if (reply.code) reply.code(cached.status);
|
|
217
|
+
else if (reply.status) reply.status(cached.status);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (reply.send) return reply.send(cached.body);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Miss path: intercept reply.send on completion
|
|
225
|
+
const origSend = reply.send?.bind(reply);
|
|
226
|
+
if (origSend) {
|
|
227
|
+
reply.send = function (resPayload: any) {
|
|
228
|
+
if (resPayload !== undefined && resPayload !== null) {
|
|
229
|
+
const payloadEtag = options.etag !== false ? generateETag(resPayload) : undefined;
|
|
230
|
+
const contentType = (reply.getHeader && reply.getHeader('content-type'))
|
|
231
|
+
|| (typeof resPayload === 'object' ? 'application/json' : 'text/plain');
|
|
232
|
+
|
|
233
|
+
const respStatus = reply.statusCode ?? reply.raw?.statusCode ?? 200;
|
|
234
|
+
if (respStatus >= 200 && respStatus < 300) {
|
|
235
|
+
const resolvedTags = typeof options.tags === 'function' ? options.tags(req) : options.tags;
|
|
236
|
+
void activeCache!.set(
|
|
237
|
+
key,
|
|
238
|
+
{ body: resPayload, contentType, etag: payloadEtag, status: respStatus },
|
|
239
|
+
options.ttl ?? 300,
|
|
240
|
+
undefined,
|
|
241
|
+
{ tags: resolvedTags }
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (ifNoneMatch && ifNoneMatch === payloadEtag) {
|
|
246
|
+
if (reply.code) reply.code(304);
|
|
247
|
+
else if (reply.status) reply.status(304);
|
|
248
|
+
return origSend();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (payloadEtag && reply.header) reply.header('ETag', payloadEtag);
|
|
252
|
+
}
|
|
253
|
+
return origSend(resPayload);
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
(dualHandler as any)[Symbol.for('skip-override')] = true;
|
|
259
|
+
return dualHandler;
|
|
260
|
+
}
|