|
1 | 1 | import assert from "node:assert"; |
2 | | -import { exec } from "node:child_process"; |
3 | 2 | import { existsSync } from "node:fs"; |
4 | 3 | import { join } from "node:path"; |
| 4 | +import { ensure, partition } from "../common"; |
| 5 | +import { exec } from "node:child_process"; |
5 | 6 |
|
6 | 7 | export type Report = { |
7 | 8 | auditReportVersion: 2; |
@@ -58,15 +59,28 @@ export type Vulnerability = { |
58 | 59 | range: string; |
59 | 60 | }; |
60 | 61 |
|
61 | | -export function collectVulnerabilities(report: Report, dependency: Dependency): Vulnerability[] { |
62 | | - const vulnerabilities = dependency.via.filter(v => typeof v !== "string"); |
63 | | - if (vulnerabilities.length > 0) { |
64 | | - return vulnerabilities; |
| 62 | +export function collectVulnerabilities(report: Report, rootDependency: PackageName): Vulnerability[] { |
| 63 | + const dependencies: PackageName[] = [rootDependency]; |
| 64 | + const dependenciesSeen: PackageName[] = []; |
| 65 | + const allVulnerabilities: Vulnerability[] = []; |
| 66 | + |
| 67 | + while (dependencies.length > 0) { |
| 68 | + const dependencyName = ensure(dependencies.shift()); |
| 69 | + dependenciesSeen.push(dependencyName); |
| 70 | + |
| 71 | + const [transients, vulnerabilities] = partition( |
| 72 | + report.vulnerabilities[dependencyName].via, |
| 73 | + v => typeof v === "string" |
| 74 | + ); |
| 75 | + |
| 76 | + if (vulnerabilities.length > 0) { |
| 77 | + allVulnerabilities.push(...vulnerabilities); |
| 78 | + continue; |
| 79 | + } |
| 80 | + dependencies.push(...transients.filter(d => !dependenciesSeen.includes(d))); |
65 | 81 | } |
66 | 82 |
|
67 | | - return dependency.via |
68 | | - .filter(v => typeof v === "string") |
69 | | - .flatMap(v => collectVulnerabilities(report, report.vulnerabilities[v])); |
| 83 | + return allVulnerabilities; |
70 | 84 | } |
71 | 85 |
|
72 | 86 | export async function run(): Promise<Report> { |
|
0 commit comments