mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
Don't use corsProxy for Nostr git
This commit is contained in:
2 files changed
+52
-34
No files matched your search
+3
-8
@@ -1,9 +1,8 @@
|
|||||||
import { useCallback, useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { Git } from '@/lib/git';
|
import { Git } from '@/lib/git';
|
||||||
import { useFS } from '@/hooks/useFS';
|
import { useFS } from '@/hooks/useFS';
|
||||||
import { useNostr } from '@nostrify/react';
|
import { useNostr } from '@nostrify/react';
|
||||||
import { useAppContext } from '@/hooks/useAppContext';
|
import { useAppContext } from '@/hooks/useAppContext';
|
||||||
import { findCredentialsForRepo } from '@/lib/gitCredentials';
|
|
||||||
import { useGitSettings } from './useGitSettings';
|
import { useGitSettings } from './useGitSettings';
|
||||||
import { useCurrentUser } from './useCurrentUser';
|
import { useCurrentUser } from './useCurrentUser';
|
||||||
|
|
||||||
@@ -31,20 +30,16 @@ export function useGit(): { git: Git } {
|
|||||||
}).filter((hostname): hostname is string => Boolean(hostname));
|
}).filter((hostname): hostname is string => Boolean(hostname));
|
||||||
}, [graspMetadata.relays]);
|
}, [graspMetadata.relays]);
|
||||||
|
|
||||||
const onAuth = useCallback((url: string) => {
|
|
||||||
return findCredentialsForRepo(url, settings.credentials);
|
|
||||||
}, [settings.credentials]);
|
|
||||||
|
|
||||||
const git = useMemo(() => {
|
const git = useMemo(() => {
|
||||||
return new Git({
|
return new Git({
|
||||||
fs,
|
fs,
|
||||||
nostr,
|
nostr,
|
||||||
corsProxy,
|
corsProxy,
|
||||||
ngitServers,
|
ngitServers,
|
||||||
onAuth,
|
credentials: settings.credentials,
|
||||||
signer: user?.signer,
|
signer: user?.signer,
|
||||||
});
|
});
|
||||||
}, [fs, nostr, corsProxy, ngitServers, onAuth, user?.signer]);
|
}, [fs, nostr, corsProxy, ngitServers, settings.credentials, user?.signer]);
|
||||||
|
|
||||||
return { git };
|
return { git };
|
||||||
}
|
}
|
||||||
+49
-26
@@ -1,10 +1,12 @@
|
|||||||
import git, { AuthCallback, FetchResult, GitHttpRequest, GitHttpResponse, HttpClient } from 'isomorphic-git';
|
import git, { FetchResult, GitHttpRequest, GitHttpResponse, HttpClient } from 'isomorphic-git';
|
||||||
import { proxyUrl } from './proxyUrl';
|
import { proxyUrl } from './proxyUrl';
|
||||||
import { readGitSettings } from './configUtils';
|
import { readGitSettings } from './configUtils';
|
||||||
import { parseRepositoryAnnouncement, RepositoryAnnouncement } from './announceRepository';
|
import { parseRepositoryAnnouncement, RepositoryAnnouncement } from './announceRepository';
|
||||||
import { NostrURI } from './NostrURI';
|
import { NostrURI } from './NostrURI';
|
||||||
import type { NostrEvent, NostrSigner, NPool } from '@nostrify/nostrify';
|
import type { NostrEvent, NostrSigner, NPool } from '@nostrify/nostrify';
|
||||||
import type { JSRuntimeFS } from './JSRuntime';
|
import type { JSRuntimeFS } from './JSRuntime';
|
||||||
|
import type { GitCredential } from '@/contexts/GitSettingsContext';
|
||||||
|
import { findCredentialsForRepo } from './gitCredentials';
|
||||||
|
|
||||||
export interface GitOptions {
|
export interface GitOptions {
|
||||||
fs: JSRuntimeFS;
|
fs: JSRuntimeFS;
|
||||||
@@ -12,8 +14,8 @@ export interface GitOptions {
|
|||||||
corsProxy?: string;
|
corsProxy?: string;
|
||||||
ngitServers?: string[];
|
ngitServers?: string[];
|
||||||
systemAuthor?: { name: string; email: string };
|
systemAuthor?: { name: string; email: string };
|
||||||
onAuth?: AuthCallback;
|
|
||||||
signer?: NostrSigner;
|
signer?: NostrSigner;
|
||||||
|
credentials?: GitCredential[];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,26 +26,47 @@ export interface GitOptions {
|
|||||||
*/
|
*/
|
||||||
export class Git {
|
export class Git {
|
||||||
private fs: JSRuntimeFS;
|
private fs: JSRuntimeFS;
|
||||||
private http: HttpClient;
|
|
||||||
private nostr: NPool;
|
private nostr: NPool;
|
||||||
private ngitServers: string[];
|
private ngitServers: string[];
|
||||||
private systemAuthor: { name: string; email: string };
|
private systemAuthor: { name: string; email: string };
|
||||||
private onAuth?: AuthCallback;
|
|
||||||
private signer?: NostrSigner;
|
private signer?: NostrSigner;
|
||||||
|
private corsProxy?: string;
|
||||||
|
private credentials?: GitCredential[];
|
||||||
|
|
||||||
constructor(options: GitOptions) {
|
constructor(options: GitOptions) {
|
||||||
this.fs = options.fs;
|
this.fs = options.fs;
|
||||||
this.http = new GitHttp(options.corsProxy);
|
|
||||||
this.nostr = options.nostr;
|
this.nostr = options.nostr;
|
||||||
|
this.corsProxy = options.corsProxy;
|
||||||
this.ngitServers = options.ngitServers ?? ['git.shakespeare.diy', 'relay.ngit.dev'];
|
this.ngitServers = options.ngitServers ?? ['git.shakespeare.diy', 'relay.ngit.dev'];
|
||||||
this.systemAuthor = options.systemAuthor ?? {
|
this.systemAuthor = options.systemAuthor ?? {
|
||||||
name: 'shakespeare.diy',
|
name: 'shakespeare.diy',
|
||||||
email: '[email protected]',
|
email: '[email protected]',
|
||||||
};
|
};
|
||||||
this.onAuth = options.onAuth;
|
this.credentials = options.credentials;
|
||||||
this.signer = options.signer;
|
this.signer = options.signer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shared onAuth method
|
||||||
|
private onAuth(url: string) {
|
||||||
|
if (this.credentials) {
|
||||||
|
return findCredentialsForRepo(url, this.credentials);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get HTTP adapter for the given URL
|
||||||
|
private httpForUrl(url: string | null | undefined): GitHttp {
|
||||||
|
if (url?.startsWith('nostr://')) {
|
||||||
|
return new GitHttp();
|
||||||
|
}
|
||||||
|
if (this.credentials && url) {
|
||||||
|
const cred = findCredentialsForRepo(url, this.credentials);
|
||||||
|
if (cred && cred.proxy) {
|
||||||
|
return new GitHttp(this.corsProxy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new GitHttp();
|
||||||
|
}
|
||||||
|
|
||||||
// Repository initialization and configuration
|
// Repository initialization and configuration
|
||||||
async init(options: Omit<Parameters<typeof git.init>[0], 'fs'>) {
|
async init(options: Omit<Parameters<typeof git.init>[0], 'fs'>) {
|
||||||
return git.init({
|
return git.init({
|
||||||
@@ -63,7 +86,7 @@ export class Git {
|
|||||||
// Regular Git URL
|
// Regular Git URL
|
||||||
return git.clone({
|
return git.clone({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(options.url),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
@@ -196,7 +219,7 @@ export class Git {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return git.getRemoteInfo({
|
return git.getRemoteInfo({
|
||||||
http: this.http,
|
http: this.httpForUrl(options.url),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
@@ -206,7 +229,7 @@ export class Git {
|
|||||||
// Check if this is a Nostr repository by looking at the remote URL
|
// Check if this is a Nostr repository by looking at the remote URL
|
||||||
const remote = options.remote || 'origin';
|
const remote = options.remote || 'origin';
|
||||||
const dir = options.dir || '.';
|
const dir = options.dir || '.';
|
||||||
const remoteUrl = await this.getRemoteURL(dir, remote);
|
const remoteUrl = options.url || await this.getRemoteURL(dir, remote);
|
||||||
|
|
||||||
if (remoteUrl && remoteUrl.startsWith('nostr://')) {
|
if (remoteUrl && remoteUrl.startsWith('nostr://')) {
|
||||||
const nostrURI = await NostrURI.parse(remoteUrl);
|
const nostrURI = await NostrURI.parse(remoteUrl);
|
||||||
@@ -216,7 +239,7 @@ export class Git {
|
|||||||
// Regular Git fetch
|
// Regular Git fetch
|
||||||
return git.fetch({
|
return git.fetch({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(remoteUrl),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
@@ -226,7 +249,7 @@ export class Git {
|
|||||||
// Check if this is a Nostr repository by looking at the remote URL
|
// Check if this is a Nostr repository by looking at the remote URL
|
||||||
const remote = options.remote || 'origin';
|
const remote = options.remote || 'origin';
|
||||||
const dir = options.dir || '.';
|
const dir = options.dir || '.';
|
||||||
const remoteUrl = await this.getRemoteURL(dir, remote);
|
const remoteUrl = options.url || await this.getRemoteURL(dir, remote);
|
||||||
const { author } = await this.getGitSettings();
|
const { author } = await this.getGitSettings();
|
||||||
|
|
||||||
if (remoteUrl && remoteUrl.startsWith('nostr://')) {
|
if (remoteUrl && remoteUrl.startsWith('nostr://')) {
|
||||||
@@ -237,7 +260,7 @@ export class Git {
|
|||||||
// Regular Git pull
|
// Regular Git pull
|
||||||
return git.pull({
|
return git.pull({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(remoteUrl),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
author,
|
author,
|
||||||
@@ -248,7 +271,7 @@ export class Git {
|
|||||||
// Check if this is a Nostr repository by looking at the remote URL
|
// Check if this is a Nostr repository by looking at the remote URL
|
||||||
const remote = options.remote || 'origin';
|
const remote = options.remote || 'origin';
|
||||||
const dir = options.dir || '.';
|
const dir = options.dir || '.';
|
||||||
const remoteUrl = await this.getRemoteURL(dir, remote);
|
const remoteUrl = options.url || await this.getRemoteURL(dir, remote);
|
||||||
|
|
||||||
if (remoteUrl && remoteUrl.startsWith('nostr://')) {
|
if (remoteUrl && remoteUrl.startsWith('nostr://')) {
|
||||||
const nostrURI = await NostrURI.parse(remoteUrl);
|
const nostrURI = await NostrURI.parse(remoteUrl);
|
||||||
@@ -257,7 +280,7 @@ export class Git {
|
|||||||
|
|
||||||
return git.push({
|
return git.push({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(remoteUrl),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
@@ -410,7 +433,7 @@ export class Git {
|
|||||||
|
|
||||||
async getRemoteInfo2(options: Omit<Parameters<typeof git.getRemoteInfo2>[0], 'http' | 'corsProxy'>) {
|
async getRemoteInfo2(options: Omit<Parameters<typeof git.getRemoteInfo2>[0], 'http' | 'corsProxy'>) {
|
||||||
return git.getRemoteInfo2({
|
return git.getRemoteInfo2({
|
||||||
http: this.http,
|
http: this.httpForUrl(options.url),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
@@ -536,7 +559,7 @@ export class Git {
|
|||||||
|
|
||||||
async listServerRefs(options: Omit<Parameters<typeof git.listServerRefs>[0], 'http' | 'corsProxy'>) {
|
async listServerRefs(options: Omit<Parameters<typeof git.listServerRefs>[0], 'http' | 'corsProxy'>) {
|
||||||
return git.listServerRefs({
|
return git.listServerRefs({
|
||||||
http: this.http,
|
http: this.httpForUrl(options.url),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
@@ -640,7 +663,7 @@ export class Git {
|
|||||||
// Try to get remote info from the first clone URL
|
// Try to get remote info from the first clone URL
|
||||||
try {
|
try {
|
||||||
const gitRemoteInfo = await git.getRemoteInfo({
|
const gitRemoteInfo = await git.getRemoteInfo({
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
url: repo.httpsCloneUrls[0],
|
url: repo.httpsCloneUrls[0],
|
||||||
});
|
});
|
||||||
@@ -1150,7 +1173,7 @@ export class Git {
|
|||||||
// Get remote repository information
|
// Get remote repository information
|
||||||
const remoteInfo = await Promise.race([
|
const remoteInfo = await Promise.race([
|
||||||
git.getRemoteInfo({
|
git.getRemoteInfo({
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
url: cloneUrl,
|
url: cloneUrl,
|
||||||
}),
|
}),
|
||||||
@@ -1178,7 +1201,7 @@ export class Git {
|
|||||||
await Promise.race([
|
await Promise.race([
|
||||||
git.clone({
|
git.clone({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
url: cloneUrl,
|
url: cloneUrl,
|
||||||
@@ -1236,7 +1259,7 @@ export class Git {
|
|||||||
await Promise.race([
|
await Promise.race([
|
||||||
git.clone({
|
git.clone({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
url: bestCloneUrl,
|
url: bestCloneUrl,
|
||||||
@@ -1264,7 +1287,7 @@ export class Git {
|
|||||||
await Promise.race([
|
await Promise.race([
|
||||||
git.clone({
|
git.clone({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
...options,
|
...options,
|
||||||
url: cloneUrl,
|
url: cloneUrl,
|
||||||
@@ -1503,7 +1526,7 @@ export class Git {
|
|||||||
repo.httpsCloneUrls.map(async (cloneUrl) => {
|
repo.httpsCloneUrls.map(async (cloneUrl) => {
|
||||||
try {
|
try {
|
||||||
const result = await git.getRemoteInfo({
|
const result = await git.getRemoteInfo({
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
url: cloneUrl,
|
url: cloneUrl,
|
||||||
});
|
});
|
||||||
@@ -1962,7 +1985,7 @@ export class Git {
|
|||||||
try {
|
try {
|
||||||
await git.push({
|
await git.push({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
dir,
|
dir,
|
||||||
url: cloneUrl,
|
url: cloneUrl,
|
||||||
@@ -1992,7 +2015,7 @@ export class Git {
|
|||||||
// Fetch from the temporary remote
|
// Fetch from the temporary remote
|
||||||
const fetchResult = await git.fetch({
|
const fetchResult = await git.fetch({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
dir,
|
dir,
|
||||||
tags: true,
|
tags: true,
|
||||||
@@ -2181,7 +2204,7 @@ export class Git {
|
|||||||
try {
|
try {
|
||||||
await git.push({
|
await git.push({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
dir,
|
dir,
|
||||||
url: cloneUrl,
|
url: cloneUrl,
|
||||||
@@ -2202,7 +2225,7 @@ export class Git {
|
|||||||
try {
|
try {
|
||||||
await git.push({
|
await git.push({
|
||||||
fs: this.fs,
|
fs: this.fs,
|
||||||
http: this.http,
|
http: this.httpForUrl(undefined),
|
||||||
onAuth: this.onAuth,
|
onAuth: this.onAuth,
|
||||||
dir,
|
dir,
|
||||||
url: cloneUrl,
|
url: cloneUrl,
|
||||||
|
|||||||
Reference in new issue
Block a user