@trustify-da/trustify-da-javascript-client 0.3.0-ea.2ea1d77 → 0.3.0-ea.38515a7

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.
@@ -0,0 +1,146 @@
1
+ import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
2
+ import Base_pyproject from './base_pyproject.js';
3
+ export default class Python_poetry extends Base_pyproject {
4
+ /** @returns {string} */
5
+ _lockFileName() {
6
+ return 'poetry.lock';
7
+ }
8
+ /** @returns {string} */
9
+ _cmdName() {
10
+ return 'poetry';
11
+ }
12
+ /**
13
+ * @param {string} manifestDir
14
+ * @param {object} parsed - parsed pyproject.toml
15
+ * @param {Object} opts
16
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
17
+ */
18
+ async _getDependencyData(manifestDir, parsed, opts) {
19
+ let treeOutput = this._getPoetryShowTreeOutput(manifestDir, opts);
20
+ let showAllOutput = this._getPoetryShowAllOutput(manifestDir, opts);
21
+ let versionMap = this._parsePoetryShowAll(showAllOutput);
22
+ return this._parsePoetryTree(treeOutput, versionMap);
23
+ }
24
+ /**
25
+ * Get poetry show --tree output.
26
+ * @param {string} manifestDir
27
+ * @param {Object} opts
28
+ * @returns {string}
29
+ */
30
+ _getPoetryShowTreeOutput(manifestDir, opts) {
31
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_TREE')) {
32
+ return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_TREE'], 'base64').toString('utf-8');
33
+ }
34
+ let poetryBin = getCustomPath('poetry', opts);
35
+ return invokeCommand(poetryBin, ['show', '--tree', '--no-ansi'], { cwd: manifestDir }).toString();
36
+ }
37
+ /**
38
+ * Get poetry show --all output (flat list with resolved versions).
39
+ * @param {string} manifestDir
40
+ * @param {Object} opts
41
+ * @returns {string}
42
+ */
43
+ _getPoetryShowAllOutput(manifestDir, opts) {
44
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_ALL')) {
45
+ return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_ALL'], 'base64').toString('utf-8');
46
+ }
47
+ let poetryBin = getCustomPath('poetry', opts);
48
+ return invokeCommand(poetryBin, ['show', '--no-ansi', '--all'], { cwd: manifestDir }).toString();
49
+ }
50
+ /**
51
+ * Parse poetry show --all output into a version map.
52
+ * Lines look like: "name (!) 1.2.3 Description text..."
53
+ * or: "name 1.2.3 Description text..."
54
+ * @param {string} output
55
+ * @returns {Map<string, string>} canonical name -> version
56
+ */
57
+ _parsePoetryShowAll(output) {
58
+ let versions = new Map();
59
+ let lines = output.split(/\r?\n/);
60
+ for (let line of lines) {
61
+ let trimmed = line.trim();
62
+ if (!trimmed) {
63
+ continue;
64
+ }
65
+ let match = trimmed.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(?:\(!\)\s+)?(\S+)/);
66
+ if (match) {
67
+ versions.set(this._canonicalize(match[1]), match[2]);
68
+ }
69
+ }
70
+ return versions;
71
+ }
72
+ /**
73
+ * Parse poetry show --tree output into a dependency graph structure.
74
+ * Top-level lines (no indentation/tree chars) are direct deps: "name version description"
75
+ * Indented lines are transitive deps with tree chars: "├── name >=constraint"
76
+ *
77
+ * @param {string} treeOutput
78
+ * @param {Map<string, string>} versionMap - canonical name -> resolved version
79
+ * @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
80
+ */
81
+ _parsePoetryTree(treeOutput, versionMap) {
82
+ let lines = treeOutput.split(/\r?\n/);
83
+ let graph = new Map();
84
+ let directDeps = [];
85
+ let stack = []; // [{key, depth}]
86
+ let currentDirectDep = null;
87
+ for (let line of lines) {
88
+ if (!line.trim()) {
89
+ continue;
90
+ }
91
+ // top-level line: "name version description..."
92
+ let topMatch = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(\S+)\s/);
93
+ if (topMatch) {
94
+ let name = topMatch[1];
95
+ let version = topMatch[2];
96
+ let key = this._canonicalize(name);
97
+ directDeps.push(key);
98
+ if (!graph.has(key)) {
99
+ graph.set(key, { name, version, children: [] });
100
+ }
101
+ currentDirectDep = key;
102
+ stack = [{ key, depth: -1 }];
103
+ continue;
104
+ }
105
+ if (!currentDirectDep) {
106
+ continue;
107
+ }
108
+ // indented line with tree chars (UTF-8 box-drawing: ├── └── │)
109
+ let nameStart = line.search(/[A-Za-z0-9]/);
110
+ if (nameStart < 0) {
111
+ continue;
112
+ }
113
+ let rest = line.substring(nameStart);
114
+ let depMatch = rest.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)/);
115
+ if (!depMatch) {
116
+ continue;
117
+ }
118
+ let depName = depMatch[1];
119
+ let depKey = this._canonicalize(depName);
120
+ // determine depth by counting tree-drawing groups in the prefix
121
+ let prefix = line.substring(0, nameStart);
122
+ let depth = (prefix.match(/(?:[├└│ ][\s─]{2} ?)/g) || []).length;
123
+ // resolve version from the version map
124
+ let version = versionMap.get(depKey) || null;
125
+ if (!version) {
126
+ throw new Error(`poetry: package '${depName}' has no resolved version`);
127
+ }
128
+ if (!graph.has(depKey)) {
129
+ graph.set(depKey, { name: depName, version, children: [] });
130
+ }
131
+ // pop stack back to find the parent at depth-1
132
+ while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
133
+ stack.pop();
134
+ }
135
+ if (stack.length > 0) {
136
+ let parentKey = stack[stack.length - 1].key;
137
+ let parentEntry = graph.get(parentKey);
138
+ if (parentEntry && !parentEntry.children.includes(depKey)) {
139
+ parentEntry.children.push(depKey);
140
+ }
141
+ }
142
+ stack.push({ key: depKey, depth });
143
+ }
144
+ return { directDeps, graph };
145
+ }
146
+ }
@@ -0,0 +1,26 @@
1
+ export default class Python_uv extends Base_pyproject {
2
+ /**
3
+ * Get the uv export output, either from env var or by running the command.
4
+ * @param {string} manifestDir
5
+ * @param {Object} opts
6
+ * @returns {string}
7
+ */
8
+ _getUvExportOutput(manifestDir: string, opts: any): string;
9
+ /**
10
+ * Parse uv export output into a dependency graph using tree-sitter-requirements
11
+ * for package/version extraction and string parsing for "# via" comments.
12
+ *
13
+ * @param {string} output
14
+ * @param {string} projectName - canonical project name to identify direct deps
15
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
16
+ */
17
+ _parseUvExport(output: string, projectName: string): Promise<{
18
+ directDeps: string[];
19
+ graph: Map<string, {
20
+ name: string;
21
+ version: string;
22
+ children: string[];
23
+ }>;
24
+ }>;
25
+ }
26
+ import Base_pyproject from './base_pyproject.js';
@@ -0,0 +1,118 @@
1
+ import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
2
+ import Base_pyproject from './base_pyproject.js';
3
+ import { getParser, getPinnedVersionQuery } from './requirements_parser.js';
4
+ export default class Python_uv extends Base_pyproject {
5
+ /** @returns {string} */
6
+ _lockFileName() {
7
+ return 'uv.lock';
8
+ }
9
+ /** @returns {string} */
10
+ _cmdName() {
11
+ return 'uv';
12
+ }
13
+ /**
14
+ * @param {string} manifestDir
15
+ * @param {object} parsed - parsed pyproject.toml
16
+ * @param {Object} opts
17
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
18
+ */
19
+ async _getDependencyData(manifestDir, parsed, opts) {
20
+ let projectName = this._getProjectName(parsed);
21
+ let uvOutput = this._getUvExportOutput(manifestDir, opts);
22
+ return this._parseUvExport(uvOutput, projectName);
23
+ }
24
+ /**
25
+ * Get the uv export output, either from env var or by running the command.
26
+ * @param {string} manifestDir
27
+ * @param {Object} opts
28
+ * @returns {string}
29
+ */
30
+ _getUvExportOutput(manifestDir, opts) {
31
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_UV_EXPORT')) {
32
+ return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii');
33
+ }
34
+ let uvBin = getCustomPath('uv', opts);
35
+ return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes'], { cwd: manifestDir }).toString();
36
+ }
37
+ /**
38
+ * Parse uv export output into a dependency graph using tree-sitter-requirements
39
+ * for package/version extraction and string parsing for "# via" comments.
40
+ *
41
+ * @param {string} output
42
+ * @param {string} projectName - canonical project name to identify direct deps
43
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
44
+ */
45
+ async _parseUvExport(output, projectName) {
46
+ let [parser, pinnedVersionQuery] = await Promise.all([
47
+ getParser(), getPinnedVersionQuery()
48
+ ]);
49
+ let tree = parser.parse(output);
50
+ let root = tree.rootNode;
51
+ let canonProjectName = this._canonicalize(projectName);
52
+ let packages = new Map(); // canonical name -> {name, version, parents: Set}
53
+ let currentPkg = null;
54
+ let collectingVia = false;
55
+ for (let child of root.children) {
56
+ if (child.type === 'requirement') {
57
+ let nameNode = child.children.find(c => c.type === 'package');
58
+ if (!nameNode) {
59
+ continue;
60
+ }
61
+ let name = nameNode.text;
62
+ let version = null;
63
+ let versionMatches = pinnedVersionQuery.matches(child);
64
+ if (versionMatches.length > 0) {
65
+ version = versionMatches[0].captures.find(c => c.name === 'version').node.text;
66
+ }
67
+ if (!version) {
68
+ throw new Error(`uv export: package '${name}' has no pinned version`);
69
+ }
70
+ let key = this._canonicalize(name);
71
+ currentPkg = { name, version, parents: new Set() };
72
+ packages.set(key, currentPkg);
73
+ collectingVia = false;
74
+ continue;
75
+ }
76
+ if (child.type === 'comment' && currentPkg) {
77
+ let text = child.text.trim();
78
+ let viaSingle = text.match(/^# via ([A-Za-z0-9][A-Za-z0-9._-]*)$/);
79
+ if (viaSingle) {
80
+ currentPkg.parents.add(this._canonicalize(viaSingle[1]));
81
+ collectingVia = false;
82
+ continue;
83
+ }
84
+ if (text === '# via') {
85
+ collectingVia = true;
86
+ continue;
87
+ }
88
+ if (collectingVia) {
89
+ let parentMatch = text.match(/^#\s+([A-Za-z0-9][A-Za-z0-9._-]*)$/);
90
+ if (parentMatch) {
91
+ currentPkg.parents.add(this._canonicalize(parentMatch[1]));
92
+ continue;
93
+ }
94
+ collectingVia = false;
95
+ }
96
+ }
97
+ }
98
+ // Build forward dependency map and extract direct deps in one pass
99
+ let graph = new Map();
100
+ let directDeps = [];
101
+ for (let [key, pkg] of packages) {
102
+ graph.set(key, { name: pkg.name, version: pkg.version, children: [] });
103
+ }
104
+ for (let [childKey, pkg] of packages) {
105
+ for (let parentKey of pkg.parents) {
106
+ if (parentKey === canonProjectName) {
107
+ directDeps.push(childKey);
108
+ continue;
109
+ }
110
+ let parentEntry = graph.get(parentKey);
111
+ if (parentEntry) {
112
+ parentEntry.children.push(childKey);
113
+ }
114
+ }
115
+ }
116
+ return { directDeps, graph };
117
+ }
118
+ }
@@ -53,6 +53,13 @@ export default class Sbom {
53
53
  * @return {boolean}
54
54
  */
55
55
  checkIfPackageInsideDependsOnList(component: any, name: string): boolean;
56
+ /**
57
+ * Checks if any entry in the dependsOn list of sourceRef starts with the given purl prefix.
58
+ * @param {PackageURL} sourceRef - The source component to check
59
+ * @param {string} purlPrefix - The purl prefix to match (e.g. "pkg:npm/minimist@")
60
+ * @return {boolean}
61
+ */
62
+ checkDependsOnByPurlPrefix(sourceRef: PackageURL, purlPrefix: string): boolean;
56
63
  /** Removes the root component from the sbom
57
64
  */
58
65
  removeRootComponent(): void;
package/dist/src/sbom.js CHANGED
@@ -77,6 +77,15 @@ export default class Sbom {
77
77
  checkIfPackageInsideDependsOnList(component, name) {
78
78
  return this.sbomModel.checkIfPackageInsideDependsOnList(component, name);
79
79
  }
80
+ /**
81
+ * Checks if any entry in the dependsOn list of sourceRef starts with the given purl prefix.
82
+ * @param {PackageURL} sourceRef - The source component to check
83
+ * @param {string} purlPrefix - The purl prefix to match (e.g. "pkg:npm/minimist@")
84
+ * @return {boolean}
85
+ */
86
+ checkDependsOnByPurlPrefix(sourceRef, purlPrefix) {
87
+ return this.sbomModel.checkDependsOnByPurlPrefix(sourceRef, purlPrefix);
88
+ }
80
89
  /** Removes the root component from the sbom
81
90
  */
82
91
  removeRootComponent() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trustify-da/trustify-da-javascript-client",
3
- "version": "0.3.0-ea.2ea1d77",
3
+ "version": "0.3.0-ea.38515a7",
4
4
  "description": "Code-Ready Dependency Analytics JavaScript API.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/guacsec/trustify-da-javascript-client#README.md",
@@ -38,13 +38,13 @@
38
38
  "lint": "eslint src test --ext js",
39
39
  "lint:fix": "eslint src test --ext js --fix",
40
40
  "test": "c8 npm run tests",
41
- "tests": "mocha --config .mocharc.json --grep \".*analysis module.*\" --invert",
41
+ "tests": "mocha --config .mocharc.json",
42
42
  "tests:rep": "mocha --reporter-option maxDiffSize=0 --reporter json > unit-tests-result.json",
43
- "pretest": "cp node_modules/tree-sitter-requirements/tree-sitter-requirements.wasm src/providers/tree-sitter-requirements.wasm",
43
+ "pretest": "cp node_modules/tree-sitter-requirements/tree-sitter-requirements.wasm src/providers/tree-sitter-requirements.wasm && cp node_modules/tree-sitter-gomod/tree-sitter-gomod.wasm src/providers/tree-sitter-gomod.wasm",
44
44
  "precompile": "rm -rf dist",
45
45
  "compile": "tsc -p tsconfig.json",
46
46
  "compile:dev": "tsc -p tsconfig.dev.json",
47
- "postcompile": "cp node_modules/tree-sitter-requirements/tree-sitter-requirements.wasm dist/src/providers/tree-sitter-requirements.wasm"
47
+ "postcompile": "cp node_modules/tree-sitter-requirements/tree-sitter-requirements.wasm dist/src/providers/tree-sitter-requirements.wasm && cp node_modules/tree-sitter-gomod/tree-sitter-gomod.wasm dist/src/providers/tree-sitter-gomod.wasm"
48
48
  },
49
49
  "dependencies": {
50
50
  "@babel/core": "^7.23.2",
@@ -58,11 +58,12 @@
58
58
  "js-yaml": "^4.1.1",
59
59
  "micromatch": "^4.0.8",
60
60
  "node-fetch": "^3.3.2",
61
- "p-limit": "^5.0.0",
61
+ "p-limit": "^4.0.0",
62
62
  "packageurl-js": "~1.0.2",
63
63
  "smol-toml": "^1.6.0",
64
+ "tree-sitter-gomod": "github:strum355/tree-sitter-go-mod#56326f2ad478892ace58ff247a97d492a3cbcdda",
64
65
  "tree-sitter-requirements": "github:Strum355/tree-sitter-requirements#d0261ee76b84253997fe70d7d397e78c006c3801",
65
- "web-tree-sitter": "^0.26.6",
66
+ "web-tree-sitter": "^0.26.7",
66
67
  "yargs": "^18.0.0"
67
68
  },
68
69
  "devDependencies": {