From d3e257f75997c7c956aba5f097bcf01aa0129079 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 21 Sep 2025 14:45:27 -0500 Subject: [PATCH] Transfer Nostr clone logic into Git class --- src/lib/ProjectsManager.ts | 2 +- src/lib/git.ts | 155 ++++++++++++++++++++++++++++ src/pages/Clone.tsx | 204 ++++++------------------------------- 3 files changed, 188 insertions(+), 173 deletions(-) diff --git a/src/lib/ProjectsManager.ts b/src/lib/ProjectsManager.ts index 014ac6d4..07dddad6 100644 --- a/src/lib/ProjectsManager.ts +++ b/src/lib/ProjectsManager.ts @@ -107,7 +107,7 @@ export class ProjectsManager { await this.fs.mkdir(projectPath); - // Clone the repository + // Clone the repository (Git class now handles Nostr URIs automatically) await this.git.clone({ dir: projectPath, url: repoUrl, diff --git a/src/lib/git.ts b/src/lib/git.ts index 8e2dec5f..ae8debfb 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -1,5 +1,7 @@ import git from 'isomorphic-git'; import http from 'isomorphic-git/http/web'; +import { NIP05 } from '@nostrify/nostrify'; +import { nip19 } from 'nostr-tools'; import type { NPool } from '@nostrify/nostrify'; import type { JSRuntimeFS } from './JSRuntime'; @@ -9,6 +11,12 @@ export interface GitOptions { corsProxy?: string; } +interface NostrCloneURI { + pubkey: string; + d: string; + relay?: string; +} + /** * Git class that wraps isomorphic-git and provides a cleaner interface. * Instantiate with an fs implementation, optional corsProxy string, and optional corsProxyRegex. @@ -35,6 +43,21 @@ export class Git { } async clone(options: Omit[0], 'fs' | 'http' | 'corsProxy'>) { + // Check if the URL is a Nostr URI + if (options.url.startsWith('nostr://')) { + const nostrUri = await this.parseNostrCloneURI(options.url); + if (!nostrUri) { + throw new Error('Invalid Nostr clone URI format'); + } + + // Fetch clone URLs from Nostr + const cloneUrls = await this.fetchNostrRepository(nostrUri); + + // Try cloning from each URL in order + return this.tryCloneUrls(cloneUrls, options); + } + + // Regular Git URL return git.clone({ fs: this.fs, http, @@ -502,4 +525,136 @@ export class Git { version() { return git.version(); } + + // Nostr-specific helper methods + private async parseNostrCloneURI(uri: string): Promise { + try { + if (!uri.startsWith('nostr://')) { + return null; + } + + const path = uri.slice(8); // Remove 'nostr://' prefix + const parts = path.split('/'); + + if (parts.length < 2) { + return null; + } + + const identifier = parts[0]; + let pubkey: string; + + // Try to decode as npub first, otherwise assume it's already a hex pubkey + try { + const decoded = nip19.decode(identifier); + if (decoded.type === 'npub') { + pubkey = decoded.data; + } else { + return null; + } + } catch { + // If decoding fails, check if it's a NIP-05 identifier + if (identifier.split('@').length === 2) { + const pointer = await NIP05.lookup(identifier, { + signal: AbortSignal.timeout(3000), + }); + pubkey = pointer.pubkey; + } else { + return null; + } + } + + // Check if we have relay and d-tag or just d-tag + if (parts.length === 3) { + // Format: nostr://npub/relay/d + return { + pubkey, + relay: parts[1], + d: parts[2], + }; + } else if (parts.length === 2) { + // Format: nostr://npub/d + return { + pubkey, + d: parts[1], + }; + } + + return null; + } catch { + return null; + } + } + + private async fetchNostrRepository(nostrURI: NostrCloneURI): Promise { + const signal = AbortSignal.timeout(10000); // 10 second timeout + + // Build the filter for the NIP-34 repository announcement + const filter = { + kinds: [30617], + authors: [nostrURI.pubkey], + '#d': [nostrURI.d], + limit: 1, + }; + + // Use a specific relay if provided + const client = nostrURI.relay + ? this.nostr.relay(nostrURI.relay) + : this.nostr.group(['wss://relay.ngit.dev/', 'wss://gitnostr.com/']); + + // Query for the repository announcement event + const events = await client.query([filter], { signal }); + + if (events.length === 0) { + throw new Error('Repository not found on Nostr network'); + } + + const repoEvent = events[0]; + + // Extract clone URLs from the event tags + const cloneUrls: string[] = []; + for (const tag of repoEvent.tags) { + if (tag[0] === 'clone' && tag[1]) { + cloneUrls.push(tag[1]); + } + } + + if (cloneUrls.length === 0) { + throw new Error('No clone URLs found in repository announcement'); + } + + return cloneUrls; + } + + private async tryCloneUrls(cloneUrls: string[], originalOptions: Omit[0], 'fs' | 'http' | 'corsProxy'>): Promise { + let lastError: Error | null = null; + + for (const cloneUrl of cloneUrls) { + try { + console.log(`Attempting to clone from: ${cloneUrl}`); + + // Try cloning with a timeout + await Promise.race([ + git.clone({ + fs: this.fs, + http, + corsProxy: this.corsProxy, + ...originalOptions, + url: cloneUrl, // Override the URL with the actual Git URL + }), + new Promise((_, reject) => + setTimeout(() => reject(new Error('Clone timeout')), 30000) + ) + ]); + + return; // Success! + } catch (error) { + console.warn(`Failed to clone from ${cloneUrl}:`, error); + lastError = error instanceof Error ? error : new Error('Unknown error'); + continue; // Try the next URL + } + } + + // If we get here, all clone attempts failed + throw lastError || new Error('All clone attempts failed'); + } } \ No newline at end of file diff --git a/src/pages/Clone.tsx b/src/pages/Clone.tsx index 892b3261..49142e96 100644 --- a/src/pages/Clone.tsx +++ b/src/pages/Clone.tsx @@ -7,19 +7,9 @@ import { Input } from '@/components/ui/input'; import { GitBranch, Loader2, AlertCircle } from 'lucide-react'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { useToast } from '@/hooks/useToast'; -import { NIP05 } from '@nostrify/nostrify'; -import { useNostr } from '@nostrify/react'; -import { nip19 } from 'nostr-tools'; -import type { Project } from '@/lib/ProjectsManager'; import { useSeoMeta } from '@unhead/react'; -interface NostrCloneUri { - pubkey: string; - dTag: string; - relay?: string; -} - export default function Clone() { const { t } = useTranslation(); const [repoUrl, setRepoUrl] = useState(''); @@ -29,7 +19,6 @@ export default function Clone() { const [searchParams] = useSearchParams(); const projectsManager = useProjectsManager(); const { toast } = useToast(); - const { nostr } = useNostr(); useSeoMeta({ title: `${t('importRepository')} - Shakespeare`, @@ -38,9 +27,20 @@ export default function Clone() { const extractRepoName = (url: string): string => { try { - // Handle different URL formats const cleanUrl = url.trim(); + // Handle Nostr clone URIs + if (cleanUrl.startsWith('nostr://')) { + const path = cleanUrl.slice(8); // Remove 'nostr://' prefix + const parts = path.split('/'); + + // Return the d-tag (last part) as the repo name + if (parts.length >= 2) { + return parts[parts.length - 1] || 'nostr-repo'; + } + return 'nostr-repo'; + } + // Extract from GitHub URLs, GitLab URLs, etc. const match = cleanUrl.match(/\/([^/]+?)(?:\.git)?(?:\/)?$/); if (match && match[1]) { @@ -56,70 +56,12 @@ export default function Clone() { } }; - const parseNostrCloneUri = async (uri: string): Promise => { - try { - if (!uri.startsWith('nostr://')) { - return null; - } - - const path = uri.slice(8); // Remove 'nostr://' prefix - const parts = path.split('/'); - - if (parts.length < 2) { - return null; - } - - const identifier = parts[0]; - let pubkey: string; - - // Try to decode as npub first, otherwise assume it's already a hex pubkey - try { - const decoded = nip19.decode(identifier); - if (decoded.type === 'npub') { - pubkey = decoded.data; - } else { - return null; - } - } catch { - // If decoding fails, check if it's a NIP-05 identifier - if (identifier.split('@').length === 2) { - const pointer = await NIP05.lookup(identifier, { - signal: AbortSignal.timeout(3000), - }); - pubkey = pointer.pubkey; - } else { - return null; - } - } - - // Check if we have relay and d-tag or just d-tag - if (parts.length === 3) { - // Format: nostr://npub/relay/dtag - return { - pubkey, - relay: parts[1], - dTag: parts[2], - }; - } else if (parts.length === 2) { - // Format: nostr://npub/dtag - return { - pubkey, - dTag: parts[1], - }; - } - - return null; - } catch { - return null; - } - }; - const validateGitUrl = useCallback((url: string): boolean => { if (!url.trim()) return false; // Check if it's a Nostr clone URI if (url.startsWith('nostr://')) { - return parseNostrCloneUri(url) !== null; + return true; // Git class will handle validation } // Basic URL validation @@ -134,73 +76,6 @@ export default function Clone() { return gitUrlPattern.test(url) || url.includes('github.com') || url.includes('gitlab.com'); }, []); - const fetchNostrRepository = useCallback(async (nostrUri: NostrCloneUri): Promise => { - const signal = AbortSignal.timeout(10000); // 10 second timeout - - // Build the filter for the NIP-34 repository announcement - const filter = { - kinds: [30617], - authors: [nostrUri.pubkey], - '#d': [nostrUri.dTag], - limit: 1, - }; - - // Use a specific relay if provided - const client = nostrUri.relay - ? nostr.relay(nostrUri.relay) - : nostr.group(['wss://relay.ngit.dev/', 'wss://gitnostr.com/']); - - // Query for the repository announcement event - const events = await client.query([filter], { signal }); - - if (events.length === 0) { - throw new Error('Repository not found on Nostr network'); - } - - const repoEvent = events[0]; - - // Extract clone URLs from the event tags - const cloneUrls: string[] = []; - for (const tag of repoEvent.tags) { - if (tag[0] === 'clone' && tag[1]) { - cloneUrls.push(tag[1]); - } - } - - if (cloneUrls.length === 0) { - throw new Error('No clone URLs found in repository announcement'); - } - - return cloneUrls; - }, [nostr]); - - const tryCloneUrls = useCallback(async (cloneUrls: string[], repoName: string): Promise => { - let lastError: Error | null = null; - - for (const cloneUrl of cloneUrls) { - try { - console.log(`Attempting to clone from: ${cloneUrl}`); - - // Try cloning with a timeout - const project = await Promise.race([ - projectsManager.cloneProject(repoName, cloneUrl), - new Promise((_, reject) => - setTimeout(() => reject(new Error('Clone timeout')), 30000) - ) - ]); - - return project; // Success! - } catch (error) { - console.warn(`Failed to clone from ${cloneUrl}:`, error); - lastError = error instanceof Error ? error : new Error('Unknown error'); - continue; // Try the next URL - } - } - - // If we get here, all clone attempts failed - throw lastError || new Error('All clone attempts failed'); - }, [projectsManager]); - const handleClone = useCallback(async (url?: string) => { const targetUrl = url || repoUrl; @@ -220,43 +95,28 @@ export default function Clone() { try { await projectsManager.init(); - // Check if it's a Nostr clone URI - const nostrUri = await parseNostrCloneUri(targetUrl.trim()); + // Extract repository name + const repoName = extractRepoName(targetUrl); - if (nostrUri) { - // Handle Nostr clone URI - console.log('Parsing Nostr clone URI:', nostrUri); + // Clone the repository (Git class handles both regular Git URLs and Nostr URIs) + const project = await projectsManager.cloneProject(repoName, targetUrl.trim()); - // Fetch the repository announcement from Nostr - const cloneUrls = await fetchNostrRepository(nostrUri); - console.log('Found clone URLs:', cloneUrls); + // Determine success message based on URL type + const isNostrUri = targetUrl.startsWith('nostr://'); + const successTitle = isNostrUri + ? t('nostrRepositoryImportedSuccessfully') + : t('repositoryImportedSuccessfully'); + const successDescription = isNostrUri + ? t('repositoryClonedFromNostr', { repoName }) + : t('repositoryClonedReady', { repoName }); - // Extract repository name from d-tag - const repoName = nostrUri.dTag; + toast({ + title: successTitle, + description: successDescription, + }); - // Try cloning from each URL in order - const project = await tryCloneUrls(cloneUrls, repoName); - - toast({ - title: t('nostrRepositoryImportedSuccessfully'), - description: t('repositoryClonedFromNostr', { repoName }), - }); - - // Navigate to the new project - navigate(`/project/${project.id}`); - } else { - // Handle regular Git URL - const repoName = extractRepoName(targetUrl); - const project = await projectsManager.cloneProject(repoName, targetUrl.trim()); - - toast({ - title: t('repositoryImportedSuccessfully'), - description: t('repositoryClonedReady', { repoName }), - }); - - // Navigate to the new project - navigate(`/project/${project.id}`); - } + // Navigate to the new project + navigate(`/project/${project.id}`); } catch (error) { console.error('Failed to clone repository:', error); @@ -289,7 +149,7 @@ export default function Clone() { } finally { setIsCloning(false); } - }, [fetchNostrRepository, navigate, projectsManager, repoUrl, t, toast, tryCloneUrls, validateGitUrl]); + }, [navigate, projectsManager, repoUrl, t, toast, validateGitUrl]); const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !isCloning) {