@trustify-da/trustify-da-javascript-client 0.3.0-ea.f2d5d72 → 0.3.0-ea.fa16f06

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 (77) hide show
  1. package/README.md +2 -2
  2. package/dist/package.json +26 -23
  3. package/dist/src/analysis.d.ts +14 -7
  4. package/dist/src/analysis.js +32 -15
  5. package/dist/src/cli.js +156 -3
  6. package/dist/src/cyclone_dx_sbom.d.ts +7 -1
  7. package/dist/src/cyclone_dx_sbom.js +29 -8
  8. package/dist/src/index.d.ts +84 -11
  9. package/dist/src/index.js +95 -8
  10. package/dist/src/license/index.d.ts +3 -3
  11. package/dist/src/license/index.js +4 -1
  12. package/dist/src/license/license_utils.js +4 -1
  13. package/dist/src/license/licenses_api.d.ts +2 -2
  14. package/dist/src/oci_image/images.d.ts +1 -1
  15. package/dist/src/oci_image/images.js +15 -1
  16. package/dist/src/oci_image/utils.d.ts +5 -5
  17. package/dist/src/oci_image/utils.js +11 -2
  18. package/dist/src/provider.d.ts +4 -3
  19. package/dist/src/provider.js +8 -2
  20. package/dist/src/providers/base_java.d.ts +14 -12
  21. package/dist/src/providers/base_java.js +18 -44
  22. package/dist/src/providers/base_javascript.d.ts +20 -9
  23. package/dist/src/providers/base_javascript.js +10 -3
  24. package/dist/src/providers/base_pyproject.d.ts +42 -31
  25. package/dist/src/providers/base_pyproject.js +121 -78
  26. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  27. package/dist/src/providers/containerfile_parser.js +20 -0
  28. package/dist/src/providers/golang_gomodules.d.ts +11 -1
  29. package/dist/src/providers/golang_gomodules.js +65 -8
  30. package/dist/src/providers/java_gradle.d.ts +19 -0
  31. package/dist/src/providers/java_gradle.js +116 -2
  32. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  33. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  34. package/dist/src/providers/java_maven.d.ts +10 -2
  35. package/dist/src/providers/java_maven.js +165 -1
  36. package/dist/src/providers/javascript_bun.d.ts +10 -0
  37. package/dist/src/providers/javascript_bun.js +100 -0
  38. package/dist/src/providers/javascript_npm.d.ts +6 -0
  39. package/dist/src/providers/javascript_npm.js +21 -0
  40. package/dist/src/providers/javascript_pnpm.js +6 -2
  41. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  42. package/dist/src/providers/marker_evaluator.js +191 -0
  43. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  44. package/dist/src/providers/oci_dockerfile.js +177 -0
  45. package/dist/src/providers/processors/yarn_berry_processor.d.ts +2 -2
  46. package/dist/src/providers/processors/yarn_berry_processor.js +6 -2
  47. package/dist/src/providers/processors/yarn_classic_processor.d.ts +2 -2
  48. package/dist/src/providers/python_controller.d.ts +5 -1
  49. package/dist/src/providers/python_controller.js +69 -8
  50. package/dist/src/providers/python_pip.d.ts +5 -0
  51. package/dist/src/providers/python_pip.js +5 -5
  52. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  53. package/dist/src/providers/python_pip_pyproject.js +146 -0
  54. package/dist/src/providers/python_poetry.d.ts +64 -8
  55. package/dist/src/providers/python_poetry.js +165 -25
  56. package/dist/src/providers/python_uv.d.ts +22 -2
  57. package/dist/src/providers/python_uv.js +157 -6
  58. package/dist/src/providers/rust_cargo.d.ts +6 -2
  59. package/dist/src/providers/rust_cargo.js +105 -19
  60. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  61. package/dist/src/remediate.d.ts +21 -0
  62. package/dist/src/remediate.js +149 -0
  63. package/dist/src/remediation.d.ts +57 -0
  64. package/dist/src/remediation.js +436 -0
  65. package/dist/src/remediation_report.d.ts +40 -0
  66. package/dist/src/remediation_report.js +159 -0
  67. package/dist/src/sbom.d.ts +7 -1
  68. package/dist/src/sbom.js +4 -2
  69. package/dist/src/tools.d.ts +28 -2
  70. package/dist/src/tools.js +58 -0
  71. package/dist/src/updaters/maven_updater.d.ts +26 -0
  72. package/dist/src/updaters/maven_updater.js +322 -0
  73. package/dist/src/updaters/toml_updater.d.ts +33 -0
  74. package/dist/src/updaters/toml_updater.js +244 -0
  75. package/dist/src/workspace.d.ts +9 -0
  76. package/dist/src/workspace.js +1 -1
  77. package/package.json +27 -24
@@ -1,7 +1,6 @@
1
- import fs from 'node:fs';
2
1
  import path from 'node:path';
3
2
  import { PackageURL } from 'packageurl-js';
4
- import { getCustomPath, getGitRootDir, getWrapperPreference, invokeCommand } from "../tools.js";
3
+ import { getCustomPath, getWrapperPreference, invokeCommand, traverseForWrapper } from "../tools.js";
5
4
  /** @typedef {import('../provider').Provider} */
6
5
  /** @typedef {import('../provider').Provided} Provided */
7
6
  /** @typedef {{name: string, version: string}} Package */
@@ -26,6 +25,13 @@ export default class Base_Java {
26
25
  this.globalBinary = globalBinary;
27
26
  this.localWrapper = localWrapper;
28
27
  }
28
+ /**
29
+ * Returns the package manager name (e.g. mvn, gradle)
30
+ * @returns {string}
31
+ */
32
+ packageManagerName() {
33
+ return this.globalBinary;
34
+ }
29
35
  /**
30
36
  * Recursively populates the SBOM instance with the parsed graph
31
37
  * @param {string} src - Source dependency to start the calculations from
@@ -42,7 +48,7 @@ export default class Base_Java {
42
48
  }
43
49
  let index = 0;
44
50
  let target = lines[index];
45
- let targetDepth = this.#getDepth(target);
51
+ let targetDepth = this._getDepth(target);
46
52
  while (targetDepth > srcDepth && index < lines.length) {
47
53
  if (targetDepth === srcDepth + 1) {
48
54
  let from = this.parseDep(src);
@@ -55,22 +61,25 @@ export default class Base_Java {
55
61
  }
56
62
  }
57
63
  else {
58
- this.parseDependencyTree(lines[index - 1], this.#getDepth(lines[index - 1]), lines.slice(index), sbom);
64
+ this.parseDependencyTree(lines[index - 1], this._getDepth(lines[index - 1]), lines.slice(index), sbom);
59
65
  }
60
66
  target = lines[++index];
61
- targetDepth = this.#getDepth(target);
67
+ targetDepth = this._getDepth(target);
62
68
  }
63
69
  }
64
70
  /**
65
71
  * Calculates how deep in the graph is the given line
66
72
  * @param {string} line - line to calculate the depth from
67
73
  * @returns {number} The calculated depth
68
- * @private
74
+ * @protected
69
75
  */
70
- #getDepth(line) {
71
- if (line === undefined) {
76
+ _getDepth(line) {
77
+ if (!line || line.trim() === '') {
72
78
  return -1;
73
79
  }
80
+ if (line.match(/^\w/)) {
81
+ return 0;
82
+ }
74
83
  return ((line.indexOf('-') - 1) / 3) + 1;
75
84
  }
76
85
  /**
@@ -131,7 +140,7 @@ export default class Base_Java {
131
140
  const toolPath = getCustomPath(this.globalBinary, opts);
132
141
  const useWrapper = getWrapperPreference(this.globalBinary, opts);
133
142
  if (useWrapper) {
134
- const wrapper = this.traverseForWrapper(manifestPath);
143
+ const wrapper = traverseForWrapper(manifestDir, this.localWrapper);
135
144
  if (wrapper !== undefined) {
136
145
  try {
137
146
  this._invokeCommand(wrapper, ['--version'], { cwd: manifestDir });
@@ -156,39 +165,4 @@ export default class Base_Java {
156
165
  }
157
166
  return toolPath;
158
167
  }
159
- /**
160
- *
161
- * @param {string} startingManifest - the path of the manifest from which to start searching for the wrapper
162
- * @param {string} repoRoot - the root of the repository at which point to stop searching for mvnw, derived via git if unset and then fallsback
163
- * to the root of the drive the manifest is on (assumes absolute path is given)
164
- * @returns {string|undefined}
165
- */
166
- traverseForWrapper(startingManifest, repoRoot = undefined) {
167
- const normalizedManifest = this.normalizePath(startingManifest);
168
- const currentDir = this.normalizePath(path.dirname(normalizedManifest));
169
- repoRoot = repoRoot || getGitRootDir(currentDir) || path.parse(normalizedManifest).root;
170
- const wrapperPath = path.join(currentDir, this.localWrapper);
171
- try {
172
- fs.accessSync(wrapperPath, fs.constants.X_OK);
173
- return wrapperPath;
174
- }
175
- catch (error) {
176
- if (error.code === 'ENOENT') {
177
- const rootDir = path.parse(currentDir).root;
178
- if (currentDir === repoRoot || currentDir === rootDir) {
179
- return undefined;
180
- }
181
- const parentDir = path.dirname(currentDir);
182
- if (parentDir === currentDir || parentDir === rootDir) {
183
- return undefined;
184
- }
185
- return this.traverseForWrapper(path.join(parentDir, path.basename(normalizedManifest)), repoRoot);
186
- }
187
- throw new Error(`failure searching for ${this.localWrapper}`, { cause: error });
188
- }
189
- }
190
- normalizePath(thePath) {
191
- const normalized = path.resolve(thePath).normalize();
192
- return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
193
- }
194
168
  }
@@ -1,4 +1,4 @@
1
- export type purlType = import("../provider").Provider;
1
+ export type purlType = any;
2
2
  /** @typedef {import('../provider').Provider} */
3
3
  /** @typedef {import('../provider').Provided} Provided */
4
4
  /**
@@ -19,7 +19,7 @@ export default class Base_javascript {
19
19
  * @param {Object} opts - Configuration options for the provider
20
20
  * @protected
21
21
  */
22
- protected _setUp(manifestPath: string, opts: any): void;
22
+ protected _setUp(manifestPath: string, opts: Object): void;
23
23
  /**
24
24
  * Gets the current manifest object
25
25
  * @returns {Manifest} The manifest object
@@ -47,6 +47,11 @@ export default class Base_javascript {
47
47
  */
48
48
  protected _cmdName(): string;
49
49
  /**
50
+ * Returns the package manager name (e.g. npm, yarn, pnpm, bun)
51
+ * @returns {string} The package manager name
52
+ */
53
+ packageManagerName(): string;
54
+ /**
50
55
  * Returns the command arguments for listing dependencies
51
56
  * @returns {Array<string>} The command arguments
52
57
  * @abstract
@@ -86,21 +91,21 @@ export default class Base_javascript {
86
91
  * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
87
92
  * @returns {boolean} True if the lock file exists
88
93
  */
89
- validateLockFile(manifestDir: string, opts?: any): boolean;
94
+ validateLockFile(manifestDir: string, opts?: Object): boolean;
90
95
  /**
91
96
  * Provides content and content type for stack analysis
92
97
  * @param {string} manifestPath - The manifest path or name
93
98
  * @param {Object} [opts={}] - Optional configuration options
94
99
  * @returns {Provided} The provided data for stack analysis
95
100
  */
96
- provideStack(manifestPath: string, opts?: any): Provided;
101
+ provideStack(manifestPath: string, opts?: Object): Provided;
97
102
  /**
98
103
  * Provides content and content type for component analysis
99
104
  * @param {string} manifestPath - Path to package.json for component report
100
105
  * @param {Object} [opts={}] - Optional configuration options
101
106
  * @returns {Provided} The provided data for component analysis
102
107
  */
103
- provideComponent(manifestPath: string, opts?: any): Provided;
108
+ provideComponent(manifestPath: string, opts?: Object): Provided;
104
109
  /**
105
110
  * Read license from manifest (package.json). Reused by npm, pnpm, yarn.
106
111
  * @param {string} manifestPath - path to package.json
@@ -114,21 +119,21 @@ export default class Base_javascript {
114
119
  * @returns {Object} The dependency tree
115
120
  * @protected
116
121
  */
117
- protected _buildDependencyTree(includeTransitive: boolean, opts?: any): any;
122
+ protected _buildDependencyTree(includeTransitive: boolean, opts?: Object): Object;
118
123
  /**
119
124
  * Recursively builds the Sbom from the JSON that npm listing returns
120
125
  * @param {Sbom} sbom - The SBOM object to add dependencies to
121
126
  * @param {Object} depTree - The current dependency tree
122
127
  * @protected
123
128
  */
124
- protected _addDependenciesToSbom(sbom: Sbom, depTree: any): void;
129
+ protected _addDependenciesToSbom(sbom: Sbom, depTree: Object): void;
125
130
  /**
126
131
  * Extracts root dependencies from the dependency tree
127
132
  * @param {Object} depTree - The dependency tree object
128
133
  * @returns {Map<string, PackageURL>} Map of dependency names to their PackageURL objects
129
134
  * @protected
130
135
  */
131
- protected _getRootDependencies(depTree: any): Map<string, PackageURL>;
136
+ protected _getRootDependencies(depTree: Object): Map<string, PackageURL>;
132
137
  /**
133
138
  * Gets the version of the package manager
134
139
  * @returns {string} The version string of the package manager
@@ -136,6 +141,12 @@ export default class Base_javascript {
136
141
  */
137
142
  protected _version(): string;
138
143
  /**
144
+ * Creates or updates the lock file for the package manager
145
+ * @param {string} manifestDir - Directory containing the manifest file
146
+ * @protected
147
+ */
148
+ protected _createLockFile(manifestDir: string): void;
149
+ /**
139
150
  * Parses the dependency tree output
140
151
  * @param {string} output - The output to parse
141
152
  * @returns {string} The parsed output
@@ -144,6 +155,6 @@ export default class Base_javascript {
144
155
  protected _parseDepTreeOutput(output: string): string;
145
156
  #private;
146
157
  }
147
- export type Provided = import("../provider").Provided;
158
+ export type Provided = any;
148
159
  import Manifest from './manifest.js';
149
160
  import Sbom from '../sbom.js';
@@ -71,6 +71,13 @@ export default class Base_javascript {
71
71
  throw new TypeError("_cmdName must be implemented");
72
72
  }
73
73
  /**
74
+ * Returns the package manager name (e.g. npm, yarn, pnpm, bun)
75
+ * @returns {string} The package manager name
76
+ */
77
+ packageManagerName() {
78
+ return this._cmdName();
79
+ }
80
+ /**
74
81
  * Returns the command arguments for listing dependencies
75
82
  * @returns {Array<string>} The command arguments
76
83
  * @abstract
@@ -217,7 +224,7 @@ export default class Base_javascript {
217
224
  this._version();
218
225
  const manifestDir = path.dirname(this.#manifest.manifestPath);
219
226
  const cmdDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
220
- this.#createLockFile(cmdDir);
227
+ this._createLockFile(cmdDir);
221
228
  let output = this.#executeListCmd(includeTransitive, cmdDir);
222
229
  output = this._parseDepTreeOutput(output);
223
230
  return JSON.parse(output);
@@ -363,9 +370,9 @@ export default class Base_javascript {
363
370
  /**
364
371
  * Creates or updates the lock file for the package manager
365
372
  * @param {string} manifestDir - Directory containing the manifest file
366
- * @private
373
+ * @protected
367
374
  */
368
- #createLockFile(manifestDir) {
375
+ _createLockFile(manifestDir) {
369
376
  const originalDir = process.cwd();
370
377
  const isWindows = os.platform() === 'win32';
371
378
  if (isWindows) {
@@ -1,4 +1,4 @@
1
- /** @typedef {{name: string, version: string, children: string[]}} GraphEntry */
1
+ /** @typedef {{name: string, version: string, children: string[], hashes?: Array<{alg: string, content: string}>}} GraphEntry */
2
2
  /** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
3
3
  /** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
4
4
  /** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
@@ -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?: Object): 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?: Object): 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
@@ -24,13 +44,13 @@ export default class Base_pyproject {
24
44
  * @param {Object} [opts={}]
25
45
  * @returns {Promise<Provided>}
26
46
  */
27
- provideStack(manifest: string, opts?: any): Promise<Provided>;
47
+ provideStack(manifest: string, opts?: Object): Promise<Provided>;
28
48
  /**
29
49
  * @param {string} manifest - path to pyproject.toml
30
50
  * @param {Object} [opts={}]
31
51
  * @returns {Promise<Provided>}
32
52
  */
33
- provideComponent(manifest: string, opts?: any): Promise<Provided>;
53
+ provideComponent(manifest: string, opts?: Object): Promise<Provided>;
34
54
  /**
35
55
  * @returns {string}
36
56
  * @protected
@@ -41,15 +61,23 @@ export default class Base_pyproject {
41
61
  * @protected
42
62
  */
43
63
  protected _cmdName(): string;
64
+ /**
65
+ * Returns the package manager name (e.g. pip, poetry, uv)
66
+ * @returns {string}
67
+ */
68
+ packageManagerName(): string;
44
69
  /**
45
70
  * Resolve dependencies using the tool-specific command and parser.
46
- * @param {string} manifestDir
71
+ *
72
+ * @param {string} manifestDir - directory containing the target pyproject.toml
73
+ * @param {string} workspaceDir - workspace root (where the lock file lives);
74
+ * only used by providers that need workspace-level resolution (e.g. uv)
47
75
  * @param {object} parsed - parsed pyproject.toml
48
76
  * @param {Object} opts
49
77
  * @returns {Promise<DependencyData>}
50
78
  * @protected
51
79
  */
52
- protected _getDependencyData(manifestDir: string, parsed: object, opts: any): Promise<DependencyData>;
80
+ protected _getDependencyData(manifestDir: string, workspaceDir: string, parsed: object, opts: Object): Promise<DependencyData>;
53
81
  /**
54
82
  * Canonicalize a Python package name per PEP 503.
55
83
  * @param {string} name
@@ -79,26 +107,14 @@ export default class Base_pyproject {
79
107
  */
80
108
  protected _getIgnoredDeps(manifestPath: string): Set<string>;
81
109
  /**
82
- * Build dependency tree from graph, starting from direct deps.
83
- * @param {Map<string, GraphEntry>} graph
84
- * @param {string[]} directDeps - canonical names of direct deps
85
- * @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.
110
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
93
111
  * @param {Map<string, GraphEntry>} graph
94
- * @param {string[]} childKeys
95
- * @param {DepTreeEntry[]} result - mutated in place
112
+ * @param {string[]} directDeps
96
113
  * @param {Set<string>} ignoredDeps
97
- * @param {Set<string>} visited
98
- * @returns {void}
114
+ * @returns {Set<string>}
99
115
  * @protected
100
116
  */
101
- protected _collectTransitive(graph: Map<string, GraphEntry>, childKeys: string[], result: DepTreeEntry[], ignoredDeps: Set<string>, visited: Set<string>): void;
117
+ protected _reachableNodes(graph: Map<string, GraphEntry>, directDeps: string[], ignoredDeps: Set<string>): Set<string>;
102
118
  /**
103
119
  * @param {string} name
104
120
  * @param {string} version
@@ -106,15 +122,6 @@ export default class Base_pyproject {
106
122
  * @protected
107
123
  */
108
124
  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
125
  /**
119
126
  * Create SBOM json string for a pyproject.toml project.
120
127
  * @param {string} manifest - path to pyproject.toml
@@ -129,6 +136,10 @@ export type GraphEntry = {
129
136
  name: string;
130
137
  version: string;
131
138
  children: string[];
139
+ hashes?: Array<{
140
+ alg: string;
141
+ content: string;
142
+ }>;
132
143
  };
133
144
  export type DepTreeEntry = {
134
145
  name: string;
@@ -4,11 +4,13 @@ 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'];
10
+ const NO_SCOPE = undefined;
9
11
  const DEFAULT_ROOT_NAME = 'default-pip-root';
10
12
  const DEFAULT_ROOT_VERSION = '0.0.0';
11
- /** @typedef {{name: string, version: string, children: string[]}} GraphEntry */
13
+ /** @typedef {{name: string, version: string, children: string[], hashes?: Array<{alg: string, content: string}>}} GraphEntry */
12
14
  /** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
13
15
  /** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
14
16
  /** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
@@ -22,10 +24,64 @@ export default class Base_pyproject {
22
24
  }
23
25
  /**
24
26
  * @param {string} manifestDir
27
+ * @param {Object} [opts={}]
25
28
  * @returns {boolean}
26
29
  */
27
- validateLockFile(manifestDir) {
28
- return fs.existsSync(path.join(manifestDir, this._lockFileName()));
30
+ validateLockFile(manifestDir, opts = {}) {
31
+ return this._findLockFileDir(manifestDir, opts) != null;
32
+ }
33
+ /**
34
+ * Walk up from manifestDir to find the directory containing the lock file.
35
+ * Follows the same pattern as Base_javascript._findLockFileDir().
36
+ * @param {string} manifestDir
37
+ * @param {Object} [opts={}]
38
+ * @returns {string|null}
39
+ * @protected
40
+ */
41
+ _findLockFileDir(manifestDir, opts = {}) {
42
+ const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
43
+ if (workspaceDir) {
44
+ const dir = path.resolve(workspaceDir);
45
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
46
+ }
47
+ let dir = path.resolve(manifestDir);
48
+ let parent = dir;
49
+ do {
50
+ dir = parent;
51
+ if (fs.existsSync(path.join(dir, this._lockFileName()))) {
52
+ return dir;
53
+ }
54
+ if (this._isWorkspaceRoot(dir)) {
55
+ return null;
56
+ }
57
+ parent = path.dirname(dir);
58
+ } while (parent !== dir);
59
+ return null;
60
+ }
61
+ /**
62
+ * Detect workspace root boundaries.
63
+ * Currently only uv has native workspace support ([tool.uv.workspace] in pyproject.toml).
64
+ * Poetry has no workspace/monorepo support (python-poetry/poetry#2270), so each
65
+ * poetry project is treated independently — see Python_poetry._findLockFileDir().
66
+ * @param {string} dir
67
+ * @returns {boolean}
68
+ * @protected
69
+ */
70
+ _isWorkspaceRoot(dir) {
71
+ const pyprojectPath = path.join(dir, 'pyproject.toml');
72
+ if (!fs.existsSync(pyprojectPath)) {
73
+ return false;
74
+ }
75
+ try {
76
+ const content = parseToml(fs.readFileSync(pyprojectPath, 'utf-8'));
77
+ if (content.tool?.uv?.workspace) {
78
+ return true;
79
+ }
80
+ }
81
+ catch (_) {
82
+ // ignore parse errors
83
+ }
84
+ return false;
29
85
  }
30
86
  /**
31
87
  * Read project license from pyproject.toml, with fallback to LICENSE file.
@@ -89,16 +145,26 @@ export default class Base_pyproject {
89
145
  _cmdName() {
90
146
  throw new TypeError('_cmdName must be implemented');
91
147
  }
148
+ /**
149
+ * Returns the package manager name (e.g. pip, poetry, uv)
150
+ * @returns {string}
151
+ */
152
+ packageManagerName() {
153
+ return this._cmdName();
154
+ }
92
155
  /**
93
156
  * Resolve dependencies using the tool-specific command and parser.
94
- * @param {string} manifestDir
157
+ *
158
+ * @param {string} manifestDir - directory containing the target pyproject.toml
159
+ * @param {string} workspaceDir - workspace root (where the lock file lives);
160
+ * only used by providers that need workspace-level resolution (e.g. uv)
95
161
  * @param {object} parsed - parsed pyproject.toml
96
162
  * @param {Object} opts
97
163
  * @returns {Promise<DependencyData>}
98
164
  * @protected
99
165
  */
100
166
  // eslint-disable-next-line no-unused-vars
101
- async _getDependencyData(manifestDir, parsed, opts) {
167
+ async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
102
168
  throw new TypeError('_getDependencyData must be implemented');
103
169
  }
104
170
  // --- shared helpers ---
@@ -162,64 +228,29 @@ export default class Base_pyproject {
162
228
  return ignored;
163
229
  }
164
230
  /**
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.
231
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
196
232
  * @param {Map<string, GraphEntry>} graph
197
- * @param {string[]} childKeys
198
- * @param {DepTreeEntry[]} result - mutated in place
233
+ * @param {string[]} directDeps
199
234
  * @param {Set<string>} ignoredDeps
200
- * @param {Set<string>} visited
201
- * @returns {void}
235
+ * @returns {Set<string>}
202
236
  * @protected
203
237
  */
204
- _collectTransitive(graph, childKeys, result, ignoredDeps, visited) {
205
- for (let childKey of childKeys) {
206
- let canonKey = this._canonicalize(childKey);
207
- if (ignoredDeps.has(canonKey)) {
238
+ _reachableNodes(graph, directDeps, ignoredDeps) {
239
+ let reachable = new Set();
240
+ let queue = directDeps.filter(k => !ignoredDeps.has(k) && graph.has(k));
241
+ while (queue.length > 0) {
242
+ let key = queue.shift();
243
+ if (reachable.has(key)) {
208
244
  continue;
209
245
  }
210
- if (visited.has(canonKey)) {
211
- continue;
212
- }
213
- visited.add(canonKey);
214
- let entry = graph.get(canonKey);
215
- if (!entry) {
216
- continue;
246
+ reachable.add(key);
247
+ for (let child of graph.get(key).children) {
248
+ if (!ignoredDeps.has(child) && graph.has(child) && !reachable.has(child)) {
249
+ queue.push(child);
250
+ }
217
251
  }
218
- let childDeps = [];
219
- this._collectTransitive(graph, entry.children, childDeps, ignoredDeps, visited);
220
- result.push({ name: entry.name, version: entry.version, dependencies: childDeps });
221
252
  }
222
- result.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
253
+ return reachable;
223
254
  }
224
255
  /**
225
256
  * @param {string} name
@@ -230,21 +261,6 @@ export default class Base_pyproject {
230
261
  _toPurl(name, version) {
231
262
  return new PackageURL('pypi', undefined, name, version, undefined, undefined);
232
263
  }
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
264
  /**
249
265
  * Create SBOM json string for a pyproject.toml project.
250
266
  * @param {string} manifest - path to pyproject.toml
@@ -257,23 +273,50 @@ export default class Base_pyproject {
257
273
  let manifestDir = path.dirname(manifest);
258
274
  let content = fs.readFileSync(manifest, 'utf-8');
259
275
  let parsed = parseToml(content);
260
- let { directDeps, graph } = await this._getDependencyData(manifestDir, parsed, opts);
276
+ let workspaceDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
277
+ let { directDeps, graph } = await this._getDependencyData(manifestDir, workspaceDir, parsed, opts);
261
278
  let ignoredDeps = this._getIgnoredDeps(manifest);
262
- let dependencies = this._buildDependencyTree(graph, directDeps, ignoredDeps, includeTransitive);
263
279
  let sbom = new Sbom();
264
280
  let rootName = this._getProjectName(parsed) || DEFAULT_ROOT_NAME;
265
281
  let rootVersion = this._getProjectVersion(parsed) || DEFAULT_ROOT_VERSION;
266
282
  let rootPurl = this._toPurl(rootName, rootVersion);
267
283
  let license = this.readLicenseFromManifest(manifest);
268
284
  sbom.addRoot(rootPurl, license);
269
- dependencies.forEach(dep => {
270
- if (includeTransitive) {
271
- this._addAllDependencies(rootPurl, dep, sbom);
285
+ if (includeTransitive) {
286
+ let reachable = this._reachableNodes(graph, directDeps, ignoredDeps);
287
+ for (let key of directDeps) {
288
+ if (!reachable.has(key)) {
289
+ continue;
290
+ }
291
+ let entry = graph.get(key);
292
+ sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version), NO_SCOPE, entry.hashes);
272
293
  }
273
- else {
274
- sbom.addDependency(rootPurl, this._toPurl(dep.name, dep.version));
294
+ for (let [key, entry] of graph) {
295
+ if (!reachable.has(key)) {
296
+ continue;
297
+ }
298
+ let parentPurl = this._toPurl(entry.name, entry.version);
299
+ for (let child of entry.children) {
300
+ if (!reachable.has(child)) {
301
+ continue;
302
+ }
303
+ let childEntry = graph.get(child);
304
+ sbom.addDependency(parentPurl, this._toPurl(childEntry.name, childEntry.version), NO_SCOPE, childEntry.hashes);
305
+ }
275
306
  }
276
- });
307
+ }
308
+ else {
309
+ for (let key of directDeps) {
310
+ if (ignoredDeps.has(key)) {
311
+ continue;
312
+ }
313
+ let entry = graph.get(key);
314
+ if (!entry) {
315
+ continue;
316
+ }
317
+ sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version), NO_SCOPE, entry.hashes);
318
+ }
319
+ }
277
320
  return sbom.getAsJsonString(opts);
278
321
  }
279
322
  }
@@ -0,0 +1,5 @@
1
+ export function getParser(): Promise<Parser>;
2
+ export function getFromQuery(): Promise<Query>;
3
+ export function getArgQuery(): Promise<Query>;
4
+ import { Parser } from 'web-tree-sitter';
5
+ import { Query } from 'web-tree-sitter';
@@ -0,0 +1,20 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { Language, Parser, Query } from 'web-tree-sitter';
3
+ const wasmUrl = new URL('./tree-sitter-containerfile.wasm', import.meta.url);
4
+ async function init() {
5
+ await Parser.init();
6
+ const wasmBytes = new Uint8Array(await readFile(wasmUrl));
7
+ return await Language.load(wasmBytes);
8
+ }
9
+ export async function getParser() {
10
+ const language = await init();
11
+ return new Parser().setLanguage(language);
12
+ }
13
+ export async function getFromQuery() {
14
+ const language = await init();
15
+ return new Query(language, '(from_instruction (image_spec) @image)');
16
+ }
17
+ export async function getArgQuery() {
18
+ const language = await init();
19
+ return new Query(language, '(arg_instruction (arg_pair name: (_) @name default: (_) @default))');
20
+ }