mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
89 lines
2.8 KiB
JavaScript
89 lines
2.8 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 = 15000) {
|
|
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}/polymarket${requestUrl.search || ''}`;
|
|
const response = await fetchWithTimeout(relayUrl, {
|
|
headers: getRelayHeaders({ Accept: 'application/json' }),
|
|
}, 15000);
|
|
|
|
const body = await response.text();
|
|
const headers = {
|
|
'Content-Type': response.headers.get('content-type') || 'application/json',
|
|
'Cache-Control': response.headers.get('cache-control') || 'no-cache',
|
|
...corsHeaders,
|
|
};
|
|
|
|
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 },
|
|
});
|
|
}
|
|
}
|