fix: introduce our own cors proxy for git import to fix 403 errors on isometric git cors proxy (#924)
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
This commit is contained in:
Eduard Ruzga 2025-01-05 13:56:02 +02:00 committed by GitHub
parent 31e03ce99f
commit b1f9380c30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 264 additions and 135 deletions

View File

@ -3,6 +3,9 @@ import { useGit } from '~/lib/hooks/useGit';
import type { Message } from 'ai'; import type { Message } from 'ai';
import { detectProjectCommands, createCommandsMessage } from '~/utils/projectCommands'; import { detectProjectCommands, createCommandsMessage } from '~/utils/projectCommands';
import { generateId } from '~/utils/fileUtils'; import { generateId } from '~/utils/fileUtils';
import { useState } from 'react';
import { toast } from 'react-toastify';
import { LoadingOverlay } from '~/components/ui/LoadingOverlay';
const IGNORE_PATTERNS = [ const IGNORE_PATTERNS = [
'node_modules/**', 'node_modules/**',
@ -37,6 +40,8 @@ interface GitCloneButtonProps {
export default function GitCloneButton({ importChat }: GitCloneButtonProps) { export default function GitCloneButton({ importChat }: GitCloneButtonProps) {
const { ready, gitClone } = useGit(); const { ready, gitClone } = useGit();
const [loading, setLoading] = useState(false);
const onClick = async (_e: any) => { const onClick = async (_e: any) => {
if (!ready) { if (!ready) {
return; return;
@ -45,6 +50,9 @@ export default function GitCloneButton({ importChat }: GitCloneButtonProps) {
const repoUrl = prompt('Enter the Git url'); const repoUrl = prompt('Enter the Git url');
if (repoUrl) { if (repoUrl) {
setLoading(true);
try {
const { workdir, data } = await gitClone(repoUrl); const { workdir, data } = await gitClone(repoUrl);
if (importChat) { if (importChat) {
@ -53,22 +61,20 @@ export default function GitCloneButton({ importChat }: GitCloneButtonProps) {
const textDecoder = new TextDecoder('utf-8'); const textDecoder = new TextDecoder('utf-8');
// Convert files to common format for command detection
const fileContents = filePaths const fileContents = filePaths
.map((filePath) => { .map((filePath) => {
const { data: content, encoding } = data[filePath]; const { data: content, encoding } = data[filePath];
return { return {
path: filePath, path: filePath,
content: encoding === 'utf8' ? content : content instanceof Uint8Array ? textDecoder.decode(content) : '', content:
encoding === 'utf8' ? content : content instanceof Uint8Array ? textDecoder.decode(content) : '',
}; };
}) })
.filter((f) => f.content); .filter((f) => f.content);
// Detect and create commands message
const commands = await detectProjectCommands(fileContents); const commands = await detectProjectCommands(fileContents);
const commandsMessage = createCommandsMessage(commands); const commandsMessage = createCommandsMessage(commands);
// Create files message
const filesMessage: Message = { const filesMessage: Message = {
role: 'assistant', role: 'assistant',
content: `Cloning the repo ${repoUrl} into ${workdir} content: `Cloning the repo ${repoUrl} into ${workdir}
@ -94,10 +100,17 @@ ${file.content}
await importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, messages); await importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, messages);
} }
} catch (error) {
console.error('Error during import:', error);
toast.error('Failed to import repository');
} finally {
setLoading(false);
}
} }
}; };
return ( return (
<>
<button <button
onClick={onClick} onClick={onClick}
title="Clone a Git Repo" title="Clone a Git Repo"
@ -106,5 +119,7 @@ ${file.content}
<span className="i-ph:git-branch" /> <span className="i-ph:git-branch" />
Clone a Git Repo Clone a Git Repo
</button> </button>
{loading && <LoadingOverlay message="Please wait while we clone the repository..." />}
</>
); );
} }

View File

@ -49,29 +49,28 @@ export function GitUrlImport() {
if (repoUrl) { if (repoUrl) {
const ig = ignore().add(IGNORE_PATTERNS); const ig = ignore().add(IGNORE_PATTERNS);
try {
const { workdir, data } = await gitClone(repoUrl); const { workdir, data } = await gitClone(repoUrl);
if (importChat) { if (importChat) {
const filePaths = Object.keys(data).filter((filePath) => !ig.ignores(filePath)); const filePaths = Object.keys(data).filter((filePath) => !ig.ignores(filePath));
const textDecoder = new TextDecoder('utf-8'); const textDecoder = new TextDecoder('utf-8');
// Convert files to common format for command detection
const fileContents = filePaths const fileContents = filePaths
.map((filePath) => { .map((filePath) => {
const { data: content, encoding } = data[filePath]; const { data: content, encoding } = data[filePath];
return { return {
path: filePath, path: filePath,
content: encoding === 'utf8' ? content : content instanceof Uint8Array ? textDecoder.decode(content) : '', content:
encoding === 'utf8' ? content : content instanceof Uint8Array ? textDecoder.decode(content) : '',
}; };
}) })
.filter((f) => f.content); .filter((f) => f.content);
// Detect and create commands message
const commands = await detectProjectCommands(fileContents); const commands = await detectProjectCommands(fileContents);
const commandsMessage = createCommandsMessage(commands); const commandsMessage = createCommandsMessage(commands);
// Create files message
const filesMessage: Message = { const filesMessage: Message = {
role: 'assistant', role: 'assistant',
content: `Cloning the repo ${repoUrl} into ${workdir} content: `Cloning the repo ${repoUrl} into ${workdir}
@ -97,6 +96,14 @@ ${file.content}
await importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, messages); await importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, messages);
} }
} catch (error) {
console.error('Error during import:', error);
toast.error('Failed to import repository');
setLoading(false);
window.location.href = '/';
return;
}
} }
}; };

View File

@ -1,13 +1,31 @@
export const LoadingOverlay = ({ message = 'Loading...' }) => { export const LoadingOverlay = ({
message = 'Loading...',
progress,
progressText,
}: {
message?: string;
progress?: number;
progressText?: string;
}) => {
return ( return (
<div className="fixed inset-0 flex items-center justify-center bg-black/80 z-50 backdrop-blur-sm"> <div className="fixed inset-0 flex items-center justify-center bg-black/80 z-50 backdrop-blur-sm">
{/* Loading content */}
<div className="relative flex flex-col items-center gap-4 p-8 rounded-lg bg-bolt-elements-background-depth-2 shadow-lg"> <div className="relative flex flex-col items-center gap-4 p-8 rounded-lg bg-bolt-elements-background-depth-2 shadow-lg">
<div <div
className={'i-svg-spinners:90-ring-with-bg text-bolt-elements-loader-progress'} className={'i-svg-spinners:90-ring-with-bg text-bolt-elements-loader-progress'}
style={{ fontSize: '2rem' }} style={{ fontSize: '2rem' }}
></div> ></div>
<p className="text-lg text-bolt-elements-textTertiary">{message}</p> <p className="text-lg text-bolt-elements-textTertiary">{message}</p>
{progress !== undefined && (
<div className="w-64 flex flex-col gap-2">
<div className="w-full h-2 bg-bolt-elements-background-depth-1 rounded-full overflow-hidden">
<div
className="h-full bg-bolt-elements-loader-progress transition-all duration-300 ease-out rounded-full"
style={{ width: `${Math.min(100, Math.max(0, progress))}%` }}
/>
</div>
{progressText && <p className="text-sm text-bolt-elements-textTertiary text-center">{progressText}</p>}
</div>
)}
</div> </div>
</div> </div>
); );

View File

@ -49,6 +49,8 @@ export function useGit() {
} }
fileData.current = {}; fileData.current = {};
try {
await git.clone({ await git.clone({
fs, fs,
http, http,
@ -56,10 +58,8 @@ export function useGit() {
url, url,
depth: 1, depth: 1,
singleBranch: true, singleBranch: true,
corsProxy: 'https://cors.isomorphic-git.org', corsProxy: '/api/git-proxy',
onAuth: (url) => { onAuth: (url) => {
// let domain=url.split("/")[2]
let auth = lookupSavedPassword(url); let auth = lookupSavedPassword(url);
if (auth) { if (auth) {
@ -91,8 +91,12 @@ export function useGit() {
} }
return { workdir: webcontainer.workdir, data }; return { workdir: webcontainer.workdir, data };
} catch (error) {
console.error('Git clone error:', error);
throw error;
}
}, },
[webcontainer], [webcontainer, fs, ready],
); );
return { ready, gitClone }; return { ready, gitClone };
@ -104,55 +108,86 @@ const getFs = (
) => ({ ) => ({
promises: { promises: {
readFile: async (path: string, options: any) => { readFile: async (path: string, options: any) => {
const encoding = options.encoding; const encoding = options?.encoding;
const relativePath = pathUtils.relative(webcontainer.workdir, path); const relativePath = pathUtils.relative(webcontainer.workdir, path);
console.log('readFile', relativePath, encoding);
return await webcontainer.fs.readFile(relativePath, encoding); try {
const result = await webcontainer.fs.readFile(relativePath, encoding);
return result;
} catch (error) {
throw error;
}
}, },
writeFile: async (path: string, data: any, options: any) => { writeFile: async (path: string, data: any, options: any) => {
const encoding = options.encoding; const encoding = options.encoding;
const relativePath = pathUtils.relative(webcontainer.workdir, path); const relativePath = pathUtils.relative(webcontainer.workdir, path);
console.log('writeFile', { relativePath, data, encoding });
if (record.current) { if (record.current) {
record.current[relativePath] = { data, encoding }; record.current[relativePath] = { data, encoding };
} }
return await webcontainer.fs.writeFile(relativePath, data, { ...options, encoding }); try {
const result = await webcontainer.fs.writeFile(relativePath, data, { ...options, encoding });
return result;
} catch (error) {
throw error;
}
}, },
mkdir: async (path: string, options: any) => { mkdir: async (path: string, options: any) => {
const relativePath = pathUtils.relative(webcontainer.workdir, path); const relativePath = pathUtils.relative(webcontainer.workdir, path);
console.log('mkdir', relativePath, options);
return await webcontainer.fs.mkdir(relativePath, { ...options, recursive: true }); try {
const result = await webcontainer.fs.mkdir(relativePath, { ...options, recursive: true });
return result;
} catch (error) {
throw error;
}
}, },
readdir: async (path: string, options: any) => { readdir: async (path: string, options: any) => {
const relativePath = pathUtils.relative(webcontainer.workdir, path); const relativePath = pathUtils.relative(webcontainer.workdir, path);
console.log('readdir', relativePath, options);
return await webcontainer.fs.readdir(relativePath, options); try {
const result = await webcontainer.fs.readdir(relativePath, options);
return result;
} catch (error) {
throw error;
}
}, },
rm: async (path: string, options: any) => { rm: async (path: string, options: any) => {
const relativePath = pathUtils.relative(webcontainer.workdir, path); const relativePath = pathUtils.relative(webcontainer.workdir, path);
console.log('rm', relativePath, options);
return await webcontainer.fs.rm(relativePath, { ...(options || {}) }); try {
const result = await webcontainer.fs.rm(relativePath, { ...(options || {}) });
return result;
} catch (error) {
throw error;
}
}, },
rmdir: async (path: string, options: any) => { rmdir: async (path: string, options: any) => {
const relativePath = pathUtils.relative(webcontainer.workdir, path); const relativePath = pathUtils.relative(webcontainer.workdir, path);
console.log('rmdir', relativePath, options);
return await webcontainer.fs.rm(relativePath, { recursive: true, ...options }); try {
const result = await webcontainer.fs.rm(relativePath, { recursive: true, ...options });
return result;
} catch (error) {
throw error;
}
}, },
// Mock implementations for missing functions
unlink: async (path: string) => { unlink: async (path: string) => {
// unlink is just removing a single file
const relativePath = pathUtils.relative(webcontainer.workdir, path); const relativePath = pathUtils.relative(webcontainer.workdir, path);
return await webcontainer.fs.rm(relativePath, { recursive: false });
},
try {
return await webcontainer.fs.rm(relativePath, { recursive: false });
} catch (error) {
throw error;
}
},
stat: async (path: string) => { stat: async (path: string) => {
try { try {
const relativePath = pathUtils.relative(webcontainer.workdir, path); const relativePath = pathUtils.relative(webcontainer.workdir, path);
@ -185,23 +220,12 @@ const getFs = (
throw err; throw err;
} }
}, },
lstat: async (path: string) => { lstat: async (path: string) => {
/*
* For basic usage, lstat can return the same as stat
* since we're not handling symbolic links
*/
return await getFs(webcontainer, record).promises.stat(path); return await getFs(webcontainer, record).promises.stat(path);
}, },
readlink: async (path: string) => { readlink: async (path: string) => {
/*
* Since WebContainer doesn't support symlinks,
* we'll throw a "not a symbolic link" error
*/
throw new Error(`EINVAL: invalid argument, readlink '${path}'`); throw new Error(`EINVAL: invalid argument, readlink '${path}'`);
}, },
symlink: async (target: string, path: string) => { symlink: async (target: string, path: string) => {
/* /*
* Since WebContainer doesn't support symlinks, * Since WebContainer doesn't support symlinks,

View File

@ -0,0 +1,65 @@
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 });
}
}