mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Use new corsProxy format
This commit is contained in:
10 files changed
+176
-62
No files matched your search
@@ -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'));
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -185,7 +185,7 @@ describe('configUtils', () => {
|
||||
|
||||
expect(result).toEqual({
|
||||
credentials: {},
|
||||
corsProxy: 'https://cors.isomorphic-git.org',
|
||||
corsProxy: 'https://proxy.shakespeare.diy/?url={href}',
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
@@ -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
@@ -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>
|
||||
|
||||
Reference in new issue
Block a user