@trustify-da/trustify-da-javascript-client 0.3.0-ea.608d6fa → 0.3.0-ea.62b88e5

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 (94) hide show
  1. package/README.md +326 -21
  2. package/dist/package.json +36 -27
  3. package/dist/src/analysis.d.ts +58 -14
  4. package/dist/src/analysis.js +118 -17
  5. package/dist/src/batch_opts.d.ts +24 -0
  6. package/dist/src/batch_opts.js +35 -0
  7. package/dist/src/cli.js +357 -8
  8. package/dist/src/config.d.ts +120 -0
  9. package/dist/src/config.js +260 -0
  10. package/dist/src/cyclone_dx_sbom.d.ts +27 -1
  11. package/dist/src/cyclone_dx_sbom.js +84 -10
  12. package/dist/src/index.d.ts +151 -16
  13. package/dist/src/index.js +369 -36
  14. package/dist/src/license/index.d.ts +4 -4
  15. package/dist/src/license/index.js +13 -7
  16. package/dist/src/license/license_utils.js +4 -1
  17. package/dist/src/license/licenses_api.d.ts +3 -3
  18. package/dist/src/license/licenses_api.js +1 -1
  19. package/dist/src/oci_image/images.d.ts +1 -1
  20. package/dist/src/oci_image/images.js +15 -1
  21. package/dist/src/oci_image/utils.d.ts +5 -5
  22. package/dist/src/oci_image/utils.js +11 -2
  23. package/dist/src/package_version.d.ts +8 -0
  24. package/dist/src/package_version.js +31 -0
  25. package/dist/src/provider.d.ts +8 -4
  26. package/dist/src/provider.js +16 -5
  27. package/dist/src/providers/base_java.d.ts +67 -12
  28. package/dist/src/providers/base_java.js +82 -61
  29. package/dist/src/providers/base_javascript.d.ts +91 -10
  30. package/dist/src/providers/base_javascript.js +214 -24
  31. package/dist/src/providers/base_pyproject.d.ts +158 -0
  32. package/dist/src/providers/base_pyproject.js +322 -0
  33. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  34. package/dist/src/providers/containerfile_parser.js +20 -0
  35. package/dist/src/providers/golang_gomodules.d.ts +23 -13
  36. package/dist/src/providers/golang_gomodules.js +208 -120
  37. package/dist/src/providers/gomod_parser.d.ts +4 -0
  38. package/dist/src/providers/gomod_parser.js +16 -0
  39. package/dist/src/providers/java_gradle.d.ts +67 -0
  40. package/dist/src/providers/java_gradle.js +320 -22
  41. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  42. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  43. package/dist/src/providers/java_maven.d.ts +37 -9
  44. package/dist/src/providers/java_maven.js +269 -11
  45. package/dist/src/providers/javascript_bun.d.ts +22 -0
  46. package/dist/src/providers/javascript_bun.js +141 -0
  47. package/dist/src/providers/javascript_npm.d.ts +19 -0
  48. package/dist/src/providers/javascript_npm.js +61 -1
  49. package/dist/src/providers/javascript_pnpm.d.ts +14 -1
  50. package/dist/src/providers/javascript_pnpm.js +55 -5
  51. package/dist/src/providers/javascript_yarn.d.ts +12 -0
  52. package/dist/src/providers/javascript_yarn.js +75 -2
  53. package/dist/src/providers/manifest.d.ts +2 -0
  54. package/dist/src/providers/manifest.js +28 -7
  55. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  56. package/dist/src/providers/marker_evaluator.js +191 -0
  57. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  58. package/dist/src/providers/oci_dockerfile.js +177 -0
  59. package/dist/src/providers/processors/yarn_berry_processor.d.ts +6 -2
  60. package/dist/src/providers/processors/yarn_berry_processor.js +91 -7
  61. package/dist/src/providers/processors/yarn_classic_processor.d.ts +6 -2
  62. package/dist/src/providers/processors/yarn_classic_processor.js +8 -6
  63. package/dist/src/providers/python_controller.d.ts +5 -1
  64. package/dist/src/providers/python_controller.js +69 -8
  65. package/dist/src/providers/python_pip.d.ts +5 -0
  66. package/dist/src/providers/python_pip.js +6 -6
  67. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  68. package/dist/src/providers/python_pip_pyproject.js +146 -0
  69. package/dist/src/providers/python_poetry.d.ts +98 -0
  70. package/dist/src/providers/python_poetry.js +286 -0
  71. package/dist/src/providers/python_uv.d.ts +46 -0
  72. package/dist/src/providers/python_uv.js +275 -0
  73. package/dist/src/providers/requirements_parser.js +1 -1
  74. package/dist/src/providers/rust_cargo.d.ts +12 -3
  75. package/dist/src/providers/rust_cargo.js +107 -31
  76. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  77. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  78. package/dist/src/remediate.d.ts +107 -0
  79. package/dist/src/remediate.js +244 -0
  80. package/dist/src/remediation.d.ts +81 -0
  81. package/dist/src/remediation.js +475 -0
  82. package/dist/src/remediation_report.d.ts +23 -0
  83. package/dist/src/remediation_report.js +180 -0
  84. package/dist/src/sbom.d.ts +25 -1
  85. package/dist/src/sbom.js +23 -2
  86. package/dist/src/tools.d.ts +37 -13
  87. package/dist/src/tools.js +92 -13
  88. package/dist/src/updaters/maven_updater.d.ts +58 -0
  89. package/dist/src/updaters/maven_updater.js +345 -0
  90. package/dist/src/updaters/toml_updater.d.ts +62 -0
  91. package/dist/src/updaters/toml_updater.js +276 -0
  92. package/dist/src/workspace.d.ts +71 -0
  93. package/dist/src/workspace.js +257 -0
  94. package/package.json +37 -28
@@ -1,4 +1,17 @@
1
- export type purlType = import("../provider").Provider;
1
+ /**
2
+ * Converts a Subresource Integrity (SRI) string to a CycloneDX hash object.
3
+ * SRI is formatted as `<alg>-<base64>` (e.g. `sha512-Ab3d...`). CycloneDX
4
+ * expects an uppercased algorithm label (e.g. `SHA-512`) and a hex-encoded
5
+ * digest. When the SRI string contains multiple space-separated hashes, only
6
+ * the first is used.
7
+ * @param {string} integrity - SRI string from a lock file
8
+ * @returns {{alg: string, content: string}|null} CycloneDX hash, or null when unparseable
9
+ */
10
+ export function sriToHash(integrity: string): {
11
+ alg: string;
12
+ content: string;
13
+ } | null;
14
+ export type purlType = any;
2
15
  /** @typedef {import('../provider').Provider} */
3
16
  /** @typedef {import('../provider').Provided} Provided */
4
17
  /**
@@ -19,7 +32,7 @@ export default class Base_javascript {
19
32
  * @param {Object} opts - Configuration options for the provider
20
33
  * @protected
21
34
  */
22
- protected _setUp(manifestPath: string, opts: any): void;
35
+ protected _setUp(manifestPath: string, opts: Object): void;
23
36
  /**
24
37
  * Gets the current manifest object
25
38
  * @returns {Manifest} The manifest object
@@ -47,6 +60,11 @@ export default class Base_javascript {
47
60
  */
48
61
  protected _cmdName(): string;
49
62
  /**
63
+ * Returns the package manager name (e.g. npm, yarn, pnpm, bun)
64
+ * @returns {string} The package manager name
65
+ */
66
+ packageManagerName(): string;
67
+ /**
50
68
  * Returns the command arguments for listing dependencies
51
69
  * @returns {Array<string>} The command arguments
52
70
  * @abstract
@@ -60,6 +78,47 @@ export default class Base_javascript {
60
78
  * @protected
61
79
  */
62
80
  protected _updateLockFileCmdArgs(): Array<string>;
81
+ /**
82
+ * Parses the provider's lock file into a hash map keyed by `name@version`.
83
+ * Subclasses override this (accepting the lock directory) to support their
84
+ * lock file format. The default implementation returns an empty map so
85
+ * providers without lock parsing (or without a lock file present) gracefully
86
+ * omit hashes.
87
+ * @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
88
+ * @protected
89
+ */
90
+ protected _parseLockFileHashes(): Map<string, Array<{
91
+ alg: string;
92
+ content: string;
93
+ }>>;
94
+ /**
95
+ * Loads lock-file hashes for the given directory into internal state.
96
+ * Parsing failures degrade gracefully to an empty map (no hashes emitted).
97
+ * @param {string} lockDir - Directory containing the lock file
98
+ * @protected
99
+ */
100
+ protected _loadHashes(lockDir: string): void;
101
+ /**
102
+ * Looks up CycloneDX hashes for a package by name and resolved version.
103
+ * @param {string} name - The package name (including scope, e.g. `@hapi/joi`)
104
+ * @param {string} version - The resolved package version
105
+ * @returns {Array<{alg: string, content: string}>|undefined} Hashes, or undefined
106
+ * @protected
107
+ */
108
+ protected _hashesFor(name: string, version: string): Array<{
109
+ alg: string;
110
+ content: string;
111
+ }> | undefined;
112
+ /**
113
+ * Looks up CycloneDX hashes for a package described by a PackageURL.
114
+ * @param {PackageURL} purl - The package URL
115
+ * @returns {Array<{alg: string, content: string}>|undefined} Hashes, or undefined
116
+ * @protected
117
+ */
118
+ protected _hashesForPurl(purl: PackageURL): Array<{
119
+ alg: string;
120
+ content: string;
121
+ }> | undefined;
63
122
  /**
64
123
  * Checks if the provider supports the given manifest name
65
124
  * @param {string} manifestName - The manifest name to check
@@ -67,25 +126,40 @@ export default class Base_javascript {
67
126
  */
68
127
  isSupported(manifestName: string): boolean;
69
128
  /**
70
- * Checks if a required lock file exists in the same path as the manifest
129
+ * Walks up the directory tree from manifestDir looking for the lock file.
130
+ * Stops when the lock file is found, when a package.json with a "workspaces"
131
+ * field is encountered without a lock file (workspace root boundary), or
132
+ * when the filesystem root is reached.
133
+ *
134
+ * When TRUSTIFY_DA_WORKSPACE_DIR is set, checks only that directory (no walk-up).
135
+ *
136
+ * @param {string} manifestDir - The directory to start searching from
137
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
138
+ * @returns {string|null} The directory containing the lock file, or null
139
+ * @protected
140
+ */
141
+ protected _isWorkspaceRoot(dir: any): string | null;
142
+ _findLockFileDir(manifestDir: any, opts?: {}): string | null;
143
+ /**
71
144
  * @param {string} manifestDir - The base directory where the manifest is located
145
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
72
146
  * @returns {boolean} True if the lock file exists
73
147
  */
74
- validateLockFile(manifestDir: string): boolean;
148
+ validateLockFile(manifestDir: string, opts?: Object): boolean;
75
149
  /**
76
150
  * Provides content and content type for stack analysis
77
151
  * @param {string} manifestPath - The manifest path or name
78
152
  * @param {Object} [opts={}] - Optional configuration options
79
153
  * @returns {Provided} The provided data for stack analysis
80
154
  */
81
- provideStack(manifestPath: string, opts?: any): Provided;
155
+ provideStack(manifestPath: string, opts?: Object): Provided;
82
156
  /**
83
157
  * Provides content and content type for component analysis
84
158
  * @param {string} manifestPath - Path to package.json for component report
85
159
  * @param {Object} [opts={}] - Optional configuration options
86
160
  * @returns {Provided} The provided data for component analysis
87
161
  */
88
- provideComponent(manifestPath: string, opts?: any): Provided;
162
+ provideComponent(manifestPath: string, opts?: Object): Provided;
89
163
  /**
90
164
  * Read license from manifest (package.json). Reused by npm, pnpm, yarn.
91
165
  * @param {string} manifestPath - path to package.json
@@ -95,24 +169,25 @@ export default class Base_javascript {
95
169
  /**
96
170
  * Builds the dependency tree for the project
97
171
  * @param {boolean} includeTransitive - Whether to include transitive dependencies
172
+ * @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root
98
173
  * @returns {Object} The dependency tree
99
174
  * @protected
100
175
  */
101
- protected _buildDependencyTree(includeTransitive: boolean): any;
176
+ protected _buildDependencyTree(includeTransitive: boolean, opts?: Object): Object;
102
177
  /**
103
178
  * Recursively builds the Sbom from the JSON that npm listing returns
104
179
  * @param {Sbom} sbom - The SBOM object to add dependencies to
105
180
  * @param {Object} depTree - The current dependency tree
106
181
  * @protected
107
182
  */
108
- protected _addDependenciesToSbom(sbom: Sbom, depTree: any): void;
183
+ protected _addDependenciesToSbom(sbom: Sbom, depTree: Object): void;
109
184
  /**
110
185
  * Extracts root dependencies from the dependency tree
111
186
  * @param {Object} depTree - The dependency tree object
112
187
  * @returns {Map<string, PackageURL>} Map of dependency names to their PackageURL objects
113
188
  * @protected
114
189
  */
115
- protected _getRootDependencies(depTree: any): Map<string, PackageURL>;
190
+ protected _getRootDependencies(depTree: Object): Map<string, PackageURL>;
116
191
  /**
117
192
  * Gets the version of the package manager
118
193
  * @returns {string} The version string of the package manager
@@ -120,6 +195,12 @@ export default class Base_javascript {
120
195
  */
121
196
  protected _version(): string;
122
197
  /**
198
+ * Creates or updates the lock file for the package manager
199
+ * @param {string} manifestDir - Directory containing the manifest file
200
+ * @protected
201
+ */
202
+ protected _createLockFile(manifestDir: string): void;
203
+ /**
123
204
  * Parses the dependency tree output
124
205
  * @param {string} output - The output to parse
125
206
  * @returns {string} The parsed output
@@ -128,6 +209,6 @@ export default class Base_javascript {
128
209
  protected _parseDepTreeOutput(output: string): string;
129
210
  #private;
130
211
  }
131
- export type Provided = import("../provider").Provided;
212
+ export type Provided = any;
132
213
  import Manifest from './manifest.js';
133
214
  import Sbom from '../sbom.js';
@@ -3,7 +3,7 @@ import os from "node:os";
3
3
  import path from 'node:path';
4
4
  import { getLicense } from '../license/license_utils.js';
5
5
  import Sbom from '../sbom.js';
6
- import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from "../tools.js";
6
+ import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from '../tools.js';
7
7
  import Manifest from './manifest.js';
8
8
  /** @typedef {import('../provider').Provider} */
9
9
  /** @typedef {import('../provider').Provided} Provided */
@@ -12,6 +12,49 @@ import Manifest from './manifest.js';
12
12
  * @type {string}
13
13
  */
14
14
  export const purlType = 'npm';
15
+ /**
16
+ * Maps a Subresource Integrity (SRI) algorithm token to its CycloneDX label.
17
+ * @type {Object<string, string>}
18
+ */
19
+ const SRI_ALG_LABELS = {
20
+ sha1: 'SHA-1',
21
+ sha256: 'SHA-256',
22
+ sha384: 'SHA-384',
23
+ sha512: 'SHA-512',
24
+ };
25
+ /**
26
+ * Converts a Subresource Integrity (SRI) string to a CycloneDX hash object.
27
+ * SRI is formatted as `<alg>-<base64>` (e.g. `sha512-Ab3d...`). CycloneDX
28
+ * expects an uppercased algorithm label (e.g. `SHA-512`) and a hex-encoded
29
+ * digest. When the SRI string contains multiple space-separated hashes, only
30
+ * the first is used.
31
+ * @param {string} integrity - SRI string from a lock file
32
+ * @returns {{alg: string, content: string}|null} CycloneDX hash, or null when unparseable
33
+ */
34
+ export function sriToHash(integrity) {
35
+ if (typeof integrity !== 'string') {
36
+ return null;
37
+ }
38
+ const first = integrity.trim().split(/\s+/)[0];
39
+ if (!first) {
40
+ return null;
41
+ }
42
+ const dash = first.indexOf('-');
43
+ if (dash < 0) {
44
+ return null;
45
+ }
46
+ const alg = SRI_ALG_LABELS[first.slice(0, dash).toLowerCase()];
47
+ // Strip any SRI options (e.g. "?foo") that may follow the base64 payload
48
+ const base64 = first.slice(dash + 1).split('?')[0];
49
+ if (!alg || !base64) {
50
+ return null;
51
+ }
52
+ const content = Buffer.from(base64, 'base64').toString('hex');
53
+ if (!content) {
54
+ return null;
55
+ }
56
+ return { alg, content };
57
+ }
15
58
  /**
16
59
  * Base class for JavaScript package manager providers.
17
60
  * This class provides common functionality for different JavaScript package managers
@@ -25,6 +68,11 @@ export default class Base_javascript {
25
68
  #cmd;
26
69
  /** @type {string} */
27
70
  #ecosystem;
71
+ /**
72
+ * Lock-file hashes keyed by `name@version`.
73
+ * @type {Map<string, Array<{alg: string, content: string}>>}
74
+ */
75
+ #hashes = new Map();
28
76
  /**
29
77
  * Sets up the provider with the manifest path and options
30
78
  * @param {string} manifestPath - Path to the package.json manifest file
@@ -71,6 +119,13 @@ export default class Base_javascript {
71
119
  throw new TypeError("_cmdName must be implemented");
72
120
  }
73
121
  /**
122
+ * Returns the package manager name (e.g. npm, yarn, pnpm, bun)
123
+ * @returns {string} The package manager name
124
+ */
125
+ packageManagerName() {
126
+ return this._cmdName();
127
+ }
128
+ /**
74
129
  * Returns the command arguments for listing dependencies
75
130
  * @returns {Array<string>} The command arguments
76
131
  * @abstract
@@ -88,6 +143,58 @@ export default class Base_javascript {
88
143
  _updateLockFileCmdArgs() {
89
144
  throw new TypeError("_updateLockFileCmdArgs must be implemented");
90
145
  }
146
+ /**
147
+ * Parses the provider's lock file into a hash map keyed by `name@version`.
148
+ * Subclasses override this (accepting the lock directory) to support their
149
+ * lock file format. The default implementation returns an empty map so
150
+ * providers without lock parsing (or without a lock file present) gracefully
151
+ * omit hashes.
152
+ * @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
153
+ * @protected
154
+ */
155
+ _parseLockFileHashes() {
156
+ return new Map();
157
+ }
158
+ /**
159
+ * Loads lock-file hashes for the given directory into internal state.
160
+ * Parsing failures degrade gracefully to an empty map (no hashes emitted).
161
+ * @param {string} lockDir - Directory containing the lock file
162
+ * @protected
163
+ */
164
+ _loadHashes(lockDir) {
165
+ try {
166
+ this.#hashes = this._parseLockFileHashes(lockDir) || new Map();
167
+ }
168
+ catch (_) {
169
+ this.#hashes = new Map();
170
+ }
171
+ }
172
+ /**
173
+ * Looks up CycloneDX hashes for a package by name and resolved version.
174
+ * @param {string} name - The package name (including scope, e.g. `@hapi/joi`)
175
+ * @param {string} version - The resolved package version
176
+ * @returns {Array<{alg: string, content: string}>|undefined} Hashes, or undefined
177
+ * @protected
178
+ */
179
+ _hashesFor(name, version) {
180
+ if (!name || !version) {
181
+ return undefined;
182
+ }
183
+ return this.#hashes.get(`${name}@${version}`);
184
+ }
185
+ /**
186
+ * Looks up CycloneDX hashes for a package described by a PackageURL.
187
+ * @param {PackageURL} purl - The package URL
188
+ * @returns {Array<{alg: string, content: string}>|undefined} Hashes, or undefined
189
+ * @protected
190
+ */
191
+ _hashesForPurl(purl) {
192
+ if (!purl) {
193
+ return undefined;
194
+ }
195
+ const name = purl.namespace ? `${purl.namespace}/${purl.name}` : purl.name;
196
+ return this._hashesFor(name, purl.version);
197
+ }
91
198
  /**
92
199
  * Checks if the provider supports the given manifest name
93
200
  * @param {string} manifestName - The manifest name to check
@@ -97,13 +204,63 @@ export default class Base_javascript {
97
204
  return 'package.json' === manifestName;
98
205
  }
99
206
  /**
100
- * Checks if a required lock file exists in the same path as the manifest
207
+ * Walks up the directory tree from manifestDir looking for the lock file.
208
+ * Stops when the lock file is found, when a package.json with a "workspaces"
209
+ * field is encountered without a lock file (workspace root boundary), or
210
+ * when the filesystem root is reached.
211
+ *
212
+ * When TRUSTIFY_DA_WORKSPACE_DIR is set, checks only that directory (no walk-up).
213
+ *
214
+ * @param {string} manifestDir - The directory to start searching from
215
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
216
+ * @returns {string|null} The directory containing the lock file, or null
217
+ * @protected
218
+ */
219
+ _isWorkspaceRoot(dir) {
220
+ if (fs.existsSync(path.join(dir, 'pnpm-workspace.yaml'))) {
221
+ return true;
222
+ }
223
+ const pkgJsonPath = path.join(dir, 'package.json');
224
+ if (fs.existsSync(pkgJsonPath)) {
225
+ try {
226
+ const content = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
227
+ if (content.workspaces) {
228
+ return true;
229
+ }
230
+ }
231
+ catch (_) {
232
+ // ignore parse errors
233
+ }
234
+ }
235
+ return false;
236
+ }
237
+ _findLockFileDir(manifestDir, opts = {}) {
238
+ const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
239
+ if (workspaceDir) {
240
+ const dir = path.resolve(workspaceDir);
241
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
242
+ }
243
+ let dir = path.resolve(manifestDir);
244
+ let parent = dir;
245
+ do {
246
+ dir = parent;
247
+ if (fs.existsSync(path.join(dir, this._lockFileName()))) {
248
+ return dir;
249
+ }
250
+ if (this._isWorkspaceRoot(dir)) {
251
+ return null;
252
+ }
253
+ parent = path.dirname(dir);
254
+ } while (parent !== dir);
255
+ return null;
256
+ }
257
+ /**
101
258
  * @param {string} manifestDir - The base directory where the manifest is located
259
+ * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR
102
260
  * @returns {boolean} True if the lock file exists
103
261
  */
104
- validateLockFile(manifestDir) {
105
- const lock = path.join(manifestDir, this._lockFileName());
106
- return fs.existsSync(lock);
262
+ validateLockFile(manifestDir, opts = {}) {
263
+ return this._findLockFileDir(manifestDir, opts) !== null;
107
264
  }
108
265
  /**
109
266
  * Provides content and content type for stack analysis
@@ -159,14 +316,17 @@ export default class Base_javascript {
159
316
  /**
160
317
  * Builds the dependency tree for the project
161
318
  * @param {boolean} includeTransitive - Whether to include transitive dependencies
319
+ * @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root
162
320
  * @returns {Object} The dependency tree
163
321
  * @protected
164
322
  */
165
- _buildDependencyTree(includeTransitive) {
323
+ _buildDependencyTree(includeTransitive, opts = {}) {
166
324
  this._version();
167
- let manifestDir = path.dirname(this.#manifest.manifestPath);
168
- this.#createLockFile(manifestDir);
169
- let output = this.#executeListCmd(includeTransitive, manifestDir);
325
+ const manifestDir = path.dirname(this.#manifest.manifestPath);
326
+ const cmdDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
327
+ this._createLockFile(cmdDir);
328
+ this._loadHashes(cmdDir);
329
+ let output = this.#executeListCmd(includeTransitive, cmdDir);
170
330
  output = this._parseDepTreeOutput(output);
171
331
  return JSON.parse(output);
172
332
  }
@@ -177,15 +337,38 @@ export default class Base_javascript {
177
337
  * @private
178
338
  */
179
339
  #getSBOM(opts = {}) {
180
- const depsObject = this._buildDependencyTree(true);
340
+ const depsObject = this._buildDependencyTree(true, opts);
181
341
  let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
182
342
  const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
183
343
  let sbom = new Sbom();
184
344
  sbom.addRoot(mainComponent, license);
185
345
  this._addDependenciesToSbom(sbom, depsObject);
346
+ this.#ensurePeerAndOptionalDeps(sbom);
186
347
  sbom.filterIgnoredDeps(this.#manifest.ignored);
187
348
  return sbom.getAsJsonString(opts);
188
349
  }
350
+ /**
351
+ * Ensures peer and optional dependencies declared in the manifest are
352
+ * present in the SBOM, even when the package manager does not resolve them
353
+ * (e.g. yarn does not include peer deps in its dependency listing).
354
+ * @param {Sbom} sbom - The SBOM to supplement
355
+ * @private
356
+ */
357
+ #ensurePeerAndOptionalDeps(sbom) {
358
+ const rootPurl = toPurl(purlType, this.#manifest.name, this.#manifest.version);
359
+ const depSources = [this.#manifest.peerDependencies, this.#manifest.optionalDependencies];
360
+ for (const source of depSources) {
361
+ for (const [name, version] of Object.entries(source)) {
362
+ // Build the purl prefix for exact matching (e.g. "pkg:npm/minimist@"
363
+ // or "pkg:npm/%40hapi/joi@") to avoid substring false positives
364
+ const probe = toPurl(purlType, name, version);
365
+ const purlPrefix = probe.toString().replace(/@[^@]*$/, '@');
366
+ if (!sbom.checkDependsOnByPurlPrefix(rootPurl, purlPrefix)) {
367
+ sbom.addDependency(rootPurl, probe);
368
+ }
369
+ }
370
+ }
371
+ }
189
372
  /**
190
373
  * Recursively builds the Sbom from the JSON that npm listing returns
191
374
  * @param {Sbom} sbom - The SBOM object to add dependencies to
@@ -193,13 +376,16 @@ export default class Base_javascript {
193
376
  * @protected
194
377
  */
195
378
  _addDependenciesToSbom(sbom, depTree) {
196
- const dependencies = depTree["dependencies"] || {};
379
+ const dependencies = {
380
+ ...depTree["dependencies"],
381
+ ...depTree["optionalDependencies"],
382
+ };
197
383
  Object.entries(dependencies)
198
384
  .forEach(entry => {
199
385
  const [name, artifact] = entry;
200
386
  const target = toPurl(purlType, name, artifact.version);
201
387
  const rootPurl = toPurl(purlType, this.#manifest.name, this.#manifest.version);
202
- sbom.addDependency(rootPurl, target);
388
+ sbom.addDependency(rootPurl, target, undefined, this._hashesFor(name, artifact.version));
203
389
  this.#addDependenciesOf(sbom, target, artifact);
204
390
  });
205
391
  }
@@ -217,7 +403,7 @@ export default class Base_javascript {
217
403
  const [name, depArtifact] = entry;
218
404
  if (depArtifact.version !== undefined) {
219
405
  const target = toPurl(purlType, name, depArtifact.version);
220
- sbom.addDependency(from, target);
406
+ sbom.addDependency(from, target, undefined, this._hashesFor(name, depArtifact.version));
221
407
  this.#addDependenciesOf(sbom, target, depArtifact);
222
408
  }
223
409
  });
@@ -229,7 +415,7 @@ export default class Base_javascript {
229
415
  * @private
230
416
  */
231
417
  #getDirectDependencySbom(opts = {}) {
232
- const depTree = this._buildDependencyTree(false);
418
+ const depTree = this._buildDependencyTree(false, opts);
233
419
  let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
234
420
  const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
235
421
  let sbom = new Sbom();
@@ -241,8 +427,10 @@ export default class Base_javascript {
241
427
  .sort();
242
428
  for (const key of sortedDepsKeys) {
243
429
  const rootPurl = toPurlFromString(sbom.getRoot().purl);
244
- sbom.addDependency(rootPurl, rootDeps.get(key));
430
+ const targetPurl = rootDeps.get(key);
431
+ sbom.addDependency(rootPurl, targetPurl, undefined, this._hashesForPurl(targetPurl));
245
432
  }
433
+ this.#ensurePeerAndOptionalDeps(sbom);
246
434
  sbom.filterIgnoredDeps(this.#manifest.ignored);
247
435
  return sbom.getAsJsonString(opts);
248
436
  }
@@ -253,10 +441,14 @@ export default class Base_javascript {
253
441
  * @protected
254
442
  */
255
443
  _getRootDependencies(depTree) {
256
- if (!depTree.dependencies) {
444
+ const allDeps = {
445
+ ...depTree.dependencies,
446
+ ...depTree.optionalDependencies,
447
+ };
448
+ if (Object.keys(allDeps).length === 0) {
257
449
  return new Map();
258
450
  }
259
- return new Map(Object.entries(depTree.dependencies).map(([key, value]) => [key, toPurl(purlType, key, value.version)]));
451
+ return new Map(Object.entries(allDeps).map(([key, value]) => [key, toPurl(purlType, key, value.version)]));
260
452
  }
261
453
  /**
262
454
  * Executes the list command to get dependencies
@@ -267,7 +459,7 @@ export default class Base_javascript {
267
459
  */
268
460
  #executeListCmd(includeTransitive, manifestDir) {
269
461
  const listArgs = this._listCmdArgs(includeTransitive, manifestDir);
270
- return this.#invokeCommand(listArgs);
462
+ return this.#invokeCommand(listArgs, { cwd: manifestDir });
271
463
  }
272
464
  /**
273
465
  * Gets the version of the package manager
@@ -280,19 +472,17 @@ export default class Base_javascript {
280
472
  /**
281
473
  * Creates or updates the lock file for the package manager
282
474
  * @param {string} manifestDir - Directory containing the manifest file
283
- * @private
475
+ * @protected
284
476
  */
285
- #createLockFile(manifestDir) {
477
+ _createLockFile(manifestDir) {
286
478
  const originalDir = process.cwd();
287
479
  const isWindows = os.platform() === 'win32';
288
480
  if (isWindows) {
289
- // On Windows, --prefix flag doesn't work as expected
290
- // Instead of installing from the prefix folder, it installs from current working directory
291
481
  process.chdir(manifestDir);
292
482
  }
293
483
  try {
294
484
  const args = this._updateLockFileCmdArgs(manifestDir);
295
- this.#invokeCommand(args);
485
+ this.#invokeCommand(args, { cwd: manifestDir });
296
486
  }
297
487
  finally {
298
488
  if (isWindows) {
@@ -342,7 +532,7 @@ export default class Base_javascript {
342
532
  }
343
533
  }
344
534
  // Try to find the command in the following order:
345
- // 1. Custom path from environment/opts (via getCustomPath)
535
+ // 1. Custom path from opts/environment (via getCustomPath)
346
536
  // 2. Local node_modules/.bin
347
537
  // 3. Global installation
348
538
  let cmd = this.#cmd;