refactor(sbom): share CycloneDX component ref assignment

This commit is contained in:
Vincent Koc
2026-12-06 17:15:39 +08:00
parent fc5fd502b8
commit 939b342ea1
+18 -21
View File
@@ -167,12 +167,16 @@ const FILE_NAME_UNKNOWN =
const presentSum = (sum: string | null): string | undefined =>
sum !== null && sum.length > 0 ? sum : undefined;
const bomRefsFor = (purls: readonly string[]): string[] => {
const bomRefsFor = (
purls: readonly (string | undefined)[],
fallbackPrefix: string,
): string[] => {
const seen = new Map<string, number>();
return purls.map((purl) => {
const count = seen.get(purl) ?? 0;
seen.set(purl, count + 1);
return count === 0 ? purl : `${purl}#${String(count)}`;
return purls.map((purl, index) => {
const key = purl ?? `${fallbackPrefix}:${String(index)}`;
const count = seen.get(key) ?? 0;
seen.set(key, count + 1);
return count === 0 ? key : `${key}#${String(count)}`;
});
};
@@ -555,20 +559,19 @@ const projectGoCycloneDx = (
const minimumBytes = documentBytesAtLeast(0, published, goComponentBytes);
if (minimumBytes > CYCLONEDX_DOCUMENT_BYTE_LIMIT)
return documentAllowanceExceeded(minimumBytes, "lower_bound");
const refKeys = published.map((item, index) =>
const purls = published.map((item) =>
item.componentPath === "qualified"
? golangPackageUrl(item.component.path, item.component.version)
: `go-module:${String(index)}`,
: undefined,
);
const refs = bomRefsFor(refKeys);
const refs = bomRefsFor(purls, "go-module");
const components = published.map((item, index) => {
const ref = refs[index] ?? refKeys[index] ?? `go-module:${String(index)}`;
const component = item.component;
return libraryComponent(
ref,
refs[index]!,
component.path,
component.version,
item.componentPath === "qualified" ? refKeys[index] : undefined,
purls[index],
goComponentProperties(item),
);
});
@@ -659,10 +662,7 @@ const projectRustCycloneDx = (
if (minimumBytes > CYCLONEDX_DOCUMENT_BYTE_LIMIT)
return documentAllowanceExceeded(minimumBytes, "lower_bound");
const purls = packages.map(cratesIoPackageUrl);
const refKeys = packages.map(
(_, index) => purls[index] ?? `rust-package:${String(index)}`,
);
const refs = bomRefsFor(refKeys);
const refs = bomRefsFor(purls, "rust-package");
// Edges expand small indices into full refs, so a report inside its own limit
// can describe a document far past V8's string limit. Count before expanding.
const expandedBytes = documentBytesAtLeast(minimumBytes, packages, (pkg) =>
@@ -675,7 +675,7 @@ const projectRustCycloneDx = (
return documentAllowanceExceeded(expandedBytes, "lower_bound");
const components = packages.map((pkg, index) =>
libraryComponent(
refs[index] ?? `rust-package:${String(index)}`,
refs[index]!,
pkg.name,
pkg.version,
purls[index],
@@ -693,15 +693,12 @@ const projectRustCycloneDx = (
packages.length === 0
? undefined
: packages.map((pkg, index) => ({
ref: refs[index] ?? `rust-package:${String(index)}`,
ref: refs[index]!,
dependsOn: uniqueRefs(pkg.dependencies.map((target) => refs[target])),
}));
const metadata = report.auditable_metadata;
const rootIndex = metadata?.root_package_index ?? null;
const rootRef =
rootIndex === null
? undefined
: (refs[rootIndex] ?? `rust-package:${String(rootIndex)}`);
const rootRef = rootIndex === null ? undefined : refs[rootIndex];
const reportedUnknowns = rustBinaryUnknowns(report);
return projectSizedDocument(
parent,