@trustify-da/trustify-da-javascript-client 0.3.0-ea.57f5d5e → 0.3.0-ea.7ed8d8c

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/dist/package.json CHANGED
@@ -45,14 +45,17 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@babel/core": "^7.23.2",
48
- "@cyclonedx/cyclonedx-library": "~1.13.3",
48
+ "@cyclonedx/cyclonedx-library": "^6.13.0",
49
+ "eslint-import-resolver-typescript": "^4.4.4",
49
50
  "fast-toml": "^0.5.4",
50
- "fast-xml-parser": "^4.5.3",
51
+ "fast-xml-parser": "^5.3.4",
51
52
  "help": "^3.0.2",
52
53
  "https-proxy-agent": "^7.0.6",
53
- "node-fetch": "^2.7.0",
54
- "packageurl-js": "^1.0.2",
55
- "yargs": "^17.7.2"
54
+ "node-fetch": "^3.3.2",
55
+ "packageurl-js": "~1.0.2",
56
+ "tree-sitter-requirements": "github:Strum355/tree-sitter-requirements#d0261ee76b84253997fe70d7d397e78c006c3801",
57
+ "web-tree-sitter": "^0.26.6",
58
+ "yargs": "^18.0.0"
56
59
  },
57
60
  "devDependencies": {
58
61
  "@babel/core": "^7.23.2",
@@ -60,14 +63,15 @@
60
63
  "@types/node": "^20.17.30",
61
64
  "@types/which": "^3.0.4",
62
65
  "babel-plugin-rewire": "^1.2.0",
63
- "c8": "^8.0.0",
66
+ "c8": "^11.0.0",
64
67
  "chai": "^4.3.7",
65
68
  "eslint": "^8.42.0",
69
+ "eslint-import-resolver-typescript": "^4.4.4",
66
70
  "eslint-plugin-editorconfig": "^4.0.3",
67
71
  "eslint-plugin-import": "^2.29.1",
68
72
  "esmock": "^2.6.2",
69
73
  "mocha": "^10.2.0",
70
- "msw": "^1.3.2",
74
+ "msw": "^2.12.7",
71
75
  "sinon": "^15.1.2",
72
76
  "sinon-chai": "^3.7.0",
73
77
  "typescript": "^5.1.3",
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Utility function for fetching vendor tokens
3
+ * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
4
+ * @returns {{}}
5
+ */
6
+ export function getTokenHeaders(opts?: import("index.js").Options): {};
1
7
  declare namespace _default {
2
8
  export { requestComponent };
3
9
  export { requestStack };
@@ -13,7 +19,7 @@ export default _default;
13
19
  * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
14
20
  * @returns {Promise<import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>}
15
21
  */
16
- declare function requestComponent(provider: import('./provider').Provider, manifest: string, url: string, opts?: import("index.js").Options | undefined): Promise<import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>;
22
+ declare function requestComponent(provider: import("./provider").Provider, manifest: string, url: string, opts?: import("index.js").Options): Promise<import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport>;
17
23
  /**
18
24
  * Send a stack analysis request and get the report as 'text/html' or 'application/json'.
19
25
  * @param {import('./provider').Provider} provider - the provided data for constructing the request
@@ -23,7 +29,7 @@ declare function requestComponent(provider: import('./provider').Provider, manif
23
29
  * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
24
30
  * @returns {Promise<string|import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>}
25
31
  */
26
- declare function requestStack(provider: import('./provider').Provider, manifest: string, url: string, html?: boolean | undefined, opts?: import("index.js").Options | undefined): Promise<string | import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>;
32
+ declare function requestStack(provider: import("./provider").Provider, manifest: string, url: string, html?: boolean, opts?: import("index.js").Options): Promise<string | import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport>;
27
33
  /**
28
34
  *
29
35
  * @param {Array<string>} imageRefs
@@ -31,8 +37,8 @@ declare function requestStack(provider: import('./provider').Provider, manifest:
31
37
  * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
32
38
  * @returns {Promise<string|Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>>}
33
39
  */
34
- declare function requestImages(imageRefs: Array<string>, url: string, html?: boolean, opts?: import("index.js").Options | undefined): Promise<string | {
35
- [x: string]: import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport;
40
+ declare function requestImages(imageRefs: Array<string>, url: string, html?: boolean, opts?: import("index.js").Options): Promise<string | {
41
+ [x: string]: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport;
36
42
  }>;
37
43
  /**
38
44
  *
@@ -40,4 +46,4 @@ declare function requestImages(imageRefs: Array<string>, url: string, html?: boo
40
46
  * @param {import("index.js").Options} [opts={}] - optional various options to pass headers for t he validateToken Request
41
47
  * @return {Promise<number>} return the HTTP status Code of the response from the validate token request.
42
48
  */
43
- declare function validateToken(url: any, opts?: import("index.js").Options | undefined): Promise<number>;
49
+ declare function validateToken(url: any, opts?: import("index.js").Options): Promise<number>;
@@ -5,11 +5,11 @@ import { HttpsProxyAgent } from "https-proxy-agent";
5
5
  import { generateImageSBOM, parseImageRef } from "./oci_image/utils.js";
6
6
  import { RegexNotToBeLogged, getCustom } from "./tools.js";
7
7
  export default { requestComponent, requestStack, requestImages, validateToken };
8
- const rhdaTokenHeader = "rhda-token";
9
- const rhdaTelemetryId = "rhda-telemetry-id";
10
- const rhdaSourceHeader = "rhda-source";
11
- const rhdaOperationTypeHeader = "rhda-operation-type";
12
- const rhdaPackageManagerHeader = "rhda-pkg-manager";
8
+ const rhdaTokenHeader = "trust-da-token";
9
+ const rhdaTelemetryId = "telemetry-anonymous-id";
10
+ const rhdaSourceHeader = "trust-da-source";
11
+ const rhdaOperationTypeHeader = "trust-da-operation-type";
12
+ const rhdaPackageManagerHeader = "trust-da-pkg-manager";
13
13
  /**
14
14
  * Adds proxy agent configuration to fetch options if a proxy URL is specified
15
15
  * @param {RequestInit} options - The base fetch options
@@ -35,7 +35,7 @@ function addProxyAgent(options, opts) {
35
35
  async function requestStack(provider, manifest, url, html = false, opts = {}) {
36
36
  opts["source-manifest"] = Buffer.from(fs.readFileSync(manifest).toString()).toString('base64');
37
37
  opts["manifest-type"] = path.parse(manifest).base;
38
- let provided = provider.provideStack(manifest, opts); // throws error if content providing failed
38
+ let provided = await provider.provideStack(manifest, opts); // throws error if content providing failed
39
39
  opts["source-manifest"] = "";
40
40
  opts[rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_")] = "stack-analysis";
41
41
  let startTime = new Date();
@@ -94,7 +94,7 @@ async function requestStack(provider, manifest, url, html = false, opts = {}) {
94
94
  */
95
95
  async function requestComponent(provider, manifest, url, opts = {}) {
96
96
  opts["source-manifest"] = Buffer.from(fs.readFileSync(manifest).toString()).toString('base64');
97
- let provided = provider.provideComponent(manifest, opts); // throws error if content providing failed
97
+ let provided = await provider.provideComponent(manifest, opts); // throws error if content providing failed
98
98
  opts["source-manifest"] = "";
99
99
  opts[rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_")] = "component-analysis";
100
100
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
@@ -208,11 +208,12 @@ async function validateToken(url, opts = {}) {
208
208
  *
209
209
  * @param {string} headerName - the header name to populate in request
210
210
  * @param headers
211
- * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
211
+ * @param {string} optsKey - key in the options object to use the value for
212
+ * @param {import("index.js").Options} [opts={}] - options input object to fetch header values from
212
213
  * @private
213
214
  */
214
- function setRhdaHeader(headerName, headers, opts) {
215
- let rhdaHeaderValue = getCustom(headerName.toUpperCase().replaceAll("-", "_"), null, opts);
215
+ function setRhdaHeader(headerName, headers, optsKey, opts) {
216
+ let rhdaHeaderValue = getCustom(optsKey, null, opts);
216
217
  if (rhdaHeaderValue) {
217
218
  headers[headerName] = rhdaHeaderValue;
218
219
  }
@@ -222,25 +223,14 @@ function setRhdaHeader(headerName, headers, opts) {
222
223
  * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
223
224
  * @returns {{}}
224
225
  */
225
- function getTokenHeaders(opts = {}) {
226
- let supportedTokens = ['snyk', 'oss-index'];
226
+ export function getTokenHeaders(opts = {}) {
227
227
  let headers = {};
228
- supportedTokens.forEach(vendor => {
229
- let token = getCustom(`TRUSTIFY_DA_${vendor.replace("-", "_").toUpperCase()}_TOKEN`, null, opts);
230
- if (token) {
231
- headers[`ex-${vendor}-token`] = token;
232
- }
233
- let user = getCustom(`TRUSTIFY_DA_${vendor.replace("-", "_").toUpperCase()}_USER`, null, opts);
234
- if (user) {
235
- headers[`ex-${vendor}-user`] = user;
236
- }
237
- });
238
- setRhdaHeader(rhdaTokenHeader, headers, opts);
239
- setRhdaHeader(rhdaSourceHeader, headers, opts);
240
- setRhdaHeader(rhdaOperationTypeHeader, headers, opts);
241
- setRhdaHeader(rhdaPackageManagerHeader, headers, opts);
242
- setRhdaHeader(rhdaTelemetryId, headers, opts);
243
- if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
228
+ setRhdaHeader(rhdaTokenHeader, headers, 'TRUSTIFY_DA_TOKEN', opts);
229
+ setRhdaHeader(rhdaSourceHeader, headers, 'TRUSTIFY_DA_SOURCE', opts);
230
+ setRhdaHeader(rhdaOperationTypeHeader, headers, rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_"), opts);
231
+ setRhdaHeader(rhdaPackageManagerHeader, headers, rhdaPackageManagerHeader.toUpperCase().replaceAll("-", "_"), opts);
232
+ setRhdaHeader(rhdaTelemetryId, headers, 'TRUSTIFY_DA_TELEMETRY_ID', opts);
233
+ if (getCustom("TRUSTIFY_DA_DEBUG", null, opts) === "true") {
244
234
  console.log("Headers Values to be sent to Trustify DA backend:" + EOL);
245
235
  for (const headerKey in headers) {
246
236
  if (!headerKey.match(RegexNotToBeLogged)) {
@@ -1,4 +1,3 @@
1
- /// <reference types="packageurl-js/src/package-url" />
2
1
  export default class CycloneDxSbom {
3
2
  sbomObject: any;
4
3
  rootComponent: any;
@@ -12,7 +12,7 @@
12
12
  export function selectTrustifyDABackend(opts?: {
13
13
  TRUSTIFY_DA_DEBUG?: string | undefined;
14
14
  TRUSTIFY_DA_BACKEND_URL?: string | undefined;
15
- } | undefined): string;
15
+ }): string;
16
16
  export { parseImageRef } from "./oci_image/utils.js";
17
17
  export { ImageRef } from "./oci_image/images.js";
18
18
  declare namespace _default {
@@ -23,7 +23,6 @@ declare namespace _default {
23
23
  }
24
24
  export default _default;
25
25
  export type Options = {
26
- [key: string]: string | undefined;
27
26
  TRUSTIFY_DA_DOCKER_PATH?: string | undefined;
28
27
  TRUSTIFY_DA_GO_MVS_LOGIC_ENABLED?: string | undefined;
29
28
  TRUSTIFY_DA_GO_PATH?: string | undefined;
@@ -49,9 +48,10 @@ export type Options = {
49
48
  TRUSTIFY_DA_SYFT_PATH?: string | undefined;
50
49
  TRUSTIFY_DA_YARN_PATH?: string | undefined;
51
50
  MATCH_MANIFEST_VERSIONS?: string | undefined;
52
- RHDA_SOURCE?: string | undefined;
53
- RHDA_TOKEN?: string | undefined;
54
- RHDA_TELEMETRY_ID?: string | undefined;
51
+ TRUSTIFY_DA_SOURCE?: string | undefined;
52
+ TRUSTIFY_DA_TOKEN?: string | undefined;
53
+ TRUSTIFY_DA_TELEMETRY_ID?: string | undefined;
54
+ [key: string]: string | undefined;
55
55
  };
56
56
  /**
57
57
  * Get component analysis report for a manifest content.
@@ -60,16 +60,68 @@ export type Options = {
60
60
  * @returns {Promise<import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>}
61
61
  * @throws {Error} if no matching provider, failed to get create content, or backend request failed
62
62
  */
63
- declare function componentAnalysis(manifest: string, opts?: Options | undefined): Promise<import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>;
63
+ declare function componentAnalysis(manifest: string, opts?: Options): Promise<import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport>;
64
+ /**
65
+ * @overload
66
+ * @param {string} manifest
67
+ * @param {true} html
68
+ * @param {Options} [opts={}]
69
+ * @returns {Promise<string>}
70
+ * @throws {Error}
71
+ */
64
72
  declare function stackAnalysis(manifest: string, html: true, opts?: Options | undefined): Promise<string>;
65
- declare function stackAnalysis(manifest: string, html: false, opts?: Options | undefined): Promise<import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>;
66
- declare function stackAnalysis(manifest: string, html?: boolean | undefined, opts?: Options | undefined): Promise<string | import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>;
73
+ /**
74
+ * @overload
75
+ * @param {string} manifest
76
+ * @param {false} html
77
+ * @param {Options} [opts={}]
78
+ * @returns {Promise<import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>}
79
+ * @throws {Error}
80
+ */
81
+ declare function stackAnalysis(manifest: string, html: false, opts?: Options | undefined): Promise<import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport>;
82
+ /**
83
+ * Get stack analysis report for a manifest file.
84
+ * @overload
85
+ * @param {string} manifest - path for the manifest
86
+ * @param {boolean} [html=false] - true will return a html string, false will return AnalysisReport object.
87
+ * @param {Options} [opts={}] - optional various options to pass along the application
88
+ * @returns {Promise<string|import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>}
89
+ * @throws {Error} if manifest inaccessible, no matching provider, failed to get create content,
90
+ * or backend request failed
91
+ */
92
+ declare function stackAnalysis(manifest: string, html?: boolean | undefined, opts?: Options | undefined): Promise<string | import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport>;
93
+ /**
94
+ * @overload
95
+ * @param {Array<string>} imageRefs
96
+ * @param {true} html
97
+ * @param {Options} [opts={}]
98
+ * @returns {Promise<string>}
99
+ * @throws {Error}
100
+ */
67
101
  declare function imageAnalysis(imageRefs: Array<string>, html: true, opts?: Options | undefined): Promise<string>;
102
+ /**
103
+ * @overload
104
+ * @param {Array<string>} imageRefs
105
+ * @param {false} html
106
+ * @param {Options} [opts={}]
107
+ * @returns {Promise<Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>>}
108
+ * @throws {Error}
109
+ */
68
110
  declare function imageAnalysis(imageRefs: Array<string>, html: false, opts?: Options | undefined): Promise<{
69
- [x: string]: import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport;
111
+ [x: string]: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport;
70
112
  }>;
113
+ /**
114
+ * Get image analysis report for a set of OCI image references.
115
+ * @overload
116
+ * @param {Array<string>} imageRefs - OCI image references
117
+ * @param {boolean} [html=false] - true will return a html string, false will return AnalysisReport
118
+ * @param {Options} [opts={}] - optional various options to pass along the application
119
+ * @returns {Promise<string|Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>>}
120
+ * @throws {Error} if manifest inaccessible, no matching provider, failed to get create content,
121
+ * or backend request failed
122
+ */
71
123
  declare function imageAnalysis(imageRefs: Array<string>, html?: boolean | undefined, opts?: Options | undefined): Promise<string | {
72
- [x: string]: import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport;
124
+ [x: string]: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport;
73
125
  }>;
74
126
  /**
75
127
  * Validates the Exhort token.
@@ -77,4 +129,4 @@ declare function imageAnalysis(imageRefs: Array<string>, html?: boolean | undefi
77
129
  * @returns {Promise<object>} A promise that resolves with the validation result from the backend.
78
130
  * @throws {Error} if the backend request failed.
79
131
  */
80
- declare function validateToken(opts?: Options | undefined): Promise<object>;
132
+ declare function validateToken(opts?: Options): Promise<object>;
package/dist/src/index.js CHANGED
@@ -36,9 +36,9 @@ export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken
36
36
  * TRUSTIFY_DA_SYFT_PATH?: string | undefined,
37
37
  * TRUSTIFY_DA_YARN_PATH?: string | undefined,
38
38
  * MATCH_MANIFEST_VERSIONS?: string | undefined,
39
- * RHDA_SOURCE?: string | undefined,
40
- * RHDA_TOKEN?: string | undefined,
41
- * RHDA_TELEMETRY_ID?: string | undefined,
39
+ * TRUSTIFY_DA_SOURCE?: string | undefined,
40
+ * TRUSTIFY_DA_TOKEN?: string | undefined,
41
+ * TRUSTIFY_DA_TELEMETRY_ID?: string | undefined,
42
42
  * [key: string]: string | undefined,
43
43
  * }} Options
44
44
  */
@@ -1,4 +1,3 @@
1
- /// <reference types="packageurl-js/src/package-url.js" />
2
1
  /**
3
2
  * Helper class for parsing docker repository/image names:
4
3
  *
@@ -26,7 +25,7 @@ export class Image {
26
25
  * @param {string} fullName
27
26
  * @param {string} [givenTag]
28
27
  */
29
- constructor(fullName: string, givenTag?: string | undefined);
28
+ constructor(fullName: string, givenTag?: string);
30
29
  repository: string;
31
30
  registry: string;
32
31
  tag: string;
@@ -46,12 +45,12 @@ export class Image {
46
45
  * @param {string} [optionalRegistry]
47
46
  * @returns {string}
48
47
  */
49
- getNameWithoutTag(optionalRegistry?: string | undefined): string;
48
+ getNameWithoutTag(optionalRegistry?: string): string;
50
49
  /**
51
50
  * @param {string} [optionalRegistry]
52
51
  * @returns {string}
53
52
  */
54
- getFullName(optionalRegistry?: string | undefined): string;
53
+ getFullName(optionalRegistry?: string): string;
55
54
  /**
56
55
  * @returns {string}
57
56
  */
@@ -79,7 +78,7 @@ export class ImageRef {
79
78
  * @param {string} [platform]
80
79
  * @param {import("index.js").Options} [opts={}]
81
80
  */
82
- constructor(image: string, platform?: string | undefined, opts?: import("index.js").Options | undefined);
81
+ constructor(image: string, platform?: string, opts?: import("index.js").Options);
83
82
  /** @type {Image} */
84
83
  image: Image;
85
84
  /** @type {Platform} */
@@ -4,20 +4,20 @@
4
4
  * @param {import("../index.js").Options} [opts={}] - optional various options to pass along the application
5
5
  * @returns {{}}
6
6
  */
7
- export function generateImageSBOM(imageRef: import('./images').ImageRef, opts?: import("../index.js").Options | undefined): {};
7
+ export function generateImageSBOM(imageRef: import("./images").ImageRef, opts?: import("../index.js").Options): {};
8
8
  /**
9
9
  *
10
10
  * @param {string} image
11
11
  * @param {import("../index.js").Options} [opts={}] - optional various options to pass along the application
12
12
  * @returns {ImageRef}
13
13
  */
14
- export function parseImageRef(image: string, opts?: import("../index.js").Options | undefined): ImageRef;
14
+ export function parseImageRef(image: string, opts?: import("../index.js").Options): ImageRef;
15
15
  /**
16
16
  * Gets the platform information for an image
17
17
  * @param {import("../index.js").Options} [opts={}] - optional various options to pass along the application
18
18
  * @returns {Platform|null} - The platform information or null
19
19
  */
20
- export function getImagePlatform(opts?: import("../index.js").Options | undefined): Platform | null;
20
+ export function getImagePlatform(opts?: import("../index.js").Options): Platform | null;
21
21
  /**
22
22
  * Gets the digests for an image
23
23
  * @param {import('./images').ImageRef} imageRef - The image reference
@@ -25,7 +25,7 @@ export function getImagePlatform(opts?: import("../index.js").Options | undefine
25
25
  * @returns {Object.<string, string>} - The image digests
26
26
  * @throws {Error} If the image info is invalid
27
27
  */
28
- export function getImageDigests(imageRef: import('./images').ImageRef, opts?: import("../index.js").Options | undefined): {
28
+ export function getImageDigests(imageRef: import("./images").ImageRef, opts?: import("../index.js").Options): {
29
29
  [x: string]: string;
30
30
  };
31
31
  export type SyftImageSource = {
@@ -10,7 +10,7 @@
10
10
  */
11
11
  export function match(manifest: string, providers: [Provider]): Provider;
12
12
  /** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
13
- /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided, provideStack: function(string, {}): Provided}} Provider */
13
+ /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>}} Provider */
14
14
  /**
15
15
  * MUST include all providers here.
16
16
  * @type {[Provider]}
@@ -24,6 +24,6 @@ export type Provided = {
24
24
  export type Provider = {
25
25
  isSupported: (arg0: string) => boolean;
26
26
  validateLockFile: (arg0: string) => void;
27
- provideComponent: (arg0: string, arg1: {}) => Provided;
28
- provideStack: (arg0: string, arg1: {}) => Provided;
27
+ provideComponent: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
28
+ provideStack: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
29
29
  };
@@ -8,7 +8,7 @@ import Javascript_pnpm from './providers/javascript_pnpm.js';
8
8
  import Javascript_yarn from './providers/javascript_yarn.js';
9
9
  import pythonPipProvider from './providers/python_pip.js';
10
10
  /** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
11
- /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided, provideStack: function(string, {}): Provided}} Provider */
11
+ /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>}} Provider */
12
12
  /**
13
13
  * MUST include all providers here.
14
14
  * @type {[Provider]}
@@ -1,6 +1,4 @@
1
- /// <reference types="node" />
2
- /// <reference types="packageurl-js/src/package-url.js" />
3
- export type ecosystem_maven = import('../provider').Provider;
1
+ export type ecosystem_maven = import("../provider").Provider;
4
2
  /** @typedef {import('../provider').Provider} */
5
3
  /** @typedef {import('../provider').Provided} Provided */
6
4
  /** @typedef {{name: string, version: string}} Package */
@@ -51,7 +49,7 @@ export default class Base_Java {
51
49
  * @param {import('child_process').ExecFileOptionsWithStringEncoding} [opts={}]
52
50
  * @protected
53
51
  */
54
- protected _invokeCommand(bin: any, args: any, opts?: import("child_process").ExecFileOptionsWithStringEncoding | undefined): string;
52
+ protected _invokeCommand(bin: any, args: any, opts?: import("child_process").ExecFileOptionsWithStringEncoding): string;
55
53
  /**
56
54
  *
57
55
  * @param {string} manifestPath
@@ -70,7 +68,7 @@ export default class Base_Java {
70
68
  normalizePath(thePath: any): string;
71
69
  #private;
72
70
  }
73
- export type Provided = import('../provider').Provided;
71
+ export type Provided = import("../provider").Provided;
74
72
  export type Package = {
75
73
  name: string;
76
74
  version: string;
@@ -121,7 +121,7 @@ export default class Base_javascript {
121
121
  protected _parseDepTreeOutput(output: string): string;
122
122
  #private;
123
123
  }
124
- export type Provider = import('../provider.js').Provider;
125
- export type Provided = import('../provider.js').Provided;
124
+ export type Provider = import("../provider.js").Provider;
125
+ export type Provided = import("../provider.js").Provided;
126
126
  import Manifest from './manifest.js';
127
127
  import Sbom from '../sbom.js';
@@ -5,7 +5,7 @@ declare namespace _default {
5
5
  export { provideStack };
6
6
  }
7
7
  export default _default;
8
- export type Provided = import('../provider').Provided;
8
+ export type Provided = import("../provider").Provided;
9
9
  export type Package = {
10
10
  name: string;
11
11
  version: string;
@@ -32,11 +32,11 @@ declare function validateLockFile(): boolean;
32
32
  * @param {{}} [opts={}] - optional various options to pass along the application
33
33
  * @returns {Provided}
34
34
  */
35
- declare function provideComponent(manifest: string, opts?: {} | undefined): Provided;
35
+ declare function provideComponent(manifest: string, opts?: {}): Provided;
36
36
  /**
37
37
  * Provide content and content type for maven-maven stack analysis.
38
38
  * @param {string} manifest - the manifest path or name
39
39
  * @param {{}} [opts={}] - optional various options to pass along the application
40
40
  * @returns {Provided}
41
41
  */
42
- declare function provideStack(manifest: string, opts?: {} | undefined): Provided;
42
+ declare function provideStack(manifest: string, opts?: {}): Provided;
@@ -21,15 +21,15 @@ export default class Java_gradle extends Base_java {
21
21
  * @param {{}} [opts={}] - optional various options to pass along the application
22
22
  * @returns {Provided}
23
23
  */
24
- provideStack(manifest: string, opts?: {} | undefined): Provided;
24
+ provideStack(manifest: string, opts?: {}): Provided;
25
25
  /**
26
26
  * Provide content and content type for maven-maven component analysis.
27
27
  * @param {string} manifest - path to pom.xml for component report
28
28
  * @param {{}} [opts={}] - optional various options to pass along the application
29
29
  * @returns {Provided}
30
30
  */
31
- provideComponent(manifest: string, opts?: {} | undefined): Provided;
31
+ provideComponent(manifest: string, opts?: {}): Provided;
32
32
  #private;
33
33
  }
34
- export type Provided = import('../provider.js').Provided;
34
+ export type Provided = import("../provider.js").Provided;
35
35
  import Base_java from "./base_java.js";
@@ -3,5 +3,5 @@ export default class Java_gradle_groovy extends Java_gradle {
3
3
  _parseAliasForLibsNotation(alias: any): any;
4
4
  _extractDepToBeIgnored(dep: any): any;
5
5
  }
6
- export type Provided = import('../provider').Provided;
6
+ export type Provided = import("../provider").Provided;
7
7
  import Java_gradle from './java_gradle.js';
@@ -7,5 +7,5 @@ export default class Java_gradle_kotlin extends Java_gradle {
7
7
  _parseAliasForLibsNotation(alias: any): any;
8
8
  _extractDepToBeIgnored(dep: any): string | null;
9
9
  }
10
- export type Provided = import('../provider').Provided;
10
+ export type Provided = import("../provider").Provided;
11
11
  import Java_gradle from './java_gradle.js';
@@ -19,14 +19,14 @@ export default class Java_maven extends Base_java {
19
19
  * @param {{}} [opts={}] - optional various options to pass along the application
20
20
  * @returns {Provided}
21
21
  */
22
- provideStack(manifest: string, opts?: {} | undefined): Provided;
22
+ provideStack(manifest: string, opts?: {}): Provided;
23
23
  /**
24
24
  * Provide content and content type for maven-maven component analysis.
25
25
  * @param {string} manifest - path to the manifest file
26
26
  * @param {{}} [opts={}] - optional various options to pass along the application
27
27
  * @returns {Provided}
28
28
  */
29
- provideComponent(manifest: string, opts?: {} | undefined): Provided;
29
+ provideComponent(manifest: string, opts?: {}): Provided;
30
30
  /**
31
31
  *
32
32
  * @param {String} textGraphList Text graph String of the manifest
@@ -36,8 +36,8 @@ export default class Java_maven extends Base_java {
36
36
  createSbomFileFromTextFormat(textGraphList: string, ignoredDeps: [string], opts: any): string;
37
37
  #private;
38
38
  }
39
- export type Java_maven = import('../provider').Provider;
40
- export type Provided = import('../provider').Provided;
39
+ export type Java_maven = import("../provider").Provider;
40
+ export type Provided = import("../provider").Provided;
41
41
  export type Package = {
42
42
  name: string;
43
43
  version: string;
@@ -209,7 +209,7 @@ export default class Java_maven extends Base_java {
209
209
  let ignored = [];
210
210
  // build xml parser with options
211
211
  let parser = new XMLParser({
212
- commentPropName: '#comment',
212
+ commentPropName: '#comment', // mark comments with #comment
213
213
  isArray: (_, jpath) => 'project.dependencies.dependency' === jpath,
214
214
  parseTagValue: false
215
215
  });
@@ -15,13 +15,16 @@ export default class Python_controller {
15
15
  realEnvironment: boolean;
16
16
  pathToRequirements: string;
17
17
  options: {};
18
+ parser: Promise<import("web-tree-sitter").Parser>;
19
+ requirementsQuery: Promise<import("web-tree-sitter").Query>;
20
+ pinnedVersionQuery: Promise<import("web-tree-sitter").Query>;
18
21
  prepareEnvironment(): void;
19
22
  /**
20
23
  *
21
24
  * @param {boolean} includeTransitive - whether to return include in returned object transitive dependencies or not
22
- * @return {[DependencyEntry]}
25
+ * @return {Promise<[DependencyEntry]>}
23
26
  */
24
- getDependencies(includeTransitive: boolean): [DependencyEntry];
27
+ getDependencies(includeTransitive: boolean): Promise<[DependencyEntry]>;
25
28
  #private;
26
29
  }
27
30
  export type DependencyEntry = {
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import path from 'node:path';
3
3
  import os, { EOL } from "os";
4
4
  import { environmentVariableIsPopulated, getCustom, invokeCommand } from "../tools.js";
5
+ import { getParser, getRequirementQuery, getPinnedVersionQuery } from './requirements_parser.js';
5
6
  function getPipFreezeOutput() {
6
7
  try {
7
8
  return environmentVariableIsPopulated("TRUSTIFY_DA_PIP_FREEZE") ? new Buffer.from(process.env["TRUSTIFY_DA_PIP_FREEZE"], 'base64').toString('ascii') : invokeCommand(this.pathToPipBin, ['freeze', '--all']).toString();
@@ -26,6 +27,9 @@ export default class Python_controller {
26
27
  realEnvironment;
27
28
  pathToRequirements;
28
29
  options;
30
+ parser;
31
+ requirementsQuery;
32
+ pinnedVersionQuery;
29
33
  /**
30
34
  * Constructor to create new python controller instance to interact with pip package manager
31
35
  * @param {boolean} realEnvironment - whether to use real environment supplied by client or to create virtual environment
@@ -41,6 +45,9 @@ export default class Python_controller {
41
45
  this.prepareEnvironment();
42
46
  this.pathToRequirements = pathToRequirements;
43
47
  this.options = options;
48
+ this.parser = getParser();
49
+ this.requirementsQuery = getRequirementQuery();
50
+ this.pinnedVersionQuery = getPinnedVersionQuery();
44
51
  }
45
52
  prepareEnvironment() {
46
53
  if (!this.realEnvironment) {
@@ -86,6 +93,23 @@ export default class Python_controller {
86
93
  }
87
94
  }
88
95
  }
96
+ /**
97
+ * Parse the requirements.txt file using tree-sitter and return structured requirement data.
98
+ * @return {Promise<{name: string, version: string|null}[]>}
99
+ */
100
+ async #parseRequirements() {
101
+ const content = fs.readFileSync(this.pathToRequirements).toString();
102
+ const tree = (await this.parser).parse(content);
103
+ return Promise.all((await this.requirementsQuery).matches(tree.rootNode).map(async (match) => {
104
+ const reqNode = match.captures.find(c => c.name === 'req').node;
105
+ const name = match.captures.find(c => c.name === 'name').node.text;
106
+ const versionMatches = (await this.pinnedVersionQuery).matches(reqNode);
107
+ const version = versionMatches.length > 0
108
+ ? versionMatches[0].captures.find(c => c.name === 'version').node.text
109
+ : null;
110
+ return { name, version };
111
+ }));
112
+ }
89
113
  #decideIfWindowsOrLinuxPath(fileName) {
90
114
  if (os.platform() === "win32") {
91
115
  return fileName + ".exe";
@@ -97,9 +121,9 @@ export default class Python_controller {
97
121
  /**
98
122
  *
99
123
  * @param {boolean} includeTransitive - whether to return include in returned object transitive dependencies or not
100
- * @return {[DependencyEntry]}
124
+ * @return {Promise<[DependencyEntry]>}
101
125
  */
102
- getDependencies(includeTransitive) {
126
+ async getDependencies(includeTransitive) {
103
127
  let startingTime;
104
128
  let endingTime;
105
129
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
@@ -124,10 +148,10 @@ export default class Python_controller {
124
148
  if (matchManifestVersions === "true") {
125
149
  throw new Error("Conflicting settings, TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS=true can only work with MATCH_MANIFEST_VERSIONS=false");
126
150
  }
127
- this.#installingRequirementsOneByOne();
151
+ await this.#installingRequirementsOneByOne();
128
152
  }
129
153
  }
130
- let dependencies = this.#getDependenciesImpl(includeTransitive);
154
+ let dependencies = await this.#getDependenciesImpl(includeTransitive);
131
155
  this.#cleanEnvironment();
132
156
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
133
157
  endingTime = new Date();
@@ -137,16 +161,14 @@ export default class Python_controller {
137
161
  }
138
162
  return dependencies;
139
163
  }
140
- #installingRequirementsOneByOne() {
141
- let requirementsContent = fs.readFileSync(this.pathToRequirements);
142
- let requirementsRows = requirementsContent.toString().split(EOL);
143
- requirementsRows.filter((line) => !line.trim().startsWith("#")).filter((line) => line.trim() !== "").forEach((dependency) => {
144
- let dependencyName = getDependencyName(dependency);
164
+ async #installingRequirementsOneByOne() {
165
+ const requirements = await this.#parseRequirements();
166
+ requirements.forEach(({ name }) => {
145
167
  try {
146
- invokeCommand(this.pathToPipBin, ['install', dependencyName]);
168
+ invokeCommand(this.pathToPipBin, ['install', name]);
147
169
  }
148
170
  catch (error) {
149
- throw new Error(`Failed in best-effort installing ${dependencyName} in virtual python environment`, { cause: error });
171
+ throw new Error(`Failed in best-effort installing ${name} in virtual python environment`, { cause: error });
150
172
  }
151
173
  });
152
174
  }
@@ -163,34 +185,26 @@ export default class Python_controller {
163
185
  }
164
186
  }
165
187
  }
166
- #getDependenciesImpl(includeTransitive) {
167
- let dependencies = new Array();
188
+ async #getDependenciesImpl(includeTransitive) {
189
+ let dependencies = [];
168
190
  let usePipDepTree = getCustom("TRUSTIFY_DA_PIP_USE_DEP_TREE", "false", this.options);
169
- let freezeOutput;
170
- let lines;
171
- let depNames;
172
- let pipShowOutput;
173
191
  let allPipShowDeps;
174
192
  let pipDepTreeJsonArrayOutput;
175
193
  if (usePipDepTree !== "true") {
176
- freezeOutput = getPipFreezeOutput.call(this);
177
- lines = freezeOutput.split(EOL);
178
- depNames = lines.map(line => getDependencyName(line));
194
+ const freezeOutput = getPipFreezeOutput.call(this);
195
+ const lines = freezeOutput.split(EOL);
196
+ const depNames = lines.map(line => getDependencyName(line));
197
+ const pipShowOutput = getPipShowOutput.call(this, depNames);
198
+ allPipShowDeps = pipShowOutput.split(EOL + "---" + EOL);
179
199
  }
180
200
  else {
181
201
  pipDepTreeJsonArrayOutput = getDependencyTreeJsonFromPipDepTree(this.pathToPipBin, this.pathToPythonBin);
182
202
  }
183
- if (usePipDepTree !== "true") {
184
- pipShowOutput = getPipShowOutput.call(this, depNames);
185
- allPipShowDeps = pipShowOutput.split(EOL + "---" + EOL);
186
- }
187
- //debug
188
- // pipShowOutput = "alternative pip show output goes here for debugging"
189
203
  let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "true", this.options);
190
- let linesOfRequirements = fs.readFileSync(this.pathToRequirements).toString().split(EOL).filter((line) => !line.trim().startsWith("#")).map(line => line.trim());
204
+ let parsedRequirements = await this.#parseRequirements();
191
205
  let CachedEnvironmentDeps = {};
192
206
  if (usePipDepTree !== "true") {
193
- allPipShowDeps.forEach((record) => {
207
+ allPipShowDeps.forEach(record => {
194
208
  let dependencyName = getDependencyNameShow(record).toLowerCase();
195
209
  CachedEnvironmentDeps[dependencyName] = record;
196
210
  CachedEnvironmentDeps[dependencyName.replace("-", "_")] = record;
@@ -210,40 +224,24 @@ export default class Python_controller {
210
224
  CachedEnvironmentDeps[packageName.replace("_", "-")] = pipDepTreeEntryForCache;
211
225
  });
212
226
  }
213
- linesOfRequirements.forEach((dep) => {
214
- // if matchManifestVersions setting is turned on , then
215
- if (matchManifestVersions === "true") {
216
- let dependencyName;
217
- let manifestVersion;
227
+ parsedRequirements.forEach(({ name: depName, version: manifestVersion }) => {
228
+ if (matchManifestVersions === "true" && manifestVersion != null) {
218
229
  let installedVersion;
219
- let doubleEqualSignPosition;
220
- if (dep.includes("==")) {
221
- doubleEqualSignPosition = dep.indexOf("==");
222
- manifestVersion = dep.substring(doubleEqualSignPosition + 2).trim();
223
- if (manifestVersion.includes("#")) {
224
- let hashCharIndex = manifestVersion.indexOf("#");
225
- manifestVersion = manifestVersion.substring(0, hashCharIndex);
230
+ if (CachedEnvironmentDeps[depName.toLowerCase()] !== undefined) {
231
+ if (usePipDepTree !== "true") {
232
+ installedVersion = getDependencyVersion(CachedEnvironmentDeps[depName.toLowerCase()]);
226
233
  }
227
- dependencyName = getDependencyName(dep);
228
- // only compare between declared version in manifest to installed version , if the package is installed.
229
- if (CachedEnvironmentDeps[dependencyName.toLowerCase()] !== undefined) {
230
- if (usePipDepTree !== "true") {
231
- installedVersion = getDependencyVersion(CachedEnvironmentDeps[dependencyName.toLowerCase()]);
232
- }
233
- else {
234
- installedVersion = CachedEnvironmentDeps[dependencyName.toLowerCase()].version;
235
- }
234
+ else {
235
+ installedVersion = CachedEnvironmentDeps[depName.toLowerCase()].version;
236
236
  }
237
- if (installedVersion) {
238
- if (manifestVersion.trim() !== installedVersion.trim()) {
239
- throw new Error(`Can't continue with analysis - versions mismatch for dependency name ${dependencyName} (manifest version=${manifestVersion}, installed version=${installedVersion}).If you want to allow version mismatch for analysis between installed and requested packages, set environment variable/setting MATCH_MANIFEST_VERSIONS=false`);
240
- }
237
+ }
238
+ if (installedVersion) {
239
+ if (manifestVersion.trim() !== installedVersion.trim()) {
240
+ throw new Error(`Can't continue with analysis - versions mismatch for dependency name ${depName} (manifest version=${manifestVersion}, installed version=${installedVersion}).If you want to allow version mismatch for analysis between installed and requested packages, set environment variable/setting MATCH_MANIFEST_VERSIONS=false`);
241
241
  }
242
242
  }
243
243
  }
244
- let path = new Array();
245
- let depName = getDependencyName(dep);
246
- //array to track a path for each branch in the dependency tree
244
+ let path = [];
247
245
  path.push(depName.toLowerCase());
248
246
  bringAllDependencies(dependencies, depName, CachedEnvironmentDeps, includeTransitive, path, usePipDepTree);
249
247
  });
@@ -347,11 +345,11 @@ function bringAllDependencies(dependencies, dependencyName, cachedEnvironmentDep
347
345
  version = record.version;
348
346
  directDeps = record.dependencies;
349
347
  }
350
- let targetDeps = new Array();
348
+ let targetDeps = [];
351
349
  let entry = { "name": depName, "version": version, "dependencies": [] };
352
350
  dependencies.push(entry);
353
351
  directDeps.forEach((dep) => {
354
- let depArray = new Array();
352
+ let depArray = [];
355
353
  // to avoid infinite loop, check if the dependency not already on current path, before going recursively resolving its dependencies.
356
354
  if (!path.includes(dep.toLowerCase())) {
357
355
  // send to recurrsion the path + the current dep
@@ -23,13 +23,13 @@ declare function validateLockFile(): boolean;
23
23
  * Provide content and content type for python-pip component analysis.
24
24
  * @param {string} manifest - path to requirements.txt for component report
25
25
  * @param {{}} [opts={}] - optional various options to pass along the application
26
- * @returns {Provided}
26
+ * @returns {Promise<Provided>}
27
27
  */
28
- declare function provideComponent(manifest: string, opts?: {} | undefined): Provided;
28
+ declare function provideComponent(manifest: string, opts?: {}): Promise<Provided>;
29
29
  /**
30
30
  * Provide content and content type for python-pip stack analysis.
31
31
  * @param {string} manifest - the manifest path or name
32
32
  * @param {{}} [opts={}] - optional various options to pass along the application
33
- * @returns {Provided}
33
+ * @returns {Promise<Provided>}
34
34
  */
35
- declare function provideStack(manifest: string, opts?: {} | undefined): Provided;
35
+ declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
@@ -1,9 +1,9 @@
1
1
  import fs from 'node:fs';
2
- import { EOL } from 'os';
3
2
  import { PackageURL } from 'packageurl-js';
4
3
  import Sbom from '../sbom.js';
5
4
  import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from "../tools.js";
6
5
  import Python_controller from './python_controller.js';
6
+ import { getParser, getIgnoreQuery, getPinnedVersionQuery } from './requirements_parser.js';
7
7
  export default { isSupported, validateLockFile, provideComponent, provideStack };
8
8
  /** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
9
9
  /**
@@ -26,12 +26,12 @@ function validateLockFile() { return true; }
26
26
  * Provide content and content type for python-pip stack analysis.
27
27
  * @param {string} manifest - the manifest path or name
28
28
  * @param {{}} [opts={}] - optional various options to pass along the application
29
- * @returns {Provided}
29
+ * @returns {Promise<Provided>}
30
30
  */
31
- function provideStack(manifest, opts = {}) {
31
+ async function provideStack(manifest, opts = {}) {
32
32
  return {
33
33
  ecosystem,
34
- content: createSbomStackAnalysis(manifest, opts),
34
+ content: await createSbomStackAnalysis(manifest, opts),
35
35
  contentType: 'application/vnd.cyclonedx+json'
36
36
  };
37
37
  }
@@ -39,12 +39,12 @@ function provideStack(manifest, opts = {}) {
39
39
  * Provide content and content type for python-pip component analysis.
40
40
  * @param {string} manifest - path to requirements.txt for component report
41
41
  * @param {{}} [opts={}] - optional various options to pass along the application
42
- * @returns {Provided}
42
+ * @returns {Promise<Provided>}
43
43
  */
44
- function provideComponent(manifest, opts = {}) {
44
+ async function provideComponent(manifest, opts = {}) {
45
45
  return {
46
46
  ecosystem,
47
- content: getSbomForComponentAnalysis(manifest, opts),
47
+ content: await getSbomForComponentAnalysis(manifest, opts),
48
48
  contentType: 'application/vnd.cyclonedx+json'
49
49
  };
50
50
  }
@@ -66,49 +66,34 @@ function addAllDependencies(source, dep, sbom) {
66
66
  }
67
67
  /**
68
68
  *
69
- * @param nameVersion
70
- * @return {string}
71
- */
72
- function splitToNameVersion(nameVersion) {
73
- let result = [];
74
- if (nameVersion.includes("==")) {
75
- return nameVersion.split("==");
76
- }
77
- const regex = /[^\w\s-_]/g;
78
- let endIndex = nameVersion.search(regex);
79
- if (endIndex === -1) {
80
- return [nameVersion.trim()];
81
- }
82
- result.push(nameVersion.substring(0, endIndex).trim());
83
- return result;
84
- }
85
- /**
86
- *
87
- * @param {string} requirementTxtContent
69
+ * @param {string} manifest - path to requirements.txt
88
70
  * @return {PackageURL []}
89
71
  */
90
- function getIgnoredDependencies(requirementTxtContent) {
91
- let requirementsLines = requirementTxtContent.split(EOL);
92
- return requirementsLines
93
- .filter(line => line.includes("#exhortignore") || line.includes("# exhortignore"))
94
- .map((line) => line.substring(0, line.indexOf("#")).trim())
95
- .map((name) => {
96
- let nameVersion = splitToNameVersion(name);
97
- if (nameVersion.length === 2) {
98
- return toPurl(nameVersion[0], nameVersion[1]);
99
- }
100
- return toPurl(nameVersion[0], undefined);
72
+ async function getIgnoredDependencies(manifest) {
73
+ const [parser, ignoreQuery, pinnedVersionQuery] = await Promise.all([
74
+ getParser(), getIgnoreQuery(), getPinnedVersionQuery()
75
+ ]);
76
+ const content = fs.readFileSync(manifest).toString();
77
+ const tree = parser.parse(content);
78
+ return ignoreQuery.matches(tree.rootNode).map(match => {
79
+ const reqNode = match.captures.find(c => c.name === 'req').node;
80
+ const name = match.captures.find(c => c.name === 'name').node.text;
81
+ const versionMatches = pinnedVersionQuery.matches(reqNode);
82
+ const version = versionMatches.length > 0
83
+ ? versionMatches[0].captures.find(c => c.name === 'version').node.text
84
+ : undefined;
85
+ return toPurl(name, version);
101
86
  });
102
87
  }
103
88
  /**
104
89
  *
105
- * @param {string} requirementTxtContent content of requirments.txt in string
90
+ * @param {string} manifest - path to requirements.txt
106
91
  * @param {Sbom} sbom object to filter out from it exhortignore dependencies.
107
92
  * @param {{Object}} opts - various options and settings for the application
108
93
  * @private
109
94
  */
110
- function handleIgnoredDependencies(requirementTxtContent, sbom, opts = {}) {
111
- let ignoredDeps = getIgnoredDependencies(requirementTxtContent);
95
+ async function handleIgnoredDependencies(manifest, sbom, opts = {}) {
96
+ let ignoredDeps = await getIgnoredDependencies(manifest);
112
97
  let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "true", opts);
113
98
  if (matchManifestVersions === "true") {
114
99
  const ignoredDepsVersion = ignoredDeps.filter(dep => dep.version !== undefined);
@@ -172,22 +157,21 @@ const DEFAULT_PIP_ROOT_COMPONENT_VERSION = "0.0.0";
172
157
  * Create sbom json string out of a manifest path for stack analysis.
173
158
  * @param {string} manifest - path for requirements.txt
174
159
  * @param {{}} [opts={}] - optional various options to pass along the application
175
- * @returns {string} the sbom json string content
160
+ * @returns {Promise<string>} the sbom json string content
176
161
  * @private
177
162
  */
178
- function createSbomStackAnalysis(manifest, opts = {}) {
163
+ async function createSbomStackAnalysis(manifest, opts = {}) {
179
164
  let binaries = {};
180
165
  let createVirtualPythonEnv = handlePythonEnvironment(binaries, opts);
181
166
  let pythonController = new Python_controller(createVirtualPythonEnv === "false", binaries.pip, binaries.python, manifest, opts);
182
- let dependencies = pythonController.getDependencies(true);
167
+ let dependencies = await pythonController.getDependencies(true);
183
168
  let sbom = new Sbom();
184
169
  const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
185
170
  sbom.addRoot(rootPurl);
186
171
  dependencies.forEach(dep => {
187
172
  addAllDependencies(rootPurl, dep, sbom);
188
173
  });
189
- let requirementTxtContent = fs.readFileSync(manifest).toString();
190
- handleIgnoredDependencies(requirementTxtContent, sbom, opts);
174
+ await handleIgnoredDependencies(manifest, sbom, opts);
191
175
  // In python there is no root component, then we must remove the dummy root we added, so the sbom json will be accepted by the DA backend
192
176
  // sbom.removeRootComponent()
193
177
  return sbom.getAsJsonString(opts);
@@ -196,22 +180,21 @@ function createSbomStackAnalysis(manifest, opts = {}) {
196
180
  * Create a sbom json string out of a manifest content for component analysis
197
181
  * @param {string} manifest - path to requirements.txt
198
182
  * @param {{}} [opts={}] - optional various options to pass along the application
199
- * @returns {string} the sbom json string content
183
+ * @returns {Promise<string>} the sbom json string content
200
184
  * @private
201
185
  */
202
- function getSbomForComponentAnalysis(manifest, opts = {}) {
186
+ async function getSbomForComponentAnalysis(manifest, opts = {}) {
203
187
  let binaries = {};
204
188
  let createVirtualPythonEnv = handlePythonEnvironment(binaries, opts);
205
189
  let pythonController = new Python_controller(createVirtualPythonEnv === "false", binaries.pip, binaries.python, manifest, opts);
206
- let dependencies = pythonController.getDependencies(false);
190
+ let dependencies = await pythonController.getDependencies(false);
207
191
  let sbom = new Sbom();
208
192
  const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
209
193
  sbom.addRoot(rootPurl);
210
194
  dependencies.forEach(dep => {
211
195
  sbom.addDependency(rootPurl, toPurl(dep.name, dep.version));
212
196
  });
213
- let requirementTxtContent = fs.readFileSync(manifest).toString();
214
- handleIgnoredDependencies(requirementTxtContent, sbom, opts);
197
+ await handleIgnoredDependencies(manifest, sbom, opts);
215
198
  // In python there is no root component, then we must remove the dummy root we added, so the sbom json will be accepted by the DA backend
216
199
  // sbom.removeRootComponent()
217
200
  return sbom.getAsJsonString(opts);
@@ -0,0 +1,6 @@
1
+ export function getParser(): Promise<Parser>;
2
+ export function getRequirementQuery(): Promise<Query>;
3
+ export function getIgnoreQuery(): Promise<Query>;
4
+ export function getPinnedVersionQuery(): Promise<Query>;
5
+ import { Parser } from 'web-tree-sitter';
6
+ import { Query } from 'web-tree-sitter';
@@ -0,0 +1,27 @@
1
+ import { createRequire } from 'node:module';
2
+ import { Language, Parser, Query } from 'web-tree-sitter';
3
+ const require = createRequire(import.meta.url);
4
+ async function init() {
5
+ await Parser.init({
6
+ locateFile() {
7
+ return require.resolve('web-tree-sitter/web-tree-sitter.wasm');
8
+ }
9
+ });
10
+ return await Language.load(require.resolve('tree-sitter-requirements/tree-sitter-requirements.wasm'));
11
+ }
12
+ export async function getParser() {
13
+ const language = await init();
14
+ return new Parser().setLanguage(language);
15
+ }
16
+ export async function getRequirementQuery() {
17
+ const language = await init();
18
+ return new Query(language, '(requirement (package) @name) @req');
19
+ }
20
+ export async function getIgnoreQuery() {
21
+ const language = await init();
22
+ return new Query(language, '((requirement (package) @name) @req . (comment) @comment (#match? @comment "^#[\\t ]*exhortignore"))');
23
+ }
24
+ export async function getPinnedVersionQuery() {
25
+ const language = await init();
26
+ return new Query(language, '(version_spec (version_cmp) @cmp (version) @version (#eq? @cmp "=="))');
27
+ }
@@ -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?: {} | undefined, defValue: string): void;
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 | undefined, opts?: {} | undefined): 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?: {} | undefined): string | null;
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.
@@ -69,6 +67,6 @@ export function getGitRootDir(cwd: string): string | undefined;
69
67
  * @param {import('child_process').ExecFileOptionsWithStringEncoding} [opts={}]
70
68
  * @returns {string}
71
69
  */
72
- export function invokeCommand(bin: string, args: Array<string>, opts?: import("child_process").ExecFileOptionsWithStringEncoding | undefined): string;
70
+ export function invokeCommand(bin: string, args: Array<string>, opts?: import("child_process").ExecFileOptionsWithStringEncoding): string;
73
71
  export const RegexNotToBeLogged: RegExp;
74
72
  import { PackageURL } from "packageurl-js";
package/dist/src/tools.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { execFileSync } from "child_process";
2
2
  import { EOL } from "os";
3
3
  import { PackageURL } from "packageurl-js";
4
- export const RegexNotToBeLogged = /TRUSTIFY_DA_.*_TOKEN|ex-.*-token/;
4
+ export const RegexNotToBeLogged = /TRUSTIFY_DA_(.*_)?TOKEN|ex-.*-token|trust-.*-token/;
5
5
  /**
6
6
  *
7
7
  * @param {string} key to log its value from environment variables and from opts, if it exists
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trustify-da/trustify-da-javascript-client",
3
- "version": "0.3.0-ea.57f5d5e",
3
+ "version": "0.3.0-ea.7ed8d8c",
4
4
  "description": "Code-Ready Dependency Analytics JavaScript API.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/guacsec/trustify-da-javascript-client#README.md",
@@ -45,14 +45,17 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@babel/core": "^7.23.2",
48
- "@cyclonedx/cyclonedx-library": "~1.13.3",
48
+ "@cyclonedx/cyclonedx-library": "^6.13.0",
49
+ "eslint-import-resolver-typescript": "^4.4.4",
49
50
  "fast-toml": "^0.5.4",
50
- "fast-xml-parser": "^4.5.3",
51
+ "fast-xml-parser": "^5.3.4",
51
52
  "help": "^3.0.2",
52
53
  "https-proxy-agent": "^7.0.6",
53
- "node-fetch": "^2.7.0",
54
- "packageurl-js": "^1.0.2",
55
- "yargs": "^17.7.2"
54
+ "node-fetch": "^3.3.2",
55
+ "packageurl-js": "~1.0.2",
56
+ "tree-sitter-requirements": "github:Strum355/tree-sitter-requirements#d0261ee76b84253997fe70d7d397e78c006c3801",
57
+ "web-tree-sitter": "^0.26.6",
58
+ "yargs": "^18.0.0"
56
59
  },
57
60
  "devDependencies": {
58
61
  "@babel/core": "^7.23.2",
@@ -60,14 +63,15 @@
60
63
  "@types/node": "^20.17.30",
61
64
  "@types/which": "^3.0.4",
62
65
  "babel-plugin-rewire": "^1.2.0",
63
- "c8": "^8.0.0",
66
+ "c8": "^11.0.0",
64
67
  "chai": "^4.3.7",
65
68
  "eslint": "^8.42.0",
69
+ "eslint-import-resolver-typescript": "^4.4.4",
66
70
  "eslint-plugin-editorconfig": "^4.0.3",
67
71
  "eslint-plugin-import": "^2.29.1",
68
72
  "esmock": "^2.6.2",
69
73
  "mocha": "^10.2.0",
70
- "msw": "^1.3.2",
74
+ "msw": "^2.12.7",
71
75
  "sinon": "^15.1.2",
72
76
  "sinon-chai": "^3.7.0",
73
77
  "typescript": "^5.1.3",