tricache 1.0.0 → 1.0.2
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/README.md +36 -28
- package/dist/{cache-service-pJ-nFcVj.d.ts → cache-service-B108FzER.d.ts} +63 -3
- package/dist/cache-service-CWI4jkS7.d.cts +963 -0
- package/dist/{cache-service-26GCO3UU.js → cache-service-GYPZFB7O.js} +2 -2
- package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
- package/dist/{chunk-2AJKEQMZ.js → chunk-H4GFLQ26.js} +7 -4
- package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
- package/dist/chunk-OYDU3SMI.js +89 -0
- package/dist/{chunk-VXFTFZ5N.js → chunk-VNRDL5RA.js} +117 -138
- package/dist/{chunk-7SZSQS55.js → chunk-WYF7NOAT.js} +1831 -759
- package/dist/cli.cjs +1757 -777
- package/dist/cli.d.cts +12 -0
- package/dist/cli.js +27 -6
- package/dist/dashboard/index.cjs +111 -29
- package/dist/dashboard/index.d.cts +185 -0
- package/dist/dashboard/index.d.ts +5 -3
- package/dist/dashboard/index.js +1 -1
- package/dist/drizzle/index.cjs +1757 -773
- package/dist/drizzle/index.d.cts +46 -0
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +280 -153
- package/dist/edge/index.d.cts +127 -0
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +254 -147
- package/dist/hono/index.cjs +1805 -793
- package/dist/hono/index.d.cts +93 -0
- package/dist/hono/index.d.ts +7 -5
- package/dist/hono/index.js +16 -9
- package/dist/{hono-DuNvciyq.d.ts → hono-3rDMYVtY.d.ts} +23 -3
- package/dist/hono-C886ok3X.d.cts +391 -0
- package/dist/http/index.cjs +1946 -842
- package/dist/http/index.d.cts +103 -0
- package/dist/http/index.d.ts +13 -5
- package/dist/http/index.js +137 -57
- package/dist/index.cjs +2073 -937
- package/dist/index.d.cts +1006 -0
- package/dist/index.d.ts +40 -8
- package/dist/index.js +8 -8
- package/dist/murmur3-CMGw9PbV.d.cts +98 -0
- package/dist/nestjs/index.cjs +1849 -806
- package/dist/nestjs/index.d.cts +285 -0
- package/dist/nestjs/index.d.ts +103 -30
- package/dist/nestjs/index.js +115 -32
- package/dist/next/index.cjs +1913 -887
- package/dist/next/index.d.cts +232 -0
- package/dist/next/index.d.ts +33 -11
- package/dist/next/index.js +174 -107
- package/dist/prisma/index.cjs +11 -1
- package/dist/prisma/index.d.cts +62 -0
- package/dist/prisma/index.d.ts +2 -2
- package/dist/prisma/index.js +11 -1
- package/dist/serialize-worker.d.cts +2 -0
- package/dist/{types-5HXqBfAf.d.ts → types-BA4pz1A0.d.cts} +26 -9
- package/dist/types-BA4pz1A0.d.ts +1519 -0
- package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
- package/dist/utils-Bvzn-rKS.d.ts +29 -0
- package/package.json +96 -36
- package/src/adapters/auto-pipeliner.ts +107 -24
- package/src/adapters/node-redis.ts +123 -25
- package/src/cache-service.ts +1238 -659
- package/src/cli.ts +24 -6
- package/src/cross-region.ts +21 -3
- package/src/dashboard/handler.ts +26 -6
- package/src/dashboard/html.ts +35 -10
- package/src/dashboard/index.ts +35 -13
- package/src/dashboard/types.ts +3 -1
- package/src/disk-tier.ts +171 -65
- package/src/edge/cache.ts +202 -129
- package/src/edge/hono.ts +23 -6
- package/src/edge/types.ts +2 -0
- package/src/hono/index.ts +19 -9
- package/src/http/express.ts +110 -26
- package/src/http/fastify.ts +38 -28
- package/src/http/utils.ts +54 -20
- package/src/ipc-telemetry.ts +35 -9
- package/src/nestjs/decorators.ts +1 -1
- package/src/nestjs/index.ts +2 -0
- package/src/nestjs/keyv-adapter.ts +68 -0
- package/src/nestjs/tricache.module.ts +13 -11
- package/src/nestjs/tricache.store.ts +51 -20
- package/src/nestjs/types.ts +29 -15
- package/src/next/cache-handler.ts +132 -57
- package/src/next/index.ts +1 -0
- package/src/next/isr-handler.ts +1 -4
- package/src/next/types.ts +21 -0
- package/src/prisma/index.ts +12 -1
- package/src/smart-memory-cache.ts +65 -27
- package/src/types.ts +27 -2
- package/src/utils/cgroup.ts +12 -0
- package/src/utils/deadline.ts +14 -0
- package/src/utils/diagnostics.ts +27 -0
- package/src/utils/redis-slot.ts +13 -0
- package/src/wasm/bloom-filter-wasm.ts +9 -4
- package/src/worker-pool.ts +17 -8
- package/src/wtiny-lfu.ts +20 -6
- package/dist/chunk-BQRURGGD.js +0 -60
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { K as KeyDerivationOptions } from '../utils-Bvzn-rKS.cjs';
|
|
2
|
+
export { b as buildDeterministicKey, g as generateETag, s as shouldSkipCache } from '../utils-Bvzn-rKS.cjs';
|
|
3
|
+
import { C as CacheService } from '../cache-service-CWI4jkS7.cjs';
|
|
4
|
+
import { W as WrapOptions } from '../types-BA4pz1A0.cjs';
|
|
5
|
+
export { C as CachedEdgeHttpResponse, H as HonoEdgeCacheOptions, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoCache } from '../hono-C886ok3X.cjs';
|
|
6
|
+
import 'crypto';
|
|
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
|
+
/** @deprecated Response refreshes run on live requests. Use data-level cache.get()/wrap() for SWR. */
|
|
18
|
+
swr?: number;
|
|
19
|
+
}
|
|
20
|
+
interface CachedHttpResponse {
|
|
21
|
+
body: unknown;
|
|
22
|
+
contentType?: string;
|
|
23
|
+
etag?: string;
|
|
24
|
+
status: number;
|
|
25
|
+
binary?: boolean;
|
|
26
|
+
headers?: Record<string, string | string[]>;
|
|
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
|
+
declare function createExpressMiddleware(options?: ExpressCacheOptions): (req: any, res: any, next: any) => Promise<any>;
|
|
55
|
+
/** Alias matching legacy nomenclature */
|
|
56
|
+
declare const expressCache: typeof createExpressMiddleware;
|
|
57
|
+
|
|
58
|
+
interface FastifyCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOptions {
|
|
59
|
+
/** TriCache instance. If omitted, lazily resolves the default singleton via CacheService.create(). */
|
|
60
|
+
cache?: CacheService;
|
|
61
|
+
/** Whether to generate and evaluate weak ETags. Default: true. */
|
|
62
|
+
etag?: boolean;
|
|
63
|
+
/** Custom predicate to skip caching dynamically for this request. */
|
|
64
|
+
skipCache?: (req: any) => boolean;
|
|
65
|
+
/** Dynamic tags derivation from request. */
|
|
66
|
+
tags?: string[] | ((req: any) => string[]);
|
|
67
|
+
/** @deprecated Response refreshes run on live requests. Use data-level cache.get()/wrap() for SWR. */
|
|
68
|
+
swr?: number;
|
|
69
|
+
}
|
|
70
|
+
interface CachedFastifyResponse {
|
|
71
|
+
body: unknown;
|
|
72
|
+
contentType?: string;
|
|
73
|
+
etag?: string;
|
|
74
|
+
status: number;
|
|
75
|
+
binary?: boolean;
|
|
76
|
+
headers?: Record<string, string | string[]>;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Creates a Fastify plugin that registers `onRequest` and `onSend` lifecycle hooks.
|
|
80
|
+
*
|
|
81
|
+
* Designed to bypass Fastify's route encapsulation barrier (`skip-override`),
|
|
82
|
+
* allowing caching across global and nested route scopes.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* import Fastify from 'fastify';
|
|
86
|
+
* import { fastifyCachePlugin } from 'tricache/http';
|
|
87
|
+
*
|
|
88
|
+
* const app = Fastify();
|
|
89
|
+
* await app.register(fastifyCachePlugin, {
|
|
90
|
+
* ttl: 300,
|
|
91
|
+
* tags: ['api'],
|
|
92
|
+
* });
|
|
93
|
+
*/
|
|
94
|
+
declare function createFastifyPlugin(options?: FastifyCacheOptions): (fastify: any, pluginOpts?: FastifyCacheOptions) => Promise<void>;
|
|
95
|
+
/** Standard Fastify plugin export */
|
|
96
|
+
declare const fastifyCachePlugin: (fastify: any, pluginOpts?: FastifyCacheOptions) => Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Universal Fastify middleware: functions both as a Fastify plugin (`fastify.register(fastifyCache(options))`)
|
|
99
|
+
* and as a route preHandler hook (`fastify.get('/route', { preHandler: fastifyCache(options) })`).
|
|
100
|
+
*/
|
|
101
|
+
declare function fastifyCache(options?: FastifyCacheOptions): (first: any, second?: any) => Promise<any>;
|
|
102
|
+
|
|
103
|
+
export { type CachedFastifyResponse, type CachedHttpResponse, type ExpressCacheOptions, type FastifyCacheOptions, type ExpressCacheOptions as HttpCacheOptions, KeyDerivationOptions, createExpressMiddleware, createFastifyPlugin, expressCache, fastifyCache, fastifyCachePlugin };
|
package/dist/http/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { K as KeyDerivationOptions } from '../utils-
|
|
2
|
-
export { b as buildDeterministicKey, g as generateETag, s as shouldSkipCache } from '../utils-
|
|
3
|
-
import { C as CacheService } from '../cache-service-
|
|
4
|
-
import { W as WrapOptions } from '../types-
|
|
5
|
-
export { C as CachedEdgeHttpResponse, H as HonoEdgeCacheOptions, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoCache } from '../hono-
|
|
1
|
+
import { K as KeyDerivationOptions } from '../utils-Bvzn-rKS.js';
|
|
2
|
+
export { b as buildDeterministicKey, g as generateETag, s as shouldSkipCache } from '../utils-Bvzn-rKS.js';
|
|
3
|
+
import { C as CacheService } from '../cache-service-B108FzER.js';
|
|
4
|
+
import { W as WrapOptions } from '../types-BA4pz1A0.js';
|
|
5
|
+
export { C as CachedEdgeHttpResponse, H as HonoEdgeCacheOptions, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoCache } from '../hono-3rDMYVtY.js';
|
|
6
6
|
import 'crypto';
|
|
7
7
|
|
|
8
8
|
interface ExpressCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOptions {
|
|
@@ -14,12 +14,16 @@ interface ExpressCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOp
|
|
|
14
14
|
skipCache?: (req: any) => boolean;
|
|
15
15
|
/** Dynamic tags derivation from request. */
|
|
16
16
|
tags?: string[] | ((req: any) => string[]);
|
|
17
|
+
/** @deprecated Response refreshes run on live requests. Use data-level cache.get()/wrap() for SWR. */
|
|
18
|
+
swr?: number;
|
|
17
19
|
}
|
|
18
20
|
interface CachedHttpResponse {
|
|
19
21
|
body: unknown;
|
|
20
22
|
contentType?: string;
|
|
21
23
|
etag?: string;
|
|
22
24
|
status: number;
|
|
25
|
+
binary?: boolean;
|
|
26
|
+
headers?: Record<string, string | string[]>;
|
|
23
27
|
}
|
|
24
28
|
/**
|
|
25
29
|
* Creates an Express / Connect middleware that provides deterministic response caching,
|
|
@@ -60,12 +64,16 @@ interface FastifyCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOp
|
|
|
60
64
|
skipCache?: (req: any) => boolean;
|
|
61
65
|
/** Dynamic tags derivation from request. */
|
|
62
66
|
tags?: string[] | ((req: any) => string[]);
|
|
67
|
+
/** @deprecated Response refreshes run on live requests. Use data-level cache.get()/wrap() for SWR. */
|
|
68
|
+
swr?: number;
|
|
63
69
|
}
|
|
64
70
|
interface CachedFastifyResponse {
|
|
65
71
|
body: unknown;
|
|
66
72
|
contentType?: string;
|
|
67
73
|
etag?: string;
|
|
68
74
|
status: number;
|
|
75
|
+
binary?: boolean;
|
|
76
|
+
headers?: Record<string, string | string[]>;
|
|
69
77
|
}
|
|
70
78
|
/**
|
|
71
79
|
* Creates a Fastify plugin that registers `onRequest` and `onSend` lifecycle hooks.
|
package/dist/http/index.js
CHANGED
|
@@ -2,61 +2,119 @@ import {
|
|
|
2
2
|
buildEdgeDeterministicKey,
|
|
3
3
|
computeEdgeETag,
|
|
4
4
|
honoEdgeCache
|
|
5
|
-
} from "../chunk-
|
|
5
|
+
} from "../chunk-EUKB7RKP.js";
|
|
6
6
|
import {
|
|
7
7
|
buildDeterministicKey,
|
|
8
8
|
generateETag,
|
|
9
|
+
isCacheableHttpResponse,
|
|
10
|
+
isShareableHttpResponse,
|
|
11
|
+
matchesETag,
|
|
9
12
|
shouldSkipCache
|
|
10
|
-
} from "../chunk-
|
|
13
|
+
} from "../chunk-OYDU3SMI.js";
|
|
11
14
|
|
|
12
15
|
// src/http/express.ts
|
|
16
|
+
var UncacheableHttpResponse = class extends Error {
|
|
17
|
+
constructor(response, shareable) {
|
|
18
|
+
super("Response is not cacheable");
|
|
19
|
+
this.response = response;
|
|
20
|
+
this.shareable = shareable;
|
|
21
|
+
}
|
|
22
|
+
response;
|
|
23
|
+
shareable;
|
|
24
|
+
};
|
|
13
25
|
function createExpressMiddleware(options = {}) {
|
|
14
26
|
const {
|
|
15
27
|
cache,
|
|
16
28
|
etag = true,
|
|
17
29
|
ttl = 300,
|
|
18
|
-
swr,
|
|
19
30
|
tags,
|
|
20
31
|
skipCache,
|
|
21
32
|
keyGenerator,
|
|
22
|
-
headerWhitelist
|
|
33
|
+
headerWhitelist,
|
|
34
|
+
cacheAuthenticated
|
|
23
35
|
} = options;
|
|
24
36
|
return async (req, res, next) => {
|
|
25
37
|
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
26
38
|
return next();
|
|
27
39
|
}
|
|
28
|
-
if (shouldSkipCache(req, skipCache)) {
|
|
40
|
+
if (shouldSkipCache(req, skipCache, options)) {
|
|
29
41
|
return next();
|
|
30
42
|
}
|
|
31
43
|
let activeCache = cache;
|
|
32
44
|
if (!activeCache) {
|
|
33
|
-
const { CacheService } = await import("../cache-service-
|
|
45
|
+
const { CacheService } = await import("../cache-service-GYPZFB7O.js");
|
|
34
46
|
activeCache = CacheService.create();
|
|
35
47
|
}
|
|
36
|
-
const
|
|
48
|
+
const keyOptions = { keyGenerator, headerWhitelist, cacheAuthenticated };
|
|
49
|
+
const key = buildDeterministicKey(req, keyOptions);
|
|
37
50
|
const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
|
|
38
51
|
const resolvedTags = typeof tags === "function" ? tags(req) : tags;
|
|
52
|
+
let respondedLocally = false;
|
|
39
53
|
try {
|
|
40
54
|
const cached = await activeCache.get(
|
|
41
55
|
key,
|
|
42
56
|
async () => {
|
|
43
|
-
return new Promise((resolve) => {
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
const rawJson = res.json;
|
|
59
|
+
const rawSend = res.send;
|
|
60
|
+
const rawEnd = res.end;
|
|
61
|
+
const rawWrite = res.write;
|
|
62
|
+
const origJson = rawJson?.bind(res);
|
|
63
|
+
const origSend = rawSend?.bind(res);
|
|
64
|
+
const origEnd = rawEnd?.bind(res);
|
|
65
|
+
let settled = false;
|
|
66
|
+
let streamed = false;
|
|
67
|
+
const cleanup = () => {
|
|
68
|
+
if (rawJson !== void 0) res.json = rawJson;
|
|
69
|
+
if (rawSend !== void 0) res.send = rawSend;
|
|
70
|
+
if (rawEnd !== void 0) res.end = rawEnd;
|
|
71
|
+
if (rawWrite !== void 0) res.write = rawWrite;
|
|
72
|
+
res.removeListener?.("close", onClose);
|
|
73
|
+
};
|
|
74
|
+
const settle = (response) => {
|
|
75
|
+
if (settled) return;
|
|
76
|
+
settled = true;
|
|
77
|
+
respondedLocally = true;
|
|
78
|
+
response.binary = Buffer.isBuffer(response.body);
|
|
79
|
+
if (typeof res.getHeaders === "function") {
|
|
80
|
+
const headers = {};
|
|
81
|
+
for (const [name, value] of Object.entries(res.getHeaders())) {
|
|
82
|
+
if (!["content-length", "transfer-encoding", "connection", "set-cookie", "etag", "content-type"].includes(name.toLowerCase()) && value != null) {
|
|
83
|
+
headers[name] = Array.isArray(value) ? value : String(value);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
response.headers = headers;
|
|
87
|
+
}
|
|
88
|
+
const header = (name) => res.getHeader?.(name);
|
|
89
|
+
cleanup();
|
|
90
|
+
if (!streamed && isCacheableHttpResponse(response.status, header, keyOptions)) resolve(response);
|
|
91
|
+
else reject(new UncacheableHttpResponse(response, !streamed && isShareableHttpResponse(response.status, header, keyOptions)));
|
|
92
|
+
};
|
|
93
|
+
const onClose = () => {
|
|
94
|
+
if (settled) return;
|
|
95
|
+
settled = true;
|
|
96
|
+
respondedLocally = true;
|
|
97
|
+
cleanup();
|
|
98
|
+
reject(new Error("Express response closed before completion"));
|
|
99
|
+
};
|
|
100
|
+
res.once?.("close", onClose);
|
|
101
|
+
if (rawWrite) res.write = (...args) => {
|
|
102
|
+
streamed = true;
|
|
103
|
+
return rawWrite.apply(res, args);
|
|
104
|
+
};
|
|
47
105
|
const getStatusCode = () => res.statusCode ?? 200;
|
|
48
106
|
if (origJson) {
|
|
49
107
|
res.json = (body) => {
|
|
50
108
|
const status = getStatusCode();
|
|
51
109
|
const bodyEtag = etag ? generateETag(body) : void 0;
|
|
52
110
|
const contentType = res.getHeader?.("content-type") || "application/json; charset=utf-8";
|
|
53
|
-
|
|
54
|
-
if (
|
|
111
|
+
settle({ body, contentType, etag: bodyEtag, status });
|
|
112
|
+
if (isCacheableHttpResponse(status, (name) => res.getHeader?.(name), keyOptions) && bodyEtag && matchesETag(ifNoneMatch, bodyEtag)) {
|
|
55
113
|
res.status?.(304);
|
|
56
114
|
origEnd?.();
|
|
57
115
|
return;
|
|
58
116
|
}
|
|
59
|
-
if (bodyEtag && res.setHeader) {
|
|
117
|
+
if (status >= 200 && status < 300 && bodyEtag && res.setHeader) {
|
|
60
118
|
res.setHeader("ETag", bodyEtag);
|
|
61
119
|
}
|
|
62
120
|
return origJson(body);
|
|
@@ -67,42 +125,47 @@ function createExpressMiddleware(options = {}) {
|
|
|
67
125
|
const status = getStatusCode();
|
|
68
126
|
const bodyEtag = etag ? generateETag(body) : void 0;
|
|
69
127
|
const contentType = res.getHeader?.("content-type");
|
|
70
|
-
|
|
71
|
-
if (
|
|
128
|
+
settle({ body, contentType, etag: bodyEtag, status });
|
|
129
|
+
if (isCacheableHttpResponse(status, (name) => res.getHeader?.(name), keyOptions) && bodyEtag && matchesETag(ifNoneMatch, bodyEtag)) {
|
|
72
130
|
res.status?.(304);
|
|
73
131
|
origEnd?.();
|
|
74
132
|
return;
|
|
75
133
|
}
|
|
76
|
-
if (bodyEtag && res.setHeader) {
|
|
134
|
+
if (status >= 200 && status < 300 && bodyEtag && res.setHeader) {
|
|
77
135
|
res.setHeader("ETag", bodyEtag);
|
|
78
136
|
}
|
|
79
137
|
return origSend(body);
|
|
80
138
|
};
|
|
81
139
|
}
|
|
82
140
|
if (origEnd) {
|
|
83
|
-
res.end = (body) => {
|
|
141
|
+
res.end = (body, ...args) => {
|
|
84
142
|
const status = getStatusCode();
|
|
85
143
|
const bodyEtag = etag && body != null ? generateETag(body) : void 0;
|
|
86
144
|
const contentType = res.getHeader?.("content-type");
|
|
87
|
-
|
|
88
|
-
return origEnd(body);
|
|
145
|
+
settle({ body, contentType, etag: bodyEtag, status });
|
|
146
|
+
return origEnd(body, ...args);
|
|
89
147
|
};
|
|
90
148
|
}
|
|
91
|
-
|
|
149
|
+
try {
|
|
150
|
+
next();
|
|
151
|
+
} catch (err) {
|
|
152
|
+
if (!settled) {
|
|
153
|
+
settled = true;
|
|
154
|
+
cleanup();
|
|
155
|
+
reject(err);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
92
158
|
});
|
|
93
159
|
},
|
|
94
160
|
ttl,
|
|
95
|
-
|
|
161
|
+
// res/next belong to this request and cannot be reused for background SWR.
|
|
162
|
+
{ swr: 0, tags: resolvedTags }
|
|
96
163
|
);
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
});
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (!res.headersSent) {
|
|
164
|
+
if (!respondedLocally && !res.headersSent) {
|
|
165
|
+
for (const [name, value] of Object.entries(cached.headers ?? {})) res.setHeader?.(name, value);
|
|
103
166
|
if (cached.etag) {
|
|
104
167
|
if (res.setHeader) res.setHeader("ETag", cached.etag);
|
|
105
|
-
if (ifNoneMatch
|
|
168
|
+
if (matchesETag(ifNoneMatch, cached.etag)) {
|
|
106
169
|
return res.status(304).end();
|
|
107
170
|
}
|
|
108
171
|
}
|
|
@@ -112,6 +175,11 @@ function createExpressMiddleware(options = {}) {
|
|
|
112
175
|
if (cached.status && res.status) {
|
|
113
176
|
res.status(cached.status);
|
|
114
177
|
}
|
|
178
|
+
if (req.method === "HEAD") return res.end?.();
|
|
179
|
+
if (cached.binary) {
|
|
180
|
+
const body = Buffer.isBuffer(cached.body) ? cached.body : Buffer.from(cached.body.data);
|
|
181
|
+
return res.send ? res.send(body) : res.end?.(body);
|
|
182
|
+
}
|
|
115
183
|
if (typeof cached.body === "object" && cached.body !== null && res.json) {
|
|
116
184
|
return res.json(cached.body);
|
|
117
185
|
} else if (res.send) {
|
|
@@ -121,6 +189,14 @@ function createExpressMiddleware(options = {}) {
|
|
|
121
189
|
}
|
|
122
190
|
}
|
|
123
191
|
} catch (err) {
|
|
192
|
+
if (err instanceof UncacheableHttpResponse) {
|
|
193
|
+
if (respondedLocally || res.headersSent) return;
|
|
194
|
+
if (!err.shareable) return next();
|
|
195
|
+
const response = err.response;
|
|
196
|
+
if (response.contentType) res.setHeader?.("Content-Type", response.contentType);
|
|
197
|
+
res.status?.(response.status);
|
|
198
|
+
return typeof response.body === "object" && response.body !== null && !response.binary && res.json ? res.json(response.body) : res.send ? res.send(response.body) : res.end?.(response.body);
|
|
199
|
+
}
|
|
124
200
|
next(err);
|
|
125
201
|
}
|
|
126
202
|
};
|
|
@@ -128,6 +204,9 @@ function createExpressMiddleware(options = {}) {
|
|
|
128
204
|
var expressCache = createExpressMiddleware;
|
|
129
205
|
|
|
130
206
|
// src/http/fastify.ts
|
|
207
|
+
function responseBody(entry) {
|
|
208
|
+
return entry.binary && !Buffer.isBuffer(entry.body) ? Buffer.from(entry.body.data) : entry.body;
|
|
209
|
+
}
|
|
131
210
|
function createFastifyPlugin(options = {}) {
|
|
132
211
|
const plugin = async function(fastify, pluginOpts = {}) {
|
|
133
212
|
const mergedOpts = { ...options, ...pluginOpts };
|
|
@@ -135,14 +214,14 @@ function createFastifyPlugin(options = {}) {
|
|
|
135
214
|
const ttl = mergedOpts.ttl ?? 300;
|
|
136
215
|
let activeCache = mergedOpts.cache;
|
|
137
216
|
if (!activeCache) {
|
|
138
|
-
const { CacheService } = await import("../cache-service-
|
|
217
|
+
const { CacheService } = await import("../cache-service-GYPZFB7O.js");
|
|
139
218
|
activeCache = CacheService.create();
|
|
140
219
|
}
|
|
141
220
|
fastify.addHook("onRequest", async (req, reply) => {
|
|
142
221
|
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
143
222
|
return;
|
|
144
223
|
}
|
|
145
|
-
if (shouldSkipCache(req, mergedOpts.skipCache)) {
|
|
224
|
+
if (shouldSkipCache(req, mergedOpts.skipCache, mergedOpts)) {
|
|
146
225
|
return;
|
|
147
226
|
}
|
|
148
227
|
const key = buildDeterministicKey(req, {
|
|
@@ -150,15 +229,12 @@ function createFastifyPlugin(options = {}) {
|
|
|
150
229
|
headerWhitelist: mergedOpts.headerWhitelist
|
|
151
230
|
});
|
|
152
231
|
const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
|
|
153
|
-
const cached = await activeCache.
|
|
154
|
-
key,
|
|
155
|
-
async () => null,
|
|
156
|
-
0
|
|
157
|
-
);
|
|
232
|
+
const cached = await activeCache.peek(key);
|
|
158
233
|
if (cached && typeof cached === "object" && cached.body !== null) {
|
|
234
|
+
for (const [name, value] of Object.entries(cached.headers ?? {})) reply.header(name, value);
|
|
159
235
|
if (cached.etag) {
|
|
160
236
|
reply.header("ETag", cached.etag);
|
|
161
|
-
if (ifNoneMatch
|
|
237
|
+
if (matchesETag(ifNoneMatch, cached.etag)) {
|
|
162
238
|
reply.code(304);
|
|
163
239
|
return reply.send();
|
|
164
240
|
}
|
|
@@ -167,9 +243,10 @@ function createFastifyPlugin(options = {}) {
|
|
|
167
243
|
reply.header("Content-Type", cached.contentType);
|
|
168
244
|
}
|
|
169
245
|
reply.code(cached.status ?? 200);
|
|
170
|
-
return reply.send(cached
|
|
246
|
+
return reply.send(req.method === "HEAD" ? void 0 : responseBody(cached));
|
|
171
247
|
}
|
|
172
248
|
req._tricacheKey = key;
|
|
249
|
+
req._tricacheCanCommit = activeCache.captureMutationFence(key);
|
|
173
250
|
const resolvedTags = typeof mergedOpts.tags === "function" ? mergedOpts.tags(req) : mergedOpts.tags;
|
|
174
251
|
req._tricacheTags = resolvedTags;
|
|
175
252
|
});
|
|
@@ -179,26 +256,29 @@ function createFastifyPlugin(options = {}) {
|
|
|
179
256
|
return payload;
|
|
180
257
|
}
|
|
181
258
|
const statusCode = reply.statusCode ?? reply.raw?.statusCode ?? 200;
|
|
182
|
-
if (
|
|
259
|
+
if (req._tricacheCanCommit?.() && isCacheableHttpResponse(statusCode, (name) => reply.getHeader(name), mergedOpts) && payload !== void 0 && payload !== null && !(typeof payload === "object" && typeof payload.pipe === "function")) {
|
|
183
260
|
const payloadEtag = etag ? generateETag(payload) : void 0;
|
|
184
261
|
const contentType = reply.getHeader("content-type") || (typeof payload === "object" ? "application/json" : "text/plain");
|
|
185
262
|
if (payloadEtag) {
|
|
186
263
|
reply.header("ETag", payloadEtag);
|
|
187
264
|
}
|
|
188
265
|
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
266
|
const entry = {
|
|
194
267
|
body: payload,
|
|
195
268
|
contentType,
|
|
196
269
|
etag: payloadEtag,
|
|
197
|
-
status: statusCode
|
|
270
|
+
status: statusCode,
|
|
271
|
+
binary: Buffer.isBuffer(payload),
|
|
272
|
+
headers: reply.getHeaders ? Object.fromEntries(Object.entries(reply.getHeaders()).filter(([name]) => !["content-length", "transfer-encoding", "connection", "set-cookie", "content-type", "etag"].includes(name.toLowerCase()))) : void 0
|
|
198
273
|
};
|
|
199
274
|
void activeCache.set(key, entry, mergedOpts.ttl ?? ttl, void 0, {
|
|
200
275
|
tags: req._tricacheTags
|
|
276
|
+
}).catch(() => {
|
|
201
277
|
});
|
|
278
|
+
if (payloadEtag && matchesETag(ifNoneMatch, payloadEtag)) {
|
|
279
|
+
reply.code(304);
|
|
280
|
+
return "";
|
|
281
|
+
}
|
|
202
282
|
}
|
|
203
283
|
return payload;
|
|
204
284
|
});
|
|
@@ -216,10 +296,10 @@ function fastifyCache(options = {}) {
|
|
|
216
296
|
const req = first;
|
|
217
297
|
const reply = second;
|
|
218
298
|
if (req.method !== "GET" && req.method !== "HEAD") return;
|
|
219
|
-
if (shouldSkipCache(req, options.skipCache)) return;
|
|
299
|
+
if (shouldSkipCache(req, options.skipCache, options)) return;
|
|
220
300
|
let activeCache = options.cache;
|
|
221
301
|
if (!activeCache) {
|
|
222
|
-
const { CacheService } = await import("../cache-service-
|
|
302
|
+
const { CacheService } = await import("../cache-service-GYPZFB7O.js");
|
|
223
303
|
activeCache = CacheService.create();
|
|
224
304
|
}
|
|
225
305
|
const key = buildDeterministicKey(req, {
|
|
@@ -227,15 +307,12 @@ function fastifyCache(options = {}) {
|
|
|
227
307
|
headerWhitelist: options.headerWhitelist
|
|
228
308
|
});
|
|
229
309
|
const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
|
|
230
|
-
const cached = await activeCache.
|
|
231
|
-
key,
|
|
232
|
-
async () => null,
|
|
233
|
-
0
|
|
234
|
-
);
|
|
310
|
+
const cached = await activeCache.peek(key);
|
|
235
311
|
if (cached && typeof cached === "object" && cached.body !== null) {
|
|
312
|
+
for (const [name, value] of Object.entries(cached.headers ?? {})) reply.header?.(name, value);
|
|
236
313
|
if (cached.etag) {
|
|
237
314
|
if (reply.header) reply.header("ETag", cached.etag);
|
|
238
|
-
if (ifNoneMatch
|
|
315
|
+
if (matchesETag(ifNoneMatch, cached.etag)) {
|
|
239
316
|
if (reply.code) reply.code(304);
|
|
240
317
|
else if (reply.status) reply.status(304);
|
|
241
318
|
if (reply.send) return reply.send();
|
|
@@ -249,9 +326,10 @@ function fastifyCache(options = {}) {
|
|
|
249
326
|
if (reply.code) reply.code(cached.status);
|
|
250
327
|
else if (reply.status) reply.status(cached.status);
|
|
251
328
|
}
|
|
252
|
-
if (reply.send) return reply.send(cached
|
|
329
|
+
if (reply.send) return reply.send(req.method === "HEAD" ? void 0 : responseBody(cached));
|
|
253
330
|
return;
|
|
254
331
|
}
|
|
332
|
+
const canCommit = activeCache.captureMutationFence(key);
|
|
255
333
|
const origSend = reply.send?.bind(reply);
|
|
256
334
|
if (origSend) {
|
|
257
335
|
reply.send = function(resPayload) {
|
|
@@ -259,17 +337,19 @@ function fastifyCache(options = {}) {
|
|
|
259
337
|
const payloadEtag = options.etag !== false ? generateETag(resPayload) : void 0;
|
|
260
338
|
const contentType = reply.getHeader && reply.getHeader("content-type") || (typeof resPayload === "object" ? "application/json" : "text/plain");
|
|
261
339
|
const respStatus = reply.statusCode ?? reply.raw?.statusCode ?? 200;
|
|
262
|
-
|
|
340
|
+
const cacheable = isCacheableHttpResponse(respStatus, (name) => reply.getHeader?.(name), options);
|
|
341
|
+
if (cacheable && canCommit() && !(typeof resPayload === "object" && typeof resPayload.pipe === "function")) {
|
|
263
342
|
const resolvedTags = typeof options.tags === "function" ? options.tags(req) : options.tags;
|
|
264
343
|
void activeCache.set(
|
|
265
344
|
key,
|
|
266
|
-
{ body: resPayload, contentType, etag: payloadEtag, status: respStatus },
|
|
345
|
+
{ body: resPayload, contentType, etag: payloadEtag, status: respStatus, binary: Buffer.isBuffer(resPayload) },
|
|
267
346
|
options.ttl ?? 300,
|
|
268
347
|
void 0,
|
|
269
348
|
{ tags: resolvedTags }
|
|
270
|
-
)
|
|
349
|
+
).catch(() => {
|
|
350
|
+
});
|
|
271
351
|
}
|
|
272
|
-
if (
|
|
352
|
+
if (cacheable && payloadEtag && matchesETag(ifNoneMatch, payloadEtag)) {
|
|
273
353
|
if (reply.code) reply.code(304);
|
|
274
354
|
else if (reply.status) reply.status(304);
|
|
275
355
|
return origSend();
|