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'; export interface GitOptions { fs: JSRuntimeFS; nostr: NPool; 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. * All methods have the same names as isomorphic-git but don't require * passing fs, http, and corsProxy each time. */ export class Git { private fs: JSRuntimeFS; private nostr: NPool; private corsProxy?: string; constructor(options: GitOptions) { this.fs = options.fs; this.nostr = options.nostr; this.corsProxy = options.corsProxy; } // Repository initialization and configuration async init(options: Omit[0], 'fs'>) { return git.init({ fs: this.fs, ...options, }); } 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, corsProxy: this.corsProxy, ...options, }); } // Working with files async add(options: Omit[0], 'fs'>) { return git.add({ fs: this.fs, ...options, }); } async remove(options: Omit[0], 'fs'>) { return git.remove({ fs: this.fs, ...options, }); } async status(options: Omit[0], 'fs'>) { return git.status({ fs: this.fs, ...options, }); } async statusMatrix(options: Omit[0], 'fs'>) { return git.statusMatrix({ fs: this.fs, ...options, }); } // Commits and history async commit(options: Omit[0], 'fs'>) { return git.commit({ fs: this.fs, ...options, }); } async log(options: Omit[0], 'fs'>) { return git.log({ fs: this.fs, ...options, }); } async readCommit(options: Omit[0], 'fs'>) { return git.readCommit({ fs: this.fs, ...options, }); } // Branches async branch(options: Omit[0], 'fs'>) { return git.branch({ fs: this.fs, ...options, }); } async checkout(options: Omit[0], 'fs'>) { return git.checkout({ fs: this.fs, ...options, }); } async currentBranch(options: Omit[0], 'fs'>) { return git.currentBranch({ fs: this.fs, ...options, }); } async deleteBranch(options: Omit[0], 'fs'>) { return git.deleteBranch({ fs: this.fs, ...options, }); } async listBranches(options: Omit[0], 'fs'>) { return git.listBranches({ fs: this.fs, ...options, }); } // Remote operations async addRemote(options: Omit[0], 'fs'>) { return git.addRemote({ fs: this.fs, ...options, }); } async deleteRemote(options: Omit[0], 'fs'>) { return git.deleteRemote({ fs: this.fs, ...options, }); } async listRemotes(options: Omit[0], 'fs'>) { return git.listRemotes({ fs: this.fs, ...options, }); } async getRemoteInfo(options: Omit[0], 'http' | 'corsProxy'>) { return git.getRemoteInfo({ http, corsProxy: this.corsProxy, ...options, }); } async fetch(options: Omit[0], 'fs' | 'http' | 'corsProxy'>) { return git.fetch({ fs: this.fs, http, corsProxy: this.corsProxy, ...options, }); } async pull(options: Omit[0], 'fs' | 'http' | 'corsProxy'>) { return git.pull({ fs: this.fs, http, corsProxy: this.corsProxy, ...options, }); } async push(options: Omit[0], 'fs' | 'http' | 'corsProxy'>) { return git.push({ fs: this.fs, http, corsProxy: this.corsProxy, ...options, }); } // Tags async tag(options: Omit[0], 'fs'>) { return git.tag({ fs: this.fs, ...options, }); } async deleteTag(options: Omit[0], 'fs'>) { return git.deleteTag({ fs: this.fs, ...options, }); } async listTags(options: Omit[0], 'fs'>) { return git.listTags({ fs: this.fs, ...options, }); } // Configuration async getConfig(options: Omit[0], 'fs'>) { return git.getConfig({ fs: this.fs, ...options, }); } async setConfig(options: Omit[0], 'fs'>) { return git.setConfig({ fs: this.fs, ...options, }); } async getConfigAll(options: Omit[0], 'fs'>) { return git.getConfigAll({ fs: this.fs, ...options, }); } // Walking and comparisons async walk(options: Omit[0], 'fs'>) { return git.walk({ fs: this.fs, ...options, }); } // Reset operations async resetIndex(options: Omit[0], 'fs'>) { return git.resetIndex({ fs: this.fs, ...options, }); } // Object operations async readObject(options: Omit[0], 'fs'>) { return git.readObject({ fs: this.fs, ...options, }); } async writeObject(options: Omit[0], 'fs'>) { return git.writeObject({ fs: this.fs, ...options, }); } async readTree(options: Omit[0], 'fs'>) { return git.readTree({ fs: this.fs, ...options, }); } async writeTree(options: Omit[0], 'fs'>) { return git.writeTree({ fs: this.fs, ...options, }); } async readBlob(options: Omit[0], 'fs'>) { return git.readBlob({ fs: this.fs, ...options, }); } async writeBlob(options: Omit[0], 'fs'>) { return git.writeBlob({ fs: this.fs, ...options, }); } // Index operations async updateIndex(options: Omit[0], 'fs'>) { return git.updateIndex({ fs: this.fs, ...options, }); } // Merge operations async merge(options: Omit[0], 'fs'>) { return git.merge({ fs: this.fs, ...options, }); } async findMergeBase(options: Omit[0], 'fs'>) { return git.findMergeBase({ fs: this.fs, ...options, }); } // Utilities async isDescendent(options: Omit[0], 'fs'>) { return git.isDescendent({ fs: this.fs, ...options, }); } async findRoot(options: Omit[0], 'fs'>) { return git.findRoot({ fs: this.fs, ...options, }); } async getRemoteInfo2(options: Omit[0], 'http' | 'corsProxy'>) { return git.getRemoteInfo2({ http, corsProxy: this.corsProxy, ...options, }); } async listFiles(options: Omit[0], 'fs'>) { return git.listFiles({ fs: this.fs, ...options, }); } async hashBlob(options: Parameters[0]) { return git.hashBlob(options); } async annotatedTag(options: Omit[0], 'fs'>) { return git.annotatedTag({ fs: this.fs, ...options, }); } async readTag(options: Omit[0], 'fs'>) { return git.readTag({ fs: this.fs, ...options, }); } async writeTag(options: Omit[0], 'fs'>) { return git.writeTag({ fs: this.fs, ...options, }); } async writeRef(options: Omit[0], 'fs'>) { return git.writeRef({ fs: this.fs, ...options, }); } async deleteRef(options: Omit[0], 'fs'>) { return git.deleteRef({ fs: this.fs, ...options, }); } async listNotes(options: Omit[0], 'fs'>) { return git.listNotes({ fs: this.fs, ...options, }); } async readNote(options: Omit[0], 'fs'>) { return git.readNote({ fs: this.fs, ...options, }); } async addNote(options: Omit[0], 'fs'>) { return git.addNote({ fs: this.fs, ...options, }); } async removeNote(options: Omit[0], 'fs'>) { return git.removeNote({ fs: this.fs, ...options, }); } // Packfile operations async packObjects(options: Omit[0], 'fs'>) { return git.packObjects({ fs: this.fs, ...options, }); } async indexPack(options: Omit[0], 'fs'>) { return git.indexPack({ fs: this.fs, ...options, }); } // Additional utility methods async resolveRef(options: Omit[0], 'fs'>) { return git.resolveRef({ fs: this.fs, ...options, }); } async expandRef(options: Omit[0], 'fs'>) { return git.expandRef({ fs: this.fs, ...options, }); } async expandOid(options: Omit[0], 'fs'>) { return git.expandOid({ fs: this.fs, ...options, }); } async listRefs(options: Omit[0], 'fs'>) { return git.listRefs({ fs: this.fs, ...options, }); } async listServerRefs(options: Omit[0], 'http' | 'corsProxy'>) { return git.listServerRefs({ http, corsProxy: this.corsProxy, ...options, }); } async renameBranch(options: Omit[0], 'fs'>) { return git.renameBranch({ fs: this.fs, ...options, }); } async isIgnored(options: Omit[0], 'fs'>) { return git.isIgnored({ fs: this.fs, ...options, }); } async fastForward(options: Omit[0], 'fs'>) { return git.fastForward({ fs: this.fs, ...options, }); } async abortMerge(options: Omit[0], 'fs'>) { return git.abortMerge({ fs: this.fs, ...options, }); } async writeCommit(options: Omit[0], 'fs'>) { return git.writeCommit({ fs: this.fs, ...options, }); } // Version info 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) ) ]); // After successful clone, update the origin remote to point to the Nostr URI await this.setRemoteURL({ dir: originalOptions.dir, remote: 'origin', url: originalOptions.url, }); 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'); } async setRemoteURL(options: { dir: string; remote: string; url: string }): Promise { // Remove the existing remote await git.deleteRemote({ fs: this.fs, dir: options.dir, remote: options.remote, }); // Add the remote with the new URL await git.addRemote({ fs: this.fs, dir: options.dir, remote: options.remote, url: options.url, }); } }