Skip to content

Commit df678e7

Browse files
author
Giovanni Henrique Marschall Zadinello
committed
fixes para mais cves e perfomance
1 parent c189600 commit df678e7

File tree

6 files changed

+400
-333
lines changed

6 files changed

+400
-333
lines changed

src/commands/ping.ts

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ import { colors } from '../utils/colors';
33
import { createSpinner } from '../utils/spinner';
44
import { logger } from '../utils/logger';
55
import { config } from '../utils/config';
6-
import { mongoApiClient } from '../services/mongoApi';
6+
import { ApiClient } from '../services/api';
77

88
/**
99
* Ping command to test API connectivity
1010
*/
1111
export const pingCommand = new Command('ping')
12-
.description('Test connectivity to vulnerability analysis APIs')
12+
.description('Test connectivity to vulnerability analysis API')
1313
.option('--verbose', 'show detailed connection information')
1414
.action(async (options) => {
1515
console.log(colors.title('🏓 Vulnify API Connectivity Test'));
1616
console.log('');
1717

1818
if (options.verbose) {
1919
console.log(colors.muted('Configuration:'));
20-
console.log(colors.muted(` MongoDB API URL: ${config.getMongoApiUrl() || 'not set'}`));
21-
console.log(colors.muted(` Fallback API URL: ${config.getApiUrl()}`));
20+
console.log(colors.muted(` API URL: ${config.getApiUrl()}`));
2221
console.log(colors.muted(` Timeout: ${config.getTimeout()}ms`));
2322
console.log('');
2423
}
@@ -27,72 +26,72 @@ export const pingCommand = new Command('ping')
2726
spinner.start();
2827

2928
try {
30-
// Test connectivity to both APIs
31-
const connectivity = await mongoApiClient.testConnectivity();
29+
// Test connectivity to API - simple HTTP check
30+
const apiClient = new ApiClient();
31+
const startTime = Date.now();
32+
33+
// Try to make a simple request to test connectivity
34+
// We'll catch the error but if we get a response (even an error), it means the API is reachable
35+
try {
36+
await apiClient.analyze({
37+
ecosystem: 'npm',
38+
dependencies: []
39+
});
40+
} catch (error) {
41+
// If we get a validation error, it means the API is reachable
42+
if (error instanceof Error && (
43+
error.message.includes('dependencies') ||
44+
error.message.includes('At least one dependency is required')
45+
)) {
46+
// This is expected - empty dependencies array causes validation error
47+
// but it means the API is responding
48+
} else {
49+
throw error;
50+
}
51+
}
52+
53+
const responseTime = Date.now() - startTime;
3254

3355
spinner.stop();
3456
console.log('');
3557

36-
// Display MongoDB API results
37-
console.log(colors.info('📡 MongoDB API Service:'));
38-
if (connectivity.mongodb.available) {
39-
console.log(colors.success(` ✅ Available (${connectivity.mongodb.responseTime}ms)`));
40-
} else {
41-
console.log(colors.error(` ❌ Unavailable: ${connectivity.mongodb.error}`));
42-
}
58+
// Display API results
59+
console.log(colors.info('📡 API Service:'));
60+
console.log(colors.success(` ✅ Available (${responseTime}ms)`));
4361

44-
// Display Fallback API results
62+
// Overall status
4563
console.log('');
46-
console.log(colors.info('🔄 Fallback API Service:'));
47-
if (connectivity.fallback.available) {
48-
console.log(colors.success(` ✅ Available (${connectivity.fallback.responseTime}ms)`));
49-
} else {
50-
console.log(colors.error(` ❌ Unavailable: ${connectivity.fallback.error || 'Not configured'}`));
51-
}
64+
console.log(colors.success('🎉 API service is available!'));
65+
console.log(colors.info('💡 Ready to analyze dependencies for vulnerabilities'));
5266

53-
// Overall status
5467
console.log('');
55-
if (connectivity.mongodb.available || connectivity.fallback.available) {
56-
console.log(colors.success('🎉 At least one API service is available!'));
57-
58-
if (connectivity.mongodb.available) {
59-
console.log(colors.info('💡 MongoDB API will be used for faster analysis'));
60-
} else {
61-
console.log(colors.warning('⚠️ Using fallback API (may be slower)'));
62-
}
68+
console.log(colors.muted('Use "vulnify test" to start analyzing your project'));
69+
70+
} catch (error) {
71+
spinner.fail('❌ Connectivity test failed');
72+
73+
console.log('');
74+
console.log(colors.error('Error details:'));
75+
if (error instanceof Error) {
76+
console.log(colors.error(` ${error.message}`));
6377
} else {
64-
console.log(colors.error('❌ No API services are available'));
65-
console.log('');
66-
console.log(colors.warning('💡 Troubleshooting tips:'));
67-
console.log(' • Check your internet connection');
68-
console.log(' • Verify API URLs are correct');
69-
console.log(' • Check if services are running');
70-
console.log(' • Try increasing timeout with --timeout option');
71-
}
72-
73-
// Show configuration help
74-
if (!connectivity.mongodb.available && !connectivity.fallback.available) {
75-
console.log('');
76-
console.log(colors.info('🔧 Configuration:'));
77-
console.log(' Set MongoDB API URL:');
78-
console.log(colors.muted(' export VULNIFY_MONGO_API_URL="https://your-lambda-url"'));
79-
console.log(' Set fallback API URL:');
80-
console.log(colors.muted(' export VULNIFY_API_URL="https://api.vulnify.io"'));
78+
console.log(colors.error(' Unknown error occurred'));
8179
}
8280

83-
} catch (error) {
84-
spinner.stop();
8581
console.log('');
86-
console.log(colors.error('❌ Connectivity test failed'));
87-
console.log(colors.error(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
88-
82+
console.log(colors.warning('💡 Troubleshooting:'));
83+
console.log(' • Check your internet connection');
84+
console.log(' • Verify the API endpoint is accessible');
85+
console.log(' • Try again in a few moments');
86+
console.log(' • Use --verbose for more details');
87+
8988
if (options.verbose) {
9089
logger.error('Ping command failed', {
9190
error: error instanceof Error ? error.message : 'Unknown error',
9291
stack: error instanceof Error ? error.stack : undefined
9392
});
9493
}
95-
94+
9695
process.exit(1);
9796
}
9897
});

src/services/api.ts

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,42 +82,55 @@ export class ApiClient {
8282
throw this.handleError(error);
8383
}
8484
}
85-
86-
/**
87-
* Get API statistics
88-
*/
89-
async getStats(): Promise<ApiStats> {
90-
try {
91-
const response: AxiosResponse<ApiStats> = await this.client.get('api/v1/analyze/stats');
92-
return response.data;
93-
} catch (error) {
94-
throw this.handleError(error);
95-
}
85+
/**
86+
* Get API information
87+
*/
88+
async getInfo(): Promise<ApiInfo> {
89+
try {
90+
const response = await this.client.request<ApiInfo>({
91+
url: '/api/v1/info',
92+
method: 'GET',
93+
data: {} // força envio de JSON vazio
94+
});
95+
return response.data;
96+
} catch (error) {
97+
throw this.handleError(error);
9698
}
99+
}
97100

98-
/**
99-
* Get API information
100-
*/
101-
async getInfo(): Promise<ApiInfo> {
102-
try {
103-
const response: AxiosResponse<ApiInfo> = await this.client.get('/info');
104-
return response.data;
105-
} catch (error) {
106-
throw this.handleError(error);
107-
}
101+
/**
102+
* Get API statistics and health information
103+
*/
104+
async getStats(): Promise<ApiStats> {
105+
try {
106+
const response = await this.client.request<ApiStats>({
107+
url: '/api/v1/stats',
108+
method: 'GET',
109+
data: {} // força envio de JSON vazio
110+
});
111+
return response.data;
112+
} catch (error) {
113+
throw this.handleError(error);
108114
}
115+
}
109116

110-
/**
111-
* Health check
112-
*/
113-
async healthCheck(): Promise<{ status: string; timestamp: string }> {
114-
try {
115-
const response = await this.client.get('/health');
116-
return response.data;
117-
} catch (error) {
118-
throw this.handleError(error);
119-
}
117+
/**
118+
* Health check
119+
*/
120+
async healthCheck(): Promise<{ status: string; timestamp: string }> {
121+
try {
122+
const response = await this.client.request({
123+
url: '/api/v1/health',
124+
method: 'GET',
125+
data: {} // força envio de JSON vazio
126+
});
127+
return response.data;
128+
} catch (error) {
129+
throw this.handleError(error);
120130
}
131+
}
132+
133+
121134

122135
/**
123136
* Handle API errors and convert to user-friendly messages

0 commit comments

Comments
 (0)