|
| 1 | +import { XMLParser } from 'fast-xml-parser' |
| 2 | + |
| 3 | +import { ExtractorResult, ProvenanceEntry, RawContact } from '../../types' |
| 4 | +import { fetchText, isEmail, registryHeaders } from '../http' |
| 5 | + |
| 6 | +import { ParsedPurl } from './purl' |
| 7 | + |
| 8 | +const SOURCE = 'maven-pom' |
| 9 | +const BASE = 'https://repo1.maven.org/maven2' |
| 10 | +const parser = new XMLParser({ ignoreAttributes: true }) |
| 11 | + |
| 12 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 13 | + |
| 14 | +function asArray<T>(x: T | T[] | undefined): T[] { |
| 15 | + if (x === undefined || x === null) return [] |
| 16 | + return Array.isArray(x) ? x : [x] |
| 17 | +} |
| 18 | + |
| 19 | +export function mapMavenPom(xml: string, sourceUrl: string, fetchedAt: string): RawContact[] { |
| 20 | + let doc: any |
| 21 | + try { |
| 22 | + doc = parser.parse(xml) |
| 23 | + } catch { |
| 24 | + return [] |
| 25 | + } |
| 26 | + const project = doc?.project |
| 27 | + if (!project) return [] |
| 28 | + |
| 29 | + const prov = (): ProvenanceEntry[] => [ |
| 30 | + { source: SOURCE, sourceTier: 'B', path: sourceUrl, fetchedAt }, |
| 31 | + ] |
| 32 | + const contacts: RawContact[] = [] |
| 33 | + const seen = new Set<string>() |
| 34 | + for (const dev of asArray(project.developers?.developer)) { |
| 35 | + const email = dev?.email |
| 36 | + if (typeof email !== 'string' || !isEmail(email)) continue |
| 37 | + const key = email.toLowerCase() |
| 38 | + if (seen.has(key)) continue |
| 39 | + seen.add(key) |
| 40 | + contacts.push({ |
| 41 | + channel: 'email', |
| 42 | + value: email, |
| 43 | + name: typeof dev.name === 'string' ? dev.name : undefined, |
| 44 | + role: 'maintainer', |
| 45 | + tier: 'B', |
| 46 | + provenance: prov(), |
| 47 | + }) |
| 48 | + } |
| 49 | + return contacts |
| 50 | +} |
| 51 | + |
| 52 | +async function resolveVersion( |
| 53 | + groupPath: string, |
| 54 | + artifact: string, |
| 55 | + timeoutMs: number, |
| 56 | + userAgent: string, |
| 57 | +): Promise<string | null> { |
| 58 | + const url = `${BASE}/${groupPath}/${artifact}/maven-metadata.xml` |
| 59 | + const { text } = await fetchText(url, timeoutMs, registryHeaders(userAgent)) |
| 60 | + if (!text) return null |
| 61 | + let doc: any |
| 62 | + try { |
| 63 | + doc = parser.parse(text) |
| 64 | + } catch { |
| 65 | + return null |
| 66 | + } |
| 67 | + const versioning = doc?.metadata?.versioning |
| 68 | + const release = versioning?.release ?? versioning?.latest |
| 69 | + if (typeof release === 'string') return release |
| 70 | + const versions = asArray(versioning?.versions?.version) |
| 71 | + return versions.length ? String(versions[versions.length - 1]) : null |
| 72 | +} |
| 73 | + |
| 74 | +export async function fetchMaven( |
| 75 | + parsed: ParsedPurl, |
| 76 | + timeoutMs: number, |
| 77 | + userAgent: string, |
| 78 | +): Promise<ExtractorResult> { |
| 79 | + if (!parsed.namespace) return { contacts: [], policies: {} } |
| 80 | + const groupPath = parsed.namespace.replace(/\./g, '/') |
| 81 | + const artifact = parsed.name |
| 82 | + |
| 83 | + const version = await resolveVersion(groupPath, artifact, timeoutMs, userAgent) |
| 84 | + if (!version) return { contacts: [], policies: {} } |
| 85 | + |
| 86 | + const url = `${BASE}/${groupPath}/${artifact}/${version}/${artifact}-${version}.pom` |
| 87 | + const { text } = await fetchText(url, timeoutMs, registryHeaders(userAgent)) |
| 88 | + if (!text) return { contacts: [], policies: {} } |
| 89 | + return { contacts: mapMavenPom(text, url, new Date().toISOString()), policies: {} } |
| 90 | +} |
0 commit comments