@trustify-da/trustify-da-javascript-client 0.3.0-ea.cb4ae28 → 0.3.0-ea.cdf078c

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