Skip to content

Commit

Permalink
Fix: Update display.js for optimized stream handling
Browse files Browse the repository at this point in the history
Signed-off-by: deeshantk <[email protected]>
  • Loading branch information
deeshantk committed Dec 5, 2024
1 parent 36791cf commit 16353c0
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions lib/helpers/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,34 +178,51 @@ 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?.occurrences) {
const row = [
comp.group || "",
comp.name,
comp.version || "",
comp.evidence.occurrences
.map((l) => l.location)
.sort(locationComparator)
.join("\n"),
];
stream.write(row);
}
} catch (error) {
console.error("Error processing component:", error);
}
}

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

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

0 comments on commit 16353c0

Please sign in to comment.