(segment: string, segmentIndex: number, errors: string[])
| 835 | } |
| 836 | |
| 837 | function parseSegment(segment: string, segmentIndex: number, errors: string[]): unknown[] { |
| 838 | const trimmed = (segment ?? '').trim(); |
| 839 | if (trimmed.length === 0) { |
| 840 | return []; |
| 841 | } |
| 842 | |
| 843 | try { |
| 844 | const parsed = JSON.parse(trimmed); |
| 845 | if (Array.isArray(parsed)) { |
| 846 | return parsed; |
| 847 | } |
| 848 | if (parsed && typeof parsed === 'object') { |
| 849 | const record = parsed as Record<string, unknown>; |
| 850 | if (Array.isArray(record.Findings)) { |
| 851 | return record.Findings; |
| 852 | } |
| 853 | if (Array.isArray(record.findings)) { |
| 854 | return record.findings; |
| 855 | } |
| 856 | return [parsed]; |
| 857 | } |
| 858 | } catch (_error) { |
| 859 | // Fallback to NDJSON parsing |
| 860 | const ndjsonResults: unknown[] = []; |
| 861 | trimmed |
| 862 | .split(/\r?\n/) |
| 863 | .map((line) => line.trim()) |
| 864 | .filter((line) => line.length > 0) |
| 865 | .forEach((line, lineIndex) => { |
| 866 | try { |
| 867 | ndjsonResults.push(JSON.parse(line)); |
| 868 | } catch (innerError) { |
| 869 | errors.push( |
| 870 | `Segment ${segmentIndex + 1} line ${lineIndex + 1}: Unable to parse JSON (${(innerError as Error).message})`, |
| 871 | ); |
| 872 | } |
| 873 | }); |
| 874 | |
| 875 | if (ndjsonResults.length > 0) { |
| 876 | return ndjsonResults; |
| 877 | } |
| 878 | |
| 879 | errors.push(`Segment ${segmentIndex + 1}: Unable to parse Prowler output as JSON.`); |
| 880 | } |
| 881 | |
| 882 | return []; |
| 883 | } |
| 884 | |
| 885 | function toNormalisedFinding( |
| 886 | finding: ProwlerFinding, |
no test coverage detected