@trustify-da/trustify-da-javascript-client 0.3.0-ea.f2c4df7 → 0.3.0-ea.f501753

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 (62) hide show
  1. package/README.md +151 -13
  2. package/dist/package.json +11 -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 +14 -1
  9. package/dist/src/cyclone_dx_sbom.js +34 -6
  10. package/dist/src/index.d.ts +132 -1
  11. package/dist/src/index.js +340 -4
  12. package/dist/src/license/licenses_api.js +9 -2
  13. package/dist/src/license/project_license.d.ts +0 -8
  14. package/dist/src/license/project_license.js +0 -11
  15. package/dist/src/oci_image/utils.js +11 -2
  16. package/dist/src/provider.d.ts +6 -3
  17. package/dist/src/provider.js +14 -5
  18. package/dist/src/providers/base_java.d.ts +0 -9
  19. package/dist/src/providers/base_java.js +2 -38
  20. package/dist/src/providers/base_javascript.d.ts +19 -3
  21. package/dist/src/providers/base_javascript.js +99 -18
  22. package/dist/src/providers/base_pyproject.d.ts +153 -0
  23. package/dist/src/providers/base_pyproject.js +315 -0
  24. package/dist/src/providers/golang_gomodules.d.ts +21 -12
  25. package/dist/src/providers/golang_gomodules.js +164 -118
  26. package/dist/src/providers/gomod_parser.d.ts +4 -0
  27. package/dist/src/providers/gomod_parser.js +16 -0
  28. package/dist/src/providers/java_gradle.d.ts +19 -0
  29. package/dist/src/providers/java_gradle.js +114 -0
  30. package/dist/src/providers/java_maven.d.ts +8 -0
  31. package/dist/src/providers/java_maven.js +93 -1
  32. package/dist/src/providers/javascript_npm.d.ts +1 -0
  33. package/dist/src/providers/javascript_npm.js +21 -0
  34. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  35. package/dist/src/providers/javascript_pnpm.js +8 -4
  36. package/dist/src/providers/manifest.d.ts +2 -0
  37. package/dist/src/providers/manifest.js +22 -4
  38. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  39. package/dist/src/providers/marker_evaluator.js +191 -0
  40. package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
  41. package/dist/src/providers/python_controller.d.ts +5 -1
  42. package/dist/src/providers/python_controller.js +8 -4
  43. package/dist/src/providers/python_pip.d.ts +4 -0
  44. package/dist/src/providers/python_pip.js +5 -5
  45. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  46. package/dist/src/providers/python_pip_pyproject.js +144 -0
  47. package/dist/src/providers/python_poetry.d.ts +75 -0
  48. package/dist/src/providers/python_poetry.js +238 -0
  49. package/dist/src/providers/python_uv.d.ts +42 -0
  50. package/dist/src/providers/python_uv.js +160 -0
  51. package/dist/src/providers/requirements_parser.js +4 -3
  52. package/dist/src/providers/rust_cargo.d.ts +52 -0
  53. package/dist/src/providers/rust_cargo.js +614 -0
  54. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  55. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  56. package/dist/src/sbom.d.ts +14 -1
  57. package/dist/src/sbom.js +13 -2
  58. package/dist/src/tools.d.ts +26 -0
  59. package/dist/src/tools.js +58 -0
  60. package/dist/src/workspace.d.ts +61 -0
  61. package/dist/src/workspace.js +256 -0
  62. package/package.json +12 -4
@@ -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
@@ -3,7 +3,7 @@ import os from "node:os";
3
3
  import path from 'node:path';
4
4
  import { getLicense } from '../license/license_utils.js';
5
5
  import Sbom from '../sbom.js';
6
- import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from "../tools.js";
6
+ import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from '../tools.js';
7
7
  import Manifest from './manifest.js';
8
8
  /** @typedef {import('../provider').Provider} */
9
9
  /** @typedef {import('../provider').Provided} Provided */
@@ -97,13 +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 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
+ /**
101
151
  * @param {string} manifestDir - The base directory where the manifest is located
152
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
102
153
  * @returns {boolean} True if the lock file exists
103
154
  */
104
- validateLockFile(manifestDir) {
105
- const lock = path.join(manifestDir, this._lockFileName());
106
- return fs.existsSync(lock);
155
+ validateLockFile(manifestDir, opts = {}) {
156
+ return this._findLockFileDir(manifestDir, opts) !== null;
107
157
  }
108
158
  /**
109
159
  * Provides content and content type for stack analysis
@@ -159,14 +209,16 @@ export default class Base_javascript {
159
209
  /**
160
210
  * Builds the dependency tree for the project
161
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
162
213
  * @returns {Object} The dependency tree
163
214
  * @protected
164
215
  */
165
- _buildDependencyTree(includeTransitive) {
216
+ _buildDependencyTree(includeTransitive, opts = {}) {
166
217
  this._version();
167
- let manifestDir = path.dirname(this.#manifest.manifestPath);
168
- this.#createLockFile(manifestDir);
169
- 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);
170
222
  output = this._parseDepTreeOutput(output);
171
223
  return JSON.parse(output);
172
224
  }
@@ -177,15 +229,38 @@ export default class Base_javascript {
177
229
  * @private
178
230
  */
179
231
  #getSBOM(opts = {}) {
180
- const depsObject = this._buildDependencyTree(true);
232
+ const depsObject = this._buildDependencyTree(true, opts);
181
233
  let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
182
234
  const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
183
235
  let sbom = new Sbom();
184
236
  sbom.addRoot(mainComponent, license);
185
237
  this._addDependenciesToSbom(sbom, depsObject);
238
+ this.#ensurePeerAndOptionalDeps(sbom);
186
239
  sbom.filterIgnoredDeps(this.#manifest.ignored);
187
240
  return sbom.getAsJsonString(opts);
188
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
+ }
189
264
  /**
190
265
  * Recursively builds the Sbom from the JSON that npm listing returns
191
266
  * @param {Sbom} sbom - The SBOM object to add dependencies to
@@ -193,7 +268,10 @@ export default class Base_javascript {
193
268
  * @protected
194
269
  */
195
270
  _addDependenciesToSbom(sbom, depTree) {
196
- const dependencies = depTree["dependencies"] || {};
271
+ const dependencies = {
272
+ ...depTree["dependencies"],
273
+ ...depTree["optionalDependencies"],
274
+ };
197
275
  Object.entries(dependencies)
198
276
  .forEach(entry => {
199
277
  const [name, artifact] = entry;
@@ -229,7 +307,7 @@ export default class Base_javascript {
229
307
  * @private
230
308
  */
231
309
  #getDirectDependencySbom(opts = {}) {
232
- const depTree = this._buildDependencyTree(false);
310
+ const depTree = this._buildDependencyTree(false, opts);
233
311
  let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
234
312
  const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
235
313
  let sbom = new Sbom();
@@ -243,6 +321,7 @@ export default class Base_javascript {
243
321
  const rootPurl = toPurlFromString(sbom.getRoot().purl);
244
322
  sbom.addDependency(rootPurl, rootDeps.get(key));
245
323
  }
324
+ this.#ensurePeerAndOptionalDeps(sbom);
246
325
  sbom.filterIgnoredDeps(this.#manifest.ignored);
247
326
  return sbom.getAsJsonString(opts);
248
327
  }
@@ -253,10 +332,14 @@ export default class Base_javascript {
253
332
  * @protected
254
333
  */
255
334
  _getRootDependencies(depTree) {
256
- if (!depTree.dependencies) {
335
+ const allDeps = {
336
+ ...depTree.dependencies,
337
+ ...depTree.optionalDependencies,
338
+ };
339
+ if (Object.keys(allDeps).length === 0) {
257
340
  return new Map();
258
341
  }
259
- 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)]));
260
343
  }
261
344
  /**
262
345
  * Executes the list command to get dependencies
@@ -267,7 +350,7 @@ export default class Base_javascript {
267
350
  */
268
351
  #executeListCmd(includeTransitive, manifestDir) {
269
352
  const listArgs = this._listCmdArgs(includeTransitive, manifestDir);
270
- return this.#invokeCommand(listArgs);
353
+ return this.#invokeCommand(listArgs, { cwd: manifestDir });
271
354
  }
272
355
  /**
273
356
  * Gets the version of the package manager
@@ -286,13 +369,11 @@ export default class Base_javascript {
286
369
  const originalDir = process.cwd();
287
370
  const isWindows = os.platform() === 'win32';
288
371
  if (isWindows) {
289
- // On Windows, --prefix flag doesn't work as expected
290
- // Instead of installing from the prefix folder, it installs from current working directory
291
372
  process.chdir(manifestDir);
292
373
  }
293
374
  try {
294
375
  const args = this._updateLockFileCmdArgs(manifestDir);
295
- this.#invokeCommand(args);
376
+ this.#invokeCommand(args, { cwd: manifestDir });
296
377
  }
297
378
  finally {
298
379
  if (isWindows) {
@@ -0,0 +1,153 @@
1
+ /** @typedef {{name: string, version: string, children: string[], hashes?: Array<{alg: string, content: 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
+ hashes?: Array<{
135
+ alg: string;
136
+ content: string;
137
+ }>;
138
+ };
139
+ export type DepTreeEntry = {
140
+ name: string;
141
+ version: string;
142
+ dependencies: DepTreeEntry[];
143
+ };
144
+ export type DependencyData = {
145
+ directDeps: string[];
146
+ graph: Map<string, GraphEntry>;
147
+ };
148
+ export type Provided = {
149
+ ecosystem: string;
150
+ content: string;
151
+ contentType: string;
152
+ };
153
+ import { PackageURL } from 'packageurl-js';