@trustify-da/trustify-da-javascript-client 0.3.0-ea.6549d2a → 0.3.0-ea.6645ba5

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 (70) hide show
  1. package/README.md +179 -11
  2. package/dist/package.json +13 -4
  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 +45 -9
  10. package/dist/src/index.d.ts +134 -2
  11. package/dist/src/index.js +352 -6
  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 +8 -4
  21. package/dist/src/provider.js +16 -5
  22. package/dist/src/providers/base_java.d.ts +12 -10
  23. package/dist/src/providers/base_java.js +18 -44
  24. package/dist/src/providers/base_javascript.d.ts +30 -3
  25. package/dist/src/providers/base_javascript.js +115 -25
  26. package/dist/src/providers/base_pyproject.d.ts +158 -0
  27. package/dist/src/providers/base_pyproject.js +322 -0
  28. package/dist/src/providers/golang_gomodules.d.ts +22 -12
  29. package/dist/src/providers/golang_gomodules.js +167 -120
  30. package/dist/src/providers/gomod_parser.d.ts +4 -0
  31. package/dist/src/providers/gomod_parser.js +16 -0
  32. package/dist/src/providers/java_gradle.d.ts +19 -0
  33. package/dist/src/providers/java_gradle.js +118 -3
  34. package/dist/src/providers/java_maven.d.ts +9 -1
  35. package/dist/src/providers/java_maven.js +175 -10
  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 +1 -0
  39. package/dist/src/providers/javascript_npm.js +21 -0
  40. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  41. package/dist/src/providers/javascript_pnpm.js +8 -4
  42. package/dist/src/providers/manifest.d.ts +2 -0
  43. package/dist/src/providers/manifest.js +22 -4
  44. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  45. package/dist/src/providers/marker_evaluator.js +191 -0
  46. package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
  47. package/dist/src/providers/python_controller.d.ts +5 -1
  48. package/dist/src/providers/python_controller.js +65 -7
  49. package/dist/src/providers/python_pip.d.ts +5 -0
  50. package/dist/src/providers/python_pip.js +8 -7
  51. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  52. package/dist/src/providers/python_pip_pyproject.js +146 -0
  53. package/dist/src/providers/python_poetry.d.ts +97 -0
  54. package/dist/src/providers/python_poetry.js +271 -0
  55. package/dist/src/providers/python_uv.d.ts +46 -0
  56. package/dist/src/providers/python_uv.js +269 -0
  57. package/dist/src/providers/requirements_parser.js +4 -3
  58. package/dist/src/providers/rust_cargo.d.ts +56 -0
  59. package/dist/src/providers/rust_cargo.js +641 -0
  60. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  61. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  62. package/dist/src/sbom.d.ts +14 -1
  63. package/dist/src/sbom.js +13 -2
  64. package/dist/src/tools.d.ts +26 -0
  65. package/dist/src/tools.js +58 -0
  66. package/dist/src/workspace.d.ts +70 -0
  67. package/dist/src/workspace.js +256 -0
  68. package/package.json +14 -5
  69. package/dist/src/license/compatibility.d.ts +0 -18
  70. package/dist/src/license/compatibility.js +0 -45
@@ -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 */
@@ -70,6 +71,13 @@ export default class Base_javascript {
70
71
  throw new TypeError("_cmdName must be implemented");
71
72
  }
72
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
+ /**
73
81
  * Returns the command arguments for listing dependencies
74
82
  * @returns {Array<string>} The command arguments
75
83
  * @abstract
@@ -96,13 +104,63 @@ export default class Base_javascript {
96
104
  return 'package.json' === manifestName;
97
105
  }
98
106
  /**
99
- * Checks if a required lock file exists in the same path as the manifest
107
+ * Walks up the directory tree from manifestDir looking for the lock file.
108
+ * Stops when the lock file is found, when a package.json with a "workspaces"
109
+ * field is encountered without a lock file (workspace root boundary), or
110
+ * when the filesystem root is reached.
111
+ *
112
+ * When TRUSTIFY_DA_WORKSPACE_DIR is set, checks only that directory (no walk-up).
113
+ *
114
+ * @param {string} manifestDir - The directory to start searching from
115
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
116
+ * @returns {string|null} The directory containing the lock file, or null
117
+ * @protected
118
+ */
119
+ _isWorkspaceRoot(dir) {
120
+ if (fs.existsSync(path.join(dir, 'pnpm-workspace.yaml'))) {
121
+ return true;
122
+ }
123
+ const pkgJsonPath = path.join(dir, 'package.json');
124
+ if (fs.existsSync(pkgJsonPath)) {
125
+ try {
126
+ const content = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
127
+ if (content.workspaces) {
128
+ return true;
129
+ }
130
+ }
131
+ catch (_) {
132
+ // ignore parse errors
133
+ }
134
+ }
135
+ return false;
136
+ }
137
+ _findLockFileDir(manifestDir, opts = {}) {
138
+ const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
139
+ if (workspaceDir) {
140
+ const dir = path.resolve(workspaceDir);
141
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
142
+ }
143
+ let dir = path.resolve(manifestDir);
144
+ let parent = dir;
145
+ do {
146
+ dir = parent;
147
+ if (fs.existsSync(path.join(dir, this._lockFileName()))) {
148
+ return dir;
149
+ }
150
+ if (this._isWorkspaceRoot(dir)) {
151
+ return null;
152
+ }
153
+ parent = path.dirname(dir);
154
+ } while (parent !== dir);
155
+ return null;
156
+ }
157
+ /**
100
158
  * @param {string} manifestDir - The base directory where the manifest is located
159
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
101
160
  * @returns {boolean} True if the lock file exists
102
161
  */
103
- validateLockFile(manifestDir) {
104
- const lock = path.join(manifestDir, this._lockFileName());
105
- return fs.existsSync(lock);
162
+ validateLockFile(manifestDir, opts = {}) {
163
+ return this._findLockFileDir(manifestDir, opts) !== null;
106
164
  }
107
165
  /**
108
166
  * Provides content and content type for stack analysis
@@ -138,33 +196,36 @@ export default class Base_javascript {
138
196
  * @returns {string|null}
139
197
  */
140
198
  readLicenseFromManifest(manifestPath) {
199
+ let manifestLicense;
141
200
  try {
142
201
  const content = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
143
202
  if (typeof content.license === 'string') {
144
- return content.license.trim() || null;
203
+ manifestLicense = content.license.trim() || null;
145
204
  }
146
- if (Array.isArray(content.licenses) && content.licenses.length > 0) {
205
+ else if (Array.isArray(content.licenses) && content.licenses.length > 0) {
147
206
  const first = content.licenses[0];
148
207
  const name = first.type || first.name;
149
- return typeof name === 'string' ? name.trim() : null;
208
+ manifestLicense = (typeof name === 'string' ? name.trim() : null);
150
209
  }
151
- return null;
152
210
  }
153
211
  catch {
154
- return null;
212
+ manifestLicense = null;
155
213
  }
214
+ return getLicense(manifestLicense, manifestPath);
156
215
  }
157
216
  /**
158
217
  * Builds the dependency tree for the project
159
218
  * @param {boolean} includeTransitive - Whether to include transitive dependencies
219
+ * @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root
160
220
  * @returns {Object} The dependency tree
161
221
  * @protected
162
222
  */
163
- _buildDependencyTree(includeTransitive) {
223
+ _buildDependencyTree(includeTransitive, opts = {}) {
164
224
  this._version();
165
- let manifestDir = path.dirname(this.#manifest.manifestPath);
166
- this.#createLockFile(manifestDir);
167
- let output = this.#executeListCmd(includeTransitive, manifestDir);
225
+ const manifestDir = path.dirname(this.#manifest.manifestPath);
226
+ const cmdDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
227
+ this._createLockFile(cmdDir);
228
+ let output = this.#executeListCmd(includeTransitive, cmdDir);
168
229
  output = this._parseDepTreeOutput(output);
169
230
  return JSON.parse(output);
170
231
  }
@@ -175,15 +236,38 @@ export default class Base_javascript {
175
236
  * @private
176
237
  */
177
238
  #getSBOM(opts = {}) {
178
- const depsObject = this._buildDependencyTree(true);
239
+ const depsObject = this._buildDependencyTree(true, opts);
179
240
  let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
180
241
  const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
181
242
  let sbom = new Sbom();
182
243
  sbom.addRoot(mainComponent, license);
183
244
  this._addDependenciesToSbom(sbom, depsObject);
245
+ this.#ensurePeerAndOptionalDeps(sbom);
184
246
  sbom.filterIgnoredDeps(this.#manifest.ignored);
185
247
  return sbom.getAsJsonString(opts);
186
248
  }
249
+ /**
250
+ * Ensures peer and optional dependencies declared in the manifest are
251
+ * present in the SBOM, even when the package manager does not resolve them
252
+ * (e.g. yarn does not include peer deps in its dependency listing).
253
+ * @param {Sbom} sbom - The SBOM to supplement
254
+ * @private
255
+ */
256
+ #ensurePeerAndOptionalDeps(sbom) {
257
+ const rootPurl = toPurl(purlType, this.#manifest.name, this.#manifest.version);
258
+ const depSources = [this.#manifest.peerDependencies, this.#manifest.optionalDependencies];
259
+ for (const source of depSources) {
260
+ for (const [name, version] of Object.entries(source)) {
261
+ // Build the purl prefix for exact matching (e.g. "pkg:npm/minimist@"
262
+ // or "pkg:npm/%40hapi/joi@") to avoid substring false positives
263
+ const probe = toPurl(purlType, name, version);
264
+ const purlPrefix = probe.toString().replace(/@[^@]*$/, '@');
265
+ if (!sbom.checkDependsOnByPurlPrefix(rootPurl, purlPrefix)) {
266
+ sbom.addDependency(rootPurl, probe);
267
+ }
268
+ }
269
+ }
270
+ }
187
271
  /**
188
272
  * Recursively builds the Sbom from the JSON that npm listing returns
189
273
  * @param {Sbom} sbom - The SBOM object to add dependencies to
@@ -191,7 +275,10 @@ export default class Base_javascript {
191
275
  * @protected
192
276
  */
193
277
  _addDependenciesToSbom(sbom, depTree) {
194
- const dependencies = depTree["dependencies"] || {};
278
+ const dependencies = {
279
+ ...depTree["dependencies"],
280
+ ...depTree["optionalDependencies"],
281
+ };
195
282
  Object.entries(dependencies)
196
283
  .forEach(entry => {
197
284
  const [name, artifact] = entry;
@@ -227,7 +314,7 @@ export default class Base_javascript {
227
314
  * @private
228
315
  */
229
316
  #getDirectDependencySbom(opts = {}) {
230
- const depTree = this._buildDependencyTree(false);
317
+ const depTree = this._buildDependencyTree(false, opts);
231
318
  let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
232
319
  const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
233
320
  let sbom = new Sbom();
@@ -241,6 +328,7 @@ export default class Base_javascript {
241
328
  const rootPurl = toPurlFromString(sbom.getRoot().purl);
242
329
  sbom.addDependency(rootPurl, rootDeps.get(key));
243
330
  }
331
+ this.#ensurePeerAndOptionalDeps(sbom);
244
332
  sbom.filterIgnoredDeps(this.#manifest.ignored);
245
333
  return sbom.getAsJsonString(opts);
246
334
  }
@@ -251,10 +339,14 @@ export default class Base_javascript {
251
339
  * @protected
252
340
  */
253
341
  _getRootDependencies(depTree) {
254
- if (!depTree.dependencies) {
342
+ const allDeps = {
343
+ ...depTree.dependencies,
344
+ ...depTree.optionalDependencies,
345
+ };
346
+ if (Object.keys(allDeps).length === 0) {
255
347
  return new Map();
256
348
  }
257
- return new Map(Object.entries(depTree.dependencies).map(([key, value]) => [key, toPurl(purlType, key, value.version)]));
349
+ return new Map(Object.entries(allDeps).map(([key, value]) => [key, toPurl(purlType, key, value.version)]));
258
350
  }
259
351
  /**
260
352
  * Executes the list command to get dependencies
@@ -265,7 +357,7 @@ export default class Base_javascript {
265
357
  */
266
358
  #executeListCmd(includeTransitive, manifestDir) {
267
359
  const listArgs = this._listCmdArgs(includeTransitive, manifestDir);
268
- return this.#invokeCommand(listArgs);
360
+ return this.#invokeCommand(listArgs, { cwd: manifestDir });
269
361
  }
270
362
  /**
271
363
  * Gets the version of the package manager
@@ -278,19 +370,17 @@ export default class Base_javascript {
278
370
  /**
279
371
  * Creates or updates the lock file for the package manager
280
372
  * @param {string} manifestDir - Directory containing the manifest file
281
- * @private
373
+ * @protected
282
374
  */
283
- #createLockFile(manifestDir) {
375
+ _createLockFile(manifestDir) {
284
376
  const originalDir = process.cwd();
285
377
  const isWindows = os.platform() === 'win32';
286
378
  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
379
  process.chdir(manifestDir);
290
380
  }
291
381
  try {
292
382
  const args = this._updateLockFileCmdArgs(manifestDir);
293
- this.#invokeCommand(args);
383
+ this.#invokeCommand(args, { cwd: manifestDir });
294
384
  }
295
385
  finally {
296
386
  if (isWindows) {
@@ -0,0 +1,158 @@
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
+ * Returns the package manager name (e.g. pip, poetry, uv)
66
+ * @returns {string}
67
+ */
68
+ packageManagerName(): string;
69
+ /**
70
+ * Resolve dependencies using the tool-specific command and parser.
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)
75
+ * @param {object} parsed - parsed pyproject.toml
76
+ * @param {Object} opts
77
+ * @returns {Promise<DependencyData>}
78
+ * @protected
79
+ */
80
+ protected _getDependencyData(manifestDir: string, workspaceDir: string, parsed: object, opts: any): Promise<DependencyData>;
81
+ /**
82
+ * Canonicalize a Python package name per PEP 503.
83
+ * @param {string} name
84
+ * @returns {string}
85
+ * @protected
86
+ */
87
+ protected _canonicalize(name: string): string;
88
+ /**
89
+ * Get the project name from pyproject.toml.
90
+ * @param {object} parsed
91
+ * @returns {string|null}
92
+ * @protected
93
+ */
94
+ protected _getProjectName(parsed: object): string | null;
95
+ /**
96
+ * Get the project version from pyproject.toml.
97
+ * @param {object} parsed
98
+ * @returns {string|null}
99
+ * @protected
100
+ */
101
+ protected _getProjectVersion(parsed: object): string | null;
102
+ /**
103
+ * Scan raw pyproject.toml text for dependencies with ignore markers.
104
+ * @param {string} manifestPath
105
+ * @returns {Set<string>}
106
+ * @protected
107
+ */
108
+ protected _getIgnoredDeps(manifestPath: string): Set<string>;
109
+ /**
110
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
111
+ * @param {Map<string, GraphEntry>} graph
112
+ * @param {string[]} directDeps
113
+ * @param {Set<string>} ignoredDeps
114
+ * @returns {Set<string>}
115
+ * @protected
116
+ */
117
+ protected _reachableNodes(graph: Map<string, GraphEntry>, directDeps: string[], ignoredDeps: Set<string>): Set<string>;
118
+ /**
119
+ * @param {string} name
120
+ * @param {string} version
121
+ * @returns {PackageURL}
122
+ * @protected
123
+ */
124
+ protected _toPurl(name: string, version: string): PackageURL;
125
+ /**
126
+ * Create SBOM json string for a pyproject.toml project.
127
+ * @param {string} manifest - path to pyproject.toml
128
+ * @param {Object} opts
129
+ * @param {boolean} includeTransitive
130
+ * @returns {Promise<string>}
131
+ * @private
132
+ */
133
+ private _createSbom;
134
+ }
135
+ export type GraphEntry = {
136
+ name: string;
137
+ version: string;
138
+ children: string[];
139
+ hashes?: Array<{
140
+ alg: string;
141
+ content: string;
142
+ }>;
143
+ };
144
+ export type DepTreeEntry = {
145
+ name: string;
146
+ version: string;
147
+ dependencies: DepTreeEntry[];
148
+ };
149
+ export type DependencyData = {
150
+ directDeps: string[];
151
+ graph: Map<string, GraphEntry>;
152
+ };
153
+ export type Provided = {
154
+ ecosystem: string;
155
+ content: string;
156
+ contentType: string;
157
+ };
158
+ import { PackageURL } from 'packageurl-js';