@dawans/promptshield
Version:
Secure your LLM stack with enterprise-grade RulePacks for AI safety scanning
77 lines (76 loc) • 2.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonRenderer = void 0;
const Result_1 = require("../../../../shared/types/Result");
const Violation_1 = require("../../../../shared/types/Violation");
const ScanMetrics_1 = require("../../../../shared/types/ScanMetrics");
/**
* JSON format renderer
*/
class JsonRenderer {
/**
* Renders a report to JSON format
*/
async render(report) {
try {
const violations = report.getFilteredViolations();
const summary = Violation_1.ViolationUtils.createSummary(violations);
const metrics = report.scanResult.metrics;
const output = {
summary: {
total_violations: summary.total,
by_severity: summary.bySeverity,
by_category: summary.byCategory,
},
violations: violations.map((v) => ({
rule_id: v.ruleId,
rule_description: v.ruleDescription,
severity: v.severity,
category: v.category,
message: v.message,
field: v.field,
object_index: v.objectIndex,
position: v.position,
context: v.context,
metadata: v.metadata,
})),
};
// Include metrics if requested
if (report.shouldIncludeMetrics()) {
output.metrics = {
objects_scanned: metrics.objectsScanned,
processing_time_ms: metrics.processingTime,
memory_usage_bytes: metrics.memoryUsage,
rules_applied: metrics.rulesApplied,
processing_rate: ScanMetrics_1.ScanMetricsUtils.calculateProcessingRate(metrics),
average_time_per_object_ms: metrics.averageTimePerObject ?? 0,
};
}
// Include metadata
output.metadata = {
generated_at: new Date().toISOString(),
version: '1.0.0',
};
const jsonString = report.options.verbose
? JSON.stringify(output, null, 2)
: JSON.stringify(output);
return (0, Result_1.ok)(jsonString);
}
catch (error) {
return (0, Result_1.err)(new Error(`Failed to render JSON report: ${error}`));
}
}
/**
* Gets the output format this renderer handles
*/
getFormat() {
return 'json';
}
/**
* Checks if this renderer supports streaming
*/
supportsStreaming() {
return false;
}
}
exports.JsonRenderer = JsonRenderer;