Flash unwinder contracts and scripts, relay lane tuning, trustless bridge and token-aggregation updates.
Made-with: Cursor
This commit is contained in:
@@ -8,6 +8,17 @@ interface CacheEntry {
|
||||
const cache = new Map<string, CacheEntry>();
|
||||
const DEFAULT_TTL = 60 * 1000; // 1 minute
|
||||
|
||||
/** Never cache generic API error envelopes (avoids poisoning cache if status/body ever disagree). */
|
||||
function looksLikeGenericErrorPayload(body: unknown): boolean {
|
||||
if (body == null || typeof body !== 'object' || Array.isArray(body)) return false;
|
||||
const o = body as Record<string, unknown>;
|
||||
if (typeof o.error !== 'string') return false;
|
||||
// Success shapes we must not treat as errors
|
||||
if ('pools' in o || 'tokens' in o || 'data' in o || 'chains' in o || 'tree' in o || 'quote' in o) return false;
|
||||
if (o.status === 'healthy') return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
export function cacheMiddleware(ttl: number = DEFAULT_TTL) {
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
const bypassCache =
|
||||
@@ -29,7 +40,9 @@ export function cacheMiddleware(ttl: number = DEFAULT_TTL) {
|
||||
|
||||
// Override json method to cache response
|
||||
res.json = function (body: unknown) {
|
||||
if (!bypassCache) {
|
||||
// Only cache successful payloads. Otherwise a 500 body gets replayed on cache hit with HTTP 200.
|
||||
const okStatus = res.statusCode >= 200 && res.statusCode < 300;
|
||||
if (!bypassCache && okStatus && !looksLikeGenericErrorPayload(body)) {
|
||||
cache.set(key, {
|
||||
data: body,
|
||||
expiresAt: Date.now() + ttl,
|
||||
|
||||
Reference in New Issue
Block a user