@trustify-da/trustify-da-javascript-client 0.3.0-ea.6549d2a → 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 (37) hide show
  1. package/README.md +107 -4
  2. package/dist/package.json +9 -1
  3. package/dist/src/analysis.d.ts +16 -0
  4. package/dist/src/analysis.js +50 -2
  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 +121 -3
  8. package/dist/src/cyclone_dx_sbom.js +2 -1
  9. package/dist/src/index.d.ts +63 -2
  10. package/dist/src/index.js +266 -5
  11. package/dist/src/license/index.d.ts +2 -2
  12. package/dist/src/license/index.js +4 -4
  13. package/dist/src/license/license_utils.d.ts +40 -0
  14. package/dist/src/license/license_utils.js +134 -0
  15. package/dist/src/license/licenses_api.js +9 -2
  16. package/dist/src/license/project_license.d.ts +1 -6
  17. package/dist/src/license/project_license.js +4 -81
  18. package/dist/src/provider.d.ts +6 -3
  19. package/dist/src/provider.js +8 -5
  20. package/dist/src/providers/base_javascript.d.ts +9 -3
  21. package/dist/src/providers/base_javascript.js +25 -15
  22. package/dist/src/providers/golang_gomodules.js +2 -1
  23. package/dist/src/providers/java_gradle.js +2 -1
  24. package/dist/src/providers/java_maven.d.ts +1 -1
  25. package/dist/src/providers/java_maven.js +10 -9
  26. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  27. package/dist/src/providers/javascript_pnpm.js +2 -2
  28. package/dist/src/providers/python_pip.js +2 -1
  29. package/dist/src/providers/requirements_parser.js +4 -3
  30. package/dist/src/providers/rust_cargo.d.ts +52 -0
  31. package/dist/src/providers/rust_cargo.js +614 -0
  32. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  33. package/dist/src/workspace.d.ts +61 -0
  34. package/dist/src/workspace.js +256 -0
  35. package/package.json +10 -2
  36. package/dist/src/license/compatibility.d.ts +0 -18
  37. package/dist/src/license/compatibility.js +0 -45
@@ -0,0 +1,256 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import fg from 'fast-glob';
4
+ import { load as yamlLoad } from 'js-yaml';
5
+ import micromatch from 'micromatch';
6
+ import { getCustom, getCustomPath, invokeCommand } from './tools.js';
7
+ /** Default paths skipped during JS workspace discovery (merged with user patterns). */
8
+ const DEFAULT_WORKSPACE_DISCOVERY_IGNORE = [
9
+ '**/node_modules/**',
10
+ '**/.git/**',
11
+ ];
12
+ /**
13
+ * Resolve ignore globs for workspace discovery: defaults + `TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE` + `opts.workspaceDiscoveryIgnore`.
14
+ * Patterns are fast-glob / micromatch style, relative to the workspace root (forward slashes).
15
+ *
16
+ * @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}]
17
+ * @returns {string[]}
18
+ */
19
+ export function resolveWorkspaceDiscoveryIgnore(opts = {}) {
20
+ const merged = [...DEFAULT_WORKSPACE_DISCOVERY_IGNORE];
21
+ const fromEnv = getCustom('TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE', null, opts);
22
+ if (fromEnv && String(fromEnv).trim()) {
23
+ merged.push(...String(fromEnv).split(',').map(s => s.trim()).filter(Boolean));
24
+ }
25
+ const extra = opts.workspaceDiscoveryIgnore;
26
+ if (Array.isArray(extra)) {
27
+ for (const p of extra) {
28
+ if (typeof p === 'string' && p.trim()) {
29
+ merged.push(p.trim());
30
+ }
31
+ }
32
+ }
33
+ return [...new Set(merged)];
34
+ }
35
+ /**
36
+ * @param {string} root - Workspace root (absolute)
37
+ * @param {string[]} ignorePatterns
38
+ */
39
+ function buildWorkspaceDiscoveryGlobOptions(root, ignorePatterns) {
40
+ return {
41
+ cwd: root,
42
+ absolute: true,
43
+ onlyFiles: true,
44
+ ignore: ignorePatterns,
45
+ followSymbolicLinks: false,
46
+ };
47
+ }
48
+ /**
49
+ * @param {string} filePath - Absolute path
50
+ * @param {string} root - Workspace root (absolute)
51
+ * @returns {string} Relative path with forward slashes
52
+ */
53
+ function relativePathForGlobMatch(filePath, root) {
54
+ const rel = path.relative(root, filePath);
55
+ return rel.split(path.sep).join('/');
56
+ }
57
+ /**
58
+ * Drop manifest paths whose location matches an ignore pattern (e.g. root unshift, Cargo paths).
59
+ *
60
+ * @param {string[]} manifestPaths
61
+ * @param {string} root
62
+ * @param {string[]} ignorePatterns
63
+ * @returns {string[]}
64
+ */
65
+ export function filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns) {
66
+ if (!ignorePatterns.length) {
67
+ return manifestPaths;
68
+ }
69
+ const resolvedRoot = path.resolve(root);
70
+ return manifestPaths.filter(absPath => {
71
+ const rel = relativePathForGlobMatch(absPath, resolvedRoot);
72
+ if (rel === '') {
73
+ return true;
74
+ }
75
+ return !ignorePatterns.some(pattern => micromatch.isMatch(rel, pattern, { dot: true }));
76
+ });
77
+ }
78
+ /**
79
+ * @typedef {{ valid: true, name: string, version: string } | { valid: false, error: string }} ValidatePackageJsonResult
80
+ */
81
+ /**
82
+ * Validate a package.json has non-empty `name` and `version` (required for stable SBOM root identity in batch).
83
+ *
84
+ * @param {string} packageJsonPath - Absolute or relative path to package.json
85
+ * @returns {ValidatePackageJsonResult}
86
+ */
87
+ export function validatePackageJson(packageJsonPath) {
88
+ let content;
89
+ try {
90
+ const raw = fs.readFileSync(packageJsonPath, 'utf-8');
91
+ content = JSON.parse(raw);
92
+ }
93
+ catch (err) {
94
+ const msg = err instanceof Error ? err.message : String(err);
95
+ return { valid: false, error: `Invalid package.json: ${msg}` };
96
+ }
97
+ if (!content || typeof content !== 'object') {
98
+ return { valid: false, error: 'package.json must be a JSON object' };
99
+ }
100
+ const name = content.name;
101
+ const version = content.version;
102
+ if (typeof name !== 'string' || !name.trim()) {
103
+ return { valid: false, error: 'Missing or invalid name' };
104
+ }
105
+ if (typeof version !== 'string' || !version.trim()) {
106
+ return { valid: false, error: 'Missing or invalid version' };
107
+ }
108
+ return { valid: true, name: name.trim(), version: version.trim() };
109
+ }
110
+ /**
111
+ * Discover all package.json paths in a JS/TS workspace.
112
+ * Reads pnpm-workspace.yaml or package.json workspaces.
113
+ *
114
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root
115
+ * @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}] - optional `workspaceDiscoveryIgnore` globs (merged with defaults and env)
116
+ * @returns {Promise<string[]>} Paths to package.json files (absolute)
117
+ */
118
+ export async function discoverWorkspacePackages(workspaceRoot, opts = {}) {
119
+ const root = path.resolve(workspaceRoot);
120
+ const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts);
121
+ const globOpts = buildWorkspaceDiscoveryGlobOptions(root, ignorePatterns);
122
+ const pnpmWorkspace = path.join(root, 'pnpm-workspace.yaml');
123
+ const packageJson = path.join(root, 'package.json');
124
+ if (fs.existsSync(pnpmWorkspace)) {
125
+ return discoverFromPnpmWorkspace(root, pnpmWorkspace, globOpts, ignorePatterns);
126
+ }
127
+ if (fs.existsSync(packageJson)) {
128
+ return discoverFromPackageJsonWorkspaces(root, packageJson, globOpts, ignorePatterns);
129
+ }
130
+ return [];
131
+ }
132
+ /**
133
+ * @param {string} root
134
+ * @param {string} pnpmWorkspacePath
135
+ * @param {object} globOpts - fast-glob options (cwd, ignore, followSymbolicLinks, …)
136
+ * @param {string[]} ignorePatterns - for post-filter
137
+ * @returns {Promise<string[]>}
138
+ */
139
+ async function discoverFromPnpmWorkspace(root, pnpmWorkspacePath, globOpts, ignorePatterns) {
140
+ const content = fs.readFileSync(pnpmWorkspacePath, 'utf-8');
141
+ const packages = parsePnpmPackages(content);
142
+ if (packages.length === 0) {
143
+ return [];
144
+ }
145
+ const patterns = toManifestGlobPatterns(packages, 'package.json');
146
+ const manifestPaths = await fg(patterns, globOpts);
147
+ return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
148
+ }
149
+ /**
150
+ * Parse the `packages` array from pnpm-workspace.yaml content.
151
+ * @param {string} content - Raw YAML content
152
+ * @returns {string[]}
153
+ */
154
+ function parsePnpmPackages(content) {
155
+ let doc;
156
+ try {
157
+ doc = yamlLoad(content);
158
+ }
159
+ catch {
160
+ return [];
161
+ }
162
+ if (!doc || typeof doc !== 'object' || !Array.isArray(doc.packages)) {
163
+ return [];
164
+ }
165
+ return doc.packages.filter(p => typeof p === 'string' && p.trim()).map(p => p.trim());
166
+ }
167
+ /**
168
+ * Convert workspace glob patterns to manifest-file glob patterns,
169
+ * correctly handling negation prefixes.
170
+ *
171
+ * @param {string[]} patterns - Workspace glob patterns (may include negations)
172
+ * @param {string} manifestFileName - e.g. 'package.json' or 'Cargo.toml'
173
+ * @returns {string[]}
174
+ */
175
+ function toManifestGlobPatterns(patterns, manifestFileName) {
176
+ return patterns.map(p => {
177
+ if (p.startsWith('!')) {
178
+ return `!${p.slice(1)}/${manifestFileName}`;
179
+ }
180
+ return `${p}/${manifestFileName}`;
181
+ });
182
+ }
183
+ /**
184
+ * @param {string} root
185
+ * @param {string} packageJsonPath
186
+ * @param {object} globOpts
187
+ * @param {string[]} ignorePatterns
188
+ * @returns {Promise<string[]>}
189
+ */
190
+ async function discoverFromPackageJsonWorkspaces(root, packageJsonPath, globOpts, ignorePatterns) {
191
+ let pkg;
192
+ try {
193
+ pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
194
+ }
195
+ catch {
196
+ return [];
197
+ }
198
+ const workspaces = pkg.workspaces;
199
+ if (!workspaces) {
200
+ return [];
201
+ }
202
+ const raw = Array.isArray(workspaces) ? workspaces : workspaces.packages || [];
203
+ const patterns = toManifestGlobPatterns(raw.filter(p => typeof p === 'string'), 'package.json');
204
+ if (patterns.length === 0) {
205
+ return [];
206
+ }
207
+ const manifestPaths = await fg(patterns, globOpts);
208
+ const rootPkg = path.join(root, 'package.json');
209
+ if (fs.existsSync(rootPkg) && !manifestPaths.includes(rootPkg)) {
210
+ manifestPaths.unshift(rootPkg);
211
+ }
212
+ return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
213
+ }
214
+ /**
215
+ * Discover all Cargo.toml manifest paths in a Cargo workspace.
216
+ * Uses `cargo metadata` to get workspace members.
217
+ *
218
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain Cargo.toml and Cargo.lock)
219
+ * @param {import('./index.js').Options} [opts={}]
220
+ * @returns {Promise<string[]>} Paths to Cargo.toml files (absolute)
221
+ */
222
+ export async function discoverWorkspaceCrates(workspaceRoot, opts = {}) {
223
+ const root = path.resolve(workspaceRoot);
224
+ const cargoToml = path.join(root, 'Cargo.toml');
225
+ const cargoLock = path.join(root, 'Cargo.lock');
226
+ if (!fs.existsSync(cargoToml) || !fs.existsSync(cargoLock)) {
227
+ return [];
228
+ }
229
+ const cargoBin = getCustomPath('cargo', opts);
230
+ let output;
231
+ try {
232
+ output = invokeCommand(cargoBin, ['metadata', '--format-version', '1', '--no-deps'], { cwd: root });
233
+ }
234
+ catch {
235
+ return [];
236
+ }
237
+ let metadata;
238
+ try {
239
+ metadata = JSON.parse(output.toString().trim());
240
+ }
241
+ catch {
242
+ return [];
243
+ }
244
+ const memberIds = new Set(metadata.workspace_members || []);
245
+ const manifestPaths = [];
246
+ for (const pkg of metadata.packages || []) {
247
+ if (memberIds.has(pkg.id) && pkg.manifest_path) {
248
+ const manifestPath = path.resolve(pkg.manifest_path);
249
+ if (fs.existsSync(manifestPath)) {
250
+ manifestPaths.push(manifestPath);
251
+ }
252
+ }
253
+ }
254
+ const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts);
255
+ return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
256
+ }
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.6549d2a",
3
+ "version": "0.3.0-ea.7144952",
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",
@@ -40,19 +40,27 @@
40
40
  "test": "c8 npm run tests",
41
41
  "tests": "mocha --config .mocharc.json --grep \".*analysis module.*\" --invert",
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
44
  "precompile": "rm -rf dist",
44
- "compile": "tsc -p tsconfig.json"
45
+ "compile": "tsc -p tsconfig.json",
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"
45
48
  },
46
49
  "dependencies": {
47
50
  "@babel/core": "^7.23.2",
48
51
  "@cyclonedx/cyclonedx-library": "^6.13.0",
49
52
  "eslint-import-resolver-typescript": "^4.4.4",
53
+ "fast-glob": "^3.3.3",
50
54
  "fast-toml": "^0.5.4",
51
55
  "fast-xml-parser": "^5.3.4",
52
56
  "help": "^3.0.2",
53
57
  "https-proxy-agent": "^7.0.6",
58
+ "js-yaml": "^4.1.1",
59
+ "micromatch": "^4.0.8",
54
60
  "node-fetch": "^3.3.2",
61
+ "p-limit": "^5.0.0",
55
62
  "packageurl-js": "~1.0.2",
63
+ "smol-toml": "^1.6.0",
56
64
  "tree-sitter-requirements": "github:Strum355/tree-sitter-requirements#d0261ee76b84253997fe70d7d397e78c006c3801",
57
65
  "web-tree-sitter": "^0.26.6",
58
66
  "yargs": "^18.0.0"
@@ -1,18 +0,0 @@
1
- /**
2
- * License compatibility: whether a dependency license is compatible with the project license.
3
- * Relies on backend-provided license categories.
4
- *
5
- * Compatibility is based on restrictiveness hierarchy:
6
- * PERMISSIVE < WEAK_COPYLEFT < STRONG_COPYLEFT
7
- *
8
- * A dependency is compatible if it's equal or less restrictive than the project license.
9
- * A dependency is incompatible if it's more restrictive than the project license.
10
- */
11
- /**
12
- * Check if a dependency's license is compatible with the project license based on backend categories.
13
- *
14
- * @param {string} [projectCategory] - backend category for project license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
15
- * @param {string} [dependencyCategory] - backend category for dependency license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
16
- * @returns {'compatible'|'incompatible'|'unknown'}
17
- */
18
- export function getCompatibility(projectCategory?: string, dependencyCategory?: string): "compatible" | "incompatible" | "unknown";
@@ -1,45 +0,0 @@
1
- /**
2
- * License compatibility: whether a dependency license is compatible with the project license.
3
- * Relies on backend-provided license categories.
4
- *
5
- * Compatibility is based on restrictiveness hierarchy:
6
- * PERMISSIVE < WEAK_COPYLEFT < STRONG_COPYLEFT
7
- *
8
- * A dependency is compatible if it's equal or less restrictive than the project license.
9
- * A dependency is incompatible if it's more restrictive than the project license.
10
- */
11
- /**
12
- * Check if a dependency's license is compatible with the project license based on backend categories.
13
- *
14
- * @param {string} [projectCategory] - backend category for project license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
15
- * @param {string} [dependencyCategory] - backend category for dependency license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
16
- * @returns {'compatible'|'incompatible'|'unknown'}
17
- */
18
- export function getCompatibility(projectCategory, dependencyCategory) {
19
- if (!projectCategory || !dependencyCategory) {
20
- return 'unknown';
21
- }
22
- const proj = projectCategory.toUpperCase();
23
- const dep = dependencyCategory.toUpperCase();
24
- // Unknown categories
25
- if (proj === 'UNKNOWN' || dep === 'UNKNOWN') {
26
- return 'unknown';
27
- }
28
- // Define restrictiveness levels (higher number = more restrictive)
29
- const restrictiveness = {
30
- 'PERMISSIVE': 1,
31
- 'WEAK_COPYLEFT': 2,
32
- 'STRONG_COPYLEFT': 3
33
- };
34
- const projLevel = restrictiveness[proj];
35
- const depLevel = restrictiveness[dep];
36
- if (projLevel === undefined || depLevel === undefined) {
37
- return 'unknown';
38
- }
39
- // Dependency is more restrictive than project → incompatible
40
- if (depLevel > projLevel) {
41
- return 'incompatible';
42
- }
43
- // Dependency is equal or less restrictive → compatible
44
- return 'compatible';
45
- }