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
package/dist/http/index.d.ts
CHANGED
|
@@ -1,33 +1,119 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as CacheService } from '../cache-service-CyLdpo77.js';
|
|
2
|
+
import { W as WrapOptions } from '../types-B6krw64X.js';
|
|
3
|
+
export { C as CachedEdgeHttpResponse, H as HonoEdgeCacheOptions, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoCache } from '../hono-DQRS1Xd7.js';
|
|
4
|
+
import 'crypto';
|
|
2
5
|
|
|
6
|
+
interface KeyDerivationOptions {
|
|
7
|
+
/** Custom key derivation function. Overrides default URL and query string serialization. */
|
|
8
|
+
keyGenerator?: (req: any) => string;
|
|
9
|
+
/** Optional list of header names (case-insensitive) to include in the cache key. */
|
|
10
|
+
headerWhitelist?: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Generates a deterministic HTTP cache key from request method, URL path, sorted query parameters,
|
|
14
|
+
* and optional whitelisted headers.
|
|
15
|
+
*
|
|
16
|
+
* Example:
|
|
17
|
+
* GET /api/products?sort=desc&page=2
|
|
18
|
+
* GET /api/products?page=2&sort=desc
|
|
19
|
+
* Both map to: http:GET:/api/products?page=2&sort=desc
|
|
20
|
+
*/
|
|
21
|
+
declare function buildDeterministicKey(req: any, options?: KeyDerivationOptions): string;
|
|
22
|
+
/**
|
|
23
|
+
* Checks if request specifies Cache-Control bypass directives (no-cache, no-store)
|
|
24
|
+
* or matches a custom skipCache predicate.
|
|
25
|
+
*/
|
|
26
|
+
declare function shouldSkipCache(req: any, customSkip?: (req: any) => boolean): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Compute a weak ETag (W/"...") from string, Buffer, or JSON-serializable object.
|
|
29
|
+
*/
|
|
30
|
+
declare function generateETag(data: unknown): string;
|
|
31
|
+
|
|
32
|
+
interface ExpressCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOptions {
|
|
33
|
+
/** TriCache instance. If omitted, lazily resolves the default singleton via CacheService.create(). */
|
|
34
|
+
cache?: CacheService;
|
|
35
|
+
/** Whether to generate and evaluate weak ETags. Default: true. */
|
|
36
|
+
etag?: boolean;
|
|
37
|
+
/** Custom predicate to skip caching dynamically for this request (e.g. authenticated sessions). */
|
|
38
|
+
skipCache?: (req: any) => boolean;
|
|
39
|
+
/** Dynamic tags derivation from request. */
|
|
40
|
+
tags?: string[] | ((req: any) => string[]);
|
|
41
|
+
}
|
|
42
|
+
interface CachedHttpResponse {
|
|
43
|
+
body: unknown;
|
|
44
|
+
contentType?: string;
|
|
45
|
+
etag?: string;
|
|
46
|
+
status: number;
|
|
47
|
+
}
|
|
3
48
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
49
|
+
* Creates an Express / Connect middleware that provides deterministic response caching,
|
|
50
|
+
* weak ETag generation, conditional 304 Not Modified short-circuiting, and Cache-Control bypass controls.
|
|
6
51
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
52
|
+
* @example
|
|
53
|
+
* import express from 'express';
|
|
54
|
+
* import { CacheService } from 'tricache';
|
|
55
|
+
* import { createExpressMiddleware } from 'tricache/http';
|
|
10
56
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
57
|
+
* const app = express();
|
|
58
|
+
* const cache = CacheService.create();
|
|
59
|
+
*
|
|
60
|
+
* app.get(
|
|
61
|
+
* '/api/products',
|
|
62
|
+
* createExpressMiddleware({
|
|
63
|
+
* cache,
|
|
64
|
+
* ttl: 120,
|
|
65
|
+
* tags: ['products'],
|
|
66
|
+
* headerWhitelist: ['accept-language'],
|
|
67
|
+
* }),
|
|
68
|
+
* async (req, res) => {
|
|
69
|
+
* const data = await getProducts();
|
|
70
|
+
* res.json(data);
|
|
71
|
+
* }
|
|
72
|
+
* );
|
|
14
73
|
*/
|
|
74
|
+
declare function createExpressMiddleware(options?: ExpressCacheOptions): (req: any, res: any, next: any) => Promise<any>;
|
|
75
|
+
/** Alias matching legacy nomenclature */
|
|
76
|
+
declare const expressCache: typeof createExpressMiddleware;
|
|
15
77
|
|
|
16
|
-
interface
|
|
17
|
-
/** TriCache instance. If omitted,
|
|
78
|
+
interface FastifyCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOptions {
|
|
79
|
+
/** TriCache instance. If omitted, lazily resolves the default singleton via CacheService.create(). */
|
|
18
80
|
cache?: CacheService;
|
|
19
|
-
/**
|
|
20
|
-
keyGenerator?: (req: any) => string;
|
|
21
|
-
/** Whether to emit weak ETags (default: true). */
|
|
81
|
+
/** Whether to generate and evaluate weak ETags. Default: true. */
|
|
22
82
|
etag?: boolean;
|
|
83
|
+
/** Custom predicate to skip caching dynamically for this request. */
|
|
84
|
+
skipCache?: (req: any) => boolean;
|
|
85
|
+
/** Dynamic tags derivation from request. */
|
|
86
|
+
tags?: string[] | ((req: any) => string[]);
|
|
23
87
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
|
|
88
|
+
interface CachedFastifyResponse {
|
|
89
|
+
body: unknown;
|
|
90
|
+
contentType?: string;
|
|
91
|
+
etag?: string;
|
|
92
|
+
status: number;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Creates a Fastify plugin that registers `onRequest` and `onSend` lifecycle hooks.
|
|
96
|
+
*
|
|
97
|
+
* Designed to bypass Fastify's route encapsulation barrier (`skip-override`),
|
|
98
|
+
* allowing caching across global and nested route scopes.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* import Fastify from 'fastify';
|
|
102
|
+
* import { fastifyCachePlugin } from 'tricache/http';
|
|
103
|
+
*
|
|
104
|
+
* const app = Fastify();
|
|
105
|
+
* await app.register(fastifyCachePlugin, {
|
|
106
|
+
* ttl: 300,
|
|
107
|
+
* tags: ['api'],
|
|
108
|
+
* });
|
|
109
|
+
*/
|
|
110
|
+
declare function createFastifyPlugin(options?: FastifyCacheOptions): (fastify: any, pluginOpts?: FastifyCacheOptions) => Promise<void>;
|
|
111
|
+
/** Standard Fastify plugin export */
|
|
112
|
+
declare const fastifyCachePlugin: (fastify: any, pluginOpts?: FastifyCacheOptions) => Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* Universal Fastify middleware: functions both as a Fastify plugin (`fastify.register(fastifyCache(options))`)
|
|
115
|
+
* and as a route preHandler hook (`fastify.get('/route', { preHandler: fastifyCache(options) })`).
|
|
116
|
+
*/
|
|
117
|
+
declare function fastifyCache(options?: FastifyCacheOptions): (first: any, second?: any) => Promise<any>;
|
|
32
118
|
|
|
33
|
-
export { type HttpCacheOptions, expressCache, fastifyCache, generateETag,
|
|
119
|
+
export { type CachedFastifyResponse, type CachedHttpResponse, type ExpressCacheOptions, type FastifyCacheOptions, type ExpressCacheOptions as HttpCacheOptions, type KeyDerivationOptions, buildDeterministicKey, createExpressMiddleware, createFastifyPlugin, expressCache, fastifyCache, fastifyCachePlugin, generateETag, shouldSkipCache };
|
package/dist/http/index.js
CHANGED
|
@@ -1,23 +1,91 @@
|
|
|
1
|
-
|
|
1
|
+
import {
|
|
2
|
+
buildEdgeDeterministicKey,
|
|
3
|
+
computeEdgeETag,
|
|
4
|
+
honoEdgeCache
|
|
5
|
+
} from "../chunk-ZTSWCFYO.js";
|
|
6
|
+
|
|
7
|
+
// src/http/utils.ts
|
|
2
8
|
import crypto from "crypto";
|
|
9
|
+
function buildDeterministicKey(req, options) {
|
|
10
|
+
if (options?.keyGenerator) {
|
|
11
|
+
return options.keyGenerator(req);
|
|
12
|
+
}
|
|
13
|
+
const method = (req.method || "GET").toUpperCase();
|
|
14
|
+
const rawUrl = req.originalUrl || req.url || "/";
|
|
15
|
+
const qIdx = rawUrl.indexOf("?");
|
|
16
|
+
const path = qIdx >= 0 ? rawUrl.slice(0, qIdx) : rawUrl;
|
|
17
|
+
let queryString = "";
|
|
18
|
+
if (qIdx >= 0) {
|
|
19
|
+
const rawQuery = rawUrl.slice(qIdx + 1);
|
|
20
|
+
try {
|
|
21
|
+
const params = new URLSearchParams(rawQuery);
|
|
22
|
+
params.sort();
|
|
23
|
+
queryString = params.toString();
|
|
24
|
+
} catch {
|
|
25
|
+
queryString = rawQuery;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
let key = `http:${method}:${path}`;
|
|
29
|
+
if (queryString) {
|
|
30
|
+
key += `?${queryString}`;
|
|
31
|
+
}
|
|
32
|
+
if (options?.headerWhitelist && options.headerWhitelist.length > 0 && req.headers) {
|
|
33
|
+
const sortedHeaders = [...options.headerWhitelist].sort();
|
|
34
|
+
for (const h of sortedHeaders) {
|
|
35
|
+
const normalizedHeader = h.toLowerCase();
|
|
36
|
+
const val = req.headers[normalizedHeader];
|
|
37
|
+
if (val !== void 0 && val !== null && val !== "") {
|
|
38
|
+
const headerStr = Array.isArray(val) ? val.join(",") : String(val);
|
|
39
|
+
key += `|h:${normalizedHeader}=${headerStr}`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return key;
|
|
44
|
+
}
|
|
45
|
+
function shouldSkipCache(req, customSkip) {
|
|
46
|
+
if (customSkip && customSkip(req)) return true;
|
|
47
|
+
const cc = req.headers ? req.headers["cache-control"] || req.headers["Cache-Control"] : void 0;
|
|
48
|
+
if (typeof cc === "string") {
|
|
49
|
+
const lower = cc.toLowerCase();
|
|
50
|
+
if (lower.includes("no-cache") || lower.includes("no-store")) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
3
56
|
function generateETag(data) {
|
|
4
57
|
const str = typeof data === "string" ? data : Buffer.isBuffer(data) ? data.toString("utf8") : JSON.stringify(data);
|
|
5
58
|
const hash = crypto.createHash("sha1").update(str).digest("hex").slice(0, 16);
|
|
6
59
|
return `W/"${str.length.toString(16)}-${hash}"`;
|
|
7
60
|
}
|
|
8
|
-
|
|
9
|
-
|
|
61
|
+
|
|
62
|
+
// src/http/express.ts
|
|
63
|
+
function createExpressMiddleware(options = {}) {
|
|
64
|
+
const {
|
|
65
|
+
cache,
|
|
66
|
+
etag = true,
|
|
67
|
+
ttl = 300,
|
|
68
|
+
swr,
|
|
69
|
+
tags,
|
|
70
|
+
skipCache,
|
|
71
|
+
keyGenerator,
|
|
72
|
+
headerWhitelist
|
|
73
|
+
} = options;
|
|
10
74
|
return async (req, res, next) => {
|
|
11
75
|
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
12
76
|
return next();
|
|
13
77
|
}
|
|
78
|
+
if (shouldSkipCache(req, skipCache)) {
|
|
79
|
+
return next();
|
|
80
|
+
}
|
|
14
81
|
let activeCache = cache;
|
|
15
82
|
if (!activeCache) {
|
|
16
|
-
const { CacheService } = await import("../cache-service-
|
|
83
|
+
const { CacheService } = await import("../cache-service-5MZXFIGK.js");
|
|
17
84
|
activeCache = CacheService.create();
|
|
18
85
|
}
|
|
19
|
-
const key =
|
|
20
|
-
const ifNoneMatch = req.headers ? req.headers["if-none-match"] : void 0;
|
|
86
|
+
const key = buildDeterministicKey(req, { keyGenerator, headerWhitelist });
|
|
87
|
+
const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
|
|
88
|
+
const resolvedTags = typeof tags === "function" ? tags(req) : tags;
|
|
21
89
|
try {
|
|
22
90
|
const cached = await activeCache.get(
|
|
23
91
|
key,
|
|
@@ -25,34 +93,62 @@ function expressCache(options = {}) {
|
|
|
25
93
|
return new Promise((resolve) => {
|
|
26
94
|
const origJson = res.json?.bind(res);
|
|
27
95
|
const origSend = res.send?.bind(res);
|
|
96
|
+
const origEnd = res.end?.bind(res);
|
|
97
|
+
const getStatusCode = () => res.statusCode ?? 200;
|
|
28
98
|
if (origJson) {
|
|
29
99
|
res.json = (body) => {
|
|
100
|
+
const status = getStatusCode();
|
|
30
101
|
const bodyEtag = etag ? generateETag(body) : void 0;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
102
|
+
const contentType = res.getHeader?.("content-type") || "application/json; charset=utf-8";
|
|
103
|
+
resolve({ body, contentType, etag: bodyEtag, status });
|
|
104
|
+
if (ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
|
|
105
|
+
res.status?.(304);
|
|
106
|
+
origEnd?.();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (bodyEtag && res.setHeader) {
|
|
110
|
+
res.setHeader("ETag", bodyEtag);
|
|
34
111
|
}
|
|
35
|
-
if (bodyEtag && res.setHeader) res.setHeader("ETag", bodyEtag);
|
|
36
112
|
return origJson(body);
|
|
37
113
|
};
|
|
38
114
|
}
|
|
39
115
|
if (origSend) {
|
|
40
116
|
res.send = (body) => {
|
|
117
|
+
const status = getStatusCode();
|
|
41
118
|
const bodyEtag = etag ? generateETag(body) : void 0;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
119
|
+
const contentType = res.getHeader?.("content-type");
|
|
120
|
+
resolve({ body, contentType, etag: bodyEtag, status });
|
|
121
|
+
if (ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
|
|
122
|
+
res.status?.(304);
|
|
123
|
+
origEnd?.();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (bodyEtag && res.setHeader) {
|
|
127
|
+
res.setHeader("ETag", bodyEtag);
|
|
45
128
|
}
|
|
46
|
-
if (bodyEtag && res.setHeader) res.setHeader("ETag", bodyEtag);
|
|
47
129
|
return origSend(body);
|
|
48
130
|
};
|
|
49
131
|
}
|
|
132
|
+
if (origEnd) {
|
|
133
|
+
res.end = (body) => {
|
|
134
|
+
const status = getStatusCode();
|
|
135
|
+
const bodyEtag = etag && body != null ? generateETag(body) : void 0;
|
|
136
|
+
const contentType = res.getHeader?.("content-type");
|
|
137
|
+
resolve({ body, contentType, etag: bodyEtag, status });
|
|
138
|
+
return origEnd(body);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
50
141
|
next();
|
|
51
142
|
});
|
|
52
143
|
},
|
|
53
144
|
ttl,
|
|
54
|
-
{ swr, tags }
|
|
145
|
+
{ swr, tags: resolvedTags }
|
|
55
146
|
);
|
|
147
|
+
if (typeof cached.status === "number" && (cached.status < 200 || cached.status >= 300)) {
|
|
148
|
+
await activeCache.delete(key).catch(() => {
|
|
149
|
+
});
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
56
152
|
if (!res.headersSent) {
|
|
57
153
|
if (cached.etag) {
|
|
58
154
|
if (res.setHeader) res.setHeader("ETag", cached.etag);
|
|
@@ -63,10 +159,15 @@ function expressCache(options = {}) {
|
|
|
63
159
|
if (cached.contentType && res.setHeader) {
|
|
64
160
|
res.setHeader("Content-Type", cached.contentType);
|
|
65
161
|
}
|
|
162
|
+
if (cached.status && res.status) {
|
|
163
|
+
res.status(cached.status);
|
|
164
|
+
}
|
|
66
165
|
if (typeof cached.body === "object" && cached.body !== null && res.json) {
|
|
67
166
|
return res.json(cached.body);
|
|
68
167
|
} else if (res.send) {
|
|
69
168
|
return res.send(cached.body);
|
|
169
|
+
} else if (res.end) {
|
|
170
|
+
return res.end(cached.body);
|
|
70
171
|
}
|
|
71
172
|
}
|
|
72
173
|
} catch (err) {
|
|
@@ -74,61 +175,114 @@ function expressCache(options = {}) {
|
|
|
74
175
|
}
|
|
75
176
|
};
|
|
76
177
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
178
|
+
var expressCache = createExpressMiddleware;
|
|
179
|
+
|
|
180
|
+
// src/http/fastify.ts
|
|
181
|
+
function createFastifyPlugin(options = {}) {
|
|
182
|
+
const plugin = async function(fastify, pluginOpts = {}) {
|
|
183
|
+
const mergedOpts = { ...options, ...pluginOpts };
|
|
184
|
+
const etag = mergedOpts.etag ?? true;
|
|
185
|
+
const ttl = mergedOpts.ttl ?? 300;
|
|
186
|
+
let activeCache = mergedOpts.cache;
|
|
84
187
|
if (!activeCache) {
|
|
85
|
-
const { CacheService } = await import("../cache-service-
|
|
188
|
+
const { CacheService } = await import("../cache-service-5MZXFIGK.js");
|
|
86
189
|
activeCache = CacheService.create();
|
|
87
190
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
191
|
+
fastify.addHook("onRequest", async (req, reply) => {
|
|
192
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (shouldSkipCache(req, mergedOpts.skipCache)) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const key = buildDeterministicKey(req, {
|
|
199
|
+
keyGenerator: mergedOpts.keyGenerator,
|
|
200
|
+
headerWhitelist: mergedOpts.headerWhitelist
|
|
201
|
+
});
|
|
202
|
+
const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
|
|
203
|
+
const cached = await activeCache.get(
|
|
204
|
+
key,
|
|
205
|
+
async () => null,
|
|
206
|
+
0
|
|
207
|
+
);
|
|
208
|
+
if (cached && typeof cached === "object" && cached.body !== null) {
|
|
209
|
+
if (cached.etag) {
|
|
210
|
+
reply.header("ETag", cached.etag);
|
|
211
|
+
if (ifNoneMatch === cached.etag) {
|
|
212
|
+
reply.code(304);
|
|
213
|
+
return reply.send();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (cached.contentType) {
|
|
217
|
+
reply.header("Content-Type", cached.contentType);
|
|
218
|
+
}
|
|
219
|
+
reply.code(cached.status ?? 200);
|
|
220
|
+
return reply.send(cached.body);
|
|
221
|
+
}
|
|
222
|
+
req._tricacheKey = key;
|
|
223
|
+
const resolvedTags = typeof mergedOpts.tags === "function" ? mergedOpts.tags(req) : mergedOpts.tags;
|
|
224
|
+
req._tricacheTags = resolvedTags;
|
|
225
|
+
});
|
|
226
|
+
fastify.addHook("onSend", async (req, reply, payload) => {
|
|
227
|
+
const key = req._tricacheKey;
|
|
228
|
+
if (!key) {
|
|
229
|
+
return payload;
|
|
230
|
+
}
|
|
231
|
+
const statusCode = reply.statusCode ?? reply.raw?.statusCode ?? 200;
|
|
232
|
+
if (statusCode >= 200 && statusCode < 300 && payload !== void 0 && payload !== null) {
|
|
233
|
+
const payloadEtag = etag ? generateETag(payload) : void 0;
|
|
234
|
+
const contentType = reply.getHeader("content-type") || (typeof payload === "object" ? "application/json" : "text/plain");
|
|
235
|
+
if (payloadEtag) {
|
|
236
|
+
reply.header("ETag", payloadEtag);
|
|
237
|
+
}
|
|
238
|
+
const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
|
|
239
|
+
if (ifNoneMatch && payloadEtag && ifNoneMatch === payloadEtag) {
|
|
240
|
+
reply.code(304);
|
|
241
|
+
return "";
|
|
242
|
+
}
|
|
243
|
+
const entry = {
|
|
244
|
+
body: payload,
|
|
245
|
+
contentType,
|
|
246
|
+
etag: payloadEtag,
|
|
247
|
+
status: statusCode
|
|
101
248
|
};
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
const headers = {};
|
|
110
|
-
if (cached.etag) headers["ETag"] = cached.etag;
|
|
111
|
-
if (cached.contentType) headers["Content-Type"] = cached.contentType;
|
|
112
|
-
return c.body(cached.body, 200, headers);
|
|
249
|
+
void activeCache.set(key, entry, mergedOpts.ttl ?? ttl, void 0, {
|
|
250
|
+
tags: req._tricacheTags
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
return payload;
|
|
254
|
+
});
|
|
113
255
|
};
|
|
256
|
+
plugin[/* @__PURE__ */ Symbol.for("skip-override")] = true;
|
|
257
|
+
return plugin;
|
|
114
258
|
}
|
|
259
|
+
var fastifyCachePlugin = createFastifyPlugin();
|
|
115
260
|
function fastifyCache(options = {}) {
|
|
116
|
-
const
|
|
117
|
-
|
|
261
|
+
const plugin = createFastifyPlugin(options);
|
|
262
|
+
const dualHandler = async function(first, second) {
|
|
263
|
+
if (first && typeof first.addHook === "function") {
|
|
264
|
+
return plugin(first, second);
|
|
265
|
+
}
|
|
266
|
+
const req = first;
|
|
267
|
+
const reply = second;
|
|
118
268
|
if (req.method !== "GET" && req.method !== "HEAD") return;
|
|
119
|
-
|
|
269
|
+
if (shouldSkipCache(req, options.skipCache)) return;
|
|
270
|
+
let activeCache = options.cache;
|
|
120
271
|
if (!activeCache) {
|
|
121
|
-
const { CacheService } = await import("../cache-service-
|
|
272
|
+
const { CacheService } = await import("../cache-service-5MZXFIGK.js");
|
|
122
273
|
activeCache = CacheService.create();
|
|
123
274
|
}
|
|
124
|
-
const key =
|
|
125
|
-
|
|
275
|
+
const key = buildDeterministicKey(req, {
|
|
276
|
+
keyGenerator: options.keyGenerator,
|
|
277
|
+
headerWhitelist: options.headerWhitelist
|
|
278
|
+
});
|
|
279
|
+
const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
|
|
126
280
|
const cached = await activeCache.get(
|
|
127
281
|
key,
|
|
128
282
|
async () => null,
|
|
129
283
|
0
|
|
130
284
|
);
|
|
131
|
-
if (cached) {
|
|
285
|
+
if (cached && typeof cached === "object" && cached.body !== null) {
|
|
132
286
|
if (cached.etag) {
|
|
133
287
|
if (reply.header) reply.header("ETag", cached.etag);
|
|
134
288
|
if (ifNoneMatch === cached.etag) {
|
|
@@ -141,17 +295,30 @@ function fastifyCache(options = {}) {
|
|
|
141
295
|
if (cached.contentType && reply.header) {
|
|
142
296
|
reply.header("Content-Type", cached.contentType);
|
|
143
297
|
}
|
|
298
|
+
if (cached.status) {
|
|
299
|
+
if (reply.code) reply.code(cached.status);
|
|
300
|
+
else if (reply.status) reply.status(cached.status);
|
|
301
|
+
}
|
|
144
302
|
if (reply.send) return reply.send(cached.body);
|
|
145
303
|
return;
|
|
146
304
|
}
|
|
147
305
|
const origSend = reply.send?.bind(reply);
|
|
148
306
|
if (origSend) {
|
|
149
|
-
reply.send = function(
|
|
150
|
-
if (
|
|
151
|
-
const payloadEtag = etag ? generateETag(
|
|
152
|
-
const contentType = reply.getHeader && reply.getHeader("content-type") || (typeof
|
|
153
|
-
|
|
154
|
-
|
|
307
|
+
reply.send = function(resPayload) {
|
|
308
|
+
if (resPayload !== void 0 && resPayload !== null) {
|
|
309
|
+
const payloadEtag = options.etag !== false ? generateETag(resPayload) : void 0;
|
|
310
|
+
const contentType = reply.getHeader && reply.getHeader("content-type") || (typeof resPayload === "object" ? "application/json" : "text/plain");
|
|
311
|
+
const respStatus = reply.statusCode ?? reply.raw?.statusCode ?? 200;
|
|
312
|
+
if (respStatus >= 200 && respStatus < 300) {
|
|
313
|
+
const resolvedTags = typeof options.tags === "function" ? options.tags(req) : options.tags;
|
|
314
|
+
void activeCache.set(
|
|
315
|
+
key,
|
|
316
|
+
{ body: resPayload, contentType, etag: payloadEtag, status: respStatus },
|
|
317
|
+
options.ttl ?? 300,
|
|
318
|
+
void 0,
|
|
319
|
+
{ tags: resolvedTags }
|
|
320
|
+
);
|
|
321
|
+
}
|
|
155
322
|
if (ifNoneMatch && ifNoneMatch === payloadEtag) {
|
|
156
323
|
if (reply.code) reply.code(304);
|
|
157
324
|
else if (reply.status) reply.status(304);
|
|
@@ -159,14 +326,23 @@ function fastifyCache(options = {}) {
|
|
|
159
326
|
}
|
|
160
327
|
if (payloadEtag && reply.header) reply.header("ETag", payloadEtag);
|
|
161
328
|
}
|
|
162
|
-
return origSend(
|
|
329
|
+
return origSend(resPayload);
|
|
163
330
|
};
|
|
164
331
|
}
|
|
165
332
|
};
|
|
333
|
+
dualHandler[/* @__PURE__ */ Symbol.for("skip-override")] = true;
|
|
334
|
+
return dualHandler;
|
|
166
335
|
}
|
|
167
336
|
export {
|
|
337
|
+
buildDeterministicKey,
|
|
338
|
+
buildEdgeDeterministicKey,
|
|
339
|
+
computeEdgeETag,
|
|
340
|
+
createExpressMiddleware,
|
|
341
|
+
createFastifyPlugin,
|
|
168
342
|
expressCache,
|
|
169
343
|
fastifyCache,
|
|
344
|
+
fastifyCachePlugin,
|
|
170
345
|
generateETag,
|
|
171
|
-
honoCache
|
|
346
|
+
honoEdgeCache as honoCache,
|
|
347
|
+
shouldSkipCache
|
|
172
348
|
};
|