parseReleaseRecord

tcmts.records.parseReleaseRecord · #bvia2xqz · term · runs anywhere

A release record as read: what it says, if it's one (every field of the lexicon, of its type; the package an npm package name, the version a semver one, the namespace a hash), else undefined.

import { RELEASE_COLLECTION } from "#codebase/tcmts/records";
import type { ReleaseRecord } from "#codebase/tcmts/records";

export function parseReleaseRecord(v: unknown): ReleaseRecord | undefined {
  if (!v || typeof v !== "object") return undefined;
  const r = v as Record<string, unknown>;
  const strings = ["package", "version", "namespace", "entry", "notes", "createdAt"] as const;
  if (r.$type !== RELEASE_COLLECTION || strings.some((k) => typeof r[k] !== "string"))
    return undefined;
  if (typeof r.hashFormat !== "number" || !Array.isArray(r.sources)) return undefined;
  // (names a command, a path and a page are made of: nothing else in them)
  const pkg = r.package as string;
  if (pkg.length > 214 || !/^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/.test(pkg))
    return undefined;
  if (
    !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(r.version as string) ||
    (r.version as string).length > 64
  )
    return undefined;
  if (!/^[a-z0-9]{8,128}$/.test(r.namespace as string)) return undefined;
  const sources = r.sources.filter(
    (s): s is string => typeof s === "string" && /^https?:\/\//.test(s),
  );
  return {
    $type: RELEASE_COLLECTION,
    package: r.package as string,
    version: r.version as string,
    namespace: r.namespace as string,
    entry: r.entry as string,
    hashFormat: r.hashFormat,
    notes: r.notes as string,
    sources,
    createdAt: r.createdAt as string,
  };
}

uses

used by