diff --git a/package-lock.json b/package-lock.json index 73908c5f..2d331915 100644 --- a/package-lock.json +++ b/package-lock.json @@ -80,6 +80,7 @@ "tailwind-merge": "^2.5.2", "tailwindcss-animate": "^1.0.7", "turndown": "^7.2.1", + "uri-templates": "^0.2.0", "use-stick-to-bottom": "^1.1.1", "vaul": "^0.9.3", "webln": "^0.3.2", @@ -100,6 +101,7 @@ "@types/react": "^18.3.1", "@types/react-dom": "^18.3.1", "@types/turndown": "^5.0.5", + "@types/uri-templates": "^0.1.34", "@vitejs/plugin-react-swc": "^3.5.0", "autoprefixer": "^10.4.20", "eslint": "^9.9.0", @@ -3849,6 +3851,13 @@ "integrity": "sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==", "license": "MIT" }, + "node_modules/@types/uri-templates": { + "version": "0.1.34", + "resolved": "https://registry.npmjs.org/@types/uri-templates/-/uri-templates-0.1.34.tgz", + "integrity": "sha512-13v4r/Op3iEO1y6FvEHQjrUNnrNyO67SigdFC9n80sVfsrM2AWJRNYbE1pBs4/p87I7z1J979JGeLAo3rM1L/Q==", + "dev": true, + "license": "MIT" + }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "8.31.1", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.31.1.tgz", @@ -10627,6 +10636,12 @@ "punycode": "^2.1.0" } }, + "node_modules/uri-templates": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/uri-templates/-/uri-templates-0.2.0.tgz", + "integrity": "sha512-EWkjYEN0L6KOfEoOH6Wj4ghQqU7eBZMJqRHQnxQAq+dSEzRPClkWjf8557HkWQXF6BrAUoLSAyy9i3RVTliaNg==", + "license": "http://geraintluff.github.io/tv4/LICENSE.txt" + }, "node_modules/use-callback-ref": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/use-callback-ref/-/use-callback-ref-1.3.3.tgz", diff --git a/package.json b/package.json index 07ecc7ed..8761be75 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,7 @@ "tailwind-merge": "^2.5.2", "tailwindcss-animate": "^1.0.7", "turndown": "^7.2.1", + "uri-templates": "^0.2.0", "use-stick-to-bottom": "^1.1.1", "vaul": "^0.9.3", "webln": "^0.3.2", @@ -103,6 +104,7 @@ "@types/react": "^18.3.1", "@types/react-dom": "^18.3.1", "@types/turndown": "^5.0.5", + "@types/uri-templates": "^0.1.34", "@vitejs/plugin-react-swc": "^3.5.0", "autoprefixer": "^10.4.20", "eslint": "^9.9.0", diff --git a/proxy.ts b/proxy.ts new file mode 100644 index 00000000..209de288 --- /dev/null +++ b/proxy.ts @@ -0,0 +1,22 @@ +export default { + async fetch(request: Request): Promise { + const url = new URL(request.url); + const target = url.searchParams.get("url"); + + if (!target) { + return new Response("Include a `url` query parameter", { status: 400 }); + } + + const headers = new Headers(request.headers); + headers.set('User-Agent', 'Shakespeare Proxy '); + + const targetResponse = await fetch(target, request); + const response = new Response(targetResponse.body, targetResponse); + + response.headers.set("Access-Control-Allow-Origin", "*"); + response.headers.set("Access-Control-Allow-Methods", "GET,HEAD,POST,OPTIONS"); + response.headers.set("Access-Control-Allow-Headers", "Content-Type"); + + return response; + } +}; diff --git a/src/components/GitSettingsProvider.test.tsx b/src/components/GitSettingsProvider.test.tsx index 96a201f3..2192aa12 100644 --- a/src/components/GitSettingsProvider.test.tsx +++ b/src/components/GitSettingsProvider.test.tsx @@ -179,7 +179,7 @@ describe('GitSettingsProvider', () => { password: 'test-token', } }, - corsProxy: 'https://cors.isomorphic-git.org' + corsProxy: 'https://proxy.shakespeare.diy/?url={href}' }) ); }); @@ -220,7 +220,7 @@ describe('GitSettingsProvider', () => { ); await waitFor(() => { - expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://cors.isomorphic-git.org'); + expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://proxy.shakespeare.diy/?url={href}'); }); }); @@ -232,7 +232,7 @@ describe('GitSettingsProvider', () => { ); await waitFor(() => { - expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://cors.isomorphic-git.org'); + expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://proxy.shakespeare.diy/?url={href}'); }); fireEvent.click(screen.getByText('Update CORS Proxy')); diff --git a/src/components/GitSettingsProvider.tsx b/src/components/GitSettingsProvider.tsx index 3f49fbf3..275a805f 100644 --- a/src/components/GitSettingsProvider.tsx +++ b/src/components/GitSettingsProvider.tsx @@ -9,7 +9,7 @@ interface GitSettingsProviderProps { const DEFAULT_SETTINGS: GitSettings = { credentials: {}, - corsProxy: 'https://cors.isomorphic-git.org', + corsProxy: 'https://proxy.shakespeare.diy/?url={href}', }; export function GitSettingsProvider({ children }: GitSettingsProviderProps) { @@ -22,6 +22,10 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) { const initializeSettings = async () => { try { const settings = await readGitSettings(fs); + // Migrate old CORS proxy format + if (settings.corsProxy === 'https://cors.isomorphic-git.org') { + settings.corsProxy = DEFAULT_SETTINGS.corsProxy; + } setSettings(settings); } catch (error) { console.error('Failed to initialize Git settings:', error); diff --git a/src/lib/configUtils.test.ts b/src/lib/configUtils.test.ts index 0aec9603..2936d823 100644 --- a/src/lib/configUtils.test.ts +++ b/src/lib/configUtils.test.ts @@ -185,7 +185,7 @@ describe('configUtils', () => { expect(result).toEqual({ credentials: {}, - corsProxy: 'https://cors.isomorphic-git.org', + corsProxy: 'https://proxy.shakespeare.diy/?url={href}', }); }); diff --git a/src/lib/configUtils.ts b/src/lib/configUtils.ts index 779dc548..394ae951 100644 --- a/src/lib/configUtils.ts +++ b/src/lib/configUtils.ts @@ -135,7 +135,7 @@ async function migrateGitSettingsFromLocalStorage(fs: JSRuntimeFS): Promise { const defaultSettings: GitSettings = { credentials: {}, - corsProxy: 'https://cors.isomorphic-git.org', + corsProxy: 'https://proxy.shakespeare.diy/?url={href}', }; // Try to read from VFS first @@ -169,7 +169,7 @@ export async function readGitSettings(fs: JSRuntimeFS): Promise { if (parsed && typeof parsed === 'object' && 'credentials' in parsed) { return { credentials: parsed.credentials || {}, - corsProxy: parsed.corsProxy || 'https://cors.isomorphic-git.org', + corsProxy: parsed.corsProxy || 'https://proxy.shakespeare.diy/?url={href}', }; } } catch { diff --git a/src/lib/git.test.ts b/src/lib/git.test.ts index 9720d919..e5fb2292 100644 --- a/src/lib/git.test.ts +++ b/src/lib/git.test.ts @@ -66,7 +66,6 @@ describe('Git', () => { expect(mockGit.clone).toHaveBeenCalledWith({ fs, http: expect.any(Object), - corsProxy: 'https://cors.example.com', dir: '/test', url: 'https://github.com/user/repo.git', }); @@ -81,7 +80,6 @@ describe('Git', () => { expect(mockGit.clone).toHaveBeenCalledWith({ fs, http: expect.any(Object), - corsProxy: 'https://cors.example.com', dir: '/test', url: 'https://gitlab.com/user/repo.git', }); @@ -96,7 +94,6 @@ describe('Git', () => { expect(mockGit.clone).toHaveBeenCalledWith({ fs, http: expect.any(Object), - corsProxy: 'https://cors.example.com', dir: '/test', url: 'https://custom.example.com/user/repo.git', }); @@ -111,7 +108,6 @@ describe('Git', () => { expect(mockGit.clone).toHaveBeenCalledWith({ fs, http: expect.any(Object), - corsProxy: 'https://cors.example.com', dir: '/test', url: 'http://github.com/user/repo.git', }); @@ -130,7 +126,6 @@ describe('Git', () => { expect(mockGit.getRemoteInfo).toHaveBeenCalledWith({ http: expect.any(Object), - corsProxy: 'https://cors.example.com', url: 'https://github.com/user/repo.git', }); }); @@ -142,7 +137,6 @@ describe('Git', () => { expect(mockGit.getRemoteInfo).toHaveBeenCalledWith({ http: expect.any(Object), - corsProxy: 'https://cors.example.com', url: 'https://custom.example.com/user/repo.git', }); }); @@ -165,7 +159,6 @@ describe('Git', () => { expect(mockGit.fetch).toHaveBeenCalledWith({ fs, http: expect.any(Object), - corsProxy: 'https://cors.example.com', dir: '/test', remote: 'origin', }); @@ -180,7 +173,6 @@ describe('Git', () => { expect(mockGit.fetch).toHaveBeenCalledWith({ fs, http: expect.any(Object), - corsProxy: 'https://cors.example.com', dir: '/test', url: 'https://gitlab.com/user/repo.git', }); @@ -232,7 +224,6 @@ describe('Git', () => { expect(mockGit.clone).toHaveBeenCalledWith({ fs, http: expect.any(Object), - corsProxy: 'https://cors.example.com', dir: '/test', url: 'https://github.com/user/repo.git', }); diff --git a/src/lib/git.ts b/src/lib/git.ts index 776eb294..bec2dd33 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -1,7 +1,7 @@ -import git from 'isomorphic-git'; -import http from 'isomorphic-git/http/web'; +import git, { GitHttpRequest, GitHttpResponse, HttpClient } from 'isomorphic-git'; import { NIP05 } from '@nostrify/nostrify'; import { nip19 } from 'nostr-tools'; +import UriTemplate from 'uri-templates'; import type { NostrEvent, NostrSigner, NPool } from '@nostrify/nostrify'; import type { JSRuntimeFS } from './JSRuntime'; @@ -25,13 +25,13 @@ interface NostrCloneURI { */ export class Git { private fs: JSRuntimeFS; + private http: HttpClient; private nostr: NPool; - private corsProxy?: string; constructor(options: GitOptions) { this.fs = options.fs; + this.http = new GitHttp(options.corsProxy); this.nostr = options.nostr; - this.corsProxy = options.corsProxy; } // Repository initialization and configuration @@ -56,8 +56,7 @@ export class Git { // Regular Git URL return git.clone({ fs: this.fs, - http, - corsProxy: this.corsProxy, + http: this.http, ...options, }); } @@ -178,8 +177,7 @@ export class Git { } return git.getRemoteInfo({ - http, - corsProxy: this.corsProxy, + http: this.http, ...options, }); } @@ -197,8 +195,7 @@ export class Git { // Regular Git fetch return git.fetch({ fs: this.fs, - http, - corsProxy: this.corsProxy, + http: this.http, ...options, }); } @@ -216,8 +213,7 @@ export class Git { // Regular Git pull return git.pull({ fs: this.fs, - http, - corsProxy: this.corsProxy, + http: this.http, ...options, }); } @@ -237,8 +233,7 @@ export class Git { return git.push({ fs: this.fs, - http, - corsProxy: this.corsProxy, + http: this.http, ...options, }); } @@ -387,8 +382,7 @@ export class Git { async getRemoteInfo2(options: Omit[0], 'http' | 'corsProxy'>) { return git.getRemoteInfo2({ - http, - corsProxy: this.corsProxy, + http: this.http, ...options, }); } @@ -513,8 +507,7 @@ export class Git { async listServerRefs(options: Omit[0], 'http' | 'corsProxy'>) { return git.listServerRefs({ - http, - corsProxy: this.corsProxy, + http: this.http, ...options, }); } @@ -618,8 +611,7 @@ export class Git { // Try to get remote info from the first clone URL try { const gitRemoteInfo = await git.getRemoteInfo({ - http, - corsProxy: this.corsProxy, + http: this.http, url: cloneUrls[0], }); @@ -1016,8 +1008,7 @@ export class Git { // Get remote repository information to check if it has the expected commit const remoteInfo = await Promise.race([ git.getRemoteInfo({ - http, - corsProxy: this.corsProxy, + http: this.http, url: cloneUrl, }), new Promise((_, reject) => @@ -1035,8 +1026,7 @@ export class Git { await Promise.race([ git.clone({ fs: this.fs, - http, - corsProxy: this.corsProxy, + http: this.http, ...options, url: cloneUrl, }), @@ -1093,8 +1083,7 @@ export class Git { await Promise.race([ git.clone({ fs: this.fs, - http, - corsProxy: this.corsProxy, + http: this.http, ...options, url: cloneUrl, }), @@ -1456,8 +1445,7 @@ export class Git { // Push to the temporary remote await git.push({ fs: this.fs, - http, - corsProxy: this.corsProxy, + http: this.http, dir, url: cloneUrl, ...gitPushOptions, @@ -1510,8 +1498,7 @@ export class Git { // Fetch from the clone URL await git.fetch({ fs: this.fs, - http, - corsProxy: this.corsProxy, + http: this.http, dir, url: cloneUrl, }); @@ -1588,8 +1575,7 @@ export class Git { // Fetch from the temporary remote const fetchResult = await git.fetch({ fs: this.fs, - http, - corsProxy: this.corsProxy, + http: this.http, dir, remote: tempRemoteName, }); @@ -1626,4 +1612,100 @@ export class Git { throw lastError || new Error('All fetch attempts failed'); } -} \ No newline at end of file +} + +class GitHttp implements HttpClient { + private proxy?: string; + + constructor(proxy?: string) { + this.proxy = proxy; + } + + async request(request: GitHttpRequest): Promise { + const method = request.method ?? "GET"; + const url = new URL(request.url); + + const target = this.proxy + ? UriTemplate(this.proxy).fill({ + href: url.href, + origin: url.origin, + protocol: url.protocol, + username: url.username, + password: url.password, + host: url.host, + hostname: url.hostname, + port: url.port, + pathname: url.pathname, + hash: url.hash, + search: url.search, + }) + : url.href; + + const init: RequestInit = { + method, + headers: request.headers, + }; + + if (request.body) { + const buffered = await collectToUint8Array(request.body); + init.body = new Blob([new Uint8Array(buffered)]); + } + + const response = await fetch(target, init); + const headers = Object.fromEntries(response.headers.entries()); + const body = response.body ? readableStreamToAsyncIterator(response.body) : undefined; + + return { + url: response.url, + method, + statusCode: response.status, + statusMessage: response.statusText, + body, + headers, + }; + } +} + +// Drain an async iterable of Uint8Array into one Uint8Array +async function collectToUint8Array( + src: AsyncIterable | AsyncIterableIterator +): Promise { + const chunks: Uint8Array[] = []; + let total = 0; + for await (const chunk of src) { + chunks.push(chunk); + total += chunk.byteLength; + } + const out = new Uint8Array(total); + let offset = 0; + for (const c of chunks) { + out.set(c, offset); + offset += c.byteLength; + } + return out; +} + +// Portable async iterator over a ReadableStream (works even if +// ReadableStream doesn't implement [Symbol.asyncIterator]) +function readableStreamToAsyncIterator( + stream: ReadableStream +): AsyncIterableIterator { + const reader = stream.getReader(); + return { + async next() { + const { value, done } = await reader.read(); + return done ? { value: undefined, done: true } : { value, done: false }; + }, + async return() { + try { + await reader.cancel(); + } catch { + // Ignore cancel errors + } + return { value: undefined, done: true }; + }, + [Symbol.asyncIterator]() { + return this; + }, + }; +} diff --git a/src/pages/GitSettings.tsx b/src/pages/GitSettings.tsx index 303417ad..7c047bc0 100644 --- a/src/pages/GitSettings.tsx +++ b/src/pages/GitSettings.tsx @@ -382,18 +382,16 @@ export function GitSettings() {

{t('corsProxy')}

-
-
- updateSettings({ corsProxy: e.target.value })} - /> -

- {t('corsProxyDescription')} -

-
+
+ updateSettings({ corsProxy: e.target.value })} + /> +

+ {t('corsProxyDescription')} +