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.
- package/LICENSE +21 -21
- package/README.md +89 -1371
- package/dashboards/prometheus-scrape.yml +18 -0
- package/dashboards/tricache-alerts.yaml +54 -0
- package/dashboards/tricache-grafana.json +548 -0
- package/dist/cache-service-DVvroyqA.d.ts +886 -0
- package/dist/cache-service-KI5ACF3F.js +12 -0
- package/dist/chunk-7RV5ZVBB.js +1023 -0
- package/dist/chunk-BQRURGGD.js +60 -0
- package/dist/chunk-IITRFWHY.js +196 -0
- package/dist/{chunk-X5AXHPVD.js → chunk-LJ5QJB5O.js} +90 -5
- package/dist/{chunk-EUGYR7KR.js → chunk-PL5J3ZNT.js} +3184 -514
- package/dist/chunk-QNU7B43J.js +687 -0
- package/dist/chunk-UGD4VEPD.js +138 -0
- package/dist/chunk-XZL45ABR.js +108 -0
- package/dist/cli.cjs +3122 -495
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +121 -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 +2846 -494
- package/dist/drizzle/index.d.ts +3 -1
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +1559 -0
- package/dist/edge/index.d.ts +127 -0
- package/dist/edge/index.js +1109 -0
- package/dist/hono/index.cjs +7760 -0
- package/dist/hono/index.d.ts +91 -0
- package/dist/hono/index.js +185 -0
- package/dist/hono-VA9A1m0I.d.ts +371 -0
- package/dist/http/index.cjs +3281 -587
- package/dist/http/index.d.ts +85 -23
- package/dist/http/index.js +176 -78
- package/dist/index.cjs +7212 -2505
- 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 +2787 -487
- package/dist/nestjs/index.d.ts +3 -1
- package/dist/nestjs/index.js +6 -3
- package/dist/next/index.cjs +2804 -501
- package/dist/next/index.d.ts +4 -2
- package/dist/next/index.js +14 -10
- package/dist/prisma/index.d.ts +3 -1
- package/dist/serialize-worker.cjs +5 -4
- package/dist/serialize-worker.js +1 -1
- package/dist/{cache-service-D8tIXmzb.d.ts → types-B_QkVwJ7.d.ts} +622 -662
- package/dist/utils-CEsU_Z0s.d.ts +27 -0
- package/package.json +48 -10
- package/src/adapters/auto-pipeliner.ts +206 -0
- package/src/adapters/node-redis.ts +370 -0
- package/src/cache-service.ts +1619 -415
- package/src/cli.ts +137 -2
- package/src/codec.ts +72 -0
- 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 +449 -47
- 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 +284 -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 +127 -5
- package/src/hono/index.ts +316 -0
- package/src/http/express.ts +195 -0
- package/src/http/fastify.ts +260 -0
- package/src/http/index.ts +44 -258
- package/src/http/utils.ts +92 -0
- package/src/index.ts +149 -47
- package/src/ipc-telemetry.ts +509 -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 +12 -9
- package/src/next/index.ts +37 -37
- package/src/next/isr-handler.ts +105 -105
- package/src/next/types.ts +130 -130
- 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 +91 -8
- package/src/types.ts +315 -2
- 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-5GATUYAK.js +0 -7
- package/dist/chunk-UBSR5UYA.js +0 -0
package/dist/http/index.d.ts
CHANGED
|
@@ -1,33 +1,95 @@
|
|
|
1
|
-
import {
|
|
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
|
-
*
|
|
5
|
-
*
|
|
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
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
28
|
+
* @example
|
|
29
|
+
* import express from 'express';
|
|
30
|
+
* import { CacheService } from 'tricache';
|
|
31
|
+
* import { createExpressMiddleware } from 'tricache/http';
|
|
10
32
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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
|
|
17
|
-
/** TriCache instance. If omitted,
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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,
|
|
95
|
+
export { type CachedFastifyResponse, type CachedHttpResponse, type ExpressCacheOptions, type FastifyCacheOptions, type ExpressCacheOptions as HttpCacheOptions, KeyDerivationOptions, createExpressMiddleware, createFastifyPlugin, expressCache, fastifyCache, fastifyCachePlugin };
|
package/dist/http/index.js
CHANGED
|
@@ -1,23 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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-
|
|
33
|
+
const { CacheService } = await import("../cache-service-KI5ACF3F.js");
|
|
17
34
|
activeCache = CacheService.create();
|
|
18
35
|
}
|
|
19
|
-
const key =
|
|
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
|
|
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
|
-
|
|
34
|
-
|
|
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)
|
|
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
|
-
|
|
47
|
-
|
|
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)
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
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-
|
|
138
|
+
const { CacheService } = await import("../cache-service-KI5ACF3F.js");
|
|
105
139
|
activeCache = CacheService.create();
|
|
106
140
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
|
142
|
-
|
|
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
|
-
|
|
219
|
+
if (shouldSkipCache(req, options.skipCache)) return;
|
|
220
|
+
let activeCache = options.cache;
|
|
145
221
|
if (!activeCache) {
|
|
146
|
-
const { CacheService } = await import("../cache-service-
|
|
222
|
+
const { CacheService } = await import("../cache-service-KI5ACF3F.js");
|
|
147
223
|
activeCache = CacheService.create();
|
|
148
224
|
}
|
|
149
|
-
const key =
|
|
150
|
-
|
|
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(
|
|
175
|
-
if (
|
|
176
|
-
const payloadEtag = etag ? generateETag(
|
|
177
|
-
const contentType = reply.getHeader && reply.getHeader("content-type") || (typeof
|
|
178
|
-
const respStatus = reply.statusCode ?? reply.raw?.statusCode;
|
|
179
|
-
if (respStatus
|
|
180
|
-
|
|
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(
|
|
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
|
};
|