tricache 1.0.2 → 2.0.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/README.md +78 -90
- package/dashboards/tricache-alerts.yaml +80 -4
- package/dashboards/tricache-grafana.json +54 -1
- package/dashboards/tricache-inventory.rules.yml +21 -0
- package/dist/{cache-service-CWI4jkS7.d.cts → cache-service-BV5T8-20.d.cts} +122 -21
- package/dist/cache-service-UZXOQGEY.js +14 -0
- package/dist/{cache-service-B108FzER.d.ts → cache-service-tpyrXvlH.d.ts} +122 -21
- package/dist/{chunk-UBWLPKTI.js → chunk-EJXG7WO6.js} +152 -32
- package/dist/{chunk-H4GFLQ26.js → chunk-KV7KISK2.js} +47 -1
- package/dist/{chunk-WYF7NOAT.js → chunk-MB3DMVWN.js} +2111 -543
- package/dist/{chunk-OYDU3SMI.js → chunk-RBJRTGFK.js} +5 -0
- package/dist/chunk-S5N65I45.js +17 -0
- package/dist/cli.cjs +2454 -572
- package/dist/cli.js +186 -11
- package/dist/dashboard/index.d.cts +2 -2
- package/dist/dashboard/index.d.ts +2 -2
- package/dist/drizzle/index.cjs +2335 -571
- package/dist/drizzle/index.d.cts +2 -2
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +6 -2
- package/dist/edge/index.cjs +145 -43
- package/dist/edge/index.d.cts +11 -4
- package/dist/edge/index.d.ts +11 -4
- package/dist/edge/index.js +118 -43
- package/dist/hono/index.cjs +2322 -569
- package/dist/hono/index.d.cts +2 -2
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +2 -2
- package/dist/{hono-C886ok3X.d.cts → hono-CMv2VdQ6.d.cts} +18 -5
- package/dist/{hono-3rDMYVtY.d.ts → hono-Dvq04kX9.d.ts} +18 -5
- package/dist/http/index.cjs +2323 -570
- package/dist/http/index.d.cts +3 -3
- package/dist/http/index.d.ts +3 -3
- package/dist/http/index.js +4 -4
- package/dist/index.cjs +2554 -620
- package/dist/index.d.cts +88 -23
- package/dist/index.d.ts +88 -23
- package/dist/index.js +873 -16
- package/dist/{murmur3-CMGw9PbV.d.cts → murmur3-DF8BFRnu.d.cts} +1 -0
- package/dist/{murmur3-CMGw9PbV.d.ts → murmur3-DF8BFRnu.d.ts} +1 -0
- package/dist/nestjs/index.cjs +2292 -574
- package/dist/nestjs/index.d.cts +19 -5
- package/dist/nestjs/index.d.ts +19 -5
- package/dist/nestjs/index.js +25 -19
- package/dist/next/index.cjs +2306 -571
- package/dist/next/index.d.cts +8 -2
- package/dist/next/index.d.ts +8 -2
- package/dist/next/index.js +36 -9
- package/dist/prisma/index.cjs +26 -25
- package/dist/prisma/index.d.cts +2 -2
- package/dist/prisma/index.d.ts +2 -2
- package/dist/prisma/index.js +14 -25
- package/dist/serialize-worker.cjs +162 -39
- package/dist/serialize-worker.js +9 -7
- package/dist/{types-BA4pz1A0.d.cts → types-F8vRWMrE.d.cts} +162 -45
- package/dist/{types-BA4pz1A0.d.ts → types-F8vRWMrE.d.ts} +162 -45
- package/package.json +21 -2
- package/src/adapters/auto-pipeliner.ts +52 -11
- package/src/cache-service.ts +1537 -415
- package/src/cli.ts +2 -9
- package/src/codec.ts +3 -3
- package/src/compression.ts +39 -16
- package/src/cross-region.ts +1 -1
- package/src/disk-tier.ts +283 -43
- package/src/drizzle/index.ts +2 -1
- package/src/edge/adapters/cloudflare-do.ts +25 -10
- package/src/edge/adapters/cloudflare-kv.ts +12 -9
- package/src/edge/adapters/upstash.ts +27 -11
- package/src/edge/cache.ts +59 -17
- package/src/edge/types.ts +15 -5
- package/src/encryption.ts +50 -18
- package/src/env.d.ts +1 -1
- package/src/errors.ts +12 -0
- package/src/http/utils.ts +6 -0
- package/src/index.ts +6 -0
- package/src/latency-watchdog.ts +5 -1
- package/src/nestjs/decorators.ts +11 -6
- package/src/nestjs/tricache.module.ts +2 -1
- package/src/nestjs/tricache.store.ts +21 -9
- package/src/nestjs/types.ts +1 -1
- package/src/next/cache-handler.ts +24 -4
- package/src/next/types.ts +4 -0
- package/src/origin-capacity.ts +134 -0
- package/src/prisma/index.ts +14 -32
- package/src/remote-snapshot.ts +37 -27
- package/src/serialize-worker.ts +8 -5
- package/src/sigv4-snapshot-adapter.ts +37 -27
- package/src/smart-memory-cache.ts +62 -22
- package/src/types.ts +92 -35
- package/src/utils/config-comparison.ts +29 -0
- package/src/utils/lz4-size.ts +74 -0
- package/src/utils/namespace-key.ts +48 -0
- package/src/utils/query-key.ts +13 -0
- package/src/utils/snapshot-transfer.ts +83 -0
- package/src/utils/work-limits.ts +73 -0
- package/src/utils/write-revision.ts +4 -0
- package/src/wasm/bloom-filter-wasm.ts +16 -1
- package/src/worker-pool.ts +28 -8
- package/src/wtiny-lfu.ts +62 -14
- package/dist/cache-service-GYPZFB7O.js +0 -12
- package/dist/chunk-VNRDL5RA.js +0 -666
|
@@ -29,9 +29,13 @@ var IV_BYTES = 12;
|
|
|
29
29
|
var TAG_BYTES = 16;
|
|
30
30
|
var PREFIX_256 = "enc:v1:";
|
|
31
31
|
var PREFIX_128 = "a128:v1:";
|
|
32
|
+
var PREFIX_BOUND_256 = "enc:v2:";
|
|
33
|
+
var PREFIX_BOUND_128 = "a128:v2:";
|
|
32
34
|
var PREFIX_XOR = "xor:v1:";
|
|
33
35
|
var MAGIC_256 = Buffer.from([84, 82, 73, 67, 49, 69, 78, 67]);
|
|
34
36
|
var MAGIC_128 = Buffer.from([84, 82, 73, 67, 49, 49, 50, 56]);
|
|
37
|
+
var MAGIC_BOUND_256 = Buffer.from("TRIC2ENC");
|
|
38
|
+
var MAGIC_BOUND_128 = Buffer.from("TRIC2128");
|
|
35
39
|
var MAGIC_XOR = Buffer.from([84, 82, 73, 67, 49, 88, 79, 82]);
|
|
36
40
|
var MAGIC_CTR = Buffer.from([84, 82, 73, 67, 49, 67, 84, 82]);
|
|
37
41
|
var MAGIC_LEN = 8;
|
|
@@ -46,6 +50,7 @@ var CacheEncryption = class {
|
|
|
46
50
|
_mode;
|
|
47
51
|
/** Fail-closed on invalid key material when opted in via constructor opts. */
|
|
48
52
|
_strictKeyValidation;
|
|
53
|
+
_allowLegacyPlaintextRead;
|
|
49
54
|
_prevKey = null;
|
|
50
55
|
_prevKeyObj = null;
|
|
51
56
|
_prevMode = "aes-256-gcm";
|
|
@@ -115,6 +120,7 @@ var CacheEncryption = class {
|
|
|
115
120
|
const logger = typeof loggerOrMode === "object" && loggerOrMode !== null ? loggerOrMode : consoleLogger;
|
|
116
121
|
const resolvedMode = typeof loggerOrMode === "string" ? loggerOrMode : mode;
|
|
117
122
|
this._strictKeyValidation = opts?.strictKeyValidation ?? false;
|
|
123
|
+
this._allowLegacyPlaintextRead = opts?.allowLegacyPlaintextRead ?? true;
|
|
118
124
|
this._mode = resolvedMode;
|
|
119
125
|
if (keyBase64 === void 0 || keyBase64 === null) {
|
|
120
126
|
if (process.env.NODE_ENV === "production") {
|
|
@@ -201,7 +207,8 @@ var CacheEncryption = class {
|
|
|
201
207
|
keyBase64: this._key ? this._key.toString("base64") : "",
|
|
202
208
|
mode: this._mode,
|
|
203
209
|
prevKeyBase64: this._prevKey ? this._prevKey.toString("base64") : void 0,
|
|
204
|
-
prevMode: this._prevMode
|
|
210
|
+
prevMode: this._prevMode,
|
|
211
|
+
allowLegacyPlaintextRead: this._allowLegacyPlaintextRead
|
|
205
212
|
};
|
|
206
213
|
}
|
|
207
214
|
/**
|
|
@@ -247,7 +254,7 @@ var CacheEncryption = class {
|
|
|
247
254
|
* Returns a mode-prefixed base64 string when a key is configured;
|
|
248
255
|
* otherwise returns the original string unchanged (backward-compatible).
|
|
249
256
|
*/
|
|
250
|
-
encrypt(plaintext) {
|
|
257
|
+
encrypt(plaintext, context) {
|
|
251
258
|
if (!this._key) return plaintext;
|
|
252
259
|
if (this._mode === "xor") {
|
|
253
260
|
const masked = this._xorBuffer(Buffer.from(plaintext, "utf8"));
|
|
@@ -265,9 +272,10 @@ var CacheEncryption = class {
|
|
|
265
272
|
return PREFIX_CTR + out2.toString("base64");
|
|
266
273
|
}
|
|
267
274
|
const algo = this._mode === "aes-128-gcm" ? "aes-128-gcm" : "aes-256-gcm";
|
|
268
|
-
const prefix = this._mode === "aes-128-gcm" ? PREFIX_128 : PREFIX_256;
|
|
275
|
+
const prefix = context === void 0 ? this._mode === "aes-128-gcm" ? PREFIX_128 : PREFIX_256 : this._mode === "aes-128-gcm" ? PREFIX_BOUND_128 : PREFIX_BOUND_256;
|
|
269
276
|
const iv = this._nextIV();
|
|
270
277
|
const c = createCipheriv(algo, this._keyObj, iv);
|
|
278
|
+
if (context !== void 0) c.setAAD(Buffer.from(context, "utf8"));
|
|
271
279
|
const enc = c.update(plaintext, "utf8");
|
|
272
280
|
c.final();
|
|
273
281
|
const out = Buffer.allocUnsafe(IV_BYTES + TAG_BYTES + enc.length);
|
|
@@ -281,20 +289,30 @@ var CacheEncryption = class {
|
|
|
281
289
|
* Auto-detects the envelope prefix; returns plaintext unchanged if no prefix matches.
|
|
282
290
|
* Falls back to `previousEncryptionKey` when the primary key fails (key rotation).
|
|
283
291
|
*/
|
|
284
|
-
|
|
292
|
+
assertReadPolicy(value, context) {
|
|
293
|
+
if (!this.isEnabled || this._allowLegacyPlaintextRead || !this._mode.endsWith("-gcm")) return;
|
|
294
|
+
if (context === void 0) throw new Error("Authenticated cache read requires context");
|
|
295
|
+
if (typeof value === "string") {
|
|
296
|
+
if (value.startsWith("ecp:v1:")) return this.assertReadPolicy(Buffer.from(value.slice(7), "base64"), context);
|
|
297
|
+
if (value.startsWith(PREFIX_BOUND_256) || value.startsWith(PREFIX_BOUND_128)) return;
|
|
298
|
+
} else if (value.subarray(0, 8).equals(MAGIC_BOUND_256) || value.subarray(0, 8).equals(MAGIC_BOUND_128)) return;
|
|
299
|
+
throw new Error("Legacy or plaintext cache value rejected; opt in to allowLegacyPlaintextRead for migration");
|
|
300
|
+
}
|
|
301
|
+
decrypt(value, context) {
|
|
302
|
+
this.assertReadPolicy(value, context);
|
|
285
303
|
try {
|
|
286
|
-
return this._decryptWithKey(value, this._key, this._keyObj);
|
|
304
|
+
return this._decryptWithKey(value, this._key, this._keyObj, context);
|
|
287
305
|
} catch (primaryErr) {
|
|
288
306
|
if (this._prevKey) {
|
|
289
307
|
try {
|
|
290
|
-
return this._decryptWithKey(value, this._prevKey, this._prevKeyObj);
|
|
308
|
+
return this._decryptWithKey(value, this._prevKey, this._prevKeyObj, context);
|
|
291
309
|
} catch {
|
|
292
310
|
}
|
|
293
311
|
}
|
|
294
312
|
throw primaryErr;
|
|
295
313
|
}
|
|
296
314
|
}
|
|
297
|
-
_decryptWithKey(value, key, keyObj) {
|
|
315
|
+
_decryptWithKey(value, key, keyObj, context) {
|
|
298
316
|
if (value.startsWith(PREFIX_XOR)) {
|
|
299
317
|
if (!key) throw new Error("Cannot decrypt: encryption key is not set");
|
|
300
318
|
const masked = Buffer.from(value.slice(PREFIX_XOR.length), "base64");
|
|
@@ -314,17 +332,20 @@ var CacheEncryption = class {
|
|
|
314
332
|
const fin = d2.final();
|
|
315
333
|
return (fin.length > 0 ? Buffer.concat([plain2, fin]) : plain2).toString("utf8");
|
|
316
334
|
}
|
|
317
|
-
const
|
|
318
|
-
const
|
|
335
|
+
const bound = value.startsWith(PREFIX_BOUND_256) || value.startsWith(PREFIX_BOUND_128);
|
|
336
|
+
const is256 = value.startsWith(PREFIX_256) || value.startsWith(PREFIX_BOUND_256);
|
|
337
|
+
const is128 = value.startsWith(PREFIX_128) || value.startsWith(PREFIX_BOUND_128);
|
|
319
338
|
if (!is256 && !is128) return value;
|
|
320
339
|
if (!keyObj) throw new Error("Cannot decrypt: encryption key is not set");
|
|
321
|
-
|
|
340
|
+
if (bound && context === void 0) throw new Error("Encrypted value requires authenticated context");
|
|
341
|
+
const prefix = bound ? is128 ? PREFIX_BOUND_128 : PREFIX_BOUND_256 : is128 ? PREFIX_128 : PREFIX_256;
|
|
322
342
|
const algo = is128 ? "aes-128-gcm" : "aes-256-gcm";
|
|
323
343
|
const combined = Buffer.from(value.slice(prefix.length), "base64");
|
|
324
344
|
const iv = combined.subarray(0, IV_BYTES);
|
|
325
345
|
const tag = combined.subarray(IV_BYTES, IV_BYTES + TAG_BYTES);
|
|
326
346
|
const ct = combined.subarray(IV_BYTES + TAG_BYTES);
|
|
327
347
|
const d = createDecipheriv(algo, keyObj, iv);
|
|
348
|
+
if (bound) d.setAAD(Buffer.from(context, "utf8"));
|
|
328
349
|
d.setAuthTag(tag);
|
|
329
350
|
const plain = d.update(ct);
|
|
330
351
|
d.final();
|
|
@@ -332,7 +353,7 @@ var CacheEncryption = class {
|
|
|
332
353
|
}
|
|
333
354
|
// ── Buffer (disk / snapshot) ──────────────────────────────────────────────
|
|
334
355
|
/** Encrypt a raw Buffer for disk/snapshot. Returns MAGIC | payload when a key is set. */
|
|
335
|
-
encryptBuffer(data) {
|
|
356
|
+
encryptBuffer(data, context) {
|
|
336
357
|
if (!this._key) return data;
|
|
337
358
|
if (this._mode === "xor") {
|
|
338
359
|
const masked = this._xorBuffer(data);
|
|
@@ -353,10 +374,11 @@ var CacheEncryption = class {
|
|
|
353
374
|
if (fin.length > 0) fin.copy(out2, MAGIC_LEN + CTR_IV_BYTES + enc2.length);
|
|
354
375
|
return out2;
|
|
355
376
|
}
|
|
356
|
-
const magic = this._mode === "aes-128-gcm" ? MAGIC_128 : MAGIC_256;
|
|
377
|
+
const magic = context === void 0 ? this._mode === "aes-128-gcm" ? MAGIC_128 : MAGIC_256 : this._mode === "aes-128-gcm" ? MAGIC_BOUND_128 : MAGIC_BOUND_256;
|
|
357
378
|
const algo = this._mode === "aes-128-gcm" ? "aes-128-gcm" : "aes-256-gcm";
|
|
358
379
|
const iv = this._nextIV();
|
|
359
380
|
const c = createCipheriv(algo, this._keyObj, iv);
|
|
381
|
+
if (context !== void 0) c.setAAD(Buffer.from(context, "utf8"));
|
|
360
382
|
const enc = c.update(data);
|
|
361
383
|
c.final();
|
|
362
384
|
const out = Buffer.allocUnsafe(MAGIC_LEN + IV_BYTES + TAG_BYTES + enc.length);
|
|
@@ -371,20 +393,21 @@ var CacheEncryption = class {
|
|
|
371
393
|
* Auto-detects the magic header; returns the original buffer if no magic matches (legacy/unencrypted).
|
|
372
394
|
* Falls back to `previousEncryptionKey` when the primary key fails (key rotation).
|
|
373
395
|
*/
|
|
374
|
-
decryptBuffer(data) {
|
|
396
|
+
decryptBuffer(data, context) {
|
|
397
|
+
this.assertReadPolicy(data, context);
|
|
375
398
|
try {
|
|
376
|
-
return this._decryptBufferWithKey(data, this._key, this._keyObj);
|
|
399
|
+
return this._decryptBufferWithKey(data, this._key, this._keyObj, context);
|
|
377
400
|
} catch (primaryErr) {
|
|
378
401
|
if (this._prevKey) {
|
|
379
402
|
try {
|
|
380
|
-
return this._decryptBufferWithKey(data, this._prevKey, this._prevKeyObj);
|
|
403
|
+
return this._decryptBufferWithKey(data, this._prevKey, this._prevKeyObj, context);
|
|
381
404
|
} catch {
|
|
382
405
|
}
|
|
383
406
|
}
|
|
384
407
|
throw primaryErr;
|
|
385
408
|
}
|
|
386
409
|
}
|
|
387
|
-
_decryptBufferWithKey(data, key, keyObj) {
|
|
410
|
+
_decryptBufferWithKey(data, key, keyObj, context) {
|
|
388
411
|
if (data.length < MAGIC_LEN) return data;
|
|
389
412
|
const magic = data.subarray(0, MAGIC_LEN);
|
|
390
413
|
if (magic.equals(MAGIC_XOR)) {
|
|
@@ -404,15 +427,18 @@ var CacheEncryption = class {
|
|
|
404
427
|
const fin = d2.final();
|
|
405
428
|
return fin.length > 0 ? Buffer.concat([plain2, fin]) : plain2;
|
|
406
429
|
}
|
|
407
|
-
const
|
|
408
|
-
const
|
|
430
|
+
const bound = magic.equals(MAGIC_BOUND_256) || magic.equals(MAGIC_BOUND_128);
|
|
431
|
+
const is256 = magic.equals(MAGIC_256) || magic.equals(MAGIC_BOUND_256);
|
|
432
|
+
const is128 = magic.equals(MAGIC_128) || magic.equals(MAGIC_BOUND_128);
|
|
409
433
|
if (!is256 && !is128) return data;
|
|
410
434
|
if (!keyObj) throw new Error("Cannot decrypt buffer: encryption key is not set");
|
|
435
|
+
if (bound && context === void 0) throw new Error("Encrypted buffer requires authenticated context");
|
|
411
436
|
const algo = is128 ? "aes-128-gcm" : "aes-256-gcm";
|
|
412
437
|
const iv = data.subarray(MAGIC_LEN, MAGIC_LEN + IV_BYTES);
|
|
413
438
|
const tag = data.subarray(MAGIC_LEN + IV_BYTES, MAGIC_LEN + IV_BYTES + TAG_BYTES);
|
|
414
439
|
const ct = data.subarray(MAGIC_LEN + IV_BYTES + TAG_BYTES);
|
|
415
440
|
const d = createDecipheriv(algo, keyObj, iv);
|
|
441
|
+
if (bound) d.setAAD(Buffer.from(context, "utf8"));
|
|
416
442
|
d.setAuthTag(tag);
|
|
417
443
|
const plain = d.update(ct);
|
|
418
444
|
d.final();
|
|
@@ -497,31 +523,109 @@ var EnvelopeEncryption = class {
|
|
|
497
523
|
|
|
498
524
|
// src/compression.ts
|
|
499
525
|
import zlib from "zlib";
|
|
500
|
-
import
|
|
501
|
-
|
|
526
|
+
import { createRequire } from "module";
|
|
527
|
+
|
|
528
|
+
// src/utils/lz4-size.ts
|
|
529
|
+
function lz4DecodedSize(source, maximum) {
|
|
530
|
+
const malformed = () => new Error("tricache: malformed or unsupported LZ4 frame");
|
|
531
|
+
const oversized = () => new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${maximum} bytes`);
|
|
532
|
+
if (source.length < 11 || source.readUInt32LE(0) !== 407708164) throw malformed();
|
|
533
|
+
const flags = source[4];
|
|
534
|
+
const descriptor = source[5];
|
|
535
|
+
const blockMaximum = [0, 0, 0, 0, 65536, 262144, 1048576, 4194304][descriptor >> 4];
|
|
536
|
+
if ((flags & 192) !== 64 || (flags & 19) !== 0 || (descriptor & 143) !== 0 || !blockMaximum) throw malformed();
|
|
537
|
+
let cursor = 6;
|
|
538
|
+
let declared;
|
|
539
|
+
if (flags & 8) {
|
|
540
|
+
if (cursor + 9 > source.length) throw malformed();
|
|
541
|
+
const size = source.readBigUInt64LE(cursor);
|
|
542
|
+
if (size > BigInt(maximum)) throw oversized();
|
|
543
|
+
declared = Number(size);
|
|
544
|
+
cursor += 8;
|
|
545
|
+
}
|
|
546
|
+
cursor++;
|
|
547
|
+
let total = 0;
|
|
548
|
+
const grow = (amount) => {
|
|
549
|
+
if (amount > maximum - total) throw oversized();
|
|
550
|
+
total += amount;
|
|
551
|
+
};
|
|
552
|
+
while (true) {
|
|
553
|
+
if (cursor + 4 > source.length) throw malformed();
|
|
554
|
+
const rawLength = source.readUInt32LE(cursor);
|
|
555
|
+
cursor += 4;
|
|
556
|
+
if (rawLength === 0) break;
|
|
557
|
+
const length = rawLength & 2147483647;
|
|
558
|
+
if (length > blockMaximum || length > source.length - cursor) throw malformed();
|
|
559
|
+
const end = cursor + length;
|
|
560
|
+
const blockStart = total;
|
|
561
|
+
if (rawLength & 2147483648) {
|
|
562
|
+
grow(length);
|
|
563
|
+
cursor = end;
|
|
564
|
+
} else {
|
|
565
|
+
const readLength = (initial) => {
|
|
566
|
+
let count = initial;
|
|
567
|
+
if (initial === 15) {
|
|
568
|
+
let extra;
|
|
569
|
+
do {
|
|
570
|
+
if (cursor >= end) throw malformed();
|
|
571
|
+
extra = source[cursor++];
|
|
572
|
+
count += extra;
|
|
573
|
+
if (count > maximum) throw oversized();
|
|
574
|
+
} while (extra === 255);
|
|
575
|
+
}
|
|
576
|
+
return count;
|
|
577
|
+
};
|
|
578
|
+
while (cursor < end) {
|
|
579
|
+
const token = source[cursor++];
|
|
580
|
+
const literals = readLength(token >>> 4);
|
|
581
|
+
if (literals > end - cursor) throw malformed();
|
|
582
|
+
grow(literals);
|
|
583
|
+
cursor += literals;
|
|
584
|
+
if (cursor === end) break;
|
|
585
|
+
if (cursor + 2 > end) throw malformed();
|
|
586
|
+
const offset = source.readUInt16LE(cursor);
|
|
587
|
+
cursor += 2;
|
|
588
|
+
const history = flags & 32 ? total - blockStart : total;
|
|
589
|
+
if (offset === 0 || offset > history) throw malformed();
|
|
590
|
+
grow(readLength(token & 15) + 4);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
if (total - blockStart > blockMaximum) throw malformed();
|
|
594
|
+
}
|
|
595
|
+
if (flags & 4) cursor += 4;
|
|
596
|
+
if (cursor !== source.length || declared !== void 0 && declared !== total) throw malformed();
|
|
597
|
+
return total;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// src/compression.ts
|
|
601
|
+
var requireCompression = createRequire(typeof __filename === "string" ? __filename : import.meta.url);
|
|
602
|
+
var lz4js;
|
|
603
|
+
function getLz4() {
|
|
604
|
+
return lz4js ??= requireCompression("lz4js");
|
|
605
|
+
}
|
|
502
606
|
var PREFIX_COMPRESSED = "cmp:v1:";
|
|
503
607
|
var PREFIX_ENC_COMPRESSED = "ecp:v1:";
|
|
504
608
|
var COMPRESSION_MAGIC = Buffer.from([84, 82, 67, 1]);
|
|
505
609
|
var zstdSimple = null;
|
|
610
|
+
var zstdGeneric = null;
|
|
506
611
|
var zstdInitPromise = null;
|
|
507
612
|
function initZstd() {
|
|
508
613
|
if (zstdSimple) return Promise.resolve(zstdSimple);
|
|
509
614
|
if (!zstdInitPromise) {
|
|
510
|
-
zstdInitPromise = new Promise((resolve) => {
|
|
615
|
+
zstdInitPromise = import("zstd-codec").then(({ ZstdCodec }) => new Promise((resolve) => {
|
|
511
616
|
try {
|
|
512
617
|
ZstdCodec.run((zstd) => {
|
|
513
618
|
zstdSimple = new zstd.Simple();
|
|
619
|
+
zstdGeneric = new zstd.Generic();
|
|
514
620
|
resolve(zstdSimple);
|
|
515
621
|
});
|
|
516
622
|
} catch {
|
|
517
623
|
resolve(null);
|
|
518
624
|
}
|
|
519
|
-
});
|
|
625
|
+
})).catch(() => null);
|
|
520
626
|
}
|
|
521
627
|
return zstdInitPromise;
|
|
522
628
|
}
|
|
523
|
-
initZstd().catch(() => {
|
|
524
|
-
});
|
|
525
629
|
function isCompressed(buf) {
|
|
526
630
|
return buf.length >= 4 && buf[0] === 84 && buf[1] === 82 && buf[2] === 67 && buf[3] === 1;
|
|
527
631
|
}
|
|
@@ -541,10 +645,11 @@ function compressBuffer(buf, algorithm, custom) {
|
|
|
541
645
|
return zlib.gzipSync(buf, { level: 6 });
|
|
542
646
|
}
|
|
543
647
|
if (algorithm === "lz4") {
|
|
544
|
-
return Buffer.from(
|
|
648
|
+
return Buffer.from(getLz4().compress(buf));
|
|
545
649
|
}
|
|
546
650
|
if (algorithm === "zstd") {
|
|
547
651
|
if (!zstdSimple) {
|
|
652
|
+
void initZstd();
|
|
548
653
|
return zlib.brotliCompressSync(buf, {
|
|
549
654
|
params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4 }
|
|
550
655
|
});
|
|
@@ -554,9 +659,24 @@ function compressBuffer(buf, algorithm, custom) {
|
|
|
554
659
|
return buf;
|
|
555
660
|
}
|
|
556
661
|
var MAX_DECOMPRESS_BYTES = 25 * 1024 * 1024;
|
|
662
|
+
function decompressLz4(buf) {
|
|
663
|
+
const size = lz4DecodedSize(buf, MAX_DECOMPRESS_BYTES);
|
|
664
|
+
const decoded = Buffer.from(getLz4().decompress(buf, size));
|
|
665
|
+
if (decoded.length !== size) throw new Error("tricache: corrupt LZ4 decoded size");
|
|
666
|
+
return decoded;
|
|
667
|
+
}
|
|
668
|
+
function decompressZstd(buf) {
|
|
669
|
+
const size = zstdGeneric?.contentSize(buf);
|
|
670
|
+
if (!Number.isSafeInteger(size) || size < 0) throw new Error("tricache: Zstandard frame requires a bounded content size");
|
|
671
|
+
if (size > MAX_DECOMPRESS_BYTES) throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
|
|
672
|
+
const decoded = zstdSimple.decompress(buf);
|
|
673
|
+
if (!decoded || decoded.length !== size) throw new Error("tricache: corrupt Zstandard decoded size");
|
|
674
|
+
return Buffer.from(decoded);
|
|
675
|
+
}
|
|
557
676
|
function decompressBuffer(buf, algorithm = "zstd", custom) {
|
|
677
|
+
if (buf.length > 32 * 1024 * 1024) throw new Error("tricache: compressed input exceeds maximum safety threshold");
|
|
558
678
|
if (custom) {
|
|
559
|
-
const res = custom.decompress(buf);
|
|
679
|
+
const res = custom.decompress(buf, MAX_DECOMPRESS_BYTES);
|
|
560
680
|
if (res.length > MAX_DECOMPRESS_BYTES) {
|
|
561
681
|
throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
|
|
562
682
|
}
|
|
@@ -571,7 +691,7 @@ function decompressBuffer(buf, algorithm = "zstd", custom) {
|
|
|
571
691
|
if (buf.length >= 4 && buf[0] === 40 && buf[1] === 181 && buf[2] === 47 && buf[3] === 253) {
|
|
572
692
|
if (zstdSimple) {
|
|
573
693
|
try {
|
|
574
|
-
return checkSafety(
|
|
694
|
+
return checkSafety(decompressZstd(buf));
|
|
575
695
|
} catch (err) {
|
|
576
696
|
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
577
697
|
}
|
|
@@ -579,7 +699,7 @@ function decompressBuffer(buf, algorithm = "zstd", custom) {
|
|
|
579
699
|
}
|
|
580
700
|
if (buf.length >= 4 && buf[0] === 4 && buf[1] === 34 && buf[2] === 77 && buf[3] === 24) {
|
|
581
701
|
try {
|
|
582
|
-
return checkSafety(
|
|
702
|
+
return checkSafety(decompressLz4(buf));
|
|
583
703
|
} catch (err) {
|
|
584
704
|
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
585
705
|
}
|
|
@@ -598,14 +718,14 @@ function decompressBuffer(buf, algorithm = "zstd", custom) {
|
|
|
598
718
|
}
|
|
599
719
|
if (algorithm === "lz4") {
|
|
600
720
|
try {
|
|
601
|
-
return checkSafety(
|
|
721
|
+
return checkSafety(decompressLz4(buf));
|
|
602
722
|
} catch (err) {
|
|
603
723
|
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
604
724
|
}
|
|
605
725
|
}
|
|
606
726
|
if (algorithm === "zstd" && zstdSimple) {
|
|
607
727
|
try {
|
|
608
|
-
return checkSafety(
|
|
728
|
+
return checkSafety(decompressZstd(buf));
|
|
609
729
|
} catch (err) {
|
|
610
730
|
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
611
731
|
}
|
|
@@ -632,7 +752,7 @@ function decompressBuffer(buf, algorithm = "zstd", custom) {
|
|
|
632
752
|
};
|
|
633
753
|
const tryLz4 = () => {
|
|
634
754
|
try {
|
|
635
|
-
return checkSafety(
|
|
755
|
+
return checkSafety(decompressLz4(buf));
|
|
636
756
|
} catch (err) {
|
|
637
757
|
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
638
758
|
return null;
|
|
@@ -641,7 +761,7 @@ function decompressBuffer(buf, algorithm = "zstd", custom) {
|
|
|
641
761
|
const tryZstd = () => {
|
|
642
762
|
if (!zstdSimple) return null;
|
|
643
763
|
try {
|
|
644
|
-
return checkSafety(
|
|
764
|
+
return checkSafety(decompressZstd(buf));
|
|
645
765
|
} catch (err) {
|
|
646
766
|
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
647
767
|
return null;
|
|
@@ -47,6 +47,7 @@ var WasmBloomFilter = class {
|
|
|
47
47
|
exports;
|
|
48
48
|
mem;
|
|
49
49
|
stagingTarget;
|
|
50
|
+
stagingView;
|
|
50
51
|
encoder = new TextEncoder();
|
|
51
52
|
numBits = 1e5;
|
|
52
53
|
/** Pre-computed — k and numBits are fixed constants; no need to recalculate on every probe. */
|
|
@@ -58,12 +59,24 @@ var WasmBloomFilter = class {
|
|
|
58
59
|
this.exports = instance.exports;
|
|
59
60
|
this.mem = new Uint8Array(this.exports.memory.buffer);
|
|
60
61
|
this.stagingTarget = new Uint8Array(this.exports.memory.buffer, KEY_STAGING_OFFSET, MAX_KEY_BYTES);
|
|
62
|
+
this.stagingView = new DataView(this.exports.memory.buffer, KEY_STAGING_OFFSET, MAX_KEY_BYTES);
|
|
61
63
|
const k = 7, p = 0.01;
|
|
62
64
|
this._maxCapacity = Math.floor(-this.numBits * Math.log(1 - Math.pow(p, 1 / k)) / k);
|
|
63
65
|
}
|
|
64
66
|
writeKey(key) {
|
|
65
67
|
if (key.length === 0) return 0;
|
|
66
|
-
|
|
68
|
+
const encoded = this.encoder.encodeInto(key, this.stagingTarget);
|
|
69
|
+
if (encoded.read === key.length) return encoded.written;
|
|
70
|
+
let first = 2166136261, second = 5381;
|
|
71
|
+
for (let index = 0; index < key.length; index++) {
|
|
72
|
+
const unit = key.charCodeAt(index);
|
|
73
|
+
first = Math.imul(first ^ unit, 16777619) >>> 0;
|
|
74
|
+
second = (Math.imul(second, 33) ^ unit) >>> 0;
|
|
75
|
+
}
|
|
76
|
+
this.stagingTarget[0] = 255;
|
|
77
|
+
this.stagingView.setUint32(1, first, true);
|
|
78
|
+
this.stagingView.setUint32(5, second, true);
|
|
79
|
+
return 9;
|
|
67
80
|
}
|
|
68
81
|
add(key) {
|
|
69
82
|
const len = this.writeKey(key);
|
|
@@ -102,6 +115,33 @@ var WasmBloomFilter = class {
|
|
|
102
115
|
}
|
|
103
116
|
};
|
|
104
117
|
|
|
118
|
+
// src/utils/namespace-key.ts
|
|
119
|
+
var NAMESPACE_KEY_VERSION = "tricache:v3:";
|
|
120
|
+
function encodeNamespaceSegment(namespace) {
|
|
121
|
+
let encoded = "";
|
|
122
|
+
for (let index = 0; index < namespace.length; index++) {
|
|
123
|
+
encoded += namespace.charCodeAt(index).toString(16).padStart(4, "0");
|
|
124
|
+
}
|
|
125
|
+
return encoded;
|
|
126
|
+
}
|
|
127
|
+
function namespaceKeyPrefix(namespace) {
|
|
128
|
+
return `${NAMESPACE_KEY_VERSION}${encodeNamespaceSegment(namespace)}:`;
|
|
129
|
+
}
|
|
130
|
+
function namespaceDomainPrefix(namespace, domain) {
|
|
131
|
+
return `${namespaceKeyPrefix(namespace)}${domain}:`;
|
|
132
|
+
}
|
|
133
|
+
function namespaceStreamKey(namespace, customBase) {
|
|
134
|
+
const namespaceTag = `ns-${encodeNamespaceSegment(namespace)}`;
|
|
135
|
+
const streamName = customBase === void 0 ? "default" : `custom-${encodeNamespaceSegment(customBase)}`;
|
|
136
|
+
return `${NAMESPACE_KEY_VERSION}stream:{${namespaceTag}}:${streamName}`;
|
|
137
|
+
}
|
|
138
|
+
function namespaceBackplaneChannel(namespace) {
|
|
139
|
+
return `${NAMESPACE_KEY_VERSION}inv:${encodeNamespaceSegment(namespace)}`;
|
|
140
|
+
}
|
|
141
|
+
function namespaceSingletonKey(namespace) {
|
|
142
|
+
return `__tricache_v3_ns_${encodeNamespaceSegment(namespace)}__`;
|
|
143
|
+
}
|
|
144
|
+
|
|
105
145
|
// src/utils/tag-matcher.ts
|
|
106
146
|
var globRegexCache = /* @__PURE__ */ new Map();
|
|
107
147
|
function isWildcardTag(tag) {
|
|
@@ -157,6 +197,12 @@ export {
|
|
|
157
197
|
utf8ToUint8Array,
|
|
158
198
|
uint8ArrayToUtf8,
|
|
159
199
|
WasmBloomFilter,
|
|
200
|
+
encodeNamespaceSegment,
|
|
201
|
+
namespaceKeyPrefix,
|
|
202
|
+
namespaceDomainPrefix,
|
|
203
|
+
namespaceStreamKey,
|
|
204
|
+
namespaceBackplaneChannel,
|
|
205
|
+
namespaceSingletonKey,
|
|
160
206
|
isWildcardTag,
|
|
161
207
|
matchesTagPattern,
|
|
162
208
|
isChildTag,
|