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.
Files changed (55) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1428 -970
  3. package/bin/tricache.js +8 -0
  4. package/dist/cache-service-5GATUYAK.js +7 -0
  5. package/dist/cache-service-D8tIXmzb.d.ts +1498 -0
  6. package/dist/chunk-EUGYR7KR.js +4629 -0
  7. package/dist/chunk-UBSR5UYA.js +0 -0
  8. package/dist/{chunk-FKY5R3CE.js → chunk-X5AXHPVD.js} +139 -6
  9. package/dist/cli.cjs +5238 -0
  10. package/dist/cli.d.ts +11 -0
  11. package/dist/cli.js +130 -0
  12. package/dist/drizzle/index.cjs +5218 -0
  13. package/dist/drizzle/index.d.ts +44 -0
  14. package/dist/drizzle/index.js +46 -0
  15. package/dist/http/index.cjs +5374 -0
  16. package/dist/http/index.d.ts +33 -0
  17. package/dist/http/index.js +200 -0
  18. package/dist/index.cjs +1245 -209
  19. package/dist/index.d.ts +15 -1230
  20. package/dist/index.js +11 -3705
  21. package/dist/nestjs/index.cjs +5362 -0
  22. package/dist/nestjs/index.d.ts +210 -0
  23. package/dist/nestjs/index.js +250 -0
  24. package/dist/next/index.cjs +5407 -0
  25. package/dist/next/index.d.ts +208 -0
  26. package/dist/next/index.js +297 -0
  27. package/dist/prisma/index.cjs +123 -0
  28. package/dist/prisma/index.d.ts +60 -0
  29. package/dist/prisma/index.js +87 -0
  30. package/dist/serialize-worker.cjs +190 -15
  31. package/dist/serialize-worker.js +63 -12
  32. package/package.json +71 -21
  33. package/src/cache-service.ts +3294 -2429
  34. package/src/cli.ts +143 -0
  35. package/src/compression.ts +56 -0
  36. package/src/disk-tier.ts +821 -757
  37. package/src/drizzle/index.ts +86 -0
  38. package/src/encryption.ts +513 -442
  39. package/src/http/index.ts +265 -0
  40. package/src/index.ts +7 -0
  41. package/src/nestjs/decorators.ts +161 -0
  42. package/src/nestjs/index.ts +23 -0
  43. package/src/nestjs/tricache.module.ts +104 -0
  44. package/src/nestjs/tricache.store.ts +97 -0
  45. package/src/nestjs/types.ts +79 -0
  46. package/src/next/cache-handler.ts +268 -0
  47. package/src/next/index.ts +37 -0
  48. package/src/next/isr-handler.ts +105 -0
  49. package/src/next/types.ts +130 -0
  50. package/src/prisma/index.ts +154 -0
  51. package/src/serialize-worker.ts +77 -13
  52. package/src/smart-memory-cache.ts +1026 -1010
  53. package/src/types.ts +911 -732
  54. package/src/wasm/bloom-filter-wasm.ts +3 -2
  55. package/src/worker-pool.ts +156 -42
File without changes
@@ -1,3 +1,19 @@
1
+ // src/types.ts
2
+ var consoleLogger = {
3
+ debug: () => {
4
+ },
5
+ info: (msg, meta) => console.info("[tricache]", msg, meta ?? ""),
6
+ warn: (msg, meta) => console.warn("[tricache]", msg, meta ?? ""),
7
+ error: (msg, meta, err) => console.error("[tricache]", msg, meta ?? "", err ?? "")
8
+ };
9
+ var CachePriority = /* @__PURE__ */ ((CachePriority2) => {
10
+ CachePriority2[CachePriority2["LOW"] = 1] = "LOW";
11
+ CachePriority2[CachePriority2["NORMAL"] = 2] = "NORMAL";
12
+ CachePriority2[CachePriority2["HIGH"] = 3] = "HIGH";
13
+ CachePriority2[CachePriority2["CRITICAL"] = 4] = "CRITICAL";
14
+ return CachePriority2;
15
+ })(CachePriority || {});
16
+
1
17
  // src/encryption.ts
2
18
  import { createCipheriv, createDecipheriv, createSecretKey, randomFillSync } from "crypto";
3
19
  var IV_BYTES = 12;
@@ -19,6 +35,8 @@ var CacheEncryption = class {
19
35
  _keyObj = null;
20
36
  // parsed KeyObject — used for all AES modes
21
37
  _mode;
38
+ /** Fail-closed on invalid key material when opted in via constructor opts. */
39
+ _strictKeyValidation;
22
40
  _prevKey = null;
23
41
  _prevKeyObj = null;
24
42
  _prevMode = "aes-256-gcm";
@@ -84,9 +102,12 @@ var CacheEncryption = class {
84
102
  }
85
103
  return out;
86
104
  }
87
- constructor(keyBase64, logger, mode = "aes-256-gcm", previousKeyBase64, previousMode) {
88
- this._mode = mode;
89
- if (!keyBase64) {
105
+ constructor(keyBase64, loggerOrMode = consoleLogger, mode = "aes-256-gcm", previousKeyBase64, previousMode, opts) {
106
+ const logger = typeof loggerOrMode === "object" && loggerOrMode !== null ? loggerOrMode : consoleLogger;
107
+ const resolvedMode = typeof loggerOrMode === "string" ? loggerOrMode : mode;
108
+ this._strictKeyValidation = opts?.strictKeyValidation ?? false;
109
+ this._mode = resolvedMode;
110
+ if (keyBase64 === void 0 || keyBase64 === null) {
90
111
  if (process.env.NODE_ENV === "production") {
91
112
  logger.warn(
92
113
  "SECURITY: encryption key not set \u2014 cache data stored unencrypted at rest",
@@ -95,11 +116,17 @@ var CacheEncryption = class {
95
116
  }
96
117
  return;
97
118
  }
119
+ if (keyBase64 === "") {
120
+ const err = new Error("Encryption key is empty \u2014 set encryptionKey option or CACHE_ENCRYPTION_KEY");
121
+ if (this._strictKeyValidation) throw err;
122
+ logger.error("Empty encryption key \u2014 falling back to plaintext", {}, err);
123
+ return;
124
+ }
98
125
  try {
99
126
  const buf = Buffer.from(keyBase64, "base64");
100
- const requiredLen = mode === "aes-256-gcm" ? 32 : mode === "aes-128-gcm" || mode === "aes-128-ctr" ? 16 : 0;
127
+ const requiredLen = resolvedMode === "aes-256-gcm" ? 32 : resolvedMode === "aes-128-gcm" || resolvedMode === "aes-128-ctr" ? 16 : 0;
101
128
  if (requiredLen > 0 && buf.length !== requiredLen) {
102
- throw new Error(`${mode} requires exactly ${requiredLen} bytes (got ${buf.length})`);
129
+ throw new Error(`${resolvedMode} requires exactly ${requiredLen} bytes (got ${buf.length})`);
103
130
  }
104
131
  if (buf.length < 1) {
105
132
  throw new Error("XOR key must be at least 1 byte");
@@ -123,6 +150,9 @@ var CacheEncryption = class {
123
150
  logger.debug(`Cache encryption enabled (${mode.toUpperCase()})`);
124
151
  }
125
152
  } catch (err) {
153
+ if (this._strictKeyValidation) {
154
+ throw err;
155
+ }
126
156
  logger.error("Invalid encryption key \u2014 falling back to plaintext", {}, err);
127
157
  }
128
158
  if (previousKeyBase64) {
@@ -148,6 +178,9 @@ var CacheEncryption = class {
148
178
  get isEnabled() {
149
179
  return this._key !== null;
150
180
  }
181
+ get mode() {
182
+ return this._mode;
183
+ }
151
184
  /**
152
185
  * Snapshot the key material needed to spin up an off-main-thread worker pool.
153
186
  * Replaces the previously-used `as unknown as { _key, _mode, … }` private-field
@@ -162,6 +195,43 @@ var CacheEncryption = class {
162
195
  prevMode: this._prevMode
163
196
  };
164
197
  }
198
+ /**
199
+ * Rotate the active encryption key dynamically at runtime.
200
+ * Promotes the current primary key to previousEncryptionKey for zero-downtime
201
+ * fallback decryption of existing cache entries in L2/Disk, and installs
202
+ * the new key as primary for all subsequent writes.
203
+ */
204
+ rotateKey(newKeyBase64, newMode) {
205
+ if (!newKeyBase64) {
206
+ throw new Error("rotateKey requires a valid non-empty base64 key");
207
+ }
208
+ const mode = newMode ?? this._mode;
209
+ const buf = Buffer.from(newKeyBase64, "base64");
210
+ const requiredLen = mode === "aes-256-gcm" ? 32 : mode === "aes-128-gcm" || mode === "aes-128-ctr" ? 16 : 0;
211
+ if (requiredLen > 0 && buf.length !== requiredLen) {
212
+ throw new Error(`${mode} requires exactly ${requiredLen} bytes (got ${buf.length})`);
213
+ }
214
+ if (buf.length < 1) {
215
+ throw new Error("XOR key must be at least 1 byte");
216
+ }
217
+ let newKeyObj = null;
218
+ if (mode !== "xor") {
219
+ newKeyObj = createSecretKey(buf);
220
+ const _wc = createCipheriv(
221
+ mode,
222
+ newKeyObj,
223
+ Buffer.alloc(mode === "aes-128-ctr" ? CTR_IV_BYTES : IV_BYTES)
224
+ );
225
+ _wc.update(Buffer.alloc(0));
226
+ _wc.final();
227
+ }
228
+ this._prevKey = this._key;
229
+ this._prevKeyObj = this._keyObj;
230
+ this._prevMode = this._mode;
231
+ this._key = buf;
232
+ this._keyObj = newKeyObj;
233
+ this._mode = mode;
234
+ }
165
235
  // ── String (Redis) ────────────────────────────────────────────────────────
166
236
  /**
167
237
  * Encrypt a string for Redis storage.
@@ -341,6 +411,69 @@ var CacheEncryption = class {
341
411
  }
342
412
  };
343
413
 
414
+ // src/compression.ts
415
+ import zlib from "zlib";
416
+ var PREFIX_COMPRESSED = "cmp:v1:";
417
+ var PREFIX_ENC_COMPRESSED = "ecp:v1:";
418
+ var COMPRESSION_MAGIC = Buffer.from([84, 82, 67, 1]);
419
+ function isCompressed(buf) {
420
+ return buf.length >= 4 && buf[0] === 84 && buf[1] === 82 && buf[2] === 67 && buf[3] === 1;
421
+ }
422
+ function compressBuffer(buf, algorithm) {
423
+ if (algorithm === "brotli") {
424
+ return zlib.brotliCompressSync(buf, {
425
+ params: {
426
+ [zlib.constants.BROTLI_PARAM_QUALITY]: 4
427
+ // Fast level 4 compression
428
+ }
429
+ });
430
+ }
431
+ if (algorithm === "gzip") {
432
+ return zlib.gzipSync(buf, { level: 6 });
433
+ }
434
+ return buf;
435
+ }
436
+ function decompressBuffer(buf, algorithm = "brotli") {
437
+ const tryBrotli = () => {
438
+ try {
439
+ return zlib.brotliDecompressSync(buf);
440
+ } catch {
441
+ return null;
442
+ }
443
+ };
444
+ const tryGzip = () => {
445
+ try {
446
+ return zlib.gunzipSync(buf);
447
+ } catch {
448
+ return null;
449
+ }
450
+ };
451
+ const result = algorithm === "gzip" ? tryGzip() ?? tryBrotli() : tryBrotli() ?? tryGzip();
452
+ if (result === null) {
453
+ throw new Error(`tricache: corrupt compressed payload (${buf.length} bytes, algorithm=${algorithm})`);
454
+ }
455
+ return result;
456
+ }
457
+ function compressWithHeader(buf, algorithm) {
458
+ if (algorithm === "none") return buf;
459
+ const compressed = compressBuffer(buf, algorithm);
460
+ return Buffer.concat([COMPRESSION_MAGIC, compressed]);
461
+ }
462
+ function decompressWithHeader(buf, algorithm = "brotli") {
463
+ if (isCompressed(buf)) {
464
+ return decompressBuffer(buf.subarray(4), algorithm);
465
+ }
466
+ return buf;
467
+ }
468
+
344
469
  export {
345
- CacheEncryption
470
+ consoleLogger,
471
+ CachePriority,
472
+ CacheEncryption,
473
+ PREFIX_COMPRESSED,
474
+ PREFIX_ENC_COMPRESSED,
475
+ compressBuffer,
476
+ decompressBuffer,
477
+ compressWithHeader,
478
+ decompressWithHeader
346
479
  };