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.
Files changed (100) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +144 -1428
  3. package/bin/tricache.js +8 -8
  4. package/dashboards/tricache-alerts.yaml +54 -0
  5. package/dashboards/tricache-grafana.json +548 -0
  6. package/dist/cache-service-5MZXFIGK.js +12 -0
  7. package/dist/cache-service-CyLdpo77.d.ts +871 -0
  8. package/dist/{chunk-3XQUQBOZ.js → chunk-6PQ5MOWR.js} +2950 -523
  9. package/dist/chunk-7RV5ZVBB.js +1023 -0
  10. package/dist/{chunk-PFB5FMEY.js → chunk-LJ5QJB5O.js} +116 -20
  11. package/dist/chunk-QNU7B43J.js +687 -0
  12. package/dist/chunk-UGD4VEPD.js +138 -0
  13. package/dist/chunk-XZL45ABR.js +108 -0
  14. package/dist/chunk-ZTSWCFYO.js +135 -0
  15. package/dist/cli.cjs +2894 -518
  16. package/dist/cli.d.ts +1 -0
  17. package/dist/cli.js +103 -5
  18. package/dist/dashboard/index.cjs +1065 -0
  19. package/dist/dashboard/index.d.ts +183 -0
  20. package/dist/dashboard/index.js +22 -0
  21. package/dist/drizzle/index.cjs +2648 -508
  22. package/dist/drizzle/index.d.ts +3 -1
  23. package/dist/drizzle/index.js +1 -1
  24. package/dist/edge/index.cjs +1498 -0
  25. package/dist/edge/index.d.ts +127 -0
  26. package/dist/edge/index.js +1109 -0
  27. package/dist/hono-DQRS1Xd7.d.ts +370 -0
  28. package/dist/http/index.cjs +3038 -589
  29. package/dist/http/index.d.ts +109 -23
  30. package/dist/http/index.js +238 -62
  31. package/dist/index.cjs +6962 -2487
  32. package/dist/index.d.ts +547 -30
  33. package/dist/index.js +104 -5
  34. package/dist/murmur3-CMGw9PbV.d.ts +98 -0
  35. package/dist/nestjs/index.cjs +2586 -498
  36. package/dist/nestjs/index.d.ts +3 -1
  37. package/dist/nestjs/index.js +6 -3
  38. package/dist/next/index.cjs +2633 -519
  39. package/dist/next/index.d.ts +13 -7
  40. package/dist/next/index.js +45 -18
  41. package/dist/prisma/index.cjs +2 -1
  42. package/dist/prisma/index.d.ts +3 -1
  43. package/dist/prisma/index.js +2 -1
  44. package/dist/serialize-worker.cjs +31 -19
  45. package/dist/serialize-worker.js +1 -1
  46. package/dist/{cache-service-BblHuF3i.d.ts → types-B6krw64X.d.ts} +605 -646
  47. package/package.json +37 -7
  48. package/src/adapters/auto-pipeliner.ts +206 -0
  49. package/src/adapters/node-redis.ts +370 -0
  50. package/src/cache-service.ts +4231 -3226
  51. package/src/cli.ts +261 -143
  52. package/src/codec.ts +72 -0
  53. package/src/compression.ts +56 -63
  54. package/src/cross-region.ts +245 -0
  55. package/src/dashboard/handler.ts +392 -0
  56. package/src/dashboard/html.ts +594 -0
  57. package/src/dashboard/index.ts +195 -0
  58. package/src/dashboard/types.ts +96 -0
  59. package/src/disk-tier.ts +1223 -821
  60. package/src/drizzle/index.ts +86 -86
  61. package/src/edge/adapters/cloudflare-do.ts +130 -0
  62. package/src/edge/adapters/cloudflare-kv.ts +115 -0
  63. package/src/edge/adapters/upstash.ts +143 -0
  64. package/src/edge/cache.ts +797 -0
  65. package/src/edge/crypto.ts +182 -0
  66. package/src/edge/hono.ts +203 -0
  67. package/src/edge/index.ts +37 -0
  68. package/src/edge/types.ts +230 -0
  69. package/src/edge/utils/base64.ts +63 -0
  70. package/src/edge/utils/murmur3.ts +179 -0
  71. package/src/encryption.ts +635 -493
  72. package/src/http/express.ts +195 -0
  73. package/src/http/fastify.ts +260 -0
  74. package/src/http/index.ts +46 -223
  75. package/src/http/utils.ts +92 -0
  76. package/src/index.ts +149 -47
  77. package/src/ipc-telemetry.ts +481 -0
  78. package/src/latency-watchdog.ts +282 -0
  79. package/src/nestjs/decorators.ts +161 -161
  80. package/src/nestjs/index.ts +23 -23
  81. package/src/nestjs/tricache.module.ts +104 -104
  82. package/src/nestjs/tricache.store.ts +97 -97
  83. package/src/nestjs/types.ts +79 -79
  84. package/src/next/cache-handler.ts +271 -244
  85. package/src/next/index.ts +37 -37
  86. package/src/next/isr-handler.ts +105 -105
  87. package/src/next/types.ts +130 -130
  88. package/src/prisma/index.ts +154 -151
  89. package/src/remote-snapshot.ts +153 -0
  90. package/src/serialize-worker.ts +120 -120
  91. package/src/sigv4-snapshot-adapter.ts +260 -0
  92. package/src/smart-memory-cache.ts +1109 -1024
  93. package/src/types.ts +1199 -902
  94. package/src/utils/cgroup.ts +100 -0
  95. package/src/utils/tracing.ts +54 -0
  96. package/src/wasm/bloom-filter-wasm.ts +109 -102
  97. package/src/worker-pool.ts +299 -299
  98. package/src/wtiny-lfu.ts +624 -0
  99. package/dist/cache-service-AJASH5FX.js +0 -7
  100. package/dist/chunk-UBSR5UYA.js +0 -0
@@ -0,0 +1,138 @@
1
+ // src/edge/utils/murmur3.ts
2
+ function murmur3_32(key, seed = 0) {
3
+ const c1 = 3432918353;
4
+ const c2 = 461845907;
5
+ let h1 = seed >>> 0;
6
+ const len = key.length;
7
+ const remainder = len & 1;
8
+ const bytes = len - remainder;
9
+ for (let i = 0; i < bytes; i += 2) {
10
+ let k1 = key.charCodeAt(i) & 65535 | (key.charCodeAt(i + 1) & 65535) << 16;
11
+ k1 = Math.imul(k1, c1);
12
+ k1 = k1 << 15 | k1 >>> 17;
13
+ k1 = Math.imul(k1, c2);
14
+ h1 ^= k1;
15
+ h1 = h1 << 13 | h1 >>> 19;
16
+ h1 = Math.imul(h1, 5) + 3864292196 >>> 0;
17
+ }
18
+ if (remainder === 1) {
19
+ let k1 = key.charCodeAt(bytes) & 65535;
20
+ k1 = Math.imul(k1, c1);
21
+ k1 = k1 << 15 | k1 >>> 17;
22
+ k1 = Math.imul(k1, c2);
23
+ h1 ^= k1;
24
+ }
25
+ h1 ^= len;
26
+ h1 ^= h1 >>> 16;
27
+ h1 = Math.imul(h1, 2246822507);
28
+ h1 ^= h1 >>> 13;
29
+ h1 = Math.imul(h1, 3266489909);
30
+ h1 ^= h1 >>> 16;
31
+ return h1 >>> 0;
32
+ }
33
+ var Murmur3BloomFilter = class {
34
+ bits;
35
+ numBits;
36
+ k;
37
+ _maxCapacity;
38
+ _insertionCount = 0;
39
+ /**
40
+ * @param numBits Total bits in the filter (default: 100,000 bits = 12.5 KB)
41
+ * @param k Number of hash probes per key (default: 7, targeting ~1% FPR at 2,000 items)
42
+ */
43
+ constructor(numBits = 1e5, k = 7) {
44
+ this.numBits = Math.max(64, numBits);
45
+ this.k = Math.max(1, k);
46
+ this.bits = new Uint8Array(Math.ceil(this.numBits / 8));
47
+ const p = 0.01;
48
+ this._maxCapacity = Math.floor(-this.numBits * Math.log(1 - Math.pow(p, 1 / this.k)) / this.k);
49
+ }
50
+ setBit(bitIndex) {
51
+ const byteIndex = bitIndex >>> 3;
52
+ this.bits[byteIndex] |= 1 << (bitIndex & 7);
53
+ }
54
+ getBit(bitIndex) {
55
+ const byteIndex = bitIndex >>> 3;
56
+ return (this.bits[byteIndex] & 1 << (bitIndex & 7)) !== 0;
57
+ }
58
+ /**
59
+ * Adds a key to the Bloom filter.
60
+ */
61
+ add(key) {
62
+ if (key.length === 0) return;
63
+ const h1 = murmur3_32(key, 0);
64
+ const h2 = murmur3_32(key, 2538058380) || 1;
65
+ const m = this.numBits;
66
+ for (let i = 0; i < this.k; i++) {
67
+ const bit = (h1 + Math.imul(i, h2) >>> 0) % m;
68
+ this.setBit(bit);
69
+ }
70
+ this._insertionCount++;
71
+ }
72
+ /**
73
+ * Tests whether a key might be in the set.
74
+ * Returns `false` for guaranteed misses, `true` for possible hits.
75
+ */
76
+ mightContain(key) {
77
+ if (key.length === 0) return true;
78
+ const h1 = murmur3_32(key, 0);
79
+ const h2 = murmur3_32(key, 2538058380) || 1;
80
+ const m = this.numBits;
81
+ for (let i = 0; i < this.k; i++) {
82
+ const bit = (h1 + Math.imul(i, h2) >>> 0) % m;
83
+ if (!this.getBit(bit)) return false;
84
+ }
85
+ return true;
86
+ }
87
+ /**
88
+ * Clears all set bits in the filter.
89
+ */
90
+ reset() {
91
+ this.bits.fill(0);
92
+ this._insertionCount = 0;
93
+ }
94
+ /**
95
+ * Rebuilds the filter from a collection of keys.
96
+ */
97
+ rebuild(keys) {
98
+ this.reset();
99
+ for (const key of keys) {
100
+ this.add(key);
101
+ }
102
+ }
103
+ /**
104
+ * Total add() calls since creation or last reset.
105
+ */
106
+ get insertions() {
107
+ return this._insertionCount;
108
+ }
109
+ /**
110
+ * Maximum safe insertions before false positive rate exceeds target ~1%.
111
+ */
112
+ get maxCapacity() {
113
+ return this._maxCapacity;
114
+ }
115
+ /**
116
+ * Returns current bit saturation metrics.
117
+ */
118
+ get stats() {
119
+ let bitsSet = 0;
120
+ const len = this.bits.length;
121
+ for (let i = 0; i < len; i++) {
122
+ let b = this.bits[i];
123
+ while (b) {
124
+ bitsSet++;
125
+ b &= b - 1;
126
+ }
127
+ }
128
+ return {
129
+ bitsSet,
130
+ fillFactor: bitsSet / this.numBits
131
+ };
132
+ }
133
+ };
134
+
135
+ export {
136
+ murmur3_32,
137
+ Murmur3BloomFilter
138
+ };
@@ -0,0 +1,108 @@
1
+ // src/edge/utils/base64.ts
2
+ var CHUNK_SIZE = 8192;
3
+ function uint8ArrayToBase64(bytes) {
4
+ const modernBytes = bytes;
5
+ if (typeof modernBytes.toBase64 === "function") {
6
+ return modernBytes.toBase64();
7
+ }
8
+ let binary = "";
9
+ const len = bytes.length;
10
+ for (let i = 0; i < len; i += CHUNK_SIZE) {
11
+ const chunk = bytes.subarray(i, Math.min(i + CHUNK_SIZE, len));
12
+ binary += String.fromCharCode(...chunk);
13
+ }
14
+ return globalThis.btoa(binary);
15
+ }
16
+ function base64ToUint8Array(base64) {
17
+ const modernU8 = Uint8Array;
18
+ if (typeof modernU8.fromBase64 === "function") {
19
+ return modernU8.fromBase64(base64);
20
+ }
21
+ const binary = globalThis.atob(base64);
22
+ const len = binary.length;
23
+ const bytes = new Uint8Array(len);
24
+ for (let i = 0; i < len; i++) {
25
+ bytes[i] = binary.charCodeAt(i);
26
+ }
27
+ return bytes;
28
+ }
29
+ function utf8ToUint8Array(str) {
30
+ return new TextEncoder().encode(str);
31
+ }
32
+ function uint8ArrayToUtf8(bytes) {
33
+ return new TextDecoder().decode(bytes);
34
+ }
35
+
36
+ // src/wasm/bloom-filter-wasm.ts
37
+ var BLOOM_WASM_BASE64 = "AGFzbQEAAAABIwdgAn9/AX9gAX8AYAF/AX9gA39/fwF/YAJ/fwBgAABgAAF/AwoJAAABAgMEAAUGBQMBAAEGDQJ/AEGgjQYLfwBBBwsHMwUGbWVtb3J5AgADYWRkAAUMbWlnaHRDb250YWluAAYFcmVzZXQABwljb3VudEJpdHMACAqtAwk/AQJ/QcW78oh4IQIgACABaiEDAkADQCAAIANPDQEgAiAALQAAcyECIAJBk4OACGwhAiAAQQFqIQAMAAsLIAILOAECf0GFKiECIAAgAWohAwJAA0AgACADTw0BIAJBBXQgAmogAC0AAGohAiAAQQFqIQAMAAsLIAILHgEBfyAAQQN2IQEgASABLQAAQQEgAEEHcXRyOgAACxYAIABBA3YtAABBASAAQQdxdHFBAEcLDQAgACACIAFsaiMAcAs4AQN/IAAgARAAIQIgACABEAEhA0EAIQQCQANAIAQjAU8NASACIAMgBBAEEAIgBEEBaiEEDAALCwtBAQN/IAAgARAAIQIgACABEAEhA0EAIQQCQANAIAQjAU8NASACIAMgBBAEEANFBEBBAA8LIARBAWohBAwACwtBAQsnAQF/QQAhAAJAA0AgAEHU4QBPDQEgAEEANgIAIABBBGohAAwACwsLSwEDf0EAIQBBACEBAkADQCAAQdThAE8NASAALQAAIQICQANAIAJFDQEgAUEBaiEBIAIgAkEBa3EhAgwACwsgAEEBaiEADAALCyABCw==";
38
+ var KEY_STAGING_OFFSET = 2e4;
39
+ var MAX_KEY_BYTES = 512;
40
+ var BLOOM_WASM_MODULE = new WebAssembly.Module(
41
+ base64ToUint8Array(BLOOM_WASM_BASE64)
42
+ );
43
+ var WasmBloomFilter = class {
44
+ exports;
45
+ mem;
46
+ stagingTarget;
47
+ encoder = new TextEncoder();
48
+ numBits = 1e5;
49
+ /** Pre-computed — k and numBits are fixed constants; no need to recalculate on every probe. */
50
+ _maxCapacity;
51
+ /** Tracks total add() calls so we know when phantom bits have saturated the filter. */
52
+ _insertionCount = 0;
53
+ constructor() {
54
+ const instance = new WebAssembly.Instance(BLOOM_WASM_MODULE, {});
55
+ this.exports = instance.exports;
56
+ this.mem = new Uint8Array(this.exports.memory.buffer);
57
+ this.stagingTarget = new Uint8Array(this.exports.memory.buffer, KEY_STAGING_OFFSET, MAX_KEY_BYTES);
58
+ const k = 7, p = 0.01;
59
+ this._maxCapacity = Math.floor(-this.numBits * Math.log(1 - Math.pow(p, 1 / k)) / k);
60
+ }
61
+ writeKey(key) {
62
+ if (key.length === 0) return 0;
63
+ return this.encoder.encodeInto(key, this.stagingTarget).written;
64
+ }
65
+ add(key) {
66
+ const len = this.writeKey(key);
67
+ if (len === 0) return;
68
+ this.exports.add(KEY_STAGING_OFFSET, len);
69
+ this._insertionCount++;
70
+ }
71
+ mightContain(key) {
72
+ const len = this.writeKey(key);
73
+ if (len === 0) return true;
74
+ return this.exports.mightContain(KEY_STAGING_OFFSET, len) === 1;
75
+ }
76
+ reset() {
77
+ this.exports.reset();
78
+ this._insertionCount = 0;
79
+ }
80
+ rebuild(keys) {
81
+ this.reset();
82
+ for (const key of keys) this.add(key);
83
+ }
84
+ get stats() {
85
+ const bitsSet = this.exports.countBits();
86
+ return { bitsSet, fillFactor: bitsSet / this.numBits };
87
+ }
88
+ /** Number of add() calls since last reset/rebuild. */
89
+ get insertions() {
90
+ return this._insertionCount;
91
+ }
92
+ /**
93
+ * Maximum safe insertions before false-positive rate exceeds ~1 %.
94
+ * Exact formula for k=7 hash rounds, m=100 000 bits, p=1%:
95
+ * n_max = -m * ln(1 - p^(1/k)) / k ≈ 10 434
96
+ */
97
+ get maxCapacity() {
98
+ return this._maxCapacity;
99
+ }
100
+ };
101
+
102
+ export {
103
+ uint8ArrayToBase64,
104
+ base64ToUint8Array,
105
+ utf8ToUint8Array,
106
+ uint8ArrayToUtf8,
107
+ WasmBloomFilter
108
+ };
@@ -0,0 +1,135 @@
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
+ };