Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950export interface RepoHandle { did: string; handle: string | null;}
// hydrant's management api, which has no auth so it's only reachable over the tunnelexport interface Repos { // every repo hydrant tracks, in did order, starting after `after` list(after: string | undefined, limit: number): Promise<RepoHandle[]>; // throws when there's no answer, so a flaky read never drops a good handle get(did: string): Promise<string | null>;}
// whatever the did doc claims, nobody checked the domain agrees. that's fine for a// sitemap because a wrong one just 404sfunction handleOf(repo: { handle?: string }): string | null { const handle = repo.handle?.toLowerCase(); return handle && !handle.endsWith('.invalid') ? handle : null;}
export function hydrantRepos(fetcher: Pick<Fetcher, 'fetch'>, base: string): Repos { const read = (path: string, params: Record<string, string> = {}) => { const url = new URL(path, base); for (const [key, value] of Object.entries(params)) url.searchParams.set(key, value); return fetcher.fetch(url, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(10_000) }); }; return { async list(after, limit) { const res = await read('/repos', after ? { limit: String(limit), cursor: after } : { limit: String(limit) }); if (!res.ok) throw new Error(`hydrant /repos: http ${res.status}`); const repos = await res.json<{ did: string; handle?: string }[]>(); return repos.map((repo) => ({ did: repo.did, handle: handleOf(repo) })); }, async get(did) { const res = await read(`/repos/${did}`); if (res.status === 404) return null; if (!res.ok) throw new Error(`hydrant /repos/${did}: http ${res.status}`); return handleOf(await res.json<{ handle?: string }>()); }, };}
export function withHandle(url: string, did: string | null | undefined, handle: string | null | undefined): string { if (!did || !handle) return url; return url .split('/') .map((segment) => (segment === did ? handle : segment)) .join('/');}