/** * The package ecosystems we monitor, in one place: names, labels, purl types * and OSV's names for them. Browser-safe. * * Operating-system packages (Debian, Alpine, Ubuntu) are only meaningful with * the distribution release: OSV calls the ecosystem "Debian:12", * "Alpine:v3.20" or "Ubuntu:22.04:LTS". A monitored package stores the * ecosystem as 'Debian' (the CHECK list) and the release, in OSV's form, in * `monitored_projects.metadata.distro_release`. Lookups use * osvEcosystem(ecosystem, metadata); the SBOM puts it in the purl's `distro` * qualifier; the app shows it as "Debian 12". */ import type { DistroEcosystem, Ecosystem } from '@cra-compliance/types'; export interface EcosystemInfo { value: Ecosystem; /** Shown in lists and pickers. */ label: string; /** Who uses it, for the add-package picker ("Ruby", "Dart and Flutter"). */ language: string; /** Two or three letters for an icon badge. */ badge: string; /** Example package name. */ placeholder: string; /** Example version. */ versionPlaceholder: string; /** purl type (https://github.com/package-url/purl-spec). */ purlType: string; /** Operating-system packages: need a distribution release. */ distro: boolean; /** A public registry can confirm the package exists (validate_package). */ registry: boolean; } export const ECOSYSTEMS: readonly EcosystemInfo[] = [ { value: 'npm', label: 'npm', language: 'JavaScript and TypeScript', badge: 'npm', placeholder: 'lodash', versionPlaceholder: '4.17.21', purlType: 'npm', distro: false, registry: true }, { value: 'PyPI', label: 'PyPI', language: 'Python', badge: 'py', placeholder: 'requests', versionPlaceholder: '2.32.3', purlType: 'pypi', distro: false, registry: true }, { value: 'Go', label: 'Go', language: 'Go', badge: 'go', placeholder: 'github.com/gin-gonic/gin', versionPlaceholder: '1.10.0', purlType: 'golang', distro: false, registry: true }, { value: 'crates.io', label: 'crates.io', language: 'Rust', badge: 'rs', placeholder: 'serde', versionPlaceholder: '1.0.210', purlType: 'cargo', distro: false, registry: true }, { value: 'Maven', label: 'Maven', language: 'Java and Kotlin (Maven, Gradle)', badge: 'mvn', placeholder: 'org.apache.logging.log4j:log4j-core', versionPlaceholder: '2.24.1', purlType: 'maven', distro: false, registry: true }, { value: 'Packagist', label: 'Packagist', language: 'PHP (Composer)', badge: 'php', placeholder: 'guzzlehttp/guzzle', versionPlaceholder: '7.9.2', purlType: 'composer', distro: false, registry: true }, { value: 'NuGet', label: 'NuGet', language: '.NET', badge: '.net', placeholder: 'Newtonsoft.Json', versionPlaceholder: '13.0.3', purlType: 'nuget', distro: false, registry: true }, { value: 'wordpress-plugin', label: 'WordPress plugin', language: 'WordPress', badge: 'wp', placeholder: 'contact-form-7', versionPlaceholder: '5.9.8', purlType: 'generic', distro: false, registry: true }, { value: 'RubyGems', label: 'RubyGems', language: 'Ruby', badge: 'rb', placeholder: 'rails', versionPlaceholder: '7.1.3', purlType: 'gem', distro: false, registry: true }, { value: 'Pub', label: 'Pub', language: 'Dart and Flutter', badge: 'dart', placeholder: 'http', versionPlaceholder: '1.2.2', purlType: 'pub', distro: false, registry: true }, { value: 'Hex', label: 'Hex', language: 'Elixir and Erlang', badge: 'ex', placeholder: 'phoenix', versionPlaceholder: '1.7.14', purlType: 'hex', distro: false, registry: true }, { value: 'SwiftURL', label: 'Swift', language: 'Swift (Swift Package Manager)', badge: 'swift', placeholder: 'github.com/apple/swift-nio', versionPlaceholder: '2.72.0', purlType: 'swift', distro: false, registry: true }, { value: 'GitHub Actions', label: 'GitHub Actions', language: 'CI workflows', badge: 'gha', placeholder: 'actions/checkout', versionPlaceholder: '4.2.0', purlType: 'githubactions', distro: false, registry: true }, { value: 'Debian', label: 'Debian', language: 'Debian packages (.deb)', badge: 'deb', placeholder: 'openssl', versionPlaceholder: '3.0.15-1~deb12u1', purlType: 'deb', distro: true, registry: false }, { value: 'Alpine', label: 'Alpine', language: 'Alpine packages (apk)', badge: 'apk', placeholder: 'musl', versionPlaceholder: '1.2.5-r0', purlType: 'apk', distro: true, registry: false }, { value: 'Ubuntu', label: 'Ubuntu', language: 'Ubuntu packages (.deb)', badge: 'ubu', placeholder: 'openssl', versionPlaceholder: '3.0.2-0ubuntu1.18', purlType: 'deb', distro: true, registry: false }, ]; const BY_VALUE = new Map(ECOSYSTEMS.map((e) => [e.value, e])); export const ECOSYSTEM_VALUES = ECOSYSTEMS.map((e) => e.value) as Ecosystem[]; export const DISTRO_ECOSYSTEMS: readonly DistroEcosystem[] = ['Debian', 'Alpine', 'Ubuntu']; export function ecosystemInfo(ecosystem: string): EcosystemInfo | undefined { return BY_VALUE.get(ecosystem); } export function isDistroEcosystem(ecosystem: string): ecosystem is DistroEcosystem { return ecosystem === 'Debian' || ecosystem === 'Alpine' || ecosystem === 'Ubuntu'; } const DEBIAN_CODENAMES: Record = { jessie: '8', stretch: '9', buster: '10', bullseye: '11', bookworm: '12', trixie: '13', forky: '14', }; const UBUNTU_CODENAMES: Record = { xenial: '16.04', bionic: '18.04', focal: '20.04', jammy: '22.04', kinetic: '22.10', lunar: '23.04', mantic: '23.10', noble: '24.04', oracular: '24.10', plucky: '25.04', questing: '25.10', }; /** * A distribution release in OSV's form, from what a person types or a purl's * `distro` qualifier says. Null when it can't be read. * Debian: "12", "12.7", "debian-12.4", "bookworm" -> "12" * Alpine: "3.20", "v3.20", "3.20.3", "alpine-3.20.0" -> "v3.20" * Ubuntu: "22.04", "ubuntu-22.04", "jammy" -> "22.04:LTS" * "23.10" -> "23.10" (not an LTS) */ export function normalizeDistroRelease(ecosystem: string, raw: string | null | undefined): string | null { let s = String(raw ?? '').trim().toLowerCase(); if (!s) return null; s = s.replace(/^(?:debian|alpine|ubuntu)[-:_ ]?/, '').replace(/:lts$/, ''); switch (ecosystem) { case 'Debian': { const code = DEBIAN_CODENAMES[s]; if (code) return code; const m = /^(\d{1,2})(?:\.\d+)*$/.exec(s); return m ? String(Number(m[1])) : null; } case 'Alpine': { const m = /^v?(\d+)\.(\d+)(?:\.\d+)*$/.exec(s); return m ? `v${Number(m[1])}.${Number(m[2])}` : (s === 'edge' ? null : null); } case 'Ubuntu': { const code = UBUNTU_CODENAMES[s]; const v = code ?? s; const m = /^(\d{2})\.(\d{2})(?:\.\d+)?$/.exec(v); if (!m) return null; const year = Number(m[1]); const lts = m[2] === '04' && year % 2 === 0; return `${m[1]}.${m[2]}${lts ? ':LTS' : ''}`; } default: return null; } } /** The release stored with a distro package (metadata.distro_release), or null. */ export function distroRelease(metadata: unknown): string | null { if (!metadata || typeof metadata !== 'object' || Array.isArray(metadata)) return null; const r = (metadata as { distro_release?: unknown }).distro_release; return typeof r === 'string' && r.trim() ? r.trim() : null; } /** * OSV's ecosystem name for a stored package: "Debian:12", "Alpine:v3.20", * "Ubuntu:22.04:LTS" for distro packages with a release, otherwise the * ecosystem itself. (WordPress plugins are mapped by the OSV client.) */ export function osvEcosystem(ecosystem: string, metadata?: unknown): string { if (!isDistroEcosystem(ecosystem)) return ecosystem; const release = distroRelease(metadata); return release ? `${ecosystem}:${release}` : ecosystem; } /** "Debian 12", "Alpine 3.20", "Ubuntu 22.04 LTS". */ export function distroReleaseLabel(ecosystem: string, release: string | null | undefined): string { if (!release) return ecosystem; const [ver, lts] = release.replace(/^v(?=\d)/, '').split(':'); return `${ecosystem} ${ver}${lts === 'LTS' ? ' LTS' : ''}`; } /** The label for a stored package's ecosystem, with the release for distro packages. */ export function ecosystemLabel(ecosystem: string, metadata?: unknown): string { if (isDistroEcosystem(ecosystem)) return distroReleaseLabel(ecosystem, distroRelease(metadata)); return BY_VALUE.get(ecosystem)?.label ?? ecosystem; } /** * The purl `distro` qualifier for a stored distro package: "debian-12", * "alpine-3.20", "ubuntu-22.04". Null when there is no release. */ export function purlDistroQualifier(ecosystem: string, metadata?: unknown): string | null { const release = isDistroEcosystem(ecosystem) ? distroRelease(metadata) : null; if (!release) return null; return `${ecosystem.toLowerCase()}-${release.replace(/^v(?=\d)/, '').split(':')[0]}`; }