Skip to content

Commit 1319aa0

Browse files
committed
chore:SP-3670 handles pathname on scanner configs setup
1 parent 736e8c7 commit 1319aa0

File tree

7 files changed

+33
-91
lines changed

7 files changed

+33
-91
lines changed

src/sdk/BaseConfig.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs';
2+
import { logger } from "./Logger/Logger";
23

34
/**
45
* Base configuration class for SCANOSS SDK services.
@@ -42,7 +43,7 @@ export abstract class BaseConfig {
4243
this.IGNORE_CERT_ERRORS = config.IGNORE_CERT_ERRORS ?? false;
4344
this.API_URL = config.API_URL || BaseConfig.getDefaultURL();
4445
}
45-
this.API_URL = this.API_URL || BaseConfig.getDefaultURL();
46+
this.API_URL = BaseConfig.getDefaultURL();
4647
}
4748

4849
/**
@@ -69,7 +70,31 @@ export abstract class BaseConfig {
6970
* @param currentUrl - The current API URL
7071
* @returns The resolved API URL
7172
*/
72-
protected abstract resolveApiUrl(apiKey: string, currentUrl: string): string;
73+
/**
74+
* Resolves the appropriate scanner URL based on API key presence and current URL.
75+
* If an API key is provided and the current URL is the default, returns the premium
76+
* scanner URL, otherwise appends '/scan/direct' to the current URL.
77+
* @param apiKey - The API key (if any)
78+
* @param currentUrl - The current API URL
79+
* @returns The resolved scanner URL
80+
*/
81+
protected resolveApiUrl(apiKey: string, currentUrl: string): string {
82+
let url = new URL(currentUrl);
83+
if (url.pathname !== '/') {
84+
logger.warn(`Removing ${url.pathname} from ${currentUrl}`);
85+
currentUrl = url.origin;
86+
}
87+
if(!apiKey) {
88+
if (currentUrl !== BaseConfig.getDefaultURL()) {
89+
return currentUrl;
90+
}
91+
return BaseConfig.getDefaultURL();
92+
}
93+
if (currentUrl !== BaseConfig.getDefaultURL() && currentUrl !== BaseConfig.getPremiumURL()) {
94+
return currentUrl;
95+
}
96+
return BaseConfig.getPremiumURL();
97+
}
7398

7499
/**
75100
* Sets the HTTPS proxy server URL.

src/sdk/Clients/http/HttpClient.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class HttpClient extends Transport<Response> {
4242
if (caCertPath) Utils.loadCaCertFromFile(caCertPath);
4343
}
4444

45-
protected async get(url: string): Promise<Response> {
45+
public async get(url: string): Promise<Response> {
4646
return await fetch(url, {
4747
agent: this.proxyAgent,
4848
method: 'get',
@@ -52,7 +52,7 @@ export class HttpClient extends Transport<Response> {
5252
});
5353
}
5454

55-
protected async post(url: string, body: any): Promise<Response> {
55+
public async post(url: string, body: any): Promise<Response> {
5656
return await fetch(url, {
5757
agent: this.proxyAgent,
5858
method: 'post',
@@ -64,7 +64,7 @@ export class HttpClient extends Transport<Response> {
6464
});
6565
}
6666

67-
protected async delete(url: string): Promise<Response> {
67+
public async delete(url: string): Promise<Response> {
6868
return await fetch(url, {
6969
agent: this.proxyAgent,
7070
method: 'delete',
@@ -74,7 +74,7 @@ export class HttpClient extends Transport<Response> {
7474
});
7575
}
7676

77-
protected async put(url: string, body: FormData): Promise<Response> {
77+
public async put(url: string, body: FormData): Promise<Response> {
7878
return await fetch(url, {
7979
agent: this.proxyAgent,
8080
method: 'put',

src/sdk/Cryptography/CryptoCfg.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,6 @@ export class CryptoCfg extends BaseConfig {
2222
}
2323
}
2424

25-
/**
26-
* Resolves the appropriate API URL based on API key presence and current URL.
27-
* If an API key is provided and the current URL is the default, returns the premium
28-
* URL, otherwise returns the current URL.
29-
* @param apiKey - The API key (if any)
30-
* @param currentUrl - The current API URL
31-
* @returns The resolved API URL
32-
*/
33-
protected resolveApiUrl(apiKey: string, currentUrl: string): string {
34-
if(!apiKey) {
35-
if (currentUrl !== BaseConfig.getDefaultURL()) {
36-
return currentUrl;
37-
}
38-
return currentUrl;
39-
}
40-
if (currentUrl !== BaseConfig.getDefaultURL() && currentUrl !== BaseConfig.getPremiumURL()) {
41-
return currentUrl;
42-
}
43-
return BaseConfig.getPremiumURL();
44-
}
45-
4625
public get API_URL(): string{
4726
return this.resolveApiUrl(this.API_KEY, super.API_URL);
4827
}

src/sdk/Dependencies/DependencyScannerCfg.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,6 @@ export class DependencyScannerCfg extends BaseConfig {
3030
this._API_KEY = value;
3131
}
3232

33-
/**
34-
* Resolves the appropriate scanner URL based on API key presence and current URL.
35-
* If an API key is provided and the current URL is the default, returns the premium
36-
* scanner URL, otherwise appends '/scan/direct' to the current URL.
37-
* @param apiKey - The API key (if any)
38-
* @param currentUrl - The current API URL
39-
* @returns The resolved scanner URL
40-
*/
41-
protected resolveApiUrl(apiKey: string, currentUrl: string): string {
42-
if(!apiKey) {
43-
if (currentUrl !== BaseConfig.getDefaultURL()) {
44-
return currentUrl;
45-
}
46-
return currentUrl;
47-
}
48-
if (currentUrl !== BaseConfig.getDefaultURL() && currentUrl !== BaseConfig.getPremiumURL()) {
49-
return currentUrl;
50-
}
51-
return BaseConfig.getPremiumURL();
52-
}
53-
5433
get API_URL(): string {
5534
return this.resolveApiUrl(this.API_KEY, super.API_URL);
5635
}

src/sdk/Vulnerability/VulnerabilityCfg.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,6 @@ export class VulnerabilityCfg extends BaseConfig {
2424
this.API_KEY = config?.API_KEY ?? this.API_KEY;
2525
}
2626

27-
/**
28-
* Resolves the appropriate API URL based on API key presence and current URL. See: src/sdk/BaseConfig.ts
29-
* If an API key is provided and the current URL is the default, returns the premium
30-
* URL, otherwise returns the current URL.
31-
* @param apiKey - The API key (if any)
32-
* @param currentUrl - The current API URL
33-
* @returns The resolved API URL
34-
*/
35-
protected resolveApiUrl(apiKey: string, currentUrl: string): string {
36-
if(!apiKey) {
37-
if (currentUrl !== BaseConfig.getDefaultURL()) {
38-
return currentUrl;
39-
}
40-
return currentUrl;
41-
}
42-
if (currentUrl !== BaseConfig.getDefaultURL() && currentUrl !== BaseConfig.getPremiumURL()) {
43-
return currentUrl;
44-
}
45-
return BaseConfig.getPremiumURL();
46-
}
47-
4827
public get API_URL(): string{
4928
return this.resolveApiUrl(this.API_KEY, super.API_URL);
5029
}

src/sdk/scanner/Dispatcher/Dispatcher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ export class Dispatcher extends EventEmitter {
174174
let plain_response: string;
175175
try {
176176
this.emit(ScannerEvents.DISPATCHER_WFP_SENDED);
177-
const response = await fetch(this.scannerCfg.API_URL, {
177+
const scanURL = new URL('/scan/direct', this.scannerCfg.API_URL);
178+
const response = await fetch(scanURL.href, {
178179
agent: this.proxyAgent,
179180
method: 'post',
180181
body: item.getForm(),

src/sdk/scanner/ScannerCfg.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ export class ScannerCfg extends BaseConfig {
3737
super();
3838
}
3939

40-
/**
41-
* Resolves the appropriate scanner URL based on API key presence and current URL.
42-
* If an API key is provided and the current URL is the default, returns the premium
43-
* scanner URL, otherwise appends '/scan/direct' to the current URL.
44-
* @param apiKey - The API key (if any)
45-
* @param currentUrl - The current API URL
46-
* @returns The resolved scanner URL
47-
*/
48-
protected resolveApiUrl(apiKey: string, currentUrl: string): string {
49-
if(!apiKey) {
50-
if (currentUrl !== BaseConfig.getDefaultURL()) {
51-
return currentUrl;
52-
}
53-
return currentUrl + "/scan/direct";
54-
}
55-
if (currentUrl !== BaseConfig.getDefaultURL() && currentUrl !== BaseConfig.getPremiumURL()) {
56-
return currentUrl;
57-
}
58-
return BaseConfig.getPremiumURL() + "/scan/direct";
59-
}
60-
6140
get API_URL(): string {
6241
return this.resolveApiUrl(this.API_KEY, super.API_URL);
6342
}

0 commit comments

Comments
 (0)