refactor(nuget): share manifest report construction (#15483)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-01 14:05:47 +00:00
1 parent 4fbb76ddaf
commit be4cdec7ca
2 files changed
+75 -102

No files matched your search

+26
View File
@@ -5,6 +5,7 @@ import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import {
TextReader,
Uint8ArrayReader,
Uint8ArrayWriter,
ZipReader,
ZipWriter,
@@ -279,6 +280,31 @@ it("inventories a package whose .nuspec is not well-formed XML", async () => {
expect(report.limitations).toContain(NUPKG_LIMITATIONS.malformedNuspec);
});
it("counts an unreadable manifest's bytes and keeps its selected path", async () => {
const manifest = Uint8Array.of(0xff, 0x41, 0x42);
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("Demo.Package.nuspec", new Uint8ArrayReader(manifest));
const bytes = await writer.close();
const report = nugetNupkgSchema.parse(
await parseNugetNupkg(bufferByteSource(bytes), artifactOf(bytes), defaults),
);
expect(report).toMatchObject({
identity: null,
license: null,
repository: null,
nuspec: {
path: "Demo.Package.nuspec",
bytes: manifest.length,
source: "root-nuspec",
},
coverage: { entries: 1, examined_nuspec_bytes: manifest.length },
});
expect(report.dependency_groups).toEqual([]);
expect(report.limitations).toContain(
"The selected .nuspec is not valid UTF-8 or BOM-marked UTF-16 XML text.",
);
});
it.each([
[
"nuspec",
+49 -102
View File
@@ -273,22 +273,22 @@ const parseDependencies = (
];
};
type NuspecMetadata = Pick<
NugetNupkg,
"identity" | "license" | "repository" | "dependency_groups"
>;
const emptyNuspecMetadata = (): NuspecMetadata => ({
identity: null,
license: null,
repository: null,
dependency_groups: [],
});
const parseNuspecDocument = (
xml: string,
limitations: string[],
): Pick<
NugetNupkg,
"identity" | "license" | "repository" | "dependency_groups"
> => {
const empty: Pick<
NugetNupkg,
"identity" | "license" | "repository" | "dependency_groups"
> = {
identity: null,
license: null,
repository: null,
dependency_groups: [],
};
): NuspecMetadata => {
// NuGet reads the .nuspec with XDocument, which applies XML 1.0 end-of-line
// handling, so a description packed with CRLF reads with LF line breaks.
const parsed = parseXmlDocument(xml, {
@@ -302,17 +302,17 @@ const parseNuspecDocument = (
? "The selected .nuspec declares a document type, which is unsupported."
: NUPKG_LIMITATIONS.malformedNuspec,
);
return empty;
return emptyNuspecMetadata();
}
const root = parsed.value.documentElement;
if (!isXmlElement(root) || root.localName !== "package") {
limitations.push("The selected .nuspec root element is not package.");
return empty;
return emptyNuspecMetadata();
}
const metadata = xmlFirstChildNamed(root, "metadata");
if (metadata === undefined) {
limitations.push("The selected .nuspec has no metadata element.");
return empty;
return emptyNuspecMetadata();
}
// NuGet's typed getters select children in the default namespace in scope.
// Authors and description come from its local-name metadata index.
@@ -600,55 +600,38 @@ export const parseNugetNupkg = async (
}
}
}
const report = (
nuspec: NugetNupkg["nuspec"],
metadata = emptyNuspecMetadata(),
): NugetNupkg => ({
artifact: {
path: artifact.path,
sha256: artifact.sha256,
bytes: artifact.bytes,
},
...metadata,
contents,
nuspec,
coverage: {
status: listing.status,
entries: contents.length,
examined_nuspec_bytes: nuspec?.bytes ?? 0,
},
limitations,
});
const selected = findNuspec(files, relsXml, limitations);
if (selected.member === undefined || selected.source === undefined)
return {
artifact: {
path: artifact.path,
sha256: artifact.sha256,
bytes: artifact.bytes,
},
identity: null,
license: null,
repository: null,
dependency_groups: [],
contents,
nuspec: null,
coverage: {
status: listing.status,
entries: contents.length,
examined_nuspec_bytes: 0,
},
limitations,
};
return report(null);
if (selected.member.entry.encrypted) {
limitations.push(
`Selected .nuspec ${JSON.stringify(selected.member.name)} is encrypted and was not extracted.`,
);
return {
artifact: {
path: artifact.path,
sha256: artifact.sha256,
bytes: artifact.bytes,
},
identity: null,
license: null,
repository: null,
dependency_groups: [],
contents,
nuspec: {
path: selected.member.name,
// Nothing was decoded; the declared size remains in contents.
bytes: null,
source: selected.source,
},
coverage: {
status: listing.status,
entries: contents.length,
examined_nuspec_bytes: 0,
},
limitations,
};
return report({
path: selected.member.name,
// Nothing was decoded; the declared size remains in contents.
bytes: null,
source: selected.source,
});
}
const nuspecBytes = await readMemberBytes(
selected.member.entry,
@@ -656,6 +639,11 @@ export const parseNugetNupkg = async (
input.max_nuspec_bytes,
"Selected .nuspec",
);
const nuspec = {
path: selected.member.name,
bytes: nuspecBytes.length,
source: selected.source,
};
let xml: string;
try {
xml = decodeXmlPlistText(nuspecBytes);
@@ -663,29 +651,7 @@ export const parseNugetNupkg = async (
limitations.push(
"The selected .nuspec is not valid UTF-8 or BOM-marked UTF-16 XML text.",
);
return {
artifact: {
path: artifact.path,
sha256: artifact.sha256,
bytes: artifact.bytes,
},
identity: null,
license: null,
repository: null,
dependency_groups: [],
contents,
nuspec: {
path: selected.member.name,
bytes: nuspecBytes.length,
source: selected.source,
},
coverage: {
status: listing.status,
entries: contents.length,
examined_nuspec_bytes: nuspecBytes.length,
},
limitations,
};
return report(nuspec);
}
const decoded = parseNuspecDocument(xml, limitations);
if (
@@ -698,26 +664,7 @@ export const parseNugetNupkg = async (
limitations.push(
"The selected .nuspec metadata did not include id, version, authors, or description.",
);
return {
artifact: {
path: artifact.path,
sha256: artifact.sha256,
bytes: artifact.bytes,
},
...decoded,
contents,
nuspec: {
path: selected.member.name,
bytes: nuspecBytes.length,
source: selected.source,
},
coverage: {
status: listing.status,
entries: contents.length,
examined_nuspec_bytes: nuspecBytes.length,
},
limitations,
};
return report(nuspec, decoded);
} finally {
signal?.removeEventListener("abort", onAbort);
await reader.close().catch(() => undefined);