tricache 0.8.0 → 1.0.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.
Files changed (56) hide show
  1. package/README.md +10 -4
  2. package/dashboards/prometheus-scrape.yml +18 -0
  3. package/dist/{cache-service-5MZXFIGK.js → cache-service-26GCO3UU.js} +3 -3
  4. package/dist/{cache-service-CyLdpo77.d.ts → cache-service-pJ-nFcVj.d.ts} +44 -12
  5. package/dist/{chunk-XZL45ABR.js → chunk-2AJKEQMZ.js} +54 -1
  6. package/dist/{chunk-6PQ5MOWR.js → chunk-7SZSQS55.js} +571 -115
  7. package/dist/chunk-BQRURGGD.js +60 -0
  8. package/dist/chunk-IITRFWHY.js +196 -0
  9. package/dist/{chunk-LJ5QJB5O.js → chunk-UBWLPKTI.js} +127 -11
  10. package/dist/cli.cjs +767 -133
  11. package/dist/cli.js +24 -6
  12. package/dist/dashboard/index.d.ts +2 -2
  13. package/dist/drizzle/index.cjs +734 -132
  14. package/dist/drizzle/index.d.ts +2 -2
  15. package/dist/drizzle/index.js +1 -1
  16. package/dist/edge/index.cjs +163 -46
  17. package/dist/edge/index.d.ts +3 -3
  18. package/dist/edge/index.js +42 -14
  19. package/dist/hono/index.cjs +8093 -0
  20. package/dist/hono/index.d.ts +91 -0
  21. package/dist/hono/index.js +185 -0
  22. package/dist/{hono-DQRS1Xd7.d.ts → hono-DuNvciyq.d.ts} +6 -5
  23. package/dist/http/index.cjs +833 -170
  24. package/dist/http/index.d.ts +6 -30
  25. package/dist/http/index.js +9 -59
  26. package/dist/index.cjs +755 -130
  27. package/dist/index.d.ts +35 -6
  28. package/dist/index.js +16 -6
  29. package/dist/nestjs/index.cjs +725 -130
  30. package/dist/nestjs/index.d.ts +2 -2
  31. package/dist/nestjs/index.js +4 -4
  32. package/dist/next/index.cjs +725 -130
  33. package/dist/next/index.d.ts +2 -2
  34. package/dist/next/index.js +3 -3
  35. package/dist/prisma/index.d.ts +2 -2
  36. package/dist/serialize-worker.cjs +127 -9
  37. package/dist/serialize-worker.js +8 -4
  38. package/dist/{types-B6krw64X.d.ts → types-5HXqBfAf.d.ts} +74 -7
  39. package/dist/utils-CEsU_Z0s.d.ts +27 -0
  40. package/package.json +22 -9
  41. package/src/cache-service.ts +540 -68
  42. package/src/cli.ts +21 -4
  43. package/src/compression.ts +193 -15
  44. package/src/cross-region.ts +1 -1
  45. package/src/edge/cache.ts +38 -11
  46. package/src/edge/hono.ts +123 -42
  47. package/src/env.d.ts +13 -0
  48. package/src/hono/index.ts +316 -0
  49. package/src/http/index.ts +5 -0
  50. package/src/index.ts +9 -1
  51. package/src/ipc-telemetry.ts +29 -1
  52. package/src/serialize-worker.ts +6 -2
  53. package/src/types.ts +68 -5
  54. package/src/utils/tag-matcher.ts +90 -0
  55. package/dist/chunk-ZTSWCFYO.js +0 -135
  56. /package/dist/{chunk-QNU7B43J.js → chunk-VXFTFZ5N.js} +0 -0
@@ -1,135 +0,0 @@
1
- // src/edge/hono.ts
2
- async function computeEdgeETag(text) {
3
- try {
4
- if (typeof crypto !== "undefined" && crypto.subtle) {
5
- const encoder = new TextEncoder();
6
- const data = encoder.encode(text);
7
- const hashBuf = await crypto.subtle.digest("SHA-1", data);
8
- const hashArr = Array.from(new Uint8Array(hashBuf));
9
- const hashHex = hashArr.map((b) => b.toString(16).padStart(2, "0")).join("").slice(0, 16);
10
- return `W/"${text.length.toString(16)}-${hashHex}"`;
11
- }
12
- } catch {
13
- }
14
- let h = 0;
15
- for (let i = 0; i < text.length; i++) {
16
- h = Math.imul(31, h) + text.charCodeAt(i) | 0;
17
- }
18
- return `W/"${text.length.toString(16)}-${Math.abs(h).toString(16)}"`;
19
- }
20
- function buildEdgeDeterministicKey(c, options) {
21
- if (options?.keyGenerator) {
22
- return options.keyGenerator(c);
23
- }
24
- const req = c.req;
25
- const method = (req.method || "GET").toUpperCase();
26
- const rawUrl = req.url || "/";
27
- const qIdx = rawUrl.indexOf("?");
28
- const path = qIdx >= 0 ? rawUrl.slice(0, qIdx) : rawUrl;
29
- let queryString = "";
30
- if (qIdx >= 0) {
31
- const rawQuery = rawUrl.slice(qIdx + 1);
32
- try {
33
- const params = new URLSearchParams(rawQuery);
34
- params.sort();
35
- queryString = params.toString();
36
- } catch {
37
- queryString = rawQuery;
38
- }
39
- }
40
- let key = `edge:http:${method}:${path}`;
41
- if (queryString) {
42
- key += `?${queryString}`;
43
- }
44
- if (options?.headerWhitelist && options.headerWhitelist.length > 0 && req.header) {
45
- const sortedHeaders = [...options.headerWhitelist].sort();
46
- for (const h of sortedHeaders) {
47
- const val = req.header(h);
48
- if (val !== void 0 && val !== null && val !== "") {
49
- key += `|h:${h.toLowerCase()}=${val}`;
50
- }
51
- }
52
- }
53
- return key;
54
- }
55
- function honoEdgeCache(options = {}) {
56
- const {
57
- cache,
58
- etag = true,
59
- ttl = 300,
60
- swr,
61
- tags,
62
- skipCache,
63
- keyGenerator,
64
- headerWhitelist
65
- } = options;
66
- return async (c, next) => {
67
- const method = c.req.method;
68
- if (method !== "GET" && method !== "HEAD") {
69
- return await next();
70
- }
71
- if (skipCache && skipCache(c)) {
72
- return await next();
73
- }
74
- const cc = c.req.header?.("cache-control") || c.req.header?.("Cache-Control");
75
- if (typeof cc === "string") {
76
- const lower = cc.toLowerCase();
77
- if (lower.includes("no-cache") || lower.includes("no-store")) {
78
- return await next();
79
- }
80
- }
81
- if (!cache) {
82
- return await next();
83
- }
84
- const key = buildEdgeDeterministicKey(c, { keyGenerator, headerWhitelist });
85
- const ifNoneMatch = c.req.header?.("if-none-match") || c.req.header?.("If-None-Match");
86
- const resolvedTags = typeof tags === "function" ? tags(c) : tags;
87
- const cached = await cache.get(
88
- key,
89
- async () => {
90
- await next();
91
- const res = c.res;
92
- if (!res) {
93
- return null;
94
- }
95
- const clone = res.clone();
96
- const text = await clone.text();
97
- const statusCode = res.status ?? 200;
98
- const bodyEtag = etag ? await computeEdgeETag(text) : void 0;
99
- const contentType = res.headers.get("content-type") || "text/plain; charset=utf-8";
100
- return {
101
- body: text,
102
- contentType,
103
- etag: bodyEtag,
104
- status: statusCode
105
- };
106
- },
107
- ttl,
108
- {
109
- swr,
110
- tags: resolvedTags,
111
- ctx: c.executionCtx
112
- }
113
- );
114
- if (!cached) {
115
- return;
116
- }
117
- if (typeof cached.status === "number" && (cached.status < 200 || cached.status >= 300)) {
118
- await cache.delete?.(key);
119
- return;
120
- }
121
- if (cached.etag && ifNoneMatch === cached.etag) {
122
- return c.body(null, 304, { ETag: cached.etag });
123
- }
124
- const headers = {};
125
- if (cached.etag) headers["ETag"] = cached.etag;
126
- if (cached.contentType) headers["Content-Type"] = cached.contentType;
127
- return c.body(cached.body, cached.status ?? 200, headers);
128
- };
129
- }
130
-
131
- export {
132
- computeEdgeETag,
133
- buildEdgeDeterministicKey,
134
- honoEdgeCache
135
- };
File without changes