resolveAnnounced

tcmts.cli.announce.resolveAnnounced · #3u5fhgb7 · term · runs anywhere

An announced release: what (geo@1.2.0, or geo for its highest version) among those who (a handle, checked both ways, or a DID) has announced, as their repository signs it (signedRecord: what a PDS lists isn't taken on its word).

import { NotTheirs, resolveIdentity, signedRecord } from "#codebase/tcmts/atproto";
import type { Identity } from "#codebase/tcmts/atproto";
import { announcedReleases } from "#codebase/tcmts/cli/announce";
import { UsageError } from "#codebase/tcmts/cli/ui";
import { IR_VERSION } from "#codebase/tcmts/core/hash";
import { compareVersions } from "#codebase/tcmts/core/semver";
import { parseReleaseRecord, recordKey, RELEASE_COLLECTION } from "#codebase/tcmts/records";
import type { ReleaseRecord } from "#codebase/tcmts/records";

export async function resolveAnnounced(
  who: string,
  what: string,
): Promise<{ id: Identity; record: ReleaseRecord }> {
  const id = await resolveIdentity(who);
  const at = what.lastIndexOf("@");
  const pkg = at > 0 ? what.slice(0, at) : what;
  const version = at > 0 ? what.slice(at + 1) : undefined;
  const by = id.handle ? `@${id.handle}` : id.did;
  const all = (await announcedReleases(id))
    .filter((r) => r.package === pkg)
    .sort((a, b) => compareVersions(b.version, a.version));
  const listed = version === undefined ? all[0] : all.find((r) => r.version === version);
  if (!listed)
    throw new UsageError(
      all.length
        ? `${by} hasn't announced ${pkg}@${version}; they've announced ${all.map((r) => r.version).join(", ")}`
        : `${by} hasn't announced any releases of ${pkg}`,
    );
  let record: ReleaseRecord;
  try {
    const signed = parseReleaseRecord(
      (await signedRecord(id.did, RELEASE_COLLECTION, recordKey(listed))).record,
    );
    if (!signed || recordKey(signed) !== recordKey(listed))
      throw new NotTheirs("it isn't that release's record");
    record = signed;
  } catch (e) {
    if (!(e instanceof NotTheirs)) throw e;
    throw new UsageError(
      `${by}'s announcement of ${pkg}@${listed.version} doesn't check against their signed repository: ${e.message}`,
    );
  }
  if (record.hashFormat !== IR_VERSION)
    throw new UsageError(
      `${by}'s ${pkg}@${record.version} is announced in hash format ${record.hashFormat}, and this tcm's is ${IR_VERSION}: ${record.hashFormat > IR_VERSION ? "a newer tcm reads it" : "its author can announce it again from their migrated codebase"}`,
    );
  if (!record.sources.length)
    throw new UsageError(`${by}'s ${pkg}@${record.version} names nowhere to fetch it from`);
  return { id, record };
}

uses

used by