@@ -8,7 +8,7 @@ import {PackageURL} from "packageurl-js";
8
8
* @return {{"bom-ref": string, name, purl: string, type, version} }
9
9
* @private
10
10
*/
11
- function getComponent ( component , type ) {
11
+ function getComponent ( component , type ) {
12
12
let componentObject ;
13
13
if ( component instanceof PackageURL )
14
14
{
@@ -88,34 +88,41 @@ export default class CycloneDxSbom {
88
88
}
89
89
90
90
/**
91
- * @param {component } sourceRef current target Component ( Starting from root component by clients)
92
- * @param {PackageURL } targetRef current dependency to add to Dependencies list of component sourceRef
93
- * @return Sbom
91
+ * Adds a dependency relationship between two components in the SBOM
92
+ * @param {PackageURL } sourceRef - The source component (parent)
93
+ * @param {PackageURL } targetRef - The target component (dependency)
94
+ * @return {CycloneDxSbom } The updated SBOM
94
95
*/
95
96
addDependency ( sourceRef , targetRef ) {
96
- let componentIndex = this . getComponentIndex ( sourceRef ) ;
97
- if ( componentIndex < 0 ) {
98
- this . components . push ( getComponent ( sourceRef , "library" ) )
99
- }
100
- let dependencyIndex = this . getDependencyIndex ( sourceRef . purl )
101
- if ( dependencyIndex < 0 ) {
102
- this . dependencies . push ( createDependency ( sourceRef . purl ) )
103
- dependencyIndex = this . getDependencyIndex ( sourceRef . purl )
104
- }
97
+ const sourcePurl = sourceRef . toString ( ) ;
98
+ const targetPurl = targetRef . toString ( ) ;
99
+
100
+ // Ensure both components exist in the components list
101
+ [ sourceRef , targetRef ] . forEach ( ( ref , index ) => {
102
+ const purl = index === 0 ? sourcePurl : targetPurl ;
103
+ if ( this . getComponentIndex ( purl ) < 0 ) {
104
+ this . components . push ( getComponent ( ref , "library" ) ) ;
105
+ }
106
+ } ) ;
105
107
106
- //Only if the dependency doesn't exists on the dependency list of dependency, then add it to this list.
107
- if ( this . dependencies [ dependencyIndex ] . dependsOn . findIndex ( dep => dep === targetRef . toString ( ) ) === - 1 ) {
108
- this . dependencies [ dependencyIndex ] . dependsOn . push ( targetRef . toString ( ) )
108
+ // Ensure source dependency exists
109
+ let sourceDepIndex = this . getDependencyIndex ( sourcePurl ) ;
110
+ if ( sourceDepIndex < 0 ) {
111
+ this . dependencies . push ( createDependency ( sourcePurl ) ) ;
112
+ sourceDepIndex = this . dependencies . length - 1 ;
109
113
}
110
- if ( this . getDependencyIndex ( targetRef . toString ( ) ) < 0 ) {
111
- this . dependencies . push ( createDependency ( targetRef . toString ( ) ) )
114
+
115
+ // Add target to source's dependencies if not already present
116
+ if ( ! this . dependencies [ sourceDepIndex ] . dependsOn . includes ( targetPurl ) ) {
117
+ this . dependencies [ sourceDepIndex ] . dependsOn . push ( targetPurl ) ;
112
118
}
113
- let newComponent = getComponent ( targetRef , "library" ) ;
114
- // Only if component doesn't exists in component list, add it to the list.
115
- if ( this . getComponentIndex ( newComponent ) < 0 ) {
116
- this . components . push ( newComponent )
119
+
120
+ // Ensure target dependency exists
121
+ if ( this . getDependencyIndex ( targetPurl ) < 0 ) {
122
+ this . dependencies . push ( createDependency ( targetPurl ) ) ;
117
123
}
118
- return this
124
+
125
+ return this ;
119
126
}
120
127
121
128
/** @param {{} } opts - various options, settings and configuration of application.
@@ -170,8 +177,7 @@ export default class CycloneDxSbom {
170
177
* @private
171
178
*/
172
179
getComponentIndex ( theComponent ) {
173
-
174
- return this . components . findIndex ( component => component . purl === theComponent . purl )
180
+ return this . components . findIndex ( component => component . purl === theComponent )
175
181
}
176
182
177
183
/** This method gets a PackageUrl, and returns a Component of CycloneDx Sbom
@@ -190,16 +196,18 @@ export default class CycloneDxSbom {
190
196
filterIgnoredDeps ( deps ) {
191
197
deps . forEach ( dep => {
192
198
let index = this . components . findIndex ( component => component . name === dep ) ;
193
- if ( index >= 0 ) {
194
- this . components . splice ( index , 1 )
199
+ if ( index === - 1 ) {
200
+ return ;
195
201
}
202
+ const depPurl = this . components [ index ] . purl ;
203
+ this . components . splice ( index , 1 )
196
204
index = this . dependencies . findIndex ( dependency => dependency . ref . includes ( dep ) ) ;
197
- if ( index >= 0 ) {
198
- this . dependencies . splice ( index , 1 )
205
+ if ( index === - 1 ) {
206
+ return ;
199
207
}
200
-
208
+ this . dependencies . splice ( index , 1 )
201
209
this . dependencies . forEach ( dependency => {
202
- let indexDependsOn = dependency . dependsOn . findIndex ( theDep => theDep . includes ( dep ) ) ;
210
+ let indexDependsOn = dependency . dependsOn . findIndex ( theDep => theDep . includes ( depPurl ) ) ;
203
211
if ( indexDependsOn > - 1 ) {
204
212
dependency . dependsOn . splice ( indexDependsOn , 1 )
205
213
}
0 commit comments