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
package/dist/next/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as CacheService } from '../cache-service-CyLdpo77.js';
|
|
2
|
+
import { C as CacheOptions, h as ILogger } from '../types-B6krw64X.js';
|
|
3
|
+
import 'crypto';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Next.js 16 modern CacheHandler value shape for `"use cache"` components.
|
|
@@ -138,21 +140,25 @@ declare class TriCacheHandler {
|
|
|
138
140
|
* Get an entry from cache. Checks softTags against generational tag versions
|
|
139
141
|
* and returns a fresh unlocked ReadableStream if the stored value was a stream.
|
|
140
142
|
*/
|
|
141
|
-
get(cacheKey: string, ctx?: CacheHandlerContext): Promise<CacheHandlerValue |
|
|
143
|
+
get(cacheKey: string, ctx?: CacheHandlerContext): Promise<CacheHandlerValue | undefined>;
|
|
142
144
|
/**
|
|
143
145
|
* Write an entry into cache. Drains incoming ReadableStream into a contiguous buffer
|
|
144
146
|
* before storing. Catches mid-flight stream aborts to avoid crashing Next.js.
|
|
145
147
|
*/
|
|
146
148
|
set(cacheKey: string, pendingEntry: Promise<CacheHandlerValue | null>, ctx?: CacheHandlerContext): Promise<void>;
|
|
147
149
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
+
* Returns the expiration timestamp (ms since epoch) of the most relevant
|
|
151
|
+
* cached entry for the given tags, or 0 when nothing is tracked. Next.js
|
|
152
|
+
* uses this to decide whether a revalidation is due — a hardcoded 0 means
|
|
153
|
+
* "always expired", so this reads the real stored entries instead.
|
|
150
154
|
*/
|
|
151
|
-
|
|
155
|
+
getExpiration(tags: string[]): Promise<number>;
|
|
152
156
|
/**
|
|
153
|
-
*
|
|
157
|
+
* Reconcile local generational tag knowledge against shared state: drops the
|
|
158
|
+
* in-memory tag-version cache so every subsequent read re-syncs versions
|
|
159
|
+
* from Redis. The previous implementation was an empty try/catch.
|
|
154
160
|
*/
|
|
155
|
-
|
|
161
|
+
refreshTags(): Promise<void>;
|
|
156
162
|
/**
|
|
157
163
|
* Update or invalidate tags cluster-wide.
|
|
158
164
|
*/
|
package/dist/next/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CacheService
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-6PQ5MOWR.js";
|
|
4
4
|
import {
|
|
5
5
|
consoleLogger
|
|
6
|
-
} from "../chunk-
|
|
6
|
+
} from "../chunk-LJ5QJB5O.js";
|
|
7
|
+
import "../chunk-XZL45ABR.js";
|
|
7
8
|
|
|
8
9
|
// src/next/cache-handler.ts
|
|
9
10
|
var PRESET_CACHE_LIFE_PROFILES = {
|
|
@@ -80,14 +81,15 @@ var TriCacheHandler = class {
|
|
|
80
81
|
async () => null
|
|
81
82
|
);
|
|
82
83
|
if (!stored || stored.data == null) {
|
|
83
|
-
return
|
|
84
|
+
return void 0;
|
|
84
85
|
}
|
|
85
86
|
if (ctx?.softTags && ctx.softTags.length > 0) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
const softTags = ctx.softTags;
|
|
88
|
+
const currentVers = await Promise.all(softTags.map((st) => this.cache.getTagVersion(st)));
|
|
89
|
+
for (let i = 0; i < softTags.length; i++) {
|
|
90
|
+
const storedSoftVer = stored.softTagVersions?.[softTags[i]] ?? 0;
|
|
91
|
+
if (currentVers[i] > storedSoftVer) {
|
|
92
|
+
return void 0;
|
|
91
93
|
}
|
|
92
94
|
}
|
|
93
95
|
}
|
|
@@ -141,9 +143,11 @@ var TriCacheHandler = class {
|
|
|
141
143
|
const tags = Array.from(/* @__PURE__ */ new Set([...entry.tags ?? [], ...ctx?.tags ?? []]));
|
|
142
144
|
let softTagVersions;
|
|
143
145
|
if (ctx?.softTags && ctx.softTags.length > 0) {
|
|
146
|
+
const softTags = ctx.softTags;
|
|
147
|
+
const vers = await Promise.all(softTags.map((st) => this.cache.getTagVersion(st)));
|
|
144
148
|
softTagVersions = {};
|
|
145
|
-
for (
|
|
146
|
-
softTagVersions[
|
|
149
|
+
for (let i = 0; i < softTags.length; i++) {
|
|
150
|
+
softTagVersions[softTags[i]] = vers[i];
|
|
147
151
|
}
|
|
148
152
|
}
|
|
149
153
|
const stored = {
|
|
@@ -157,23 +161,46 @@ var TriCacheHandler = class {
|
|
|
157
161
|
await this.cache.set(cacheKey, stored, ttl, void 0, tags.length > 0 ? { tags } : void 0);
|
|
158
162
|
}
|
|
159
163
|
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
164
|
+
* Returns the expiration timestamp (ms since epoch) of the most relevant
|
|
165
|
+
* cached entry for the given tags, or 0 when nothing is tracked. Next.js
|
|
166
|
+
* uses this to decide whether a revalidation is due — a hardcoded 0 means
|
|
167
|
+
* "always expired", so this reads the real stored entries instead.
|
|
168
|
+
*/
|
|
169
|
+
async getExpiration(tags) {
|
|
170
|
+
try {
|
|
171
|
+
let maxExpiration = 0;
|
|
172
|
+
const tagSet = new Set(tags);
|
|
173
|
+
const l1 = this.cache.l1;
|
|
174
|
+
l1.scan((key, entry) => {
|
|
175
|
+
if (!entry.tagVersions) return;
|
|
176
|
+
for (const tag of Object.keys(entry.tagVersions)) {
|
|
177
|
+
if (!tagSet.has(tag)) continue;
|
|
178
|
+
const exp = entry.expiresAt ?? 0;
|
|
179
|
+
if (exp > maxExpiration) maxExpiration = exp;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
}, 0);
|
|
183
|
+
return maxExpiration;
|
|
184
|
+
} catch (err) {
|
|
185
|
+
this.logger.debug("Next.js cacheHandler getExpiration fail-soft", { error: err.message });
|
|
186
|
+
return 0;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Reconcile local generational tag knowledge against shared state: drops the
|
|
191
|
+
* in-memory tag-version cache so every subsequent read re-syncs versions
|
|
192
|
+
* from Redis. The previous implementation was an empty try/catch.
|
|
162
193
|
*/
|
|
163
194
|
async refreshTags() {
|
|
164
195
|
try {
|
|
196
|
+
const cache = this.cache;
|
|
197
|
+
cache.tagVersions?.clear();
|
|
165
198
|
} catch (err) {
|
|
166
199
|
this.logger.debug("Next.js cacheHandler refreshTags fail-soft catch", {
|
|
167
200
|
error: err.message
|
|
168
201
|
});
|
|
169
202
|
}
|
|
170
203
|
}
|
|
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
204
|
/**
|
|
178
205
|
* Update or invalidate tags cluster-wide.
|
|
179
206
|
*/
|
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
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as CacheService } from '../cache-service-CyLdpo77.js';
|
|
2
|
+
import { W as WrapOptions } from '../types-B6krw64X.js';
|
|
3
|
+
import 'crypto';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* tricache/prisma — First-class Prisma Client extension for TriCache.
|
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,27 +138,33 @@ 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");
|
|
146
155
|
}
|
|
147
156
|
this._key = buf;
|
|
148
|
-
if (
|
|
157
|
+
if (resolvedMode !== "xor") {
|
|
149
158
|
this._keyObj = (0, import_crypto.createSecretKey)(buf);
|
|
150
159
|
const _wc = (0, import_crypto.createCipheriv)(
|
|
151
|
-
|
|
160
|
+
resolvedMode,
|
|
152
161
|
this._keyObj,
|
|
153
|
-
Buffer.alloc(
|
|
162
|
+
Buffer.alloc(resolvedMode === "aes-128-ctr" ? CTR_IV_BYTES : IV_BYTES)
|
|
154
163
|
);
|
|
155
164
|
_wc.update(Buffer.alloc(0));
|
|
156
165
|
_wc.final();
|
|
157
166
|
}
|
|
158
|
-
if (
|
|
167
|
+
if (resolvedMode === "xor") {
|
|
159
168
|
logger.warn(
|
|
160
169
|
"Cache obfuscation enabled (XOR). WARNING: XOR is NOT cryptographic \u2014 use only for dev or non-sensitive data."
|
|
161
170
|
);
|
|
@@ -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) {
|
|
@@ -420,6 +432,7 @@ var CacheEncryption = class {
|
|
|
420
432
|
return plain;
|
|
421
433
|
}
|
|
422
434
|
};
|
|
435
|
+
var MAGIC_ENVELOPE = Buffer.from([84, 82, 73, 67, 69, 78, 86, 49]);
|
|
423
436
|
|
|
424
437
|
// src/compression.ts
|
|
425
438
|
var import_zlib = __toESM(require("zlib"), 1);
|
|
@@ -441,26 +454,25 @@ function compressBuffer(buf, algorithm) {
|
|
|
441
454
|
return buf;
|
|
442
455
|
}
|
|
443
456
|
function decompressBuffer(buf, algorithm = "brotli") {
|
|
444
|
-
|
|
457
|
+
const tryBrotli = () => {
|
|
445
458
|
try {
|
|
446
|
-
return import_zlib.default.
|
|
459
|
+
return import_zlib.default.brotliDecompressSync(buf);
|
|
447
460
|
} catch {
|
|
448
|
-
|
|
449
|
-
return import_zlib.default.brotliDecompressSync(buf);
|
|
450
|
-
} catch {
|
|
451
|
-
return buf;
|
|
452
|
-
}
|
|
461
|
+
return null;
|
|
453
462
|
}
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
return import_zlib.default.brotliDecompressSync(buf);
|
|
457
|
-
} catch {
|
|
463
|
+
};
|
|
464
|
+
const tryGzip = () => {
|
|
458
465
|
try {
|
|
459
466
|
return import_zlib.default.gunzipSync(buf);
|
|
460
467
|
} catch {
|
|
461
|
-
return
|
|
468
|
+
return null;
|
|
462
469
|
}
|
|
470
|
+
};
|
|
471
|
+
const result = algorithm === "gzip" ? tryGzip() ?? tryBrotli() : tryBrotli() ?? tryGzip();
|
|
472
|
+
if (result === null) {
|
|
473
|
+
throw new Error(`tricache: corrupt compressed payload (${buf.length} bytes, algorithm=${algorithm})`);
|
|
463
474
|
}
|
|
475
|
+
return result;
|
|
464
476
|
}
|
|
465
477
|
|
|
466
478
|
// src/serialize-worker.ts
|