@trustify-da/trustify-da-javascript-client 0.3.0-ea.51c394b → 0.3.0-ea.5348b9e

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.
@@ -18,9 +18,14 @@ export default class CycloneDxSbom {
18
18
  * Adds a dependency relationship between two components in the SBOM
19
19
  * @param {PackageURL} sourceRef - The source component (parent)
20
20
  * @param {PackageURL} targetRef - The target component (dependency)
21
+ * @param {string} [scope] - Scope of the dependency
22
+ * @param {Array<{alg: string, content: string}>} [targetHashes] - Optional hashes for the target component
21
23
  * @return {CycloneDxSbom} The updated SBOM
22
24
  */
23
- addDependency(sourceRef: PackageURL, targetRef: PackageURL, scope: any): CycloneDxSbom;
25
+ addDependency(sourceRef: PackageURL, targetRef: PackageURL, scope?: string, targetHashes?: Array<{
26
+ alg: string;
27
+ content: string;
28
+ }>): CycloneDxSbom;
24
29
  /** @param {{}} opts - various options, settings and configuration of application.
25
30
  * @return String CycloneDx Sbom json object in a string format
26
31
  */
@@ -50,6 +55,7 @@ export default class CycloneDxSbom {
50
55
  version: any;
51
56
  scope: any;
52
57
  licenses?: any;
58
+ hashes?: any;
53
59
  };
54
60
  /**
55
61
  * This method gets an array of dependencies to be ignored, and remove all of them from CycloneDx Sbom
@@ -6,10 +6,11 @@ import { PackageURL } from "packageurl-js";
6
6
  * @param type type of package - application or library
7
7
  * @param scope scope of the component - runtime or compile
8
8
  * @param licenses optional license string or array of licenses for the component
9
- * @return {{"bom-ref": string, name, purl: string, type, version, scope, licenses?}}
9
+ * @param hashes optional array of hash objects for the component, e.g. [{alg: "SHA-256", content: "..."}]
10
+ * @return {{"bom-ref": string, name, purl: string, type, version, scope, licenses?, hashes?}}
10
11
  * @private
11
12
  */
12
- function getComponent(component, type, scope, licenses) {
13
+ function getComponent(component, type, scope, licenses, hashes) {
13
14
  let componentObject;
14
15
  if (component instanceof PackageURL) {
15
16
  if (component.namespace) {
@@ -47,6 +48,10 @@ function getComponent(component, type, scope, licenses) {
47
48
  return lic;
48
49
  });
49
50
  }
51
+ // Add hashes if provided (CycloneDX 1.4 format).
52
+ if (hashes && hashes.length > 0) {
53
+ componentObject.hashes = hashes;
54
+ }
50
55
  return componentObject;
51
56
  }
52
57
  function createDependency(dependency) {
@@ -86,16 +91,24 @@ export default class CycloneDxSbom {
86
91
  * Adds a dependency relationship between two components in the SBOM
87
92
  * @param {PackageURL} sourceRef - The source component (parent)
88
93
  * @param {PackageURL} targetRef - The target component (dependency)
94
+ * @param {string} [scope] - Scope of the dependency
95
+ * @param {Array<{alg: string, content: string}>} [targetHashes] - Optional hashes for the target component
89
96
  * @return {CycloneDxSbom} The updated SBOM
90
97
  */
91
- addDependency(sourceRef, targetRef, scope) {
98
+ addDependency(sourceRef, targetRef, scope, targetHashes) {
92
99
  const sourcePurl = sourceRef.toString();
93
100
  const targetPurl = targetRef.toString();
94
101
  // Ensure both components exist in the components list
95
102
  [sourceRef, targetRef].forEach((ref, index) => {
96
103
  const purl = index === 0 ? sourcePurl : targetPurl;
97
- if (this.getComponentIndex(purl) < 0) {
98
- this.components.push(getComponent(ref, "library", scope));
104
+ const existingIndex = this.getComponentIndex(purl);
105
+ if (existingIndex < 0) {
106
+ const hashes = index === 1 ? targetHashes : undefined;
107
+ this.components.push(getComponent(ref, "library", scope, undefined, hashes));
108
+ }
109
+ else if (index === 1 && targetHashes && targetHashes.length > 0 && !this.components[existingIndex].hashes) {
110
+ // Update hashes if the component was first seen without them (e.g. as a source)
111
+ this.components[existingIndex].hashes = targetHashes;
99
112
  }
100
113
  });
101
114
  // Ensure source dependency exists
@@ -136,19 +136,74 @@ declare function stackAnalysis(manifest: string, html: false, opts?: Options | u
136
136
  * or backend request failed
137
137
  */
138
138
  declare function stackAnalysis(manifest: string, html?: boolean | undefined, opts?: Options | undefined): Promise<string | import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport>;
139
+ /**
140
+ * @overload
141
+ * @param {string} workspaceRoot
142
+ * @param {true} html
143
+ * @param {Options & { batchMetadata: true }} opts
144
+ * @returns {Promise<{ analysis: string, metadata: BatchAnalysisMetadata }>}
145
+ * @throws {Error}
146
+ */
147
+ declare function stackAnalysisBatch(workspaceRoot: string, html: true, opts: Options & {
148
+ batchMetadata: true;
149
+ }): Promise<{
150
+ analysis: string;
151
+ metadata: BatchAnalysisMetadata;
152
+ }>;
153
+ /**
154
+ * @overload
155
+ * @param {string} workspaceRoot
156
+ * @param {true} html
157
+ * @param {Options & { batchMetadata?: false }} [opts={}]
158
+ * @returns {Promise<string>}
159
+ * @throws {Error}
160
+ */
161
+ declare function stackAnalysisBatch(workspaceRoot: string, html: true, opts?: (Options & {
162
+ batchMetadata?: false;
163
+ }) | undefined): Promise<string>;
164
+ /**
165
+ * @overload
166
+ * @param {string} workspaceRoot
167
+ * @param {false} html
168
+ * @param {Options & { batchMetadata: true }} opts
169
+ * @returns {Promise<{ analysis: Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>, metadata: BatchAnalysisMetadata }>}
170
+ * @throws {Error}
171
+ */
172
+ declare function stackAnalysisBatch(workspaceRoot: string, html: false, opts: Options & {
173
+ batchMetadata: true;
174
+ }): Promise<{
175
+ analysis: {
176
+ [x: string]: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport;
177
+ };
178
+ metadata: BatchAnalysisMetadata;
179
+ }>;
180
+ /**
181
+ * @overload
182
+ * @param {string} workspaceRoot
183
+ * @param {false} html
184
+ * @param {Options & { batchMetadata?: false }} [opts={}]
185
+ * @returns {Promise<Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>>}
186
+ * @throws {Error}
187
+ */
188
+ declare function stackAnalysisBatch(workspaceRoot: string, html: false, opts?: (Options & {
189
+ batchMetadata?: false;
190
+ }) | undefined): Promise<{
191
+ [x: string]: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport;
192
+ }>;
139
193
  /**
140
194
  * Get stack analysis for all workspace packages/crates (batch).
141
195
  * Detects ecosystem from workspace root: Cargo (Cargo.toml + Cargo.lock) or JS/TS (package.json + lock file).
142
196
  * SBOMs are generated in parallel (see `batchConcurrency`) unless `continueOnError: false` (fail-fast sequential).
143
197
  * With `opts.batchMetadata` / `TRUSTIFY_DA_BATCH_METADATA`, returns `{ analysis, metadata }` including validation and SBOM errors.
144
198
  *
199
+ * @overload
145
200
  * @param {string} workspaceRoot - Path to workspace root (containing lock file and workspace config)
146
201
  * @param {boolean} [html=false] - true returns HTML, false returns JSON report
147
202
  * @param {Options} [opts={}] - `batchConcurrency`, discovery ignores, `continueOnError` (default true), `batchMetadata` (default false)
148
203
  * @returns {Promise<string|Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>|{ analysis: string|Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>, metadata: BatchAnalysisMetadata }>}
149
204
  * @throws {Error} if workspace root invalid, no manifests found, no packages pass validation, no SBOMs produced, or backend request failed. When `opts.batchMetadata` is set, `error.batchMetadata` may be set on thrown errors.
150
205
  */
151
- declare function stackAnalysisBatch(workspaceRoot: string, html?: boolean, opts?: Options): Promise<string | {
206
+ declare function stackAnalysisBatch(workspaceRoot: string, html?: boolean | undefined, opts?: Options | undefined): Promise<string | {
152
207
  [x: string]: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport;
153
208
  } | {
154
209
  analysis: string | {
@@ -196,6 +251,7 @@ declare function imageAnalysis(imageRefs: Array<string>, html?: boolean | undefi
196
251
  * @throws {Error} if the backend request failed.
197
252
  */
198
253
  declare function validateToken(opts?: Options): Promise<object>;
254
+ import { discoverMavenModules } from './providers/java_maven.js';
199
255
  import { discoverWorkspacePackages } from './workspace.js';
200
256
  import { discoverWorkspaceCrates } from './workspace.js';
201
257
  import { validatePackageJson } from './workspace.js';
@@ -203,5 +259,5 @@ import { resolveWorkspaceDiscoveryIgnore } from './workspace.js';
203
259
  import { filterManifestPathsByDiscoveryIgnore } from './workspace.js';
204
260
  import { resolveContinueOnError } from './batch_opts.js';
205
261
  import { resolveBatchMetadata } from './batch_opts.js';
206
- export { discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata };
262
+ export { discoverMavenModules, discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata };
207
263
  export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
package/dist/src/index.js CHANGED
@@ -6,6 +6,7 @@ import analysis from './analysis.js';
6
6
  import fs from 'node:fs';
7
7
  import { getCustom } from "./tools.js";
8
8
  import { resolveBatchMetadata, resolveContinueOnError } from './batch_opts.js';
9
+ import { discoverMavenModules } from './providers/java_maven.js';
9
10
  import { discoverWorkspaceCrates, discoverWorkspacePackages, filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore, validatePackageJson, } from './workspace.js';
10
11
  import.meta.dirname;
11
12
  import * as url from 'url';
@@ -13,7 +14,7 @@ export { parseImageRef } from "./oci_image/utils.js";
13
14
  export { ImageRef } from "./oci_image/images.js";
14
15
  export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
15
16
  export default { componentAnalysis, stackAnalysis, stackAnalysisBatch, imageAnalysis, validateToken, generateSbom };
16
- export { discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata, };
17
+ export { discoverMavenModules, discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata, };
17
18
  /**
18
19
  * @typedef {{
19
20
  * TRUSTIFY_DA_CARGO_PATH?: string | undefined,
@@ -279,16 +280,23 @@ async function generateOneSbom(manifestPath, workspaceOpts) {
279
280
  *
280
281
  * @param {string} root - Resolved workspace root
281
282
  * @param {Options} opts
282
- * @returns {Promise<{ ecosystem: 'javascript' | 'cargo' | 'unknown', manifestPaths: string[] }>}
283
+ * @returns {Promise<{ ecosystem: 'javascript' | 'cargo' | 'maven' | 'unknown', manifestPaths: string[] }>}
283
284
  * @private
284
285
  */
285
286
  async function detectWorkspaceManifests(root, opts) {
286
287
  const cargoToml = path.join(root, 'Cargo.toml');
287
288
  const cargoLock = path.join(root, 'Cargo.lock');
288
289
  const packageJson = path.join(root, 'package.json');
290
+ const pomXml = path.join(root, 'pom.xml');
289
291
  if (fs.existsSync(cargoToml) && fs.existsSync(cargoLock)) {
290
292
  return { ecosystem: 'cargo', manifestPaths: await discoverWorkspaceCrates(root, opts) };
291
293
  }
294
+ if (fs.existsSync(pomXml)) {
295
+ const manifestPaths = await discoverMavenModules(root, opts);
296
+ if (manifestPaths.length > 0) {
297
+ return { ecosystem: 'maven', manifestPaths };
298
+ }
299
+ }
292
300
  const hasJsLock = fs.existsSync(path.join(root, 'pnpm-lock.yaml'))
293
301
  || fs.existsSync(path.join(root, 'yarn.lock'))
294
302
  || fs.existsSync(path.join(root, 'package-lock.json'));
@@ -398,12 +406,45 @@ function batchError(message, wantMetadata, metadata) {
398
406
  }
399
407
  return err;
400
408
  }
409
+ /**
410
+ * @overload
411
+ * @param {string} workspaceRoot
412
+ * @param {true} html
413
+ * @param {Options & { batchMetadata: true }} opts
414
+ * @returns {Promise<{ analysis: string, metadata: BatchAnalysisMetadata }>}
415
+ * @throws {Error}
416
+ */
417
+ /**
418
+ * @overload
419
+ * @param {string} workspaceRoot
420
+ * @param {true} html
421
+ * @param {Options & { batchMetadata?: false }} [opts={}]
422
+ * @returns {Promise<string>}
423
+ * @throws {Error}
424
+ */
425
+ /**
426
+ * @overload
427
+ * @param {string} workspaceRoot
428
+ * @param {false} html
429
+ * @param {Options & { batchMetadata: true }} opts
430
+ * @returns {Promise<{ analysis: Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>, metadata: BatchAnalysisMetadata }>}
431
+ * @throws {Error}
432
+ */
433
+ /**
434
+ * @overload
435
+ * @param {string} workspaceRoot
436
+ * @param {false} html
437
+ * @param {Options & { batchMetadata?: false }} [opts={}]
438
+ * @returns {Promise<Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>>}
439
+ * @throws {Error}
440
+ */
401
441
  /**
402
442
  * Get stack analysis for all workspace packages/crates (batch).
403
443
  * Detects ecosystem from workspace root: Cargo (Cargo.toml + Cargo.lock) or JS/TS (package.json + lock file).
404
444
  * SBOMs are generated in parallel (see `batchConcurrency`) unless `continueOnError: false` (fail-fast sequential).
405
445
  * With `opts.batchMetadata` / `TRUSTIFY_DA_BATCH_METADATA`, returns `{ analysis, metadata }` including validation and SBOM errors.
406
446
  *
447
+ * @overload
407
448
  * @param {string} workspaceRoot - Path to workspace root (containing lock file and workspace config)
408
449
  * @param {boolean} [html=false] - true returns HTML, false returns JSON report
409
450
  * @param {Options} [opts={}] - `batchConcurrency`, discovery ignores, `continueOnError` (default true), `batchMetadata` (default false)
@@ -7,6 +7,7 @@ import Javascript_npm from './providers/javascript_npm.js';
7
7
  import Javascript_pnpm from './providers/javascript_pnpm.js';
8
8
  import Javascript_yarn from './providers/javascript_yarn.js';
9
9
  import pythonPipProvider from './providers/python_pip.js';
10
+ import Python_pip_pyproject from './providers/python_pip_pyproject.js';
10
11
  import Python_poetry from './providers/python_poetry.js';
11
12
  import Python_uv from './providers/python_uv.js';
12
13
  import rustCargoProvider from './providers/rust_cargo.js';
@@ -27,6 +28,7 @@ export const availableProviders = [
27
28
  pythonPipProvider,
28
29
  new Python_poetry(),
29
30
  new Python_uv(),
31
+ new Python_pip_pyproject(),
30
32
  rustCargoProvider
31
33
  ];
32
34
  /**
@@ -57,15 +57,6 @@ export default class Base_Java {
57
57
  * @returns string
58
58
  */
59
59
  selectToolBinary(manifestPath: string, opts: {}): string | null;
60
- /**
61
- *
62
- * @param {string} startingManifest - the path of the manifest from which to start searching for the wrapper
63
- * @param {string} repoRoot - the root of the repository at which point to stop searching for mvnw, derived via git if unset and then fallsback
64
- * to the root of the drive the manifest is on (assumes absolute path is given)
65
- * @returns {string|undefined}
66
- */
67
- traverseForWrapper(startingManifest: string, repoRoot?: string): string | undefined;
68
- normalizePath(thePath: any): string;
69
60
  #private;
70
61
  }
71
62
  export type Provided = import("../provider").Provided;
@@ -1,7 +1,6 @@
1
- import fs from 'node:fs';
2
1
  import path from 'node:path';
3
2
  import { PackageURL } from 'packageurl-js';
4
- import { getCustomPath, getGitRootDir, getWrapperPreference, invokeCommand } from "../tools.js";
3
+ import { getCustomPath, getWrapperPreference, invokeCommand, traverseForWrapper } from "../tools.js";
5
4
  /** @typedef {import('../provider').Provider} */
6
5
  /** @typedef {import('../provider').Provided} Provided */
7
6
  /** @typedef {{name: string, version: string}} Package */
@@ -131,7 +130,7 @@ export default class Base_Java {
131
130
  const toolPath = getCustomPath(this.globalBinary, opts);
132
131
  const useWrapper = getWrapperPreference(this.globalBinary, opts);
133
132
  if (useWrapper) {
134
- const wrapper = this.traverseForWrapper(manifestPath);
133
+ const wrapper = traverseForWrapper(manifestDir, this.localWrapper);
135
134
  if (wrapper !== undefined) {
136
135
  try {
137
136
  this._invokeCommand(wrapper, ['--version'], { cwd: manifestDir });
@@ -156,39 +155,4 @@ export default class Base_Java {
156
155
  }
157
156
  return toolPath;
158
157
  }
159
- /**
160
- *
161
- * @param {string} startingManifest - the path of the manifest from which to start searching for the wrapper
162
- * @param {string} repoRoot - the root of the repository at which point to stop searching for mvnw, derived via git if unset and then fallsback
163
- * to the root of the drive the manifest is on (assumes absolute path is given)
164
- * @returns {string|undefined}
165
- */
166
- traverseForWrapper(startingManifest, repoRoot = undefined) {
167
- const normalizedManifest = this.normalizePath(startingManifest);
168
- const currentDir = this.normalizePath(path.dirname(normalizedManifest));
169
- repoRoot = repoRoot || getGitRootDir(currentDir) || path.parse(normalizedManifest).root;
170
- const wrapperPath = path.join(currentDir, this.localWrapper);
171
- try {
172
- fs.accessSync(wrapperPath, fs.constants.X_OK);
173
- return wrapperPath;
174
- }
175
- catch (error) {
176
- if (error.code === 'ENOENT') {
177
- const rootDir = path.parse(currentDir).root;
178
- if (currentDir === repoRoot || currentDir === rootDir) {
179
- return undefined;
180
- }
181
- const parentDir = path.dirname(currentDir);
182
- if (parentDir === currentDir || parentDir === rootDir) {
183
- return undefined;
184
- }
185
- return this.traverseForWrapper(path.join(parentDir, path.basename(normalizedManifest)), repoRoot);
186
- }
187
- throw new Error(`failure searching for ${this.localWrapper}`, { cause: error });
188
- }
189
- }
190
- normalizePath(thePath) {
191
- const normalized = path.resolve(thePath).normalize();
192
- return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
193
- }
194
158
  }
@@ -1,4 +1,4 @@
1
- /** @typedef {{name: string, version: string, children: string[]}} GraphEntry */
1
+ /** @typedef {{name: string, version: string, children: string[], hashes?: Array<{alg: string, content: string}>}} GraphEntry */
2
2
  /** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
3
3
  /** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
4
4
  /** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
@@ -102,26 +102,14 @@ export default class Base_pyproject {
102
102
  */
103
103
  protected _getIgnoredDeps(manifestPath: string): Set<string>;
104
104
  /**
105
- * Build dependency tree from graph, starting from direct deps.
105
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
106
106
  * @param {Map<string, GraphEntry>} graph
107
- * @param {string[]} directDeps - canonical names of direct deps
107
+ * @param {string[]} directDeps
108
108
  * @param {Set<string>} ignoredDeps
109
- * @param {boolean} includeTransitive
110
- * @returns {DepTreeEntry[]}
111
- * @protected
112
- */
113
- protected _buildDependencyTree(graph: Map<string, GraphEntry>, directDeps: string[], ignoredDeps: Set<string>, includeTransitive: boolean): DepTreeEntry[];
114
- /**
115
- * Recursively collect transitive dependencies.
116
- * @param {Map<string, GraphEntry>} graph
117
- * @param {string[]} childKeys
118
- * @param {DepTreeEntry[]} result - mutated in place
119
- * @param {Set<string>} ignoredDeps
120
- * @param {Set<string>} visited
121
- * @returns {void}
109
+ * @returns {Set<string>}
122
110
  * @protected
123
111
  */
124
- protected _collectTransitive(graph: Map<string, GraphEntry>, childKeys: string[], result: DepTreeEntry[], ignoredDeps: Set<string>, visited: Set<string>): void;
112
+ protected _reachableNodes(graph: Map<string, GraphEntry>, directDeps: string[], ignoredDeps: Set<string>): Set<string>;
125
113
  /**
126
114
  * @param {string} name
127
115
  * @param {string} version
@@ -129,15 +117,6 @@ export default class Base_pyproject {
129
117
  * @protected
130
118
  */
131
119
  protected _toPurl(name: string, version: string): PackageURL;
132
- /**
133
- * Recursively add a dependency and its transitive deps to the SBOM.
134
- * @param {PackageURL} source
135
- * @param {DepTreeEntry} dep
136
- * @param {Sbom} sbom
137
- * @returns {void}
138
- * @private
139
- */
140
- private _addAllDependencies;
141
120
  /**
142
121
  * Create SBOM json string for a pyproject.toml project.
143
122
  * @param {string} manifest - path to pyproject.toml
@@ -152,6 +131,10 @@ export type GraphEntry = {
152
131
  name: string;
153
132
  version: string;
154
133
  children: string[];
134
+ hashes?: Array<{
135
+ alg: string;
136
+ content: string;
137
+ }>;
155
138
  };
156
139
  export type DepTreeEntry = {
157
140
  name: string;
@@ -7,9 +7,10 @@ import Sbom from '../sbom.js';
7
7
  import { getCustom } from '../tools.js';
8
8
  const ecosystem = 'pip';
9
9
  const IGNORE_MARKERS = ['exhortignore', 'trustify-da-ignore'];
10
+ const NO_SCOPE = undefined;
10
11
  const DEFAULT_ROOT_NAME = 'default-pip-root';
11
12
  const DEFAULT_ROOT_VERSION = '0.0.0';
12
- /** @typedef {{name: string, version: string, children: string[]}} GraphEntry */
13
+ /** @typedef {{name: string, version: string, children: string[], hashes?: Array<{alg: string, content: string}>}} GraphEntry */
13
14
  /** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
14
15
  /** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
15
16
  /** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
@@ -220,64 +221,29 @@ export default class Base_pyproject {
220
221
  return ignored;
221
222
  }
222
223
  /**
223
- * Build dependency tree from graph, starting from direct deps.
224
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
224
225
  * @param {Map<string, GraphEntry>} graph
225
- * @param {string[]} directDeps - canonical names of direct deps
226
+ * @param {string[]} directDeps
226
227
  * @param {Set<string>} ignoredDeps
227
- * @param {boolean} includeTransitive
228
- * @returns {DepTreeEntry[]}
229
- * @protected
230
- */
231
- _buildDependencyTree(graph, directDeps, ignoredDeps, includeTransitive) {
232
- let result = [];
233
- for (let key of directDeps) {
234
- if (ignoredDeps.has(key)) {
235
- continue;
236
- }
237
- let entry = graph.get(key);
238
- if (!entry) {
239
- continue;
240
- }
241
- let depTree = [];
242
- if (includeTransitive) {
243
- let visited = new Set();
244
- visited.add(key);
245
- this._collectTransitive(graph, entry.children, depTree, ignoredDeps, visited);
246
- }
247
- result.push({ name: entry.name, version: entry.version, dependencies: depTree });
248
- }
249
- result.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
250
- return result;
251
- }
252
- /**
253
- * Recursively collect transitive dependencies.
254
- * @param {Map<string, GraphEntry>} graph
255
- * @param {string[]} childKeys
256
- * @param {DepTreeEntry[]} result - mutated in place
257
- * @param {Set<string>} ignoredDeps
258
- * @param {Set<string>} visited
259
- * @returns {void}
228
+ * @returns {Set<string>}
260
229
  * @protected
261
230
  */
262
- _collectTransitive(graph, childKeys, result, ignoredDeps, visited) {
263
- for (let childKey of childKeys) {
264
- let canonKey = this._canonicalize(childKey);
265
- if (ignoredDeps.has(canonKey)) {
266
- continue;
267
- }
268
- if (visited.has(canonKey)) {
231
+ _reachableNodes(graph, directDeps, ignoredDeps) {
232
+ let reachable = new Set();
233
+ let queue = directDeps.filter(k => !ignoredDeps.has(k) && graph.has(k));
234
+ while (queue.length > 0) {
235
+ let key = queue.shift();
236
+ if (reachable.has(key)) {
269
237
  continue;
270
238
  }
271
- visited.add(canonKey);
272
- let entry = graph.get(canonKey);
273
- if (!entry) {
274
- continue;
239
+ reachable.add(key);
240
+ for (let child of graph.get(key).children) {
241
+ if (!ignoredDeps.has(child) && graph.has(child) && !reachable.has(child)) {
242
+ queue.push(child);
243
+ }
275
244
  }
276
- let childDeps = [];
277
- this._collectTransitive(graph, entry.children, childDeps, ignoredDeps, visited);
278
- result.push({ name: entry.name, version: entry.version, dependencies: childDeps });
279
245
  }
280
- result.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
246
+ return reachable;
281
247
  }
282
248
  /**
283
249
  * @param {string} name
@@ -288,21 +254,6 @@ export default class Base_pyproject {
288
254
  _toPurl(name, version) {
289
255
  return new PackageURL('pypi', undefined, name, version, undefined, undefined);
290
256
  }
291
- /**
292
- * Recursively add a dependency and its transitive deps to the SBOM.
293
- * @param {PackageURL} source
294
- * @param {DepTreeEntry} dep
295
- * @param {Sbom} sbom
296
- * @returns {void}
297
- * @private
298
- */
299
- _addAllDependencies(source, dep, sbom) {
300
- let targetPurl = this._toPurl(dep.name, dep.version);
301
- sbom.addDependency(source, targetPurl);
302
- if (dep.dependencies && dep.dependencies.length > 0) {
303
- dep.dependencies.forEach(child => this._addAllDependencies(this._toPurl(dep.name, dep.version), child, sbom));
304
- }
305
- }
306
257
  /**
307
258
  * Create SBOM json string for a pyproject.toml project.
308
259
  * @param {string} manifest - path to pyproject.toml
@@ -318,21 +269,47 @@ export default class Base_pyproject {
318
269
  let workspaceDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
319
270
  let { directDeps, graph } = await this._getDependencyData(manifestDir, workspaceDir, parsed, opts);
320
271
  let ignoredDeps = this._getIgnoredDeps(manifest);
321
- let dependencies = this._buildDependencyTree(graph, directDeps, ignoredDeps, includeTransitive);
322
272
  let sbom = new Sbom();
323
273
  let rootName = this._getProjectName(parsed) || DEFAULT_ROOT_NAME;
324
274
  let rootVersion = this._getProjectVersion(parsed) || DEFAULT_ROOT_VERSION;
325
275
  let rootPurl = this._toPurl(rootName, rootVersion);
326
276
  let license = this.readLicenseFromManifest(manifest);
327
277
  sbom.addRoot(rootPurl, license);
328
- dependencies.forEach(dep => {
329
- if (includeTransitive) {
330
- this._addAllDependencies(rootPurl, dep, sbom);
278
+ if (includeTransitive) {
279
+ let reachable = this._reachableNodes(graph, directDeps, ignoredDeps);
280
+ for (let key of directDeps) {
281
+ if (!reachable.has(key)) {
282
+ continue;
283
+ }
284
+ let entry = graph.get(key);
285
+ sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version), NO_SCOPE, entry.hashes);
331
286
  }
332
- else {
333
- sbom.addDependency(rootPurl, this._toPurl(dep.name, dep.version));
287
+ for (let [key, entry] of graph) {
288
+ if (!reachable.has(key)) {
289
+ continue;
290
+ }
291
+ let parentPurl = this._toPurl(entry.name, entry.version);
292
+ for (let child of entry.children) {
293
+ if (!reachable.has(child)) {
294
+ continue;
295
+ }
296
+ let childEntry = graph.get(child);
297
+ sbom.addDependency(parentPurl, this._toPurl(childEntry.name, childEntry.version), NO_SCOPE, childEntry.hashes);
298
+ }
334
299
  }
335
- });
300
+ }
301
+ else {
302
+ for (let key of directDeps) {
303
+ if (ignoredDeps.has(key)) {
304
+ continue;
305
+ }
306
+ let entry = graph.get(key);
307
+ if (!entry) {
308
+ continue;
309
+ }
310
+ sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version), NO_SCOPE, entry.hashes);
311
+ }
312
+ }
336
313
  return sbom.getAsJsonString(opts);
337
314
  }
338
315
  }
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Discover all pom.xml manifest paths in a Maven multi-module project.
3
+ *
4
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pom.xml)
5
+ * @param {object} [opts={}]
6
+ * @returns {Promise<string[]>} Paths to pom.xml files (absolute)
7
+ */
8
+ export function discoverMavenModules(workspaceRoot: string, opts?: object): Promise<string[]>;
1
9
  /** @typedef {import('../provider').Provider} */
2
10
  /** @typedef {import('../provider').Provided} Provided */
3
11
  /** @typedef {{name: string, version: string}} Package */