Transfer Nostr clone logic into Git class

This commit is contained in:
Alex Gleason committed 2025-09-21 14:45:27 -05:00
1 parent 69c1000063
commit d3e257f759
3 files changed
+188 -173

No files matched your search

+155
View File
@@ -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<Parameters<typeof git.clone>[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<NostrCloneURI | null> {
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<string[]> {
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<Parameters<typeof git.clone>[0], 'fs' | 'http' | 'corsProxy'>): Promise<void> {
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<never>((_, 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');
}
}