@trustify-da/trustify-da-javascript-client 0.3.0-ea.2ea1d77 → 0.3.0-ea.320ec49

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 (33) hide show
  1. package/README.md +73 -8
  2. package/dist/package.json +6 -5
  3. package/dist/src/analysis.d.ts +2 -0
  4. package/dist/src/analysis.js +7 -4
  5. package/dist/src/cli.js +51 -2
  6. package/dist/src/cyclone_dx_sbom.d.ts +7 -0
  7. package/dist/src/cyclone_dx_sbom.js +14 -0
  8. package/dist/src/index.d.ts +12 -0
  9. package/dist/src/index.js +19 -1
  10. package/dist/src/oci_image/utils.js +11 -2
  11. package/dist/src/provider.js +6 -0
  12. package/dist/src/providers/base_javascript.d.ts +17 -7
  13. package/dist/src/providers/base_javascript.js +90 -17
  14. package/dist/src/providers/base_pyproject.d.ts +149 -0
  15. package/dist/src/providers/base_pyproject.js +314 -0
  16. package/dist/src/providers/golang_gomodules.d.ts +12 -12
  17. package/dist/src/providers/golang_gomodules.js +100 -111
  18. package/dist/src/providers/gomod_parser.d.ts +4 -0
  19. package/dist/src/providers/gomod_parser.js +16 -0
  20. package/dist/src/providers/manifest.d.ts +2 -0
  21. package/dist/src/providers/manifest.js +22 -4
  22. package/dist/src/providers/processors/yarn_berry_processor.js +82 -3
  23. package/dist/src/providers/python_pip.js +1 -1
  24. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  25. package/dist/src/providers/python_pip_pyproject.js +144 -0
  26. package/dist/src/providers/python_poetry.d.ts +42 -0
  27. package/dist/src/providers/python_poetry.js +169 -0
  28. package/dist/src/providers/python_uv.d.ts +27 -0
  29. package/dist/src/providers/python_uv.js +146 -0
  30. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  31. package/dist/src/sbom.d.ts +7 -0
  32. package/dist/src/sbom.js +9 -0
  33. package/package.json +7 -6
@@ -97,18 +97,63 @@ export default class Base_javascript {
97
97
  return 'package.json' === manifestName;
98
98
  }
99
99
  /**
100
- * Checks if a required lock file exists in the manifest directory or at the workspace root.
101
- * When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
102
- * checks only that directory for the lock file.
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
+ /**
103
151
  * @param {string} manifestDir - The base directory where the manifest is located
104
- * @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
152
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
105
153
  * @returns {boolean} True if the lock file exists
106
154
  */
107
155
  validateLockFile(manifestDir, opts = {}) {
108
- const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
109
- const dirToCheck = workspaceDir ? path.resolve(workspaceDir) : manifestDir;
110
- const lock = path.join(dirToCheck, this._lockFileName());
111
- return fs.existsSync(lock);
156
+ return this._findLockFileDir(manifestDir, opts) !== null;
112
157
  }
113
158
  /**
114
159
  * Provides content and content type for stack analysis
@@ -171,8 +216,7 @@ export default class Base_javascript {
171
216
  _buildDependencyTree(includeTransitive, opts = {}) {
172
217
  this._version();
173
218
  const manifestDir = path.dirname(this.#manifest.manifestPath);
174
- const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
175
- const cmdDir = workspaceDir ? path.resolve(workspaceDir) : manifestDir;
219
+ const cmdDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
176
220
  this.#createLockFile(cmdDir);
177
221
  let output = this.#executeListCmd(includeTransitive, cmdDir);
178
222
  output = this._parseDepTreeOutput(output);
@@ -191,9 +235,32 @@ export default class Base_javascript {
191
235
  let sbom = new Sbom();
192
236
  sbom.addRoot(mainComponent, license);
193
237
  this._addDependenciesToSbom(sbom, depsObject);
238
+ this.#ensurePeerAndOptionalDeps(sbom);
194
239
  sbom.filterIgnoredDeps(this.#manifest.ignored);
195
240
  return sbom.getAsJsonString(opts);
196
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
+ }
197
264
  /**
198
265
  * Recursively builds the Sbom from the JSON that npm listing returns
199
266
  * @param {Sbom} sbom - The SBOM object to add dependencies to
@@ -201,7 +268,10 @@ export default class Base_javascript {
201
268
  * @protected
202
269
  */
203
270
  _addDependenciesToSbom(sbom, depTree) {
204
- const dependencies = depTree["dependencies"] || {};
271
+ const dependencies = {
272
+ ...depTree["dependencies"],
273
+ ...depTree["optionalDependencies"],
274
+ };
205
275
  Object.entries(dependencies)
206
276
  .forEach(entry => {
207
277
  const [name, artifact] = entry;
@@ -251,6 +321,7 @@ export default class Base_javascript {
251
321
  const rootPurl = toPurlFromString(sbom.getRoot().purl);
252
322
  sbom.addDependency(rootPurl, rootDeps.get(key));
253
323
  }
324
+ this.#ensurePeerAndOptionalDeps(sbom);
254
325
  sbom.filterIgnoredDeps(this.#manifest.ignored);
255
326
  return sbom.getAsJsonString(opts);
256
327
  }
@@ -261,10 +332,14 @@ export default class Base_javascript {
261
332
  * @protected
262
333
  */
263
334
  _getRootDependencies(depTree) {
264
- if (!depTree.dependencies) {
335
+ const allDeps = {
336
+ ...depTree.dependencies,
337
+ ...depTree.optionalDependencies,
338
+ };
339
+ if (Object.keys(allDeps).length === 0) {
265
340
  return new Map();
266
341
  }
267
- 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)]));
268
343
  }
269
344
  /**
270
345
  * Executes the list command to get dependencies
@@ -275,7 +350,7 @@ export default class Base_javascript {
275
350
  */
276
351
  #executeListCmd(includeTransitive, manifestDir) {
277
352
  const listArgs = this._listCmdArgs(includeTransitive, manifestDir);
278
- return this.#invokeCommand(listArgs);
353
+ return this.#invokeCommand(listArgs, { cwd: manifestDir });
279
354
  }
280
355
  /**
281
356
  * Gets the version of the package manager
@@ -294,13 +369,11 @@ export default class Base_javascript {
294
369
  const originalDir = process.cwd();
295
370
  const isWindows = os.platform() === 'win32';
296
371
  if (isWindows) {
297
- // On Windows, --prefix flag doesn't work as expected
298
- // Instead of installing from the prefix folder, it installs from current working directory
299
372
  process.chdir(manifestDir);
300
373
  }
301
374
  try {
302
375
  const args = this._updateLockFileCmdArgs(manifestDir);
303
- this.#invokeCommand(args);
376
+ this.#invokeCommand(args, { cwd: manifestDir });
304
377
  }
305
378
  finally {
306
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';
@@ -0,0 +1,314 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { PackageURL } from 'packageurl-js';
4
+ import { parse as parseToml } from 'smol-toml';
5
+ import { getLicense } from '../license/license_utils.js';
6
+ import Sbom from '../sbom.js';
7
+ import { getCustom } from '../tools.js';
8
+ const ecosystem = 'pip';
9
+ const IGNORE_MARKERS = ['exhortignore', 'trustify-da-ignore'];
10
+ const DEFAULT_ROOT_NAME = 'default-pip-root';
11
+ const DEFAULT_ROOT_VERSION = '0.0.0';
12
+ /** @typedef {{name: string, version: string, children: string[]}} GraphEntry */
13
+ /** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
14
+ /** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
15
+ /** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
16
+ export default class Base_pyproject {
17
+ /**
18
+ * @param {string} manifestName
19
+ * @returns {boolean}
20
+ */
21
+ isSupported(manifestName) {
22
+ return 'pyproject.toml' === manifestName;
23
+ }
24
+ /**
25
+ * @param {string} manifestDir
26
+ * @param {Object} [opts={}]
27
+ * @returns {boolean}
28
+ */
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;
84
+ }
85
+ /**
86
+ * Read project license from pyproject.toml, with fallback to LICENSE file.
87
+ * @param {string} manifestPath
88
+ * @returns {string|null}
89
+ */
90
+ readLicenseFromManifest(manifestPath) {
91
+ let fromManifest = null;
92
+ try {
93
+ let content = fs.readFileSync(manifestPath, 'utf-8');
94
+ let parsed = parseToml(content);
95
+ fromManifest = parsed.project?.license;
96
+ if (typeof fromManifest === 'object' && fromManifest != null) {
97
+ fromManifest = fromManifest.text || null;
98
+ }
99
+ if (!fromManifest) {
100
+ fromManifest = parsed.tool?.poetry?.license || null;
101
+ }
102
+ }
103
+ catch (_) {
104
+ // leave fromManifest as null
105
+ }
106
+ return getLicense(fromManifest, manifestPath);
107
+ }
108
+ /**
109
+ * @param {string} manifest - path to pyproject.toml
110
+ * @param {Object} [opts={}]
111
+ * @returns {Promise<Provided>}
112
+ */
113
+ async provideStack(manifest, opts = {}) {
114
+ return {
115
+ ecosystem,
116
+ content: await this._createSbom(manifest, opts, true),
117
+ contentType: 'application/vnd.cyclonedx+json'
118
+ };
119
+ }
120
+ /**
121
+ * @param {string} manifest - path to pyproject.toml
122
+ * @param {Object} [opts={}]
123
+ * @returns {Promise<Provided>}
124
+ */
125
+ async provideComponent(manifest, opts = {}) {
126
+ return {
127
+ ecosystem,
128
+ content: await this._createSbom(manifest, opts, false),
129
+ contentType: 'application/vnd.cyclonedx+json'
130
+ };
131
+ }
132
+ // --- abstract methods (subclasses must override) ---
133
+ /**
134
+ * @returns {string}
135
+ * @protected
136
+ */
137
+ _lockFileName() {
138
+ throw new TypeError('_lockFileName must be implemented');
139
+ }
140
+ /**
141
+ * @returns {string}
142
+ * @protected
143
+ */
144
+ _cmdName() {
145
+ throw new TypeError('_cmdName must be implemented');
146
+ }
147
+ /**
148
+ * Resolve dependencies using the tool-specific command and parser.
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)
153
+ * @param {object} parsed - parsed pyproject.toml
154
+ * @param {Object} opts
155
+ * @returns {Promise<DependencyData>}
156
+ * @protected
157
+ */
158
+ // eslint-disable-next-line no-unused-vars
159
+ async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
160
+ throw new TypeError('_getDependencyData must be implemented');
161
+ }
162
+ // --- shared helpers ---
163
+ /**
164
+ * Canonicalize a Python package name per PEP 503.
165
+ * @param {string} name
166
+ * @returns {string}
167
+ * @protected
168
+ */
169
+ _canonicalize(name) {
170
+ return name.toLowerCase().replace(/[-_.]+/g, '-');
171
+ }
172
+ /**
173
+ * Get the project name from pyproject.toml.
174
+ * @param {object} parsed
175
+ * @returns {string|null}
176
+ * @protected
177
+ */
178
+ _getProjectName(parsed) {
179
+ return parsed.project?.name || parsed.tool?.poetry?.name || null;
180
+ }
181
+ /**
182
+ * Get the project version from pyproject.toml.
183
+ * @param {object} parsed
184
+ * @returns {string|null}
185
+ * @protected
186
+ */
187
+ _getProjectVersion(parsed) {
188
+ return parsed.project?.version || parsed.tool?.poetry?.version || null;
189
+ }
190
+ /**
191
+ * Scan raw pyproject.toml text for dependencies with ignore markers.
192
+ * @param {string} manifestPath
193
+ * @returns {Set<string>}
194
+ * @protected
195
+ */
196
+ _getIgnoredDeps(manifestPath) {
197
+ let ignored = new Set();
198
+ let content = fs.readFileSync(manifestPath, 'utf-8');
199
+ let lines = content.split(/\r?\n/);
200
+ for (let line of lines) {
201
+ if (!IGNORE_MARKERS.some(m => line.includes(m))) {
202
+ continue;
203
+ }
204
+ // PEP 621 style: "requests>=2.25" #exhortignore
205
+ let pep621Match = line.match(/^\s*"([^"]+)"/);
206
+ if (pep621Match) {
207
+ let reqStr = pep621Match[1];
208
+ let nameMatch = reqStr.match(/^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)/);
209
+ if (nameMatch) {
210
+ ignored.add(this._canonicalize(nameMatch[1]));
211
+ }
212
+ continue;
213
+ }
214
+ // Poetry style: requests = "^2.25" #exhortignore
215
+ let poetryMatch = line.match(/^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*=/);
216
+ if (poetryMatch) {
217
+ ignored.add(this._canonicalize(poetryMatch[1]));
218
+ }
219
+ }
220
+ return ignored;
221
+ }
222
+ /**
223
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
224
+ * @param {Map<string, GraphEntry>} graph
225
+ * @param {string[]} directDeps
226
+ * @param {Set<string>} ignoredDeps
227
+ * @returns {Set<string>}
228
+ * @protected
229
+ */
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)) {
236
+ continue;
237
+ }
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
+ }
243
+ }
244
+ }
245
+ return reachable;
246
+ }
247
+ /**
248
+ * @param {string} name
249
+ * @param {string} version
250
+ * @returns {PackageURL}
251
+ * @protected
252
+ */
253
+ _toPurl(name, version) {
254
+ return new PackageURL('pypi', undefined, name, version, undefined, undefined);
255
+ }
256
+ /**
257
+ * Create SBOM json string for a pyproject.toml project.
258
+ * @param {string} manifest - path to pyproject.toml
259
+ * @param {Object} opts
260
+ * @param {boolean} includeTransitive
261
+ * @returns {Promise<string>}
262
+ * @private
263
+ */
264
+ async _createSbom(manifest, opts, includeTransitive) {
265
+ let manifestDir = path.dirname(manifest);
266
+ let content = fs.readFileSync(manifest, 'utf-8');
267
+ let parsed = parseToml(content);
268
+ let workspaceDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
269
+ let { directDeps, graph } = await this._getDependencyData(manifestDir, workspaceDir, parsed, opts);
270
+ let ignoredDeps = this._getIgnoredDeps(manifest);
271
+ let sbom = new Sbom();
272
+ let rootName = this._getProjectName(parsed) || DEFAULT_ROOT_NAME;
273
+ let rootVersion = this._getProjectVersion(parsed) || DEFAULT_ROOT_VERSION;
274
+ let rootPurl = this._toPurl(rootName, rootVersion);
275
+ let license = this.readLicenseFromManifest(manifest);
276
+ sbom.addRoot(rootPurl, license);
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));
285
+ }
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));
310
+ }
311
+ }
312
+ return sbom.getAsJsonString(opts);
313
+ }
314
+ }
@@ -19,31 +19,31 @@ export type Dependency = {
19
19
  ignore: boolean;
20
20
  };
21
21
  /**
22
- * @param {string} manifestName - the subject manifest name-type
23
- * @returns {boolean} - return true if `pom.xml` is the manifest name-type
22
+ * @param {string} manifestName the subject manifest name-type
23
+ * @returns {boolean} return true if `pom.xml` is the manifest name-type
24
24
  */
25
25
  declare function isSupported(manifestName: string): boolean;
26
26
  /**
27
- * @param {string} manifestDir - the directory where the manifest lies
27
+ * @param {string} manifestDir the directory where the manifest lies
28
28
  */
29
29
  declare function validateLockFile(): boolean;
30
30
  /**
31
31
  * Provide content and content type for maven-maven component analysis.
32
- * @param {string} manifest - path to go.mod for component report
33
- * @param {{}} [opts={}] - optional various options to pass along the application
34
- * @returns {Provided}
32
+ * @param {string} manifest path to go.mod for component report
33
+ * @param {{}} [opts={}] optional various options to pass along the application
34
+ * @returns {Promise<Provided>}
35
35
  */
36
- declare function provideComponent(manifest: string, opts?: {}): Provided;
36
+ declare function provideComponent(manifest: string, opts?: {}): Promise<Provided>;
37
37
  /**
38
38
  * Provide content and content type for maven-maven stack analysis.
39
- * @param {string} manifest - the manifest path or name
40
- * @param {{}} [opts={}] - optional various options to pass along the application
41
- * @returns {Provided}
39
+ * @param {string} manifest the manifest path or name
40
+ * @param {{}} [opts={}] optional various options to pass along the application
41
+ * @returns {Promise<Provided>}
42
42
  */
43
- declare function provideStack(manifest: string, opts?: {}): Provided;
43
+ declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
44
44
  /**
45
45
  * Go modules have no standard license field in go.mod
46
- * @param {string} manifestPath - path to go.mod
46
+ * @param {string} manifestPath path to go.mod
47
47
  * @returns {string|null}
48
48
  */
49
49
  declare function readLicenseFromManifest(manifestPath: string): string | null;