tricache 0.6.6 → 0.7.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 +1 -1
- package/README.md +500 -24
- package/bin/tricache.js +8 -0
- package/dist/cache-service-AJASH5FX.js +7 -0
- package/dist/cache-service-BblHuF3i.d.ts +1476 -0
- package/dist/chunk-3XQUQBOZ.js +4583 -0
- package/dist/{chunk-FKY5R3CE.js → chunk-PFB5FMEY.js} +126 -4
- package/dist/chunk-UBSR5UYA.js +0 -0
- package/dist/cli.cjs +5181 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +130 -0
- package/dist/drizzle/index.cjs +5161 -0
- package/dist/drizzle/index.d.ts +44 -0
- package/dist/drizzle/index.js +46 -0
- package/dist/http/index.cjs +5289 -0
- package/dist/http/index.d.ts +33 -0
- package/dist/http/index.js +172 -0
- package/dist/index.cjs +1181 -194
- package/dist/index.d.ts +15 -1230
- package/dist/index.js +11 -3697
- package/dist/nestjs/index.cjs +5305 -0
- package/dist/nestjs/index.d.ts +210 -0
- package/dist/nestjs/index.js +250 -0
- package/dist/next/index.cjs +5327 -0
- package/dist/next/index.d.ts +204 -0
- package/dist/next/index.js +274 -0
- package/dist/prisma/index.cjs +122 -0
- package/dist/prisma/index.d.ts +60 -0
- package/dist/prisma/index.js +86 -0
- package/dist/serialize-worker.cjs +177 -13
- package/dist/serialize-worker.js +63 -12
- package/package.json +64 -15
- package/src/cache-service.ts +1005 -192
- package/src/cli.ts +143 -0
- package/src/compression.ts +63 -0
- package/src/disk-tier.ts +71 -7
- package/src/drizzle/index.ts +86 -0
- package/src/encryption.ts +56 -5
- package/src/http/index.ts +223 -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 +244 -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 +151 -0
- package/src/serialize-worker.ts +77 -13
- package/src/smart-memory-cache.ts +22 -8
- package/src/types.ts +172 -2
- package/src/wasm/bloom-filter-wasm.ts +3 -2
- package/src/worker-pool.ts +156 -42
|
@@ -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;
|
|
@@ -84,8 +100,10 @@ var CacheEncryption = class {
|
|
|
84
100
|
}
|
|
85
101
|
return out;
|
|
86
102
|
}
|
|
87
|
-
constructor(keyBase64,
|
|
88
|
-
|
|
103
|
+
constructor(keyBase64, loggerOrMode = consoleLogger, mode = "aes-256-gcm", previousKeyBase64, previousMode) {
|
|
104
|
+
const logger = typeof loggerOrMode === "object" && loggerOrMode !== null ? loggerOrMode : consoleLogger;
|
|
105
|
+
const resolvedMode = typeof loggerOrMode === "string" ? loggerOrMode : mode;
|
|
106
|
+
this._mode = resolvedMode;
|
|
89
107
|
if (!keyBase64) {
|
|
90
108
|
if (process.env.NODE_ENV === "production") {
|
|
91
109
|
logger.warn(
|
|
@@ -97,7 +115,7 @@ var CacheEncryption = class {
|
|
|
97
115
|
}
|
|
98
116
|
try {
|
|
99
117
|
const buf = Buffer.from(keyBase64, "base64");
|
|
100
|
-
const requiredLen =
|
|
118
|
+
const requiredLen = resolvedMode === "aes-256-gcm" ? 32 : resolvedMode === "aes-128-gcm" || resolvedMode === "aes-128-ctr" ? 16 : 0;
|
|
101
119
|
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
102
120
|
throw new Error(`${mode} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
103
121
|
}
|
|
@@ -148,6 +166,9 @@ var CacheEncryption = class {
|
|
|
148
166
|
get isEnabled() {
|
|
149
167
|
return this._key !== null;
|
|
150
168
|
}
|
|
169
|
+
get mode() {
|
|
170
|
+
return this._mode;
|
|
171
|
+
}
|
|
151
172
|
/**
|
|
152
173
|
* Snapshot the key material needed to spin up an off-main-thread worker pool.
|
|
153
174
|
* Replaces the previously-used `as unknown as { _key, _mode, … }` private-field
|
|
@@ -162,6 +183,43 @@ var CacheEncryption = class {
|
|
|
162
183
|
prevMode: this._prevMode
|
|
163
184
|
};
|
|
164
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Rotate the active encryption key dynamically at runtime.
|
|
188
|
+
* Promotes the current primary key to previousEncryptionKey for zero-downtime
|
|
189
|
+
* fallback decryption of existing cache entries in L2/Disk, and installs
|
|
190
|
+
* the new key as primary for all subsequent writes.
|
|
191
|
+
*/
|
|
192
|
+
rotateKey(newKeyBase64, newMode) {
|
|
193
|
+
if (!newKeyBase64) {
|
|
194
|
+
throw new Error("rotateKey requires a valid non-empty base64 key");
|
|
195
|
+
}
|
|
196
|
+
const mode = newMode ?? this._mode;
|
|
197
|
+
const buf = Buffer.from(newKeyBase64, "base64");
|
|
198
|
+
const requiredLen = mode === "aes-256-gcm" ? 32 : mode === "aes-128-gcm" || mode === "aes-128-ctr" ? 16 : 0;
|
|
199
|
+
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
200
|
+
throw new Error(`${mode} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
201
|
+
}
|
|
202
|
+
if (buf.length < 1) {
|
|
203
|
+
throw new Error("XOR key must be at least 1 byte");
|
|
204
|
+
}
|
|
205
|
+
let newKeyObj = null;
|
|
206
|
+
if (mode !== "xor") {
|
|
207
|
+
newKeyObj = createSecretKey(buf);
|
|
208
|
+
const _wc = createCipheriv(
|
|
209
|
+
mode,
|
|
210
|
+
newKeyObj,
|
|
211
|
+
Buffer.alloc(mode === "aes-128-ctr" ? CTR_IV_BYTES : IV_BYTES)
|
|
212
|
+
);
|
|
213
|
+
_wc.update(Buffer.alloc(0));
|
|
214
|
+
_wc.final();
|
|
215
|
+
}
|
|
216
|
+
this._prevKey = this._key;
|
|
217
|
+
this._prevKeyObj = this._keyObj;
|
|
218
|
+
this._prevMode = this._mode;
|
|
219
|
+
this._key = buf;
|
|
220
|
+
this._keyObj = newKeyObj;
|
|
221
|
+
this._mode = mode;
|
|
222
|
+
}
|
|
165
223
|
// ── String (Redis) ────────────────────────────────────────────────────────
|
|
166
224
|
/**
|
|
167
225
|
* Encrypt a string for Redis storage.
|
|
@@ -341,6 +399,70 @@ var CacheEncryption = class {
|
|
|
341
399
|
}
|
|
342
400
|
};
|
|
343
401
|
|
|
402
|
+
// src/compression.ts
|
|
403
|
+
import zlib from "zlib";
|
|
404
|
+
var PREFIX_COMPRESSED = "cmp:v1:";
|
|
405
|
+
var PREFIX_ENC_COMPRESSED = "ecp:v1:";
|
|
406
|
+
var COMPRESSION_MAGIC = Buffer.from([84, 82, 67, 1]);
|
|
407
|
+
function isCompressed(buf) {
|
|
408
|
+
return buf.length >= 4 && buf[0] === 84 && buf[1] === 82 && buf[2] === 67 && buf[3] === 1;
|
|
409
|
+
}
|
|
410
|
+
function compressBuffer(buf, algorithm) {
|
|
411
|
+
if (algorithm === "brotli") {
|
|
412
|
+
return zlib.brotliCompressSync(buf, {
|
|
413
|
+
params: {
|
|
414
|
+
[zlib.constants.BROTLI_PARAM_QUALITY]: 4
|
|
415
|
+
// Fast level 4 compression
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
if (algorithm === "gzip") {
|
|
420
|
+
return zlib.gzipSync(buf, { level: 6 });
|
|
421
|
+
}
|
|
422
|
+
return buf;
|
|
423
|
+
}
|
|
424
|
+
function decompressBuffer(buf, algorithm = "brotli") {
|
|
425
|
+
if (algorithm === "gzip") {
|
|
426
|
+
try {
|
|
427
|
+
return zlib.gunzipSync(buf);
|
|
428
|
+
} catch {
|
|
429
|
+
try {
|
|
430
|
+
return zlib.brotliDecompressSync(buf);
|
|
431
|
+
} catch {
|
|
432
|
+
return buf;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
try {
|
|
437
|
+
return zlib.brotliDecompressSync(buf);
|
|
438
|
+
} catch {
|
|
439
|
+
try {
|
|
440
|
+
return zlib.gunzipSync(buf);
|
|
441
|
+
} catch {
|
|
442
|
+
return buf;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
function compressWithHeader(buf, algorithm) {
|
|
447
|
+
if (algorithm === "none") return buf;
|
|
448
|
+
const compressed = compressBuffer(buf, algorithm);
|
|
449
|
+
return Buffer.concat([COMPRESSION_MAGIC, compressed]);
|
|
450
|
+
}
|
|
451
|
+
function decompressWithHeader(buf, algorithm = "brotli") {
|
|
452
|
+
if (isCompressed(buf)) {
|
|
453
|
+
return decompressBuffer(buf.subarray(4), algorithm);
|
|
454
|
+
}
|
|
455
|
+
return buf;
|
|
456
|
+
}
|
|
457
|
+
|
|
344
458
|
export {
|
|
345
|
-
|
|
459
|
+
consoleLogger,
|
|
460
|
+
CachePriority,
|
|
461
|
+
CacheEncryption,
|
|
462
|
+
PREFIX_COMPRESSED,
|
|
463
|
+
PREFIX_ENC_COMPRESSED,
|
|
464
|
+
compressBuffer,
|
|
465
|
+
decompressBuffer,
|
|
466
|
+
compressWithHeader,
|
|
467
|
+
decompressWithHeader
|
|
346
468
|
};
|
|
File without changes
|