From 16353c098e6209823ed09265705c485b915cff1e Mon Sep 17 00:00:00 2001 From: deeshantk Date: Thu, 5 Dec 2024 14:25:00 +0530 Subject: [PATCH] Fix: Update display.js for optimized stream handling Signed-off-by: deeshantk --- lib/helpers/display.js | 57 +++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/lib/helpers/display.js b/lib/helpers/display.js index b82c205fd..f6d9e49a7 100644 --- a/lib/helpers/display.js +++ b/lib/helpers/display.js @@ -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) {