Skip to content

Commit f854eab

Browse files
committed
chore: refactor addProxyAgent and revert removed typedefs
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent 3afee05 commit f854eab

6 files changed

Lines changed: 30 additions & 36 deletions

File tree

src/analysis.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,12 @@ import fs from "node:fs";
22
import path from "node:path";
33
import { EOL } from "os";
44

5-
import { HttpsProxyAgent } from "https-proxy-agent";
6-
75
import { runLicenseCheck } from "./license/index.js";
86
import { generateImageSBOM, parseImageRef } from "./oci_image/utils.js";
9-
import { getCustom, getTokenHeaders , TRUSTIFY_DA_OPERATION_TYPE_HEADER, TRUSTIFY_DA_PACKAGE_MANAGER_HEADER } from "./tools.js";
7+
import { addProxyAgent, getCustom, getTokenHeaders , TRUSTIFY_DA_OPERATION_TYPE_HEADER, TRUSTIFY_DA_PACKAGE_MANAGER_HEADER } from "./tools.js";
108

119
export default { requestComponent, requestStack, requestImages, validateToken }
1210

13-
/**
14-
* Adds proxy agent configuration to fetch options if a proxy URL is specified
15-
* @param {RequestInit} options - The base fetch options
16-
* @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
17-
* @returns {RequestInit} The fetch options with proxy agent if applicable
18-
*/
19-
function addProxyAgent(options, opts) {
20-
const proxyUrl = getCustom('TRUSTIFY_DA_PROXY_URL', null, opts);
21-
if (proxyUrl) {
22-
options.agent = new HttpsProxyAgent(proxyUrl);
23-
}
24-
return options;
25-
}
26-
2711
/**
2812
* Send a stack analysis request and get the report as 'text/html' or 'application/json'.
2913
* @param {import('./provider').Provider} provider - the provided data for constructing the request

src/license/licenses_api.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
* @see https://github.com/guacsec/trustify-da-api-spec/blob/main/api/v5/openapi.yaml
66
*/
77

8-
import { HttpsProxyAgent } from 'https-proxy-agent';
9-
108
import { selectTrustifyDABackend } from '../index.js';
11-
import { getCustom , getTokenHeaders } from '../tools.js';
9+
import { addProxyAgent, getTokenHeaders } from '../tools.js';
1210

1311
/**
1412
* Fetch license details by SPDX identifier from the backend GET /api/v5/licenses/{spdx}.
@@ -24,18 +22,13 @@ export async function getLicenseDetails(spdxId, opts = {}) {
2422
const url = selectTrustifyDABackend(opts);
2523
const finalUrl = new URL(`${url}/api/v5/licenses/${encodeURIComponent(spdxId)}`);
2624

27-
const fetchOptions = {
25+
const fetchOptions = addProxyAgent({
2826
method: 'GET',
2927
headers: {
3028
'Accept': 'application/json',
3129
...getTokenHeaders(opts)
3230
},
33-
};
34-
35-
const proxyUrl = getCustom('TRUSTIFY_DA_PROXY_URL', null, opts);
36-
if (proxyUrl) {
37-
fetchOptions.agent = new HttpsProxyAgent(proxyUrl);
38-
}
31+
}, opts);
3932

4033
try {
4134
const resp = await fetch(finalUrl, fetchOptions);

src/license/project_license.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import path from 'node:path';
88

99
import { selectTrustifyDABackend } from '../index.js';
1010
import { matchForLicense, availableProviders } from '../provider.js';
11-
import { getCustom, getTokenHeaders } from '../tools.js';
11+
import { addProxyAgent, getTokenHeaders } from '../tools.js';
1212

1313
const LICENSE_FILES = ['LICENSE', 'LICENSE.md', 'LICENSE.txt'];
1414

@@ -67,20 +67,14 @@ export async function identifyLicense(licenseFilePath, opts = {}) {
6767
const backendUrl = selectTrustifyDABackend(opts);
6868
const url = new URL(`${backendUrl}/licenses/identify`);
6969
const tokenHeaders = getTokenHeaders(opts);
70-
const fetchOptions = {
70+
const fetchOptions = addProxyAgent({
7171
method: 'POST',
7272
headers: {
7373
'Content-Type': 'application/octet-stream',
7474
...tokenHeaders,
7575
},
7676
body: fileContent,
77-
};
78-
79-
const proxyUrl = getCustom('TRUSTIFY_DA_PROXY_URL', null, opts);
80-
if (proxyUrl) {
81-
const { HttpsProxyAgent } = await import('https-proxy-agent');
82-
fetchOptions.agent = new HttpsProxyAgent(proxyUrl);
83-
}
77+
}, opts);
8478

8579
const resp = await fetch(url, fetchOptions);
8680
if (!resp.ok) {

src/providers/base_java.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { PackageURL } from 'packageurl-js'
55

66
import { getCustomPath, getGitRootDir, getWrapperPreference, invokeCommand } from "../tools.js"
77

8+
/** @typedef {import('../provider').Provider} */
9+
10+
/** @typedef {import('../provider').Provided} Provided */
11+
812
/** @typedef {{name: string, version: string}} Package */
913

1014
/** @typedef {{groupId: string, artifactId: string, version: string, scope: string, ignore: boolean}} Dependency */

src/providers/base_javascript.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } fro
77

88
import Manifest from './manifest.js';
99

10+
/** @typedef {import('../provider').Provider} */
11+
12+
/** @typedef {import('../provider').Provided} Provided */
13+
1014
/**
1115
* The ecosystem identifier for JavaScript/npm packages
1216
* @type {string}

src/tools.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { execFileSync } from "child_process";
22
import { EOL } from "os";
33

4+
import { HttpsProxyAgent } from "https-proxy-agent";
45
import { PackageURL } from "packageurl-js";
56

67
export const RegexNotToBeLogged = /TRUSTIFY_DA_(.*_)?TOKEN|ex-.*-token|trust-.*-token/
@@ -178,6 +179,20 @@ export const TRUSTIFY_DA_SOURCE_HEADER = "trust-da-source"
178179
export const TRUSTIFY_DA_OPERATION_TYPE_HEADER = "trust-da-operation-type"
179180
export const TRUSTIFY_DA_PACKAGE_MANAGER_HEADER = "trust-da-pkg-manager"
180181

182+
/**
183+
* Adds proxy agent configuration to fetch options if a proxy URL is specified
184+
* @param {RequestInit} options - The base fetch options
185+
* @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
186+
* @returns {RequestInit} The fetch options with proxy agent if applicable
187+
*/
188+
export function addProxyAgent(options, opts) {
189+
const proxyUrl = getCustom('TRUSTIFY_DA_PROXY_URL', null, opts);
190+
if (proxyUrl) {
191+
options.agent = new HttpsProxyAgent(proxyUrl);
192+
}
193+
return options;
194+
}
195+
181196
/**
182197
* Utility function for fetching vendor tokens
183198
* @param {import("index.js").Options} [opts={}] - optional various options to pass along the application

0 commit comments

Comments
 (0)