tricache 0.7.0 → 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/README.md +1428 -1428
- package/bin/tricache.js +8 -8
- package/dist/cache-service-5GATUYAK.js +7 -0
- package/dist/{cache-service-BblHuF3i.d.ts → cache-service-D8tIXmzb.d.ts} +23 -1
- package/dist/{chunk-3XQUQBOZ.js → chunk-EUGYR7KR.js} +87 -41
- package/dist/{chunk-PFB5FMEY.js → chunk-X5AXHPVD.js} +26 -15
- package/dist/cli.cjs +113 -56
- package/dist/cli.js +3 -3
- package/dist/drizzle/index.cjs +114 -57
- package/dist/drizzle/index.d.ts +1 -1
- package/dist/drizzle/index.js +1 -1
- package/dist/http/index.cjs +149 -64
- package/dist/http/index.d.ts +1 -1
- package/dist/http/index.js +38 -10
- package/dist/index.cjs +112 -55
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/nestjs/index.cjs +112 -55
- package/dist/nestjs/index.d.ts +1 -1
- package/dist/nestjs/index.js +2 -2
- package/dist/next/index.cjs +143 -63
- package/dist/next/index.d.ts +10 -6
- package/dist/next/index.js +33 -10
- package/dist/prisma/index.cjs +2 -1
- package/dist/prisma/index.d.ts +1 -1
- package/dist/prisma/index.js +2 -1
- package/dist/serialize-worker.cjs +26 -15
- package/dist/serialize-worker.js +1 -1
- package/package.json +4 -2
- package/src/cache-service.ts +3294 -3226
- package/src/cli.ts +143 -143
- package/src/compression.ts +56 -63
- package/src/disk-tier.ts +821 -821
- package/src/encryption.ts +513 -493
- package/src/http/index.ts +265 -223
- package/src/next/cache-handler.ts +268 -244
- package/src/prisma/index.ts +154 -151
- package/src/smart-memory-cache.ts +1026 -1024
- package/src/types.ts +911 -902
- package/dist/cache-service-AJASH5FX.js +0 -7
package/dist/next/index.cjs
CHANGED
|
@@ -282,6 +282,8 @@ var CacheEncryption = class {
|
|
|
282
282
|
_keyObj = null;
|
|
283
283
|
// parsed KeyObject — used for all AES modes
|
|
284
284
|
_mode;
|
|
285
|
+
/** Fail-closed on invalid key material when opted in via constructor opts. */
|
|
286
|
+
_strictKeyValidation;
|
|
285
287
|
_prevKey = null;
|
|
286
288
|
_prevKeyObj = null;
|
|
287
289
|
_prevMode = "aes-256-gcm";
|
|
@@ -347,11 +349,12 @@ var CacheEncryption = class {
|
|
|
347
349
|
}
|
|
348
350
|
return out;
|
|
349
351
|
}
|
|
350
|
-
constructor(keyBase64, loggerOrMode = consoleLogger, mode = "aes-256-gcm", previousKeyBase64, previousMode) {
|
|
352
|
+
constructor(keyBase64, loggerOrMode = consoleLogger, mode = "aes-256-gcm", previousKeyBase64, previousMode, opts) {
|
|
351
353
|
const logger = typeof loggerOrMode === "object" && loggerOrMode !== null ? loggerOrMode : consoleLogger;
|
|
352
354
|
const resolvedMode = typeof loggerOrMode === "string" ? loggerOrMode : mode;
|
|
355
|
+
this._strictKeyValidation = opts?.strictKeyValidation ?? false;
|
|
353
356
|
this._mode = resolvedMode;
|
|
354
|
-
if (
|
|
357
|
+
if (keyBase64 === void 0 || keyBase64 === null) {
|
|
355
358
|
if (process.env.NODE_ENV === "production") {
|
|
356
359
|
logger.warn(
|
|
357
360
|
"SECURITY: encryption key not set \u2014 cache data stored unencrypted at rest",
|
|
@@ -360,11 +363,17 @@ var CacheEncryption = class {
|
|
|
360
363
|
}
|
|
361
364
|
return;
|
|
362
365
|
}
|
|
366
|
+
if (keyBase64 === "") {
|
|
367
|
+
const err = new Error("Encryption key is empty \u2014 set encryptionKey option or CACHE_ENCRYPTION_KEY");
|
|
368
|
+
if (this._strictKeyValidation) throw err;
|
|
369
|
+
logger.error("Empty encryption key \u2014 falling back to plaintext", {}, err);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
363
372
|
try {
|
|
364
373
|
const buf = Buffer.from(keyBase64, "base64");
|
|
365
374
|
const requiredLen = resolvedMode === "aes-256-gcm" ? 32 : resolvedMode === "aes-128-gcm" || resolvedMode === "aes-128-ctr" ? 16 : 0;
|
|
366
375
|
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
367
|
-
throw new Error(`${
|
|
376
|
+
throw new Error(`${resolvedMode} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
368
377
|
}
|
|
369
378
|
if (buf.length < 1) {
|
|
370
379
|
throw new Error("XOR key must be at least 1 byte");
|
|
@@ -388,6 +397,9 @@ var CacheEncryption = class {
|
|
|
388
397
|
logger.debug(`Cache encryption enabled (${mode.toUpperCase()})`);
|
|
389
398
|
}
|
|
390
399
|
} catch (err) {
|
|
400
|
+
if (this._strictKeyValidation) {
|
|
401
|
+
throw err;
|
|
402
|
+
}
|
|
391
403
|
logger.error("Invalid encryption key \u2014 falling back to plaintext", {}, err);
|
|
392
404
|
}
|
|
393
405
|
if (previousKeyBase64) {
|
|
@@ -714,7 +726,6 @@ var WasmBloomFilter = class {
|
|
|
714
726
|
};
|
|
715
727
|
|
|
716
728
|
// src/smart-memory-cache.ts
|
|
717
|
-
var _hit = { value: void 0, isStale: false, expiresAt: 0, ttlMs: void 0, delta: void 0, fetchedAt: 0, tagVersions: void 0, setAt: void 0 };
|
|
718
729
|
var LARGE_VALUE_BYTES = 16384;
|
|
719
730
|
var EVICTION_BATCH_PERCENT = 0.2;
|
|
720
731
|
var EVICTION_SAMPLE = 16;
|
|
@@ -1117,15 +1128,17 @@ var SmartMemoryCache = class {
|
|
|
1117
1128
|
entry.lastAccess = now;
|
|
1118
1129
|
if ((entry.hits & 3) === 0) this.sketch.increment(key);
|
|
1119
1130
|
const value = entry.value !== void 0 ? entry.value : (0, import_msgpackr.unpack)(entry.data);
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1131
|
+
const hit = {
|
|
1132
|
+
value,
|
|
1133
|
+
isStale: entry.staleAt !== void 0 && now > entry.staleAt,
|
|
1134
|
+
expiresAt: entry.expiresAt,
|
|
1135
|
+
ttlMs: entry.ttlMs,
|
|
1136
|
+
delta: entry.delta,
|
|
1137
|
+
fetchedAt: now,
|
|
1138
|
+
tagVersions: entry.tagVersions,
|
|
1139
|
+
setAt: entry.setAt
|
|
1140
|
+
};
|
|
1141
|
+
return hit;
|
|
1129
1142
|
}
|
|
1130
1143
|
set(key, data, ttlMs, priority = 2, staleAt, delta, tagVersions) {
|
|
1131
1144
|
const packed = (0, import_msgpackr.pack)(data);
|
|
@@ -1580,26 +1593,25 @@ function compressBuffer(buf, algorithm) {
|
|
|
1580
1593
|
return buf;
|
|
1581
1594
|
}
|
|
1582
1595
|
function decompressBuffer(buf, algorithm = "brotli") {
|
|
1583
|
-
|
|
1596
|
+
const tryBrotli = () => {
|
|
1584
1597
|
try {
|
|
1585
|
-
return import_zlib.default.
|
|
1598
|
+
return import_zlib.default.brotliDecompressSync(buf);
|
|
1586
1599
|
} catch {
|
|
1587
|
-
|
|
1588
|
-
return import_zlib.default.brotliDecompressSync(buf);
|
|
1589
|
-
} catch {
|
|
1590
|
-
return buf;
|
|
1591
|
-
}
|
|
1600
|
+
return null;
|
|
1592
1601
|
}
|
|
1593
|
-
}
|
|
1594
|
-
|
|
1595
|
-
return import_zlib.default.brotliDecompressSync(buf);
|
|
1596
|
-
} catch {
|
|
1602
|
+
};
|
|
1603
|
+
const tryGzip = () => {
|
|
1597
1604
|
try {
|
|
1598
1605
|
return import_zlib.default.gunzipSync(buf);
|
|
1599
1606
|
} catch {
|
|
1600
|
-
return
|
|
1607
|
+
return null;
|
|
1601
1608
|
}
|
|
1609
|
+
};
|
|
1610
|
+
const result = algorithm === "gzip" ? tryGzip() ?? tryBrotli() : tryBrotli() ?? tryGzip();
|
|
1611
|
+
if (result === null) {
|
|
1612
|
+
throw new Error(`tricache: corrupt compressed payload (${buf.length} bytes, algorithm=${algorithm})`);
|
|
1602
1613
|
}
|
|
1614
|
+
return result;
|
|
1603
1615
|
}
|
|
1604
1616
|
function compressWithHeader(buf, algorithm) {
|
|
1605
1617
|
if (algorithm === "none") return buf;
|
|
@@ -2113,6 +2125,7 @@ var DiskTier = class {
|
|
|
2113
2125
|
dec = this.decrypt(raw);
|
|
2114
2126
|
} catch {
|
|
2115
2127
|
import_fs2.default.unlinkSync(filePath);
|
|
2128
|
+
this.diskUsageBytes -= Math.min(this.diskUsageBytes, stat.size);
|
|
2116
2129
|
this.fileCount = Math.max(0, this.fileCount - 1);
|
|
2117
2130
|
purged++;
|
|
2118
2131
|
continue;
|
|
@@ -2276,6 +2289,7 @@ var DiskTier = class {
|
|
|
2276
2289
|
dec = this.decrypt(raw);
|
|
2277
2290
|
} catch {
|
|
2278
2291
|
import_fs2.default.unlinkSync(filePath);
|
|
2292
|
+
this.diskUsageBytes -= Math.min(this.diskUsageBytes, stat.size);
|
|
2279
2293
|
this.fileCount = Math.max(0, this.fileCount - 1);
|
|
2280
2294
|
purged++;
|
|
2281
2295
|
continue;
|
|
@@ -2353,6 +2367,7 @@ return 0
|
|
|
2353
2367
|
`;
|
|
2354
2368
|
var SNAPSHOT_VERSION = 1;
|
|
2355
2369
|
var DEFAULT_SNAPSHOT_MAX_AGE = 2 * 60 * 60 * 1e3;
|
|
2370
|
+
var INDEFINITE_TTL_SECONDS = 31536e3;
|
|
2356
2371
|
var SNAPSHOT_MAX_FILE_BYTES = 220 * 1024 * 1024;
|
|
2357
2372
|
var DEFAULT_CATEGORY_LIMITS = {
|
|
2358
2373
|
"default": { maxEntries: 500, maxSizeBytes: 50 * 1024 * 1024 }
|
|
@@ -2627,7 +2642,8 @@ var CacheService = class _CacheService {
|
|
|
2627
2642
|
logger,
|
|
2628
2643
|
options.encryptionMode,
|
|
2629
2644
|
options.previousEncryptionKey,
|
|
2630
|
-
options.previousEncryptionMode
|
|
2645
|
+
options.previousEncryptionMode,
|
|
2646
|
+
{ strictKeyValidation: options.strictKeyValidation ?? false }
|
|
2631
2647
|
);
|
|
2632
2648
|
const rawForbidden = options.forbiddenSnapshotPrefixes ?? [...DEFAULT_FORBIDDEN_PREFIXES];
|
|
2633
2649
|
const forbiddenPrefixes = ns ? rawForbidden.map((p) => `${ns}:${p}`) : rawForbidden;
|
|
@@ -2649,8 +2665,14 @@ var CacheService = class _CacheService {
|
|
|
2649
2665
|
redisHost: options.redisHost ?? process.env.REDIS_HOST ?? "",
|
|
2650
2666
|
redisPort: options.redisPort ?? 6379,
|
|
2651
2667
|
redisTls: options.redisTls ?? process.env.NODE_ENV === "production",
|
|
2652
|
-
disableRedis: options.disableRedis ??
|
|
2668
|
+
disableRedis: options.disableRedis ?? (() => {
|
|
2669
|
+
const explicitConfig = Boolean(
|
|
2670
|
+
options.redisHost || options.redisClusterNodes?.length || options.redisSentinel
|
|
2671
|
+
);
|
|
2672
|
+
return !explicitConfig && process.env.NODE_ENV !== "production";
|
|
2673
|
+
})(),
|
|
2653
2674
|
encryptionKey: encKeyRaw,
|
|
2675
|
+
encryptionMode: options.encryptionMode,
|
|
2654
2676
|
snapshotPath: options.snapshotPath ?? import_path3.default.join(
|
|
2655
2677
|
import_os2.default.tmpdir(),
|
|
2656
2678
|
ns ? `tricache-snapshot-${ns}.msgpack` : "tricache-snapshot.msgpack"
|
|
@@ -2756,7 +2778,6 @@ var CacheService = class _CacheService {
|
|
|
2756
2778
|
}
|
|
2757
2779
|
this._shutdownHandler = () => {
|
|
2758
2780
|
if (!this._diskDisabled) this.writeSnapshot();
|
|
2759
|
-
process.exit(0);
|
|
2760
2781
|
};
|
|
2761
2782
|
process.once("SIGTERM", this._shutdownHandler);
|
|
2762
2783
|
process.once("SIGINT", this._shutdownHandler);
|
|
@@ -2935,7 +2956,10 @@ var CacheService = class _CacheService {
|
|
|
2935
2956
|
disableRedis: this._redisDisabled,
|
|
2936
2957
|
redisClusterNodes: o.redisClusterNodes,
|
|
2937
2958
|
redisSentinel: o.redisSentinel,
|
|
2938
|
-
|
|
2959
|
+
// Never surface key material through diagnostics — a JSON.stringify of
|
|
2960
|
+
// this object previously leaked the raw encryption key.
|
|
2961
|
+
encryptionKey: o.encryptionKey ? "[REDACTED]" : void 0,
|
|
2962
|
+
encryptionMode: o.encryptionMode,
|
|
2939
2963
|
l2WriteMode: o.l2WriteMode,
|
|
2940
2964
|
instanceName: o.instanceName,
|
|
2941
2965
|
frozen: o.frozen,
|
|
@@ -3140,24 +3164,7 @@ var CacheService = class _CacheService {
|
|
|
3140
3164
|
for (const [id, fields] of entries) {
|
|
3141
3165
|
this._lastStreamId = id;
|
|
3142
3166
|
this.counters.streamEntriesReceived++;
|
|
3143
|
-
|
|
3144
|
-
let key = "";
|
|
3145
|
-
let src = "";
|
|
3146
|
-
let tagVersion;
|
|
3147
|
-
for (let i = 0; i < fields.length; i += 2) {
|
|
3148
|
-
const f = fields[i];
|
|
3149
|
-
const v = fields[i + 1];
|
|
3150
|
-
if (f === "op") op = v;
|
|
3151
|
-
else if (f === "key") key = v;
|
|
3152
|
-
else if (f === "src") src = v;
|
|
3153
|
-
else if (f === "tagVersion") tagVersion = parseInt(v, 10);
|
|
3154
|
-
}
|
|
3155
|
-
if (src === this.instanceId) {
|
|
3156
|
-
this.counters.invSkipped++;
|
|
3157
|
-
continue;
|
|
3158
|
-
}
|
|
3159
|
-
this.counters.invReceived++;
|
|
3160
|
-
this._applyInvalidationEvent(op, key, tagVersion);
|
|
3167
|
+
this._processStreamEntry(fields);
|
|
3161
3168
|
}
|
|
3162
3169
|
}
|
|
3163
3170
|
}
|
|
@@ -3178,6 +3185,51 @@ var CacheService = class _CacheService {
|
|
|
3178
3185
|
};
|
|
3179
3186
|
void runLoop();
|
|
3180
3187
|
}
|
|
3188
|
+
/**
|
|
3189
|
+
* Validate + apply one XREAD stream entry. `fields` is the flat ioredis
|
|
3190
|
+
* shape [name, value, name, value, …]. Mirrors the schema rules of the
|
|
3191
|
+
* pubsub path (`_handleBackplaneMessage`): op must be a known operation,
|
|
3192
|
+
* key/src must be non-empty strings, tagVersion must parse to a finite
|
|
3193
|
+
* number. Invalid entries are logged and dropped — never applied.
|
|
3194
|
+
*/
|
|
3195
|
+
/** @internal Exposed for testing. */
|
|
3196
|
+
_processStreamEntry(fields) {
|
|
3197
|
+
let op = "";
|
|
3198
|
+
let key = "";
|
|
3199
|
+
let src = "";
|
|
3200
|
+
let tagVersionRaw;
|
|
3201
|
+
for (let i = 0; i + 1 < fields.length; i += 2) {
|
|
3202
|
+
const f = fields[i];
|
|
3203
|
+
const v = fields[i + 1];
|
|
3204
|
+
if (f === "op") op = v;
|
|
3205
|
+
else if (f === "key") key = v;
|
|
3206
|
+
else if (f === "src") src = v;
|
|
3207
|
+
else if (f === "tagVersion") tagVersionRaw = v;
|
|
3208
|
+
}
|
|
3209
|
+
const validOp = op === "del" || op === "del-glob" || op === "tag_incr";
|
|
3210
|
+
if (!validOp || typeof key !== "string" || !key || typeof src !== "string") {
|
|
3211
|
+
this.logger.warn("Backplane: rejected invalid stream entry", {
|
|
3212
|
+
op: String(op).slice(0, 40),
|
|
3213
|
+
key: typeof key === "string" ? key.slice(0, 100).replace(/[\r\n\t]/g, " ") : String(key)
|
|
3214
|
+
});
|
|
3215
|
+
return;
|
|
3216
|
+
}
|
|
3217
|
+
let tagVersion;
|
|
3218
|
+
if (tagVersionRaw !== void 0) {
|
|
3219
|
+
const parsed = parseInt(tagVersionRaw, 10);
|
|
3220
|
+
if (!Number.isFinite(parsed)) {
|
|
3221
|
+
this.logger.warn("Backplane: rejected stream entry with non-numeric tagVersion", { key });
|
|
3222
|
+
return;
|
|
3223
|
+
}
|
|
3224
|
+
tagVersion = parsed;
|
|
3225
|
+
}
|
|
3226
|
+
if (src === this.instanceId) {
|
|
3227
|
+
this.counters.invSkipped++;
|
|
3228
|
+
return;
|
|
3229
|
+
}
|
|
3230
|
+
this.counters.invReceived++;
|
|
3231
|
+
this._applyInvalidationEvent(op, key, tagVersion);
|
|
3232
|
+
}
|
|
3181
3233
|
_applyInvalidationEvent(op, key, tagVersion) {
|
|
3182
3234
|
if (op === "del") {
|
|
3183
3235
|
this.l1.delete(key);
|
|
@@ -3864,7 +3916,8 @@ var CacheService = class _CacheService {
|
|
|
3864
3916
|
async set(cacheKey, data, ttlSeconds = 300, priority, opts) {
|
|
3865
3917
|
const span = this._startSpan("tricache.set");
|
|
3866
3918
|
if (this.opts.tracer) span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
3867
|
-
const
|
|
3919
|
+
const effectiveTtlSeconds = ttlSeconds > 0 ? ttlSeconds : ttlSeconds === 0 ? INDEFINITE_TTL_SECONDS : ttlSeconds;
|
|
3920
|
+
const ttlMs = this._jitterTtl(effectiveTtlSeconds * 1e3);
|
|
3868
3921
|
const p = priority ?? inferPriority(cacheKey);
|
|
3869
3922
|
const k = this.nk(cacheKey);
|
|
3870
3923
|
this.counters.sets++;
|
|
@@ -4706,8 +4759,9 @@ var CacheService = class _CacheService {
|
|
|
4706
4759
|
const deadline = Date.now() + acquireTimeout;
|
|
4707
4760
|
let acquired = false;
|
|
4708
4761
|
if (!this._redisDisabled) {
|
|
4762
|
+
let client = null;
|
|
4709
4763
|
try {
|
|
4710
|
-
|
|
4764
|
+
client = await this.getRedis();
|
|
4711
4765
|
while (Date.now() <= deadline) {
|
|
4712
4766
|
const res = await client.set(lockKey, token, "EX", ttlSeconds, "NX");
|
|
4713
4767
|
if (res === "OK") {
|
|
@@ -4721,6 +4775,15 @@ var CacheService = class _CacheService {
|
|
|
4721
4775
|
if (!acquired) {
|
|
4722
4776
|
throw new Error(`Failed to acquire lock for resource "${resourceKey}" within ${acquireTimeout}ms`);
|
|
4723
4777
|
}
|
|
4778
|
+
} catch (err) {
|
|
4779
|
+
if (err.message.startsWith("Failed to acquire lock")) throw err;
|
|
4780
|
+
this.logger.debug("Distributed lock: Redis error, falling back to in-process mutex", {
|
|
4781
|
+
resourceKey,
|
|
4782
|
+
error: err.message
|
|
4783
|
+
});
|
|
4784
|
+
client = null;
|
|
4785
|
+
}
|
|
4786
|
+
if (client !== null && acquired) {
|
|
4724
4787
|
try {
|
|
4725
4788
|
return await fn();
|
|
4726
4789
|
} finally {
|
|
@@ -4736,12 +4799,6 @@ var CacheService = class _CacheService {
|
|
|
4736
4799
|
} catch {
|
|
4737
4800
|
}
|
|
4738
4801
|
}
|
|
4739
|
-
} catch (err) {
|
|
4740
|
-
if (err.message.startsWith("Failed to acquire lock")) throw err;
|
|
4741
|
-
this.logger.debug("Distributed lock: Redis error, falling back to in-process mutex", {
|
|
4742
|
-
resourceKey,
|
|
4743
|
-
error: err.message
|
|
4744
|
-
});
|
|
4745
4802
|
}
|
|
4746
4803
|
}
|
|
4747
4804
|
let unlock = () => {
|
|
@@ -5210,23 +5267,46 @@ var TriCacheHandler = class {
|
|
|
5210
5267
|
await this.cache.set(cacheKey, stored, ttl, void 0, tags.length > 0 ? { tags } : void 0);
|
|
5211
5268
|
}
|
|
5212
5269
|
/**
|
|
5213
|
-
*
|
|
5214
|
-
*
|
|
5270
|
+
* Returns the expiration timestamp (ms since epoch) of the most relevant
|
|
5271
|
+
* cached entry for the given tags, or 0 when nothing is tracked. Next.js
|
|
5272
|
+
* uses this to decide whether a revalidation is due — a hardcoded 0 means
|
|
5273
|
+
* "always expired", so this reads the real stored entries instead.
|
|
5274
|
+
*/
|
|
5275
|
+
async getExpiration(tags) {
|
|
5276
|
+
try {
|
|
5277
|
+
let maxExpiration = 0;
|
|
5278
|
+
const tagSet = new Set(tags);
|
|
5279
|
+
const l1 = this.cache.l1;
|
|
5280
|
+
l1.scan((key, entry) => {
|
|
5281
|
+
if (!entry.tagVersions) return;
|
|
5282
|
+
for (const tag of Object.keys(entry.tagVersions)) {
|
|
5283
|
+
if (!tagSet.has(tag)) continue;
|
|
5284
|
+
const exp = entry.expiresAt ?? 0;
|
|
5285
|
+
if (exp > maxExpiration) maxExpiration = exp;
|
|
5286
|
+
return;
|
|
5287
|
+
}
|
|
5288
|
+
}, 0);
|
|
5289
|
+
return maxExpiration;
|
|
5290
|
+
} catch (err) {
|
|
5291
|
+
this.logger.debug("Next.js cacheHandler getExpiration fail-soft", { error: err.message });
|
|
5292
|
+
return 0;
|
|
5293
|
+
}
|
|
5294
|
+
}
|
|
5295
|
+
/**
|
|
5296
|
+
* Reconcile local generational tag knowledge against shared state: drops the
|
|
5297
|
+
* in-memory tag-version cache so every subsequent read re-syncs versions
|
|
5298
|
+
* from Redis. The previous implementation was an empty try/catch.
|
|
5215
5299
|
*/
|
|
5216
5300
|
async refreshTags() {
|
|
5217
5301
|
try {
|
|
5302
|
+
const cache = this.cache;
|
|
5303
|
+
cache.tagVersions?.clear();
|
|
5218
5304
|
} catch (err) {
|
|
5219
5305
|
this.logger.debug("Next.js cacheHandler refreshTags fail-soft catch", {
|
|
5220
5306
|
error: err.message
|
|
5221
5307
|
});
|
|
5222
5308
|
}
|
|
5223
5309
|
}
|
|
5224
|
-
/**
|
|
5225
|
-
* Returns the maximum expiration timestamp across the given tags (or 0 if not tracking).
|
|
5226
|
-
*/
|
|
5227
|
-
async getExpiration(_tags) {
|
|
5228
|
-
return 0;
|
|
5229
|
-
}
|
|
5230
5310
|
/**
|
|
5231
5311
|
* Update or invalidate tags cluster-wide.
|
|
5232
5312
|
*/
|
package/dist/next/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as CacheOptions, C as CacheService, I as ILogger } from '../cache-service-
|
|
1
|
+
import { f as CacheOptions, C as CacheService, I as ILogger } from '../cache-service-D8tIXmzb.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Next.js 16 modern CacheHandler value shape for `"use cache"` components.
|
|
@@ -145,14 +145,18 @@ declare class TriCacheHandler {
|
|
|
145
145
|
*/
|
|
146
146
|
set(cacheKey: string, pendingEntry: Promise<CacheHandlerValue | null>, ctx?: CacheHandlerContext): Promise<void>;
|
|
147
147
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
148
|
+
* Returns the expiration timestamp (ms since epoch) of the most relevant
|
|
149
|
+
* cached entry for the given tags, or 0 when nothing is tracked. Next.js
|
|
150
|
+
* uses this to decide whether a revalidation is due — a hardcoded 0 means
|
|
151
|
+
* "always expired", so this reads the real stored entries instead.
|
|
150
152
|
*/
|
|
151
|
-
|
|
153
|
+
getExpiration(tags: string[]): Promise<number>;
|
|
152
154
|
/**
|
|
153
|
-
*
|
|
155
|
+
* Reconcile local generational tag knowledge against shared state: drops the
|
|
156
|
+
* in-memory tag-version cache so every subsequent read re-syncs versions
|
|
157
|
+
* from Redis. The previous implementation was an empty try/catch.
|
|
154
158
|
*/
|
|
155
|
-
|
|
159
|
+
refreshTags(): Promise<void>;
|
|
156
160
|
/**
|
|
157
161
|
* Update or invalidate tags cluster-wide.
|
|
158
162
|
*/
|
package/dist/next/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CacheService
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-EUGYR7KR.js";
|
|
4
4
|
import {
|
|
5
5
|
consoleLogger
|
|
6
|
-
} from "../chunk-
|
|
6
|
+
} from "../chunk-X5AXHPVD.js";
|
|
7
7
|
|
|
8
8
|
// src/next/cache-handler.ts
|
|
9
9
|
var PRESET_CACHE_LIFE_PROFILES = {
|
|
@@ -157,23 +157,46 @@ var TriCacheHandler = class {
|
|
|
157
157
|
await this.cache.set(cacheKey, stored, ttl, void 0, tags.length > 0 ? { tags } : void 0);
|
|
158
158
|
}
|
|
159
159
|
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
160
|
+
* Returns the expiration timestamp (ms since epoch) of the most relevant
|
|
161
|
+
* cached entry for the given tags, or 0 when nothing is tracked. Next.js
|
|
162
|
+
* uses this to decide whether a revalidation is due — a hardcoded 0 means
|
|
163
|
+
* "always expired", so this reads the real stored entries instead.
|
|
164
|
+
*/
|
|
165
|
+
async getExpiration(tags) {
|
|
166
|
+
try {
|
|
167
|
+
let maxExpiration = 0;
|
|
168
|
+
const tagSet = new Set(tags);
|
|
169
|
+
const l1 = this.cache.l1;
|
|
170
|
+
l1.scan((key, entry) => {
|
|
171
|
+
if (!entry.tagVersions) return;
|
|
172
|
+
for (const tag of Object.keys(entry.tagVersions)) {
|
|
173
|
+
if (!tagSet.has(tag)) continue;
|
|
174
|
+
const exp = entry.expiresAt ?? 0;
|
|
175
|
+
if (exp > maxExpiration) maxExpiration = exp;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
}, 0);
|
|
179
|
+
return maxExpiration;
|
|
180
|
+
} catch (err) {
|
|
181
|
+
this.logger.debug("Next.js cacheHandler getExpiration fail-soft", { error: err.message });
|
|
182
|
+
return 0;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Reconcile local generational tag knowledge against shared state: drops the
|
|
187
|
+
* in-memory tag-version cache so every subsequent read re-syncs versions
|
|
188
|
+
* from Redis. The previous implementation was an empty try/catch.
|
|
162
189
|
*/
|
|
163
190
|
async refreshTags() {
|
|
164
191
|
try {
|
|
192
|
+
const cache = this.cache;
|
|
193
|
+
cache.tagVersions?.clear();
|
|
165
194
|
} catch (err) {
|
|
166
195
|
this.logger.debug("Next.js cacheHandler refreshTags fail-soft catch", {
|
|
167
196
|
error: err.message
|
|
168
197
|
});
|
|
169
198
|
}
|
|
170
199
|
}
|
|
171
|
-
/**
|
|
172
|
-
* Returns the maximum expiration timestamp across the given tags (or 0 if not tracking).
|
|
173
|
-
*/
|
|
174
|
-
async getExpiration(_tags) {
|
|
175
|
-
return 0;
|
|
176
|
-
}
|
|
177
200
|
/**
|
|
178
201
|
* Update or invalidate tags cluster-wide.
|
|
179
202
|
*/
|
package/dist/prisma/index.cjs
CHANGED
|
@@ -81,7 +81,8 @@ function withTriCache(options) {
|
|
|
81
81
|
async $allOperations({ model, operation, args, query }) {
|
|
82
82
|
const modelTag = model.toLowerCase();
|
|
83
83
|
if (autoInvalidate && MUTATION_OPERATIONS.has(operation)) {
|
|
84
|
-
const
|
|
84
|
+
const { cache: _cacheOpt, ...cleanMutationArgs } = args ?? {};
|
|
85
|
+
const result = await query(cleanMutationArgs);
|
|
85
86
|
await cache.invalidateTag(modelTag);
|
|
86
87
|
return result;
|
|
87
88
|
}
|
package/dist/prisma/index.d.ts
CHANGED
package/dist/prisma/index.js
CHANGED
|
@@ -46,7 +46,8 @@ function withTriCache(options) {
|
|
|
46
46
|
async $allOperations({ model, operation, args, query }) {
|
|
47
47
|
const modelTag = model.toLowerCase();
|
|
48
48
|
if (autoInvalidate && MUTATION_OPERATIONS.has(operation)) {
|
|
49
|
-
const
|
|
49
|
+
const { cache: _cacheOpt, ...cleanMutationArgs } = args ?? {};
|
|
50
|
+
const result = await query(cleanMutationArgs);
|
|
50
51
|
await cache.invalidateTag(modelTag);
|
|
51
52
|
return result;
|
|
52
53
|
}
|
|
@@ -57,6 +57,8 @@ var CacheEncryption = class {
|
|
|
57
57
|
_keyObj = null;
|
|
58
58
|
// parsed KeyObject — used for all AES modes
|
|
59
59
|
_mode;
|
|
60
|
+
/** Fail-closed on invalid key material when opted in via constructor opts. */
|
|
61
|
+
_strictKeyValidation;
|
|
60
62
|
_prevKey = null;
|
|
61
63
|
_prevKeyObj = null;
|
|
62
64
|
_prevMode = "aes-256-gcm";
|
|
@@ -122,11 +124,12 @@ var CacheEncryption = class {
|
|
|
122
124
|
}
|
|
123
125
|
return out;
|
|
124
126
|
}
|
|
125
|
-
constructor(keyBase642, loggerOrMode = consoleLogger, mode2 = "aes-256-gcm", previousKeyBase64, previousMode) {
|
|
127
|
+
constructor(keyBase642, loggerOrMode = consoleLogger, mode2 = "aes-256-gcm", previousKeyBase64, previousMode, opts) {
|
|
126
128
|
const logger = typeof loggerOrMode === "object" && loggerOrMode !== null ? loggerOrMode : consoleLogger;
|
|
127
129
|
const resolvedMode = typeof loggerOrMode === "string" ? loggerOrMode : mode2;
|
|
130
|
+
this._strictKeyValidation = opts?.strictKeyValidation ?? false;
|
|
128
131
|
this._mode = resolvedMode;
|
|
129
|
-
if (
|
|
132
|
+
if (keyBase642 === void 0 || keyBase642 === null) {
|
|
130
133
|
if (process.env.NODE_ENV === "production") {
|
|
131
134
|
logger.warn(
|
|
132
135
|
"SECURITY: encryption key not set \u2014 cache data stored unencrypted at rest",
|
|
@@ -135,11 +138,17 @@ var CacheEncryption = class {
|
|
|
135
138
|
}
|
|
136
139
|
return;
|
|
137
140
|
}
|
|
141
|
+
if (keyBase642 === "") {
|
|
142
|
+
const err = new Error("Encryption key is empty \u2014 set encryptionKey option or CACHE_ENCRYPTION_KEY");
|
|
143
|
+
if (this._strictKeyValidation) throw err;
|
|
144
|
+
logger.error("Empty encryption key \u2014 falling back to plaintext", {}, err);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
138
147
|
try {
|
|
139
148
|
const buf = Buffer.from(keyBase642, "base64");
|
|
140
149
|
const requiredLen = resolvedMode === "aes-256-gcm" ? 32 : resolvedMode === "aes-128-gcm" || resolvedMode === "aes-128-ctr" ? 16 : 0;
|
|
141
150
|
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
142
|
-
throw new Error(`${
|
|
151
|
+
throw new Error(`${resolvedMode} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
143
152
|
}
|
|
144
153
|
if (buf.length < 1) {
|
|
145
154
|
throw new Error("XOR key must be at least 1 byte");
|
|
@@ -163,6 +172,9 @@ var CacheEncryption = class {
|
|
|
163
172
|
logger.debug(`Cache encryption enabled (${mode2.toUpperCase()})`);
|
|
164
173
|
}
|
|
165
174
|
} catch (err) {
|
|
175
|
+
if (this._strictKeyValidation) {
|
|
176
|
+
throw err;
|
|
177
|
+
}
|
|
166
178
|
logger.error("Invalid encryption key \u2014 falling back to plaintext", {}, err);
|
|
167
179
|
}
|
|
168
180
|
if (previousKeyBase64) {
|
|
@@ -441,26 +453,25 @@ function compressBuffer(buf, algorithm) {
|
|
|
441
453
|
return buf;
|
|
442
454
|
}
|
|
443
455
|
function decompressBuffer(buf, algorithm = "brotli") {
|
|
444
|
-
|
|
456
|
+
const tryBrotli = () => {
|
|
445
457
|
try {
|
|
446
|
-
return import_zlib.default.
|
|
458
|
+
return import_zlib.default.brotliDecompressSync(buf);
|
|
447
459
|
} catch {
|
|
448
|
-
|
|
449
|
-
return import_zlib.default.brotliDecompressSync(buf);
|
|
450
|
-
} catch {
|
|
451
|
-
return buf;
|
|
452
|
-
}
|
|
460
|
+
return null;
|
|
453
461
|
}
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
return import_zlib.default.brotliDecompressSync(buf);
|
|
457
|
-
} catch {
|
|
462
|
+
};
|
|
463
|
+
const tryGzip = () => {
|
|
458
464
|
try {
|
|
459
465
|
return import_zlib.default.gunzipSync(buf);
|
|
460
466
|
} catch {
|
|
461
|
-
return
|
|
467
|
+
return null;
|
|
462
468
|
}
|
|
469
|
+
};
|
|
470
|
+
const result = algorithm === "gzip" ? tryGzip() ?? tryBrotli() : tryBrotli() ?? tryGzip();
|
|
471
|
+
if (result === null) {
|
|
472
|
+
throw new Error(`tricache: corrupt compressed payload (${buf.length} bytes, algorithm=${algorithm})`);
|
|
463
473
|
}
|
|
474
|
+
return result;
|
|
464
475
|
}
|
|
465
476
|
|
|
466
477
|
// src/serialize-worker.ts
|
package/dist/serialize-worker.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tricache",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Three-tier Node.js cache: L1 smart RAM (adaptive LFU/LRU + Count-Min Sketch) → L1.5 NVMe disk spill → L2 Redis/Valkey. Includes AES-256-GCM at-rest encryption, WASM Bloom filter, Stale-While-Revalidate, and thundering-herd prevention.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cache",
|
|
@@ -91,6 +91,7 @@
|
|
|
91
91
|
"@nestjs/core": "^11.0.0",
|
|
92
92
|
"@types/node": "^26.2.0",
|
|
93
93
|
"oxlint": "^1.79.0",
|
|
94
|
+
"redis": "^6.2.1",
|
|
94
95
|
"tsup": "^8.5.1",
|
|
95
96
|
"tsx": "^4.23.12",
|
|
96
97
|
"typescript": "^6.0.3",
|
|
@@ -107,8 +108,9 @@
|
|
|
107
108
|
"typecheck": "tsc --noEmit",
|
|
108
109
|
"typecheck:tests": "tsc --noEmit -p tsconfig.test.json",
|
|
109
110
|
"typecheck:all": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
|
|
110
|
-
"lint": "oxlint src tests",
|
|
111
|
+
"lint": "oxlint src tests scripts",
|
|
111
112
|
"test": "vitest run",
|
|
113
|
+
"test:integration": "vitest run --config vitest.integration.config.ts",
|
|
112
114
|
"test:watch": "vitest",
|
|
113
115
|
"bench": "node --expose-gc --max-old-space-size=8192 --import tsx/esm bench/benchmark.ts"
|
|
114
116
|
}
|