Make corsProxy configurable

This commit is contained in:
Alex Gleason committed 2025-09-14 17:58:41 -05:00
1 parent bc009d444e
commit 94e1a6543c
10 files changed
+341 -13

No files matched your search

+39 -2
View File
@@ -7,7 +7,7 @@ import type { GitCredential } from '@/contexts/GitSettingsContext';
// Test component that uses the hook
function TestComponent() {
const { settings, addCredential, removeCredential, updateCredential, isConfigured } = useGitSettings();
const { settings, updateSettings, addCredential, removeCredential, updateCredential, isConfigured } = useGitSettings();
const handleAdd = () => {
const credential: GitCredential = {
@@ -25,13 +25,21 @@ function TestComponent() {
removeCredential('https://github.com');
};
const handleUpdateCorsProxy = () => {
updateSettings({ corsProxy: 'https://custom-cors.example.com' });
};
return (
<div>
<div data-testid="configured">{isConfigured ? 'configured' : 'not-configured'}</div>
<div data-testid="count">{Object.keys(settings.credentials).length}</div>
<div data-testid="cors-proxy">{settings.corsProxy}</div>
<button onClick={handleAdd}>Add GitHub</button>
<button onClick={handleUpdate}>Update GitHub</button>
<button onClick={handleRemove}>Remove GitHub</button>
<button onClick={handleUpdateCorsProxy}>Update CORS Proxy</button>
{settings.credentials['https://github.com'] && (
<div data-testid="github-creds">
{settings.credentials['https://github.com'].username}:
@@ -126,12 +134,13 @@ describe('GitSettingsProvider', () => {
// Check that settings were saved to localStorage
const stored = localStorage.getItem('git-settings');
expect(stored).toBeTruthy();
const parsed = JSON.parse(stored!);
expect(parsed.credentials['https://github.com']).toEqual({
username: 'git',
password: 'test-token',
});
expect(parsed.corsProxy).toBe('https://cors.isomorphic-git.org');
});
it('loads settings from localStorage', () => {
@@ -143,6 +152,7 @@ describe('GitSettingsProvider', () => {
password: 'gitlab-token',
},
},
corsProxy: 'https://custom-cors.example.com',
};
localStorage.setItem('git-settings', JSON.stringify(settings));
@@ -154,5 +164,32 @@ describe('GitSettingsProvider', () => {
expect(screen.getByTestId('configured')).toHaveTextContent('configured');
expect(screen.getByTestId('count')).toHaveTextContent('1');
expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://custom-cors.example.com');
});
it('provides default CORS proxy', () => {
render(
<TestWrapper>
<TestComponent />
</TestWrapper>
);
expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://cors.isomorphic-git.org');
});
it('can update CORS proxy', () => {
render(
<TestWrapper>
<TestComponent />
</TestWrapper>
);
expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://cors.isomorphic-git.org');
fireEvent.click(screen.getByText('Update CORS Proxy'));
expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://custom-cors.example.com');
});
});
+2
View File
@@ -7,6 +7,7 @@ interface GitSettingsProviderProps {
const DEFAULT_SETTINGS: GitSettings = {
credentials: {},
corsProxy: 'https://cors.isomorphic-git.org',
};
const STORAGE_KEY = 'git-settings';
@@ -21,6 +22,7 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) {
const settings = parsed as Partial<GitSettings>;
return {
credentials: settings.credentials || {},
corsProxy: settings.corsProxy || DEFAULT_SETTINGS.corsProxy,
};
}
} catch {
+1
View File
@@ -7,6 +7,7 @@ export interface GitCredential {
export interface GitSettings {
credentials: Record<string, GitCredential>; // keyed by origin
corsProxy: string;
}
export interface GitSettingsContextType {
+97
View File
@@ -0,0 +1,97 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { renderHook } from '@testing-library/react';
import { useGit } from './useGit';
import { useFS } from './useFS';
import { useGitSettings } from './useGitSettings';
import { Git } from '@/lib/git';
import type { JSRuntimeFS } from '@/lib/JSRuntime';
// Mock the dependencies
vi.mock('./useFS');
vi.mock('./useGitSettings');
vi.mock('@/lib/git');
const mockUseFS = vi.mocked(useFS);
const mockUseGitSettings = vi.mocked(useGitSettings);
const MockGit = vi.mocked(Git);
describe('useGit', () => {
const mockFS = {} as JSRuntimeFS;
beforeEach(() => {
vi.clearAllMocks();
mockUseFS.mockReturnValue({ fs: mockFS });
});
it('creates Git instance with default CORS proxy', () => {
mockUseGitSettings.mockReturnValue({
settings: {
credentials: {},
corsProxy: 'https://cors.isomorphic-git.org',
},
updateSettings: vi.fn(),
addCredential: vi.fn(),
removeCredential: vi.fn(),
updateCredential: vi.fn(),
isConfigured: false,
});
renderHook(() => useGit());
expect(MockGit).toHaveBeenCalledWith(mockFS, 'https://cors.isomorphic-git.org');
});
it('creates Git instance with custom CORS proxy', () => {
mockUseGitSettings.mockReturnValue({
settings: {
credentials: {},
corsProxy: 'https://custom-cors.example.com',
},
updateSettings: vi.fn(),
addCredential: vi.fn(),
removeCredential: vi.fn(),
updateCredential: vi.fn(),
isConfigured: false,
});
renderHook(() => useGit());
expect(MockGit).toHaveBeenCalledWith(mockFS, 'https://custom-cors.example.com');
});
it('recreates Git instance when CORS proxy changes', () => {
const { rerender } = renderHook(() => useGit());
// First render with default proxy
mockUseGitSettings.mockReturnValue({
settings: {
credentials: {},
corsProxy: 'https://cors.isomorphic-git.org',
},
updateSettings: vi.fn(),
addCredential: vi.fn(),
removeCredential: vi.fn(),
updateCredential: vi.fn(),
isConfigured: false,
});
rerender();
expect(MockGit).toHaveBeenCalledWith(mockFS, 'https://cors.isomorphic-git.org');
// Second render with custom proxy
mockUseGitSettings.mockReturnValue({
settings: {
credentials: {},
corsProxy: 'https://custom-cors.example.com',
},
updateSettings: vi.fn(),
addCredential: vi.fn(),
removeCredential: vi.fn(),
updateCredential: vi.fn(),
isConfigured: false,
});
rerender();
expect(MockGit).toHaveBeenLastCalledWith(mockFS, 'https://custom-cors.example.com');
});
});
+5 -3
View File
@@ -1,17 +1,19 @@
import { useMemo } from 'react';
import { Git } from '@/lib/git';
import { useFS } from '@/hooks/useFS';
import { useGitSettings } from '@/hooks/useGitSettings';
/**
* Hook that provides a Git instance configured with the virtual filesystem
* and appropriate CORS proxy for GitHub/GitLab repositories.
* and configurable CORS proxy for GitHub/GitLab repositories.
*/
export function useGit() {
const { fs } = useFS();
const { settings } = useGitSettings();
const git = useMemo(() => {
return new Git(fs, 'https://cors.isomorphic-git.org');
}, [fs]);
return new Git(fs, settings.corsProxy);
}, [fs, settings.corsProxy]);
return git;
}
+3 -4
View File
@@ -1,4 +1,4 @@
import http from 'isomorphic-git/http/web';
// http is handled by the Git class
import type { JSRuntimeFS } from "../../JSRuntime";
import type { ShellCommandResult } from "../ShellCommand";
import { createSuccessResult, createErrorResult } from "../ShellCommand";
@@ -47,10 +47,9 @@ export class GitCloneCommand implements GitSubcommand {
// Directory doesn't exist, which is what we want
}
// Parse URL to determine if we need CORS proxy
let url: URL;
// Validate URL format
try {
url = new URL(repository);
new URL(repository);
} catch {
return createErrorResult(`fatal: repository '${repository}' does not appear to be a git repository`);
}
+1 -1
View File
@@ -103,7 +103,7 @@ export class GitCommitCommand implements GitSubcommand {
}
// Create the commit
let commitOptions: {
const commitOptions: {
dir: string;
message: string;
author: { name: string; email: string };
+164
View File
@@ -0,0 +1,164 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { Git } from './git';
import git from 'isomorphic-git';
import type { JSRuntimeFS } from './JSRuntime';
// Mock isomorphic-git
vi.mock('isomorphic-git');
vi.mock('isomorphic-git/http/web');
const mockGit = vi.mocked(git);
describe('Git', () => {
const mockFS = {} as JSRuntimeFS;
let gitInstance: Git;
beforeEach(() => {
vi.clearAllMocks();
gitInstance = new Git(mockFS, 'https://cors.example.com');
});
describe('constructor', () => {
it('creates instance with default regex when none provided', () => {
const git = new Git(mockFS, 'https://cors.example.com');
expect(git).toBeInstanceOf(Git);
});
});
describe('clone', () => {
beforeEach(() => {
mockGit.clone = vi.fn().mockResolvedValue(undefined);
});
it('uses CORS proxy for all URLs', async () => {
await gitInstance.clone({
dir: '/test',
url: 'https://github.com/user/repo.git',
});
expect(mockGit.clone).toHaveBeenCalledWith({
fs: mockFS,
http: expect.any(Object),
corsProxy: 'https://cors.example.com',
dir: '/test',
url: 'https://github.com/user/repo.git',
});
});
it('uses CORS proxy for GitLab URLs', async () => {
await gitInstance.clone({
dir: '/test',
url: 'https://gitlab.com/user/repo.git',
});
expect(mockGit.clone).toHaveBeenCalledWith({
fs: mockFS,
http: expect.any(Object),
corsProxy: 'https://cors.example.com',
dir: '/test',
url: 'https://gitlab.com/user/repo.git',
});
});
it('uses CORS proxy for custom URLs', async () => {
await gitInstance.clone({
dir: '/test',
url: 'https://custom.example.com/user/repo.git',
});
expect(mockGit.clone).toHaveBeenCalledWith({
fs: mockFS,
http: expect.any(Object),
corsProxy: 'https://cors.example.com',
dir: '/test',
url: 'https://custom.example.com/user/repo.git',
});
});
it('handles HTTP URLs correctly', async () => {
await gitInstance.clone({
dir: '/test',
url: 'http://github.com/user/repo.git',
});
expect(mockGit.clone).toHaveBeenCalledWith({
fs: mockFS,
http: expect.any(Object),
corsProxy: 'https://cors.example.com',
dir: '/test',
url: 'http://github.com/user/repo.git',
});
});
});
describe('getRemoteInfo', () => {
beforeEach(() => {
mockGit.getRemoteInfo = vi.fn().mockResolvedValue({});
});
it('uses CORS proxy for matching URLs', async () => {
await gitInstance.getRemoteInfo({
url: 'https://github.com/user/repo.git',
});
expect(mockGit.getRemoteInfo).toHaveBeenCalledWith({
http: expect.any(Object),
corsProxy: 'https://cors.example.com',
url: 'https://github.com/user/repo.git',
});
});
it('uses CORS proxy for all URLs', async () => {
await gitInstance.getRemoteInfo({
url: 'https://custom.example.com/user/repo.git',
});
expect(mockGit.getRemoteInfo).toHaveBeenCalledWith({
http: expect.any(Object),
corsProxy: 'https://cors.example.com',
url: 'https://custom.example.com/user/repo.git',
});
});
});
describe('fetch with remote resolution', () => {
beforeEach(() => {
mockGit.fetch = vi.fn().mockResolvedValue(undefined);
mockGit.listRemotes = vi.fn().mockResolvedValue([
{ remote: 'origin', url: 'https://github.com/user/repo.git' }
]);
});
it('uses CORS proxy for all fetch operations', async () => {
await gitInstance.fetch({
dir: '/test',
remote: 'origin',
});
expect(mockGit.fetch).toHaveBeenCalledWith({
fs: mockFS,
http: expect.any(Object),
corsProxy: 'https://cors.example.com',
dir: '/test',
remote: 'origin',
});
});
it('handles fetch with direct URL parameter', async () => {
await gitInstance.fetch({
dir: '/test',
url: 'https://gitlab.com/user/repo.git',
});
expect(mockGit.fetch).toHaveBeenCalledWith({
fs: mockFS,
http: expect.any(Object),
corsProxy: 'https://cors.example.com',
dir: '/test',
url: 'https://gitlab.com/user/repo.git',
});
});
});
});
+3 -1
View File
@@ -4,7 +4,7 @@ import type { JSRuntimeFS } from './JSRuntime';
/**
* Git class that wraps isomorphic-git and provides a cleaner interface.
* Instantiate with an fs implementation and optional corsProxy string.
* Instantiate with an fs implementation, optional corsProxy string, and optional corsProxyRegex.
* All methods have the same names as isomorphic-git but don't require
* passing fs, http, and corsProxy each time.
*/
@@ -17,6 +17,8 @@ export class Git {
this.corsProxy = corsProxy;
}
// Repository initialization and configuration
async init(options: Omit<Parameters<typeof git.init>[0], 'fs'>) {
return git.init({
+26 -2
View File
@@ -37,7 +37,7 @@ const PRESET_PROVIDERS: PresetProvider[] = [
];
export function GitSettings() {
const { settings, addCredential, removeCredential, updateCredential } = useGitSettings();
const { settings, updateSettings, addCredential, removeCredential, updateCredential } = useGitSettings();
const isMobile = useIsMobile();
const navigate = useNavigate();
const [customOrigin, setCustomOrigin] = useState('');
@@ -257,7 +257,7 @@ export function GitSettings() {
</div>
)}
{availablePresets.length > 0 && <Separator />}
<Separator />
{/* Custom Provider */}
<div className="space-y-3">
@@ -319,6 +319,30 @@ export function GitSettings() {
</AccordionContent>
</AccordionItem>
</Accordion>
{/* CORS Proxy Configuration */}
<Accordion type="single" collapsible className="w-full">
<AccordionItem value="cors-proxy" className="border rounded-lg">
<AccordionTrigger className="px-4 py-3 hover:no-underline">
<h4 className="text-sm font-medium">CORS Proxy</h4>
</AccordionTrigger>
<AccordionContent className="px-4 pb-4">
<div className="space-y-3">
<div className="grid gap-2">
<Input
id="cors-proxy"
placeholder="https://cors.isomorphic-git.org"
value={settings.corsProxy}
onChange={(e) => updateSettings({ corsProxy: e.target.value })}
/>
<p className="text-xs text-muted-foreground">
CORS proxy server used for all Git operations with remote repositories. Required for browser-based Git operations.
</p>
</div>
</div>
</AccordionContent>
</AccordionItem>
</Accordion>
</div>
</div>
</div>