Files
worldmonitor/api/opensky.js
Elie Habib 0102428bb0 fix: reduce upstream API pressure with cache TTL optimization (#430)
* fix(sentry): tighten noise filters for deck.gl/maplibre and WebView errors

- Widen beforeSend regex to catch `null is not an object (evaluating 'u.id')`
  pattern from deck.gl internals during variant switch (WORLDMONITOR-4A, 270 events)
- Remove `in_app` requirement from TypeError suppression — Sentry SDK marks
  deck.gl/maplibre frames inconsistently, causing the filter to miss
- Fix Firefox lexical declaration wording: `can't access` vs Chrome's `Cannot access`
- Add noise filters: isReCreate (Android WebView injection), HTMLImageElement
  style access, WebGL context loss write access

* fix: reduce upstream API pressure with cache TTL optimization

- Military/posture: 5min → 15min (flight cache, theater posture, panel refresh, intelligence refresh)
- Theater posture: fetch 2 targeted bbox regions instead of global states/all (~95% less data)
- Wingbits batch: reduce from 20 to 10, sequential with 100ms delay instead of Promise.all burst
- Preserve intelligenceCache.military across intelligence refresh cycles
- OpenSky edge proxy: add CDN caching (s-maxage=120), align timeout to 20s
- list-military-flights: Redis cache 2min → 10min
- Market handlers: stablecoins/crypto/commodities/sectors 3min → 5min
- Cable health: 3min → 10min
- YouTube embed: s-maxage 60s → 15min
2026-02-26 21:24:32 +04:00

91 lines
2.9 KiB
JavaScript

import { getCorsHeaders, isDisallowedOrigin } from './_cors.js';
export const config = { runtime: 'edge' };
function getRelayBaseUrl() {
const relayUrl = process.env.WS_RELAY_URL;
if (!relayUrl) return null;
return relayUrl.replace('wss://', 'https://').replace('ws://', 'http://').replace(/\/$/, '');
}
function getRelayHeaders(baseHeaders = {}) {
const headers = { ...baseHeaders };
const relaySecret = process.env.RELAY_SHARED_SECRET || '';
if (relaySecret) {
const relayHeader = (process.env.RELAY_AUTH_HEADER || 'x-relay-key').toLowerCase();
headers[relayHeader] = relaySecret;
headers.Authorization = `Bearer ${relaySecret}`;
}
return headers;
}
async function fetchWithTimeout(url, options, timeoutMs = 20000) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
try {
return await fetch(url, { ...options, signal: controller.signal });
} finally {
clearTimeout(timeout);
}
}
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 requestUrl = new URL(req.url);
const relayUrl = `${relayBaseUrl}/opensky${requestUrl.search || ''}`;
const response = await fetchWithTimeout(relayUrl, {
headers: getRelayHeaders({ Accept: 'application/json' }),
});
const body = await response.text();
const headers = {
'Content-Type': response.headers.get('content-type') || 'application/json',
'Cache-Control': 'public, s-maxage=120, stale-while-revalidate=60',
...corsHeaders,
};
const xCache = response.headers.get('x-cache');
if (xCache) headers['X-Cache'] = xCache;
return new Response(body, {
status: response.status,
headers,
});
} 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', ...corsHeaders },
});
}
}