@trustify-da/trustify-da-javascript-client 0.3.0-ea.ce73b2f → 0.3.0-ea.d2fee6b

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 (72) hide show
  1. package/README.md +191 -11
  2. package/dist/package.json +23 -10
  3. package/dist/src/analysis.d.ts +21 -5
  4. package/dist/src/analysis.js +74 -80
  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 +241 -8
  8. package/dist/src/cyclone_dx_sbom.d.ts +17 -3
  9. package/dist/src/cyclone_dx_sbom.js +48 -8
  10. package/dist/src/index.d.ts +196 -11
  11. package/dist/src/index.js +345 -7
  12. package/dist/src/license/index.d.ts +28 -0
  13. package/dist/src/license/index.js +100 -0
  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.d.ts +34 -0
  17. package/dist/src/license/licenses_api.js +98 -0
  18. package/dist/src/license/project_license.d.ts +20 -0
  19. package/dist/src/license/project_license.js +62 -0
  20. package/dist/src/oci_image/images.d.ts +4 -5
  21. package/dist/src/oci_image/utils.d.ts +4 -4
  22. package/dist/src/oci_image/utils.js +11 -2
  23. package/dist/src/provider.d.ts +17 -5
  24. package/dist/src/provider.js +29 -5
  25. package/dist/src/providers/base_java.d.ts +3 -14
  26. package/dist/src/providers/base_java.js +2 -38
  27. package/dist/src/providers/base_javascript.d.ts +29 -7
  28. package/dist/src/providers/base_javascript.js +129 -22
  29. package/dist/src/providers/base_pyproject.d.ts +153 -0
  30. package/dist/src/providers/base_pyproject.js +315 -0
  31. package/dist/src/providers/golang_gomodules.d.ts +29 -13
  32. package/dist/src/providers/golang_gomodules.js +161 -114
  33. package/dist/src/providers/gomod_parser.d.ts +4 -0
  34. package/dist/src/providers/gomod_parser.js +16 -0
  35. package/dist/src/providers/java_gradle.d.ts +28 -3
  36. package/dist/src/providers/java_gradle.js +126 -2
  37. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  38. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  39. package/dist/src/providers/java_maven.d.ts +20 -5
  40. package/dist/src/providers/java_maven.js +126 -6
  41. package/dist/src/providers/javascript_npm.d.ts +1 -0
  42. package/dist/src/providers/javascript_npm.js +21 -0
  43. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  44. package/dist/src/providers/javascript_pnpm.js +8 -4
  45. package/dist/src/providers/manifest.d.ts +2 -0
  46. package/dist/src/providers/manifest.js +22 -4
  47. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  48. package/dist/src/providers/marker_evaluator.js +191 -0
  49. package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
  50. package/dist/src/providers/python_controller.d.ts +10 -3
  51. package/dist/src/providers/python_controller.js +61 -59
  52. package/dist/src/providers/python_pip.d.ts +15 -4
  53. package/dist/src/providers/python_pip.js +51 -58
  54. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  55. package/dist/src/providers/python_pip_pyproject.js +144 -0
  56. package/dist/src/providers/python_poetry.d.ts +75 -0
  57. package/dist/src/providers/python_poetry.js +238 -0
  58. package/dist/src/providers/python_uv.d.ts +42 -0
  59. package/dist/src/providers/python_uv.js +160 -0
  60. package/dist/src/providers/requirements_parser.d.ts +6 -0
  61. package/dist/src/providers/requirements_parser.js +24 -0
  62. package/dist/src/providers/rust_cargo.d.ts +52 -0
  63. package/dist/src/providers/rust_cargo.js +614 -0
  64. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  65. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  66. package/dist/src/sbom.d.ts +17 -2
  67. package/dist/src/sbom.js +16 -4
  68. package/dist/src/tools.d.ts +48 -6
  69. package/dist/src/tools.js +114 -1
  70. package/dist/src/workspace.d.ts +61 -0
  71. package/dist/src/workspace.js +256 -0
  72. package/package.json +24 -11
@@ -0,0 +1,315 @@
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 NO_SCOPE = undefined;
11
+ const DEFAULT_ROOT_NAME = 'default-pip-root';
12
+ const DEFAULT_ROOT_VERSION = '0.0.0';
13
+ /** @typedef {{name: string, version: string, children: string[], hashes?: Array<{alg: string, content: string}>}} GraphEntry */
14
+ /** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
15
+ /** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
16
+ /** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
17
+ export default class Base_pyproject {
18
+ /**
19
+ * @param {string} manifestName
20
+ * @returns {boolean}
21
+ */
22
+ isSupported(manifestName) {
23
+ return 'pyproject.toml' === manifestName;
24
+ }
25
+ /**
26
+ * @param {string} manifestDir
27
+ * @param {Object} [opts={}]
28
+ * @returns {boolean}
29
+ */
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;
85
+ }
86
+ /**
87
+ * Read project license from pyproject.toml, with fallback to LICENSE file.
88
+ * @param {string} manifestPath
89
+ * @returns {string|null}
90
+ */
91
+ readLicenseFromManifest(manifestPath) {
92
+ let fromManifest = null;
93
+ try {
94
+ let content = fs.readFileSync(manifestPath, 'utf-8');
95
+ let parsed = parseToml(content);
96
+ fromManifest = parsed.project?.license;
97
+ if (typeof fromManifest === 'object' && fromManifest != null) {
98
+ fromManifest = fromManifest.text || null;
99
+ }
100
+ if (!fromManifest) {
101
+ fromManifest = parsed.tool?.poetry?.license || null;
102
+ }
103
+ }
104
+ catch (_) {
105
+ // leave fromManifest as null
106
+ }
107
+ return getLicense(fromManifest, manifestPath);
108
+ }
109
+ /**
110
+ * @param {string} manifest - path to pyproject.toml
111
+ * @param {Object} [opts={}]
112
+ * @returns {Promise<Provided>}
113
+ */
114
+ async provideStack(manifest, opts = {}) {
115
+ return {
116
+ ecosystem,
117
+ content: await this._createSbom(manifest, opts, true),
118
+ contentType: 'application/vnd.cyclonedx+json'
119
+ };
120
+ }
121
+ /**
122
+ * @param {string} manifest - path to pyproject.toml
123
+ * @param {Object} [opts={}]
124
+ * @returns {Promise<Provided>}
125
+ */
126
+ async provideComponent(manifest, opts = {}) {
127
+ return {
128
+ ecosystem,
129
+ content: await this._createSbom(manifest, opts, false),
130
+ contentType: 'application/vnd.cyclonedx+json'
131
+ };
132
+ }
133
+ // --- abstract methods (subclasses must override) ---
134
+ /**
135
+ * @returns {string}
136
+ * @protected
137
+ */
138
+ _lockFileName() {
139
+ throw new TypeError('_lockFileName must be implemented');
140
+ }
141
+ /**
142
+ * @returns {string}
143
+ * @protected
144
+ */
145
+ _cmdName() {
146
+ throw new TypeError('_cmdName must be implemented');
147
+ }
148
+ /**
149
+ * Resolve dependencies using the tool-specific command and parser.
150
+ *
151
+ * @param {string} manifestDir - directory containing the target pyproject.toml
152
+ * @param {string} workspaceDir - workspace root (where the lock file lives);
153
+ * only used by providers that need workspace-level resolution (e.g. uv)
154
+ * @param {object} parsed - parsed pyproject.toml
155
+ * @param {Object} opts
156
+ * @returns {Promise<DependencyData>}
157
+ * @protected
158
+ */
159
+ // eslint-disable-next-line no-unused-vars
160
+ async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
161
+ throw new TypeError('_getDependencyData must be implemented');
162
+ }
163
+ // --- shared helpers ---
164
+ /**
165
+ * Canonicalize a Python package name per PEP 503.
166
+ * @param {string} name
167
+ * @returns {string}
168
+ * @protected
169
+ */
170
+ _canonicalize(name) {
171
+ return name.toLowerCase().replace(/[-_.]+/g, '-');
172
+ }
173
+ /**
174
+ * Get the project name from pyproject.toml.
175
+ * @param {object} parsed
176
+ * @returns {string|null}
177
+ * @protected
178
+ */
179
+ _getProjectName(parsed) {
180
+ return parsed.project?.name || parsed.tool?.poetry?.name || null;
181
+ }
182
+ /**
183
+ * Get the project version from pyproject.toml.
184
+ * @param {object} parsed
185
+ * @returns {string|null}
186
+ * @protected
187
+ */
188
+ _getProjectVersion(parsed) {
189
+ return parsed.project?.version || parsed.tool?.poetry?.version || null;
190
+ }
191
+ /**
192
+ * Scan raw pyproject.toml text for dependencies with ignore markers.
193
+ * @param {string} manifestPath
194
+ * @returns {Set<string>}
195
+ * @protected
196
+ */
197
+ _getIgnoredDeps(manifestPath) {
198
+ let ignored = new Set();
199
+ let content = fs.readFileSync(manifestPath, 'utf-8');
200
+ let lines = content.split(/\r?\n/);
201
+ for (let line of lines) {
202
+ if (!IGNORE_MARKERS.some(m => line.includes(m))) {
203
+ continue;
204
+ }
205
+ // PEP 621 style: "requests>=2.25" #exhortignore
206
+ let pep621Match = line.match(/^\s*"([^"]+)"/);
207
+ if (pep621Match) {
208
+ let reqStr = pep621Match[1];
209
+ let nameMatch = reqStr.match(/^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)/);
210
+ if (nameMatch) {
211
+ ignored.add(this._canonicalize(nameMatch[1]));
212
+ }
213
+ continue;
214
+ }
215
+ // Poetry style: requests = "^2.25" #exhortignore
216
+ let poetryMatch = line.match(/^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*=/);
217
+ if (poetryMatch) {
218
+ ignored.add(this._canonicalize(poetryMatch[1]));
219
+ }
220
+ }
221
+ return ignored;
222
+ }
223
+ /**
224
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
225
+ * @param {Map<string, GraphEntry>} graph
226
+ * @param {string[]} directDeps
227
+ * @param {Set<string>} ignoredDeps
228
+ * @returns {Set<string>}
229
+ * @protected
230
+ */
231
+ _reachableNodes(graph, directDeps, ignoredDeps) {
232
+ let reachable = new Set();
233
+ let queue = directDeps.filter(k => !ignoredDeps.has(k) && graph.has(k));
234
+ while (queue.length > 0) {
235
+ let key = queue.shift();
236
+ if (reachable.has(key)) {
237
+ continue;
238
+ }
239
+ reachable.add(key);
240
+ for (let child of graph.get(key).children) {
241
+ if (!ignoredDeps.has(child) && graph.has(child) && !reachable.has(child)) {
242
+ queue.push(child);
243
+ }
244
+ }
245
+ }
246
+ return reachable;
247
+ }
248
+ /**
249
+ * @param {string} name
250
+ * @param {string} version
251
+ * @returns {PackageURL}
252
+ * @protected
253
+ */
254
+ _toPurl(name, version) {
255
+ return new PackageURL('pypi', undefined, name, version, undefined, undefined);
256
+ }
257
+ /**
258
+ * Create SBOM json string for a pyproject.toml project.
259
+ * @param {string} manifest - path to pyproject.toml
260
+ * @param {Object} opts
261
+ * @param {boolean} includeTransitive
262
+ * @returns {Promise<string>}
263
+ * @private
264
+ */
265
+ async _createSbom(manifest, opts, includeTransitive) {
266
+ let manifestDir = path.dirname(manifest);
267
+ let content = fs.readFileSync(manifest, 'utf-8');
268
+ let parsed = parseToml(content);
269
+ let workspaceDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
270
+ let { directDeps, graph } = await this._getDependencyData(manifestDir, workspaceDir, parsed, opts);
271
+ let ignoredDeps = this._getIgnoredDeps(manifest);
272
+ let sbom = new Sbom();
273
+ let rootName = this._getProjectName(parsed) || DEFAULT_ROOT_NAME;
274
+ let rootVersion = this._getProjectVersion(parsed) || DEFAULT_ROOT_VERSION;
275
+ let rootPurl = this._toPurl(rootName, rootVersion);
276
+ let license = this.readLicenseFromManifest(manifest);
277
+ sbom.addRoot(rootPurl, license);
278
+ if (includeTransitive) {
279
+ let reachable = this._reachableNodes(graph, directDeps, ignoredDeps);
280
+ for (let key of directDeps) {
281
+ if (!reachable.has(key)) {
282
+ continue;
283
+ }
284
+ let entry = graph.get(key);
285
+ sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version), NO_SCOPE, entry.hashes);
286
+ }
287
+ for (let [key, entry] of graph) {
288
+ if (!reachable.has(key)) {
289
+ continue;
290
+ }
291
+ let parentPurl = this._toPurl(entry.name, entry.version);
292
+ for (let child of entry.children) {
293
+ if (!reachable.has(child)) {
294
+ continue;
295
+ }
296
+ let childEntry = graph.get(child);
297
+ sbom.addDependency(parentPurl, this._toPurl(childEntry.name, childEntry.version), NO_SCOPE, childEntry.hashes);
298
+ }
299
+ }
300
+ }
301
+ else {
302
+ for (let key of directDeps) {
303
+ if (ignoredDeps.has(key)) {
304
+ continue;
305
+ }
306
+ let entry = graph.get(key);
307
+ if (!entry) {
308
+ continue;
309
+ }
310
+ sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version), NO_SCOPE, entry.hashes);
311
+ }
312
+ }
313
+ return sbom.getAsJsonString(opts);
314
+ }
315
+ }
@@ -1,11 +1,21 @@
1
+ /**
2
+ * Discover all go.mod manifest paths in a Go workspace.
3
+ * Uses `go work edit -json` to get workspace members.
4
+ *
5
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain go.work)
6
+ * @param {import('../index.js').Options} [opts={}]
7
+ * @returns {Promise<string[]>} Paths to go.mod files (absolute)
8
+ */
9
+ export function discoverGoWorkspaceModules(workspaceRoot: string, opts?: import("../index.js").Options): Promise<string[]>;
1
10
  declare namespace _default {
2
11
  export { isSupported };
3
12
  export { validateLockFile };
4
13
  export { provideComponent };
5
14
  export { provideStack };
15
+ export { readLicenseFromManifest };
6
16
  }
7
17
  export default _default;
8
- export type Provided = import('../provider').Provided;
18
+ export type Provided = import("../provider").Provided;
9
19
  export type Package = {
10
20
  name: string;
11
21
  version: string;
@@ -18,25 +28,31 @@ export type Dependency = {
18
28
  ignore: boolean;
19
29
  };
20
30
  /**
21
- * @param {string} manifestName - the subject manifest name-type
22
- * @returns {boolean} - return true if `pom.xml` is the manifest name-type
23
- */
31
+ * @param {string} manifestName the subject manifest name-type
32
+ * @returns {boolean} return true if `pom.xml` is the manifest name-type
33
+ */
24
34
  declare function isSupported(manifestName: string): boolean;
25
35
  /**
26
- * @param {string} manifestDir - the directory where the manifest lies
36
+ * @param {string} manifestDir the directory where the manifest lies
27
37
  */
28
38
  declare function validateLockFile(): boolean;
29
39
  /**
30
40
  * Provide content and content type for maven-maven component analysis.
31
- * @param {string} manifest - path to go.mod for component report
32
- * @param {{}} [opts={}] - optional various options to pass along the application
33
- * @returns {Provided}
41
+ * @param {string} manifest path to go.mod for component report
42
+ * @param {{}} [opts={}] optional various options to pass along the application
43
+ * @returns {Promise<Provided>}
34
44
  */
35
- declare function provideComponent(manifest: string, opts?: {} | undefined): Provided;
45
+ declare function provideComponent(manifest: string, opts?: {}): Promise<Provided>;
36
46
  /**
37
47
  * Provide content and content type for maven-maven stack analysis.
38
- * @param {string} manifest - the manifest path or name
39
- * @param {{}} [opts={}] - optional various options to pass along the application
40
- * @returns {Provided}
48
+ * @param {string} manifest the manifest path or name
49
+ * @param {{}} [opts={}] optional various options to pass along the application
50
+ * @returns {Promise<Provided>}
41
51
  */
42
- declare function provideStack(manifest: string, opts?: {} | undefined): Provided;
52
+ declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
53
+ /**
54
+ * Go modules have no standard license field in go.mod
55
+ * @param {string} manifestPath path to go.mod
56
+ * @returns {string|null}
57
+ */
58
+ declare function readLicenseFromManifest(manifestPath: string): string | null;