Let ngitWebUrl be configurable

This commit is contained in:
Alex Gleason committed 2025-12-23 01:21:56 -06:00
1 parent 43190b7a35
commit b82b4539d9
22 files changed
+194 -12

No files matched your search

+3 -3
View File
@@ -127,7 +127,7 @@ describe('NostrURI', () => {
identifier: testIdentifier,
});
const naddr = uri.toNaddr(30617);
const naddr = uri.toNaddr();
const decoded = nip19.decode(naddr);
expect(decoded.type).toBe('naddr');
@@ -147,7 +147,7 @@ describe('NostrURI', () => {
relay: testRelay,
});
const naddr = uri.toNaddr(30617);
const naddr = uri.toNaddr();
const decoded = nip19.decode(naddr);
expect(decoded.type).toBe('naddr');
@@ -272,7 +272,7 @@ describe('NostrURI', () => {
relay: testRelay,
});
const naddr = uri.toNaddr(30617);
const naddr = uri.toNaddr();
const result = NostrURI.fromNaddr(naddr);
expect(result.pubkey).toBe(uri.pubkey);
+3 -4
View File
@@ -171,23 +171,22 @@ export class NostrURI {
/**
* Convert to an naddr (NIP-19 addressable event identifier)
*
* @param kind - The event kind (default: 30617 for repository announcements)
* @returns An naddr string
*
* @example
* ```ts
* const uri = new NostrURI({ pubkey: 'abc...', identifier: 'my-repo' });
* const naddr = uri.toNaddr(30617);
* const naddr = uri.toNaddr();
* ```
*/
toNaddr(kind: number = 30617): string {
toNaddr(): string {
const data: {
kind: number;
pubkey: string;
identifier: string;
relays?: string[];
} = {
kind,
kind: 30617,
pubkey: this.pubkey,
identifier: this.identifier,
};
+1
View File
@@ -44,6 +44,7 @@ const testConfig: AppConfig = {
esmUrl: 'https://esm.shakespeare.diy',
corsProxy: 'https://proxy.shakespeare.diy/?url={href}',
faviconUrl: 'https://external-content.duckduckgo.com/ip3/{hostname}.ico',
ngitWebUrl: 'https://nostrhub.io/{naddr}',
previewDomain: 'local-shakespeare.dev',
language: 'en',
showcaseEnabled: true,
+1 -1
View File
@@ -1299,7 +1299,7 @@ export class Git {
await this.setConfig({
dir: options.dir,
path: 'nostr.repo',
value: nostrURI.toNaddr(30617),
value: nostrURI.toNaddr(),
});
// Validate the cloned repository against the expected state
+74
View File
@@ -0,0 +1,74 @@
import { describe, it, expect } from 'vitest';
import { ngitWebUrl } from './ngitWebUrl';
import { NostrURI } from './NostrURI';
describe('ngitWebUrl', () => {
it('generates URL with naddr placeholder', () => {
const template = 'https://nostrhub.io/{naddr}';
const pubkey = 'a'.repeat(64); // 64-character hex pubkey
const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI);
expect(result).toMatch(/^https:\/\/nostrhub\.io\/naddr1/);
});
it('generates URL with npub placeholder', () => {
const template = 'https://example.com/{npub}';
const pubkey = 'a'.repeat(64);
const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI);
expect(result).toMatch(/^https:\/\/example\.com\/npub1/);
});
it('generates URL with pubkey placeholder', () => {
const template = 'https://example.com/{pubkey}';
const pubkey = 'a'.repeat(64);
const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI);
expect(result).toBe(`https://example.com/${pubkey}`);
});
it('generates URL with identifier placeholder', () => {
const template = 'https://example.com/{identifier}';
const pubkey = 'a'.repeat(64);
const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI);
expect(result).toBe('https://example.com/my-repo');
});
it('generates URL with multiple placeholders', () => {
const template = 'https://example.com/{npub}/{identifier}';
const pubkey = 'a'.repeat(64);
const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI);
expect(result).toMatch(/^https:\/\/example\.com\/npub1[^/]+\/my-repo$/);
});
it('includes relay in naddr when provided', () => {
const template = 'https://nostrhub.io/{naddr}';
const pubkey = 'a'.repeat(64);
const identifier = 'my-repo';
const relay = 'wss://relay.example.com/';
const nostrURI = new NostrURI({ pubkey, identifier, relay });
const result = ngitWebUrl(template, nostrURI);
// The naddr should be longer when it includes relay information
expect(result).toMatch(/^https:\/\/nostrhub\.io\/naddr1/);
expect(result.length).toBeGreaterThan(50);
});
});
+21
View File
@@ -0,0 +1,21 @@
import UriTemplate from 'uri-templates';
import { nip19 } from 'nostr-tools';
import { NostrURI } from './NostrURI';
/**
* Generate a Nostr Git web URL from a template and Nostr URI
* @param template - URL template with placeholders like {naddr}, {npub}, {pubkey}, {identifier}
* @param nostrURI - The Nostr URI containing pubkey, identifier, and optional relay
* @returns The hydrated web URL
*/
export function ngitWebUrl(template: string, nostrURI: NostrURI): string {
const naddr = nostrURI.toNaddr();
const npub = nip19.npubEncode(nostrURI.pubkey);
return UriTemplate(template).fill({
naddr,
npub,
pubkey: nostrURI.pubkey,
identifier: nostrURI.identifier,
});
}
+1
View File
@@ -31,6 +31,7 @@ const testConfig: AppConfig = {
esmUrl: 'https://esm.shakespeare.diy',
corsProxy: 'https://proxy.shakespeare.diy/?url={href}',
faviconUrl: 'https://external-content.duckduckgo.com/ip3/{hostname}.ico',
ngitWebUrl: 'https://nostrhub.io/{naddr}',
previewDomain: 'local-shakespeare.dev',
language: 'en',
showcaseEnabled: true,