mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-01-22 19:06:12 +00:00
b1f9380c30
Some checks are pending
Update Stable Branch / prepare-release (push) Waiting to run
* Exploration of improving git import * Fix our own git proxy * Clean out file counting for progress, does not seem to work well anyways
66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
import { json } from '@remix-run/cloudflare';
|
|
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/cloudflare';
|
|
|
|
// Handle all HTTP methods
|
|
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);
|
|
|
|
// Reconstruct the target URL
|
|
const targetURL = `https://${path}${url.search}`;
|
|
|
|
// Forward the request to the target URL
|
|
const response = await fetch(targetURL, {
|
|
method: request.method,
|
|
headers: {
|
|
...Object.fromEntries(request.headers),
|
|
|
|
// Override host header with the target host
|
|
host: new URL(targetURL).host,
|
|
},
|
|
body: ['GET', 'HEAD'].includes(request.method) ? null : await request.arrayBuffer(),
|
|
});
|
|
|
|
// Create response with CORS headers
|
|
const corsHeaders = {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
|
'Access-Control-Allow-Headers': '*',
|
|
};
|
|
|
|
// Handle preflight requests
|
|
if (request.method === 'OPTIONS') {
|
|
return new Response(null, {
|
|
headers: corsHeaders,
|
|
status: 204,
|
|
});
|
|
}
|
|
|
|
// Forward the response with CORS headers
|
|
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 });
|
|
}
|
|
}
|