Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 62 additions & 12 deletions README.md

Large diffs are not rendered by default.

60 changes: 0 additions & 60 deletions docs/NEWS_TRANSLATION_ANALYSIS.md

This file was deleted.

16 changes: 10 additions & 6 deletions server/_shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ export const CHROME_UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleW
*/
let yahooLastRequest = 0;
const YAHOO_MIN_GAP_MS = 600;
let yahooQueue: Promise<void> = Promise.resolve();

export async function yahooGate(): Promise<void> {
const elapsed = Date.now() - yahooLastRequest;
if (elapsed < YAHOO_MIN_GAP_MS) {
await new Promise<void>(r => setTimeout(r, YAHOO_MIN_GAP_MS - elapsed));
}
yahooLastRequest = Date.now();
export function yahooGate(): Promise<void> {
yahooQueue = yahooQueue.then(async () => {
const elapsed = Date.now() - yahooLastRequest;
if (elapsed < YAHOO_MIN_GAP_MS) {
await new Promise<void>(r => setTimeout(r, YAHOO_MIN_GAP_MS - elapsed));
}
yahooLastRequest = Date.now();
});
return yahooQueue;
}
17 changes: 15 additions & 2 deletions server/worldmonitor/aviation/v1/list-airport-delays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,26 @@ import {
determineSeverity,
generateSimulatedDelay,
} from './_shared';
import { CHROME_UA } from '../../../_shared/constants';
import { getCachedJson, setCachedJson } from '../../../_shared/redis';

const REDIS_CACHE_KEY = 'aviation:delays:v1';
const REDIS_CACHE_TTL = 1800; // 30 min — FAA updates infrequently

export async function listAirportDelays(
_ctx: ServerContext,
_req: ListAirportDelaysRequest,
): Promise<ListAirportDelaysResponse> {
try {
// Redis shared cache
const cached = (await getCachedJson(REDIS_CACHE_KEY)) as ListAirportDelaysResponse | null;
if (cached?.alerts?.length) return cached;

const alerts: AirportDelayAlert[] = [];

// 1. Fetch and parse FAA XML
const faaResponse = await fetch(FAA_URL, {
headers: { Accept: 'application/xml' },
headers: { Accept: 'application/xml', 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(15_000),
});

Expand Down Expand Up @@ -76,7 +85,11 @@ export async function listAirportDelays(
}
}

return { alerts };
const result: ListAirportDelaysResponse = { alerts };
if (alerts.length > 0) {
setCachedJson(REDIS_CACHE_KEY, result, REDIS_CACHE_TTL).catch(() => {});
}
return result;
} catch {
// Graceful empty response on ANY failure (established pattern from 2F-01)
return { alerts: [] };
Expand Down
18 changes: 16 additions & 2 deletions server/worldmonitor/climate/v1/list-climate-anomalies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import type {
ClimateAnomaly,
} from '../../../../src/generated/server/worldmonitor/climate/v1/service_server';

import { CHROME_UA } from '../../../_shared/constants';
import { getCachedJson, setCachedJson } from '../../../_shared/redis';

const REDIS_CACHE_KEY = 'climate:anomalies:v1';
const REDIS_CACHE_TTL = 1800; // 30 min — daily archive data, slow-moving

/** The 15 monitored zones matching the legacy api/climate-anomalies.js list. */
const ZONES: { name: string; lat: number; lon: number }[] = [
{ name: 'Ukraine', lat: 48.4, lon: 31.2 },
Expand Down Expand Up @@ -85,7 +91,7 @@ async function fetchZone(
): Promise<ClimateAnomaly | null> {
const url = `https://archive-api.open-meteo.com/v1/archive?latitude=${zone.lat}&longitude=${zone.lon}&start_date=${startDate}&end_date=${endDate}&daily=temperature_2m_mean,precipitation_sum&timezone=UTC`;

const response = await fetch(url, { signal: AbortSignal.timeout(20_000) });
const response = await fetch(url, { headers: { 'User-Agent': CHROME_UA }, signal: AbortSignal.timeout(20_000) });
if (!response.ok) {
throw new Error(`Open-Meteo ${response.status} for ${zone.name}`);
}
Expand Down Expand Up @@ -133,6 +139,10 @@ export const listClimateAnomalies: ClimateServiceHandler['listClimateAnomalies']
_ctx: ServerContext,
_req: ListClimateAnomaliesRequest,
): Promise<ListClimateAnomaliesResponse> => {
// Redis shared cache
const cached = (await getCachedJson(REDIS_CACHE_KEY)) as ListClimateAnomaliesResponse | null;
if (cached?.anomalies?.length) return cached;

// Compute 30-day date range
const endDate = new Date().toISOString().slice(0, 10);
const startDate = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)
Expand All @@ -156,5 +166,9 @@ export const listClimateAnomalies: ClimateServiceHandler['listClimateAnomalies']
}
}

return { anomalies, pagination: undefined };
const result: ListClimateAnomaliesResponse = { anomalies, pagination: undefined };
if (anomalies.length > 0) {
setCachedJson(REDIS_CACHE_KEY, result, REDIS_CACHE_TTL).catch(() => {});
}
return result;
};
18 changes: 16 additions & 2 deletions server/worldmonitor/conflict/v1/get-humanitarian-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import type {
HumanitarianCountrySummary,
} from '../../../../src/generated/server/worldmonitor/conflict/v1/service_server';

import { CHROME_UA } from '../../../_shared/constants';
import { getCachedJson, setCachedJson } from '../../../_shared/redis';

const REDIS_CACHE_KEY = 'conflict:humanitarian:v1';
const REDIS_CACHE_TTL = 21600; // 6 hr — monthly humanitarian data

const ISO2_TO_ISO3: Record<string, string> = {
US: 'USA', RU: 'RUS', CN: 'CHN', UA: 'UKR', IR: 'IRN',
IL: 'ISR', TW: 'TWN', KP: 'PRK', SA: 'SAU', TR: 'TUR',
Expand Down Expand Up @@ -49,7 +55,7 @@ async function fetchHapiSummary(countryCode: string): Promise<HumanitarianCountr
}

const response = await fetch(url, {
headers: { Accept: 'application/json' },
headers: { Accept: 'application/json', 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(15000),
});

Expand Down Expand Up @@ -143,8 +149,16 @@ export async function getHumanitarianSummary(
req: GetHumanitarianSummaryRequest,
): Promise<GetHumanitarianSummaryResponse> {
try {
const cacheKey = `${REDIS_CACHE_KEY}:${req.countryCode || 'all'}`;
const cached = (await getCachedJson(cacheKey)) as GetHumanitarianSummaryResponse | null;
if (cached?.summary) return cached;

const summary = await fetchHapiSummary(req.countryCode);
return { summary };
const result: GetHumanitarianSummaryResponse = { summary };
if (summary) {
setCachedJson(cacheKey, result, REDIS_CACHE_TTL).catch(() => {});
}
return result;
} catch {
return { summary: undefined };
}
Expand Down
17 changes: 16 additions & 1 deletion server/worldmonitor/conflict/v1/list-acled-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import type {
AcledConflictEvent,
} from '../../../../src/generated/server/worldmonitor/conflict/v1/service_server';

import { CHROME_UA } from '../../../_shared/constants';
import { getCachedJson, setCachedJson } from '../../../_shared/redis';

const REDIS_CACHE_KEY = 'conflict:acled:v1';
const REDIS_CACHE_TTL = 900; // 15 min — ACLED rate-limited

const ACLED_API_URL = 'https://acleddata.com/api/acled/read';

async function fetchAcledConflicts(req: ListAcledEventsRequest): Promise<AcledConflictEvent[]> {
Expand Down Expand Up @@ -44,6 +50,7 @@ async function fetchAcledConflicts(req: ListAcledEventsRequest): Promise<AcledCo
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'User-Agent': CHROME_UA,
},
signal: AbortSignal.timeout(15000),
});
Expand Down Expand Up @@ -90,8 +97,16 @@ export async function listAcledEvents(
req: ListAcledEventsRequest,
): Promise<ListAcledEventsResponse> {
try {
const cacheKey = `${REDIS_CACHE_KEY}:${req.country || 'all'}:${req.timeRange?.start || 0}:${req.timeRange?.end || 0}`;
const cached = (await getCachedJson(cacheKey)) as ListAcledEventsResponse | null;
if (cached?.events?.length) return cached;

const events = await fetchAcledConflicts(req);
return { events, pagination: undefined };
const result: ListAcledEventsResponse = { events, pagination: undefined };
if (events.length > 0) {
setCachedJson(cacheKey, result, REDIS_CACHE_TTL).catch(() => {});
}
return result;
} catch {
return { events: [], pagination: undefined };
}
Expand Down
14 changes: 9 additions & 5 deletions server/worldmonitor/conflict/v1/list-ucdp-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
UcdpViolenceType,
} from '../../../../src/generated/server/worldmonitor/conflict/v1/service_server';
import { getCachedJson, setCachedJson } from '../../../_shared/redis';
import { CHROME_UA } from '../../../_shared/constants';

const UCDP_PAGE_SIZE = 1000;
const MAX_PAGES = 12;
Expand Down Expand Up @@ -60,7 +61,7 @@ function buildVersionCandidates(): string[] {

// Negative cache: prevent hammering UCDP when it's down
let lastFailureTimestamp = 0;
const NEGATIVE_CACHE_MS = 5 * 60 * 1000; // 5 minutes backoff after failure
const NEGATIVE_CACHE_MS = 60 * 1000; // 60 seconds backoff after failure

// Discovered version cache: avoid re-probing every request
let discoveredVersion: string | null = null;
Expand All @@ -71,8 +72,8 @@ async function fetchGedPage(version: string, page: number): Promise<any> {
const response = await fetch(
`https://ucdpapi.pcr.uu.se/api/gedevents/${version}?pagesize=${UCDP_PAGE_SIZE}&page=${page}`,
{
headers: { Accept: 'application/json' },
signal: AbortSignal.timeout(6000),
headers: { Accept: 'application/json', 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(15000),
},
);
if (!response.ok) {
Expand Down Expand Up @@ -194,9 +195,12 @@ async function fetchUcdpGedEvents(req: ListUcdpEventsRequest): Promise<UcdpViole
lastFailureTimestamp = 0;

// Cache with TTL based on completeness (ported from main #198)
// Only cache non-empty results to avoid serving stale empty data for hours
const ttl = isPartial ? CACHE_TTL_PARTIAL : CACHE_TTL_FULL;
await setCachedJson(CACHE_KEY, mapped, ttl).catch(() => {});
fallbackCache = { data: mapped, timestamp: Date.now(), ttlMs: ttl * 1000 };
if (mapped.length > 0) {
await setCachedJson(CACHE_KEY, mapped, ttl).catch(() => {});
fallbackCache = { data: mapped, timestamp: Date.now(), ttlMs: ttl * 1000 };
}

return mapped;
} catch {
Expand Down
14 changes: 9 additions & 5 deletions server/worldmonitor/cyber/v1/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import type {
CriticalityLevel,
} from '../../../../src/generated/server/worldmonitor/cyber/v1/service_server';

import { CHROME_UA } from '../../../_shared/constants';

// ========================================================================
// Constants
// ========================================================================
Expand Down Expand Up @@ -276,6 +278,7 @@ async function fetchGeoIp(
// Primary: ipinfo.io
try {
const resp = await fetch(`https://ipinfo.io/${encodeURIComponent(ip)}/json`, {
headers: { 'User-Agent': CHROME_UA },
signal: signal || AbortSignal.timeout(GEO_PER_IP_TIMEOUT_MS),
});
if (resp.ok) {
Expand All @@ -295,6 +298,7 @@ async function fetchGeoIp(
// Fallback: freeipapi.com
try {
const resp = await fetch(`https://freeipapi.com/api/json/${encodeURIComponent(ip)}`, {
headers: { 'User-Agent': CHROME_UA },
signal: signal || AbortSignal.timeout(GEO_PER_IP_TIMEOUT_MS),
});
if (!resp.ok) return null;
Expand Down Expand Up @@ -434,7 +438,7 @@ function parseFeodoRecord(record: any, cutoffMs: number): RawThreat | null {
export async function fetchFeodoSource(limit: number, cutoffMs: number): Promise<SourceResult> {
try {
const response = await fetch(FEODO_URL, {
headers: { Accept: 'application/json' },
headers: { Accept: 'application/json', 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
});
if (!response.ok) return { ok: false, threats: [] };
Expand Down Expand Up @@ -524,7 +528,7 @@ export async function fetchUrlhausSource(limit: number, cutoffMs: number): Promi
try {
const response = await fetch(URLHAUS_RECENT_URL(limit), {
method: 'GET',
headers: { Accept: 'application/json', 'Auth-Key': authKey },
headers: { Accept: 'application/json', 'Auth-Key': authKey, 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
});
if (!response.ok) return { ok: false, threats: [] };
Expand Down Expand Up @@ -591,7 +595,7 @@ function parseC2IntelCsvLine(line: string): RawThreat | null {
export async function fetchC2IntelSource(limit: number): Promise<SourceResult> {
try {
const response = await fetch(C2INTEL_URL, {
headers: { Accept: 'text/plain' },
headers: { Accept: 'text/plain', 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
});
if (!response.ok) return { ok: false, threats: [] };
Expand Down Expand Up @@ -621,7 +625,7 @@ export async function fetchOtxSource(limit: number, days: number): Promise<Sourc
const response = await fetch(
`${OTX_INDICATORS_URL}${encodeURIComponent(since)}`,
{
headers: { Accept: 'application/json', 'X-OTX-API-KEY': apiKey },
headers: { Accept: 'application/json', 'X-OTX-API-KEY': apiKey, 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
},
);
Expand Down Expand Up @@ -675,7 +679,7 @@ export async function fetchAbuseIpDbSource(limit: number): Promise<SourceResult>
try {
const url = `${ABUSEIPDB_BLACKLIST_URL}?confidenceMinimum=90&limit=${Math.min(limit, 500)}`;
const response = await fetch(url, {
headers: { Accept: 'application/json', Key: apiKey },
headers: { Accept: 'application/json', Key: apiKey, 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
});
if (!response.ok) return { ok: false, threats: [] };
Expand Down
Loading