/** * Finding packages on the network, for `zena pkg add` and `zena pkg update` * (docs/design/package-manager.md, "Names and scopes"): turning what a * person typed into a DID, listing a package's versions, and reading its * profile and releases with their proofs. */ import {HashMap, HashSet, OrderedHashMap} from 'zena:collections'; import {DidDocument} from 'atproto/did'; import {IdentityResolver} from 'atproto/identity'; import {getVerifiedRecord, listRecords} from 'atproto/sync'; import {isValidDid, isValidHandle} from 'atproto/syntax'; import { PROFILE_KEY, PROFILE_TYPE, Profile, RELEASE_TYPE, Release, RETRACTION_TYPE, isValidLocalName } from './records.zena'; import {CheckedRelease, PackageSource, Versions} from './resolve.zena'; import {Version} from 'zena-lang:semver'; /** The domain `scope:name` is looked up in when no `scopes` entry names one. */ export let DEFAULT_DOMAIN = 'pkg.zena-lang.dev'; /** A package that cannot be found as asked. */ export class RegistryError extends Error { new(message: String) : super(message); } /** Whether `identifier`, a package as `zena pkg add` is given one, is `scope:name`: one `:`, and not a DID. */ export function isScopedIdentifier(identifier: String): boolean { return !identifier.startsWith('did:') && identifier.contains(':'); } /** * The handle `scope:name` stands for: `name.`, where the domain * is the one `scopes` maps `scope` to, or `scope.`. */ export function scopedHandle(identifier: String, scopes: OrderedHashMap, defaultDomain: String = DEFAULT_DOMAIN): String { let parts = identifier.split(':'); if (parts.length != 2 || parts[0] == '' || parts[1] == '') { throw new RegistryError(`'${identifier}' is not scope:name`); } let scope = parts[0]; let name = parts[1]; if (let (true, domain) = scopes.get(scope)) { return `${name}.${domain}`; } return `${name}.${scope}.${defaultDomain}`; } /** * The local name `zena pkg add` gives the package whose handle is * `handle` when it is not told one, the inverse of `scopedHandle`: * `:` for a name in a scope on the domain `scopes` maps * `` to (`.`) or on `defaultDomain` * (`..`); null for any other handle * (docs/design/import-names.md, "Names are still local"). */ export function scopedName(handle: String, scopes: OrderedHashMap, defaultDomain: String = DEFAULT_DOMAIN): String | null { let lower = handle.asciiLowerCase(); var found: String | null = null; for (let scope in scopes.keys()) { let suffix = `.${scopes[scope].asciiLowerCase()}`; if (found == null && lower.endsWith(suffix)) { found = `${scope}:${lower.sliceBytes(0, lower.length - suffix.length)}`; } } if (found == null) { let suffix = `.${defaultDomain.asciiLowerCase()}`; if (lower.endsWith(suffix)) { let labels = lower.sliceBytes(0, lower.length - suffix.length).split('.'); if (labels.length == 2) { found = `${labels[1]}:${labels[0]}`; } } } return if (found != null && isValidLocalName(found)) found else null; } /** * The DID that `identifier` names: a DID as it is, `scope:name` by the * handle it stands for, and a handle by resolving it and checking that the * DID's document claims it back. */ export async function resolveIdentifier(identifier: String, scopes: OrderedHashMap, resolver: IdentityResolver, defaultDomain: String = DEFAULT_DOMAIN): Future { if (identifier.startsWith('did:')) { if (!isValidDid(identifier)) { throw new RegistryError(`'${identifier}' is not a DID`); } return identifier; } let handle = if (isScopedIdentifier(identifier)) scopedHandle(identifier, scopes, defaultDomain) else identifier; if (!isValidHandle(handle)) { throw new RegistryError(`'${identifier}' is not a DID, a handle or scope:name`); } let identity = await resolver.resolve(handle); return identity.did; } /** * The versions the package `document` describes lists. The list is its * PDS's word: whatever is chosen from it is fetched again with its proof. * Record keys that are not versions are skipped. */ export async function listVersions(document: DidDocument): Future { let releases = new GrowableArray(); for (let record in await listRecords(document, RELEASE_TYPE)) { try { releases.push(Version.parse(record.rkey)); } catch (e) { // Not a version: no client publishes such a record. } } let retracted = new HashSet(); for (let record in await listRecords(document, RETRACTION_TYPE)) { retracted.add(record.rkey); } return new Versions(releases, retracted); } /** The package's profile, with its proof, or null when it has none. */ export async function fetchProfile(document: DidDocument): Future { let proof = await getVerifiedRecord(document, PROFILE_TYPE, PROFILE_KEY); let record = proof.record; if (record == null) { return null; } return Profile.fromValue(record); } /** * A `PackageSource` that reads each package's versions and releases from * its PDS, releases with their proofs, resolving each DID once. */ export final class NetworkSource implements PackageSource { #resolver: IdentityResolver; #documents = new HashMap(); new(resolver: IdentityResolver) : #resolver = resolver; async versions(did: String): Future { return await listVersions(await this.#document(did)); } async release(did: String, version: Version): Future { let document = await this.#document(did); let text = version.toString(); let proof = await getVerifiedRecord(document, RELEASE_TYPE, text); let record = proof.record; let cid = proof.cid; if (record == null || cid == null) { throw new RegistryError(`${did} has no release ${text}`); } return new CheckedRelease(Release.fromValue(record, text), cid); } async #document(did: String): Future { if (let (true, known) = this.#documents.get(did)) { return known; } let resolved = await this.#resolver.resolveDid(did); this.#documents[did] = resolved; return resolved; } }