@trustify-da/trustify-da-javascript-client 0.3.0-ea.6549d2a → 0.3.0-ea.6ca858a

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 (56) hide show
  1. package/README.md +179 -11
  2. package/dist/package.json +12 -3
  3. package/dist/src/analysis.d.ts +16 -0
  4. package/dist/src/analysis.js +53 -4
  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 +171 -4
  8. package/dist/src/cyclone_dx_sbom.d.ts +7 -0
  9. package/dist/src/cyclone_dx_sbom.js +16 -1
  10. package/dist/src/index.d.ts +75 -2
  11. package/dist/src/index.js +284 -5
  12. package/dist/src/license/index.d.ts +2 -2
  13. package/dist/src/license/index.js +4 -4
  14. package/dist/src/license/license_utils.d.ts +40 -0
  15. package/dist/src/license/license_utils.js +134 -0
  16. package/dist/src/license/licenses_api.js +9 -2
  17. package/dist/src/license/project_license.d.ts +1 -6
  18. package/dist/src/license/project_license.js +4 -81
  19. package/dist/src/oci_image/utils.js +11 -2
  20. package/dist/src/provider.d.ts +6 -3
  21. package/dist/src/provider.js +14 -5
  22. package/dist/src/providers/base_javascript.d.ts +19 -3
  23. package/dist/src/providers/base_javascript.js +106 -23
  24. package/dist/src/providers/base_pyproject.d.ts +149 -0
  25. package/dist/src/providers/base_pyproject.js +314 -0
  26. package/dist/src/providers/golang_gomodules.d.ts +12 -12
  27. package/dist/src/providers/golang_gomodules.js +102 -112
  28. package/dist/src/providers/gomod_parser.d.ts +4 -0
  29. package/dist/src/providers/gomod_parser.js +16 -0
  30. package/dist/src/providers/java_gradle.js +2 -1
  31. package/dist/src/providers/java_maven.d.ts +1 -1
  32. package/dist/src/providers/java_maven.js +10 -9
  33. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  34. package/dist/src/providers/javascript_pnpm.js +2 -2
  35. package/dist/src/providers/manifest.d.ts +2 -0
  36. package/dist/src/providers/manifest.js +22 -4
  37. package/dist/src/providers/processors/yarn_berry_processor.js +82 -3
  38. package/dist/src/providers/python_pip.js +3 -2
  39. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  40. package/dist/src/providers/python_pip_pyproject.js +144 -0
  41. package/dist/src/providers/python_poetry.d.ts +43 -0
  42. package/dist/src/providers/python_poetry.js +175 -0
  43. package/dist/src/providers/python_uv.d.ts +27 -0
  44. package/dist/src/providers/python_uv.js +146 -0
  45. package/dist/src/providers/requirements_parser.js +4 -3
  46. package/dist/src/providers/rust_cargo.d.ts +52 -0
  47. package/dist/src/providers/rust_cargo.js +614 -0
  48. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  49. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  50. package/dist/src/sbom.d.ts +7 -0
  51. package/dist/src/sbom.js +9 -0
  52. package/dist/src/workspace.d.ts +61 -0
  53. package/dist/src/workspace.js +256 -0
  54. package/package.json +13 -4
  55. package/dist/src/license/compatibility.d.ts +0 -18
  56. package/dist/src/license/compatibility.js +0 -45
@@ -7,7 +7,7 @@ import path from 'node:path';
7
7
  import { selectTrustifyDABackend } from '../index.js';
8
8
  import { matchForLicense, availableProviders } from '../provider.js';
9
9
  import { addProxyAgent, getTokenHeaders } from '../tools.js';
10
- const LICENSE_FILES = ['LICENSE', 'LICENSE.md', 'LICENSE.txt'];
10
+ import { normalizeSpdx, readLicenseFile } from './license_utils.js';
11
11
  /**
12
12
  * Resolve project license from manifest and from LICENSE / LICENSE.md in manifest dir or git root.
13
13
  * Uses local pattern matching for LICENSE file identification (synchronous).
@@ -19,7 +19,7 @@ export function getProjectLicense(manifestPath) {
19
19
  const resolved = path.resolve(manifestPath);
20
20
  const provider = matchForLicense(resolved, availableProviders);
21
21
  const fromManifest = provider.readLicenseFromManifest(resolved);
22
- const fromFile = readLicenseFromFile(resolved);
22
+ const fromFile = readLicenseFile(resolved);
23
23
  const mismatch = Boolean(fromManifest && fromFile && normalizeSpdx(fromManifest) !== normalizeSpdx(fromFile));
24
24
  return {
25
25
  fromManifest: fromManifest || null,
@@ -27,26 +27,7 @@ export function getProjectLicense(manifestPath) {
27
27
  mismatch
28
28
  };
29
29
  }
30
- /**
31
- * Find LICENSE file path in the same directory as the manifest.
32
- * @param {string} manifestPath
33
- * @returns {string|null} - path to LICENSE file or null if not found
34
- */
35
- export function findLicenseFilePath(manifestPath) {
36
- const manifestDir = path.dirname(path.resolve(manifestPath));
37
- for (const name of LICENSE_FILES) {
38
- const filePath = path.join(manifestDir, name);
39
- try {
40
- if (fs.statSync(filePath).isFile()) {
41
- return filePath;
42
- }
43
- }
44
- catch {
45
- // skip
46
- }
47
- }
48
- return null;
49
- }
30
+ export { findLicenseFilePath, readLicenseFile } from './license_utils.js';
50
31
  /**
51
32
  * Call backend /licenses/identify endpoint to identify license from file.
52
33
  * @param {string} licenseFilePath - path to LICENSE file
@@ -57,7 +38,7 @@ export async function identifyLicense(licenseFilePath, opts = {}) {
57
38
  try {
58
39
  const fileContent = fs.readFileSync(licenseFilePath);
59
40
  const backendUrl = selectTrustifyDABackend(opts);
60
- const url = new URL(`${backendUrl}/licenses/identify`);
41
+ const url = new URL(`${backendUrl}/api/v5/licenses/identify`);
61
42
  const tokenHeaders = getTokenHeaders(opts);
62
43
  const fetchOptions = addProxyAgent({
63
44
  method: 'POST',
@@ -79,61 +60,3 @@ export async function identifyLicense(licenseFilePath, opts = {}) {
79
60
  return null; // Fallback to local detection on error
80
61
  }
81
62
  }
82
- /**
83
- * Find and read LICENSE or LICENSE.md; use local pattern matching for identification.
84
- * @param {string} manifestPath
85
- * @returns {string|null}
86
- */
87
- function readLicenseFromFile(manifestPath) {
88
- const licenseFilePath = findLicenseFilePath(manifestPath);
89
- if (!licenseFilePath) {
90
- return null;
91
- }
92
- try {
93
- const content = fs.readFileSync(licenseFilePath, 'utf-8');
94
- return detectSpdxFromText(content) || content.split('\n')[0]?.trim() || null;
95
- }
96
- catch {
97
- return null;
98
- }
99
- }
100
- /**
101
- * Very simple SPDX detection from common license text (first ~500 chars).
102
- * @param {string} text
103
- * @returns {string|null}
104
- */
105
- function detectSpdxFromText(text) {
106
- const head = text.slice(0, 500);
107
- if (/Apache License,?\s*Version 2\.0/i.test(head)) {
108
- return 'Apache-2.0';
109
- }
110
- if (/MIT License/i.test(head) && /Permission is hereby granted/i.test(head)) {
111
- return 'MIT';
112
- }
113
- if (/GNU GENERAL PUBLIC LICENSE\s+Version 2/i.test(head)) {
114
- return 'GPL-2.0-only';
115
- }
116
- if (/GNU GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) {
117
- return 'GPL-3.0-only';
118
- }
119
- if (/BSD 2-Clause/i.test(head)) {
120
- return 'BSD-2-Clause';
121
- }
122
- if (/BSD 3-Clause/i.test(head)) {
123
- return 'BSD-3-Clause';
124
- }
125
- return null;
126
- }
127
- /**
128
- * Normalize for comparison (lowercase, strip common suffixes).
129
- * @param {string} spdxOrName
130
- * @returns {string}
131
- */
132
- function normalizeSpdx(spdxOrName) {
133
- const s = String(spdxOrName).trim().toLowerCase();
134
- // e.g. "MIT" vs "MIT License"
135
- if (s.endsWith(' license')) {
136
- return s.slice(0, -8);
137
- }
138
- return s;
139
- }
@@ -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
@@ -13,12 +13,15 @@ export function matchForLicense(manifestPath: string, providers: [Provider]): Pr
13
13
  * Each provider MUST export 'provideStack' taking manifest path returning a {@link Provided}.
14
14
  * @param {string} manifest - the name-type or path of the manifest
15
15
  * @param {[Provider]} providers - list of providers to iterate over
16
+ * @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional; TRUSTIFY_DA_WORKSPACE_DIR overrides lock file location for workspaces
16
17
  * @returns {Provider}
17
18
  * @throws {Error} when the manifest is not supported and no provider was matched
18
19
  */
19
- export function match(manifest: string, providers: [Provider]): Provider;
20
+ export function match(manifest: string, providers: [Provider], opts?: {
21
+ TRUSTIFY_DA_WORKSPACE_DIR?: string;
22
+ }): Provider;
20
23
  /** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
21
- /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
24
+ /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string, Object): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
22
25
  /**
23
26
  * MUST include all providers here.
24
27
  * @type {[Provider]}
@@ -31,7 +34,7 @@ export type Provided = {
31
34
  };
32
35
  export type Provider = {
33
36
  isSupported: (arg0: string) => boolean;
34
- validateLockFile: (arg0: string) => void;
37
+ validateLockFile: (arg0: string, arg1: any) => void;
35
38
  provideComponent: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
36
39
  provideStack: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
37
40
  readLicenseFromManifest: (arg0: string) => string | null;
@@ -7,8 +7,12 @@ 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';
11
+ import Python_poetry from './providers/python_poetry.js';
12
+ import Python_uv from './providers/python_uv.js';
13
+ import rustCargoProvider from './providers/rust_cargo.js';
10
14
  /** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
11
- /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
15
+ /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string, Object): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
12
16
  /**
13
17
  * MUST include all providers here.
14
18
  * @type {[Provider]}
@@ -17,11 +21,15 @@ export const availableProviders = [
17
21
  new Java_maven(),
18
22
  new Java_gradle_groovy(),
19
23
  new Java_gradle_kotlin(),
20
- new Javascript_npm(),
21
24
  new Javascript_pnpm(),
22
25
  new Javascript_yarn(),
26
+ new Javascript_npm(),
23
27
  golangGomodulesProvider,
24
- pythonPipProvider
28
+ pythonPipProvider,
29
+ new Python_poetry(),
30
+ new Python_uv(),
31
+ new Python_pip_pyproject(),
32
+ rustCargoProvider
25
33
  ];
26
34
  /**
27
35
  * Match a provider by manifest type only (no lock file check). Used for license reading.
@@ -45,16 +53,17 @@ export function matchForLicense(manifestPath, providers) {
45
53
  * Each provider MUST export 'provideStack' taking manifest path returning a {@link Provided}.
46
54
  * @param {string} manifest - the name-type or path of the manifest
47
55
  * @param {[Provider]} providers - list of providers to iterate over
56
+ * @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional; TRUSTIFY_DA_WORKSPACE_DIR overrides lock file location for workspaces
48
57
  * @returns {Provider}
49
58
  * @throws {Error} when the manifest is not supported and no provider was matched
50
59
  */
51
- export function match(manifest, providers) {
60
+ export function match(manifest, providers, opts = {}) {
52
61
  const manifestPath = path.parse(manifest);
53
62
  const supported = providers.filter(prov => prov.isSupported(manifestPath.base));
54
63
  if (supported.length === 0) {
55
64
  throw new Error(`${manifestPath.base} is not supported`);
56
65
  }
57
- const provider = supported.find(prov => prov.validateLockFile(manifestPath.dir));
66
+ const provider = supported.find(prov => prov.validateLockFile(manifestPath.dir, opts));
58
67
  if (!provider) {
59
68
  throw new Error(`${manifestPath.base} requires a lock file. Use your preferred package manager to generate the lock file.`);
60
69
  }
@@ -67,11 +67,26 @@ export default class Base_javascript {
67
67
  */
68
68
  isSupported(manifestName: string): boolean;
69
69
  /**
70
- * Checks if a required lock file exists in the same path as the manifest
70
+ * Walks up the directory tree from manifestDir looking for the lock file.
71
+ * Stops when the lock file is found, when a package.json with a "workspaces"
72
+ * field is encountered without a lock file (workspace root boundary), or
73
+ * when the filesystem root is reached.
74
+ *
75
+ * When TRUSTIFY_DA_WORKSPACE_DIR is set, checks only that directory (no walk-up).
76
+ *
77
+ * @param {string} manifestDir - The directory to start searching from
78
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
79
+ * @returns {string|null} The directory containing the lock file, or null
80
+ * @protected
81
+ */
82
+ protected _isWorkspaceRoot(dir: any): string | null;
83
+ _findLockFileDir(manifestDir: any, opts?: {}): string | null;
84
+ /**
71
85
  * @param {string} manifestDir - The base directory where the manifest is located
86
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
72
87
  * @returns {boolean} True if the lock file exists
73
88
  */
74
- validateLockFile(manifestDir: string): boolean;
89
+ validateLockFile(manifestDir: string, opts?: any): boolean;
75
90
  /**
76
91
  * Provides content and content type for stack analysis
77
92
  * @param {string} manifestPath - The manifest path or name
@@ -95,10 +110,11 @@ export default class Base_javascript {
95
110
  /**
96
111
  * Builds the dependency tree for the project
97
112
  * @param {boolean} includeTransitive - Whether to include transitive dependencies
113
+ * @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root
98
114
  * @returns {Object} The dependency tree
99
115
  * @protected
100
116
  */
101
- protected _buildDependencyTree(includeTransitive: boolean): any;
117
+ protected _buildDependencyTree(includeTransitive: boolean, opts?: any): any;
102
118
  /**
103
119
  * Recursively builds the Sbom from the JSON that npm listing returns
104
120
  * @param {Sbom} sbom - The SBOM object to add dependencies to
@@ -1,8 +1,9 @@
1
1
  import fs from 'node:fs';
2
2
  import os from "node:os";
3
3
  import path from 'node:path';
4
+ import { getLicense } from '../license/license_utils.js';
4
5
  import Sbom from '../sbom.js';
5
- import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from "../tools.js";
6
+ import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from '../tools.js';
6
7
  import Manifest from './manifest.js';
7
8
  /** @typedef {import('../provider').Provider} */
8
9
  /** @typedef {import('../provider').Provided} Provided */
@@ -96,13 +97,63 @@ export default class Base_javascript {
96
97
  return 'package.json' === manifestName;
97
98
  }
98
99
  /**
99
- * Checks if a required lock file exists in the same path as the manifest
100
+ * Walks up the directory tree from manifestDir looking for the lock file.
101
+ * Stops when the lock file is found, when a package.json with a "workspaces"
102
+ * field is encountered without a lock file (workspace root boundary), or
103
+ * when the filesystem root is reached.
104
+ *
105
+ * When TRUSTIFY_DA_WORKSPACE_DIR is set, checks only that directory (no walk-up).
106
+ *
107
+ * @param {string} manifestDir - The directory to start searching from
108
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
109
+ * @returns {string|null} The directory containing the lock file, or null
110
+ * @protected
111
+ */
112
+ _isWorkspaceRoot(dir) {
113
+ if (fs.existsSync(path.join(dir, 'pnpm-workspace.yaml'))) {
114
+ return true;
115
+ }
116
+ const pkgJsonPath = path.join(dir, 'package.json');
117
+ if (fs.existsSync(pkgJsonPath)) {
118
+ try {
119
+ const content = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
120
+ if (content.workspaces) {
121
+ return true;
122
+ }
123
+ }
124
+ catch (_) {
125
+ // ignore parse errors
126
+ }
127
+ }
128
+ return false;
129
+ }
130
+ _findLockFileDir(manifestDir, opts = {}) {
131
+ const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
132
+ if (workspaceDir) {
133
+ const dir = path.resolve(workspaceDir);
134
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
135
+ }
136
+ let dir = path.resolve(manifestDir);
137
+ let parent = dir;
138
+ do {
139
+ dir = parent;
140
+ if (fs.existsSync(path.join(dir, this._lockFileName()))) {
141
+ return dir;
142
+ }
143
+ if (this._isWorkspaceRoot(dir)) {
144
+ return null;
145
+ }
146
+ parent = path.dirname(dir);
147
+ } while (parent !== dir);
148
+ return null;
149
+ }
150
+ /**
100
151
  * @param {string} manifestDir - The base directory where the manifest is located
152
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
101
153
  * @returns {boolean} True if the lock file exists
102
154
  */
103
- validateLockFile(manifestDir) {
104
- const lock = path.join(manifestDir, this._lockFileName());
105
- return fs.existsSync(lock);
155
+ validateLockFile(manifestDir, opts = {}) {
156
+ return this._findLockFileDir(manifestDir, opts) !== null;
106
157
  }
107
158
  /**
108
159
  * Provides content and content type for stack analysis
@@ -138,33 +189,36 @@ export default class Base_javascript {
138
189
  * @returns {string|null}
139
190
  */
140
191
  readLicenseFromManifest(manifestPath) {
192
+ let manifestLicense;
141
193
  try {
142
194
  const content = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
143
195
  if (typeof content.license === 'string') {
144
- return content.license.trim() || null;
196
+ manifestLicense = content.license.trim() || null;
145
197
  }
146
- if (Array.isArray(content.licenses) && content.licenses.length > 0) {
198
+ else if (Array.isArray(content.licenses) && content.licenses.length > 0) {
147
199
  const first = content.licenses[0];
148
200
  const name = first.type || first.name;
149
- return typeof name === 'string' ? name.trim() : null;
201
+ manifestLicense = (typeof name === 'string' ? name.trim() : null);
150
202
  }
151
- return null;
152
203
  }
153
204
  catch {
154
- return null;
205
+ manifestLicense = null;
155
206
  }
207
+ return getLicense(manifestLicense, manifestPath);
156
208
  }
157
209
  /**
158
210
  * Builds the dependency tree for the project
159
211
  * @param {boolean} includeTransitive - Whether to include transitive dependencies
212
+ * @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root
160
213
  * @returns {Object} The dependency tree
161
214
  * @protected
162
215
  */
163
- _buildDependencyTree(includeTransitive) {
216
+ _buildDependencyTree(includeTransitive, opts = {}) {
164
217
  this._version();
165
- let manifestDir = path.dirname(this.#manifest.manifestPath);
166
- this.#createLockFile(manifestDir);
167
- let output = this.#executeListCmd(includeTransitive, manifestDir);
218
+ const manifestDir = path.dirname(this.#manifest.manifestPath);
219
+ const cmdDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
220
+ this.#createLockFile(cmdDir);
221
+ let output = this.#executeListCmd(includeTransitive, cmdDir);
168
222
  output = this._parseDepTreeOutput(output);
169
223
  return JSON.parse(output);
170
224
  }
@@ -175,15 +229,38 @@ export default class Base_javascript {
175
229
  * @private
176
230
  */
177
231
  #getSBOM(opts = {}) {
178
- const depsObject = this._buildDependencyTree(true);
232
+ const depsObject = this._buildDependencyTree(true, opts);
179
233
  let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
180
234
  const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
181
235
  let sbom = new Sbom();
182
236
  sbom.addRoot(mainComponent, license);
183
237
  this._addDependenciesToSbom(sbom, depsObject);
238
+ this.#ensurePeerAndOptionalDeps(sbom);
184
239
  sbom.filterIgnoredDeps(this.#manifest.ignored);
185
240
  return sbom.getAsJsonString(opts);
186
241
  }
242
+ /**
243
+ * Ensures peer and optional dependencies declared in the manifest are
244
+ * present in the SBOM, even when the package manager does not resolve them
245
+ * (e.g. yarn does not include peer deps in its dependency listing).
246
+ * @param {Sbom} sbom - The SBOM to supplement
247
+ * @private
248
+ */
249
+ #ensurePeerAndOptionalDeps(sbom) {
250
+ const rootPurl = toPurl(purlType, this.#manifest.name, this.#manifest.version);
251
+ const depSources = [this.#manifest.peerDependencies, this.#manifest.optionalDependencies];
252
+ for (const source of depSources) {
253
+ for (const [name, version] of Object.entries(source)) {
254
+ // Build the purl prefix for exact matching (e.g. "pkg:npm/minimist@"
255
+ // or "pkg:npm/%40hapi/joi@") to avoid substring false positives
256
+ const probe = toPurl(purlType, name, version);
257
+ const purlPrefix = probe.toString().replace(/@[^@]*$/, '@');
258
+ if (!sbom.checkDependsOnByPurlPrefix(rootPurl, purlPrefix)) {
259
+ sbom.addDependency(rootPurl, probe);
260
+ }
261
+ }
262
+ }
263
+ }
187
264
  /**
188
265
  * Recursively builds the Sbom from the JSON that npm listing returns
189
266
  * @param {Sbom} sbom - The SBOM object to add dependencies to
@@ -191,7 +268,10 @@ export default class Base_javascript {
191
268
  * @protected
192
269
  */
193
270
  _addDependenciesToSbom(sbom, depTree) {
194
- const dependencies = depTree["dependencies"] || {};
271
+ const dependencies = {
272
+ ...depTree["dependencies"],
273
+ ...depTree["optionalDependencies"],
274
+ };
195
275
  Object.entries(dependencies)
196
276
  .forEach(entry => {
197
277
  const [name, artifact] = entry;
@@ -227,7 +307,7 @@ export default class Base_javascript {
227
307
  * @private
228
308
  */
229
309
  #getDirectDependencySbom(opts = {}) {
230
- const depTree = this._buildDependencyTree(false);
310
+ const depTree = this._buildDependencyTree(false, opts);
231
311
  let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
232
312
  const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
233
313
  let sbom = new Sbom();
@@ -241,6 +321,7 @@ export default class Base_javascript {
241
321
  const rootPurl = toPurlFromString(sbom.getRoot().purl);
242
322
  sbom.addDependency(rootPurl, rootDeps.get(key));
243
323
  }
324
+ this.#ensurePeerAndOptionalDeps(sbom);
244
325
  sbom.filterIgnoredDeps(this.#manifest.ignored);
245
326
  return sbom.getAsJsonString(opts);
246
327
  }
@@ -251,10 +332,14 @@ export default class Base_javascript {
251
332
  * @protected
252
333
  */
253
334
  _getRootDependencies(depTree) {
254
- if (!depTree.dependencies) {
335
+ const allDeps = {
336
+ ...depTree.dependencies,
337
+ ...depTree.optionalDependencies,
338
+ };
339
+ if (Object.keys(allDeps).length === 0) {
255
340
  return new Map();
256
341
  }
257
- return new Map(Object.entries(depTree.dependencies).map(([key, value]) => [key, toPurl(purlType, key, value.version)]));
342
+ return new Map(Object.entries(allDeps).map(([key, value]) => [key, toPurl(purlType, key, value.version)]));
258
343
  }
259
344
  /**
260
345
  * Executes the list command to get dependencies
@@ -265,7 +350,7 @@ export default class Base_javascript {
265
350
  */
266
351
  #executeListCmd(includeTransitive, manifestDir) {
267
352
  const listArgs = this._listCmdArgs(includeTransitive, manifestDir);
268
- return this.#invokeCommand(listArgs);
353
+ return this.#invokeCommand(listArgs, { cwd: manifestDir });
269
354
  }
270
355
  /**
271
356
  * Gets the version of the package manager
@@ -284,13 +369,11 @@ export default class Base_javascript {
284
369
  const originalDir = process.cwd();
285
370
  const isWindows = os.platform() === 'win32';
286
371
  if (isWindows) {
287
- // On Windows, --prefix flag doesn't work as expected
288
- // Instead of installing from the prefix folder, it installs from current working directory
289
372
  process.chdir(manifestDir);
290
373
  }
291
374
  try {
292
375
  const args = this._updateLockFileCmdArgs(manifestDir);
293
- this.#invokeCommand(args);
376
+ this.#invokeCommand(args, { cwd: manifestDir });
294
377
  }
295
378
  finally {
296
379
  if (isWindows) {
@@ -0,0 +1,149 @@
1
+ /** @typedef {{name: string, version: string, children: string[]}} GraphEntry */
2
+ /** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
3
+ /** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
4
+ /** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
5
+ export default class Base_pyproject {
6
+ /**
7
+ * @param {string} manifestName
8
+ * @returns {boolean}
9
+ */
10
+ isSupported(manifestName: string): boolean;
11
+ /**
12
+ * @param {string} manifestDir
13
+ * @param {Object} [opts={}]
14
+ * @returns {boolean}
15
+ */
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;
36
+ /**
37
+ * Read project license from pyproject.toml, with fallback to LICENSE file.
38
+ * @param {string} manifestPath
39
+ * @returns {string|null}
40
+ */
41
+ readLicenseFromManifest(manifestPath: string): string | null;
42
+ /**
43
+ * @param {string} manifest - path to pyproject.toml
44
+ * @param {Object} [opts={}]
45
+ * @returns {Promise<Provided>}
46
+ */
47
+ provideStack(manifest: string, opts?: any): Promise<Provided>;
48
+ /**
49
+ * @param {string} manifest - path to pyproject.toml
50
+ * @param {Object} [opts={}]
51
+ * @returns {Promise<Provided>}
52
+ */
53
+ provideComponent(manifest: string, opts?: any): Promise<Provided>;
54
+ /**
55
+ * @returns {string}
56
+ * @protected
57
+ */
58
+ protected _lockFileName(): string;
59
+ /**
60
+ * @returns {string}
61
+ * @protected
62
+ */
63
+ protected _cmdName(): string;
64
+ /**
65
+ * Resolve dependencies using the tool-specific command and parser.
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)
70
+ * @param {object} parsed - parsed pyproject.toml
71
+ * @param {Object} opts
72
+ * @returns {Promise<DependencyData>}
73
+ * @protected
74
+ */
75
+ protected _getDependencyData(manifestDir: string, workspaceDir: string, parsed: object, opts: any): Promise<DependencyData>;
76
+ /**
77
+ * Canonicalize a Python package name per PEP 503.
78
+ * @param {string} name
79
+ * @returns {string}
80
+ * @protected
81
+ */
82
+ protected _canonicalize(name: string): string;
83
+ /**
84
+ * Get the project name from pyproject.toml.
85
+ * @param {object} parsed
86
+ * @returns {string|null}
87
+ * @protected
88
+ */
89
+ protected _getProjectName(parsed: object): string | null;
90
+ /**
91
+ * Get the project version from pyproject.toml.
92
+ * @param {object} parsed
93
+ * @returns {string|null}
94
+ * @protected
95
+ */
96
+ protected _getProjectVersion(parsed: object): string | null;
97
+ /**
98
+ * Scan raw pyproject.toml text for dependencies with ignore markers.
99
+ * @param {string} manifestPath
100
+ * @returns {Set<string>}
101
+ * @protected
102
+ */
103
+ protected _getIgnoredDeps(manifestPath: string): Set<string>;
104
+ /**
105
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
106
+ * @param {Map<string, GraphEntry>} graph
107
+ * @param {string[]} directDeps
108
+ * @param {Set<string>} ignoredDeps
109
+ * @returns {Set<string>}
110
+ * @protected
111
+ */
112
+ protected _reachableNodes(graph: Map<string, GraphEntry>, directDeps: string[], ignoredDeps: Set<string>): Set<string>;
113
+ /**
114
+ * @param {string} name
115
+ * @param {string} version
116
+ * @returns {PackageURL}
117
+ * @protected
118
+ */
119
+ protected _toPurl(name: string, version: string): PackageURL;
120
+ /**
121
+ * Create SBOM json string for a pyproject.toml project.
122
+ * @param {string} manifest - path to pyproject.toml
123
+ * @param {Object} opts
124
+ * @param {boolean} includeTransitive
125
+ * @returns {Promise<string>}
126
+ * @private
127
+ */
128
+ private _createSbom;
129
+ }
130
+ export type GraphEntry = {
131
+ name: string;
132
+ version: string;
133
+ children: string[];
134
+ };
135
+ export type DepTreeEntry = {
136
+ name: string;
137
+ version: string;
138
+ dependencies: DepTreeEntry[];
139
+ };
140
+ export type DependencyData = {
141
+ directDeps: string[];
142
+ graph: Map<string, GraphEntry>;
143
+ };
144
+ export type Provided = {
145
+ ecosystem: string;
146
+ content: string;
147
+ contentType: string;
148
+ };
149
+ import { PackageURL } from 'packageurl-js';