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.
- package/LICENSE +1 -1
- package/README.md +1428 -970
- package/bin/tricache.js +8 -0
- package/dist/cache-service-5GATUYAK.js +7 -0
- package/dist/cache-service-D8tIXmzb.d.ts +1498 -0
- package/dist/chunk-EUGYR7KR.js +4629 -0
- package/dist/chunk-UBSR5UYA.js +0 -0
- package/dist/{chunk-FKY5R3CE.js → chunk-X5AXHPVD.js} +139 -6
- package/dist/cli.cjs +5238 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +130 -0
- package/dist/drizzle/index.cjs +5218 -0
- package/dist/drizzle/index.d.ts +44 -0
- package/dist/drizzle/index.js +46 -0
- package/dist/http/index.cjs +5374 -0
- package/dist/http/index.d.ts +33 -0
- package/dist/http/index.js +200 -0
- package/dist/index.cjs +1245 -209
- package/dist/index.d.ts +15 -1230
- package/dist/index.js +11 -3705
- package/dist/nestjs/index.cjs +5362 -0
- package/dist/nestjs/index.d.ts +210 -0
- package/dist/nestjs/index.js +250 -0
- package/dist/next/index.cjs +5407 -0
- package/dist/next/index.d.ts +208 -0
- package/dist/next/index.js +297 -0
- package/dist/prisma/index.cjs +123 -0
- package/dist/prisma/index.d.ts +60 -0
- package/dist/prisma/index.js +87 -0
- package/dist/serialize-worker.cjs +190 -15
- package/dist/serialize-worker.js +63 -12
- package/package.json +71 -21
- package/src/cache-service.ts +3294 -2429
- package/src/cli.ts +143 -0
- package/src/compression.ts +56 -0
- package/src/disk-tier.ts +821 -757
- package/src/drizzle/index.ts +86 -0
- package/src/encryption.ts +513 -442
- package/src/http/index.ts +265 -0
- package/src/index.ts +7 -0
- package/src/nestjs/decorators.ts +161 -0
- package/src/nestjs/index.ts +23 -0
- package/src/nestjs/tricache.module.ts +104 -0
- package/src/nestjs/tricache.store.ts +97 -0
- package/src/nestjs/types.ts +79 -0
- package/src/next/cache-handler.ts +268 -0
- package/src/next/index.ts +37 -0
- package/src/next/isr-handler.ts +105 -0
- package/src/next/types.ts +130 -0
- package/src/prisma/index.ts +154 -0
- package/src/serialize-worker.ts +77 -13
- package/src/smart-memory-cache.ts +1026 -1010
- package/src/types.ts +911 -732
- package/src/wasm/bloom-filter-wasm.ts +3 -2
- 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,
|
|
88
|
-
|
|
89
|
-
|
|
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 =
|
|
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(`${
|
|
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
|
-
|
|
470
|
+
consoleLogger,
|
|
471
|
+
CachePriority,
|
|
472
|
+
CacheEncryption,
|
|
473
|
+
PREFIX_COMPRESSED,
|
|
474
|
+
PREFIX_ENC_COMPRESSED,
|
|
475
|
+
compressBuffer,
|
|
476
|
+
decompressBuffer,
|
|
477
|
+
compressWithHeader,
|
|
478
|
+
decompressWithHeader
|
|
346
479
|
};
|