@trustify-da/trustify-da-javascript-client 0.3.0-ea.63ae5c2 → 0.3.0-ea.7144952

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 (44) hide show
  1. package/README.md +119 -4
  2. package/dist/package.json +10 -2
  3. package/dist/src/analysis.d.ts +16 -6
  4. package/dist/src/analysis.js +69 -66
  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 +192 -8
  8. package/dist/src/cyclone_dx_sbom.d.ts +3 -1
  9. package/dist/src/cyclone_dx_sbom.js +18 -5
  10. package/dist/src/index.d.ts +64 -1
  11. package/dist/src/index.js +267 -4
  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/provider.d.ts +15 -3
  21. package/dist/src/provider.js +23 -5
  22. package/dist/src/providers/base_javascript.d.ts +19 -7
  23. package/dist/src/providers/base_javascript.js +48 -14
  24. package/dist/src/providers/golang_gomodules.d.ts +8 -1
  25. package/dist/src/providers/golang_gomodules.js +13 -4
  26. package/dist/src/providers/java_gradle.d.ts +6 -0
  27. package/dist/src/providers/java_gradle.js +12 -2
  28. package/dist/src/providers/java_maven.d.ts +8 -1
  29. package/dist/src/providers/java_maven.js +32 -4
  30. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  31. package/dist/src/providers/javascript_pnpm.js +2 -2
  32. package/dist/src/providers/python_pip.d.ts +7 -0
  33. package/dist/src/providers/python_pip.js +13 -3
  34. package/dist/src/providers/requirements_parser.js +5 -8
  35. package/dist/src/providers/rust_cargo.d.ts +52 -0
  36. package/dist/src/providers/rust_cargo.js +614 -0
  37. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  38. package/dist/src/sbom.d.ts +3 -1
  39. package/dist/src/sbom.js +3 -2
  40. package/dist/src/tools.d.ts +18 -0
  41. package/dist/src/tools.js +55 -0
  42. package/dist/src/workspace.d.ts +61 -0
  43. package/dist/src/workspace.js +256 -0
  44. package/package.json +11 -3
@@ -3,6 +3,7 @@ import os from 'node:os';
3
3
  import path from 'node:path';
4
4
  import { EOL } from 'os';
5
5
  import { XMLParser } from 'fast-xml-parser';
6
+ import { getLicense } from '../license/license_utils.js';
6
7
  import Sbom from '../sbom.js';
7
8
  import { getCustom } from '../tools.js';
8
9
  import Base_java, { ecosystem_maven } from "./base_java.js";
@@ -51,6 +52,30 @@ export default class Java_maven extends Base_java {
51
52
  contentType: 'application/vnd.cyclonedx+json'
52
53
  };
53
54
  }
55
+ /**
56
+ * Read license from pom.xml manifest, with fallback to LICENSE file
57
+ * @param {string} manifestPath - path to pom.xml
58
+ * @returns {string|null}
59
+ */
60
+ readLicenseFromManifest(manifestPath) {
61
+ let fromPom = null;
62
+ try {
63
+ const xml = fs.readFileSync(manifestPath, 'utf-8');
64
+ const parser = new XMLParser({ ignoreAttributes: false });
65
+ const obj = parser.parse(xml);
66
+ const project = obj?.project;
67
+ if (project?.licenses?.license) {
68
+ const license = Array.isArray(project.licenses.license)
69
+ ? project.licenses.license[0]
70
+ : project.licenses.license;
71
+ fromPom = (license?.name && license.name.trim()) || null;
72
+ }
73
+ }
74
+ catch {
75
+ // leave fromPom as null
76
+ }
77
+ return getLicense(fromPom, manifestPath);
78
+ }
54
79
  /**
55
80
  * Create a Dot Graph dependency tree for a manifest path.
56
81
  * @param {string} manifest - path for pom.xml
@@ -105,7 +130,7 @@ export default class Java_maven extends Base_java {
105
130
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
106
131
  console.error("Dependency tree that will be used as input for creating the BOM =>" + EOL + EOL + content.toString());
107
132
  }
108
- let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps, opts);
133
+ let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps, opts, manifest);
109
134
  // delete temp file and directory
110
135
  fs.rmSync(tmpDir, { recursive: true, force: true });
111
136
  // return dependency graph as string
@@ -115,15 +140,17 @@ export default class Java_maven extends Base_java {
115
140
  *
116
141
  * @param {String} textGraphList Text graph String of the manifest
117
142
  * @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
143
+ * @param {String} manifestPath Path to the pom.xml manifest
118
144
  * @return {String} formatted sbom Json String with all dependencies
119
145
  */
120
- createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts) {
146
+ createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts, manifestPath) {
121
147
  let lines = textGraphList.split(EOL);
122
148
  // get root component
123
149
  let root = lines[0];
124
150
  let rootPurl = this.parseDep(root);
151
+ const license = this.readLicenseFromManifest(manifestPath);
125
152
  let sbom = new Sbom();
126
- sbom.addRoot(rootPurl);
153
+ sbom.addRoot(rootPurl, license);
127
154
  this.parseDependencyTree(root, 0, lines.slice(1), sbom);
128
155
  return sbom.filterIgnoredDeps(ignoredDeps).getAsJsonString(opts);
129
156
  }
@@ -156,7 +183,8 @@ export default class Java_maven extends Base_java {
156
183
  let sbom = new Sbom();
157
184
  let rootDependency = this.#getRootFromPom(tmpEffectivePom, manifestPath);
158
185
  let purlRoot = this.toPurl(rootDependency.groupId, rootDependency.artifactId, rootDependency.version);
159
- sbom.addRoot(purlRoot);
186
+ const license = this.readLicenseFromManifest(manifestPath);
187
+ sbom.addRoot(purlRoot, license);
160
188
  dependencies.forEach(dep => {
161
189
  let currentPurl = this.toPurl(dep.groupId, dep.artifactId, dep.version);
162
190
  sbom.addDependency(purlRoot, currentPurl);
@@ -1,5 +1,5 @@
1
1
  export default class Javascript_pnpm extends Base_javascript {
2
2
  _listCmdArgs(includeTransitive: any): string[];
3
- _buildDependencyTree(includeTransitive: any, manifest: any): any;
3
+ _buildDependencyTree(includeTransitive: any, opts?: {}): any;
4
4
  }
5
5
  import Base_javascript from './base_javascript.js';
@@ -12,8 +12,8 @@ export default class Javascript_pnpm extends Base_javascript {
12
12
  _updateLockFileCmdArgs() {
13
13
  return ['install', '--frozen-lockfile'];
14
14
  }
15
- _buildDependencyTree(includeTransitive, manifest) {
16
- const tree = super._buildDependencyTree(includeTransitive, manifest);
15
+ _buildDependencyTree(includeTransitive, opts = {}) {
16
+ const tree = super._buildDependencyTree(includeTransitive, opts);
17
17
  if (Array.isArray(tree) && tree.length > 0) {
18
18
  return tree[0];
19
19
  }
@@ -3,6 +3,7 @@ declare namespace _default {
3
3
  export { validateLockFile };
4
4
  export { provideComponent };
5
5
  export { provideStack };
6
+ export { readLicenseFromManifest };
6
7
  }
7
8
  export default _default;
8
9
  export type DependencyEntry = {
@@ -33,3 +34,9 @@ declare function provideComponent(manifest: string, opts?: {}): Promise<Provided
33
34
  * @returns {Promise<Provided>}
34
35
  */
35
36
  declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
37
+ /**
38
+ * Python requirements.txt has no standard license field
39
+ * @param {string} manifestPath - path to requirements.txt
40
+ * @returns {string|null}
41
+ */
42
+ declare function readLicenseFromManifest(manifestPath: string): string | null;
@@ -1,10 +1,11 @@
1
1
  import fs from 'node:fs';
2
2
  import { PackageURL } from 'packageurl-js';
3
+ import { readLicenseFile } from '../license/license_utils.js';
3
4
  import Sbom from '../sbom.js';
4
5
  import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from "../tools.js";
5
6
  import Python_controller from './python_controller.js';
6
7
  import { getParser, getIgnoreQuery, getPinnedVersionQuery } from './requirements_parser.js';
7
- export default { isSupported, validateLockFile, provideComponent, provideStack };
8
+ export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
8
9
  /** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
9
10
  /**
10
11
  * @type {string} ecosystem for python-pip is 'pip'
@@ -18,6 +19,13 @@ const ecosystem = 'pip';
18
19
  function isSupported(manifestName) {
19
20
  return 'requirements.txt' === manifestName;
20
21
  }
22
+ /**
23
+ * Python requirements.txt has no standard license field
24
+ * @param {string} manifestPath - path to requirements.txt
25
+ * @returns {string|null}
26
+ */
27
+ // eslint-disable-next-line no-unused-vars
28
+ function readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
21
29
  /**
22
30
  * @param {string} manifestDir - the directory where the manifest lies
23
31
  */
@@ -167,7 +175,8 @@ async function createSbomStackAnalysis(manifest, opts = {}) {
167
175
  let dependencies = await pythonController.getDependencies(true);
168
176
  let sbom = new Sbom();
169
177
  const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
170
- sbom.addRoot(rootPurl);
178
+ const license = readLicenseFromManifest(manifest);
179
+ sbom.addRoot(rootPurl, license);
171
180
  dependencies.forEach(dep => {
172
181
  addAllDependencies(rootPurl, dep, sbom);
173
182
  });
@@ -190,7 +199,8 @@ async function getSbomForComponentAnalysis(manifest, opts = {}) {
190
199
  let dependencies = await pythonController.getDependencies(false);
191
200
  let sbom = new Sbom();
192
201
  const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
193
- sbom.addRoot(rootPurl);
202
+ const license = readLicenseFromManifest(manifest);
203
+ sbom.addRoot(rootPurl, license);
194
204
  dependencies.forEach(dep => {
195
205
  sbom.addDependency(rootPurl, toPurl(dep.name, dep.version));
196
206
  });
@@ -1,13 +1,10 @@
1
- import { createRequire } from 'node:module';
1
+ import { readFile } from 'node:fs/promises';
2
2
  import { Language, Parser, Query } from 'web-tree-sitter';
3
- const require = createRequire(import.meta.url);
3
+ const wasmUrl = new URL('./tree-sitter-requirements.wasm', import.meta.url);
4
4
  async function init() {
5
- await Parser.init({
6
- locateFile() {
7
- return require.resolve('web-tree-sitter/web-tree-sitter.wasm');
8
- }
9
- });
10
- return await Language.load(require.resolve('tree-sitter-requirements/tree-sitter-requirements.wasm'));
5
+ await Parser.init();
6
+ const wasmBytes = new Uint8Array(await readFile(wasmUrl));
7
+ return await Language.load(wasmBytes);
11
8
  }
12
9
  export async function getParser() {
13
10
  const language = await init();
@@ -0,0 +1,52 @@
1
+ declare namespace _default {
2
+ export { isSupported };
3
+ export { validateLockFile };
4
+ export { provideComponent };
5
+ export { provideStack };
6
+ export { readLicenseFromManifest };
7
+ }
8
+ export default _default;
9
+ export type Provided = import("../provider").Provided;
10
+ /**
11
+ * @param {string} manifestName - the subject manifest name-type
12
+ * @returns {boolean} - return true if `Cargo.toml` is the manifest name-type
13
+ */
14
+ declare function isSupported(manifestName: string): boolean;
15
+ /**
16
+ * Validates that Cargo.lock exists in the manifest directory or in a parent
17
+ * workspace root directory. In Cargo workspaces the lock file always lives at
18
+ * the workspace root, so when a member crate's Cargo.toml is provided we walk
19
+ * up the directory tree looking for Cargo.lock (stopping when we find a
20
+ * Cargo.toml that contains a [workspace] section, or when we reach the
21
+ * filesystem root).
22
+ * When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
23
+ * checks only that directory for Cargo.lock — no walk-up.
24
+ * @param {string} manifestDir - the directory where the manifest lies
25
+ * @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
26
+ * @returns {boolean} true if Cargo.lock is found
27
+ */
28
+ declare function validateLockFile(manifestDir: string, opts?: {
29
+ TRUSTIFY_DA_WORKSPACE_DIR?: string;
30
+ }): boolean;
31
+ /**
32
+ * Provide content and content type for Cargo component analysis.
33
+ * @param {string} manifest - path to Cargo.toml for component report
34
+ * @param {{}} [opts={}] - optional various options to pass along the application
35
+ * @returns {Provided}
36
+ */
37
+ declare function provideComponent(manifest: string, opts?: {}): Provided;
38
+ /**
39
+ * Provide content and content type for Cargo stack analysis.
40
+ * @param {string} manifest - the manifest path
41
+ * @param {{}} [opts={}] - optional various options to pass along the application
42
+ * @returns {Provided}
43
+ */
44
+ declare function provideStack(manifest: string, opts?: {}): Provided;
45
+ /**
46
+ * Read project license from Cargo.toml, with fallback to LICENSE file.
47
+ * Supports the `license` field under `[package]` (single crate / workspace
48
+ * with root) and under `[workspace.package]` (virtual workspaces).
49
+ * @param {string} manifestPath - path to Cargo.toml
50
+ * @returns {string|null} SPDX identifier or null
51
+ */
52
+ declare function readLicenseFromManifest(manifestPath: string): string | null;