-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move prune logic in separate file
- Loading branch information
1 parent
f0e51f2
commit 842ef7f
Showing
3 changed files
with
47 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as _debug from 'debug'; | ||
import { DepGraph, legacy } from '@snyk/dep-graph'; | ||
|
||
import { DepTree } from './types'; | ||
import * as config from './config'; | ||
import { SupportedPackageManagers } from './package-managers'; | ||
|
||
const debug = _debug('snyk:prune'); | ||
|
||
const {depTreeToGraph, graphToDepTree} = legacy; | ||
|
||
export function countPathsToGraphRoot(graph: DepGraph): number { | ||
return graph.getPkgs().reduce((acc, pkg) => acc + graph.countPathsToRoot(pkg), 0); | ||
} | ||
|
||
export async function pruneGraph( | ||
depGraph: DepGraph, | ||
packageManager: SupportedPackageManagers, | ||
): Promise<DepGraph> { | ||
try { | ||
// Arbitrary threshold for maximum number of elements in the tree | ||
const threshold = config.PRUNE_DEPS_THRESHOLD; | ||
const prunedTree = (await graphToDepTree( | ||
depGraph, | ||
packageManager, | ||
{ deduplicateWithinTopLevelDeps: true }, | ||
)) as DepTree; | ||
|
||
const prunedGraph = await depTreeToGraph(prunedTree, packageManager); | ||
const count = countPathsToGraphRoot(prunedGraph); | ||
debug('prunedPathsCount: ' + count); | ||
|
||
if (count < threshold) { | ||
return prunedGraph; | ||
} | ||
|
||
debug('Too many vulnerable paths to process the project'); | ||
throw new Error('Too many vulnerable paths to process the project'); | ||
} catch (e) { | ||
debug('Failed to prune the graph, returning original: ' + e); | ||
return depGraph; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters