@dawans/promptshield
Version:
Secure your LLM stack with enterprise-grade RulePacks for AI safety scanning
71 lines (70 loc) • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CsvRenderer = void 0;
const Result_1 = require("../../../../shared/types/Result");
/**
* CSV format renderer
*/
class CsvRenderer {
/**
* Renders a report to CSV format
*/
async render(report) {
try {
const violations = report.getFilteredViolations();
let output = '';
// CSV Header
output +=
'rule_id,rule_description,severity,category,message,field,object_index,match,context,position_start,position_end\n';
// CSV Rows
for (const violation of violations) {
const row = [
this.escapeCsvValue(violation.ruleId),
this.escapeCsvValue(violation.ruleDescription),
this.escapeCsvValue(violation.severity),
this.escapeCsvValue(violation.category),
this.escapeCsvValue(violation.message),
this.escapeCsvValue(violation.field || ''),
violation.objectIndex?.toString() || '',
this.escapeCsvValue(violation.context?.match || ''),
this.escapeCsvValue(violation.context ? JSON.stringify(violation.context) : ''),
violation.position?.start?.toString() || '',
violation.position?.end?.toString() || '',
];
output += row.join(',') + '\n';
}
return (0, Result_1.ok)(output);
}
catch (error) {
return (0, Result_1.err)(new Error(`Failed to render CSV report: ${error}`));
}
}
/**
* Gets the output format this renderer handles
*/
getFormat() {
return 'csv';
}
/**
* Checks if this renderer supports streaming
*/
supportsStreaming() {
return true;
}
/**
* Escapes CSV values properly
*/
escapeCsvValue(value) {
if (!value)
return '';
// If value contains comma, quote, or newline, wrap in quotes and escape quotes
if (value.includes(',') ||
value.includes('"') ||
value.includes('\n') ||
value.includes('\r')) {
return `"${value.replace(/"/g, '""')}"`;
}
return value;
}
}
exports.CsvRenderer = CsvRenderer;