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,63 @@
1
+ /**
2
+ * Zero-dependency Base64 <-> Uint8Array utilities designed for pure Edge isolates
3
+ * (Cloudflare Workers without nodejs_compat, Deno, Fastly Compute, browsers).
4
+ *
5
+ * Avoids call stack overflow (RangeError: Maximum call stack size exceeded)
6
+ * on payloads > 64 KB by chunking binary data into safe 8 KB slices.
7
+ */
8
+
9
+ const CHUNK_SIZE = 8192;
10
+
11
+ /**
12
+ * Converts a Uint8Array to a standard Base64 string safely across all JS runtimes.
13
+ */
14
+ export function uint8ArrayToBase64(bytes: Uint8Array): string {
15
+ // 1. Modern TC39 stage 3 Uint8Array.prototype.toBase64 support
16
+ const modernBytes = bytes as unknown as { toBase64?: () => string };
17
+ if (typeof modernBytes.toBase64 === 'function') {
18
+ return modernBytes.toBase64();
19
+ }
20
+
21
+ // 2. Fallback using chunked String.fromCharCode + btoa
22
+ let binary = '';
23
+ const len = bytes.length;
24
+ for (let i = 0; i < len; i += CHUNK_SIZE) {
25
+ const chunk = bytes.subarray(i, Math.min(i + CHUNK_SIZE, len));
26
+ binary += String.fromCharCode(...chunk);
27
+ }
28
+ return globalThis.btoa(binary);
29
+ }
30
+
31
+ /**
32
+ * Converts a Base64 string to a Uint8Array safely across all JS runtimes.
33
+ */
34
+ export function base64ToUint8Array(base64: string): Uint8Array {
35
+ // 1. Modern TC39 stage 3 Uint8Array.fromBase64 support
36
+ const modernU8 = Uint8Array as unknown as { fromBase64?: (s: string) => Uint8Array };
37
+ if (typeof modernU8.fromBase64 === 'function') {
38
+ return modernU8.fromBase64(base64);
39
+ }
40
+
41
+ // 2. Universal atob fallback
42
+ const binary = globalThis.atob(base64);
43
+ const len = binary.length;
44
+ const bytes = new Uint8Array(len);
45
+ for (let i = 0; i < len; i++) {
46
+ bytes[i] = binary.charCodeAt(i);
47
+ }
48
+ return bytes;
49
+ }
50
+
51
+ /**
52
+ * Encodes a UTF-8 string to a Uint8Array using standard TextEncoder.
53
+ */
54
+ export function utf8ToUint8Array(str: string): Uint8Array {
55
+ return new TextEncoder().encode(str);
56
+ }
57
+
58
+ /**
59
+ * Decodes a Uint8Array to a UTF-8 string using standard TextDecoder.
60
+ */
61
+ export function uint8ArrayToUtf8(bytes: Uint8Array): string {
62
+ return new TextDecoder().decode(bytes);
63
+ }
@@ -0,0 +1,179 @@
1
+ /**
2
+ * Standard 32-bit MurmurHash3 (x86_32) implementation.
3
+ * Pure TypeScript, zero external dependencies, zero Node.js Buffer dependencies.
4
+ *
5
+ * Fully compatible with V8 Edge Isolates (Cloudflare Workers, Fastly Compute,
6
+ * Vercel Edge, Deno, Bun, and modern browsers).
7
+ */
8
+
9
+ /**
10
+ * Computes the 32-bit MurmurHash3 of a string with an optional seed.
11
+ * Uses 32-bit unsigned arithmetic (>>> 0).
12
+ */
13
+ export function murmur3_32(key: string, seed = 0): number {
14
+ const c1 = 0xcc9e2d51;
15
+ const c2 = 0x1b873593;
16
+
17
+ let h1 = seed >>> 0;
18
+ const len = key.length;
19
+
20
+ // Process 2-character (4-byte in UTF-16 code units / 2-char chunks) blocks
21
+ const remainder = len & 1;
22
+ const bytes = len - remainder;
23
+
24
+ for (let i = 0; i < bytes; i += 2) {
25
+ let k1 = (key.charCodeAt(i) & 0xffff) | ((key.charCodeAt(i + 1) & 0xffff) << 16);
26
+
27
+ k1 = Math.imul(k1, c1);
28
+ k1 = (k1 << 15) | (k1 >>> 17);
29
+ k1 = Math.imul(k1, c2);
30
+
31
+ h1 ^= k1;
32
+ h1 = (h1 << 13) | (h1 >>> 19);
33
+ h1 = (Math.imul(h1, 5) + 0xe6546b64) >>> 0;
34
+ }
35
+
36
+ if (remainder === 1) {
37
+ let k1 = key.charCodeAt(bytes) & 0xffff;
38
+ k1 = Math.imul(k1, c1);
39
+ k1 = (k1 << 15) | (k1 >>> 17);
40
+ k1 = Math.imul(k1, c2);
41
+ h1 ^= k1;
42
+ }
43
+
44
+ // Finalization avalanche mix (fmix32)
45
+ h1 ^= len;
46
+ h1 ^= h1 >>> 16;
47
+ h1 = Math.imul(h1, 0x85ebca6b);
48
+ h1 ^= h1 >>> 13;
49
+ h1 = Math.imul(h1, 0xc2b2ae35);
50
+ h1 ^= h1 >>> 16;
51
+
52
+ return h1 >>> 0;
53
+ }
54
+
55
+ export interface BloomFilterStats {
56
+ bitsSet: number;
57
+ fillFactor: number;
58
+ }
59
+
60
+ /**
61
+ * Pure TypeScript Bloom filter backed by a Uint8Array bitset.
62
+ * Uses Murmur3 double-hashing (Kirsch-Mitzenmacher technique):
63
+ * h_i(x) = (h1(x) + i * h2(x)) % m
64
+ *
65
+ * Zero Buffer dependency — runs identically in Node.js, Edge Isolates, and Browsers.
66
+ */
67
+ export class Murmur3BloomFilter {
68
+ private readonly bits: Uint8Array;
69
+ readonly numBits: number;
70
+ readonly k: number;
71
+ private readonly _maxCapacity: number;
72
+ private _insertionCount = 0;
73
+
74
+ /**
75
+ * @param numBits Total bits in the filter (default: 100,000 bits = 12.5 KB)
76
+ * @param k Number of hash probes per key (default: 7, targeting ~1% FPR at 2,000 items)
77
+ */
78
+ constructor(numBits = 100_000, k = 7) {
79
+ this.numBits = Math.max(64, numBits);
80
+ this.k = Math.max(1, k);
81
+ this.bits = new Uint8Array(Math.ceil(this.numBits / 8));
82
+ const p = 0.01;
83
+ this._maxCapacity = Math.floor(-this.numBits * Math.log(1 - Math.pow(p, 1 / this.k)) / this.k);
84
+ }
85
+
86
+ private setBit(bitIndex: number): void {
87
+ const byteIndex = bitIndex >>> 3;
88
+ this.bits[byteIndex] |= 1 << (bitIndex & 7);
89
+ }
90
+
91
+ private getBit(bitIndex: number): boolean {
92
+ const byteIndex = bitIndex >>> 3;
93
+ return (this.bits[byteIndex] & (1 << (bitIndex & 7))) !== 0;
94
+ }
95
+
96
+ /**
97
+ * Adds a key to the Bloom filter.
98
+ */
99
+ add(key: string): void {
100
+ if (key.length === 0) return;
101
+ const h1 = murmur3_32(key, 0);
102
+ const h2 = murmur3_32(key, 0x9747b28c) || 1; // ensure non-zero step
103
+ const m = this.numBits;
104
+
105
+ for (let i = 0; i < this.k; i++) {
106
+ const bit = ((h1 + Math.imul(i, h2)) >>> 0) % m;
107
+ this.setBit(bit);
108
+ }
109
+ this._insertionCount++;
110
+ }
111
+
112
+ /**
113
+ * Tests whether a key might be in the set.
114
+ * Returns `false` for guaranteed misses, `true` for possible hits.
115
+ */
116
+ mightContain(key: string): boolean {
117
+ if (key.length === 0) return true;
118
+ const h1 = murmur3_32(key, 0);
119
+ const h2 = murmur3_32(key, 0x9747b28c) || 1;
120
+ const m = this.numBits;
121
+
122
+ for (let i = 0; i < this.k; i++) {
123
+ const bit = ((h1 + Math.imul(i, h2)) >>> 0) % m;
124
+ if (!this.getBit(bit)) return false;
125
+ }
126
+ return true;
127
+ }
128
+
129
+ /**
130
+ * Clears all set bits in the filter.
131
+ */
132
+ reset(): void {
133
+ this.bits.fill(0);
134
+ this._insertionCount = 0;
135
+ }
136
+
137
+ /**
138
+ * Rebuilds the filter from a collection of keys.
139
+ */
140
+ rebuild(keys: Iterable<string>): void {
141
+ this.reset();
142
+ for (const key of keys) {
143
+ this.add(key);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Total add() calls since creation or last reset.
149
+ */
150
+ get insertions(): number {
151
+ return this._insertionCount;
152
+ }
153
+
154
+ /**
155
+ * Maximum safe insertions before false positive rate exceeds target ~1%.
156
+ */
157
+ get maxCapacity(): number {
158
+ return this._maxCapacity;
159
+ }
160
+
161
+ /**
162
+ * Returns current bit saturation metrics.
163
+ */
164
+ get stats(): BloomFilterStats {
165
+ let bitsSet = 0;
166
+ const len = this.bits.length;
167
+ for (let i = 0; i < len; i++) {
168
+ let b = this.bits[i];
169
+ while (b) {
170
+ bitsSet++;
171
+ b &= b - 1;
172
+ }
173
+ }
174
+ return {
175
+ bitsSet,
176
+ fillFactor: bitsSet / this.numBits,
177
+ };
178
+ }
179
+ }