tricache 1.0.2 → 2.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 (101) hide show
  1. package/README.md +78 -90
  2. package/dashboards/tricache-alerts.yaml +80 -4
  3. package/dashboards/tricache-grafana.json +54 -1
  4. package/dashboards/tricache-inventory.rules.yml +21 -0
  5. package/dist/{cache-service-CWI4jkS7.d.cts → cache-service-BV5T8-20.d.cts} +122 -21
  6. package/dist/cache-service-UZXOQGEY.js +14 -0
  7. package/dist/{cache-service-B108FzER.d.ts → cache-service-tpyrXvlH.d.ts} +122 -21
  8. package/dist/{chunk-UBWLPKTI.js → chunk-EJXG7WO6.js} +152 -32
  9. package/dist/{chunk-H4GFLQ26.js → chunk-KV7KISK2.js} +47 -1
  10. package/dist/{chunk-WYF7NOAT.js → chunk-MB3DMVWN.js} +2111 -543
  11. package/dist/{chunk-OYDU3SMI.js → chunk-RBJRTGFK.js} +5 -0
  12. package/dist/chunk-S5N65I45.js +17 -0
  13. package/dist/cli.cjs +2454 -572
  14. package/dist/cli.js +186 -11
  15. package/dist/dashboard/index.d.cts +2 -2
  16. package/dist/dashboard/index.d.ts +2 -2
  17. package/dist/drizzle/index.cjs +2335 -571
  18. package/dist/drizzle/index.d.cts +2 -2
  19. package/dist/drizzle/index.d.ts +2 -2
  20. package/dist/drizzle/index.js +6 -2
  21. package/dist/edge/index.cjs +145 -43
  22. package/dist/edge/index.d.cts +11 -4
  23. package/dist/edge/index.d.ts +11 -4
  24. package/dist/edge/index.js +118 -43
  25. package/dist/hono/index.cjs +2322 -569
  26. package/dist/hono/index.d.cts +2 -2
  27. package/dist/hono/index.d.ts +2 -2
  28. package/dist/hono/index.js +2 -2
  29. package/dist/{hono-C886ok3X.d.cts → hono-CMv2VdQ6.d.cts} +18 -5
  30. package/dist/{hono-3rDMYVtY.d.ts → hono-Dvq04kX9.d.ts} +18 -5
  31. package/dist/http/index.cjs +2323 -570
  32. package/dist/http/index.d.cts +3 -3
  33. package/dist/http/index.d.ts +3 -3
  34. package/dist/http/index.js +4 -4
  35. package/dist/index.cjs +2554 -620
  36. package/dist/index.d.cts +88 -23
  37. package/dist/index.d.ts +88 -23
  38. package/dist/index.js +873 -16
  39. package/dist/{murmur3-CMGw9PbV.d.cts → murmur3-DF8BFRnu.d.cts} +1 -0
  40. package/dist/{murmur3-CMGw9PbV.d.ts → murmur3-DF8BFRnu.d.ts} +1 -0
  41. package/dist/nestjs/index.cjs +2292 -574
  42. package/dist/nestjs/index.d.cts +19 -5
  43. package/dist/nestjs/index.d.ts +19 -5
  44. package/dist/nestjs/index.js +25 -19
  45. package/dist/next/index.cjs +2306 -571
  46. package/dist/next/index.d.cts +8 -2
  47. package/dist/next/index.d.ts +8 -2
  48. package/dist/next/index.js +36 -9
  49. package/dist/prisma/index.cjs +26 -25
  50. package/dist/prisma/index.d.cts +2 -2
  51. package/dist/prisma/index.d.ts +2 -2
  52. package/dist/prisma/index.js +14 -25
  53. package/dist/serialize-worker.cjs +162 -39
  54. package/dist/serialize-worker.js +9 -7
  55. package/dist/{types-BA4pz1A0.d.cts → types-F8vRWMrE.d.cts} +162 -45
  56. package/dist/{types-BA4pz1A0.d.ts → types-F8vRWMrE.d.ts} +162 -45
  57. package/package.json +21 -2
  58. package/src/adapters/auto-pipeliner.ts +52 -11
  59. package/src/cache-service.ts +1537 -415
  60. package/src/cli.ts +2 -9
  61. package/src/codec.ts +3 -3
  62. package/src/compression.ts +39 -16
  63. package/src/cross-region.ts +1 -1
  64. package/src/disk-tier.ts +283 -43
  65. package/src/drizzle/index.ts +2 -1
  66. package/src/edge/adapters/cloudflare-do.ts +25 -10
  67. package/src/edge/adapters/cloudflare-kv.ts +12 -9
  68. package/src/edge/adapters/upstash.ts +27 -11
  69. package/src/edge/cache.ts +59 -17
  70. package/src/edge/types.ts +15 -5
  71. package/src/encryption.ts +50 -18
  72. package/src/env.d.ts +1 -1
  73. package/src/errors.ts +12 -0
  74. package/src/http/utils.ts +6 -0
  75. package/src/index.ts +6 -0
  76. package/src/latency-watchdog.ts +5 -1
  77. package/src/nestjs/decorators.ts +11 -6
  78. package/src/nestjs/tricache.module.ts +2 -1
  79. package/src/nestjs/tricache.store.ts +21 -9
  80. package/src/nestjs/types.ts +1 -1
  81. package/src/next/cache-handler.ts +24 -4
  82. package/src/next/types.ts +4 -0
  83. package/src/origin-capacity.ts +134 -0
  84. package/src/prisma/index.ts +14 -32
  85. package/src/remote-snapshot.ts +37 -27
  86. package/src/serialize-worker.ts +8 -5
  87. package/src/sigv4-snapshot-adapter.ts +37 -27
  88. package/src/smart-memory-cache.ts +62 -22
  89. package/src/types.ts +92 -35
  90. package/src/utils/config-comparison.ts +29 -0
  91. package/src/utils/lz4-size.ts +74 -0
  92. package/src/utils/namespace-key.ts +48 -0
  93. package/src/utils/query-key.ts +13 -0
  94. package/src/utils/snapshot-transfer.ts +83 -0
  95. package/src/utils/work-limits.ts +73 -0
  96. package/src/utils/write-revision.ts +4 -0
  97. package/src/wasm/bloom-filter-wasm.ts +16 -1
  98. package/src/worker-pool.ts +28 -8
  99. package/src/wtiny-lfu.ts +62 -14
  100. package/dist/cache-service-GYPZFB7O.js +0 -12
  101. package/dist/chunk-VNRDL5RA.js +0 -666
package/src/cli.ts CHANGED
@@ -8,8 +8,8 @@
8
8
  * npx tricache clear [--redis redis://localhost:6379] [--namespace <ns>] [--prefix <prefix>]
9
9
  */
10
10
 
11
- import { readFileSync } from 'node:fs';
12
11
  import { parseArgs } from 'node:util';
12
+ import pkg from '../package.json' with { type: 'json' };
13
13
  import { CacheService } from './cache-service.js';
14
14
  import { Redis } from 'ioredis';
15
15
  import {
@@ -61,14 +61,7 @@ export async function runCli(args: string[] = process.argv.slice(2)): Promise<vo
61
61
  // (bin/tricache.js). Output must go directly to process stdout without logger prefixes
62
62
  // to support UNIX pipes, terminal formatting, and automated tooling.
63
63
  if (values.version || command === 'version') {
64
- let version = '1.0.2';
65
- try {
66
- const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
67
- if (pkg?.version) version = pkg.version;
68
- } catch {
69
- // fallback to default version
70
- }
71
- console.log(`tricache v${version}`);
64
+ console.log(`tricache v${pkg.version}`);
72
65
  return;
73
66
  }
74
67
 
package/src/codec.ts CHANGED
@@ -22,14 +22,14 @@ export interface CacheCodecOptions {
22
22
  moreTypes?: boolean;
23
23
  /**
24
24
  * Decode MessagePack maps as plain JavaScript objects.
25
- * Default: true (ensures plain JS objects and standard maps decode to plain objects).
25
+ * Default: false (preserves Map; record-encoded plain objects remain objects).
26
26
  */
27
27
  mapsAsObjects?: boolean;
28
28
  }
29
29
 
30
30
  /**
31
31
  * CacheCodec wraps msgpackr's Packr/Unpackr to provide unified, high-performance binary
32
- * serialization across L1 in-memory buffers, L1.5 disk spill, and cold-start snapshots.
32
+ * serialization across L1 in-memory buffers, L1.5 disk spill, Redis, and cold-start snapshots.
33
33
  */
34
34
  export class CacheCodec {
35
35
  private readonly packr: Packr;
@@ -39,7 +39,7 @@ export class CacheCodec {
39
39
  useToJSON: options?.useToJSON ?? true,
40
40
  useRecords: options?.useRecords ?? true,
41
41
  moreTypes: options?.moreTypes ?? true,
42
- mapsAsObjects: options?.mapsAsObjects ?? true,
42
+ mapsAsObjects: options?.mapsAsObjects ?? false,
43
43
  };
44
44
  this.packr = new Packr(packrOptions);
45
45
  }
@@ -1,13 +1,19 @@
1
1
  import zlib from 'zlib';
2
- import lz4js from 'lz4js';
3
- import { ZstdCodec } from 'zstd-codec';
2
+ import { createRequire } from 'node:module';
3
+ import { lz4DecodedSize } from './utils/lz4-size.js';
4
+
5
+ const requireCompression = createRequire(typeof __filename === 'string' ? __filename : import.meta.url);
6
+ type Lz4Codec = typeof import('lz4js')['default'];
7
+ let lz4js: Lz4Codec | undefined;
8
+ function getLz4(): Lz4Codec { return lz4js ??= requireCompression('lz4js'); }
4
9
 
5
10
  export type CompressionAlgorithm = 'brotli' | 'gzip' | 'zstd' | 'lz4' | 'none';
6
11
 
7
12
  export interface CustomCompressor {
8
13
  name: string;
9
14
  compress: (buf: Buffer) => Buffer;
10
- decompress: (buf: Buffer) => Buffer;
15
+ /** Honor maxOutputBytes during allocation; the returned size is also checked. */
16
+ decompress: (buf: Buffer, maxOutputBytes?: number) => Buffer;
11
17
  }
12
18
 
13
19
  export const PREFIX_COMPRESSED = 'cmp:v1:';
@@ -17,6 +23,7 @@ export const PREFIX_ENC_COMPRESSED = 'ecp:v1:';
17
23
  const COMPRESSION_MAGIC = Buffer.from([0x54, 0x52, 0x43, 0x01]);
18
24
 
19
25
  let zstdSimple: any = null;
26
+ let zstdGeneric: any = null;
20
27
  let zstdInitPromise: Promise<any> | null = null;
21
28
 
22
29
  /**
@@ -26,23 +33,21 @@ let zstdInitPromise: Promise<any> | null = null;
26
33
  export function initZstd(): Promise<any> {
27
34
  if (zstdSimple) return Promise.resolve(zstdSimple);
28
35
  if (!zstdInitPromise) {
29
- zstdInitPromise = new Promise((resolve) => {
36
+ zstdInitPromise = import('zstd-codec').then(({ ZstdCodec }) => new Promise((resolve) => {
30
37
  try {
31
38
  ZstdCodec.run((zstd: any) => {
32
39
  zstdSimple = new zstd.Simple();
40
+ zstdGeneric = new zstd.Generic();
33
41
  resolve(zstdSimple);
34
42
  });
35
43
  } catch {
36
44
  resolve(null);
37
45
  }
38
- });
46
+ })).catch(() => null);
39
47
  }
40
48
  return zstdInitPromise;
41
49
  }
42
50
 
43
- // Auto-start initialization immediately in the background
44
- initZstd().catch(() => {});
45
-
46
51
  export function isCompressed(buf: Buffer): boolean {
47
52
  return buf.length >= 4 && buf[0] === 0x54 && buf[1] === 0x52 && buf[2] === 0x43 && buf[3] === 0x01;
48
53
  }
@@ -66,10 +71,11 @@ export function compressBuffer(
66
71
  return zlib.gzipSync(buf, { level: 6 });
67
72
  }
68
73
  if (algorithm === 'lz4') {
69
- return Buffer.from(lz4js.compress(buf));
74
+ return Buffer.from(getLz4().compress(buf));
70
75
  }
71
76
  if (algorithm === 'zstd') {
72
77
  if (!zstdSimple) {
78
+ void initZstd();
73
79
  // If zstd WASM compile has not yet finished, fallback to fast Brotli level 4
74
80
  return zlib.brotliCompressSync(buf, {
75
81
  params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4 },
@@ -82,13 +88,30 @@ export function compressBuffer(
82
88
 
83
89
  export const MAX_DECOMPRESS_BYTES = 25 * 1024 * 1024; // 25 MB decompression bomb safety ceiling
84
90
 
91
+ function decompressLz4(buf: Buffer): Buffer {
92
+ const size = lz4DecodedSize(buf, MAX_DECOMPRESS_BYTES);
93
+ const decoded = Buffer.from(getLz4().decompress(buf, size));
94
+ if (decoded.length !== size) throw new Error('tricache: corrupt LZ4 decoded size');
95
+ return decoded;
96
+ }
97
+
98
+ function decompressZstd(buf: Buffer): Buffer {
99
+ const size = zstdGeneric?.contentSize(buf);
100
+ if (!Number.isSafeInteger(size) || size < 0) throw new Error('tricache: Zstandard frame requires a bounded content size');
101
+ if (size > MAX_DECOMPRESS_BYTES) throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
102
+ const decoded = zstdSimple.decompress(buf);
103
+ if (!decoded || decoded.length !== size) throw new Error('tricache: corrupt Zstandard decoded size');
104
+ return Buffer.from(decoded);
105
+ }
106
+
85
107
  export function decompressBuffer(
86
108
  buf: Buffer,
87
109
  algorithm: CompressionAlgorithm = 'zstd',
88
110
  custom?: CustomCompressor,
89
111
  ): Buffer {
112
+ if (buf.length > 32 * 1024 * 1024) throw new Error('tricache: compressed input exceeds maximum safety threshold');
90
113
  if (custom) {
91
- const res = custom.decompress(buf);
114
+ const res = custom.decompress(buf, MAX_DECOMPRESS_BYTES);
92
115
  if (res.length > MAX_DECOMPRESS_BYTES) {
93
116
  throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
94
117
  }
@@ -106,7 +129,7 @@ export function decompressBuffer(
106
129
  if (buf.length >= 4 && buf[0] === 0x28 && buf[1] === 0xb5 && buf[2] === 0x2f && buf[3] === 0xfd) {
107
130
  if (zstdSimple) {
108
131
  try {
109
- return checkSafety(Buffer.from(zstdSimple.decompress(buf)));
132
+ return checkSafety(decompressZstd(buf));
110
133
  } catch (err: any) {
111
134
  if (err?.message?.includes('exceeds maximum safety threshold')) throw err;
112
135
  /* try recovery */
@@ -117,7 +140,7 @@ export function decompressBuffer(
117
140
  // 2. LZ4 frame magic header: 0x04 0x22 0x4D 0x18
118
141
  if (buf.length >= 4 && buf[0] === 0x04 && buf[1] === 0x22 && buf[2] === 0x4d && buf[3] === 0x18) {
119
142
  try {
120
- return checkSafety(Buffer.from(lz4js.decompress(buf)));
143
+ return checkSafety(decompressLz4(buf));
121
144
  } catch (err: any) {
122
145
  if (err?.message?.includes('exceeds maximum safety threshold')) throw err;
123
146
  /* try recovery */
@@ -143,7 +166,7 @@ export function decompressBuffer(
143
166
  // 4. Algorithm-specific attempts
144
167
  if (algorithm === 'lz4') {
145
168
  try {
146
- return checkSafety(Buffer.from(lz4js.decompress(buf)));
169
+ return checkSafety(decompressLz4(buf));
147
170
  } catch (err: any) {
148
171
  if (err?.message?.includes('exceeds maximum safety threshold')) throw err;
149
172
  /* fallback */
@@ -152,7 +175,7 @@ export function decompressBuffer(
152
175
 
153
176
  if (algorithm === 'zstd' && zstdSimple) {
154
177
  try {
155
- return checkSafety(Buffer.from(zstdSimple.decompress(buf)));
178
+ return checkSafety(decompressZstd(buf));
156
179
  } catch (err: any) {
157
180
  if (err?.message?.includes('exceeds maximum safety threshold')) throw err;
158
181
  /* fallback */
@@ -182,7 +205,7 @@ export function decompressBuffer(
182
205
  };
183
206
  const tryLz4 = (): Buffer | null => {
184
207
  try {
185
- return checkSafety(Buffer.from(lz4js.decompress(buf)));
208
+ return checkSafety(decompressLz4(buf));
186
209
  } catch (err: any) {
187
210
  if (err?.message?.includes('exceeds maximum safety threshold')) throw err;
188
211
  return null;
@@ -191,7 +214,7 @@ export function decompressBuffer(
191
214
  const tryZstd = (): Buffer | null => {
192
215
  if (!zstdSimple) return null;
193
216
  try {
194
- return checkSafety(Buffer.from(zstdSimple.decompress(buf)));
217
+ return checkSafety(decompressZstd(buf));
195
218
  } catch (err: any) {
196
219
  if (err?.message?.includes('exceeds maximum safety threshold')) throw err;
197
220
  return null;
@@ -34,7 +34,7 @@ export interface CrossRegionInvalidationEvent {
34
34
  tagVersion?: number;
35
35
  /** Timestamp when the event was created */
36
36
  timestamp: number;
37
- /** Optional namespace of the origin cache */
37
+ /** Exact trimmed namespace of the origin cache. Omitted means the default namespace. */
38
38
  namespace?: string;
39
39
  /** Optional W3C traceparent header for distributed context propagation */
40
40
  traceparent?: string;