libraries

tcmts.hub.libraries · #s2qrsuwn · term · runs on the server

uses tcmts.hub.releases, which uses tcmts.server.table, which uses tcmts.server.Table, which uses #tcm/host (database)

The libraries the hub shows: releases that mirrored (their authors checked), and ones withdrawn since, by package and author, without the versions their authors yanked (yanksOf). One that failed isn't shown as anyone's, and one whose every version is yanked isn't shown, unless yanked (for its own page, and the codebases using it).

import { compareVersions } from "#codebase/tcmts/core/semver";
import {
  latestRelease, libraryKey, releases, shownAttestations, shownRelease, usersByNamespace, yanksOf,
} from "#codebase/tcmts/hub";
import type { HubRelease, Library } from "#codebase/tcmts/hub";

export async function libraries(o: { yanked?: boolean } = {}): Promise<Library[]> {
  const shown = (await releases.find()).map((r) => r.value).filter(shownRelease);
  const yanks = yanksOf(shown, await shownAttestations());
  const byKey = new Map<string, HubRelease[]>();
  for (const r of shown) byKey.set(libraryKey(r), [...(byKey.get(libraryKey(r)) ?? []), r]);
  const users = await usersByNamespace(shown);
  return [...byKey.values()].flatMap((all) => {
    all.sort((a, b) => compareVersions(b.version, a.version));
    const versions = all.filter((v) => !yanks.has(v.uri));
    if (!versions.length && !o.yanked) return [];
    const key = libraryKey(all[0]!);
    // (who uses it: any of its versions, yanked ones too)
    const using = new Set(
      all.flatMap((v) => [...(users.get(v.namespace) ?? [])].map((u) => libraryKey(u))),
    );
    using.delete(key);
    return [
      {
        did: all[0]!.did,
        package: all[0]!.package,
        latest: latestRelease(all, yanks),
        versions,
        yanked: all.filter((v) => yanks.has(v.uri)),
        dependents: using.size,
      },
    ];
  });
}

uses

used by