@trustify-da/trustify-da-javascript-client 0.3.0-ea.fbdacbb → 0.3.0-ea.ff694a0

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 (74) hide show
  1. package/README.md +191 -11
  2. package/dist/package.json +24 -11
  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 +197 -11
  11. package/dist/src/index.js +356 -8
  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 +18 -5
  24. package/dist/src/provider.js +31 -5
  25. package/dist/src/providers/base_java.d.ts +8 -14
  26. package/dist/src/providers/base_java.js +9 -38
  27. package/dist/src/providers/base_javascript.d.ts +40 -7
  28. package/dist/src/providers/base_javascript.js +138 -24
  29. package/dist/src/providers/base_pyproject.d.ts +158 -0
  30. package/dist/src/providers/base_pyproject.js +322 -0
  31. package/dist/src/providers/golang_gomodules.d.ts +30 -13
  32. package/dist/src/providers/golang_gomodules.js +176 -121
  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 +128 -4
  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_bun.d.ts +10 -0
  42. package/dist/src/providers/javascript_bun.js +100 -0
  43. package/dist/src/providers/javascript_npm.d.ts +1 -0
  44. package/dist/src/providers/javascript_npm.js +21 -0
  45. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  46. package/dist/src/providers/javascript_pnpm.js +8 -4
  47. package/dist/src/providers/manifest.d.ts +2 -0
  48. package/dist/src/providers/manifest.js +22 -4
  49. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  50. package/dist/src/providers/marker_evaluator.js +191 -0
  51. package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
  52. package/dist/src/providers/python_controller.d.ts +10 -3
  53. package/dist/src/providers/python_controller.js +61 -59
  54. package/dist/src/providers/python_pip.d.ts +16 -4
  55. package/dist/src/providers/python_pip.js +51 -58
  56. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  57. package/dist/src/providers/python_pip_pyproject.js +146 -0
  58. package/dist/src/providers/python_poetry.d.ts +75 -0
  59. package/dist/src/providers/python_poetry.js +238 -0
  60. package/dist/src/providers/python_uv.d.ts +55 -0
  61. package/dist/src/providers/python_uv.js +227 -0
  62. package/dist/src/providers/requirements_parser.d.ts +6 -0
  63. package/dist/src/providers/requirements_parser.js +24 -0
  64. package/dist/src/providers/rust_cargo.d.ts +53 -0
  65. package/dist/src/providers/rust_cargo.js +614 -0
  66. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  67. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  68. package/dist/src/sbom.d.ts +17 -2
  69. package/dist/src/sbom.js +16 -4
  70. package/dist/src/tools.d.ts +48 -6
  71. package/dist/src/tools.js +114 -1
  72. package/dist/src/workspace.d.ts +70 -0
  73. package/dist/src/workspace.js +256 -0
  74. package/package.json +25 -12
@@ -0,0 +1,322 @@
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
+ * Returns the package manager name (e.g. pip, poetry, uv)
150
+ * @returns {string}
151
+ */
152
+ packageManagerName() {
153
+ return this._cmdName();
154
+ }
155
+ /**
156
+ * Resolve dependencies using the tool-specific command and parser.
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)
161
+ * @param {object} parsed - parsed pyproject.toml
162
+ * @param {Object} opts
163
+ * @returns {Promise<DependencyData>}
164
+ * @protected
165
+ */
166
+ // eslint-disable-next-line no-unused-vars
167
+ async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
168
+ throw new TypeError('_getDependencyData must be implemented');
169
+ }
170
+ // --- shared helpers ---
171
+ /**
172
+ * Canonicalize a Python package name per PEP 503.
173
+ * @param {string} name
174
+ * @returns {string}
175
+ * @protected
176
+ */
177
+ _canonicalize(name) {
178
+ return name.toLowerCase().replace(/[-_.]+/g, '-');
179
+ }
180
+ /**
181
+ * Get the project name from pyproject.toml.
182
+ * @param {object} parsed
183
+ * @returns {string|null}
184
+ * @protected
185
+ */
186
+ _getProjectName(parsed) {
187
+ return parsed.project?.name || parsed.tool?.poetry?.name || null;
188
+ }
189
+ /**
190
+ * Get the project version from pyproject.toml.
191
+ * @param {object} parsed
192
+ * @returns {string|null}
193
+ * @protected
194
+ */
195
+ _getProjectVersion(parsed) {
196
+ return parsed.project?.version || parsed.tool?.poetry?.version || null;
197
+ }
198
+ /**
199
+ * Scan raw pyproject.toml text for dependencies with ignore markers.
200
+ * @param {string} manifestPath
201
+ * @returns {Set<string>}
202
+ * @protected
203
+ */
204
+ _getIgnoredDeps(manifestPath) {
205
+ let ignored = new Set();
206
+ let content = fs.readFileSync(manifestPath, 'utf-8');
207
+ let lines = content.split(/\r?\n/);
208
+ for (let line of lines) {
209
+ if (!IGNORE_MARKERS.some(m => line.includes(m))) {
210
+ continue;
211
+ }
212
+ // PEP 621 style: "requests>=2.25" #exhortignore
213
+ let pep621Match = line.match(/^\s*"([^"]+)"/);
214
+ if (pep621Match) {
215
+ let reqStr = pep621Match[1];
216
+ let nameMatch = reqStr.match(/^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)/);
217
+ if (nameMatch) {
218
+ ignored.add(this._canonicalize(nameMatch[1]));
219
+ }
220
+ continue;
221
+ }
222
+ // Poetry style: requests = "^2.25" #exhortignore
223
+ let poetryMatch = line.match(/^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*=/);
224
+ if (poetryMatch) {
225
+ ignored.add(this._canonicalize(poetryMatch[1]));
226
+ }
227
+ }
228
+ return ignored;
229
+ }
230
+ /**
231
+ * Compute the set of graph nodes reachable from direct deps, excluding ignored.
232
+ * @param {Map<string, GraphEntry>} graph
233
+ * @param {string[]} directDeps
234
+ * @param {Set<string>} ignoredDeps
235
+ * @returns {Set<string>}
236
+ * @protected
237
+ */
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)) {
244
+ continue;
245
+ }
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
+ }
251
+ }
252
+ }
253
+ return reachable;
254
+ }
255
+ /**
256
+ * @param {string} name
257
+ * @param {string} version
258
+ * @returns {PackageURL}
259
+ * @protected
260
+ */
261
+ _toPurl(name, version) {
262
+ return new PackageURL('pypi', undefined, name, version, undefined, undefined);
263
+ }
264
+ /**
265
+ * Create SBOM json string for a pyproject.toml project.
266
+ * @param {string} manifest - path to pyproject.toml
267
+ * @param {Object} opts
268
+ * @param {boolean} includeTransitive
269
+ * @returns {Promise<string>}
270
+ * @private
271
+ */
272
+ async _createSbom(manifest, opts, includeTransitive) {
273
+ let manifestDir = path.dirname(manifest);
274
+ let content = fs.readFileSync(manifest, 'utf-8');
275
+ let parsed = parseToml(content);
276
+ let workspaceDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
277
+ let { directDeps, graph } = await this._getDependencyData(manifestDir, workspaceDir, parsed, opts);
278
+ let ignoredDeps = this._getIgnoredDeps(manifest);
279
+ let sbom = new Sbom();
280
+ let rootName = this._getProjectName(parsed) || DEFAULT_ROOT_NAME;
281
+ let rootVersion = this._getProjectVersion(parsed) || DEFAULT_ROOT_VERSION;
282
+ let rootPurl = this._toPurl(rootName, rootVersion);
283
+ let license = this.readLicenseFromManifest(manifest);
284
+ sbom.addRoot(rootPurl, license);
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);
293
+ }
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
+ }
306
+ }
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
+ }
320
+ return sbom.getAsJsonString(opts);
321
+ }
322
+ }
@@ -1,11 +1,22 @@
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 };
16
+ export function packageManagerName(): string;
6
17
  }
7
18
  export default _default;
8
- export type Provided = import('../provider').Provided;
19
+ export type Provided = import("../provider").Provided;
9
20
  export type Package = {
10
21
  name: string;
11
22
  version: string;
@@ -18,25 +29,31 @@ export type Dependency = {
18
29
  ignore: boolean;
19
30
  };
20
31
  /**
21
- * @param {string} manifestName - the subject manifest name-type
22
- * @returns {boolean} - return true if `pom.xml` is the manifest name-type
23
- */
32
+ * @param {string} manifestName the subject manifest name-type
33
+ * @returns {boolean} return true if `pom.xml` is the manifest name-type
34
+ */
24
35
  declare function isSupported(manifestName: string): boolean;
25
36
  /**
26
- * @param {string} manifestDir - the directory where the manifest lies
37
+ * @param {string} manifestDir the directory where the manifest lies
27
38
  */
28
39
  declare function validateLockFile(): boolean;
29
40
  /**
30
41
  * 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}
42
+ * @param {string} manifest path to go.mod for component report
43
+ * @param {{}} [opts={}] optional various options to pass along the application
44
+ * @returns {Promise<Provided>}
34
45
  */
35
- declare function provideComponent(manifest: string, opts?: {} | undefined): Provided;
46
+ declare function provideComponent(manifest: string, opts?: {}): Promise<Provided>;
36
47
  /**
37
48
  * 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}
49
+ * @param {string} manifest the manifest path or name
50
+ * @param {{}} [opts={}] optional various options to pass along the application
51
+ * @returns {Promise<Provided>}
41
52
  */
42
- declare function provideStack(manifest: string, opts?: {} | undefined): Provided;
53
+ declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
54
+ /**
55
+ * Go modules have no standard license field in go.mod
56
+ * @param {string} manifestPath path to go.mod
57
+ * @returns {string|null}
58
+ */
59
+ declare function readLicenseFromManifest(manifestPath: string): string | null;