mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 04:12:10 +03:00
5 new test files covering all 13 changed production files: - estimateSizeFast.test.ts: 16 tests for fast size estimator (circular ref protection, early exit, nested structures, Map safety) - eviction-guards-apiKeyRotator.test.ts: 5 tests for Map eviction guards (!has() check prevents evicting existing keys on update) - eviction-guards-codexQuotaFetcher.test.ts: 4 tests for connectionRegistry and quotaCache eviction guards - rateLimitManager-idle-eviction.test.ts: 6 tests for idle limiter cleanup, limiterLastUsed tracking, and shutdown behavior - registry-direct-exports.test.ts: 20 tests verifying all 8 registries export plain objects (no Proxy traps, no lazy getters, mutable entries) Extract estimateSizeFast/isSmallEnoughForSemanticCache into standalone open-sse/utils/estimateSize.ts to make them testable without importing the entire chatCore.ts dependency tree.
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
/**
|
|
* Fast object-tree size estimator — walks without JSON.stringify.
|
|
* Safe for circular references (uses WeakSet).
|
|
* Early-exits at 256KB to avoid wasting CPU on huge payloads.
|
|
*/
|
|
export function estimateSizeFast(value: unknown): number {
|
|
let bytes = 0;
|
|
const stack: unknown[] = [value];
|
|
const seen = new WeakSet();
|
|
while (stack.length > 0) {
|
|
const v = stack.pop();
|
|
if (v === null || v === undefined) continue;
|
|
if (typeof v === "string") {
|
|
bytes += v.length;
|
|
if (bytes > 262144) return bytes;
|
|
} else if (typeof v === "number") bytes += 8;
|
|
else if (typeof v === "boolean") bytes += 4;
|
|
else if (typeof v === "object") {
|
|
if (seen.has(v as object)) continue;
|
|
seen.add(v as object);
|
|
if (Array.isArray(v)) {
|
|
for (let i = 0; i < v.length; i++) stack.push(v[i]);
|
|
} else {
|
|
for (const key in v) {
|
|
if (Object.prototype.hasOwnProperty.call(v, key)) stack.push((v as Record<string, unknown>)[key]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
export function isSmallEnoughForSemanticCache(value: unknown): boolean {
|
|
return estimateSizeFast(value) <= 256 * 1024;
|
|
}
|