tricache 0.6.4 → 0.6.6
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 +27 -13
- package/dist/{chunk-F24NEZGQ.js → chunk-FKY5R3CE.js} +17 -13
- package/dist/index.cjs +264 -77
- package/dist/index.d.ts +84 -11
- package/dist/index.js +249 -74
- package/dist/serialize-worker.cjs +17 -5
- package/dist/serialize-worker.js +1 -1
- package/package.json +13 -13
- package/src/cache-service.ts +227 -21
- package/src/disk-tier.ts +31 -44
- package/src/encryption.ts +23 -4
- package/src/smart-memory-cache.ts +19 -3
- package/src/types.ts +29 -0
- package/src/wasm/bloom-filter-wasm.ts +2 -2
- package/src/worker-pool.ts +3 -8
- package/dist/index.d.cts +0 -1570
- package/dist/serialize-worker.d.cts +0 -2
package/dist/index.d.ts
CHANGED
|
@@ -156,6 +156,13 @@ interface CacheMetrics {
|
|
|
156
156
|
revalidations: {
|
|
157
157
|
total: number;
|
|
158
158
|
};
|
|
159
|
+
/** Rate-limit / singleton diagnostics */
|
|
160
|
+
counters: {
|
|
161
|
+
/** Times `increment()` hit a Redis error (fail-open by default, or fail-closed re-throw). */
|
|
162
|
+
errors: number;
|
|
163
|
+
/** Times a later `create(ns)` call passed options diverging from the live singleton. */
|
|
164
|
+
singletonDivergences: number;
|
|
165
|
+
};
|
|
159
166
|
bloom: {
|
|
160
167
|
/** Total bloom-filter checks (get() calls that passed prefix filter) */
|
|
161
168
|
checksTotal: number;
|
|
@@ -636,6 +643,25 @@ interface CacheOptions {
|
|
|
636
643
|
port: number;
|
|
637
644
|
}>;
|
|
638
645
|
};
|
|
646
|
+
/**
|
|
647
|
+
* When `true`, a second `CacheService.create()` call for an already-existing
|
|
648
|
+
* singleton (same namespace) whose options differ from the first call throws
|
|
649
|
+
* instead of silently returning the existing instance. Use in codebases where
|
|
650
|
+
* multiple init points could pass conflicting configuration (e.g. different
|
|
651
|
+
* Redis credentials or TTLs) and you want that class of bug to fail loudly.
|
|
652
|
+
*
|
|
653
|
+
* Default: `false` (warn once, return existing instance).
|
|
654
|
+
*/
|
|
655
|
+
strictSingleton?: boolean;
|
|
656
|
+
/**
|
|
657
|
+
* `cache.increment()` distributed-rate-limit behaviour on a Redis error.
|
|
658
|
+
*
|
|
659
|
+
* Default: `false` (fail-open — on a Redis error the method returns `0`, so a
|
|
660
|
+
* caller's `if (count > LIMIT) reject()` guard never rejects during an outage).
|
|
661
|
+
* Set to `true` to instead re-throw the Redis error, letting the caller's limit
|
|
662
|
+
* guard enforce the limit even when Redis is unavailable.
|
|
663
|
+
*/
|
|
664
|
+
failClosed?: boolean;
|
|
639
665
|
}
|
|
640
666
|
|
|
641
667
|
/**
|
|
@@ -697,17 +723,34 @@ declare class CacheService {
|
|
|
697
723
|
/** Timestamp (Date.now()) when the backplane subscriber last lost its connection. */
|
|
698
724
|
private _subDisconnectedAt;
|
|
699
725
|
private readonly counters;
|
|
726
|
+
/** One-time guard so the in-process increment() fallback warning fires only once. */
|
|
727
|
+
private _incrementFallbackWarned;
|
|
700
728
|
constructor(options?: CacheOptions);
|
|
701
729
|
/**
|
|
702
730
|
* Get (or create) the process-level singleton CacheService instance.
|
|
703
731
|
*
|
|
704
732
|
* Options are only applied on first call — subsequent calls return the
|
|
705
|
-
* existing instance regardless of options passed.
|
|
733
|
+
* existing instance regardless of options passed. When the new options differ
|
|
734
|
+
* from what the existing singleton was built with, this is a silent no-op by
|
|
735
|
+
* default; set `strictSingleton: true` to throw instead, or watch the
|
|
736
|
+
* `singletonDivergences` metric for detection.
|
|
706
737
|
*
|
|
707
738
|
* When `namespace` is set the singleton is keyed by namespace, so two calls
|
|
708
739
|
* with different namespaces return independent instances.
|
|
709
740
|
*/
|
|
710
741
|
static create(options?: CacheOptions): CacheService;
|
|
742
|
+
/**
|
|
743
|
+
* Keys that, if set differently on a later `create()` call, represent a
|
|
744
|
+
* meaningful configuration divergence worth flagging (security/correctness
|
|
745
|
+
* relevant — not cosmetic, e.g. log level).
|
|
746
|
+
*/
|
|
747
|
+
private static readonly DIVERGENT_KEYS;
|
|
748
|
+
/** Returns the names of options that differ between `a` and the live `b`. */
|
|
749
|
+
private static optionsDiff;
|
|
750
|
+
/** Increment the singleton-divergence counter (surfaced in `metrics()`). */
|
|
751
|
+
bumpSingletonDivergence(): void;
|
|
752
|
+
/** Read-only view of the options the singleton was constructed with. */
|
|
753
|
+
get options(): CacheOptions;
|
|
711
754
|
/**
|
|
712
755
|
* Async factory — accepts a Promise that resolves to CacheOptions.
|
|
713
756
|
* Useful when config is fetched from a secret store at startup.
|
|
@@ -728,6 +771,8 @@ declare class CacheService {
|
|
|
728
771
|
* receive and provide un-prefixed keys in the public API.
|
|
729
772
|
*/
|
|
730
773
|
private nk;
|
|
774
|
+
/** Strip the instance namespace prefix from a namespaced key (inverse of `nk`). */
|
|
775
|
+
private unnk;
|
|
731
776
|
/** Apply TTL jitter: multiply ttlMs by (1 ± jitterFactor). */
|
|
732
777
|
private _jitterTtl;
|
|
733
778
|
/** Shared no-op span for the common case where no tracer is configured.
|
|
@@ -800,8 +845,20 @@ declare class CacheService {
|
|
|
800
845
|
*/
|
|
801
846
|
delete(cacheKey: string): Promise<void>;
|
|
802
847
|
/**
|
|
803
|
-
*
|
|
804
|
-
*
|
|
848
|
+
* Distributed counter.
|
|
849
|
+
*
|
|
850
|
+
* When Redis is active, atomically increments the key via `INCR` and returns
|
|
851
|
+
* the new (fleet-wide) value. On a Redis error it increments `counters.errors`
|
|
852
|
+
* and, by default, fails OPEN — returning `0` so a caller's
|
|
853
|
+
* `if (count > LIMIT) reject()` guard does NOT reject during an outage. Set
|
|
854
|
+
* `failClosed: true` to re-throw the error instead, enforcing the limit even
|
|
855
|
+
* when Redis is unavailable.
|
|
856
|
+
*
|
|
857
|
+
* When Redis is disabled (or `disableRedis: true`), this maintains an
|
|
858
|
+
* IN-PROCESS counter with the same TTL semantics so dev/test rate-limiting
|
|
859
|
+
* works locally. The returned value is the local count (1, 2, 3, …), NOT 0 —
|
|
860
|
+
* a one-time warning is logged because rate-limiting is then per-instance, not
|
|
861
|
+
* fleet-wide.
|
|
805
862
|
*/
|
|
806
863
|
increment(cacheKey: string, ttlSeconds?: number): Promise<number>;
|
|
807
864
|
/**
|
|
@@ -1134,6 +1191,18 @@ declare class CacheEncryption {
|
|
|
1134
1191
|
private _xorBuffer;
|
|
1135
1192
|
constructor(keyBase64: string | undefined, logger: ILogger, mode?: EncryptionMode, previousKeyBase64?: string, previousMode?: EncryptionMode);
|
|
1136
1193
|
get isEnabled(): boolean;
|
|
1194
|
+
/**
|
|
1195
|
+
* Snapshot the key material needed to spin up an off-main-thread worker pool.
|
|
1196
|
+
* Replaces the previously-used `as unknown as { _key, _mode, … }` private-field
|
|
1197
|
+
* cast in cache-service.ts — the worker pool reads these via this accessor
|
|
1198
|
+
* instead of reaching into underscore-prefixed internals.
|
|
1199
|
+
*/
|
|
1200
|
+
toWorkerInit(): {
|
|
1201
|
+
keyBase64: string;
|
|
1202
|
+
mode: EncryptionMode;
|
|
1203
|
+
prevKeyBase64: string | undefined;
|
|
1204
|
+
prevMode: EncryptionMode;
|
|
1205
|
+
};
|
|
1137
1206
|
/**
|
|
1138
1207
|
* Encrypt a string for Redis storage.
|
|
1139
1208
|
* Returns a mode-prefixed base64 string when a key is configured;
|
|
@@ -1409,7 +1478,10 @@ interface DiskTierOptions {
|
|
|
1409
1478
|
maxBytes: number;
|
|
1410
1479
|
entryMaxBytes: number;
|
|
1411
1480
|
forbiddenPrefixes: readonly string[];
|
|
1412
|
-
|
|
1481
|
+
/** Mode-aware encryption instance. When set, disk files are encrypted with the
|
|
1482
|
+
* same algorithm as L2/Redis (aes-256-gcm, aes-128-gcm, aes-128-ctr, or xor).
|
|
1483
|
+
* When null, files are written unencrypted. Pass `cache.encryption` here. */
|
|
1484
|
+
encryption?: CacheEncryption | null;
|
|
1413
1485
|
logger: ILogger;
|
|
1414
1486
|
}
|
|
1415
1487
|
declare class DiskTier {
|
|
@@ -1477,15 +1549,16 @@ declare class DiskTier {
|
|
|
1477
1549
|
private encrypt;
|
|
1478
1550
|
private decrypt;
|
|
1479
1551
|
/**
|
|
1480
|
-
* V2 encryption — returns
|
|
1481
|
-
* the outer 16-byte header
|
|
1482
|
-
* Returns the buffer unchanged when no encryption
|
|
1552
|
+
* V2 encryption — returns the mode-aware encrypted payload (no magic prefix; the
|
|
1553
|
+
* magic + plaintext expiresAt live in the outer 16-byte header written by `save`).
|
|
1554
|
+
* Returns the buffer unchanged when no encryption instance is configured.
|
|
1483
1555
|
*/
|
|
1484
1556
|
private encryptV2;
|
|
1485
1557
|
/**
|
|
1486
1558
|
* V2 decryption — inner payload after stripping the 16-byte header.
|
|
1487
|
-
* When
|
|
1488
|
-
*
|
|
1559
|
+
* When an encryption instance is configured the format is the mode-aware
|
|
1560
|
+
* CacheEncryption blob (IV|TAG|CT for GCM, IV|CT for CTR, key⊕data for XOR);
|
|
1561
|
+
* without one the payload is raw msgpack.
|
|
1489
1562
|
*/
|
|
1490
1563
|
private decryptV2;
|
|
1491
1564
|
/**
|
|
@@ -1561,8 +1634,8 @@ declare class WasmBloomFilter {
|
|
|
1561
1634
|
get insertions(): number;
|
|
1562
1635
|
/**
|
|
1563
1636
|
* Maximum safe insertions before false-positive rate exceeds ~1 %.
|
|
1564
|
-
* Exact formula for k=7 hash rounds, m=100 000 bits
|
|
1565
|
-
* n_max = -m * ln(1 - p^(1/k)) / k ≈
|
|
1637
|
+
* Exact formula for k=7 hash rounds, m=100 000 bits, p=1%:
|
|
1638
|
+
* n_max = -m * ln(1 - p^(1/k)) / k ≈ 10 434
|
|
1566
1639
|
*/
|
|
1567
1640
|
get maxCapacity(): number;
|
|
1568
1641
|
}
|