Use new corsProxy format

This commit is contained in:
Alex Gleason committed 2025-09-22 06:04:29 -05:00
1 parent 04d812382b
commit 92376d58b8
10 files changed
+176 -62

No files matched your search

+15
View File
@@ -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",
+2
View File
@@ -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",
+22
View File
@@ -0,0 +1,22 @@
export default {
async fetch(request: Request): Promise<Response> {
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 <https://proxy.shakespeare.diy/>');
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;
}
};
+3 -3
View File
@@ -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'));
+5 -1
View File
@@ -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);
+1 -1
View File
@@ -185,7 +185,7 @@ describe('configUtils', () => {
expect(result).toEqual({
credentials: {},
corsProxy: 'https://cors.isomorphic-git.org',
corsProxy: 'https://proxy.shakespeare.diy/?url={href}',
});
});
+3 -3
View File
@@ -135,7 +135,7 @@ async function migrateGitSettingsFromLocalStorage(fs: JSRuntimeFS): Promise<GitS
if (parsed && typeof parsed === 'object' && 'credentials' in parsed) {
const settings: GitSettings = {
credentials: parsed.credentials || {},
corsProxy: parsed.corsProxy || 'https://cors.isomorphic-git.org',
corsProxy: parsed.corsProxy || 'https://proxy.shakespeare.diy/?url={href}',
};
// Write to VFS and remove from localStorage
@@ -158,7 +158,7 @@ async function migrateGitSettingsFromLocalStorage(fs: JSRuntimeFS): Promise<GitS
export async function readGitSettings(fs: JSRuntimeFS): Promise<GitSettings> {
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<GitSettings> {
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 {
-9
View File
@@ -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',
});
+115 -33
View File
@@ -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<Parameters<typeof git.getRemoteInfo2>[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<Parameters<typeof git.listServerRefs>[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<never>((_, 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');
}
}
}
class GitHttp implements HttpClient {
private proxy?: string;
constructor(proxy?: string) {
this.proxy = proxy;
}
async request(request: GitHttpRequest): Promise<GitHttpResponse> {
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<Uint8Array> | AsyncIterableIterator<Uint8Array>
): Promise<Uint8Array> {
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<Uint8Array>
): AsyncIterableIterator<Uint8Array> {
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;
},
};
}
+10 -12
View File
@@ -382,18 +382,16 @@ export function GitSettings() {
<h4 className="text-sm font-medium">{t('corsProxy')}</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">
{t('corsProxyDescription')}
</p>
</div>
<div className="py-1 space-y-2">
<Input
id="cors-proxy"
placeholder="https://proxy.shakespeare.diy/?url={href}"
value={settings.corsProxy}
onChange={(e) => updateSettings({ corsProxy: e.target.value })}
/>
<p className="text-xs text-muted-foreground">
{t('corsProxyDescription')}
</p>
</div>
</AccordionContent>
</AccordionItem>