releaseNamespace

tcmts.cli.libraries.releaseNamespace · #ex2dgrvf · term · runs anywhere

The namespace hash a release names: its package's namespace in the history entry it was made at. from is a remote, or a store (this codebase), which has the same two lookups.

import type { Causal, NamespaceBody, Release } from "#codebase/tcmts/store";

export async function releaseNamespace(
  from: {
    getCausals(hashes: string[]): Causal[] | Promise<Causal[]>;
    getNamespaces(hashes: string[]): { body: string }[] | Promise<{ body: string }[]>;
  },
  r: Pick<Release, "package" | "version" | "namespace" | "causal">,
  label = "this codebase",
): Promise<string> {
  const [entry] = await from.getCausals([r.causal]);
  if (!entry) throw new Error(`${label} is missing ${r.package}@${r.version}'s history entry`);
  let namespace = entry.namespace;
  for (const seg of r.namespace ? r.namespace.split(".") : []) {
    const [node] = await from.getNamespaces([namespace]);
    const child = node && (JSON.parse(node.body) as NamespaceBody)[2].find(([n]) => n === seg);
    if (!child)
      throw new Error(
        `${r.package}@${r.version}'s namespace ${r.namespace} isn't in its history entry`,
      );
    namespace = child[1];
  }
  return namespace;
}

uses

used by