@trustify-da/trustify-da-javascript-client 0.3.0-ea.02983f2 → 0.3.0-ea.0c4e031
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 +192 -12
- package/dist/package.json +43 -27
- package/dist/src/analysis.d.ts +21 -5
- package/dist/src/analysis.js +83 -83
- 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 +17 -3
- package/dist/src/cyclone_dx_sbom.js +56 -8
- package/dist/src/index.d.ts +198 -12
- package/dist/src/index.js +357 -9
- package/dist/src/license/index.d.ts +28 -0
- package/dist/src/license/index.js +103 -0
- package/dist/src/license/license_utils.d.ts +40 -0
- package/dist/src/license/license_utils.js +137 -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/images.js +15 -1
- package/dist/src/oci_image/utils.d.ts +7 -7
- package/dist/src/oci_image/utils.js +11 -2
- package/dist/src/provider.d.ts +18 -5
- package/dist/src/provider.js +33 -5
- package/dist/src/providers/base_java.d.ts +15 -15
- package/dist/src/providers/base_java.js +18 -44
- package/dist/src/providers/base_javascript.d.ts +45 -12
- package/dist/src/providers/base_javascript.js +138 -24
- package/dist/src/providers/base_pyproject.d.ts +158 -0
- package/dist/src/providers/base_pyproject.js +322 -0
- package/dist/src/providers/containerfile_parser.d.ts +5 -0
- package/dist/src/providers/containerfile_parser.js +20 -0
- package/dist/src/providers/golang_gomodules.d.ts +30 -13
- package/dist/src/providers/golang_gomodules.js +176 -121
- 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 +28 -3
- package/dist/src/providers/java_gradle.js +128 -4
- 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 +20 -5
- package/dist/src/providers/java_maven.js +198 -6
- package/dist/src/providers/javascript_bun.d.ts +10 -0
- package/dist/src/providers/javascript_bun.js +100 -0
- package/dist/src/providers/javascript_npm.d.ts +6 -0
- package/dist/src/providers/javascript_npm.js +21 -0
- package/dist/src/providers/javascript_pnpm.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +8 -4
- package/dist/src/providers/manifest.d.ts +2 -0
- package/dist/src/providers/manifest.js +22 -4
- package/dist/src/providers/marker_evaluator.d.ts +14 -0
- package/dist/src/providers/marker_evaluator.js +191 -0
- package/dist/src/providers/oci_dockerfile.d.ts +51 -0
- package/dist/src/providers/oci_dockerfile.js +177 -0
- package/dist/src/providers/processors/yarn_berry_processor.d.ts +2 -2
- package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
- package/dist/src/providers/processors/yarn_classic_processor.d.ts +2 -2
- package/dist/src/providers/python_controller.d.ts +10 -3
- package/dist/src/providers/python_controller.js +118 -62
- package/dist/src/providers/python_pip.d.ts +16 -4
- package/dist/src/providers/python_pip.js +51 -58
- package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
- package/dist/src/providers/python_pip_pyproject.js +146 -0
- package/dist/src/providers/python_poetry.d.ts +98 -0
- package/dist/src/providers/python_poetry.js +286 -0
- package/dist/src/providers/python_uv.d.ts +46 -0
- package/dist/src/providers/python_uv.js +269 -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 +56 -0
- package/dist/src/providers/rust_cargo.js +641 -0
- package/dist/src/providers/tree-sitter-containerfile.wasm +0 -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 +17 -2
- package/dist/src/sbom.js +16 -4
- package/dist/src/tools.d.ts +48 -6
- package/dist/src/tools.js +114 -1
- package/dist/src/updaters/maven_updater.d.ts +26 -0
- package/dist/src/updaters/maven_updater.js +322 -0
- package/dist/src/workspace.d.ts +70 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +44 -28
package/dist/src/sbom.js
CHANGED
|
@@ -12,10 +12,11 @@ export default class Sbom {
|
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* @param {PackageURL} root - add main/root component for sbom
|
|
15
|
+
* @param {string|Array} [licenses] - optional license(s) for the root component
|
|
15
16
|
* @return Sbom
|
|
16
17
|
*/
|
|
17
|
-
addRoot(root) {
|
|
18
|
-
return this.sbomModel.addRoot(root);
|
|
18
|
+
addRoot(root, licenses) {
|
|
19
|
+
return this.sbomModel.addRoot(root, licenses);
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
22
|
* @return {{{"bom-ref": string, name, purl: string, type, version}}} root component of sbom.
|
|
@@ -42,10 +43,12 @@ export default class Sbom {
|
|
|
42
43
|
/**
|
|
43
44
|
* @param {component} sourceRef current source Component ( Starting from root component by clients)
|
|
44
45
|
* @param {PackageURL} targetRef current dependency to add to Dependencies list of component sourceRef
|
|
46
|
+
* @param {string} [scope] - Scope of the dependency
|
|
47
|
+
* @param {Array<{alg: string, content: string}>} [targetHashes] - Optional hashes for the target component
|
|
45
48
|
* @return Sbom
|
|
46
49
|
*/
|
|
47
|
-
addDependency(sourceRef, targetRef, scope) {
|
|
48
|
-
return this.sbomModel.addDependency(sourceRef, targetRef, scope);
|
|
50
|
+
addDependency(sourceRef, targetRef, scope, targetHashes) {
|
|
51
|
+
return this.sbomModel.addDependency(sourceRef, targetRef, scope, targetHashes);
|
|
49
52
|
}
|
|
50
53
|
/**
|
|
51
54
|
* @return String sbom json in a string format
|
|
@@ -76,6 +79,15 @@ export default class Sbom {
|
|
|
76
79
|
checkIfPackageInsideDependsOnList(component, name) {
|
|
77
80
|
return this.sbomModel.checkIfPackageInsideDependsOnList(component, name);
|
|
78
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Checks if any entry in the dependsOn list of sourceRef starts with the given purl prefix.
|
|
84
|
+
* @param {PackageURL} sourceRef - The source component to check
|
|
85
|
+
* @param {string} purlPrefix - The purl prefix to match (e.g. "pkg:npm/minimist@")
|
|
86
|
+
* @return {boolean}
|
|
87
|
+
*/
|
|
88
|
+
checkDependsOnByPurlPrefix(sourceRef, purlPrefix) {
|
|
89
|
+
return this.sbomModel.checkDependsOnByPurlPrefix(sourceRef, purlPrefix);
|
|
90
|
+
}
|
|
79
91
|
/** Removes the root component from the sbom
|
|
80
92
|
*/
|
|
81
93
|
removeRootComponent() {
|
package/dist/src/tools.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="packageurl-js/src/package-url" />
|
|
3
1
|
/**
|
|
4
2
|
*
|
|
5
3
|
* @param {string} key to log its value from environment variables and from opts, if it exists
|
|
6
4
|
* @param {{}} [opts={}] different options of application, if key in it, log it.
|
|
7
5
|
* @param {string }defValue default value of key in case there is no option and environment variable values for key
|
|
8
6
|
*/
|
|
9
|
-
export function logValueFromObjects(key: string, opts?: {}
|
|
7
|
+
export function logValueFromObjects(key: string, opts?: {}, defValue: string): void;
|
|
10
8
|
/**
|
|
11
9
|
* Utility function will return the value for key from the environment variables,
|
|
12
10
|
* if not present will return the value for key from the opts objects only if it's a string,
|
|
@@ -17,7 +15,7 @@ export function logValueFromObjects(key: string, opts?: {} | undefined, defValue
|
|
|
17
15
|
* @returns {string|null} the value of the key found in the environment, options object, or the
|
|
18
16
|
* default supplied
|
|
19
17
|
*/
|
|
20
|
-
export function getCustom(key: string, def?: string | null
|
|
18
|
+
export function getCustom(key: string, def?: string | null, opts?: {}): string | null;
|
|
21
19
|
/**
|
|
22
20
|
* Utility function for looking up custom variable for a binary path.
|
|
23
21
|
* Will look in the environment variables (1) or in opts (2) for a key with TRUSTIFY_DA_x_PATH, x is an
|
|
@@ -28,7 +26,7 @@ export function getCustom(key: string, def?: string | null | undefined, opts?: {
|
|
|
28
26
|
* @returns {string|null} the value of the key found in the environment, options object, or the
|
|
29
27
|
* original name supplied
|
|
30
28
|
*/
|
|
31
|
-
export function getCustomPath(name: any, opts?: {}
|
|
29
|
+
export function getCustomPath(name: any, opts?: {}): string | null;
|
|
32
30
|
/**
|
|
33
31
|
* Utility function for determining whether wrappers for build tools such as gradlew/mvnw should be
|
|
34
32
|
* preferred over invoking the binary directly.
|
|
@@ -63,12 +61,56 @@ export function toPurlFromString(strPurl: any): PackageURL | null;
|
|
|
63
61
|
* @param {string} cwd - directory for which to find the root of the git repository.
|
|
64
62
|
*/
|
|
65
63
|
export function getGitRootDir(cwd: string): string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Normalize a filesystem path, lowercasing on Windows for case-insensitive comparison.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} thePath
|
|
68
|
+
* @returns {string}
|
|
69
|
+
*/
|
|
70
|
+
export function normalizePath(thePath: string): string;
|
|
71
|
+
/**
|
|
72
|
+
* Walk up from `startDir` to `repoRoot` looking for an executable wrapper script.
|
|
73
|
+
*
|
|
74
|
+
* @param {string} startDir - Absolute directory to start from
|
|
75
|
+
* @param {string} wrapperName - Wrapper filename (e.g. `mvnw`, `gradlew`)
|
|
76
|
+
* @param {string} [repoRoot] - Stop boundary (defaults to git root or filesystem root)
|
|
77
|
+
* @returns {string | undefined}
|
|
78
|
+
*/
|
|
79
|
+
export function traverseForWrapper(startDir: string, wrapperName: string, repoRoot?: string): string | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Resolve a build-tool binary, preferring a wrapper when configured.
|
|
82
|
+
*
|
|
83
|
+
* @param {string} globalBinary - Global binary name (e.g. `mvn`, `gradle`)
|
|
84
|
+
* @param {string} localWrapper - Wrapper filename (e.g. `mvnw`, `gradlew.bat`)
|
|
85
|
+
* @param {string} startDir - Directory from which to start the wrapper search
|
|
86
|
+
* @param {import('./index.js').Options} [opts={}]
|
|
87
|
+
* @returns {string} Path to the resolved binary
|
|
88
|
+
*/
|
|
89
|
+
export function resolveBinary(globalBinary: string, localWrapper: string, startDir: string, opts?: import("./index.js").Options): string;
|
|
66
90
|
/** this method invokes command string in a process in a synchronous way.
|
|
67
91
|
* @param {string} bin - the command to be invoked
|
|
68
92
|
* @param {Array<string>} args - the args to pass to the binary
|
|
69
93
|
* @param {import('child_process').ExecFileOptionsWithStringEncoding} [opts={}]
|
|
70
94
|
* @returns {string}
|
|
71
95
|
*/
|
|
72
|
-
export function invokeCommand(bin: string, args: Array<string>, opts?: import("child_process").ExecFileOptionsWithStringEncoding
|
|
96
|
+
export function invokeCommand(bin: string, args: Array<string>, opts?: import("child_process").ExecFileOptionsWithStringEncoding): string;
|
|
97
|
+
/**
|
|
98
|
+
* Adds proxy agent configuration to fetch options if a proxy URL is specified
|
|
99
|
+
* @param {RequestInit} options - The base fetch options
|
|
100
|
+
* @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
|
|
101
|
+
* @returns {RequestInit} The fetch options with proxy agent if applicable
|
|
102
|
+
*/
|
|
103
|
+
export function addProxyAgent(options: RequestInit, opts: any): RequestInit;
|
|
104
|
+
/**
|
|
105
|
+
* Utility function for fetching vendor tokens
|
|
106
|
+
* @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
|
|
107
|
+
* @returns {{}}
|
|
108
|
+
*/
|
|
109
|
+
export function getTokenHeaders(opts?: any): {};
|
|
73
110
|
export const RegexNotToBeLogged: RegExp;
|
|
111
|
+
export const TRUSTIFY_DA_TOKEN_HEADER: "trust-da-token";
|
|
112
|
+
export const TRUSTIFY_DA_TELEMETRY_ID_HEADER: "telemetry-anonymous-id";
|
|
113
|
+
export const TRUSTIFY_DA_SOURCE_HEADER: "trust-da-source";
|
|
114
|
+
export const TRUSTIFY_DA_OPERATION_TYPE_HEADER: "trust-da-operation-type";
|
|
115
|
+
export const TRUSTIFY_DA_PACKAGE_MANAGER_HEADER: "trust-da-pkg-manager";
|
|
74
116
|
import { PackageURL } from "packageurl-js";
|
package/dist/src/tools.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { execFileSync } from "child_process";
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
2
4
|
import { EOL } from "os";
|
|
5
|
+
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
3
6
|
import { PackageURL } from "packageurl-js";
|
|
4
|
-
export const RegexNotToBeLogged = /TRUSTIFY_DA_.*
|
|
7
|
+
export const RegexNotToBeLogged = /TRUSTIFY_DA_(.*_)?TOKEN|ex-.*-token|trust-.*-token/;
|
|
5
8
|
/**
|
|
6
9
|
*
|
|
7
10
|
* @param {string} key to log its value from environment variables and from opts, if it exists
|
|
@@ -127,6 +130,62 @@ export function getGitRootDir(cwd) {
|
|
|
127
130
|
return undefined;
|
|
128
131
|
}
|
|
129
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Normalize a filesystem path, lowercasing on Windows for case-insensitive comparison.
|
|
135
|
+
*
|
|
136
|
+
* @param {string} thePath
|
|
137
|
+
* @returns {string}
|
|
138
|
+
*/
|
|
139
|
+
export function normalizePath(thePath) {
|
|
140
|
+
const normalized = path.resolve(thePath).normalize();
|
|
141
|
+
return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Walk up from `startDir` to `repoRoot` looking for an executable wrapper script.
|
|
145
|
+
*
|
|
146
|
+
* @param {string} startDir - Absolute directory to start from
|
|
147
|
+
* @param {string} wrapperName - Wrapper filename (e.g. `mvnw`, `gradlew`)
|
|
148
|
+
* @param {string} [repoRoot] - Stop boundary (defaults to git root or filesystem root)
|
|
149
|
+
* @returns {string | undefined}
|
|
150
|
+
*/
|
|
151
|
+
export function traverseForWrapper(startDir, wrapperName, repoRoot = undefined) {
|
|
152
|
+
const currentDir = normalizePath(startDir);
|
|
153
|
+
repoRoot = repoRoot || getGitRootDir(currentDir) || path.parse(currentDir).root;
|
|
154
|
+
const wrapperPath = path.join(currentDir, wrapperName);
|
|
155
|
+
try {
|
|
156
|
+
fs.accessSync(wrapperPath, fs.constants.X_OK);
|
|
157
|
+
return wrapperPath;
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
const rootDir = path.parse(currentDir).root;
|
|
161
|
+
if (currentDir === repoRoot || currentDir === rootDir) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const parentDir = path.dirname(currentDir);
|
|
165
|
+
if (parentDir === currentDir || parentDir === rootDir) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
return traverseForWrapper(parentDir, wrapperName, repoRoot);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Resolve a build-tool binary, preferring a wrapper when configured.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} globalBinary - Global binary name (e.g. `mvn`, `gradle`)
|
|
175
|
+
* @param {string} localWrapper - Wrapper filename (e.g. `mvnw`, `gradlew.bat`)
|
|
176
|
+
* @param {string} startDir - Directory from which to start the wrapper search
|
|
177
|
+
* @param {import('./index.js').Options} [opts={}]
|
|
178
|
+
* @returns {string} Path to the resolved binary
|
|
179
|
+
*/
|
|
180
|
+
export function resolveBinary(globalBinary, localWrapper, startDir, opts = {}) {
|
|
181
|
+
if (getWrapperPreference(globalBinary, opts)) {
|
|
182
|
+
const wrapper = traverseForWrapper(startDir, localWrapper);
|
|
183
|
+
if (wrapper !== undefined) {
|
|
184
|
+
return wrapper;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return getCustomPath(globalBinary, opts);
|
|
188
|
+
}
|
|
130
189
|
/** this method invokes command string in a process in a synchronous way.
|
|
131
190
|
* @param {string} bin - the command to be invoked
|
|
132
191
|
* @param {Array<string>} args - the args to pass to the binary
|
|
@@ -157,3 +216,57 @@ export function invokeCommand(bin, args, opts = {}) {
|
|
|
157
216
|
};
|
|
158
217
|
return execFileSync(bin, args, { ...{ stdio: 'pipe', encoding: 'utf-8' }, ...opts });
|
|
159
218
|
}
|
|
219
|
+
export const TRUSTIFY_DA_TOKEN_HEADER = "trust-da-token";
|
|
220
|
+
export const TRUSTIFY_DA_TELEMETRY_ID_HEADER = "telemetry-anonymous-id";
|
|
221
|
+
export const TRUSTIFY_DA_SOURCE_HEADER = "trust-da-source";
|
|
222
|
+
export const TRUSTIFY_DA_OPERATION_TYPE_HEADER = "trust-da-operation-type";
|
|
223
|
+
export const TRUSTIFY_DA_PACKAGE_MANAGER_HEADER = "trust-da-pkg-manager";
|
|
224
|
+
/**
|
|
225
|
+
* Adds proxy agent configuration to fetch options if a proxy URL is specified
|
|
226
|
+
* @param {RequestInit} options - The base fetch options
|
|
227
|
+
* @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
|
|
228
|
+
* @returns {RequestInit} The fetch options with proxy agent if applicable
|
|
229
|
+
*/
|
|
230
|
+
export function addProxyAgent(options, opts) {
|
|
231
|
+
const proxyUrl = getCustom('TRUSTIFY_DA_PROXY_URL', null, opts);
|
|
232
|
+
if (proxyUrl) {
|
|
233
|
+
options.agent = new HttpsProxyAgent(proxyUrl);
|
|
234
|
+
}
|
|
235
|
+
return options;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Utility function for fetching vendor tokens
|
|
239
|
+
* @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
|
|
240
|
+
* @returns {{}}
|
|
241
|
+
*/
|
|
242
|
+
export function getTokenHeaders(opts = {}) {
|
|
243
|
+
let headers = {};
|
|
244
|
+
setCustomHeader(TRUSTIFY_DA_TOKEN_HEADER, headers, 'TRUSTIFY_DA_TOKEN', opts);
|
|
245
|
+
setCustomHeader(TRUSTIFY_DA_SOURCE_HEADER, headers, 'TRUSTIFY_DA_SOURCE', opts);
|
|
246
|
+
setCustomHeader(TRUSTIFY_DA_OPERATION_TYPE_HEADER, headers, TRUSTIFY_DA_OPERATION_TYPE_HEADER.toUpperCase().replaceAll("-", "_"), opts);
|
|
247
|
+
setCustomHeader(TRUSTIFY_DA_PACKAGE_MANAGER_HEADER, headers, TRUSTIFY_DA_PACKAGE_MANAGER_HEADER.toUpperCase().replaceAll("-", "_"), opts);
|
|
248
|
+
setCustomHeader(TRUSTIFY_DA_TELEMETRY_ID_HEADER, headers, 'TRUSTIFY_DA_TELEMETRY_ID', opts);
|
|
249
|
+
if (getCustom("TRUSTIFY_DA_DEBUG", null, opts) === "true") {
|
|
250
|
+
console.log("Headers Values to be sent to Trustify DA backend:" + EOL);
|
|
251
|
+
for (const headerKey in headers) {
|
|
252
|
+
if (!headerKey.match(RegexNotToBeLogged)) {
|
|
253
|
+
console.log(`${headerKey}: ${headers[headerKey]}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return headers;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
*
|
|
261
|
+
* @param {string} headerName - the header name to populate in request
|
|
262
|
+
* @param headers
|
|
263
|
+
* @param {string} optsKey - key in the options object to use the value for
|
|
264
|
+
* @param {import("index.js").Options} [opts={}] - options input object to fetch header values from
|
|
265
|
+
* @private
|
|
266
|
+
*/
|
|
267
|
+
function setCustomHeader(headerName, headers, optsKey, opts) {
|
|
268
|
+
let customHeaderValue = getCustom(optsKey, null, opts);
|
|
269
|
+
if (customHeaderValue) {
|
|
270
|
+
headers[headerName] = customHeaderValue;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Updates dependency versions in a Maven pom.xml while preserving file formatting.
|
|
3
|
+
*
|
|
4
|
+
* Uses `fast-xml-parser` to understand the XML structure (dependencies and properties),
|
|
5
|
+
* then performs position-based string splice to replace version strings without altering
|
|
6
|
+
* any other formatting, comments, whitespace, or indentation.
|
|
7
|
+
*
|
|
8
|
+
* Supports `${property}` indirection: when a dependency's `<version>` is a property
|
|
9
|
+
* reference, the updater resolves the property chain (including recursive references)
|
|
10
|
+
* and updates the terminal property value in `<properties>` instead.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} pomContent - the raw pom.xml file content
|
|
13
|
+
* @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges -
|
|
14
|
+
* list of version changes to apply
|
|
15
|
+
* @returns {{content: string, applied: Array, skipped: Array}} the updated content and
|
|
16
|
+
* lists of applied and skipped changes
|
|
17
|
+
*/
|
|
18
|
+
export function updateMavenVersions(pomContent: string, versionChanges: Array<{
|
|
19
|
+
groupId: string;
|
|
20
|
+
artifactId: string;
|
|
21
|
+
newVersion: string;
|
|
22
|
+
}>): {
|
|
23
|
+
content: string;
|
|
24
|
+
applied: any[];
|
|
25
|
+
skipped: any[];
|
|
26
|
+
};
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { XMLParser } from 'fast-xml-parser';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts the trimmed text content of a simple XML element from a string block.
|
|
4
|
+
* @param {string} block - the XML text to search within
|
|
5
|
+
* @param {string} tagName - the element name whose content to extract
|
|
6
|
+
* @returns {string|null} the trimmed text content, or null if not found
|
|
7
|
+
*/
|
|
8
|
+
function extractTagContent(block, tagName) {
|
|
9
|
+
const openTag = `<${tagName}>`;
|
|
10
|
+
const closeTag = `</${tagName}>`;
|
|
11
|
+
const start = block.indexOf(openTag);
|
|
12
|
+
if (start === -1) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const contentStart = start + openTag.length;
|
|
16
|
+
const end = block.indexOf(closeTag, contentStart);
|
|
17
|
+
if (end === -1) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return block.substring(contentStart, end).trim();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Finds the character positions of an XML element's text content within the raw file.
|
|
24
|
+
* Returns the start (inclusive) and end (exclusive) offsets of the text between the
|
|
25
|
+
* opening and closing tags.
|
|
26
|
+
* @param {string} content - full raw XML content
|
|
27
|
+
* @param {number} searchStart - start offset to search from
|
|
28
|
+
* @param {number} searchEnd - end offset to search within
|
|
29
|
+
* @param {string} tagName - the element name whose content position to locate
|
|
30
|
+
* @returns {{start: number, end: number}|null} character offsets, or null if not found
|
|
31
|
+
*/
|
|
32
|
+
function findTagContentPosition(content, searchStart, searchEnd, tagName) {
|
|
33
|
+
const openTag = `<${tagName}>`;
|
|
34
|
+
const closeTag = `</${tagName}>`;
|
|
35
|
+
const tagStart = content.indexOf(openTag, searchStart);
|
|
36
|
+
if (tagStart === -1 || tagStart >= searchEnd) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const contentStart = tagStart + openTag.length;
|
|
40
|
+
const tagEnd = content.indexOf(closeTag, contentStart);
|
|
41
|
+
if (tagEnd === -1 || tagEnd > searchEnd) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return { start: contentStart, end: tagEnd };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Checks whether the character immediately following a tag prefix indicates
|
|
48
|
+
* that the prefix is the complete tag name (e.g. `<dependency>` vs `<dependencyManagement>`).
|
|
49
|
+
* @param {string} char - the character after the tag prefix
|
|
50
|
+
* @returns {boolean} true if the character terminates the tag name
|
|
51
|
+
*/
|
|
52
|
+
function isTagBoundary(char) {
|
|
53
|
+
return char === '>' || char === ' ' || char === '\t'
|
|
54
|
+
|| char === '\n' || char === '\r' || char === '/';
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Finds the text content position of a `<version>` element within the first
|
|
58
|
+
* `<dependency>` block matching the given groupId and artifactId.
|
|
59
|
+
* Searches through all `<dependency>` elements in the file, covering both
|
|
60
|
+
* `<dependencies>` and `<dependencyManagement>` sections.
|
|
61
|
+
* @param {string} content - full raw pom.xml content
|
|
62
|
+
* @param {string} groupId - the dependency's groupId to match
|
|
63
|
+
* @param {string} artifactId - the dependency's artifactId to match
|
|
64
|
+
* @returns {{start: number, end: number}|null} version content offsets, or null
|
|
65
|
+
*/
|
|
66
|
+
function findDependencyVersionPosition(content, groupId, artifactId) {
|
|
67
|
+
let searchStart = 0;
|
|
68
|
+
const TAG_PREFIX = '<dependency';
|
|
69
|
+
const CLOSE_TAG = '</dependency>';
|
|
70
|
+
while (searchStart < content.length) {
|
|
71
|
+
const start = content.indexOf(TAG_PREFIX, searchStart);
|
|
72
|
+
if (start === -1) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
// Verify this is exactly <dependency> and not <dependencyManagement> etc.
|
|
76
|
+
const charAfterTag = content.charAt(start + TAG_PREFIX.length);
|
|
77
|
+
if (!isTagBoundary(charAfterTag)) {
|
|
78
|
+
searchStart = start + 1;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
// Find closing </dependency>
|
|
82
|
+
const end = content.indexOf(CLOSE_TAG, start + TAG_PREFIX.length);
|
|
83
|
+
if (end === -1) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
const blockEnd = end + CLOSE_TAG.length;
|
|
87
|
+
const block = content.substring(start, blockEnd);
|
|
88
|
+
const gid = extractTagContent(block, 'groupId');
|
|
89
|
+
const aid = extractTagContent(block, 'artifactId');
|
|
90
|
+
if (gid === groupId && aid === artifactId) {
|
|
91
|
+
const pos = findTagContentPosition(content, start, blockEnd, 'version');
|
|
92
|
+
if (pos) {
|
|
93
|
+
return pos;
|
|
94
|
+
}
|
|
95
|
+
// Matching dependency but no <version> element; continue searching
|
|
96
|
+
// (the version may be in <dependencyManagement>)
|
|
97
|
+
}
|
|
98
|
+
searchStart = blockEnd;
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Finds the text content position of a property element inside a `<properties>` block.
|
|
104
|
+
* Searches through all `<properties>` blocks in the file.
|
|
105
|
+
* @param {string} content - full raw pom.xml content
|
|
106
|
+
* @param {string} propName - the property element name to locate
|
|
107
|
+
* @returns {{start: number, end: number}|null} property value offsets, or null
|
|
108
|
+
*/
|
|
109
|
+
function findPropertyPosition(content, propName) {
|
|
110
|
+
let searchStart = 0;
|
|
111
|
+
const TAG_PREFIX = '<properties';
|
|
112
|
+
const CLOSE_TAG = '</properties>';
|
|
113
|
+
while (searchStart < content.length) {
|
|
114
|
+
const propsStart = content.indexOf(TAG_PREFIX, searchStart);
|
|
115
|
+
if (propsStart === -1) {
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
// Verify this is exactly <properties> and not some other tag
|
|
119
|
+
const charAfter = content.charAt(propsStart + TAG_PREFIX.length);
|
|
120
|
+
if (!isTagBoundary(charAfter)) {
|
|
121
|
+
searchStart = propsStart + 1;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const propsCloseStart = content.indexOf(CLOSE_TAG, propsStart);
|
|
125
|
+
if (propsCloseStart === -1) {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
const propsBlockEnd = propsCloseStart + CLOSE_TAG.length;
|
|
129
|
+
const result = findTagContentPosition(content, propsStart, propsBlockEnd, propName);
|
|
130
|
+
if (result) {
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
searchStart = propsBlockEnd;
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Resolves a Maven property chain, following `${propName}` references through
|
|
139
|
+
* the properties map until a concrete (non-reference) value is reached.
|
|
140
|
+
* Detects circular references via a visited set.
|
|
141
|
+
* @param {string} propName - the initial property name to resolve
|
|
142
|
+
* @param {Object} properties - properties map from the parsed XML
|
|
143
|
+
* @param {Set<string>} [visited] - visited property names (for cycle detection)
|
|
144
|
+
* @returns {{terminalPropName: string, resolvedValue: string}|{error: string}}
|
|
145
|
+
*/
|
|
146
|
+
function resolvePropertyChain(propName, properties, visited = new Set()) {
|
|
147
|
+
if (visited.has(propName)) {
|
|
148
|
+
const chain = [...visited, propName].join(' -> ');
|
|
149
|
+
return { error: `Circular property reference detected: ${chain}` };
|
|
150
|
+
}
|
|
151
|
+
visited.add(propName);
|
|
152
|
+
const value = properties[propName];
|
|
153
|
+
if (value == null) {
|
|
154
|
+
return { error: `Property '${propName}' not found in <properties>` };
|
|
155
|
+
}
|
|
156
|
+
const valueStr = String(value);
|
|
157
|
+
const propMatch = valueStr.match(/^\$\{(.+)\}$/);
|
|
158
|
+
if (propMatch) {
|
|
159
|
+
return resolvePropertyChain(propMatch[1], properties, visited);
|
|
160
|
+
}
|
|
161
|
+
return { terminalPropName: propName, resolvedValue: valueStr };
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Updates dependency versions in a Maven pom.xml while preserving file formatting.
|
|
165
|
+
*
|
|
166
|
+
* Uses `fast-xml-parser` to understand the XML structure (dependencies and properties),
|
|
167
|
+
* then performs position-based string splice to replace version strings without altering
|
|
168
|
+
* any other formatting, comments, whitespace, or indentation.
|
|
169
|
+
*
|
|
170
|
+
* Supports `${property}` indirection: when a dependency's `<version>` is a property
|
|
171
|
+
* reference, the updater resolves the property chain (including recursive references)
|
|
172
|
+
* and updates the terminal property value in `<properties>` instead.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} pomContent - the raw pom.xml file content
|
|
175
|
+
* @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges -
|
|
176
|
+
* list of version changes to apply
|
|
177
|
+
* @returns {{content: string, applied: Array, skipped: Array}} the updated content and
|
|
178
|
+
* lists of applied and skipped changes
|
|
179
|
+
*/
|
|
180
|
+
export function updateMavenVersions(pomContent, versionChanges) {
|
|
181
|
+
if (!versionChanges || versionChanges.length === 0) {
|
|
182
|
+
return { content: pomContent, applied: [], skipped: [] };
|
|
183
|
+
}
|
|
184
|
+
const parser = new XMLParser({
|
|
185
|
+
parseTagValue: false,
|
|
186
|
+
commentPropName: '#comment',
|
|
187
|
+
});
|
|
188
|
+
let parsed;
|
|
189
|
+
try {
|
|
190
|
+
parsed = parser.parse(pomContent);
|
|
191
|
+
}
|
|
192
|
+
catch (e) {
|
|
193
|
+
return {
|
|
194
|
+
content: pomContent,
|
|
195
|
+
applied: [],
|
|
196
|
+
skipped: versionChanges.map(vc => ({
|
|
197
|
+
groupId: vc.groupId,
|
|
198
|
+
artifactId: vc.artifactId,
|
|
199
|
+
newVersion: vc.newVersion,
|
|
200
|
+
reason: `Failed to parse pom.xml: ${e.message}`,
|
|
201
|
+
})),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
const project = parsed?.project;
|
|
205
|
+
if (!project) {
|
|
206
|
+
return {
|
|
207
|
+
content: pomContent,
|
|
208
|
+
applied: [],
|
|
209
|
+
skipped: versionChanges.map(vc => ({
|
|
210
|
+
groupId: vc.groupId,
|
|
211
|
+
artifactId: vc.artifactId,
|
|
212
|
+
newVersion: vc.newVersion,
|
|
213
|
+
reason: 'No <project> element found in pom.xml',
|
|
214
|
+
})),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const properties = project.properties || {};
|
|
218
|
+
const applied = [];
|
|
219
|
+
const skipped = [];
|
|
220
|
+
/** @type {Map<number, {start: number, end: number, newValue: string}>} */
|
|
221
|
+
const replacementsByPosition = new Map();
|
|
222
|
+
for (const change of versionChanges) {
|
|
223
|
+
const versionPos = findDependencyVersionPosition(pomContent, change.groupId, change.artifactId);
|
|
224
|
+
if (!versionPos) {
|
|
225
|
+
skipped.push({
|
|
226
|
+
groupId: change.groupId,
|
|
227
|
+
artifactId: change.artifactId,
|
|
228
|
+
newVersion: change.newVersion,
|
|
229
|
+
reason: 'Dependency not found in pom.xml or has no <version> element',
|
|
230
|
+
});
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
const currentVersion = pomContent.substring(versionPos.start, versionPos.end);
|
|
234
|
+
const propMatch = currentVersion.match(/^\$\{(.+)\}$/);
|
|
235
|
+
if (propMatch) {
|
|
236
|
+
const resolved = resolvePropertyChain(propMatch[1], properties);
|
|
237
|
+
if (resolved.error) {
|
|
238
|
+
skipped.push({
|
|
239
|
+
groupId: change.groupId,
|
|
240
|
+
artifactId: change.artifactId,
|
|
241
|
+
newVersion: change.newVersion,
|
|
242
|
+
reason: resolved.error,
|
|
243
|
+
});
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const propPos = findPropertyPosition(pomContent, resolved.terminalPropName);
|
|
247
|
+
if (!propPos) {
|
|
248
|
+
skipped.push({
|
|
249
|
+
groupId: change.groupId,
|
|
250
|
+
artifactId: change.artifactId,
|
|
251
|
+
newVersion: change.newVersion,
|
|
252
|
+
reason: `Could not locate property '${resolved.terminalPropName}' in pom.xml`,
|
|
253
|
+
});
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
// Deduplicate: multiple deps may share the same property
|
|
257
|
+
const existing = replacementsByPosition.get(propPos.start);
|
|
258
|
+
if (existing) {
|
|
259
|
+
if (existing.newValue !== change.newVersion) {
|
|
260
|
+
skipped.push({
|
|
261
|
+
groupId: change.groupId,
|
|
262
|
+
artifactId: change.artifactId,
|
|
263
|
+
newVersion: change.newVersion,
|
|
264
|
+
reason: `Property '${resolved.terminalPropName}' already targeted `
|
|
265
|
+
+ `with a different version '${existing.newValue}'`,
|
|
266
|
+
});
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
// Same replacement already queued; record as applied without duplicating
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
replacementsByPosition.set(propPos.start, {
|
|
273
|
+
start: propPos.start,
|
|
274
|
+
end: propPos.end,
|
|
275
|
+
newValue: change.newVersion,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
applied.push({
|
|
279
|
+
groupId: change.groupId,
|
|
280
|
+
artifactId: change.artifactId,
|
|
281
|
+
newVersion: change.newVersion,
|
|
282
|
+
type: 'property',
|
|
283
|
+
property: resolved.terminalPropName,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
const existing = replacementsByPosition.get(versionPos.start);
|
|
288
|
+
if (existing) {
|
|
289
|
+
if (existing.newValue !== change.newVersion) {
|
|
290
|
+
skipped.push({
|
|
291
|
+
groupId: change.groupId,
|
|
292
|
+
artifactId: change.artifactId,
|
|
293
|
+
newVersion: change.newVersion,
|
|
294
|
+
reason: `Version already targeted with a different version '${existing.newValue}'`,
|
|
295
|
+
});
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
replacementsByPosition.set(versionPos.start, {
|
|
301
|
+
start: versionPos.start,
|
|
302
|
+
end: versionPos.end,
|
|
303
|
+
newValue: change.newVersion,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
applied.push({
|
|
307
|
+
groupId: change.groupId,
|
|
308
|
+
artifactId: change.artifactId,
|
|
309
|
+
newVersion: change.newVersion,
|
|
310
|
+
type: 'direct',
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
// Sort replacements by position descending to preserve offsets during splice
|
|
315
|
+
const sortedReplacements = [...replacementsByPosition.values()]
|
|
316
|
+
.sort((a, b) => b.start - a.start);
|
|
317
|
+
let result = pomContent;
|
|
318
|
+
for (const r of sortedReplacements) {
|
|
319
|
+
result = result.substring(0, r.start) + r.newValue + result.substring(r.end);
|
|
320
|
+
}
|
|
321
|
+
return { content: result, applied, skipped };
|
|
322
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve ignore globs for workspace discovery: defaults + `TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE` + `opts.workspaceDiscoveryIgnore`.
|
|
3
|
+
* Patterns are fast-glob / micromatch style, relative to the workspace root (forward slashes).
|
|
4
|
+
*
|
|
5
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}]
|
|
6
|
+
* @returns {string[]}
|
|
7
|
+
*/
|
|
8
|
+
export function resolveWorkspaceDiscoveryIgnore(opts?: {
|
|
9
|
+
workspaceDiscoveryIgnore?: string[];
|
|
10
|
+
TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}): string[];
|
|
13
|
+
/**
|
|
14
|
+
* Drop manifest paths whose location matches an ignore pattern (e.g. root unshift, Cargo paths).
|
|
15
|
+
*
|
|
16
|
+
* @param {string[]} manifestPaths
|
|
17
|
+
* @param {string} root
|
|
18
|
+
* @param {string[]} ignorePatterns
|
|
19
|
+
* @returns {string[]}
|
|
20
|
+
*/
|
|
21
|
+
export function filterManifestPathsByDiscoveryIgnore(manifestPaths: string[], root: string, ignorePatterns: string[]): string[];
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {{ valid: true, name: string, version: string } | { valid: false, error: string }} ValidatePackageJsonResult
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Validate a package.json has non-empty `name` and `version` (required for stable SBOM root identity in batch).
|
|
27
|
+
*
|
|
28
|
+
* @param {string} packageJsonPath - Absolute or relative path to package.json
|
|
29
|
+
* @returns {ValidatePackageJsonResult}
|
|
30
|
+
*/
|
|
31
|
+
export function validatePackageJson(packageJsonPath: string): ValidatePackageJsonResult;
|
|
32
|
+
/**
|
|
33
|
+
* Discover all package.json paths in a JS/TS workspace.
|
|
34
|
+
* Reads pnpm-workspace.yaml or package.json workspaces.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root
|
|
37
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}] - optional `workspaceDiscoveryIgnore` globs (merged with defaults and env)
|
|
38
|
+
* @returns {Promise<string[]>} Paths to package.json files (absolute)
|
|
39
|
+
*/
|
|
40
|
+
export function discoverWorkspacePackages(workspaceRoot: string, opts?: {
|
|
41
|
+
workspaceDiscoveryIgnore?: string[];
|
|
42
|
+
TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string;
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
}): Promise<string[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Convert workspace glob patterns to manifest-file glob patterns,
|
|
47
|
+
* correctly handling negation prefixes.
|
|
48
|
+
*
|
|
49
|
+
* @param {string[]} patterns - Workspace glob patterns (may include negations)
|
|
50
|
+
* @param {string} manifestFileName - e.g. 'package.json' or 'Cargo.toml'
|
|
51
|
+
* @returns {string[]}
|
|
52
|
+
*/
|
|
53
|
+
export function toManifestGlobPatterns(patterns: string[], manifestFileName: string): string[];
|
|
54
|
+
/**
|
|
55
|
+
* Discover all Cargo.toml manifest paths in a Cargo workspace.
|
|
56
|
+
* Uses `cargo metadata` to get workspace members.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain Cargo.toml and Cargo.lock)
|
|
59
|
+
* @param {import('./index.js').Options} [opts={}]
|
|
60
|
+
* @returns {Promise<string[]>} Paths to Cargo.toml files (absolute)
|
|
61
|
+
*/
|
|
62
|
+
export function discoverWorkspaceCrates(workspaceRoot: string, opts?: import("./index.js").Options): Promise<string[]>;
|
|
63
|
+
export type ValidatePackageJsonResult = {
|
|
64
|
+
valid: true;
|
|
65
|
+
name: string;
|
|
66
|
+
version: string;
|
|
67
|
+
} | {
|
|
68
|
+
valid: false;
|
|
69
|
+
error: string;
|
|
70
|
+
};
|