@trustify-da/trustify-da-javascript-client 0.3.0-ea.e5bb86c → 0.3.0-ea.f136061

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.
package/dist/src/cli.js CHANGED
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env node
2
+ import fs from 'node:fs';
2
3
  import * as path from "path";
3
4
  import yargs from 'yargs';
4
5
  import { hideBin } from 'yargs/helpers';
5
6
  import { getProjectLicense, getLicenseDetails } from './license/index.js';
6
- import client, { selectTrustifyDABackend } from './index.js';
7
+ import client, { selectTrustifyDABackend, generateSbom } from './index.js';
7
8
  // command for component analysis take manifest type and content
8
9
  const component = {
9
10
  command: 'component </path/to/manifest>',
@@ -325,15 +326,63 @@ const license = {
325
326
  console.log(JSON.stringify(output, null, 2));
326
327
  }
327
328
  };
329
+ const sbom = {
330
+ command: 'sbom </path/to/manifest> [--output]',
331
+ desc: 'generate a CycloneDX SBOM from a manifest file',
332
+ builder: yargs => yargs.positional('/path/to/manifest', {
333
+ desc: 'manifest path for SBOM generation',
334
+ type: 'string',
335
+ normalize: true,
336
+ }).options({
337
+ output: {
338
+ alias: 'o',
339
+ desc: 'Write SBOM JSON to a file instead of stdout',
340
+ type: 'string',
341
+ normalize: true,
342
+ },
343
+ workspaceDir: {
344
+ alias: 'w',
345
+ desc: 'Workspace root directory (for monorepos; lock file is expected here)',
346
+ type: 'string',
347
+ normalize: true,
348
+ }
349
+ }),
350
+ handler: async (args) => {
351
+ let manifest = args['/path/to/manifest'];
352
+ const opts = args.workspaceDir ? { TRUSTIFY_DA_WORKSPACE_DIR: args.workspaceDir } : {};
353
+ let result;
354
+ try {
355
+ result = await generateSbom(manifest, opts);
356
+ }
357
+ catch (err) {
358
+ console.error(JSON.stringify({ error: `Failed to generate SBOM: ${err.message}` }, null, 2));
359
+ process.exit(1);
360
+ }
361
+ const json = JSON.stringify(result, null, 2);
362
+ if (args.output) {
363
+ try {
364
+ fs.writeFileSync(args.output, json);
365
+ }
366
+ catch (err) {
367
+ console.error(JSON.stringify({ error: `Failed to write output file: ${err.message}` }, null, 2));
368
+ process.exit(1);
369
+ }
370
+ }
371
+ else {
372
+ console.log(json);
373
+ }
374
+ }
375
+ };
328
376
  // parse and invoke the command
329
377
  yargs(hideBin(process.argv))
330
- .usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|stack-batch|image|validate-token|license}`)
378
+ .usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|stack-batch|image|validate-token|license|sbom}`)
331
379
  .command(stack)
332
380
  .command(stackBatch)
333
381
  .command(component)
334
382
  .command(image)
335
383
  .command(validateToken)
336
384
  .command(license)
385
+ .command(sbom)
337
386
  .scriptName('')
338
387
  .version(false)
339
388
  .demandCommand(1)
@@ -13,6 +13,15 @@ export function selectTrustifyDABackend(opts?: {
13
13
  TRUSTIFY_DA_DEBUG?: string | undefined;
14
14
  TRUSTIFY_DA_BACKEND_URL?: string | undefined;
15
15
  }): string;
16
+ /**
17
+ * Generate a CycloneDX SBOM from a manifest file. No backend HTTP request is made.
18
+ *
19
+ * @param {string} manifestPath - path to the manifest file (e.g. pom.xml, package.json)
20
+ * @param {Options} [opts={}] - optional options (e.g. workspace dir, tool paths)
21
+ * @returns {Promise<object>} parsed CycloneDX SBOM JSON object
22
+ * @throws {Error} if the manifest is unsupported or SBOM generation fails
23
+ */
24
+ export function generateSbom(manifestPath: string, opts?: Options): Promise<object>;
16
25
  export { parseImageRef } from "./oci_image/utils.js";
17
26
  export { ImageRef } from "./oci_image/images.js";
18
27
  declare namespace _default {
@@ -21,6 +30,7 @@ declare namespace _default {
21
30
  export { stackAnalysisBatch };
22
31
  export { imageAnalysis };
23
32
  export { validateToken };
33
+ export { generateSbom };
24
34
  }
25
35
  export default _default;
26
36
  export type Options = {
package/dist/src/index.js CHANGED
@@ -12,7 +12,7 @@ import * as url from 'url';
12
12
  export { parseImageRef } from "./oci_image/utils.js";
13
13
  export { ImageRef } from "./oci_image/images.js";
14
14
  export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
15
- export default { componentAnalysis, stackAnalysis, stackAnalysisBatch, imageAnalysis, validateToken };
15
+ export default { componentAnalysis, stackAnalysis, stackAnalysisBatch, imageAnalysis, validateToken, generateSbom };
16
16
  export { discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata, };
17
17
  /**
18
18
  * @typedef {{
@@ -237,6 +237,22 @@ function buildBatchAnalysisMetadata(root, ecosystem, totalSbomAttempts, successf
237
237
  errors: [...errors],
238
238
  };
239
239
  }
240
+ /**
241
+ * Generate a CycloneDX SBOM from a manifest file. No backend HTTP request is made.
242
+ *
243
+ * @param {string} manifestPath - path to the manifest file (e.g. pom.xml, package.json)
244
+ * @param {Options} [opts={}] - optional options (e.g. workspace dir, tool paths)
245
+ * @returns {Promise<object>} parsed CycloneDX SBOM JSON object
246
+ * @throws {Error} if the manifest is unsupported or SBOM generation fails
247
+ */
248
+ export async function generateSbom(manifestPath, opts = {}) {
249
+ fs.accessSync(manifestPath, fs.constants.R_OK);
250
+ const result = await generateOneSbom(manifestPath, opts);
251
+ if (!result.ok) {
252
+ throw new Error(`Failed to generate SBOM for ${result.manifestPath}: ${result.reason}`);
253
+ }
254
+ return result.sbom;
255
+ }
240
256
  /**
241
257
  * @typedef {{ ok: true, purl: string, sbom: object } | { ok: false, manifestPath: string, reason: string }} SbomResult
242
258
  */
@@ -135,7 +135,7 @@ function execSyft(imageRef, opts = {}) {
135
135
  function getSyftEnvs(dockerPath, podmanPath) {
136
136
  let path = null;
137
137
  if (dockerPath && podmanPath) {
138
- path = `${dockerPath}${File.pathSeparator}${podmanPath}`;
138
+ path = `${dockerPath}${delimiter}${podmanPath}`;
139
139
  }
140
140
  else if (dockerPath) {
141
141
  path = dockerPath;
@@ -276,7 +276,16 @@ function podmanGetVariant(opts = {}) {
276
276
  * @returns {string} - The information
277
277
  */
278
278
  function dockerPodmanInfo(dockerSupplier, podmanSupplier, opts = {}) {
279
- return dockerSupplier(opts) || podmanSupplier(opts);
279
+ try {
280
+ const result = dockerSupplier(opts);
281
+ if (result) {
282
+ return result;
283
+ }
284
+ }
285
+ catch (_) {
286
+ // docker not available, fall through to podman
287
+ }
288
+ return podmanSupplier(opts);
280
289
  }
281
290
  /**
282
291
  * Gets the digests for an image
@@ -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
  /**
@@ -10,9 +10,29 @@ export default class Base_pyproject {
10
10
  isSupported(manifestName: string): boolean;
11
11
  /**
12
12
  * @param {string} manifestDir
13
+ * @param {Object} [opts={}]
13
14
  * @returns {boolean}
14
15
  */
15
- validateLockFile(manifestDir: string): boolean;
16
+ validateLockFile(manifestDir: string, opts?: any): boolean;
17
+ /**
18
+ * Walk up from manifestDir to find the directory containing the lock file.
19
+ * Follows the same pattern as Base_javascript._findLockFileDir().
20
+ * @param {string} manifestDir
21
+ * @param {Object} [opts={}]
22
+ * @returns {string|null}
23
+ * @protected
24
+ */
25
+ protected _findLockFileDir(manifestDir: string, opts?: any): string | null;
26
+ /**
27
+ * Detect workspace root boundaries.
28
+ * Currently only uv has native workspace support ([tool.uv.workspace] in pyproject.toml).
29
+ * Poetry has no workspace/monorepo support (python-poetry/poetry#2270), so each
30
+ * poetry project is treated independently — see Python_poetry._findLockFileDir().
31
+ * @param {string} dir
32
+ * @returns {boolean}
33
+ * @protected
34
+ */
35
+ protected _isWorkspaceRoot(dir: string): boolean;
16
36
  /**
17
37
  * Read project license from pyproject.toml, with fallback to LICENSE file.
18
38
  * @param {string} manifestPath
@@ -43,13 +63,16 @@ export default class Base_pyproject {
43
63
  protected _cmdName(): string;
44
64
  /**
45
65
  * Resolve dependencies using the tool-specific command and parser.
46
- * @param {string} manifestDir
66
+ *
67
+ * @param {string} manifestDir - directory containing the target pyproject.toml
68
+ * @param {string} workspaceDir - workspace root (where the lock file lives);
69
+ * only used by providers that need workspace-level resolution (e.g. uv)
47
70
  * @param {object} parsed - parsed pyproject.toml
48
71
  * @param {Object} opts
49
72
  * @returns {Promise<DependencyData>}
50
73
  * @protected
51
74
  */
52
- protected _getDependencyData(manifestDir: string, parsed: object, opts: any): Promise<DependencyData>;
75
+ protected _getDependencyData(manifestDir: string, workspaceDir: string, parsed: object, opts: any): Promise<DependencyData>;
53
76
  /**
54
77
  * Canonicalize a Python package name per PEP 503.
55
78
  * @param {string} name
@@ -79,26 +102,14 @@ export default class Base_pyproject {
79
102
  */
80
103
  protected _getIgnoredDeps(manifestPath: string): Set<string>;
81
104
  /**
82
- * Build dependency tree from graph, starting from direct deps.
105
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
83
106
  * @param {Map<string, GraphEntry>} graph
84
- * @param {string[]} directDeps - canonical names of direct deps
107
+ * @param {string[]} directDeps
85
108
  * @param {Set<string>} ignoredDeps
86
- * @param {boolean} includeTransitive
87
- * @returns {DepTreeEntry[]}
88
- * @protected
89
- */
90
- protected _buildDependencyTree(graph: Map<string, GraphEntry>, directDeps: string[], ignoredDeps: Set<string>, includeTransitive: boolean): DepTreeEntry[];
91
- /**
92
- * Recursively collect transitive dependencies.
93
- * @param {Map<string, GraphEntry>} graph
94
- * @param {string[]} childKeys
95
- * @param {DepTreeEntry[]} result - mutated in place
96
- * @param {Set<string>} ignoredDeps
97
- * @param {Set<string>} visited
98
- * @returns {void}
109
+ * @returns {Set<string>}
99
110
  * @protected
100
111
  */
101
- 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>;
102
113
  /**
103
114
  * @param {string} name
104
115
  * @param {string} version
@@ -106,15 +117,6 @@ export default class Base_pyproject {
106
117
  * @protected
107
118
  */
108
119
  protected _toPurl(name: string, version: string): PackageURL;
109
- /**
110
- * Recursively add a dependency and its transitive deps to the SBOM.
111
- * @param {PackageURL} source
112
- * @param {DepTreeEntry} dep
113
- * @param {Sbom} sbom
114
- * @returns {void}
115
- * @private
116
- */
117
- private _addAllDependencies;
118
120
  /**
119
121
  * Create SBOM json string for a pyproject.toml project.
120
122
  * @param {string} manifest - path to pyproject.toml
@@ -4,6 +4,7 @@ import { PackageURL } from 'packageurl-js';
4
4
  import { parse as parseToml } from 'smol-toml';
5
5
  import { getLicense } from '../license/license_utils.js';
6
6
  import Sbom from '../sbom.js';
7
+ import { getCustom } from '../tools.js';
7
8
  const ecosystem = 'pip';
8
9
  const IGNORE_MARKERS = ['exhortignore', 'trustify-da-ignore'];
9
10
  const DEFAULT_ROOT_NAME = 'default-pip-root';
@@ -22,10 +23,64 @@ export default class Base_pyproject {
22
23
  }
23
24
  /**
24
25
  * @param {string} manifestDir
26
+ * @param {Object} [opts={}]
25
27
  * @returns {boolean}
26
28
  */
27
- validateLockFile(manifestDir) {
28
- return fs.existsSync(path.join(manifestDir, this._lockFileName()));
29
+ validateLockFile(manifestDir, opts = {}) {
30
+ return this._findLockFileDir(manifestDir, opts) != null;
31
+ }
32
+ /**
33
+ * Walk up from manifestDir to find the directory containing the lock file.
34
+ * Follows the same pattern as Base_javascript._findLockFileDir().
35
+ * @param {string} manifestDir
36
+ * @param {Object} [opts={}]
37
+ * @returns {string|null}
38
+ * @protected
39
+ */
40
+ _findLockFileDir(manifestDir, opts = {}) {
41
+ const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
42
+ if (workspaceDir) {
43
+ const dir = path.resolve(workspaceDir);
44
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
45
+ }
46
+ let dir = path.resolve(manifestDir);
47
+ let parent = dir;
48
+ do {
49
+ dir = parent;
50
+ if (fs.existsSync(path.join(dir, this._lockFileName()))) {
51
+ return dir;
52
+ }
53
+ if (this._isWorkspaceRoot(dir)) {
54
+ return null;
55
+ }
56
+ parent = path.dirname(dir);
57
+ } while (parent !== dir);
58
+ return null;
59
+ }
60
+ /**
61
+ * Detect workspace root boundaries.
62
+ * Currently only uv has native workspace support ([tool.uv.workspace] in pyproject.toml).
63
+ * Poetry has no workspace/monorepo support (python-poetry/poetry#2270), so each
64
+ * poetry project is treated independently — see Python_poetry._findLockFileDir().
65
+ * @param {string} dir
66
+ * @returns {boolean}
67
+ * @protected
68
+ */
69
+ _isWorkspaceRoot(dir) {
70
+ const pyprojectPath = path.join(dir, 'pyproject.toml');
71
+ if (!fs.existsSync(pyprojectPath)) {
72
+ return false;
73
+ }
74
+ try {
75
+ const content = parseToml(fs.readFileSync(pyprojectPath, 'utf-8'));
76
+ if (content.tool?.uv?.workspace) {
77
+ return true;
78
+ }
79
+ }
80
+ catch (_) {
81
+ // ignore parse errors
82
+ }
83
+ return false;
29
84
  }
30
85
  /**
31
86
  * Read project license from pyproject.toml, with fallback to LICENSE file.
@@ -91,14 +146,17 @@ export default class Base_pyproject {
91
146
  }
92
147
  /**
93
148
  * Resolve dependencies using the tool-specific command and parser.
94
- * @param {string} manifestDir
149
+ *
150
+ * @param {string} manifestDir - directory containing the target pyproject.toml
151
+ * @param {string} workspaceDir - workspace root (where the lock file lives);
152
+ * only used by providers that need workspace-level resolution (e.g. uv)
95
153
  * @param {object} parsed - parsed pyproject.toml
96
154
  * @param {Object} opts
97
155
  * @returns {Promise<DependencyData>}
98
156
  * @protected
99
157
  */
100
158
  // eslint-disable-next-line no-unused-vars
101
- async _getDependencyData(manifestDir, parsed, opts) {
159
+ async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
102
160
  throw new TypeError('_getDependencyData must be implemented');
103
161
  }
104
162
  // --- shared helpers ---
@@ -162,64 +220,29 @@ export default class Base_pyproject {
162
220
  return ignored;
163
221
  }
164
222
  /**
165
- * Build dependency tree from graph, starting from direct deps.
166
- * @param {Map<string, GraphEntry>} graph
167
- * @param {string[]} directDeps - canonical names of direct deps
168
- * @param {Set<string>} ignoredDeps
169
- * @param {boolean} includeTransitive
170
- * @returns {DepTreeEntry[]}
171
- * @protected
172
- */
173
- _buildDependencyTree(graph, directDeps, ignoredDeps, includeTransitive) {
174
- let result = [];
175
- for (let key of directDeps) {
176
- if (ignoredDeps.has(key)) {
177
- continue;
178
- }
179
- let entry = graph.get(key);
180
- if (!entry) {
181
- continue;
182
- }
183
- let depTree = [];
184
- if (includeTransitive) {
185
- let visited = new Set();
186
- visited.add(key);
187
- this._collectTransitive(graph, entry.children, depTree, ignoredDeps, visited);
188
- }
189
- result.push({ name: entry.name, version: entry.version, dependencies: depTree });
190
- }
191
- result.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
192
- return result;
193
- }
194
- /**
195
- * Recursively collect transitive dependencies.
223
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
196
224
  * @param {Map<string, GraphEntry>} graph
197
- * @param {string[]} childKeys
198
- * @param {DepTreeEntry[]} result - mutated in place
225
+ * @param {string[]} directDeps
199
226
  * @param {Set<string>} ignoredDeps
200
- * @param {Set<string>} visited
201
- * @returns {void}
227
+ * @returns {Set<string>}
202
228
  * @protected
203
229
  */
204
- _collectTransitive(graph, childKeys, result, ignoredDeps, visited) {
205
- for (let childKey of childKeys) {
206
- let canonKey = this._canonicalize(childKey);
207
- if (ignoredDeps.has(canonKey)) {
230
+ _reachableNodes(graph, directDeps, ignoredDeps) {
231
+ let reachable = new Set();
232
+ let queue = directDeps.filter(k => !ignoredDeps.has(k) && graph.has(k));
233
+ while (queue.length > 0) {
234
+ let key = queue.shift();
235
+ if (reachable.has(key)) {
208
236
  continue;
209
237
  }
210
- if (visited.has(canonKey)) {
211
- continue;
212
- }
213
- visited.add(canonKey);
214
- let entry = graph.get(canonKey);
215
- if (!entry) {
216
- continue;
238
+ reachable.add(key);
239
+ for (let child of graph.get(key).children) {
240
+ if (!ignoredDeps.has(child) && graph.has(child) && !reachable.has(child)) {
241
+ queue.push(child);
242
+ }
217
243
  }
218
- let childDeps = [];
219
- this._collectTransitive(graph, entry.children, childDeps, ignoredDeps, visited);
220
- result.push({ name: entry.name, version: entry.version, dependencies: childDeps });
221
244
  }
222
- result.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
245
+ return reachable;
223
246
  }
224
247
  /**
225
248
  * @param {string} name
@@ -230,21 +253,6 @@ export default class Base_pyproject {
230
253
  _toPurl(name, version) {
231
254
  return new PackageURL('pypi', undefined, name, version, undefined, undefined);
232
255
  }
233
- /**
234
- * Recursively add a dependency and its transitive deps to the SBOM.
235
- * @param {PackageURL} source
236
- * @param {DepTreeEntry} dep
237
- * @param {Sbom} sbom
238
- * @returns {void}
239
- * @private
240
- */
241
- _addAllDependencies(source, dep, sbom) {
242
- let targetPurl = this._toPurl(dep.name, dep.version);
243
- sbom.addDependency(source, targetPurl);
244
- if (dep.dependencies && dep.dependencies.length > 0) {
245
- dep.dependencies.forEach(child => this._addAllDependencies(this._toPurl(dep.name, dep.version), child, sbom));
246
- }
247
- }
248
256
  /**
249
257
  * Create SBOM json string for a pyproject.toml project.
250
258
  * @param {string} manifest - path to pyproject.toml
@@ -257,23 +265,50 @@ export default class Base_pyproject {
257
265
  let manifestDir = path.dirname(manifest);
258
266
  let content = fs.readFileSync(manifest, 'utf-8');
259
267
  let parsed = parseToml(content);
260
- let { directDeps, graph } = await this._getDependencyData(manifestDir, parsed, opts);
268
+ let workspaceDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
269
+ let { directDeps, graph } = await this._getDependencyData(manifestDir, workspaceDir, parsed, opts);
261
270
  let ignoredDeps = this._getIgnoredDeps(manifest);
262
- let dependencies = this._buildDependencyTree(graph, directDeps, ignoredDeps, includeTransitive);
263
271
  let sbom = new Sbom();
264
272
  let rootName = this._getProjectName(parsed) || DEFAULT_ROOT_NAME;
265
273
  let rootVersion = this._getProjectVersion(parsed) || DEFAULT_ROOT_VERSION;
266
274
  let rootPurl = this._toPurl(rootName, rootVersion);
267
275
  let license = this.readLicenseFromManifest(manifest);
268
276
  sbom.addRoot(rootPurl, license);
269
- dependencies.forEach(dep => {
270
- if (includeTransitive) {
271
- this._addAllDependencies(rootPurl, dep, sbom);
277
+ if (includeTransitive) {
278
+ let reachable = this._reachableNodes(graph, directDeps, ignoredDeps);
279
+ for (let key of directDeps) {
280
+ if (!reachable.has(key)) {
281
+ continue;
282
+ }
283
+ let entry = graph.get(key);
284
+ sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version));
272
285
  }
273
- else {
274
- sbom.addDependency(rootPurl, this._toPurl(dep.name, dep.version));
286
+ for (let [key, entry] of graph) {
287
+ if (!reachable.has(key)) {
288
+ continue;
289
+ }
290
+ let parentPurl = this._toPurl(entry.name, entry.version);
291
+ for (let child of entry.children) {
292
+ if (!reachable.has(child)) {
293
+ continue;
294
+ }
295
+ let childEntry = graph.get(child);
296
+ sbom.addDependency(parentPurl, this._toPurl(childEntry.name, childEntry.version));
297
+ }
298
+ }
299
+ }
300
+ else {
301
+ for (let key of directDeps) {
302
+ if (ignoredDeps.has(key)) {
303
+ continue;
304
+ }
305
+ let entry = graph.get(key);
306
+ if (!entry) {
307
+ continue;
308
+ }
309
+ sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version));
275
310
  }
276
- });
311
+ }
277
312
  return sbom.getAsJsonString(opts);
278
313
  }
279
314
  }
@@ -95,7 +95,7 @@ export default class Python_controller {
95
95
  }
96
96
  /**
97
97
  * Parse the requirements.txt file using tree-sitter and return structured requirement data.
98
- * @return {Promise<{name: string, version: string|null}[]>}
98
+ * @return {Promise<{name: string, version: string|null, hasMarker: boolean}[]>}
99
99
  */
100
100
  async #parseRequirements() {
101
101
  const content = fs.readFileSync(this.pathToRequirements).toString();
@@ -107,7 +107,8 @@ export default class Python_controller {
107
107
  const version = versionMatches.length > 0
108
108
  ? versionMatches[0].captures.find(c => c.name === 'version').node.text
109
109
  : null;
110
- return { name, version };
110
+ const hasMarker = reqNode.children.some(c => c.type === 'marker_spec');
111
+ return { name, version, hasMarker };
111
112
  }));
112
113
  }
113
114
  #decideIfWindowsOrLinuxPath(fileName) {
@@ -224,7 +225,10 @@ export default class Python_controller {
224
225
  CachedEnvironmentDeps[packageName.replace("_", "-")] = pipDepTreeEntryForCache;
225
226
  });
226
227
  }
227
- parsedRequirements.forEach(({ name: depName, version: manifestVersion }) => {
228
+ parsedRequirements.forEach(({ name: depName, version: manifestVersion, hasMarker }) => {
229
+ if (hasMarker && CachedEnvironmentDeps[depName.toLowerCase()] === undefined) {
230
+ return;
231
+ }
228
232
  if (matchManifestVersions === "true" && manifestVersion != null) {
229
233
  let installedVersion;
230
234
  if (CachedEnvironmentDeps[depName.toLowerCase()] !== undefined) {
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Python provider for pyproject.toml files using PEP 621 format without a lock file.
3
+ * Uses `pip install --dry-run --ignore-installed --report` to resolve the full dependency tree.
4
+ * Acts as the fallback provider when no lock file (uv.lock/poetry.lock) is found.
5
+ */
6
+ export default class Python_pip_pyproject extends Base_pyproject {
7
+ /**
8
+ * Always returns true — pip provider is the fallback when no lock file is found.
9
+ * @param {string} manifestDir
10
+ * @param {{}} [opts={}]
11
+ * @returns {boolean}
12
+ */
13
+ validateLockFile(manifestDir: string, opts?: {}): boolean;
14
+ /**
15
+ * Get pip report output from env var override or by running pip.
16
+ * @param {string} manifestDir - directory containing pyproject.toml
17
+ * @param {{}} [opts={}]
18
+ * @returns {string} pip report JSON string
19
+ */
20
+ _getPipReportOutput(manifestDir: string, opts?: {}): string;
21
+ /**
22
+ * Parse pip report JSON and build dependency graph.
23
+ * @param {string} reportJson - pip report JSON string
24
+ * @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
25
+ */
26
+ _parsePipReport(reportJson: string): {
27
+ directDeps: string[];
28
+ graph: Map<string, {
29
+ name: string;
30
+ version: string;
31
+ children: string[];
32
+ }>;
33
+ };
34
+ /**
35
+ * Check if a requires_dist entry is an extras-only dependency.
36
+ * @param {string} req - e.g. "PySocks!=1.5.7,>=1.5.6; extra == \"socks\""
37
+ * @returns {boolean}
38
+ */
39
+ _hasExtraMarker(req: string): boolean;
40
+ /**
41
+ * Extract package name from a requires_dist entry.
42
+ * @param {string} req - e.g. "charset_normalizer<4,>=2"
43
+ * @returns {string|null}
44
+ */
45
+ _extractDepName(req: string): string | null;
46
+ /**
47
+ * Resolve dependencies using pip install --dry-run --report.
48
+ * @param {string} manifestDir
49
+ * @param {string} _workspaceDir - unused (pip resolves from manifest directory)
50
+ * @param {object} parsed - parsed pyproject.toml
51
+ * @param {{}} [opts={}]
52
+ * @returns {Promise<{directDeps: string[], graph: Map}>}
53
+ */
54
+ _getDependencyData(manifestDir: string, _workspaceDir: string, parsed: object, opts?: {}): Promise<{
55
+ directDeps: string[];
56
+ graph: Map<any, any>;
57
+ }>;
58
+ _findEggInfoDirs(dir: any): string[];
59
+ _cleanupEggInfo(dir: any, existing: any): void;
60
+ }
61
+ import Base_pyproject from './base_pyproject.js';
@@ -0,0 +1,144 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
4
+ import Base_pyproject from './base_pyproject.js';
5
+ /**
6
+ * Python provider for pyproject.toml files using PEP 621 format without a lock file.
7
+ * Uses `pip install --dry-run --ignore-installed --report` to resolve the full dependency tree.
8
+ * Acts as the fallback provider when no lock file (uv.lock/poetry.lock) is found.
9
+ */
10
+ export default class Python_pip_pyproject extends Base_pyproject {
11
+ /** @returns {string} */
12
+ _lockFileName() {
13
+ return '.pip-lock-nonexistent';
14
+ }
15
+ /** @returns {string} */
16
+ _cmdName() {
17
+ return 'pip';
18
+ }
19
+ /**
20
+ * Always returns true — pip provider is the fallback when no lock file is found.
21
+ * @param {string} manifestDir
22
+ * @param {{}} [opts={}]
23
+ * @returns {boolean}
24
+ */
25
+ // eslint-disable-next-line no-unused-vars
26
+ validateLockFile(manifestDir, opts = {}) {
27
+ return true;
28
+ }
29
+ /**
30
+ * Get pip report output from env var override or by running pip.
31
+ * @param {string} manifestDir - directory containing pyproject.toml
32
+ * @param {{}} [opts={}]
33
+ * @returns {string} pip report JSON string
34
+ */
35
+ _getPipReportOutput(manifestDir, opts) {
36
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_PIP_REPORT')) {
37
+ return Buffer.from(process.env['TRUSTIFY_DA_PIP_REPORT'], 'base64').toString('ascii');
38
+ }
39
+ let pipBin = getCustomPath('pip3', opts);
40
+ try {
41
+ invokeCommand(pipBin, ['--version']);
42
+ }
43
+ catch {
44
+ pipBin = getCustomPath('pip', opts);
45
+ }
46
+ let eggInfoDirs = this._findEggInfoDirs(manifestDir);
47
+ let result = invokeCommand(pipBin, [
48
+ 'install', '--dry-run', '--ignore-installed', '--quiet', '--report', '-', '.'
49
+ ], { cwd: manifestDir }).toString();
50
+ this._cleanupEggInfo(manifestDir, eggInfoDirs);
51
+ return result;
52
+ }
53
+ /**
54
+ * Parse pip report JSON and build dependency graph.
55
+ * @param {string} reportJson - pip report JSON string
56
+ * @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
57
+ */
58
+ _parsePipReport(reportJson) {
59
+ let report = JSON.parse(reportJson);
60
+ let packages = report.install || [];
61
+ let rootEntry = packages.find(p => p.download_info?.dir_info !== undefined);
62
+ let rootRequires = rootEntry?.metadata?.requires_dist || [];
63
+ let directDepNames = new Set();
64
+ for (let req of rootRequires) {
65
+ if (this._hasExtraMarker(req)) {
66
+ continue;
67
+ }
68
+ let name = this._extractDepName(req);
69
+ if (name) {
70
+ directDepNames.add(this._canonicalize(name));
71
+ }
72
+ }
73
+ let graph = new Map();
74
+ let nonRootPackages = packages.filter(p => p !== rootEntry);
75
+ for (let pkg of nonRootPackages) {
76
+ let name = pkg.metadata.name;
77
+ let version = pkg.metadata.version;
78
+ let key = this._canonicalize(name);
79
+ graph.set(key, { name, version, children: [] });
80
+ }
81
+ for (let pkg of nonRootPackages) {
82
+ let key = this._canonicalize(pkg.metadata.name);
83
+ let entry = graph.get(key);
84
+ let requires = pkg.metadata.requires_dist || [];
85
+ for (let req of requires) {
86
+ let depName = this._extractDepName(req);
87
+ if (!depName) {
88
+ continue;
89
+ }
90
+ let depKey = this._canonicalize(depName);
91
+ if (graph.has(depKey)) {
92
+ entry.children.push(depKey);
93
+ }
94
+ }
95
+ }
96
+ let directDeps = [...directDepNames].filter(key => graph.has(key));
97
+ return { directDeps, graph };
98
+ }
99
+ /**
100
+ * Check if a requires_dist entry is an extras-only dependency.
101
+ * @param {string} req - e.g. "PySocks!=1.5.7,>=1.5.6; extra == \"socks\""
102
+ * @returns {boolean}
103
+ */
104
+ _hasExtraMarker(req) {
105
+ return /;\s*.*extra\s*==/.test(req);
106
+ }
107
+ /**
108
+ * Extract package name from a requires_dist entry.
109
+ * @param {string} req - e.g. "charset_normalizer<4,>=2"
110
+ * @returns {string|null}
111
+ */
112
+ _extractDepName(req) {
113
+ let match = req.match(/^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)/);
114
+ return match ? match[1] : null;
115
+ }
116
+ /**
117
+ * Resolve dependencies using pip install --dry-run --report.
118
+ * @param {string} manifestDir
119
+ * @param {string} _workspaceDir - unused (pip resolves from manifest directory)
120
+ * @param {object} parsed - parsed pyproject.toml
121
+ * @param {{}} [opts={}]
122
+ * @returns {Promise<{directDeps: string[], graph: Map}>}
123
+ */
124
+ // eslint-disable-next-line no-unused-vars
125
+ async _getDependencyData(manifestDir, _workspaceDir, parsed, opts) {
126
+ let reportOutput = this._getPipReportOutput(manifestDir, opts);
127
+ return this._parsePipReport(reportOutput);
128
+ }
129
+ _findEggInfoDirs(dir) {
130
+ try {
131
+ return fs.readdirSync(dir).filter(f => f.endsWith('.egg-info'));
132
+ }
133
+ catch {
134
+ return [];
135
+ }
136
+ }
137
+ _cleanupEggInfo(dir, existing) {
138
+ for (let entry of this._findEggInfoDirs(dir)) {
139
+ if (!existing.includes(entry)) {
140
+ fs.rmSync(path.join(dir, entry), { recursive: true, force: true });
141
+ }
142
+ }
143
+ }
144
+ }
@@ -2,10 +2,11 @@ export default class Python_poetry extends Base_pyproject {
2
2
  /**
3
3
  * Get poetry show --tree output.
4
4
  * @param {string} manifestDir
5
+ * @param {boolean} hasDevGroup
5
6
  * @param {Object} opts
6
7
  * @returns {string}
7
8
  */
8
- _getPoetryShowTreeOutput(manifestDir: string, opts: any): string;
9
+ _getPoetryShowTreeOutput(manifestDir: string, hasDevGroup: boolean, opts: any): string;
9
10
  /**
10
11
  * Get poetry show --all output (flat list with resolved versions).
11
12
  * @param {string} manifestDir
@@ -1,6 +1,27 @@
1
- import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from '../tools.js';
2
4
  import Base_pyproject from './base_pyproject.js';
3
5
  export default class Python_poetry extends Base_pyproject {
6
+ /**
7
+ * Poetry has no native workspace/monorepo support (python-poetry/poetry#2270).
8
+ * Each poetry project is treated independently — no lock file walk-up.
9
+ * Running `poetry show` from a parent directory returns the parent's deps, not
10
+ * the sub-package's, so walk-up would produce incorrect SBOMs.
11
+ * @param {string} manifestDir
12
+ * @param {Object} [opts={}]
13
+ * @returns {string|null}
14
+ * @protected
15
+ */
16
+ _findLockFileDir(manifestDir, opts = {}) {
17
+ const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
18
+ if (workspaceDir) {
19
+ const dir = path.resolve(workspaceDir);
20
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
21
+ }
22
+ const dir = path.resolve(manifestDir);
23
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
24
+ }
4
25
  /** @returns {string} */
5
26
  _lockFileName() {
6
27
  return 'poetry.lock';
@@ -11,12 +32,15 @@ export default class Python_poetry extends Base_pyproject {
11
32
  }
12
33
  /**
13
34
  * @param {string} manifestDir
35
+ * @param {string} _workspaceDir - unused (poetry has no workspace support)
14
36
  * @param {object} parsed - parsed pyproject.toml
15
37
  * @param {Object} opts
16
38
  * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
17
39
  */
18
- async _getDependencyData(manifestDir, parsed, opts) {
19
- let treeOutput = this._getPoetryShowTreeOutput(manifestDir, opts);
40
+ // eslint-disable-next-line no-unused-vars
41
+ async _getDependencyData(manifestDir, _workspaceDir, parsed, opts) {
42
+ let hasDevGroup = !!(parsed.tool?.poetry?.group?.dev || parsed.tool?.poetry?.['dev-dependencies']);
43
+ let treeOutput = this._getPoetryShowTreeOutput(manifestDir, hasDevGroup, opts);
20
44
  let showAllOutput = this._getPoetryShowAllOutput(manifestDir, opts);
21
45
  let versionMap = this._parsePoetryShowAll(showAllOutput);
22
46
  return this._parsePoetryTree(treeOutput, versionMap);
@@ -24,15 +48,20 @@ export default class Python_poetry extends Base_pyproject {
24
48
  /**
25
49
  * Get poetry show --tree output.
26
50
  * @param {string} manifestDir
51
+ * @param {boolean} hasDevGroup
27
52
  * @param {Object} opts
28
53
  * @returns {string}
29
54
  */
30
- _getPoetryShowTreeOutput(manifestDir, opts) {
55
+ _getPoetryShowTreeOutput(manifestDir, hasDevGroup, opts) {
31
56
  if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_TREE')) {
32
57
  return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_TREE'], 'base64').toString('utf-8');
33
58
  }
34
59
  let poetryBin = getCustomPath('poetry', opts);
35
- return invokeCommand(poetryBin, ['show', '--tree', '--no-ansi'], { cwd: manifestDir }).toString();
60
+ let args = ['show', '--tree', '--no-ansi'];
61
+ if (hasDevGroup) {
62
+ args.push('--without', 'dev');
63
+ }
64
+ return invokeCommand(poetryBin, args, { cwd: manifestDir }).toString();
36
65
  }
37
66
  /**
38
67
  * Get poetry show --all output (flat list with resolved versions).
@@ -89,7 +118,7 @@ export default class Python_poetry extends Base_pyproject {
89
118
  continue;
90
119
  }
91
120
  // top-level line: "name version description..."
92
- let topMatch = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(\S+)\s/);
121
+ let topMatch = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(\S+)(?:\s|$)/);
93
122
  if (topMatch) {
94
123
  let name = topMatch[1];
95
124
  let version = topMatch[2];
@@ -12,9 +12,10 @@ export default class Python_uv extends Base_pyproject {
12
12
  *
13
13
  * @param {string} output
14
14
  * @param {string} projectName - canonical project name to identify direct deps
15
+ * @param {string} workspaceDir - workspace root (for resolving editable install paths)
15
16
  * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
16
17
  */
17
- _parseUvExport(output: string, projectName: string): Promise<{
18
+ _parseUvExport(output: string, projectName: string, workspaceDir: string): Promise<{
18
19
  directDeps: string[];
19
20
  graph: Map<string, {
20
21
  name: string;
@@ -1,3 +1,6 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { parse as parseToml } from 'smol-toml';
1
4
  import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
2
5
  import Base_pyproject from './base_pyproject.js';
3
6
  import { getParser, getPinnedVersionQuery } from './requirements_parser.js';
@@ -11,15 +14,16 @@ export default class Python_uv extends Base_pyproject {
11
14
  return 'uv';
12
15
  }
13
16
  /**
14
- * @param {string} manifestDir
17
+ * @param {string} manifestDir - directory containing the target pyproject.toml
18
+ * @param {string} workspaceDir - workspace root (for resolving editable install paths)
15
19
  * @param {object} parsed - parsed pyproject.toml
16
20
  * @param {Object} opts
17
21
  * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
18
22
  */
19
- async _getDependencyData(manifestDir, parsed, opts) {
23
+ async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
20
24
  let projectName = this._getProjectName(parsed);
21
25
  let uvOutput = this._getUvExportOutput(manifestDir, opts);
22
- return this._parseUvExport(uvOutput, projectName);
26
+ return this._parseUvExport(uvOutput, projectName, workspaceDir);
23
27
  }
24
28
  /**
25
29
  * Get the uv export output, either from env var or by running the command.
@@ -32,7 +36,7 @@ export default class Python_uv extends Base_pyproject {
32
36
  return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii');
33
37
  }
34
38
  let uvBin = getCustomPath('uv', opts);
35
- return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes'], { cwd: manifestDir }).toString();
39
+ return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes', '--no-dev', '--no-emit-project'], { cwd: manifestDir }).toString();
36
40
  }
37
41
  /**
38
42
  * Parse uv export output into a dependency graph using tree-sitter-requirements
@@ -40,9 +44,10 @@ export default class Python_uv extends Base_pyproject {
40
44
  *
41
45
  * @param {string} output
42
46
  * @param {string} projectName - canonical project name to identify direct deps
47
+ * @param {string} workspaceDir - workspace root (for resolving editable install paths)
43
48
  * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
44
49
  */
45
- async _parseUvExport(output, projectName) {
50
+ async _parseUvExport(output, projectName, workspaceDir) {
46
51
  let [parser, pinnedVersionQuery] = await Promise.all([
47
52
  getParser(), getPinnedVersionQuery()
48
53
  ]);
@@ -53,6 +58,32 @@ export default class Python_uv extends Base_pyproject {
53
58
  let currentPkg = null;
54
59
  let collectingVia = false;
55
60
  for (let child of root.children) {
61
+ if (child.type === 'global_opt') {
62
+ let optNode = child.children.find(c => c.type === 'option');
63
+ let pathNode = child.children.find(c => c.type === 'path');
64
+ if (optNode?.text === '-e' && pathNode && workspaceDir) {
65
+ let memberDir = path.resolve(workspaceDir, pathNode.text);
66
+ let memberManifest = path.join(memberDir, 'pyproject.toml');
67
+ if (fs.existsSync(memberManifest)) {
68
+ let memberParsed = parseToml(fs.readFileSync(memberManifest, 'utf-8'));
69
+ let name = memberParsed.project?.name || memberParsed.tool?.poetry?.name;
70
+ let version = memberParsed.project?.version || memberParsed.tool?.poetry?.version;
71
+ if (name && version) {
72
+ let key = this._canonicalize(name);
73
+ if (key === canonProjectName) {
74
+ continue;
75
+ }
76
+ currentPkg = { name, version, parents: new Set() };
77
+ packages.set(key, currentPkg);
78
+ collectingVia = false;
79
+ continue;
80
+ }
81
+ }
82
+ }
83
+ currentPkg = null;
84
+ collectingVia = false;
85
+ continue;
86
+ }
56
87
  if (child.type === 'requirement') {
57
88
  let nameNode = child.children.find(c => c.type === 'package');
58
89
  if (!nameNode) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trustify-da/trustify-da-javascript-client",
3
- "version": "0.3.0-ea.e5bb86c",
3
+ "version": "0.3.0-ea.f136061",
4
4
  "description": "Code-Ready Dependency Analytics JavaScript API.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/guacsec/trustify-da-javascript-client#README.md",