Files
worldmonitor/api/telegram-feed.js
Elie Habib 554bbadaf1 fix(hydration): guard all bootstrap consumers against empty CDN-cached data (#1015)
When CDN serves stale bootstrap responses with empty arrays (e.g.,
{anomalies:[]}), the hydration check `if (hydrated)` is truthy, causing
panels to skip the RPC fallback and show empty data. Fixed all 12
vulnerable getHydratedData consumers to verify the payload has actual
data before trusting it.

Also rewrote api/telegram-feed.js to inspect response body — empty
responses get short CDN TTL (15s) to prevent prolonged staleness.
2026-03-04 22:35:22 +04:00

77 lines
2.7 KiB
JavaScript

import { getRelayBaseUrl, getRelayHeaders, fetchWithTimeout } from './_relay.js';
import { getCorsHeaders, isDisallowedOrigin } from './_cors.js';
export const config = { runtime: 'edge' };
export default async function handler(req) {
const corsHeaders = getCorsHeaders(req, 'GET, OPTIONS');
if (isDisallowedOrigin(req)) {
return new Response(JSON.stringify({ error: 'Origin not allowed' }), {
status: 403,
headers: { 'Content-Type': 'application/json', ...corsHeaders },
});
}
if (req.method === 'OPTIONS') {
return new Response(null, { status: 204, headers: corsHeaders });
}
if (req.method !== 'GET') {
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
status: 405,
headers: { 'Content-Type': 'application/json', ...corsHeaders },
});
}
const relayBaseUrl = getRelayBaseUrl();
if (!relayBaseUrl) {
return new Response(JSON.stringify({ error: 'WS_RELAY_URL is not configured' }), {
status: 503,
headers: { 'Content-Type': 'application/json', ...corsHeaders },
});
}
try {
const url = new URL(req.url);
const limit = Math.max(1, Math.min(200, parseInt(url.searchParams.get('limit') || '50', 10) || 50));
const topic = (url.searchParams.get('topic') || '').trim();
const channel = (url.searchParams.get('channel') || '').trim();
const params = new URLSearchParams();
params.set('limit', String(limit));
if (topic) params.set('topic', encodeURIComponent(topic));
if (channel) params.set('channel', encodeURIComponent(channel));
const relayUrl = `${relayBaseUrl}/telegram/feed?${params}`;
const response = await fetchWithTimeout(relayUrl, {
headers: getRelayHeaders({ Accept: 'application/json' }),
}, 15000);
const body = await response.text();
let cacheControl = 'public, max-age=30, s-maxage=120, stale-while-revalidate=60, stale-if-error=120';
try {
const parsed = JSON.parse(body);
if (!parsed || parsed.count === 0 || !parsed.items || parsed.items.length === 0) {
cacheControl = 'public, max-age=0, s-maxage=15, stale-while-revalidate=10';
}
} catch {}
return new Response(body, {
status: response.status,
headers: {
'Content-Type': response.headers.get('content-type') || 'application/json',
'Cache-Control': cacheControl,
...corsHeaders,
},
});
} catch (error) {
const isTimeout = error?.name === 'AbortError';
return new Response(JSON.stringify({
error: isTimeout ? 'Relay timeout' : 'Relay request failed',
details: error?.message || String(error),
}), {
status: isTimeout ? 504 : 502,
headers: { 'Content-Type': 'application/json', 'Cache-Control': 'no-store', ...corsHeaders },
});
}
}