@trustify-da/trustify-da-javascript-client 0.3.0-ea.b40d888 → 0.3.0-ea.c2f6c64
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 +23 -10
- package/dist/src/analysis.d.ts +21 -5
- package/dist/src/analysis.js +74 -80
- 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 -2
- package/dist/src/cyclone_dx_sbom.js +32 -5
- package/dist/src/index.d.ts +138 -11
- package/dist/src/index.js +288 -7
- 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/images.d.ts +4 -5
- package/dist/src/oci_image/utils.d.ts +4 -4
- package/dist/src/oci_image/utils.js +11 -2
- package/dist/src/provider.d.ts +17 -5
- package/dist/src/provider.js +27 -5
- package/dist/src/providers/base_java.d.ts +3 -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 +147 -0
- package/dist/src/providers/base_pyproject.js +279 -0
- package/dist/src/providers/golang_gomodules.d.ts +20 -13
- 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 +9 -3
- package/dist/src/providers/java_gradle.js +12 -2
- package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
- package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
- package/dist/src/providers/java_maven.d.ts +12 -5
- package/dist/src/providers/java_maven.js +33 -5
- 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_controller.d.ts +5 -2
- package/dist/src/providers/python_controller.js +56 -58
- package/dist/src/providers/python_pip.d.ts +11 -4
- package/dist/src/providers/python_pip.js +47 -54
- package/dist/src/providers/python_poetry.d.ts +42 -0
- package/dist/src/providers/python_poetry.js +146 -0
- package/dist/src/providers/python_uv.d.ts +26 -0
- package/dist/src/providers/python_uv.js +118 -0
- package/dist/src/providers/requirements_parser.d.ts +6 -0
- package/dist/src/providers/requirements_parser.js +24 -0
- 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 +22 -6
- package/dist/src/tools.js +56 -1
- package/dist/src/workspace.d.ts +61 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +24 -11
package/dist/src/provider.js
CHANGED
|
@@ -7,8 +7,11 @@ import Javascript_npm from './providers/javascript_npm.js';
|
|
|
7
7
|
import Javascript_pnpm from './providers/javascript_pnpm.js';
|
|
8
8
|
import Javascript_yarn from './providers/javascript_yarn.js';
|
|
9
9
|
import pythonPipProvider from './providers/python_pip.js';
|
|
10
|
+
import Python_poetry from './providers/python_poetry.js';
|
|
11
|
+
import Python_uv from './providers/python_uv.js';
|
|
12
|
+
import rustCargoProvider from './providers/rust_cargo.js';
|
|
10
13
|
/** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
|
|
11
|
-
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided
|
|
14
|
+
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string, Object): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
|
|
12
15
|
/**
|
|
13
16
|
* MUST include all providers here.
|
|
14
17
|
* @type {[Provider]}
|
|
@@ -17,12 +20,30 @@ export const availableProviders = [
|
|
|
17
20
|
new Java_maven(),
|
|
18
21
|
new Java_gradle_groovy(),
|
|
19
22
|
new Java_gradle_kotlin(),
|
|
20
|
-
new Javascript_npm(),
|
|
21
23
|
new Javascript_pnpm(),
|
|
22
24
|
new Javascript_yarn(),
|
|
25
|
+
new Javascript_npm(),
|
|
23
26
|
golangGomodulesProvider,
|
|
24
|
-
pythonPipProvider
|
|
27
|
+
pythonPipProvider,
|
|
28
|
+
new Python_poetry(),
|
|
29
|
+
new Python_uv(),
|
|
30
|
+
rustCargoProvider
|
|
25
31
|
];
|
|
32
|
+
/**
|
|
33
|
+
* Match a provider by manifest type only (no lock file check). Used for license reading.
|
|
34
|
+
* @param {string} manifestPath - path or name of the manifest
|
|
35
|
+
* @param {[Provider]} providers - list of providers to iterate over
|
|
36
|
+
* @returns {Provider}
|
|
37
|
+
* @throws {Error} when the manifest is not supported and no provider was matched
|
|
38
|
+
*/
|
|
39
|
+
export function matchForLicense(manifestPath, providers) {
|
|
40
|
+
const base = path.parse(manifestPath).base;
|
|
41
|
+
const provider = providers.find(prov => prov.isSupported(base));
|
|
42
|
+
if (!provider) {
|
|
43
|
+
throw new Error(`${base} is not supported`);
|
|
44
|
+
}
|
|
45
|
+
return provider;
|
|
46
|
+
}
|
|
26
47
|
/**
|
|
27
48
|
* Match a provider from a list or providers based on file type.
|
|
28
49
|
* Each provider MUST export 'isSupported' taking a file name-type and returning true if supported.
|
|
@@ -30,16 +51,17 @@ export const availableProviders = [
|
|
|
30
51
|
* Each provider MUST export 'provideStack' taking manifest path returning a {@link Provided}.
|
|
31
52
|
* @param {string} manifest - the name-type or path of the manifest
|
|
32
53
|
* @param {[Provider]} providers - list of providers to iterate over
|
|
54
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional; TRUSTIFY_DA_WORKSPACE_DIR overrides lock file location for workspaces
|
|
33
55
|
* @returns {Provider}
|
|
34
56
|
* @throws {Error} when the manifest is not supported and no provider was matched
|
|
35
57
|
*/
|
|
36
|
-
export function match(manifest, providers) {
|
|
58
|
+
export function match(manifest, providers, opts = {}) {
|
|
37
59
|
const manifestPath = path.parse(manifest);
|
|
38
60
|
const supported = providers.filter(prov => prov.isSupported(manifestPath.base));
|
|
39
61
|
if (supported.length === 0) {
|
|
40
62
|
throw new Error(`${manifestPath.base} is not supported`);
|
|
41
63
|
}
|
|
42
|
-
const provider = supported.find(prov => prov.validateLockFile(manifestPath.dir));
|
|
64
|
+
const provider = supported.find(prov => prov.validateLockFile(manifestPath.dir, opts));
|
|
43
65
|
if (!provider) {
|
|
44
66
|
throw new Error(`${manifestPath.base} requires a lock file. Use your preferred package manager to generate the lock file.`);
|
|
45
67
|
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/// <reference types="packageurl-js/src/package-url.js" />
|
|
3
|
-
export type ecosystem_maven = import('../provider').Provider;
|
|
1
|
+
export type ecosystem_maven = import("../provider").Provider;
|
|
4
2
|
/** @typedef {import('../provider').Provider} */
|
|
5
3
|
/** @typedef {import('../provider').Provided} Provided */
|
|
6
4
|
/** @typedef {{name: string, version: string}} Package */
|
|
@@ -51,7 +49,7 @@ export default class Base_Java {
|
|
|
51
49
|
* @param {import('child_process').ExecFileOptionsWithStringEncoding} [opts={}]
|
|
52
50
|
* @protected
|
|
53
51
|
*/
|
|
54
|
-
protected _invokeCommand(bin: any, args: any, opts?: import("child_process").ExecFileOptionsWithStringEncoding
|
|
52
|
+
protected _invokeCommand(bin: any, args: any, opts?: import("child_process").ExecFileOptionsWithStringEncoding): string;
|
|
55
53
|
/**
|
|
56
54
|
*
|
|
57
55
|
* @param {string} manifestPath
|
|
@@ -70,7 +68,7 @@ export default class Base_Java {
|
|
|
70
68
|
normalizePath(thePath: any): string;
|
|
71
69
|
#private;
|
|
72
70
|
}
|
|
73
|
-
export type Provided = import(
|
|
71
|
+
export type Provided = import("../provider").Provided;
|
|
74
72
|
export type Package = {
|
|
75
73
|
name: string;
|
|
76
74
|
version: string;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
/** @typedef {import('../provider
|
|
1
|
+
export type purlType = import("../provider").Provider;
|
|
2
|
+
/** @typedef {import('../provider').Provider} */
|
|
3
|
+
/** @typedef {import('../provider').Provided} Provided */
|
|
3
4
|
/**
|
|
4
5
|
* The ecosystem identifier for JavaScript/npm packages
|
|
5
6
|
* @type {string}
|
|
@@ -66,11 +67,26 @@ export default class Base_javascript {
|
|
|
66
67
|
*/
|
|
67
68
|
isSupported(manifestName: string): boolean;
|
|
68
69
|
/**
|
|
69
|
-
*
|
|
70
|
+
* Walks up the directory tree from manifestDir looking for the lock file.
|
|
71
|
+
* Stops when the lock file is found, when a package.json with a "workspaces"
|
|
72
|
+
* field is encountered without a lock file (workspace root boundary), or
|
|
73
|
+
* when the filesystem root is reached.
|
|
74
|
+
*
|
|
75
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is set, checks only that directory (no walk-up).
|
|
76
|
+
*
|
|
77
|
+
* @param {string} manifestDir - The directory to start searching from
|
|
78
|
+
* @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
|
|
79
|
+
* @returns {string|null} The directory containing the lock file, or null
|
|
80
|
+
* @protected
|
|
81
|
+
*/
|
|
82
|
+
protected _isWorkspaceRoot(dir: any): string | null;
|
|
83
|
+
_findLockFileDir(manifestDir: any, opts?: {}): string | null;
|
|
84
|
+
/**
|
|
70
85
|
* @param {string} manifestDir - The base directory where the manifest is located
|
|
86
|
+
* @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
|
|
71
87
|
* @returns {boolean} True if the lock file exists
|
|
72
88
|
*/
|
|
73
|
-
validateLockFile(manifestDir: string): boolean;
|
|
89
|
+
validateLockFile(manifestDir: string, opts?: any): boolean;
|
|
74
90
|
/**
|
|
75
91
|
* Provides content and content type for stack analysis
|
|
76
92
|
* @param {string} manifestPath - The manifest path or name
|
|
@@ -85,13 +101,20 @@ export default class Base_javascript {
|
|
|
85
101
|
* @returns {Provided} The provided data for component analysis
|
|
86
102
|
*/
|
|
87
103
|
provideComponent(manifestPath: string, opts?: any): Provided;
|
|
104
|
+
/**
|
|
105
|
+
* Read license from manifest (package.json). Reused by npm, pnpm, yarn.
|
|
106
|
+
* @param {string} manifestPath - path to package.json
|
|
107
|
+
* @returns {string|null}
|
|
108
|
+
*/
|
|
109
|
+
readLicenseFromManifest(manifestPath: string): string | null;
|
|
88
110
|
/**
|
|
89
111
|
* Builds the dependency tree for the project
|
|
90
112
|
* @param {boolean} includeTransitive - Whether to include transitive dependencies
|
|
113
|
+
* @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root
|
|
91
114
|
* @returns {Object} The dependency tree
|
|
92
115
|
* @protected
|
|
93
116
|
*/
|
|
94
|
-
protected _buildDependencyTree(includeTransitive: boolean): any;
|
|
117
|
+
protected _buildDependencyTree(includeTransitive: boolean, opts?: any): any;
|
|
95
118
|
/**
|
|
96
119
|
* Recursively builds the Sbom from the JSON that npm listing returns
|
|
97
120
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
@@ -121,7 +144,6 @@ export default class Base_javascript {
|
|
|
121
144
|
protected _parseDepTreeOutput(output: string): string;
|
|
122
145
|
#private;
|
|
123
146
|
}
|
|
124
|
-
export type
|
|
125
|
-
export type Provided = import('../provider.js').Provided;
|
|
147
|
+
export type Provided = import("../provider").Provided;
|
|
126
148
|
import Manifest from './manifest.js';
|
|
127
149
|
import Sbom from '../sbom.js';
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import os from "node:os";
|
|
3
3
|
import path from 'node:path';
|
|
4
|
+
import { getLicense } from '../license/license_utils.js';
|
|
4
5
|
import Sbom from '../sbom.js';
|
|
5
|
-
import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from
|
|
6
|
+
import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from '../tools.js';
|
|
6
7
|
import Manifest from './manifest.js';
|
|
7
|
-
/** @typedef {import('../provider
|
|
8
|
-
/** @typedef {import('../provider
|
|
8
|
+
/** @typedef {import('../provider').Provider} */
|
|
9
|
+
/** @typedef {import('../provider').Provided} Provided */
|
|
9
10
|
/**
|
|
10
11
|
* The ecosystem identifier for JavaScript/npm packages
|
|
11
12
|
* @type {string}
|
|
@@ -96,13 +97,63 @@ export default class Base_javascript {
|
|
|
96
97
|
return 'package.json' === manifestName;
|
|
97
98
|
}
|
|
98
99
|
/**
|
|
99
|
-
*
|
|
100
|
+
* Walks up the directory tree from manifestDir looking for the lock file.
|
|
101
|
+
* Stops when the lock file is found, when a package.json with a "workspaces"
|
|
102
|
+
* field is encountered without a lock file (workspace root boundary), or
|
|
103
|
+
* when the filesystem root is reached.
|
|
104
|
+
*
|
|
105
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is set, checks only that directory (no walk-up).
|
|
106
|
+
*
|
|
107
|
+
* @param {string} manifestDir - The directory to start searching from
|
|
108
|
+
* @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
|
|
109
|
+
* @returns {string|null} The directory containing the lock file, or null
|
|
110
|
+
* @protected
|
|
111
|
+
*/
|
|
112
|
+
_isWorkspaceRoot(dir) {
|
|
113
|
+
if (fs.existsSync(path.join(dir, 'pnpm-workspace.yaml'))) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
const pkgJsonPath = path.join(dir, 'package.json');
|
|
117
|
+
if (fs.existsSync(pkgJsonPath)) {
|
|
118
|
+
try {
|
|
119
|
+
const content = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
|
|
120
|
+
if (content.workspaces) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (_) {
|
|
125
|
+
// ignore parse errors
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
_findLockFileDir(manifestDir, opts = {}) {
|
|
131
|
+
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
|
|
132
|
+
if (workspaceDir) {
|
|
133
|
+
const dir = path.resolve(workspaceDir);
|
|
134
|
+
return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
|
|
135
|
+
}
|
|
136
|
+
let dir = path.resolve(manifestDir);
|
|
137
|
+
let parent = dir;
|
|
138
|
+
do {
|
|
139
|
+
dir = parent;
|
|
140
|
+
if (fs.existsSync(path.join(dir, this._lockFileName()))) {
|
|
141
|
+
return dir;
|
|
142
|
+
}
|
|
143
|
+
if (this._isWorkspaceRoot(dir)) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
parent = path.dirname(dir);
|
|
147
|
+
} while (parent !== dir);
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
100
151
|
* @param {string} manifestDir - The base directory where the manifest is located
|
|
152
|
+
* @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
|
|
101
153
|
* @returns {boolean} True if the lock file exists
|
|
102
154
|
*/
|
|
103
|
-
validateLockFile(manifestDir) {
|
|
104
|
-
|
|
105
|
-
return fs.existsSync(lock);
|
|
155
|
+
validateLockFile(manifestDir, opts = {}) {
|
|
156
|
+
return this._findLockFileDir(manifestDir, opts) !== null;
|
|
106
157
|
}
|
|
107
158
|
/**
|
|
108
159
|
* Provides content and content type for stack analysis
|
|
@@ -132,17 +183,42 @@ export default class Base_javascript {
|
|
|
132
183
|
contentType: 'application/vnd.cyclonedx+json'
|
|
133
184
|
};
|
|
134
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Read license from manifest (package.json). Reused by npm, pnpm, yarn.
|
|
188
|
+
* @param {string} manifestPath - path to package.json
|
|
189
|
+
* @returns {string|null}
|
|
190
|
+
*/
|
|
191
|
+
readLicenseFromManifest(manifestPath) {
|
|
192
|
+
let manifestLicense;
|
|
193
|
+
try {
|
|
194
|
+
const content = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
|
|
195
|
+
if (typeof content.license === 'string') {
|
|
196
|
+
manifestLicense = content.license.trim() || null;
|
|
197
|
+
}
|
|
198
|
+
else if (Array.isArray(content.licenses) && content.licenses.length > 0) {
|
|
199
|
+
const first = content.licenses[0];
|
|
200
|
+
const name = first.type || first.name;
|
|
201
|
+
manifestLicense = (typeof name === 'string' ? name.trim() : null);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
manifestLicense = null;
|
|
206
|
+
}
|
|
207
|
+
return getLicense(manifestLicense, manifestPath);
|
|
208
|
+
}
|
|
135
209
|
/**
|
|
136
210
|
* Builds the dependency tree for the project
|
|
137
211
|
* @param {boolean} includeTransitive - Whether to include transitive dependencies
|
|
212
|
+
* @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root
|
|
138
213
|
* @returns {Object} The dependency tree
|
|
139
214
|
* @protected
|
|
140
215
|
*/
|
|
141
|
-
_buildDependencyTree(includeTransitive) {
|
|
216
|
+
_buildDependencyTree(includeTransitive, opts = {}) {
|
|
142
217
|
this._version();
|
|
143
|
-
|
|
144
|
-
this
|
|
145
|
-
|
|
218
|
+
const manifestDir = path.dirname(this.#manifest.manifestPath);
|
|
219
|
+
const cmdDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
|
|
220
|
+
this.#createLockFile(cmdDir);
|
|
221
|
+
let output = this.#executeListCmd(includeTransitive, cmdDir);
|
|
146
222
|
output = this._parseDepTreeOutput(output);
|
|
147
223
|
return JSON.parse(output);
|
|
148
224
|
}
|
|
@@ -153,14 +229,38 @@ export default class Base_javascript {
|
|
|
153
229
|
* @private
|
|
154
230
|
*/
|
|
155
231
|
#getSBOM(opts = {}) {
|
|
156
|
-
const depsObject = this._buildDependencyTree(true);
|
|
232
|
+
const depsObject = this._buildDependencyTree(true, opts);
|
|
157
233
|
let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
|
|
234
|
+
const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
|
|
158
235
|
let sbom = new Sbom();
|
|
159
|
-
sbom.addRoot(mainComponent);
|
|
236
|
+
sbom.addRoot(mainComponent, license);
|
|
160
237
|
this._addDependenciesToSbom(sbom, depsObject);
|
|
238
|
+
this.#ensurePeerAndOptionalDeps(sbom);
|
|
161
239
|
sbom.filterIgnoredDeps(this.#manifest.ignored);
|
|
162
240
|
return sbom.getAsJsonString(opts);
|
|
163
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Ensures peer and optional dependencies declared in the manifest are
|
|
244
|
+
* present in the SBOM, even when the package manager does not resolve them
|
|
245
|
+
* (e.g. yarn does not include peer deps in its dependency listing).
|
|
246
|
+
* @param {Sbom} sbom - The SBOM to supplement
|
|
247
|
+
* @private
|
|
248
|
+
*/
|
|
249
|
+
#ensurePeerAndOptionalDeps(sbom) {
|
|
250
|
+
const rootPurl = toPurl(purlType, this.#manifest.name, this.#manifest.version);
|
|
251
|
+
const depSources = [this.#manifest.peerDependencies, this.#manifest.optionalDependencies];
|
|
252
|
+
for (const source of depSources) {
|
|
253
|
+
for (const [name, version] of Object.entries(source)) {
|
|
254
|
+
// Build the purl prefix for exact matching (e.g. "pkg:npm/minimist@"
|
|
255
|
+
// or "pkg:npm/%40hapi/joi@") to avoid substring false positives
|
|
256
|
+
const probe = toPurl(purlType, name, version);
|
|
257
|
+
const purlPrefix = probe.toString().replace(/@[^@]*$/, '@');
|
|
258
|
+
if (!sbom.checkDependsOnByPurlPrefix(rootPurl, purlPrefix)) {
|
|
259
|
+
sbom.addDependency(rootPurl, probe);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
164
264
|
/**
|
|
165
265
|
* Recursively builds the Sbom from the JSON that npm listing returns
|
|
166
266
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
@@ -168,7 +268,10 @@ export default class Base_javascript {
|
|
|
168
268
|
* @protected
|
|
169
269
|
*/
|
|
170
270
|
_addDependenciesToSbom(sbom, depTree) {
|
|
171
|
-
const dependencies =
|
|
271
|
+
const dependencies = {
|
|
272
|
+
...depTree["dependencies"],
|
|
273
|
+
...depTree["optionalDependencies"],
|
|
274
|
+
};
|
|
172
275
|
Object.entries(dependencies)
|
|
173
276
|
.forEach(entry => {
|
|
174
277
|
const [name, artifact] = entry;
|
|
@@ -204,10 +307,11 @@ export default class Base_javascript {
|
|
|
204
307
|
* @private
|
|
205
308
|
*/
|
|
206
309
|
#getDirectDependencySbom(opts = {}) {
|
|
207
|
-
const depTree = this._buildDependencyTree(false);
|
|
310
|
+
const depTree = this._buildDependencyTree(false, opts);
|
|
208
311
|
let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
|
|
312
|
+
const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
|
|
209
313
|
let sbom = new Sbom();
|
|
210
|
-
sbom.addRoot(mainComponent);
|
|
314
|
+
sbom.addRoot(mainComponent, license);
|
|
211
315
|
const rootDeps = this._getRootDependencies(depTree);
|
|
212
316
|
const sortedDepsKeys = Array
|
|
213
317
|
.from(rootDeps.keys())
|
|
@@ -217,6 +321,7 @@ export default class Base_javascript {
|
|
|
217
321
|
const rootPurl = toPurlFromString(sbom.getRoot().purl);
|
|
218
322
|
sbom.addDependency(rootPurl, rootDeps.get(key));
|
|
219
323
|
}
|
|
324
|
+
this.#ensurePeerAndOptionalDeps(sbom);
|
|
220
325
|
sbom.filterIgnoredDeps(this.#manifest.ignored);
|
|
221
326
|
return sbom.getAsJsonString(opts);
|
|
222
327
|
}
|
|
@@ -227,10 +332,14 @@ export default class Base_javascript {
|
|
|
227
332
|
* @protected
|
|
228
333
|
*/
|
|
229
334
|
_getRootDependencies(depTree) {
|
|
230
|
-
|
|
335
|
+
const allDeps = {
|
|
336
|
+
...depTree.dependencies,
|
|
337
|
+
...depTree.optionalDependencies,
|
|
338
|
+
};
|
|
339
|
+
if (Object.keys(allDeps).length === 0) {
|
|
231
340
|
return new Map();
|
|
232
341
|
}
|
|
233
|
-
return new Map(Object.entries(
|
|
342
|
+
return new Map(Object.entries(allDeps).map(([key, value]) => [key, toPurl(purlType, key, value.version)]));
|
|
234
343
|
}
|
|
235
344
|
/**
|
|
236
345
|
* Executes the list command to get dependencies
|
|
@@ -241,7 +350,7 @@ export default class Base_javascript {
|
|
|
241
350
|
*/
|
|
242
351
|
#executeListCmd(includeTransitive, manifestDir) {
|
|
243
352
|
const listArgs = this._listCmdArgs(includeTransitive, manifestDir);
|
|
244
|
-
return this.#invokeCommand(listArgs);
|
|
353
|
+
return this.#invokeCommand(listArgs, { cwd: manifestDir });
|
|
245
354
|
}
|
|
246
355
|
/**
|
|
247
356
|
* Gets the version of the package manager
|
|
@@ -260,13 +369,11 @@ export default class Base_javascript {
|
|
|
260
369
|
const originalDir = process.cwd();
|
|
261
370
|
const isWindows = os.platform() === 'win32';
|
|
262
371
|
if (isWindows) {
|
|
263
|
-
// On Windows, --prefix flag doesn't work as expected
|
|
264
|
-
// Instead of installing from the prefix folder, it installs from current working directory
|
|
265
372
|
process.chdir(manifestDir);
|
|
266
373
|
}
|
|
267
374
|
try {
|
|
268
375
|
const args = this._updateLockFileCmdArgs(manifestDir);
|
|
269
|
-
this.#invokeCommand(args);
|
|
376
|
+
this.#invokeCommand(args, { cwd: manifestDir });
|
|
270
377
|
}
|
|
271
378
|
finally {
|
|
272
379
|
if (isWindows) {
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/** @typedef {{name: string, version: string, children: string[]}} GraphEntry */
|
|
2
|
+
/** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
|
|
3
|
+
/** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
|
|
4
|
+
/** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
|
|
5
|
+
export default class Base_pyproject {
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} manifestName
|
|
8
|
+
* @returns {boolean}
|
|
9
|
+
*/
|
|
10
|
+
isSupported(manifestName: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} manifestDir
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
15
|
+
validateLockFile(manifestDir: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Read project license from pyproject.toml, with fallback to LICENSE file.
|
|
18
|
+
* @param {string} manifestPath
|
|
19
|
+
* @returns {string|null}
|
|
20
|
+
*/
|
|
21
|
+
readLicenseFromManifest(manifestPath: string): string | null;
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} manifest - path to pyproject.toml
|
|
24
|
+
* @param {Object} [opts={}]
|
|
25
|
+
* @returns {Promise<Provided>}
|
|
26
|
+
*/
|
|
27
|
+
provideStack(manifest: string, opts?: any): Promise<Provided>;
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} manifest - path to pyproject.toml
|
|
30
|
+
* @param {Object} [opts={}]
|
|
31
|
+
* @returns {Promise<Provided>}
|
|
32
|
+
*/
|
|
33
|
+
provideComponent(manifest: string, opts?: any): Promise<Provided>;
|
|
34
|
+
/**
|
|
35
|
+
* @returns {string}
|
|
36
|
+
* @protected
|
|
37
|
+
*/
|
|
38
|
+
protected _lockFileName(): string;
|
|
39
|
+
/**
|
|
40
|
+
* @returns {string}
|
|
41
|
+
* @protected
|
|
42
|
+
*/
|
|
43
|
+
protected _cmdName(): string;
|
|
44
|
+
/**
|
|
45
|
+
* Resolve dependencies using the tool-specific command and parser.
|
|
46
|
+
* @param {string} manifestDir
|
|
47
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
48
|
+
* @param {Object} opts
|
|
49
|
+
* @returns {Promise<DependencyData>}
|
|
50
|
+
* @protected
|
|
51
|
+
*/
|
|
52
|
+
protected _getDependencyData(manifestDir: string, parsed: object, opts: any): Promise<DependencyData>;
|
|
53
|
+
/**
|
|
54
|
+
* Canonicalize a Python package name per PEP 503.
|
|
55
|
+
* @param {string} name
|
|
56
|
+
* @returns {string}
|
|
57
|
+
* @protected
|
|
58
|
+
*/
|
|
59
|
+
protected _canonicalize(name: string): string;
|
|
60
|
+
/**
|
|
61
|
+
* Get the project name from pyproject.toml.
|
|
62
|
+
* @param {object} parsed
|
|
63
|
+
* @returns {string|null}
|
|
64
|
+
* @protected
|
|
65
|
+
*/
|
|
66
|
+
protected _getProjectName(parsed: object): string | null;
|
|
67
|
+
/**
|
|
68
|
+
* Get the project version from pyproject.toml.
|
|
69
|
+
* @param {object} parsed
|
|
70
|
+
* @returns {string|null}
|
|
71
|
+
* @protected
|
|
72
|
+
*/
|
|
73
|
+
protected _getProjectVersion(parsed: object): string | null;
|
|
74
|
+
/**
|
|
75
|
+
* Scan raw pyproject.toml text for dependencies with ignore markers.
|
|
76
|
+
* @param {string} manifestPath
|
|
77
|
+
* @returns {Set<string>}
|
|
78
|
+
* @protected
|
|
79
|
+
*/
|
|
80
|
+
protected _getIgnoredDeps(manifestPath: string): Set<string>;
|
|
81
|
+
/**
|
|
82
|
+
* Build dependency tree from graph, starting from direct deps.
|
|
83
|
+
* @param {Map<string, GraphEntry>} graph
|
|
84
|
+
* @param {string[]} directDeps - canonical names of direct deps
|
|
85
|
+
* @param {Set<string>} ignoredDeps
|
|
86
|
+
* @param {boolean} includeTransitive
|
|
87
|
+
* @returns {DepTreeEntry[]}
|
|
88
|
+
* @protected
|
|
89
|
+
*/
|
|
90
|
+
protected _buildDependencyTree(graph: Map<string, GraphEntry>, directDeps: string[], ignoredDeps: Set<string>, includeTransitive: boolean): DepTreeEntry[];
|
|
91
|
+
/**
|
|
92
|
+
* Recursively collect transitive dependencies.
|
|
93
|
+
* @param {Map<string, GraphEntry>} graph
|
|
94
|
+
* @param {string[]} childKeys
|
|
95
|
+
* @param {DepTreeEntry[]} result - mutated in place
|
|
96
|
+
* @param {Set<string>} ignoredDeps
|
|
97
|
+
* @param {Set<string>} visited
|
|
98
|
+
* @returns {void}
|
|
99
|
+
* @protected
|
|
100
|
+
*/
|
|
101
|
+
protected _collectTransitive(graph: Map<string, GraphEntry>, childKeys: string[], result: DepTreeEntry[], ignoredDeps: Set<string>, visited: Set<string>): void;
|
|
102
|
+
/**
|
|
103
|
+
* @param {string} name
|
|
104
|
+
* @param {string} version
|
|
105
|
+
* @returns {PackageURL}
|
|
106
|
+
* @protected
|
|
107
|
+
*/
|
|
108
|
+
protected _toPurl(name: string, version: string): PackageURL;
|
|
109
|
+
/**
|
|
110
|
+
* Recursively add a dependency and its transitive deps to the SBOM.
|
|
111
|
+
* @param {PackageURL} source
|
|
112
|
+
* @param {DepTreeEntry} dep
|
|
113
|
+
* @param {Sbom} sbom
|
|
114
|
+
* @returns {void}
|
|
115
|
+
* @private
|
|
116
|
+
*/
|
|
117
|
+
private _addAllDependencies;
|
|
118
|
+
/**
|
|
119
|
+
* Create SBOM json string for a pyproject.toml project.
|
|
120
|
+
* @param {string} manifest - path to pyproject.toml
|
|
121
|
+
* @param {Object} opts
|
|
122
|
+
* @param {boolean} includeTransitive
|
|
123
|
+
* @returns {Promise<string>}
|
|
124
|
+
* @private
|
|
125
|
+
*/
|
|
126
|
+
private _createSbom;
|
|
127
|
+
}
|
|
128
|
+
export type GraphEntry = {
|
|
129
|
+
name: string;
|
|
130
|
+
version: string;
|
|
131
|
+
children: string[];
|
|
132
|
+
};
|
|
133
|
+
export type DepTreeEntry = {
|
|
134
|
+
name: string;
|
|
135
|
+
version: string;
|
|
136
|
+
dependencies: DepTreeEntry[];
|
|
137
|
+
};
|
|
138
|
+
export type DependencyData = {
|
|
139
|
+
directDeps: string[];
|
|
140
|
+
graph: Map<string, GraphEntry>;
|
|
141
|
+
};
|
|
142
|
+
export type Provided = {
|
|
143
|
+
ecosystem: string;
|
|
144
|
+
content: string;
|
|
145
|
+
contentType: string;
|
|
146
|
+
};
|
|
147
|
+
import { PackageURL } from 'packageurl-js';
|