Skip to content

Commit

Permalink
Improve printOccurrences function with streaming output for large SBO…
Browse files Browse the repository at this point in the history
…M data
  • Loading branch information
deeshantk committed Dec 4, 2024
1 parent 36791cf commit 8494ee3
Show file tree
Hide file tree
Showing 3 changed files with 8,217 additions and 23 deletions.
70 changes: 50 additions & 20 deletions lib/helpers/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,34 +178,64 @@ const locationComparator = (a, b) => {
};

export function printOccurrences(bomJson) {
const data = [["Group", "Name", "Version", "Occurrences"]];
if (!bomJson || !bomJson.components) {
console.error("Invalid or empty SBOM data.");
return;
}
for (const comp of bomJson.components) {
if (!comp.evidence || !comp.evidence.occurrences) {
continue;
}
data.push([
comp.group || "",
comp.name,
comp.version || "",
comp.evidence.occurrences
.map((l) => l.location)
.sort(locationComparator)
.join("\n"),
]);
}

const data = [["Group", "Name", "Version", "Occurrences"]];


const config = {
header: {
alignment: "center",
content: "Component Evidence\nGenerated with \u2665 by cdxgen",
columnDefault: {
width: 50,
},
columnCount: 4,
columns: [{ width: 20 }, { width: 40 }, { width: 50 }, { width: 25 }],
};
if (data.length > 1) {
console.log(table(data, config));

const stream = createStream(config); // Create stream with the config

const header = "Component Evidence\nGenerated with ♥ by cdxgen";
console.log(header);

// Stream the components
for (const comp of bomJson.components) {
try {
if (comp.evidence && comp.evidence.occurrences) {
const row = [
comp.group || "",
comp.name,
comp.version || "",
comp.evidence.occurrences
.map((l) => l.location)
.sort(customLocationComparator)
.join("\n"),
];
stream.write(row);
}
} catch (error) {
console.error("Error processing component:", error);
}
}

stream.end();


stream.on("error", (error) => {
console.error("Stream error:", error);
});
}


function customLocationComparator(a, b) {
return a.localeCompare(b);
}





export function printCallStack(bomJson) {
const data = [["Group", "Name", "Version", "Call Stack"]];
if (!bomJson || !bomJson.components) {
Expand Down
Loading

0 comments on commit 8494ee3

Please sign in to comment.