@trustify-da/trustify-da-javascript-client 0.3.0-ea.e660b02 → 0.3.0-ea.efdc87d

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 (77) hide show
  1. package/README.md +199 -11
  2. package/dist/package.json +29 -25
  3. package/dist/src/analysis.d.ts +50 -22
  4. package/dist/src/analysis.js +70 -18
  5. package/dist/src/cli.js +188 -6
  6. package/dist/src/config.d.ts +120 -0
  7. package/dist/src/config.js +260 -0
  8. package/dist/src/cyclone_dx_sbom.d.ts +13 -0
  9. package/dist/src/cyclone_dx_sbom.js +39 -1
  10. package/dist/src/index.d.ts +27 -22
  11. package/dist/src/index.js +27 -38
  12. package/dist/src/license/index.d.ts +4 -4
  13. package/dist/src/license/index.js +13 -7
  14. package/dist/src/license/license_utils.js +4 -1
  15. package/dist/src/license/licenses_api.d.ts +3 -3
  16. package/dist/src/license/licenses_api.js +1 -1
  17. package/dist/src/oci_image/images.d.ts +1 -1
  18. package/dist/src/oci_image/images.js +15 -1
  19. package/dist/src/oci_image/utils.d.ts +5 -5
  20. package/dist/src/package_version.d.ts +8 -0
  21. package/dist/src/package_version.js +31 -0
  22. package/dist/src/provider.d.ts +2 -2
  23. package/dist/src/provider.js +3 -1
  24. package/dist/src/providers/base_java.d.ts +55 -2
  25. package/dist/src/providers/base_java.js +64 -17
  26. package/dist/src/providers/base_javascript.d.ts +63 -9
  27. package/dist/src/providers/base_javascript.js +106 -4
  28. package/dist/src/providers/base_pyproject.d.ts +5 -5
  29. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  30. package/dist/src/providers/containerfile_parser.js +20 -0
  31. package/dist/src/providers/golang_gomodules.d.ts +1 -1
  32. package/dist/src/providers/golang_gomodules.js +49 -7
  33. package/dist/src/providers/java_gradle.d.ts +48 -0
  34. package/dist/src/providers/java_gradle.js +196 -18
  35. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  36. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  37. package/dist/src/providers/java_maven.d.ts +29 -9
  38. package/dist/src/providers/java_maven.js +104 -10
  39. package/dist/src/providers/javascript_bun.d.ts +12 -0
  40. package/dist/src/providers/javascript_bun.js +42 -1
  41. package/dist/src/providers/javascript_npm.d.ts +19 -1
  42. package/dist/src/providers/javascript_npm.js +40 -1
  43. package/dist/src/providers/javascript_pnpm.d.ts +13 -0
  44. package/dist/src/providers/javascript_pnpm.js +47 -1
  45. package/dist/src/providers/javascript_yarn.d.ts +12 -0
  46. package/dist/src/providers/javascript_yarn.js +75 -2
  47. package/dist/src/providers/manifest.js +6 -3
  48. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  49. package/dist/src/providers/oci_dockerfile.js +177 -0
  50. package/dist/src/providers/processors/yarn_berry_processor.d.ts +6 -2
  51. package/dist/src/providers/processors/yarn_berry_processor.js +3 -2
  52. package/dist/src/providers/processors/yarn_classic_processor.d.ts +6 -2
  53. package/dist/src/providers/processors/yarn_classic_processor.js +8 -6
  54. package/dist/src/providers/python_controller.js +4 -1
  55. package/dist/src/providers/python_poetry.d.ts +1 -1
  56. package/dist/src/providers/python_uv.d.ts +1 -1
  57. package/dist/src/providers/requirements_parser.js +1 -1
  58. package/dist/src/providers/rust_cargo.d.ts +1 -1
  59. package/dist/src/providers/rust_cargo.js +70 -29
  60. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  61. package/dist/src/remediate.d.ts +120 -0
  62. package/dist/src/remediate.js +253 -0
  63. package/dist/src/remediation.d.ts +58 -0
  64. package/dist/src/remediation.js +425 -0
  65. package/dist/src/remediation_report.d.ts +40 -0
  66. package/dist/src/remediation_report.js +159 -0
  67. package/dist/src/sbom.d.ts +11 -0
  68. package/dist/src/sbom.js +10 -0
  69. package/dist/src/tools.d.ts +11 -13
  70. package/dist/src/tools.js +34 -13
  71. package/dist/src/updaters/maven_updater.d.ts +58 -0
  72. package/dist/src/updaters/maven_updater.js +345 -0
  73. package/dist/src/updaters/toml_updater.d.ts +62 -0
  74. package/dist/src/updaters/toml_updater.js +276 -0
  75. package/dist/src/workspace.d.ts +2 -1
  76. package/dist/src/workspace.js +2 -1
  77. package/package.json +30 -26
@@ -6,24 +6,22 @@
6
6
  */
7
7
  export function logValueFromObjects(key: string, opts?: {}, defValue: string): void;
8
8
  /**
9
- * Utility function will return the value for key from the environment variables,
10
- * if not present will return the value for key from the opts objects only if it's a string,
11
- * if not present, or not string will return the default value supplied which default to null.
9
+ * Utility function returns the value for key from opts, then environment variables,
10
+ * then the supplied default. Values from opts are used only if they are strings.
12
11
  * @param {string} key the key to look for in the environment variables and the opts object
13
12
  * @param {string|null} [def=null] the value to return if nothing else found
14
- * @param {{}} [opts={}] the options object to look for the key in if not found in environment
15
- * @returns {string|null} the value of the key found in the environment, options object, or the
13
+ * @param {{}} [opts={}] the options object to check before the environment
14
+ * @returns {string|null} the value of the key found in the options object, environment, or the
16
15
  * default supplied
17
16
  */
18
17
  export function getCustom(key: string, def?: string | null, opts?: {}): string | null;
19
18
  /**
20
19
  * Utility function for looking up custom variable for a binary path.
21
- * Will look in the environment variables (1) or in opts (2) for a key with TRUSTIFY_DA_x_PATH, x is an
22
- * uppercase version of passed name to look for. The name will also be returned if nothing else was
23
- * found.
20
+ * Looks in opts, then environment variables, for a key with TRUSTIFY_DA_x_PATH, where x is an
21
+ * uppercase version of the supplied name. The name is returned if neither contains the key.
24
22
  * @param name the binary name to look for, will be returned as value in nothing else found
25
- * @param {{}} [opts={}] the options object to look for the key in if not found in environment
26
- * @returns {string|null} the value of the key found in the environment, options object, or the
23
+ * @param {{}} [opts={}] the options object to check before the environment
24
+ * @returns {string|null} the value of the key found in the options object, environment, or the
27
25
  * original name supplied
28
26
  */
29
27
  export function getCustomPath(name: any, opts?: {}): string | null;
@@ -31,7 +29,7 @@ export function getCustomPath(name: any, opts?: {}): string | null;
31
29
  * Utility function for determining whether wrappers for build tools such as gradlew/mvnw should be
32
30
  * preferred over invoking the binary directly.
33
31
  * @param {string} name - binary for which to search for its wrapper
34
- * @param {{}} opts - the options object to look for the key in if not found in environment
32
+ * @param {{}} opts - the options object to check before the environment
35
33
  * @returns {boolean} whether to prefer the wrapper if exists or not
36
34
  */
37
35
  export function getWrapperPreference(name: string, opts?: {}): boolean;
@@ -100,13 +98,13 @@ export function invokeCommand(bin: string, args: Array<string>, opts?: import("c
100
98
  * @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
101
99
  * @returns {RequestInit} The fetch options with proxy agent if applicable
102
100
  */
103
- export function addProxyAgent(options: RequestInit, opts: import("index.js").Options): RequestInit;
101
+ export function addProxyAgent(options: RequestInit, opts: any): RequestInit;
104
102
  /**
105
103
  * Utility function for fetching vendor tokens
106
104
  * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
107
105
  * @returns {{}}
108
106
  */
109
- export function getTokenHeaders(opts?: import("index.js").Options): {};
107
+ export function getTokenHeaders(opts?: any): {};
110
108
  export const RegexNotToBeLogged: RegExp;
111
109
  export const TRUSTIFY_DA_TOKEN_HEADER: "trust-da-token";
112
110
  export const TRUSTIFY_DA_TELEMETRY_ID_HEADER: "telemetry-anonymous-id";
package/dist/src/tools.js CHANGED
@@ -27,39 +27,60 @@ export function logValueFromObjects(key, opts, defValue) {
27
27
  console.log(`default value for ${key} = ${defValue} ${EOL}`);
28
28
  }
29
29
  /**
30
- * Utility function will return the value for key from the environment variables,
31
- * if not present will return the value for key from the opts objects only if it's a string,
32
- * if not present, or not string will return the default value supplied which default to null.
30
+ * Utility function returns the value for key from opts, then environment variables,
31
+ * then the supplied default. Values from opts are used only if they are strings.
33
32
  * @param {string} key the key to look for in the environment variables and the opts object
34
33
  * @param {string|null} [def=null] the value to return if nothing else found
35
- * @param {{}} [opts={}] the options object to look for the key in if not found in environment
36
- * @returns {string|null} the value of the key found in the environment, options object, or the
34
+ * @param {{}} [opts={}] the options object to check before the environment
35
+ * @returns {string|null} the value of the key found in the options object, environment, or the
37
36
  * default supplied
38
37
  */
39
38
  export function getCustom(key, def = null, opts = {}) {
40
39
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true" && !key.match(RegexNotToBeLogged)) {
41
40
  logValueFromObjects(key, opts, def);
42
41
  }
43
- return key in process.env ? process.env[key] : key in opts && typeof opts[key] === 'string' ? opts[key] : def;
42
+ return key in opts && typeof opts[key] === 'string' ? opts[key] : key in process.env ? process.env[key] : def;
43
+ }
44
+ /**
45
+ * Validates that an executable path does not use directory traversal or relative segments.
46
+ * @param {string} binPath - The executable path to validate.
47
+ * @returns {string} The validated path.
48
+ * @throws {Error} If the path contains '..' segments or is a relative path with separators.
49
+ */
50
+ function validateExecutablePath(binPath) {
51
+ if (typeof binPath !== 'string' || binPath.length === 0) {
52
+ throw new Error('Executable path rejected: expected a non-empty string');
53
+ }
54
+ if (binPath.startsWith('./') || binPath.startsWith('.\\')) {
55
+ throw new Error(`Executable path rejected: relative paths starting with './' are not allowed: ${binPath}`);
56
+ }
57
+ const segments = binPath.split(/[/\\]/);
58
+ if (segments.includes('..')) {
59
+ throw new Error(`Executable path rejected: path contains directory traversal segment (..): ${binPath}`);
60
+ }
61
+ if ((binPath.includes('/') || binPath.includes('\\')) && !path.isAbsolute(binPath)) {
62
+ throw new Error(`Executable path rejected: relative paths are not allowed, use an absolute path or a bare command name: ${binPath}`);
63
+ }
64
+ return binPath;
44
65
  }
45
66
  /**
46
67
  * Utility function for looking up custom variable for a binary path.
47
- * Will look in the environment variables (1) or in opts (2) for a key with TRUSTIFY_DA_x_PATH, x is an
48
- * uppercase version of passed name to look for. The name will also be returned if nothing else was
49
- * found.
68
+ * Looks in opts, then environment variables, for a key with TRUSTIFY_DA_x_PATH, where x is an
69
+ * uppercase version of the supplied name. The name is returned if neither contains the key.
50
70
  * @param name the binary name to look for, will be returned as value in nothing else found
51
- * @param {{}} [opts={}] the options object to look for the key in if not found in environment
52
- * @returns {string|null} the value of the key found in the environment, options object, or the
71
+ * @param {{}} [opts={}] the options object to check before the environment
72
+ * @returns {string|null} the value of the key found in the options object, environment, or the
53
73
  * original name supplied
54
74
  */
55
75
  export function getCustomPath(name, opts = {}) {
56
- return getCustom(`TRUSTIFY_DA_${name.toUpperCase()}_PATH`, name, opts);
76
+ const resolvedPath = getCustom(`TRUSTIFY_DA_${name.toUpperCase()}_PATH`, name, opts);
77
+ return validateExecutablePath(resolvedPath);
57
78
  }
58
79
  /**
59
80
  * Utility function for determining whether wrappers for build tools such as gradlew/mvnw should be
60
81
  * preferred over invoking the binary directly.
61
82
  * @param {string} name - binary for which to search for its wrapper
62
- * @param {{}} opts - the options object to look for the key in if not found in environment
83
+ * @param {{}} opts - the options object to check before the environment
63
84
  * @returns {boolean} whether to prefer the wrapper if exists or not
64
85
  */
65
86
  export function getWrapperPreference(name, opts = {}) {
@@ -0,0 +1,58 @@
1
+ /**
2
+ * One entry in {@link updateMavenVersions}' `applied` list: a version that was changed and
3
+ * where. `type` discriminates the edit site — `'direct'` for a dependency's own `<version>`,
4
+ * `'property'` for a `${property}` reference (then `property` names the resolved property).
5
+ * @typedef {{
6
+ * groupId: string,
7
+ * artifactId: string,
8
+ * newVersion: string,
9
+ * type: ('direct'|'property'),
10
+ * property?: string
11
+ * }} MavenAppliedChange
12
+ */
13
+ /**
14
+ * Stable edit-site key for a Maven change: same key => same commit/PR. Deps whose versions
15
+ * resolve to one `${property}` share the property key and are therefore inseparable.
16
+ * @param {string} manifestPath
17
+ * @param {MavenAppliedChange} applied
18
+ * @returns {string}
19
+ */
20
+ export function mavenChangeKey(manifestPath: string, applied: MavenAppliedChange): string;
21
+ /**
22
+ * Updates dependency versions in a Maven pom.xml while preserving file formatting.
23
+ *
24
+ * Uses `fast-xml-parser` to understand the XML structure (dependencies and properties),
25
+ * then performs position-based string splice to replace version strings without altering
26
+ * any other formatting, comments, whitespace, or indentation.
27
+ *
28
+ * Supports `${property}` indirection: when a dependency's `<version>` is a property
29
+ * reference, the updater resolves the property chain (including recursive references)
30
+ * and updates the terminal property value in `<properties>` instead.
31
+ *
32
+ * @param {string} pomContent - the raw pom.xml file content
33
+ * @param {import('../remediate.js').VersionChangeRequest[]} versionChanges - list of version changes to apply
34
+ * @returns {{content: string, applied: MavenAppliedChange[], skipped: Array<{groupId: string, artifactId: string, newVersion: string, reason: string}>}} the updated content and
35
+ * lists of applied and skipped changes
36
+ */
37
+ export function updateMavenVersions(pomContent: string, versionChanges: import("../remediate.js").VersionChangeRequest[]): {
38
+ content: string;
39
+ applied: MavenAppliedChange[];
40
+ skipped: Array<{
41
+ groupId: string;
42
+ artifactId: string;
43
+ newVersion: string;
44
+ reason: string;
45
+ }>;
46
+ };
47
+ /**
48
+ * One entry in {@link updateMavenVersions}' `applied` list: a version that was changed and
49
+ * where. `type` discriminates the edit site — `'direct'` for a dependency's own `<version>`,
50
+ * `'property'` for a `${property}` reference (then `property` names the resolved property).
51
+ */
52
+ export type MavenAppliedChange = {
53
+ groupId: string;
54
+ artifactId: string;
55
+ newVersion: string;
56
+ type: ("direct" | "property");
57
+ property?: string;
58
+ };
@@ -0,0 +1,345 @@
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
+ * One entry in {@link updateMavenVersions}' `applied` list: a version that was changed and
165
+ * where. `type` discriminates the edit site — `'direct'` for a dependency's own `<version>`,
166
+ * `'property'` for a `${property}` reference (then `property` names the resolved property).
167
+ * @typedef {{
168
+ * groupId: string,
169
+ * artifactId: string,
170
+ * newVersion: string,
171
+ * type: ('direct'|'property'),
172
+ * property?: string
173
+ * }} MavenAppliedChange
174
+ */
175
+ /**
176
+ * Stable edit-site key for a Maven change: same key => same commit/PR. Deps whose versions
177
+ * resolve to one `${property}` share the property key and are therefore inseparable.
178
+ * @param {string} manifestPath
179
+ * @param {MavenAppliedChange} applied
180
+ * @returns {string}
181
+ */
182
+ export function mavenChangeKey(manifestPath, applied) {
183
+ return applied.type === 'property'
184
+ ? `mvn:prop:${manifestPath}:${applied.property}`
185
+ : `mvn:direct:${manifestPath}:${applied.groupId}:${applied.artifactId}`;
186
+ }
187
+ /**
188
+ * Updates dependency versions in a Maven pom.xml while preserving file formatting.
189
+ *
190
+ * Uses `fast-xml-parser` to understand the XML structure (dependencies and properties),
191
+ * then performs position-based string splice to replace version strings without altering
192
+ * any other formatting, comments, whitespace, or indentation.
193
+ *
194
+ * Supports `${property}` indirection: when a dependency's `<version>` is a property
195
+ * reference, the updater resolves the property chain (including recursive references)
196
+ * and updates the terminal property value in `<properties>` instead.
197
+ *
198
+ * @param {string} pomContent - the raw pom.xml file content
199
+ * @param {import('../remediate.js').VersionChangeRequest[]} versionChanges - list of version changes to apply
200
+ * @returns {{content: string, applied: MavenAppliedChange[], skipped: Array<{groupId: string, artifactId: string, newVersion: string, reason: string}>}} the updated content and
201
+ * lists of applied and skipped changes
202
+ */
203
+ export function updateMavenVersions(pomContent, versionChanges) {
204
+ if (!versionChanges || versionChanges.length === 0) {
205
+ return { content: pomContent, applied: [], skipped: [] };
206
+ }
207
+ const parser = new XMLParser({
208
+ parseTagValue: false,
209
+ commentPropName: '#comment',
210
+ });
211
+ let parsed;
212
+ try {
213
+ parsed = parser.parse(pomContent);
214
+ }
215
+ catch (e) {
216
+ return {
217
+ content: pomContent,
218
+ applied: [],
219
+ skipped: versionChanges.map(vc => ({
220
+ groupId: vc.groupId,
221
+ artifactId: vc.artifactId,
222
+ newVersion: vc.newVersion,
223
+ reason: `Failed to parse pom.xml: ${e.message}`,
224
+ })),
225
+ };
226
+ }
227
+ const project = parsed?.project;
228
+ if (!project) {
229
+ return {
230
+ content: pomContent,
231
+ applied: [],
232
+ skipped: versionChanges.map(vc => ({
233
+ groupId: vc.groupId,
234
+ artifactId: vc.artifactId,
235
+ newVersion: vc.newVersion,
236
+ reason: 'No <project> element found in pom.xml',
237
+ })),
238
+ };
239
+ }
240
+ const properties = project.properties || {};
241
+ const applied = [];
242
+ const skipped = [];
243
+ /** @type {Map<number, {start: number, end: number, newValue: string}>} */
244
+ const replacementsByPosition = new Map();
245
+ for (const change of versionChanges) {
246
+ const versionPos = findDependencyVersionPosition(pomContent, change.groupId, change.artifactId);
247
+ if (!versionPos) {
248
+ skipped.push({
249
+ groupId: change.groupId,
250
+ artifactId: change.artifactId,
251
+ newVersion: change.newVersion,
252
+ reason: 'Dependency not found in pom.xml or has no <version> element',
253
+ });
254
+ continue;
255
+ }
256
+ const currentVersion = pomContent.substring(versionPos.start, versionPos.end);
257
+ const propMatch = currentVersion.match(/^\$\{(.+)\}$/);
258
+ if (propMatch) {
259
+ const resolved = resolvePropertyChain(propMatch[1], properties);
260
+ if (resolved.error) {
261
+ skipped.push({
262
+ groupId: change.groupId,
263
+ artifactId: change.artifactId,
264
+ newVersion: change.newVersion,
265
+ reason: resolved.error,
266
+ });
267
+ continue;
268
+ }
269
+ const propPos = findPropertyPosition(pomContent, resolved.terminalPropName);
270
+ if (!propPos) {
271
+ skipped.push({
272
+ groupId: change.groupId,
273
+ artifactId: change.artifactId,
274
+ newVersion: change.newVersion,
275
+ reason: `Could not locate property '${resolved.terminalPropName}' in pom.xml`,
276
+ });
277
+ continue;
278
+ }
279
+ // Deduplicate: multiple deps may share the same property
280
+ const existing = replacementsByPosition.get(propPos.start);
281
+ if (existing) {
282
+ if (existing.newValue !== change.newVersion) {
283
+ skipped.push({
284
+ groupId: change.groupId,
285
+ artifactId: change.artifactId,
286
+ newVersion: change.newVersion,
287
+ reason: `Property '${resolved.terminalPropName}' already targeted `
288
+ + `with a different version '${existing.newValue}'`,
289
+ });
290
+ continue;
291
+ }
292
+ // Same replacement already queued; record as applied without duplicating
293
+ }
294
+ else {
295
+ replacementsByPosition.set(propPos.start, {
296
+ start: propPos.start,
297
+ end: propPos.end,
298
+ newValue: change.newVersion,
299
+ });
300
+ }
301
+ applied.push({
302
+ groupId: change.groupId,
303
+ artifactId: change.artifactId,
304
+ newVersion: change.newVersion,
305
+ type: 'property',
306
+ property: resolved.terminalPropName,
307
+ });
308
+ }
309
+ else {
310
+ const existing = replacementsByPosition.get(versionPos.start);
311
+ if (existing) {
312
+ if (existing.newValue !== change.newVersion) {
313
+ skipped.push({
314
+ groupId: change.groupId,
315
+ artifactId: change.artifactId,
316
+ newVersion: change.newVersion,
317
+ reason: `Version already targeted with a different version '${existing.newValue}'`,
318
+ });
319
+ continue;
320
+ }
321
+ }
322
+ else {
323
+ replacementsByPosition.set(versionPos.start, {
324
+ start: versionPos.start,
325
+ end: versionPos.end,
326
+ newValue: change.newVersion,
327
+ });
328
+ }
329
+ applied.push({
330
+ groupId: change.groupId,
331
+ artifactId: change.artifactId,
332
+ newVersion: change.newVersion,
333
+ type: 'direct',
334
+ });
335
+ }
336
+ }
337
+ // Sort replacements by position descending to preserve offsets during splice
338
+ const sortedReplacements = [...replacementsByPosition.values()]
339
+ .sort((a, b) => b.start - a.start);
340
+ let result = pomContent;
341
+ for (const r of sortedReplacements) {
342
+ result = result.substring(0, r.start) + r.newValue + result.substring(r.end);
343
+ }
344
+ return { content: result, applied, skipped };
345
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * One entry in {@link updateTomlVersions}' `applied` list: a version that was changed and where.
3
+ * `type` discriminates the edit site — `'ref'` for a `[libraries]` entry pointing at a shared
4
+ * `[versions]` alias via `version.ref` (then `versionRef` names that alias), `'inline'` for a
5
+ * version declared directly on the library. `alias` is the library's catalog key in both cases.
6
+ * @typedef {{
7
+ * groupId: string,
8
+ * artifactId: string,
9
+ * newVersion: string,
10
+ * oldVersion: string,
11
+ * type: ('ref'|'inline'),
12
+ * alias: string,
13
+ * versionRef?: string
14
+ * }} TomlAppliedChange
15
+ */
16
+ /**
17
+ * Stable edit-site key for a TOML (Gradle version catalog) change: same key => same commit/PR.
18
+ * Deps sharing one `[versions]` alias via `version.ref` share the ref key and are inseparable.
19
+ * @param {string} manifestPath
20
+ * @param {TomlAppliedChange} applied
21
+ * @returns {string}
22
+ */
23
+ export function tomlChangeKey(manifestPath: string, applied: TomlAppliedChange): string;
24
+ /**
25
+ * Updates dependency versions in a Gradle version catalog (libs.versions.toml) file.
26
+ *
27
+ * Supports two version declaration patterns:
28
+ * - Centralized: [versions] section defines version aliases, [libraries] references via version.ref
29
+ * - Inline: [libraries] entries include version = "x.y.z" directly
30
+ *
31
+ * Uses smol-toml parse() to locate keys, then performs position-based string replacement
32
+ * on the raw content to preserve formatting and comments.
33
+ *
34
+ * @param {string} tomlContent - raw TOML file content
35
+ * @param {import('../remediate.js').VersionChangeRequest[]} versionChanges
36
+ * @returns {{content: string, applied: TomlAppliedChange[], skipped: Array<{groupId: string, artifactId: string, newVersion: string, reason: string}>}}
37
+ */
38
+ export function updateTomlVersions(tomlContent: string, versionChanges: import("../remediate.js").VersionChangeRequest[]): {
39
+ content: string;
40
+ applied: TomlAppliedChange[];
41
+ skipped: Array<{
42
+ groupId: string;
43
+ artifactId: string;
44
+ newVersion: string;
45
+ reason: string;
46
+ }>;
47
+ };
48
+ /**
49
+ * One entry in {@link updateTomlVersions}' `applied` list: a version that was changed and where.
50
+ * `type` discriminates the edit site — `'ref'` for a `[libraries]` entry pointing at a shared
51
+ * `[versions]` alias via `version.ref` (then `versionRef` names that alias), `'inline'` for a
52
+ * version declared directly on the library. `alias` is the library's catalog key in both cases.
53
+ */
54
+ export type TomlAppliedChange = {
55
+ groupId: string;
56
+ artifactId: string;
57
+ newVersion: string;
58
+ oldVersion: string;
59
+ type: ("ref" | "inline");
60
+ alias: string;
61
+ versionRef?: string;
62
+ };