|
import { json } from '@remix-run/cloudflare'; |
|
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/cloudflare'; |
|
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs) { |
|
return handleProxyRequest(request, params['*']); |
|
} |
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs) { |
|
return handleProxyRequest(request, params['*']); |
|
} |
|
|
|
async function handleProxyRequest(request: Request, path: string | undefined) { |
|
try { |
|
if (!path) { |
|
return json({ error: 'Invalid proxy URL format' }, { status: 400 }); |
|
} |
|
|
|
const url = new URL(request.url); |
|
|
|
|
|
const targetURL = `https://${path}${url.search}`; |
|
|
|
|
|
const response = await fetch(targetURL, { |
|
method: request.method, |
|
headers: { |
|
...Object.fromEntries(request.headers), |
|
|
|
|
|
host: new URL(targetURL).host, |
|
}, |
|
body: ['GET', 'HEAD'].includes(request.method) ? null : await request.arrayBuffer(), |
|
}); |
|
|
|
|
|
const corsHeaders = { |
|
'Access-Control-Allow-Origin': '*', |
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
|
'Access-Control-Allow-Headers': '*', |
|
}; |
|
|
|
|
|
if (request.method === 'OPTIONS') { |
|
return new Response(null, { |
|
headers: corsHeaders, |
|
status: 204, |
|
}); |
|
} |
|
|
|
|
|
const responseHeaders = new Headers(response.headers); |
|
Object.entries(corsHeaders).forEach(([key, value]) => { |
|
responseHeaders.set(key, value); |
|
}); |
|
|
|
return new Response(response.body, { |
|
status: response.status, |
|
headers: responseHeaders, |
|
}); |
|
} catch (error) { |
|
console.error('Git proxy error:', error); |
|
return json({ error: 'Proxy error' }, { status: 500 }); |
|
} |
|
} |
|
|