tricache 0.6.7 → 0.7.1
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 +1 -1
- package/README.md +1428 -970
- package/bin/tricache.js +8 -0
- package/dist/cache-service-5GATUYAK.js +7 -0
- package/dist/cache-service-D8tIXmzb.d.ts +1498 -0
- package/dist/chunk-EUGYR7KR.js +4629 -0
- package/dist/chunk-UBSR5UYA.js +0 -0
- package/dist/{chunk-FKY5R3CE.js → chunk-X5AXHPVD.js} +139 -6
- package/dist/cli.cjs +5238 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +130 -0
- package/dist/drizzle/index.cjs +5218 -0
- package/dist/drizzle/index.d.ts +44 -0
- package/dist/drizzle/index.js +46 -0
- package/dist/http/index.cjs +5374 -0
- package/dist/http/index.d.ts +33 -0
- package/dist/http/index.js +200 -0
- package/dist/index.cjs +1245 -209
- package/dist/index.d.ts +15 -1230
- package/dist/index.js +11 -3705
- package/dist/nestjs/index.cjs +5362 -0
- package/dist/nestjs/index.d.ts +210 -0
- package/dist/nestjs/index.js +250 -0
- package/dist/next/index.cjs +5407 -0
- package/dist/next/index.d.ts +208 -0
- package/dist/next/index.js +297 -0
- package/dist/prisma/index.cjs +123 -0
- package/dist/prisma/index.d.ts +60 -0
- package/dist/prisma/index.js +87 -0
- package/dist/serialize-worker.cjs +190 -15
- package/dist/serialize-worker.js +63 -12
- package/package.json +71 -21
- package/src/cache-service.ts +3294 -2429
- package/src/cli.ts +143 -0
- package/src/compression.ts +56 -0
- package/src/disk-tier.ts +821 -757
- package/src/drizzle/index.ts +86 -0
- package/src/encryption.ts +513 -442
- package/src/http/index.ts +265 -0
- package/src/index.ts +7 -0
- package/src/nestjs/decorators.ts +161 -0
- package/src/nestjs/index.ts +23 -0
- package/src/nestjs/tricache.module.ts +104 -0
- package/src/nestjs/tricache.store.ts +97 -0
- package/src/nestjs/types.ts +79 -0
- package/src/next/cache-handler.ts +268 -0
- package/src/next/index.ts +37 -0
- package/src/next/isr-handler.ts +105 -0
- package/src/next/types.ts +130 -0
- package/src/prisma/index.ts +154 -0
- package/src/serialize-worker.ts +77 -13
- package/src/smart-memory-cache.ts +1026 -1010
- package/src/types.ts +911 -732
- package/src/wasm/bloom-filter-wasm.ts +3 -2
- package/src/worker-pool.ts +156 -42
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { W as WrapOptions, C as CacheService } from '../cache-service-D8tIXmzb.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* tricache/http — Universal HTTP Caching, ETag generation, and 304 Not Modified middleware
|
|
5
|
+
* for Express, Fastify, and Hono.
|
|
6
|
+
*
|
|
7
|
+
* Usage with Express:
|
|
8
|
+
* import { expressCache } from 'tricache/http';
|
|
9
|
+
* app.get('/api/users', expressCache({ cache, ttl: 300, tags: ['users'] }), handler);
|
|
10
|
+
*
|
|
11
|
+
* Usage with Hono:
|
|
12
|
+
* import { honoCache } from 'tricache/http';
|
|
13
|
+
* app.get('/api/users', honoCache({ cache, ttl: 300, tags: ['users'] }), handler);
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
interface HttpCacheOptions extends WrapOptions {
|
|
17
|
+
/** TriCache instance. If omitted, uses singleton CacheService.create(). */
|
|
18
|
+
cache?: CacheService;
|
|
19
|
+
/** Custom key derivation function. Defaults to `${method}:${url}`. */
|
|
20
|
+
keyGenerator?: (req: any) => string;
|
|
21
|
+
/** Whether to emit weak ETags (default: true). */
|
|
22
|
+
etag?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/** Compute a weak ETag from a string, Buffer, or object */
|
|
25
|
+
declare function generateETag(data: unknown): string;
|
|
26
|
+
/** Express / Connect middleware */
|
|
27
|
+
declare function expressCache(options?: HttpCacheOptions): (req: any, res: any, next: any) => Promise<any>;
|
|
28
|
+
/** Hono / Web Standard middleware */
|
|
29
|
+
declare function honoCache(options?: HttpCacheOptions): (c: any, next: any) => Promise<any>;
|
|
30
|
+
/** Fastify preHandler hook */
|
|
31
|
+
declare function fastifyCache(options?: HttpCacheOptions): (req: any, reply: any) => Promise<any>;
|
|
32
|
+
|
|
33
|
+
export { type HttpCacheOptions, expressCache, fastifyCache, generateETag, honoCache };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// src/http/index.ts
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
function generateETag(data) {
|
|
4
|
+
const str = typeof data === "string" ? data : Buffer.isBuffer(data) ? data.toString("utf8") : JSON.stringify(data);
|
|
5
|
+
const hash = crypto.createHash("sha1").update(str).digest("hex").slice(0, 16);
|
|
6
|
+
return `W/"${str.length.toString(16)}-${hash}"`;
|
|
7
|
+
}
|
|
8
|
+
function expressCache(options = {}) {
|
|
9
|
+
const { cache, keyGenerator, etag = true, ttl = 300, swr, tags } = options;
|
|
10
|
+
return async (req, res, next) => {
|
|
11
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
12
|
+
return next();
|
|
13
|
+
}
|
|
14
|
+
let activeCache = cache;
|
|
15
|
+
if (!activeCache) {
|
|
16
|
+
const { CacheService } = await import("../cache-service-5GATUYAK.js");
|
|
17
|
+
activeCache = CacheService.create();
|
|
18
|
+
}
|
|
19
|
+
const key = keyGenerator ? keyGenerator(req) : `http:${req.method}:${req.originalUrl || req.url}`;
|
|
20
|
+
const ifNoneMatch = req.headers ? req.headers["if-none-match"] : void 0;
|
|
21
|
+
try {
|
|
22
|
+
const cached = await activeCache.get(
|
|
23
|
+
key,
|
|
24
|
+
async () => {
|
|
25
|
+
return new Promise((resolve) => {
|
|
26
|
+
const origJson = res.json?.bind(res);
|
|
27
|
+
const origSend = res.send?.bind(res);
|
|
28
|
+
const origEnd = res.end?.bind(res);
|
|
29
|
+
const statusOf = () => res.statusCode;
|
|
30
|
+
if (origJson) {
|
|
31
|
+
res.json = (body) => {
|
|
32
|
+
const bodyEtag = etag ? generateETag(body) : void 0;
|
|
33
|
+
resolve({ body, contentType: "application/json; charset=utf-8", etag: bodyEtag, status: statusOf() });
|
|
34
|
+
if (ifNoneMatch && ifNoneMatch === bodyEtag) {
|
|
35
|
+
res.status?.(304);
|
|
36
|
+
origEnd?.();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (bodyEtag && res.setHeader) res.setHeader("ETag", bodyEtag);
|
|
40
|
+
return origJson(body);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (origSend) {
|
|
44
|
+
res.send = (body) => {
|
|
45
|
+
const bodyEtag = etag ? generateETag(body) : void 0;
|
|
46
|
+
resolve({ body, contentType: res.getHeader ? res.getHeader("content-type") : void 0, etag: bodyEtag, status: statusOf() });
|
|
47
|
+
if (ifNoneMatch && ifNoneMatch === bodyEtag) {
|
|
48
|
+
res.status?.(304);
|
|
49
|
+
origEnd?.();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (bodyEtag && res.setHeader) res.setHeader("ETag", bodyEtag);
|
|
53
|
+
return origSend(body);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (origEnd) {
|
|
57
|
+
res.end = (body) => {
|
|
58
|
+
const bodyEtag = etag && body != null ? generateETag(body) : void 0;
|
|
59
|
+
resolve({ body, contentType: res.getHeader ? res.getHeader("content-type") : void 0, etag: bodyEtag, status: statusOf() });
|
|
60
|
+
return origEnd(body);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
next();
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
ttl,
|
|
67
|
+
{ swr, tags }
|
|
68
|
+
);
|
|
69
|
+
const respStatus = cached.status;
|
|
70
|
+
if (typeof respStatus === "number" && (respStatus < 200 || respStatus >= 300)) {
|
|
71
|
+
await activeCache.delete(key).catch(() => {
|
|
72
|
+
});
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (!res.headersSent) {
|
|
76
|
+
if (cached.etag) {
|
|
77
|
+
if (res.setHeader) res.setHeader("ETag", cached.etag);
|
|
78
|
+
if (ifNoneMatch === cached.etag) {
|
|
79
|
+
return res.status(304).end();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (cached.contentType && res.setHeader) {
|
|
83
|
+
res.setHeader("Content-Type", cached.contentType);
|
|
84
|
+
}
|
|
85
|
+
if (typeof cached.body === "object" && cached.body !== null && res.json) {
|
|
86
|
+
return res.json(cached.body);
|
|
87
|
+
} else if (res.send) {
|
|
88
|
+
return res.send(cached.body);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
} catch (err) {
|
|
92
|
+
next(err);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function honoCache(options = {}) {
|
|
97
|
+
const { cache, keyGenerator, etag = true, ttl = 300, swr, tags } = options;
|
|
98
|
+
return async (c, next) => {
|
|
99
|
+
if (c.req.method !== "GET" && c.req.method !== "HEAD") {
|
|
100
|
+
return await next();
|
|
101
|
+
}
|
|
102
|
+
let activeCache = cache;
|
|
103
|
+
if (!activeCache) {
|
|
104
|
+
const { CacheService } = await import("../cache-service-5GATUYAK.js");
|
|
105
|
+
activeCache = CacheService.create();
|
|
106
|
+
}
|
|
107
|
+
const key = keyGenerator ? keyGenerator(c.req) : `http:${c.req.method}:${c.req.url}`;
|
|
108
|
+
const ifNoneMatch = c.req.header ? c.req.header("if-none-match") : void 0;
|
|
109
|
+
const cached = await activeCache.get(
|
|
110
|
+
key,
|
|
111
|
+
async () => {
|
|
112
|
+
await next();
|
|
113
|
+
const res = c.res;
|
|
114
|
+
const text = await res.clone().text();
|
|
115
|
+
const bodyEtag = etag ? generateETag(text) : void 0;
|
|
116
|
+
return {
|
|
117
|
+
body: text,
|
|
118
|
+
contentType: res.headers.get("content-type") ?? "text/plain",
|
|
119
|
+
etag: bodyEtag,
|
|
120
|
+
status: typeof res.status === "number" ? res.status : void 0
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
ttl,
|
|
124
|
+
{ swr, tags }
|
|
125
|
+
);
|
|
126
|
+
if (typeof cached.status === "number" && (cached.status < 200 || cached.status >= 300)) {
|
|
127
|
+
await activeCache.delete(key).catch(() => {
|
|
128
|
+
});
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (cached.etag && ifNoneMatch === cached.etag) {
|
|
132
|
+
return c.body(null, 304, { ETag: cached.etag });
|
|
133
|
+
}
|
|
134
|
+
const headers = {};
|
|
135
|
+
if (cached.etag) headers["ETag"] = cached.etag;
|
|
136
|
+
if (cached.contentType) headers["Content-Type"] = cached.contentType;
|
|
137
|
+
return c.body(cached.body, 200, headers);
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function fastifyCache(options = {}) {
|
|
141
|
+
const { cache, keyGenerator, etag = true, ttl = 300, tags } = options;
|
|
142
|
+
return async (req, reply) => {
|
|
143
|
+
if (req.method !== "GET" && req.method !== "HEAD") return;
|
|
144
|
+
let activeCache = cache;
|
|
145
|
+
if (!activeCache) {
|
|
146
|
+
const { CacheService } = await import("../cache-service-5GATUYAK.js");
|
|
147
|
+
activeCache = CacheService.create();
|
|
148
|
+
}
|
|
149
|
+
const key = keyGenerator ? keyGenerator(req) : `http:${req.method}:${req.url}`;
|
|
150
|
+
const ifNoneMatch = req.headers ? req.headers["if-none-match"] : void 0;
|
|
151
|
+
const cached = await activeCache.get(
|
|
152
|
+
key,
|
|
153
|
+
async () => null,
|
|
154
|
+
0
|
|
155
|
+
);
|
|
156
|
+
if (cached) {
|
|
157
|
+
if (cached.etag) {
|
|
158
|
+
if (reply.header) reply.header("ETag", cached.etag);
|
|
159
|
+
if (ifNoneMatch === cached.etag) {
|
|
160
|
+
if (reply.code) reply.code(304);
|
|
161
|
+
else if (reply.status) reply.status(304);
|
|
162
|
+
if (reply.send) return reply.send();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (cached.contentType && reply.header) {
|
|
167
|
+
reply.header("Content-Type", cached.contentType);
|
|
168
|
+
}
|
|
169
|
+
if (reply.send) return reply.send(cached.body);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const origSend = reply.send?.bind(reply);
|
|
173
|
+
if (origSend) {
|
|
174
|
+
reply.send = function(payload) {
|
|
175
|
+
if (payload !== void 0 && payload !== null) {
|
|
176
|
+
const payloadEtag = etag ? generateETag(payload) : void 0;
|
|
177
|
+
const contentType = reply.getHeader && reply.getHeader("content-type") || (typeof payload === "object" ? "application/json" : "text/plain");
|
|
178
|
+
const respStatus = reply.statusCode ?? reply.raw?.statusCode;
|
|
179
|
+
if (respStatus === void 0 || respStatus >= 200 && respStatus < 300) {
|
|
180
|
+
activeCache.set(key, { body: payload, contentType, etag: payloadEtag }, ttl, void 0, { tags }).catch(() => {
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
if (ifNoneMatch && ifNoneMatch === payloadEtag) {
|
|
184
|
+
if (reply.code) reply.code(304);
|
|
185
|
+
else if (reply.status) reply.status(304);
|
|
186
|
+
return origSend();
|
|
187
|
+
}
|
|
188
|
+
if (payloadEtag && reply.header) reply.header("ETag", payloadEtag);
|
|
189
|
+
}
|
|
190
|
+
return origSend(payload);
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
export {
|
|
196
|
+
expressCache,
|
|
197
|
+
fastifyCache,
|
|
198
|
+
generateETag,
|
|
199
|
+
honoCache
|
|
200
|
+
};
|