@trustify-da/trustify-da-javascript-client 0.3.0-ea.b40d888 → 0.3.0-ea.b6d9c3f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/README.md +193 -13
  2. package/dist/package.json +43 -27
  3. package/dist/src/analysis.d.ts +28 -5
  4. package/dist/src/analysis.js +99 -89
  5. package/dist/src/batch_opts.d.ts +24 -0
  6. package/dist/src/batch_opts.js +35 -0
  7. package/dist/src/cli.js +299 -9
  8. package/dist/src/cyclone_dx_sbom.d.ts +17 -3
  9. package/dist/src/cyclone_dx_sbom.js +56 -8
  10. package/dist/src/index.d.ts +201 -12
  11. package/dist/src/index.js +360 -9
  12. package/dist/src/license/index.d.ts +28 -0
  13. package/dist/src/license/index.js +103 -0
  14. package/dist/src/license/license_utils.d.ts +40 -0
  15. package/dist/src/license/license_utils.js +137 -0
  16. package/dist/src/license/licenses_api.d.ts +34 -0
  17. package/dist/src/license/licenses_api.js +98 -0
  18. package/dist/src/license/project_license.d.ts +20 -0
  19. package/dist/src/license/project_license.js +62 -0
  20. package/dist/src/oci_image/images.d.ts +4 -5
  21. package/dist/src/oci_image/images.js +15 -1
  22. package/dist/src/oci_image/utils.d.ts +7 -7
  23. package/dist/src/oci_image/utils.js +11 -2
  24. package/dist/src/provider.d.ts +18 -5
  25. package/dist/src/provider.js +33 -5
  26. package/dist/src/providers/base_java.d.ts +15 -15
  27. package/dist/src/providers/base_java.js +18 -44
  28. package/dist/src/providers/base_javascript.d.ts +45 -12
  29. package/dist/src/providers/base_javascript.js +138 -24
  30. package/dist/src/providers/base_pyproject.d.ts +158 -0
  31. package/dist/src/providers/base_pyproject.js +322 -0
  32. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  33. package/dist/src/providers/containerfile_parser.js +20 -0
  34. package/dist/src/providers/golang_gomodules.d.ts +30 -13
  35. package/dist/src/providers/golang_gomodules.js +176 -121
  36. package/dist/src/providers/gomod_parser.d.ts +4 -0
  37. package/dist/src/providers/gomod_parser.js +16 -0
  38. package/dist/src/providers/java_gradle.d.ts +28 -3
  39. package/dist/src/providers/java_gradle.js +128 -4
  40. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  41. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  42. package/dist/src/providers/java_maven.d.ts +20 -5
  43. package/dist/src/providers/java_maven.js +198 -6
  44. package/dist/src/providers/javascript_bun.d.ts +10 -0
  45. package/dist/src/providers/javascript_bun.js +100 -0
  46. package/dist/src/providers/javascript_npm.d.ts +6 -0
  47. package/dist/src/providers/javascript_npm.js +21 -0
  48. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  49. package/dist/src/providers/javascript_pnpm.js +8 -4
  50. package/dist/src/providers/manifest.d.ts +2 -0
  51. package/dist/src/providers/manifest.js +22 -4
  52. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  53. package/dist/src/providers/marker_evaluator.js +191 -0
  54. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  55. package/dist/src/providers/oci_dockerfile.js +177 -0
  56. package/dist/src/providers/processors/yarn_berry_processor.d.ts +2 -2
  57. package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
  58. package/dist/src/providers/processors/yarn_classic_processor.d.ts +2 -2
  59. package/dist/src/providers/python_controller.d.ts +10 -3
  60. package/dist/src/providers/python_controller.js +122 -63
  61. package/dist/src/providers/python_pip.d.ts +16 -4
  62. package/dist/src/providers/python_pip.js +51 -58
  63. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  64. package/dist/src/providers/python_pip_pyproject.js +146 -0
  65. package/dist/src/providers/python_poetry.d.ts +98 -0
  66. package/dist/src/providers/python_poetry.js +286 -0
  67. package/dist/src/providers/python_uv.d.ts +46 -0
  68. package/dist/src/providers/python_uv.js +269 -0
  69. package/dist/src/providers/requirements_parser.d.ts +6 -0
  70. package/dist/src/providers/requirements_parser.js +24 -0
  71. package/dist/src/providers/rust_cargo.d.ts +56 -0
  72. package/dist/src/providers/rust_cargo.js +641 -0
  73. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  74. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  75. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  76. package/dist/src/remediation.d.ts +33 -0
  77. package/dist/src/remediation.js +321 -0
  78. package/dist/src/sbom.d.ts +17 -2
  79. package/dist/src/sbom.js +16 -4
  80. package/dist/src/tools.d.ts +48 -6
  81. package/dist/src/tools.js +114 -1
  82. package/dist/src/updaters/maven_updater.d.ts +26 -0
  83. package/dist/src/updaters/maven_updater.js +322 -0
  84. package/dist/src/updaters/toml_updater.d.ts +33 -0
  85. package/dist/src/updaters/toml_updater.js +244 -0
  86. package/dist/src/workspace.d.ts +70 -0
  87. package/dist/src/workspace.js +256 -0
  88. package/package.json +44 -28
@@ -0,0 +1,321 @@
1
+ import { PackageURL } from 'packageurl-js';
2
+ /**
3
+ * Extracts actionable remediation instructions from a DA AnalysisReport response.
4
+ *
5
+ * Walks the provider/source/dependency/issue tree, collects fixedIn and trustedContent
6
+ * remediation data, applies provider priority resolution, and for the same dependency
7
+ * affected by multiple CVEs selects the highest fixedIn version from the highest-priority
8
+ * provider. Dependencies with no remediation data are skipped.
9
+ *
10
+ * @param {object} analysisReport - raw DA AnalysisReport JSON response
11
+ * @param {object} [options] - extraction options
12
+ * @param {string[]} [options.providerPriority] - provider names in descending priority order.
13
+ * The first entry has the highest priority. Providers not listed share the lowest priority.
14
+ * When omitted or empty, all providers are treated equally and the highest fix version wins.
15
+ * @returns {Array<{purl: string, groupId: string, artifactId: string, currentVersion: string, fixedInVersion: string, fixedInPurl: string, provider: string, source: string, advisories: Array<{id: string, url: string}>, severity: string, cves: string[]}>}
16
+ */
17
+ export function extractRemediations(analysisReport, options = {}) {
18
+ if (!analysisReport || !analysisReport.providers) {
19
+ return [];
20
+ }
21
+ const priorityMap = buildPriorityMap(options.providerPriority);
22
+ const remediationsByDep = new Map();
23
+ const rankByDep = new Map();
24
+ for (const [providerName, providerReport] of Object.entries(analysisReport.providers)) {
25
+ const providerRank = priorityMap.get(providerName) ?? 0;
26
+ extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep);
27
+ extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep);
28
+ }
29
+ return Array.from(remediationsByDep.values())
30
+ .sort((a, b) => a.purl.localeCompare(b.purl));
31
+ }
32
+ /**
33
+ * Builds a priority lookup map from a provider priority array.
34
+ * First entry gets the highest numeric priority.
35
+ * @param {string[]} [providerPriority]
36
+ * @returns {Map<string, number>}
37
+ */
38
+ function buildPriorityMap(providerPriority) {
39
+ const map = new Map();
40
+ if (!providerPriority || providerPriority.length === 0) {
41
+ return map;
42
+ }
43
+ for (let i = 0; i < providerPriority.length; i++) {
44
+ map.set(providerPriority[i], providerPriority.length - i);
45
+ }
46
+ return map;
47
+ }
48
+ /**
49
+ * Extracts remediations from the sources/dependencies/issues tree of a provider report.
50
+ * @param {object} providerReport
51
+ * @param {string} providerName
52
+ * @param {number} providerRank - numeric priority rank for this provider
53
+ * @param {Map<string, object>} remediationsByDep - accumulator keyed by dependency PURL
54
+ * @param {Map<string, number>} rankByDep - tracks current winning rank per dependency
55
+ */
56
+ function extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep) {
57
+ if (!providerReport.sources) {
58
+ return;
59
+ }
60
+ for (const [sourceName, sourceReport] of Object.entries(providerReport.sources)) {
61
+ if (!sourceReport.dependencies) {
62
+ continue;
63
+ }
64
+ for (const dep of sourceReport.dependencies) {
65
+ if (!dep.issues) {
66
+ continue;
67
+ }
68
+ for (const issue of dep.issues) {
69
+ processIssueRemediation(issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep);
70
+ }
71
+ }
72
+ }
73
+ }
74
+ /**
75
+ * Processes a single issue's remediation data and merges it into the accumulator.
76
+ * @param {object} issue - issue object containing remediation and CVE data
77
+ * @param {object} dep - dependency object containing the ref PURL
78
+ * @param {string} providerName
79
+ * @param {string} sourceName
80
+ * @param {number} providerRank
81
+ * @param {Map<string, object>} remediationsByDep
82
+ * @param {Map<string, number>} rankByDep
83
+ */
84
+ function processIssueRemediation(issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep) {
85
+ const fixedInPurl = getFixedInPurl(issue);
86
+ if (!fixedInPurl) {
87
+ return;
88
+ }
89
+ const depPurl = dep.ref;
90
+ if (!depPurl) {
91
+ return;
92
+ }
93
+ let parsedDep;
94
+ let parsedFix;
95
+ try {
96
+ parsedDep = PackageURL.fromString(depPurl);
97
+ parsedFix = PackageURL.fromString(fixedInPurl);
98
+ }
99
+ catch {
100
+ return;
101
+ }
102
+ const fixedInVersion = parsedFix.version;
103
+ if (!fixedInVersion) {
104
+ return;
105
+ }
106
+ const cveId = issue.id || issue.cve;
107
+ const severity = issue.severity || 'UNKNOWN';
108
+ const advisories = extractAdvisories(issue);
109
+ const existing = remediationsByDep.get(depPurl);
110
+ if (!existing) {
111
+ remediationsByDep.set(depPurl, {
112
+ purl: depPurl,
113
+ groupId: parsedDep.namespace || '',
114
+ artifactId: parsedDep.name,
115
+ currentVersion: parsedDep.version || '',
116
+ fixedInVersion,
117
+ fixedInPurl,
118
+ provider: providerName,
119
+ source: sourceName,
120
+ advisories,
121
+ severity: severity.toUpperCase(),
122
+ cves: cveId ? [cveId] : [],
123
+ });
124
+ rankByDep.set(depPurl, providerRank);
125
+ return;
126
+ }
127
+ if (cveId && !existing.cves.includes(cveId)) {
128
+ existing.cves.push(cveId);
129
+ }
130
+ mergeAdvisories(existing.advisories, advisories);
131
+ const existingRank = rankByDep.get(depPurl);
132
+ if (providerRank > existingRank) {
133
+ existing.fixedInVersion = fixedInVersion;
134
+ existing.fixedInPurl = fixedInPurl;
135
+ existing.provider = providerName;
136
+ existing.source = sourceName;
137
+ existing.severity = higherSeverity(existing.severity, severity);
138
+ rankByDep.set(depPurl, providerRank);
139
+ }
140
+ else if (providerRank === existingRank) {
141
+ if (compareVersions(fixedInVersion, existing.fixedInVersion) > 0) {
142
+ existing.fixedInVersion = fixedInVersion;
143
+ existing.fixedInPurl = fixedInPurl;
144
+ existing.provider = providerName;
145
+ existing.source = sourceName;
146
+ }
147
+ existing.severity = higherSeverity(existing.severity, severity);
148
+ }
149
+ }
150
+ /**
151
+ * Extracts remediations from the recommendations section of a provider report.
152
+ * Merges CVEs and advisories into existing entries when present.
153
+ * @param {object} providerReport
154
+ * @param {string} providerName
155
+ * @param {number} providerRank
156
+ * @param {Map<string, object>} remediationsByDep
157
+ * @param {Map<string, number>} rankByDep
158
+ */
159
+ function extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep) {
160
+ if (!providerReport.recommendations || !providerReport.recommendations.dependencies) {
161
+ return;
162
+ }
163
+ for (const dep of providerReport.recommendations.dependencies) {
164
+ if (!dep.recommendation || !dep.ref) {
165
+ continue;
166
+ }
167
+ const recommendedPurl = dep.recommendation.ref || dep.recommendation;
168
+ if (typeof recommendedPurl !== 'string') {
169
+ continue;
170
+ }
171
+ let parsedDep;
172
+ let parsedRec;
173
+ try {
174
+ parsedDep = PackageURL.fromString(dep.ref);
175
+ parsedRec = PackageURL.fromString(recommendedPurl);
176
+ }
177
+ catch {
178
+ continue;
179
+ }
180
+ const fixedInVersion = parsedRec.version;
181
+ if (!fixedInVersion) {
182
+ continue;
183
+ }
184
+ const depPurl = dep.ref;
185
+ const existing = remediationsByDep.get(depPurl);
186
+ if (!existing) {
187
+ remediationsByDep.set(depPurl, {
188
+ purl: depPurl,
189
+ groupId: parsedDep.namespace || '',
190
+ artifactId: parsedDep.name,
191
+ currentVersion: parsedDep.version || '',
192
+ fixedInVersion,
193
+ fixedInPurl: recommendedPurl,
194
+ provider: providerName,
195
+ source: 'recommendation',
196
+ advisories: [],
197
+ severity: 'UNKNOWN',
198
+ cves: [],
199
+ });
200
+ rankByDep.set(depPurl, providerRank);
201
+ continue;
202
+ }
203
+ const existingRank = rankByDep.get(depPurl);
204
+ if (providerRank > existingRank) {
205
+ existing.fixedInVersion = fixedInVersion;
206
+ existing.fixedInPurl = recommendedPurl;
207
+ existing.provider = providerName;
208
+ existing.source = 'recommendation';
209
+ rankByDep.set(depPurl, providerRank);
210
+ }
211
+ else if (providerRank === existingRank) {
212
+ if (compareVersions(fixedInVersion, existing.fixedInVersion) > 0) {
213
+ existing.fixedInVersion = fixedInVersion;
214
+ existing.fixedInPurl = recommendedPurl;
215
+ existing.provider = providerName;
216
+ existing.source = 'recommendation';
217
+ }
218
+ }
219
+ }
220
+ }
221
+ /**
222
+ * Gets the fixedIn PURL from an issue's remediation, preferring trustedContent.
223
+ * @param {object} issue
224
+ * @returns {string|undefined}
225
+ */
226
+ function getFixedInPurl(issue) {
227
+ if (!issue.remediation) {
228
+ return undefined;
229
+ }
230
+ if (issue.remediation.trustedContent && issue.remediation.trustedContent.ref) {
231
+ return issue.remediation.trustedContent.ref;
232
+ }
233
+ if (issue.remediation.fixedIn) {
234
+ return issue.remediation.fixedIn;
235
+ }
236
+ return undefined;
237
+ }
238
+ /**
239
+ * Extracts advisory objects from an issue.
240
+ * @param {object} issue
241
+ * @returns {Array<{id: string, url: string}>}
242
+ */
243
+ function extractAdvisories(issue) {
244
+ const advisories = [];
245
+ if (issue.remediation && issue.remediation.trustedContent) {
246
+ const tc = issue.remediation.trustedContent;
247
+ if (tc.advisory) {
248
+ advisories.push({
249
+ id: tc.advisory.id || tc.advisory,
250
+ url: tc.advisory.url || '',
251
+ });
252
+ }
253
+ }
254
+ if (issue.advisories) {
255
+ for (const adv of issue.advisories) {
256
+ advisories.push({
257
+ id: adv.id || adv,
258
+ url: adv.url || '',
259
+ });
260
+ }
261
+ }
262
+ return advisories;
263
+ }
264
+ /**
265
+ * Merges new advisories into existing list, avoiding duplicates by id.
266
+ * @param {Array<{id: string, url: string}>} existing
267
+ * @param {Array<{id: string, url: string}>} incoming
268
+ */
269
+ function mergeAdvisories(existing, incoming) {
270
+ const seen = new Set(existing.map(a => a.id));
271
+ for (const adv of incoming) {
272
+ if (!seen.has(adv.id)) {
273
+ existing.push(adv);
274
+ seen.add(adv.id);
275
+ }
276
+ }
277
+ }
278
+ /**
279
+ * Compares two version strings segment by segment.
280
+ * Returns positive if a > b, negative if a < b, zero if equal.
281
+ * Falls back to lexicographic comparison for non-numeric segments.
282
+ * @param {string} a
283
+ * @param {string} b
284
+ * @returns {number}
285
+ */
286
+ function compareVersions(a, b) {
287
+ const partsA = a.split('.');
288
+ const partsB = b.split('.');
289
+ const len = Math.max(partsA.length, partsB.length);
290
+ for (let i = 0; i < len; i++) {
291
+ const segA = partsA[i] || '';
292
+ const segB = partsB[i] || '';
293
+ const numA = Number(segA);
294
+ const numB = Number(segB);
295
+ if (Number.isFinite(numA) && Number.isFinite(numB)) {
296
+ const diff = numA - numB;
297
+ if (diff !== 0)
298
+ return diff;
299
+ }
300
+ else {
301
+ const cmp = segA.localeCompare(segB);
302
+ if (cmp !== 0)
303
+ return cmp;
304
+ }
305
+ }
306
+ return 0;
307
+ }
308
+ const SEVERITY_ORDER = ['UNKNOWN', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL'];
309
+ /**
310
+ * Returns the higher of two severity strings, normalized to uppercase.
311
+ * @param {string} a
312
+ * @param {string} b
313
+ * @returns {string}
314
+ */
315
+ function higherSeverity(a, b) {
316
+ const upperA = a.toUpperCase();
317
+ const upperB = b.toUpperCase();
318
+ const indexA = SEVERITY_ORDER.indexOf(upperA);
319
+ const indexB = SEVERITY_ORDER.indexOf(upperB);
320
+ return indexA >= indexB ? upperA : upperB;
321
+ }
@@ -2,9 +2,10 @@ export default class Sbom {
2
2
  sbomModel: CycloneDxSbom;
3
3
  /**
4
4
  * @param {PackageURL} root - add main/root component for sbom
5
+ * @param {string|Array} [licenses] - optional license(s) for the root component
5
6
  * @return Sbom
6
7
  */
7
- addRoot(root: PackageURL): CycloneDxSbom;
8
+ addRoot(root: PackageURL, licenses?: string | any[]): CycloneDxSbom;
8
9
  /**
9
10
  * @return {{{"bom-ref": string, name, purl: string, type, version}}} root component of sbom.
10
11
  */
@@ -24,9 +25,14 @@ export default class Sbom {
24
25
  /**
25
26
  * @param {component} sourceRef current source Component ( Starting from root component by clients)
26
27
  * @param {PackageURL} targetRef current dependency to add to Dependencies list of component sourceRef
28
+ * @param {string} [scope] - Scope of the dependency
29
+ * @param {Array<{alg: string, content: string}>} [targetHashes] - Optional hashes for the target component
27
30
  * @return Sbom
28
31
  */
29
- addDependency(sourceRef: component, targetRef: PackageURL, scope: any): CycloneDxSbom;
32
+ addDependency(sourceRef: component, targetRef: PackageURL, scope?: string, targetHashes?: Array<{
33
+ alg: string;
34
+ content: string;
35
+ }>): CycloneDxSbom;
30
36
  /**
31
37
  * @return String sbom json in a string format
32
38
  */
@@ -43,6 +49,8 @@ export default class Sbom {
43
49
  type: any;
44
50
  version: any;
45
51
  scope: any;
52
+ licenses?: any;
53
+ hashes?: any;
46
54
  };
47
55
  /** This method gets a component object, and a string name, and checks if the name is a substring of the component' purl.
48
56
  * @param {} component to search in its dependencies
@@ -51,6 +59,13 @@ export default class Sbom {
51
59
  * @return {boolean}
52
60
  */
53
61
  checkIfPackageInsideDependsOnList(component: any, name: string): boolean;
62
+ /**
63
+ * Checks if any entry in the dependsOn list of sourceRef starts with the given purl prefix.
64
+ * @param {PackageURL} sourceRef - The source component to check
65
+ * @param {string} purlPrefix - The purl prefix to match (e.g. "pkg:npm/minimist@")
66
+ * @return {boolean}
67
+ */
68
+ checkDependsOnByPurlPrefix(sourceRef: PackageURL, purlPrefix: string): boolean;
54
69
  /** Removes the root component from the sbom
55
70
  */
56
71
  removeRootComponent(): void;
package/dist/src/sbom.js CHANGED
@@ -12,10 +12,11 @@ export default class Sbom {
12
12
  }
13
13
  /**
14
14
  * @param {PackageURL} root - add main/root component for sbom
15
+ * @param {string|Array} [licenses] - optional license(s) for the root component
15
16
  * @return Sbom
16
17
  */
17
- addRoot(root) {
18
- return this.sbomModel.addRoot(root);
18
+ addRoot(root, licenses) {
19
+ return this.sbomModel.addRoot(root, licenses);
19
20
  }
20
21
  /**
21
22
  * @return {{{"bom-ref": string, name, purl: string, type, version}}} root component of sbom.
@@ -42,10 +43,12 @@ export default class Sbom {
42
43
  /**
43
44
  * @param {component} sourceRef current source Component ( Starting from root component by clients)
44
45
  * @param {PackageURL} targetRef current dependency to add to Dependencies list of component sourceRef
46
+ * @param {string} [scope] - Scope of the dependency
47
+ * @param {Array<{alg: string, content: string}>} [targetHashes] - Optional hashes for the target component
45
48
  * @return Sbom
46
49
  */
47
- addDependency(sourceRef, targetRef, scope) {
48
- return this.sbomModel.addDependency(sourceRef, targetRef, scope);
50
+ addDependency(sourceRef, targetRef, scope, targetHashes) {
51
+ return this.sbomModel.addDependency(sourceRef, targetRef, scope, targetHashes);
49
52
  }
50
53
  /**
51
54
  * @return String sbom json in a string format
@@ -76,6 +79,15 @@ export default class Sbom {
76
79
  checkIfPackageInsideDependsOnList(component, name) {
77
80
  return this.sbomModel.checkIfPackageInsideDependsOnList(component, name);
78
81
  }
82
+ /**
83
+ * Checks if any entry in the dependsOn list of sourceRef starts with the given purl prefix.
84
+ * @param {PackageURL} sourceRef - The source component to check
85
+ * @param {string} purlPrefix - The purl prefix to match (e.g. "pkg:npm/minimist@")
86
+ * @return {boolean}
87
+ */
88
+ checkDependsOnByPurlPrefix(sourceRef, purlPrefix) {
89
+ return this.sbomModel.checkDependsOnByPurlPrefix(sourceRef, purlPrefix);
90
+ }
79
91
  /** Removes the root component from the sbom
80
92
  */
81
93
  removeRootComponent() {
@@ -1,12 +1,10 @@
1
- /// <reference types="node" />
2
- /// <reference types="packageurl-js/src/package-url" />
3
1
  /**
4
2
  *
5
3
  * @param {string} key to log its value from environment variables and from opts, if it exists
6
4
  * @param {{}} [opts={}] different options of application, if key in it, log it.
7
5
  * @param {string }defValue default value of key in case there is no option and environment variable values for key
8
6
  */
9
- export function logValueFromObjects(key: string, opts?: {} | undefined, defValue: string): void;
7
+ export function logValueFromObjects(key: string, opts?: {}, defValue: string): void;
10
8
  /**
11
9
  * Utility function will return the value for key from the environment variables,
12
10
  * if not present will return the value for key from the opts objects only if it's a string,
@@ -17,7 +15,7 @@ export function logValueFromObjects(key: string, opts?: {} | undefined, defValue
17
15
  * @returns {string|null} the value of the key found in the environment, options object, or the
18
16
  * default supplied
19
17
  */
20
- export function getCustom(key: string, def?: string | null | undefined, opts?: {} | undefined): string | null;
18
+ export function getCustom(key: string, def?: string | null, opts?: {}): string | null;
21
19
  /**
22
20
  * Utility function for looking up custom variable for a binary path.
23
21
  * Will look in the environment variables (1) or in opts (2) for a key with TRUSTIFY_DA_x_PATH, x is an
@@ -28,7 +26,7 @@ export function getCustom(key: string, def?: string | null | undefined, opts?: {
28
26
  * @returns {string|null} the value of the key found in the environment, options object, or the
29
27
  * original name supplied
30
28
  */
31
- export function getCustomPath(name: any, opts?: {} | undefined): string | null;
29
+ export function getCustomPath(name: any, opts?: {}): string | null;
32
30
  /**
33
31
  * Utility function for determining whether wrappers for build tools such as gradlew/mvnw should be
34
32
  * preferred over invoking the binary directly.
@@ -63,12 +61,56 @@ export function toPurlFromString(strPurl: any): PackageURL | null;
63
61
  * @param {string} cwd - directory for which to find the root of the git repository.
64
62
  */
65
63
  export function getGitRootDir(cwd: string): string | undefined;
64
+ /**
65
+ * Normalize a filesystem path, lowercasing on Windows for case-insensitive comparison.
66
+ *
67
+ * @param {string} thePath
68
+ * @returns {string}
69
+ */
70
+ export function normalizePath(thePath: string): string;
71
+ /**
72
+ * Walk up from `startDir` to `repoRoot` looking for an executable wrapper script.
73
+ *
74
+ * @param {string} startDir - Absolute directory to start from
75
+ * @param {string} wrapperName - Wrapper filename (e.g. `mvnw`, `gradlew`)
76
+ * @param {string} [repoRoot] - Stop boundary (defaults to git root or filesystem root)
77
+ * @returns {string | undefined}
78
+ */
79
+ export function traverseForWrapper(startDir: string, wrapperName: string, repoRoot?: string): string | undefined;
80
+ /**
81
+ * Resolve a build-tool binary, preferring a wrapper when configured.
82
+ *
83
+ * @param {string} globalBinary - Global binary name (e.g. `mvn`, `gradle`)
84
+ * @param {string} localWrapper - Wrapper filename (e.g. `mvnw`, `gradlew.bat`)
85
+ * @param {string} startDir - Directory from which to start the wrapper search
86
+ * @param {import('./index.js').Options} [opts={}]
87
+ * @returns {string} Path to the resolved binary
88
+ */
89
+ export function resolveBinary(globalBinary: string, localWrapper: string, startDir: string, opts?: import("./index.js").Options): string;
66
90
  /** this method invokes command string in a process in a synchronous way.
67
91
  * @param {string} bin - the command to be invoked
68
92
  * @param {Array<string>} args - the args to pass to the binary
69
93
  * @param {import('child_process').ExecFileOptionsWithStringEncoding} [opts={}]
70
94
  * @returns {string}
71
95
  */
72
- export function invokeCommand(bin: string, args: Array<string>, opts?: import("child_process").ExecFileOptionsWithStringEncoding | undefined): string;
96
+ export function invokeCommand(bin: string, args: Array<string>, opts?: import("child_process").ExecFileOptionsWithStringEncoding): string;
97
+ /**
98
+ * Adds proxy agent configuration to fetch options if a proxy URL is specified
99
+ * @param {RequestInit} options - The base fetch options
100
+ * @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
101
+ * @returns {RequestInit} The fetch options with proxy agent if applicable
102
+ */
103
+ export function addProxyAgent(options: RequestInit, opts: any): RequestInit;
104
+ /**
105
+ * Utility function for fetching vendor tokens
106
+ * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
107
+ * @returns {{}}
108
+ */
109
+ export function getTokenHeaders(opts?: any): {};
73
110
  export const RegexNotToBeLogged: RegExp;
111
+ export const TRUSTIFY_DA_TOKEN_HEADER: "trust-da-token";
112
+ export const TRUSTIFY_DA_TELEMETRY_ID_HEADER: "telemetry-anonymous-id";
113
+ export const TRUSTIFY_DA_SOURCE_HEADER: "trust-da-source";
114
+ export const TRUSTIFY_DA_OPERATION_TYPE_HEADER: "trust-da-operation-type";
115
+ export const TRUSTIFY_DA_PACKAGE_MANAGER_HEADER: "trust-da-pkg-manager";
74
116
  import { PackageURL } from "packageurl-js";
package/dist/src/tools.js CHANGED
@@ -1,7 +1,10 @@
1
1
  import { execFileSync } from "child_process";
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
2
4
  import { EOL } from "os";
5
+ import { HttpsProxyAgent } from "https-proxy-agent";
3
6
  import { PackageURL } from "packageurl-js";
4
- export const RegexNotToBeLogged = /TRUSTIFY_DA_.*_TOKEN|ex-.*-token/;
7
+ export const RegexNotToBeLogged = /TRUSTIFY_DA_(.*_)?TOKEN|ex-.*-token|trust-.*-token/;
5
8
  /**
6
9
  *
7
10
  * @param {string} key to log its value from environment variables and from opts, if it exists
@@ -127,6 +130,62 @@ export function getGitRootDir(cwd) {
127
130
  return undefined;
128
131
  }
129
132
  }
133
+ /**
134
+ * Normalize a filesystem path, lowercasing on Windows for case-insensitive comparison.
135
+ *
136
+ * @param {string} thePath
137
+ * @returns {string}
138
+ */
139
+ export function normalizePath(thePath) {
140
+ const normalized = path.resolve(thePath).normalize();
141
+ return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
142
+ }
143
+ /**
144
+ * Walk up from `startDir` to `repoRoot` looking for an executable wrapper script.
145
+ *
146
+ * @param {string} startDir - Absolute directory to start from
147
+ * @param {string} wrapperName - Wrapper filename (e.g. `mvnw`, `gradlew`)
148
+ * @param {string} [repoRoot] - Stop boundary (defaults to git root or filesystem root)
149
+ * @returns {string | undefined}
150
+ */
151
+ export function traverseForWrapper(startDir, wrapperName, repoRoot = undefined) {
152
+ const currentDir = normalizePath(startDir);
153
+ repoRoot = repoRoot || getGitRootDir(currentDir) || path.parse(currentDir).root;
154
+ const wrapperPath = path.join(currentDir, wrapperName);
155
+ try {
156
+ fs.accessSync(wrapperPath, fs.constants.X_OK);
157
+ return wrapperPath;
158
+ }
159
+ catch {
160
+ const rootDir = path.parse(currentDir).root;
161
+ if (currentDir === repoRoot || currentDir === rootDir) {
162
+ return undefined;
163
+ }
164
+ const parentDir = path.dirname(currentDir);
165
+ if (parentDir === currentDir || parentDir === rootDir) {
166
+ return undefined;
167
+ }
168
+ return traverseForWrapper(parentDir, wrapperName, repoRoot);
169
+ }
170
+ }
171
+ /**
172
+ * Resolve a build-tool binary, preferring a wrapper when configured.
173
+ *
174
+ * @param {string} globalBinary - Global binary name (e.g. `mvn`, `gradle`)
175
+ * @param {string} localWrapper - Wrapper filename (e.g. `mvnw`, `gradlew.bat`)
176
+ * @param {string} startDir - Directory from which to start the wrapper search
177
+ * @param {import('./index.js').Options} [opts={}]
178
+ * @returns {string} Path to the resolved binary
179
+ */
180
+ export function resolveBinary(globalBinary, localWrapper, startDir, opts = {}) {
181
+ if (getWrapperPreference(globalBinary, opts)) {
182
+ const wrapper = traverseForWrapper(startDir, localWrapper);
183
+ if (wrapper !== undefined) {
184
+ return wrapper;
185
+ }
186
+ }
187
+ return getCustomPath(globalBinary, opts);
188
+ }
130
189
  /** this method invokes command string in a process in a synchronous way.
131
190
  * @param {string} bin - the command to be invoked
132
191
  * @param {Array<string>} args - the args to pass to the binary
@@ -157,3 +216,57 @@ export function invokeCommand(bin, args, opts = {}) {
157
216
  };
158
217
  return execFileSync(bin, args, { ...{ stdio: 'pipe', encoding: 'utf-8' }, ...opts });
159
218
  }
219
+ export const TRUSTIFY_DA_TOKEN_HEADER = "trust-da-token";
220
+ export const TRUSTIFY_DA_TELEMETRY_ID_HEADER = "telemetry-anonymous-id";
221
+ export const TRUSTIFY_DA_SOURCE_HEADER = "trust-da-source";
222
+ export const TRUSTIFY_DA_OPERATION_TYPE_HEADER = "trust-da-operation-type";
223
+ export const TRUSTIFY_DA_PACKAGE_MANAGER_HEADER = "trust-da-pkg-manager";
224
+ /**
225
+ * Adds proxy agent configuration to fetch options if a proxy URL is specified
226
+ * @param {RequestInit} options - The base fetch options
227
+ * @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
228
+ * @returns {RequestInit} The fetch options with proxy agent if applicable
229
+ */
230
+ export function addProxyAgent(options, opts) {
231
+ const proxyUrl = getCustom('TRUSTIFY_DA_PROXY_URL', null, opts);
232
+ if (proxyUrl) {
233
+ options.agent = new HttpsProxyAgent(proxyUrl);
234
+ }
235
+ return options;
236
+ }
237
+ /**
238
+ * Utility function for fetching vendor tokens
239
+ * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
240
+ * @returns {{}}
241
+ */
242
+ export function getTokenHeaders(opts = {}) {
243
+ let headers = {};
244
+ setCustomHeader(TRUSTIFY_DA_TOKEN_HEADER, headers, 'TRUSTIFY_DA_TOKEN', opts);
245
+ setCustomHeader(TRUSTIFY_DA_SOURCE_HEADER, headers, 'TRUSTIFY_DA_SOURCE', opts);
246
+ setCustomHeader(TRUSTIFY_DA_OPERATION_TYPE_HEADER, headers, TRUSTIFY_DA_OPERATION_TYPE_HEADER.toUpperCase().replaceAll("-", "_"), opts);
247
+ setCustomHeader(TRUSTIFY_DA_PACKAGE_MANAGER_HEADER, headers, TRUSTIFY_DA_PACKAGE_MANAGER_HEADER.toUpperCase().replaceAll("-", "_"), opts);
248
+ setCustomHeader(TRUSTIFY_DA_TELEMETRY_ID_HEADER, headers, 'TRUSTIFY_DA_TELEMETRY_ID', opts);
249
+ if (getCustom("TRUSTIFY_DA_DEBUG", null, opts) === "true") {
250
+ console.log("Headers Values to be sent to Trustify DA backend:" + EOL);
251
+ for (const headerKey in headers) {
252
+ if (!headerKey.match(RegexNotToBeLogged)) {
253
+ console.log(`${headerKey}: ${headers[headerKey]}`);
254
+ }
255
+ }
256
+ }
257
+ return headers;
258
+ }
259
+ /**
260
+ *
261
+ * @param {string} headerName - the header name to populate in request
262
+ * @param headers
263
+ * @param {string} optsKey - key in the options object to use the value for
264
+ * @param {import("index.js").Options} [opts={}] - options input object to fetch header values from
265
+ * @private
266
+ */
267
+ function setCustomHeader(headerName, headers, optsKey, opts) {
268
+ let customHeaderValue = getCustom(optsKey, null, opts);
269
+ if (customHeaderValue) {
270
+ headers[headerName] = customHeaderValue;
271
+ }
272
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Updates dependency versions in a Maven pom.xml while preserving file formatting.
3
+ *
4
+ * Uses `fast-xml-parser` to understand the XML structure (dependencies and properties),
5
+ * then performs position-based string splice to replace version strings without altering
6
+ * any other formatting, comments, whitespace, or indentation.
7
+ *
8
+ * Supports `${property}` indirection: when a dependency's `<version>` is a property
9
+ * reference, the updater resolves the property chain (including recursive references)
10
+ * and updates the terminal property value in `<properties>` instead.
11
+ *
12
+ * @param {string} pomContent - the raw pom.xml file content
13
+ * @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges -
14
+ * list of version changes to apply
15
+ * @returns {{content: string, applied: Array, skipped: Array}} the updated content and
16
+ * lists of applied and skipped changes
17
+ */
18
+ export function updateMavenVersions(pomContent: string, versionChanges: Array<{
19
+ groupId: string;
20
+ artifactId: string;
21
+ newVersion: string;
22
+ }>): {
23
+ content: string;
24
+ applied: any[];
25
+ skipped: any[];
26
+ };