@trustify-da/trustify-da-javascript-client 0.3.0-ea.477151f → 0.3.0-ea.51c394b
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.
- package/README.md +191 -11
- package/dist/package.json +13 -4
- package/dist/src/analysis.d.ts +16 -6
- package/dist/src/analysis.js +72 -68
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +241 -8
- package/dist/src/cyclone_dx_sbom.d.ts +10 -1
- package/dist/src/cyclone_dx_sbom.js +32 -5
- package/dist/src/index.d.ts +76 -1
- package/dist/src/index.js +285 -4
- package/dist/src/license/index.d.ts +28 -0
- package/dist/src/license/index.js +100 -0
- package/dist/src/license/license_utils.d.ts +40 -0
- package/dist/src/license/license_utils.js +134 -0
- package/dist/src/license/licenses_api.d.ts +34 -0
- package/dist/src/license/licenses_api.js +98 -0
- package/dist/src/license/project_license.d.ts +20 -0
- package/dist/src/license/project_license.js +62 -0
- package/dist/src/oci_image/utils.js +11 -2
- package/dist/src/provider.d.ts +15 -3
- package/dist/src/provider.js +27 -5
- package/dist/src/providers/base_javascript.d.ts +29 -7
- package/dist/src/providers/base_javascript.js +129 -22
- package/dist/src/providers/base_pyproject.d.ts +170 -0
- package/dist/src/providers/base_pyproject.js +338 -0
- package/dist/src/providers/golang_gomodules.d.ts +19 -12
- package/dist/src/providers/golang_gomodules.js +112 -114
- package/dist/src/providers/gomod_parser.d.ts +4 -0
- package/dist/src/providers/gomod_parser.js +16 -0
- package/dist/src/providers/java_gradle.d.ts +6 -0
- package/dist/src/providers/java_gradle.js +12 -2
- package/dist/src/providers/java_maven.d.ts +8 -1
- package/dist/src/providers/java_maven.js +32 -4
- package/dist/src/providers/javascript_pnpm.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +2 -2
- package/dist/src/providers/manifest.d.ts +2 -0
- package/dist/src/providers/manifest.js +22 -4
- package/dist/src/providers/processors/yarn_berry_processor.js +82 -3
- package/dist/src/providers/python_pip.d.ts +7 -0
- package/dist/src/providers/python_pip.js +14 -4
- package/dist/src/providers/python_poetry.d.ts +42 -0
- package/dist/src/providers/python_poetry.js +169 -0
- package/dist/src/providers/python_uv.d.ts +27 -0
- package/dist/src/providers/python_uv.js +146 -0
- package/dist/src/providers/requirements_parser.js +5 -8
- package/dist/src/providers/rust_cargo.d.ts +52 -0
- package/dist/src/providers/rust_cargo.js +614 -0
- package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
- package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
- package/dist/src/sbom.d.ts +10 -1
- package/dist/src/sbom.js +12 -2
- package/dist/src/tools.d.ts +18 -0
- package/dist/src/tools.js +55 -0
- package/dist/src/workspace.d.ts +61 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +14 -5
|
@@ -48,13 +48,15 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
48
48
|
if (!depTree) {
|
|
49
49
|
return new Map();
|
|
50
50
|
}
|
|
51
|
-
return new Map(depTree.filter(dep => !this.#isRoot(dep.value))
|
|
51
|
+
return new Map(depTree.filter(dep => !this.#isRoot(dep.value))
|
|
52
|
+
.map(dep => {
|
|
52
53
|
const depName = dep.value;
|
|
53
54
|
const idx = depName.lastIndexOf('@');
|
|
54
55
|
const name = depName.substring(0, idx);
|
|
55
56
|
const version = dep.children.Version;
|
|
56
57
|
return [name, toPurl(purlType, name, version)];
|
|
57
|
-
})
|
|
58
|
+
})
|
|
59
|
+
.filter(([name]) => this._manifest.dependencies.includes(name)));
|
|
58
60
|
}
|
|
59
61
|
/**
|
|
60
62
|
* Checks if a dependency is the root package
|
|
@@ -77,14 +79,58 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
77
79
|
if (!depTree) {
|
|
78
80
|
return;
|
|
79
81
|
}
|
|
82
|
+
// Build index of nodes by their value for quick lookup
|
|
83
|
+
const nodeIndex = new Map();
|
|
84
|
+
depTree.forEach(n => nodeIndex.set(n.value, n));
|
|
85
|
+
// Determine the set of node values reachable from root via production deps
|
|
86
|
+
const prodDeps = new Set(this._manifest.dependencies);
|
|
87
|
+
const reachable = new Set();
|
|
88
|
+
const queue = [];
|
|
89
|
+
// Seed with root's production dependencies
|
|
90
|
+
const rootNode = depTree.find(n => this.#isRoot(n.value));
|
|
91
|
+
if (rootNode?.children?.Dependencies) {
|
|
92
|
+
for (const d of rootNode.children.Dependencies) {
|
|
93
|
+
const to = this.#purlFromLocator(d.locator);
|
|
94
|
+
if (to) {
|
|
95
|
+
const fullName = to.namespace ? `${to.namespace}/${to.name}` : to.name;
|
|
96
|
+
if (prodDeps.has(fullName)) {
|
|
97
|
+
queue.push(d.locator);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// BFS to find all transitively reachable packages
|
|
103
|
+
while (queue.length > 0) {
|
|
104
|
+
const locator = queue.shift();
|
|
105
|
+
if (reachable.has(locator)) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
reachable.add(locator);
|
|
109
|
+
const node = nodeIndex.get(this.#nodeValueFromLocator(locator));
|
|
110
|
+
if (node?.children?.Dependencies) {
|
|
111
|
+
for (const d of node.children.Dependencies) {
|
|
112
|
+
if (!reachable.has(d.locator)) {
|
|
113
|
+
queue.push(d.locator);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Only emit edges for root and reachable nodes
|
|
80
119
|
depTree.forEach(n => {
|
|
81
120
|
const depName = n.value;
|
|
82
|
-
const
|
|
121
|
+
const isRoot = this.#isRoot(depName);
|
|
122
|
+
if (!isRoot && !this.#isReachableNode(depName, reachable)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const from = isRoot ? toPurlFromString(sbom.getRoot().purl) : this.#purlFromNode(depName, n);
|
|
83
126
|
const deps = n.children?.Dependencies;
|
|
84
127
|
if (!deps) {
|
|
85
128
|
return;
|
|
86
129
|
}
|
|
87
130
|
deps.forEach(d => {
|
|
131
|
+
if (!reachable.has(d.locator)) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
88
134
|
const to = this.#purlFromLocator(d.locator);
|
|
89
135
|
if (to) {
|
|
90
136
|
sbom.addDependency(from, to);
|
|
@@ -92,6 +138,39 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
92
138
|
});
|
|
93
139
|
});
|
|
94
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Converts a locator to the node value format used in yarn info output
|
|
143
|
+
* @param {string} locator - e.g. "express@npm:4.17.1"
|
|
144
|
+
* @returns {string} The node value, same as locator for non-virtual
|
|
145
|
+
* @private
|
|
146
|
+
*/
|
|
147
|
+
#nodeValueFromLocator(locator) {
|
|
148
|
+
// Virtual locators: "@scope/name@virtual:hash#npm:version" → "@scope/name@npm:version"
|
|
149
|
+
const virtualMatch = Yarn_berry_processor.VIRTUAL_LOCATOR_PATTERN.exec(locator);
|
|
150
|
+
if (virtualMatch) {
|
|
151
|
+
return `${virtualMatch[1]}@npm:${virtualMatch[2]}`;
|
|
152
|
+
}
|
|
153
|
+
return locator;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Checks if a node is in the reachable set by matching its value against reachable locators
|
|
157
|
+
* @param {string} depName - The node value (e.g. "express@npm:4.17.1")
|
|
158
|
+
* @param {Set<string>} reachable - Set of reachable locators
|
|
159
|
+
* @returns {boolean}
|
|
160
|
+
* @private
|
|
161
|
+
*/
|
|
162
|
+
#isReachableNode(depName, reachable) {
|
|
163
|
+
if (reachable.has(depName)) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
// Check if any reachable locator resolves to this node value
|
|
167
|
+
for (const locator of reachable) {
|
|
168
|
+
if (this.#nodeValueFromLocator(locator) === depName) {
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
95
174
|
/**
|
|
96
175
|
* Creates a PackageURL from a dependency locator
|
|
97
176
|
* @param {string} locator - The dependency locator
|
|
@@ -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
|
*/
|
|
@@ -67,7 +75,7 @@ function addAllDependencies(source, dep, sbom) {
|
|
|
67
75
|
/**
|
|
68
76
|
*
|
|
69
77
|
* @param {string} manifest - path to requirements.txt
|
|
70
|
-
* @return {PackageURL
|
|
78
|
+
* @return {Promise<PackageURL[]>}
|
|
71
79
|
*/
|
|
72
80
|
async function getIgnoredDependencies(manifest) {
|
|
73
81
|
const [parser, ignoreQuery, pinnedVersionQuery] = await Promise.all([
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export default class Python_poetry extends Base_pyproject {
|
|
2
|
+
/**
|
|
3
|
+
* Get poetry show --tree output.
|
|
4
|
+
* @param {string} manifestDir
|
|
5
|
+
* @param {Object} opts
|
|
6
|
+
* @returns {string}
|
|
7
|
+
*/
|
|
8
|
+
_getPoetryShowTreeOutput(manifestDir: string, opts: any): string;
|
|
9
|
+
/**
|
|
10
|
+
* Get poetry show --all output (flat list with resolved versions).
|
|
11
|
+
* @param {string} manifestDir
|
|
12
|
+
* @param {Object} opts
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
_getPoetryShowAllOutput(manifestDir: string, opts: any): string;
|
|
16
|
+
/**
|
|
17
|
+
* Parse poetry show --all output into a version map.
|
|
18
|
+
* Lines look like: "name (!) 1.2.3 Description text..."
|
|
19
|
+
* or: "name 1.2.3 Description text..."
|
|
20
|
+
* @param {string} output
|
|
21
|
+
* @returns {Map<string, string>} canonical name -> version
|
|
22
|
+
*/
|
|
23
|
+
_parsePoetryShowAll(output: string): Map<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* Parse poetry show --tree output into a dependency graph structure.
|
|
26
|
+
* Top-level lines (no indentation/tree chars) are direct deps: "name version description"
|
|
27
|
+
* Indented lines are transitive deps with tree chars: "├── name >=constraint"
|
|
28
|
+
*
|
|
29
|
+
* @param {string} treeOutput
|
|
30
|
+
* @param {Map<string, string>} versionMap - canonical name -> resolved version
|
|
31
|
+
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
|
|
32
|
+
*/
|
|
33
|
+
_parsePoetryTree(treeOutput: string, versionMap: Map<string, string>): {
|
|
34
|
+
directDeps: string[];
|
|
35
|
+
graph: Map<string, {
|
|
36
|
+
name: string;
|
|
37
|
+
version: string;
|
|
38
|
+
children: string[];
|
|
39
|
+
}>;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
import Base_pyproject from './base_pyproject.js';
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from '../tools.js';
|
|
4
|
+
import Base_pyproject from './base_pyproject.js';
|
|
5
|
+
export default class Python_poetry extends Base_pyproject {
|
|
6
|
+
/**
|
|
7
|
+
* Poetry has no native workspace/monorepo support (python-poetry/poetry#2270).
|
|
8
|
+
* Each poetry project is treated independently — no lock file walk-up.
|
|
9
|
+
* Running `poetry show` from a parent directory returns the parent's deps, not
|
|
10
|
+
* the sub-package's, so walk-up would produce incorrect SBOMs.
|
|
11
|
+
* @param {string} manifestDir
|
|
12
|
+
* @param {Object} [opts={}]
|
|
13
|
+
* @returns {string|null}
|
|
14
|
+
* @protected
|
|
15
|
+
*/
|
|
16
|
+
_findLockFileDir(manifestDir, opts = {}) {
|
|
17
|
+
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
|
|
18
|
+
if (workspaceDir) {
|
|
19
|
+
const dir = path.resolve(workspaceDir);
|
|
20
|
+
return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
|
|
21
|
+
}
|
|
22
|
+
const dir = path.resolve(manifestDir);
|
|
23
|
+
return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
|
|
24
|
+
}
|
|
25
|
+
/** @returns {string} */
|
|
26
|
+
_lockFileName() {
|
|
27
|
+
return 'poetry.lock';
|
|
28
|
+
}
|
|
29
|
+
/** @returns {string} */
|
|
30
|
+
_cmdName() {
|
|
31
|
+
return 'poetry';
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @param {string} manifestDir
|
|
35
|
+
* @param {string} _workspaceDir - unused (poetry has no workspace support)
|
|
36
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
37
|
+
* @param {Object} opts
|
|
38
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
39
|
+
*/
|
|
40
|
+
// eslint-disable-next-line no-unused-vars
|
|
41
|
+
async _getDependencyData(manifestDir, _workspaceDir, parsed, opts) {
|
|
42
|
+
let treeOutput = this._getPoetryShowTreeOutput(manifestDir, opts);
|
|
43
|
+
let showAllOutput = this._getPoetryShowAllOutput(manifestDir, opts);
|
|
44
|
+
let versionMap = this._parsePoetryShowAll(showAllOutput);
|
|
45
|
+
return this._parsePoetryTree(treeOutput, versionMap);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get poetry show --tree output.
|
|
49
|
+
* @param {string} manifestDir
|
|
50
|
+
* @param {Object} opts
|
|
51
|
+
* @returns {string}
|
|
52
|
+
*/
|
|
53
|
+
_getPoetryShowTreeOutput(manifestDir, opts) {
|
|
54
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_TREE')) {
|
|
55
|
+
return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_TREE'], 'base64').toString('utf-8');
|
|
56
|
+
}
|
|
57
|
+
let poetryBin = getCustomPath('poetry', opts);
|
|
58
|
+
return invokeCommand(poetryBin, ['show', '--tree', '--no-ansi'], { cwd: manifestDir }).toString();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get poetry show --all output (flat list with resolved versions).
|
|
62
|
+
* @param {string} manifestDir
|
|
63
|
+
* @param {Object} opts
|
|
64
|
+
* @returns {string}
|
|
65
|
+
*/
|
|
66
|
+
_getPoetryShowAllOutput(manifestDir, opts) {
|
|
67
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_ALL')) {
|
|
68
|
+
return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_ALL'], 'base64').toString('utf-8');
|
|
69
|
+
}
|
|
70
|
+
let poetryBin = getCustomPath('poetry', opts);
|
|
71
|
+
return invokeCommand(poetryBin, ['show', '--no-ansi', '--all'], { cwd: manifestDir }).toString();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Parse poetry show --all output into a version map.
|
|
75
|
+
* Lines look like: "name (!) 1.2.3 Description text..."
|
|
76
|
+
* or: "name 1.2.3 Description text..."
|
|
77
|
+
* @param {string} output
|
|
78
|
+
* @returns {Map<string, string>} canonical name -> version
|
|
79
|
+
*/
|
|
80
|
+
_parsePoetryShowAll(output) {
|
|
81
|
+
let versions = new Map();
|
|
82
|
+
let lines = output.split(/\r?\n/);
|
|
83
|
+
for (let line of lines) {
|
|
84
|
+
let trimmed = line.trim();
|
|
85
|
+
if (!trimmed) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
let match = trimmed.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(?:\(!\)\s+)?(\S+)/);
|
|
89
|
+
if (match) {
|
|
90
|
+
versions.set(this._canonicalize(match[1]), match[2]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return versions;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Parse poetry show --tree output into a dependency graph structure.
|
|
97
|
+
* Top-level lines (no indentation/tree chars) are direct deps: "name version description"
|
|
98
|
+
* Indented lines are transitive deps with tree chars: "├── name >=constraint"
|
|
99
|
+
*
|
|
100
|
+
* @param {string} treeOutput
|
|
101
|
+
* @param {Map<string, string>} versionMap - canonical name -> resolved version
|
|
102
|
+
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
|
|
103
|
+
*/
|
|
104
|
+
_parsePoetryTree(treeOutput, versionMap) {
|
|
105
|
+
let lines = treeOutput.split(/\r?\n/);
|
|
106
|
+
let graph = new Map();
|
|
107
|
+
let directDeps = [];
|
|
108
|
+
let stack = []; // [{key, depth}]
|
|
109
|
+
let currentDirectDep = null;
|
|
110
|
+
for (let line of lines) {
|
|
111
|
+
if (!line.trim()) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
// top-level line: "name version description..."
|
|
115
|
+
let topMatch = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(\S+)(?:\s|$)/);
|
|
116
|
+
if (topMatch) {
|
|
117
|
+
let name = topMatch[1];
|
|
118
|
+
let version = topMatch[2];
|
|
119
|
+
let key = this._canonicalize(name);
|
|
120
|
+
directDeps.push(key);
|
|
121
|
+
if (!graph.has(key)) {
|
|
122
|
+
graph.set(key, { name, version, children: [] });
|
|
123
|
+
}
|
|
124
|
+
currentDirectDep = key;
|
|
125
|
+
stack = [{ key, depth: -1 }];
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (!currentDirectDep) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
// indented line with tree chars (UTF-8 box-drawing: ├── └── │)
|
|
132
|
+
let nameStart = line.search(/[A-Za-z0-9]/);
|
|
133
|
+
if (nameStart < 0) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
let rest = line.substring(nameStart);
|
|
137
|
+
let depMatch = rest.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)/);
|
|
138
|
+
if (!depMatch) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
let depName = depMatch[1];
|
|
142
|
+
let depKey = this._canonicalize(depName);
|
|
143
|
+
// determine depth by counting tree-drawing groups in the prefix
|
|
144
|
+
let prefix = line.substring(0, nameStart);
|
|
145
|
+
let depth = (prefix.match(/(?:[├└│ ][\s─]{2} ?)/g) || []).length;
|
|
146
|
+
// resolve version from the version map
|
|
147
|
+
let version = versionMap.get(depKey) || null;
|
|
148
|
+
if (!version) {
|
|
149
|
+
throw new Error(`poetry: package '${depName}' has no resolved version`);
|
|
150
|
+
}
|
|
151
|
+
if (!graph.has(depKey)) {
|
|
152
|
+
graph.set(depKey, { name: depName, version, children: [] });
|
|
153
|
+
}
|
|
154
|
+
// pop stack back to find the parent at depth-1
|
|
155
|
+
while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
|
|
156
|
+
stack.pop();
|
|
157
|
+
}
|
|
158
|
+
if (stack.length > 0) {
|
|
159
|
+
let parentKey = stack[stack.length - 1].key;
|
|
160
|
+
let parentEntry = graph.get(parentKey);
|
|
161
|
+
if (parentEntry && !parentEntry.children.includes(depKey)) {
|
|
162
|
+
parentEntry.children.push(depKey);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
stack.push({ key: depKey, depth });
|
|
166
|
+
}
|
|
167
|
+
return { directDeps, graph };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
16
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
17
|
+
*/
|
|
18
|
+
_parseUvExport(output: string, projectName: string, workspaceDir: string): Promise<{
|
|
19
|
+
directDeps: string[];
|
|
20
|
+
graph: Map<string, {
|
|
21
|
+
name: string;
|
|
22
|
+
version: string;
|
|
23
|
+
children: string[];
|
|
24
|
+
}>;
|
|
25
|
+
}>;
|
|
26
|
+
}
|
|
27
|
+
import Base_pyproject from './base_pyproject.js';
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parse as parseToml } from 'smol-toml';
|
|
4
|
+
import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
|
|
5
|
+
import Base_pyproject from './base_pyproject.js';
|
|
6
|
+
import { getParser, getPinnedVersionQuery } from './requirements_parser.js';
|
|
7
|
+
export default class Python_uv extends Base_pyproject {
|
|
8
|
+
/** @returns {string} */
|
|
9
|
+
_lockFileName() {
|
|
10
|
+
return 'uv.lock';
|
|
11
|
+
}
|
|
12
|
+
/** @returns {string} */
|
|
13
|
+
_cmdName() {
|
|
14
|
+
return 'uv';
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} manifestDir - directory containing the target pyproject.toml
|
|
18
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
19
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
20
|
+
* @param {Object} opts
|
|
21
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
22
|
+
*/
|
|
23
|
+
async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
|
|
24
|
+
let projectName = this._getProjectName(parsed);
|
|
25
|
+
let uvOutput = this._getUvExportOutput(manifestDir, opts);
|
|
26
|
+
return this._parseUvExport(uvOutput, projectName, workspaceDir);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get the uv export output, either from env var or by running the command.
|
|
30
|
+
* @param {string} manifestDir
|
|
31
|
+
* @param {Object} opts
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
_getUvExportOutput(manifestDir, opts) {
|
|
35
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_UV_EXPORT')) {
|
|
36
|
+
return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii');
|
|
37
|
+
}
|
|
38
|
+
let uvBin = getCustomPath('uv', opts);
|
|
39
|
+
return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes'], { cwd: manifestDir }).toString();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Parse uv export output into a dependency graph using tree-sitter-requirements
|
|
43
|
+
* for package/version extraction and string parsing for "# via" comments.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} output
|
|
46
|
+
* @param {string} projectName - canonical project name to identify direct deps
|
|
47
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
48
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
49
|
+
*/
|
|
50
|
+
async _parseUvExport(output, projectName, workspaceDir) {
|
|
51
|
+
let [parser, pinnedVersionQuery] = await Promise.all([
|
|
52
|
+
getParser(), getPinnedVersionQuery()
|
|
53
|
+
]);
|
|
54
|
+
let tree = parser.parse(output);
|
|
55
|
+
let root = tree.rootNode;
|
|
56
|
+
let canonProjectName = this._canonicalize(projectName);
|
|
57
|
+
let packages = new Map(); // canonical name -> {name, version, parents: Set}
|
|
58
|
+
let currentPkg = null;
|
|
59
|
+
let collectingVia = false;
|
|
60
|
+
for (let child of root.children) {
|
|
61
|
+
if (child.type === 'global_opt') {
|
|
62
|
+
let optNode = child.children.find(c => c.type === 'option');
|
|
63
|
+
let pathNode = child.children.find(c => c.type === 'path');
|
|
64
|
+
if (optNode?.text === '-e' && pathNode && workspaceDir) {
|
|
65
|
+
let memberDir = path.resolve(workspaceDir, pathNode.text);
|
|
66
|
+
let memberManifest = path.join(memberDir, 'pyproject.toml');
|
|
67
|
+
if (fs.existsSync(memberManifest)) {
|
|
68
|
+
let memberParsed = parseToml(fs.readFileSync(memberManifest, 'utf-8'));
|
|
69
|
+
let name = memberParsed.project?.name || memberParsed.tool?.poetry?.name;
|
|
70
|
+
let version = memberParsed.project?.version || memberParsed.tool?.poetry?.version;
|
|
71
|
+
if (name && version) {
|
|
72
|
+
let key = this._canonicalize(name);
|
|
73
|
+
currentPkg = { name, version, parents: new Set() };
|
|
74
|
+
packages.set(key, currentPkg);
|
|
75
|
+
collectingVia = false;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
currentPkg = null;
|
|
81
|
+
collectingVia = false;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (child.type === 'requirement') {
|
|
85
|
+
let nameNode = child.children.find(c => c.type === 'package');
|
|
86
|
+
if (!nameNode) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
let name = nameNode.text;
|
|
90
|
+
let version = null;
|
|
91
|
+
let versionMatches = pinnedVersionQuery.matches(child);
|
|
92
|
+
if (versionMatches.length > 0) {
|
|
93
|
+
version = versionMatches[0].captures.find(c => c.name === 'version').node.text;
|
|
94
|
+
}
|
|
95
|
+
if (!version) {
|
|
96
|
+
throw new Error(`uv export: package '${name}' has no pinned version`);
|
|
97
|
+
}
|
|
98
|
+
let key = this._canonicalize(name);
|
|
99
|
+
currentPkg = { name, version, parents: new Set() };
|
|
100
|
+
packages.set(key, currentPkg);
|
|
101
|
+
collectingVia = false;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (child.type === 'comment' && currentPkg) {
|
|
105
|
+
let text = child.text.trim();
|
|
106
|
+
let viaSingle = text.match(/^# via ([A-Za-z0-9][A-Za-z0-9._-]*)$/);
|
|
107
|
+
if (viaSingle) {
|
|
108
|
+
currentPkg.parents.add(this._canonicalize(viaSingle[1]));
|
|
109
|
+
collectingVia = false;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (text === '# via') {
|
|
113
|
+
collectingVia = true;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (collectingVia) {
|
|
117
|
+
let parentMatch = text.match(/^#\s+([A-Za-z0-9][A-Za-z0-9._-]*)$/);
|
|
118
|
+
if (parentMatch) {
|
|
119
|
+
currentPkg.parents.add(this._canonicalize(parentMatch[1]));
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
collectingVia = false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// Build forward dependency map and extract direct deps in one pass
|
|
127
|
+
let graph = new Map();
|
|
128
|
+
let directDeps = [];
|
|
129
|
+
for (let [key, pkg] of packages) {
|
|
130
|
+
graph.set(key, { name: pkg.name, version: pkg.version, children: [] });
|
|
131
|
+
}
|
|
132
|
+
for (let [childKey, pkg] of packages) {
|
|
133
|
+
for (let parentKey of pkg.parents) {
|
|
134
|
+
if (parentKey === canonProjectName) {
|
|
135
|
+
directDeps.push(childKey);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
let parentEntry = graph.get(parentKey);
|
|
139
|
+
if (parentEntry) {
|
|
140
|
+
parentEntry.children.push(childKey);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { directDeps, graph };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { Language, Parser, Query } from 'web-tree-sitter';
|
|
3
|
-
const
|
|
3
|
+
const wasmUrl = new URL('./tree-sitter-requirements.wasm', import.meta.url);
|
|
4
4
|
async function init() {
|
|
5
|
-
await Parser.init(
|
|
6
|
-
|
|
7
|
-
|
|
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;
|