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.
- package/LICENSE +21 -21
- package/README.md +144 -1428
- package/bin/tricache.js +8 -8
- package/dashboards/tricache-alerts.yaml +54 -0
- package/dashboards/tricache-grafana.json +548 -0
- package/dist/cache-service-5MZXFIGK.js +12 -0
- package/dist/cache-service-CyLdpo77.d.ts +871 -0
- package/dist/{chunk-3XQUQBOZ.js → chunk-6PQ5MOWR.js} +2950 -523
- package/dist/chunk-7RV5ZVBB.js +1023 -0
- package/dist/{chunk-PFB5FMEY.js → chunk-LJ5QJB5O.js} +116 -20
- package/dist/chunk-QNU7B43J.js +687 -0
- package/dist/chunk-UGD4VEPD.js +138 -0
- package/dist/chunk-XZL45ABR.js +108 -0
- package/dist/chunk-ZTSWCFYO.js +135 -0
- package/dist/cli.cjs +2894 -518
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +103 -5
- package/dist/dashboard/index.cjs +1065 -0
- package/dist/dashboard/index.d.ts +183 -0
- package/dist/dashboard/index.js +22 -0
- package/dist/drizzle/index.cjs +2648 -508
- package/dist/drizzle/index.d.ts +3 -1
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +1498 -0
- package/dist/edge/index.d.ts +127 -0
- package/dist/edge/index.js +1109 -0
- package/dist/hono-DQRS1Xd7.d.ts +370 -0
- package/dist/http/index.cjs +3038 -589
- package/dist/http/index.d.ts +109 -23
- package/dist/http/index.js +238 -62
- package/dist/index.cjs +6962 -2487
- package/dist/index.d.ts +547 -30
- package/dist/index.js +104 -5
- package/dist/murmur3-CMGw9PbV.d.ts +98 -0
- package/dist/nestjs/index.cjs +2586 -498
- package/dist/nestjs/index.d.ts +3 -1
- package/dist/nestjs/index.js +6 -3
- package/dist/next/index.cjs +2633 -519
- package/dist/next/index.d.ts +13 -7
- package/dist/next/index.js +45 -18
- package/dist/prisma/index.cjs +2 -1
- package/dist/prisma/index.d.ts +3 -1
- package/dist/prisma/index.js +2 -1
- package/dist/serialize-worker.cjs +31 -19
- package/dist/serialize-worker.js +1 -1
- package/dist/{cache-service-BblHuF3i.d.ts → types-B6krw64X.d.ts} +605 -646
- package/package.json +37 -7
- package/src/adapters/auto-pipeliner.ts +206 -0
- package/src/adapters/node-redis.ts +370 -0
- package/src/cache-service.ts +4231 -3226
- package/src/cli.ts +261 -143
- package/src/codec.ts +72 -0
- package/src/compression.ts +56 -63
- package/src/cross-region.ts +245 -0
- package/src/dashboard/handler.ts +392 -0
- package/src/dashboard/html.ts +594 -0
- package/src/dashboard/index.ts +195 -0
- package/src/dashboard/types.ts +96 -0
- package/src/disk-tier.ts +1223 -821
- package/src/drizzle/index.ts +86 -86
- package/src/edge/adapters/cloudflare-do.ts +130 -0
- package/src/edge/adapters/cloudflare-kv.ts +115 -0
- package/src/edge/adapters/upstash.ts +143 -0
- package/src/edge/cache.ts +797 -0
- package/src/edge/crypto.ts +182 -0
- package/src/edge/hono.ts +203 -0
- package/src/edge/index.ts +37 -0
- package/src/edge/types.ts +230 -0
- package/src/edge/utils/base64.ts +63 -0
- package/src/edge/utils/murmur3.ts +179 -0
- package/src/encryption.ts +635 -493
- package/src/http/express.ts +195 -0
- package/src/http/fastify.ts +260 -0
- package/src/http/index.ts +46 -223
- package/src/http/utils.ts +92 -0
- package/src/index.ts +149 -47
- package/src/ipc-telemetry.ts +481 -0
- package/src/latency-watchdog.ts +282 -0
- package/src/nestjs/decorators.ts +161 -161
- package/src/nestjs/index.ts +23 -23
- package/src/nestjs/tricache.module.ts +104 -104
- package/src/nestjs/tricache.store.ts +97 -97
- package/src/nestjs/types.ts +79 -79
- package/src/next/cache-handler.ts +271 -244
- package/src/next/index.ts +37 -37
- package/src/next/isr-handler.ts +105 -105
- package/src/next/types.ts +130 -130
- package/src/prisma/index.ts +154 -151
- package/src/remote-snapshot.ts +153 -0
- package/src/serialize-worker.ts +120 -120
- package/src/sigv4-snapshot-adapter.ts +260 -0
- package/src/smart-memory-cache.ts +1109 -1024
- package/src/types.ts +1199 -902
- package/src/utils/cgroup.ts +100 -0
- package/src/utils/tracing.ts +54 -0
- package/src/wasm/bloom-filter-wasm.ts +109 -102
- package/src/worker-pool.ts +299 -299
- package/src/wtiny-lfu.ts +624 -0
- package/dist/cache-service-AJASH5FX.js +0 -7
- package/dist/chunk-UBSR5UYA.js +0 -0
|
@@ -15,7 +15,16 @@ var CachePriority = /* @__PURE__ */ ((CachePriority2) => {
|
|
|
15
15
|
})(CachePriority || {});
|
|
16
16
|
|
|
17
17
|
// src/encryption.ts
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
createCipheriv,
|
|
20
|
+
createDecipheriv,
|
|
21
|
+
createSecretKey,
|
|
22
|
+
randomFillSync,
|
|
23
|
+
constants,
|
|
24
|
+
publicEncrypt,
|
|
25
|
+
privateDecrypt,
|
|
26
|
+
randomBytes
|
|
27
|
+
} from "crypto";
|
|
19
28
|
var IV_BYTES = 12;
|
|
20
29
|
var TAG_BYTES = 16;
|
|
21
30
|
var PREFIX_256 = "enc:v1:";
|
|
@@ -35,6 +44,8 @@ var CacheEncryption = class {
|
|
|
35
44
|
_keyObj = null;
|
|
36
45
|
// parsed KeyObject — used for all AES modes
|
|
37
46
|
_mode;
|
|
47
|
+
/** Fail-closed on invalid key material when opted in via constructor opts. */
|
|
48
|
+
_strictKeyValidation;
|
|
38
49
|
_prevKey = null;
|
|
39
50
|
_prevKeyObj = null;
|
|
40
51
|
_prevMode = "aes-256-gcm";
|
|
@@ -100,11 +111,12 @@ var CacheEncryption = class {
|
|
|
100
111
|
}
|
|
101
112
|
return out;
|
|
102
113
|
}
|
|
103
|
-
constructor(keyBase64, loggerOrMode = consoleLogger, mode = "aes-256-gcm", previousKeyBase64, previousMode) {
|
|
114
|
+
constructor(keyBase64, loggerOrMode = consoleLogger, mode = "aes-256-gcm", previousKeyBase64, previousMode, opts) {
|
|
104
115
|
const logger = typeof loggerOrMode === "object" && loggerOrMode !== null ? loggerOrMode : consoleLogger;
|
|
105
116
|
const resolvedMode = typeof loggerOrMode === "string" ? loggerOrMode : mode;
|
|
117
|
+
this._strictKeyValidation = opts?.strictKeyValidation ?? false;
|
|
106
118
|
this._mode = resolvedMode;
|
|
107
|
-
if (
|
|
119
|
+
if (keyBase64 === void 0 || keyBase64 === null) {
|
|
108
120
|
if (process.env.NODE_ENV === "production") {
|
|
109
121
|
logger.warn(
|
|
110
122
|
"SECURITY: encryption key not set \u2014 cache data stored unencrypted at rest",
|
|
@@ -113,27 +125,33 @@ var CacheEncryption = class {
|
|
|
113
125
|
}
|
|
114
126
|
return;
|
|
115
127
|
}
|
|
128
|
+
if (keyBase64 === "") {
|
|
129
|
+
const err = new Error("Encryption key is empty \u2014 set encryptionKey option or CACHE_ENCRYPTION_KEY");
|
|
130
|
+
if (this._strictKeyValidation) throw err;
|
|
131
|
+
logger.error("Empty encryption key \u2014 falling back to plaintext", {}, err);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
116
134
|
try {
|
|
117
135
|
const buf = Buffer.from(keyBase64, "base64");
|
|
118
136
|
const requiredLen = resolvedMode === "aes-256-gcm" ? 32 : resolvedMode === "aes-128-gcm" || resolvedMode === "aes-128-ctr" ? 16 : 0;
|
|
119
137
|
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
120
|
-
throw new Error(`${
|
|
138
|
+
throw new Error(`${resolvedMode} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
121
139
|
}
|
|
122
140
|
if (buf.length < 1) {
|
|
123
141
|
throw new Error("XOR key must be at least 1 byte");
|
|
124
142
|
}
|
|
125
143
|
this._key = buf;
|
|
126
|
-
if (
|
|
144
|
+
if (resolvedMode !== "xor") {
|
|
127
145
|
this._keyObj = createSecretKey(buf);
|
|
128
146
|
const _wc = createCipheriv(
|
|
129
|
-
|
|
147
|
+
resolvedMode,
|
|
130
148
|
this._keyObj,
|
|
131
|
-
Buffer.alloc(
|
|
149
|
+
Buffer.alloc(resolvedMode === "aes-128-ctr" ? CTR_IV_BYTES : IV_BYTES)
|
|
132
150
|
);
|
|
133
151
|
_wc.update(Buffer.alloc(0));
|
|
134
152
|
_wc.final();
|
|
135
153
|
}
|
|
136
|
-
if (
|
|
154
|
+
if (resolvedMode === "xor") {
|
|
137
155
|
logger.warn(
|
|
138
156
|
"Cache obfuscation enabled (XOR). WARNING: XOR is NOT cryptographic \u2014 use only for dev or non-sensitive data."
|
|
139
157
|
);
|
|
@@ -141,6 +159,9 @@ var CacheEncryption = class {
|
|
|
141
159
|
logger.debug(`Cache encryption enabled (${mode.toUpperCase()})`);
|
|
142
160
|
}
|
|
143
161
|
} catch (err) {
|
|
162
|
+
if (this._strictKeyValidation) {
|
|
163
|
+
throw err;
|
|
164
|
+
}
|
|
144
165
|
logger.error("Invalid encryption key \u2014 falling back to plaintext", {}, err);
|
|
145
166
|
}
|
|
146
167
|
if (previousKeyBase64) {
|
|
@@ -398,6 +419,81 @@ var CacheEncryption = class {
|
|
|
398
419
|
return plain;
|
|
399
420
|
}
|
|
400
421
|
};
|
|
422
|
+
var MAGIC_ENVELOPE = Buffer.from([84, 82, 73, 67, 69, 78, 86, 49]);
|
|
423
|
+
var EnvelopeEncryption = class {
|
|
424
|
+
static isEnvelope(data) {
|
|
425
|
+
return data.length >= MAGIC_LEN && data.subarray(0, MAGIC_LEN).equals(MAGIC_ENVELOPE);
|
|
426
|
+
}
|
|
427
|
+
static async encrypt(data, options) {
|
|
428
|
+
const dek = randomBytes(32);
|
|
429
|
+
let encryptedKey;
|
|
430
|
+
if (options.kmsEncrypt) {
|
|
431
|
+
encryptedKey = await options.kmsEncrypt(dek);
|
|
432
|
+
} else if (options.publicKey) {
|
|
433
|
+
encryptedKey = publicEncrypt(
|
|
434
|
+
{
|
|
435
|
+
key: options.publicKey,
|
|
436
|
+
padding: constants.RSA_PKCS1_OAEP_PADDING,
|
|
437
|
+
oaepHash: "sha256"
|
|
438
|
+
},
|
|
439
|
+
dek
|
|
440
|
+
);
|
|
441
|
+
} else {
|
|
442
|
+
throw new Error("EnvelopeEncryption: either publicKey or kmsEncrypt must be provided to encrypt");
|
|
443
|
+
}
|
|
444
|
+
const iv = randomBytes(IV_BYTES);
|
|
445
|
+
const cipher = createCipheriv("aes-256-gcm", dek, iv);
|
|
446
|
+
const ct = cipher.update(data);
|
|
447
|
+
cipher.final();
|
|
448
|
+
const tag = cipher.getAuthTag();
|
|
449
|
+
const keyLen = encryptedKey.length;
|
|
450
|
+
const out = Buffer.allocUnsafe(MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES + ct.length);
|
|
451
|
+
MAGIC_ENVELOPE.copy(out, 0);
|
|
452
|
+
out.writeUInt32BE(keyLen, MAGIC_LEN);
|
|
453
|
+
encryptedKey.copy(out, MAGIC_LEN + 4);
|
|
454
|
+
iv.copy(out, MAGIC_LEN + 4 + keyLen);
|
|
455
|
+
tag.copy(out, MAGIC_LEN + 4 + keyLen + IV_BYTES);
|
|
456
|
+
ct.copy(out, MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES);
|
|
457
|
+
return out;
|
|
458
|
+
}
|
|
459
|
+
static async decrypt(data, options) {
|
|
460
|
+
if (!this.isEnvelope(data)) {
|
|
461
|
+
throw new Error("EnvelopeEncryption: invalid header, expected TRICENV1");
|
|
462
|
+
}
|
|
463
|
+
if (data.length < MAGIC_LEN + 4 + IV_BYTES + TAG_BYTES) {
|
|
464
|
+
throw new Error("EnvelopeEncryption: truncated envelope buffer");
|
|
465
|
+
}
|
|
466
|
+
const keyLen = data.readUInt32BE(MAGIC_LEN);
|
|
467
|
+
const minExpected = MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES;
|
|
468
|
+
if (data.length < minExpected) {
|
|
469
|
+
throw new Error("EnvelopeEncryption: truncated envelope buffer (key length mismatch)");
|
|
470
|
+
}
|
|
471
|
+
const encryptedKey = data.subarray(MAGIC_LEN + 4, MAGIC_LEN + 4 + keyLen);
|
|
472
|
+
const iv = data.subarray(MAGIC_LEN + 4 + keyLen, MAGIC_LEN + 4 + keyLen + IV_BYTES);
|
|
473
|
+
const tag = data.subarray(MAGIC_LEN + 4 + keyLen + IV_BYTES, MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES);
|
|
474
|
+
const ct = data.subarray(MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES);
|
|
475
|
+
let dek;
|
|
476
|
+
if (options.kmsDecrypt) {
|
|
477
|
+
dek = await options.kmsDecrypt(encryptedKey);
|
|
478
|
+
} else if (options.privateKey) {
|
|
479
|
+
dek = privateDecrypt(
|
|
480
|
+
{
|
|
481
|
+
key: options.privateKey,
|
|
482
|
+
padding: constants.RSA_PKCS1_OAEP_PADDING,
|
|
483
|
+
oaepHash: "sha256"
|
|
484
|
+
},
|
|
485
|
+
encryptedKey
|
|
486
|
+
);
|
|
487
|
+
} else {
|
|
488
|
+
throw new Error("EnvelopeEncryption: either privateKey or kmsDecrypt must be provided to decrypt");
|
|
489
|
+
}
|
|
490
|
+
const decipher = createDecipheriv("aes-256-gcm", dek, iv);
|
|
491
|
+
decipher.setAuthTag(tag);
|
|
492
|
+
const pt = decipher.update(ct);
|
|
493
|
+
decipher.final();
|
|
494
|
+
return pt;
|
|
495
|
+
}
|
|
496
|
+
};
|
|
401
497
|
|
|
402
498
|
// src/compression.ts
|
|
403
499
|
import zlib from "zlib";
|
|
@@ -422,26 +518,25 @@ function compressBuffer(buf, algorithm) {
|
|
|
422
518
|
return buf;
|
|
423
519
|
}
|
|
424
520
|
function decompressBuffer(buf, algorithm = "brotli") {
|
|
425
|
-
|
|
521
|
+
const tryBrotli = () => {
|
|
426
522
|
try {
|
|
427
|
-
return zlib.
|
|
523
|
+
return zlib.brotliDecompressSync(buf);
|
|
428
524
|
} catch {
|
|
429
|
-
|
|
430
|
-
return zlib.brotliDecompressSync(buf);
|
|
431
|
-
} catch {
|
|
432
|
-
return buf;
|
|
433
|
-
}
|
|
525
|
+
return null;
|
|
434
526
|
}
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
return zlib.brotliDecompressSync(buf);
|
|
438
|
-
} catch {
|
|
527
|
+
};
|
|
528
|
+
const tryGzip = () => {
|
|
439
529
|
try {
|
|
440
530
|
return zlib.gunzipSync(buf);
|
|
441
531
|
} catch {
|
|
442
|
-
return
|
|
532
|
+
return null;
|
|
443
533
|
}
|
|
534
|
+
};
|
|
535
|
+
const result = algorithm === "gzip" ? tryGzip() ?? tryBrotli() : tryBrotli() ?? tryGzip();
|
|
536
|
+
if (result === null) {
|
|
537
|
+
throw new Error(`tricache: corrupt compressed payload (${buf.length} bytes, algorithm=${algorithm})`);
|
|
444
538
|
}
|
|
539
|
+
return result;
|
|
445
540
|
}
|
|
446
541
|
function compressWithHeader(buf, algorithm) {
|
|
447
542
|
if (algorithm === "none") return buf;
|
|
@@ -459,6 +554,7 @@ export {
|
|
|
459
554
|
consoleLogger,
|
|
460
555
|
CachePriority,
|
|
461
556
|
CacheEncryption,
|
|
557
|
+
EnvelopeEncryption,
|
|
462
558
|
PREFIX_COMPRESSED,
|
|
463
559
|
PREFIX_ENC_COMPRESSED,
|
|
464
560
|
compressBuffer,
|