Files
worldmonitor/api/hackernews.js
Elie Habib c353cf2070 Reduce egress costs, add PWA support, fix Polymarket and Railway relay
Egress optimization:
- Add s-maxage + stale-while-revalidate to all API endpoints for Vercel CDN caching
- Add vercel.json with immutable caching for hashed assets
- Add gzip compression to sidecar responses >1KB
- Add gzip to Railway RSS responses (4 paths previously uncompressed)
- Increase polling intervals: markets/crypto 60s→120s, ETF/macro/stablecoins 60s→180s
- Remove hardcoded Railway URL from theater-posture.js (now env-var only)

PWA / Service Worker:
- Add vite-plugin-pwa with autoUpdate strategy
- Cache map tiles (CacheFirst), fonts (StaleWhileRevalidate), static assets
- NetworkOnly for all /api/* routes (real-time data must be fresh)
- Manual SW registration (web only, skip Tauri)
- Add offline fallback page
- Replace manual manifest with plugin-generated manifest

Polymarket fix:
- Route dev proxy through production Vercel (bypasses JA3 blocking)
- Add 4th fallback tier: production URL as absolute fallback

Desktop/Sidecar:
- Dual-backend cache (_upstash-cache.js): Redis cloud + in-memory+file desktop
- Settings window OK/Cancel redesign
- Runtime config and secret injection improvements
2026-02-14 19:53:04 +04:00

93 lines
2.9 KiB
JavaScript

export const config = { runtime: 'edge' };
// Fetch Hacker News front page stories
// Uses official HackerNews Firebase API
const ALLOWED_STORY_TYPES = new Set(['top', 'new', 'best', 'ask', 'show', 'job']);
const DEFAULT_LIMIT = 30;
const MAX_LIMIT = 60;
const MAX_CONCURRENCY = 10;
function parseLimit(rawLimit) {
const parsed = Number.parseInt(rawLimit || '', 10);
if (!Number.isFinite(parsed)) return DEFAULT_LIMIT;
return Math.max(1, Math.min(MAX_LIMIT, parsed));
}
export default async function handler(request) {
try {
const { searchParams } = new URL(request.url);
const requestedType = searchParams.get('type') || 'top';
const storyType = ALLOWED_STORY_TYPES.has(requestedType) ? requestedType : 'top';
const limit = parseLimit(searchParams.get('limit'));
// HackerNews official Firebase API
const storiesUrl = `https://hacker-news.firebaseio.com/v0/${storyType}stories.json`;
// Fetch story IDs
const storiesResponse = await fetch(storiesUrl, {
signal: AbortSignal.timeout(10000),
});
if (!storiesResponse.ok) {
throw new Error(`HackerNews API returned ${storiesResponse.status}`);
}
const storyIds = await storiesResponse.json();
if (!Array.isArray(storyIds)) {
throw new Error('HackerNews API returned unexpected payload');
}
const limitedIds = storyIds.slice(0, limit);
// Fetch story details in bounded batches to avoid unbounded fan-out.
const stories = [];
for (let i = 0; i < limitedIds.length; i += MAX_CONCURRENCY) {
const batchIds = limitedIds.slice(i, i + MAX_CONCURRENCY);
const storyPromises = batchIds.map(async (id) => {
const storyUrl = `https://hacker-news.firebaseio.com/v0/item/${id}.json`;
try {
const response = await fetch(storyUrl, {
signal: AbortSignal.timeout(5000),
});
if (response.ok) {
return await response.json();
}
return null;
} catch (error) {
console.error(`Failed to fetch story ${id}:`, error);
return null;
}
});
const batchResults = await Promise.all(storyPromises);
stories.push(...batchResults.filter((story) => story !== null));
}
return new Response(JSON.stringify({
type: storyType,
stories: stories,
total: stories.length,
timestamp: new Date().toISOString()
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, max-age=300, s-maxage=300, stale-while-revalidate=60', // 5 min cache
},
});
} catch (error) {
return new Response(
JSON.stringify({
error: 'Failed to fetch Hacker News data',
message: error.message
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
}
);
}
}