@@ -2,7 +2,7 @@ import * as fsPromises from 'fs/promises';
2
2
import * as fs from 'fs' ;
3
3
import * as path from 'path' ;
4
4
import * as tar from 'tar' ;
5
- import axios from 'axios' ;
5
+ import axios , { AxiosRequestConfig } from 'axios' ;
6
6
import * as unzipper from 'unzipper' ;
7
7
import { logger } from "../wrapper/loggerConfig" ;
8
8
import { finished } from 'stream/promises' ;
@@ -57,7 +57,7 @@ export class CxInstaller {
57
57
return ;
58
58
}
59
59
}
60
-
60
+
61
61
await this . cleanDirectoryContents ( this . resourceDirPath ) ;
62
62
const url = await this . getDownloadURL ( ) ;
63
63
const zipPath = path . join ( this . resourceDirPath , this . getCompressFolderName ( ) ) ;
@@ -92,7 +92,7 @@ export class CxInstaller {
92
92
const fileStat = await fsPromises . stat ( filePath ) ;
93
93
94
94
if ( fileStat . isDirectory ( ) ) {
95
- await fsPromises . rm ( filePath , { recursive : true , force : true } ) ;
95
+ await fsPromises . rm ( filePath , { recursive : true , force : true } ) ;
96
96
logger . info ( `Directory ${ filePath } deleted.` ) ;
97
97
} else {
98
98
await fsPromises . unlink ( filePath ) ;
@@ -148,22 +148,58 @@ export class CxInstaller {
148
148
}
149
149
150
150
private async downloadFile ( url : string , outputPath : string ) {
151
- logger . info ( 'Downloading file from:' , url ) ;
151
+ logger . info ( `Starting download from URL: ${ url } ` ) ;
152
152
const writer = fs . createWriteStream ( outputPath ) ;
153
153
154
154
try {
155
- const response = await axios ( { url, responseType : 'stream' } ) ;
155
+ // Create base Axios configuration
156
+ const axiosConfig : AxiosRequestConfig = {
157
+ url,
158
+ responseType : 'stream' ,
159
+ } ;
160
+
161
+ // Configure proxy if HTTP_PROXY environment variable is set
162
+ const proxyUrl = process . env . HTTP_PROXY ;
163
+ if ( proxyUrl ) {
164
+ logger . info ( `Detected proxy configuration in HTTP_PROXY` ) ;
165
+ const parsedProxy = new URL ( proxyUrl ) ;
166
+
167
+ axiosConfig . proxy = {
168
+ host : parsedProxy . hostname ,
169
+ port : parseInt ( parsedProxy . port , 10 ) ,
170
+ protocol : parsedProxy . protocol . replace ( ':' , '' ) , // remove the colon
171
+ auth : parsedProxy . username && parsedProxy . password
172
+ ? { username : parsedProxy . username , password : parsedProxy . password }
173
+ : undefined , // Only include auth if credentials are provided
174
+ } ;
175
+
176
+ logger . info (
177
+ `Using proxy - Host: ${ axiosConfig . proxy . host } , Port: ${ axiosConfig . proxy . port } , ` +
178
+ `Protocol: ${ axiosConfig . proxy . protocol } , Auth: ${ axiosConfig . proxy . auth ? 'Yes' : 'No' } `
179
+ ) ;
180
+ } else {
181
+ logger . info ( 'No proxy configuration detected.' ) ;
182
+ }
183
+
184
+ // Perform the download request
185
+ logger . info ( `Initiating download request to: ${ url } ` ) ;
186
+ const response = await axios ( axiosConfig ) ;
187
+
188
+ // Pipe the response data to the output file
156
189
response . data . pipe ( writer ) ;
157
190
158
- await finished ( writer ) ; // Use stream promises to await the writer
159
- logger . info ( 'Download finished' ) ;
191
+ // Await the completion of the write stream
192
+ await finished ( writer ) ;
193
+ logger . info ( `Download completed successfully. File saved to: ${ outputPath } ` ) ;
160
194
} catch ( error ) {
161
- logger . error ( ' Error downloading file:' , error . message || error ) ;
195
+ logger . error ( ` Error downloading file from ${ url } : ${ error . message || error } ` ) ;
162
196
} finally {
163
197
writer . close ( ) ;
198
+ logger . info ( 'Write stream closed.' ) ;
164
199
}
165
200
}
166
-
201
+
202
+
167
203
private checkExecutableExists ( ) : boolean {
168
204
return fs . existsSync ( this . getExecutablePath ( ) ) ;
169
205
}
@@ -183,7 +219,7 @@ export class CxInstaller {
183
219
}
184
220
185
221
private getVersionFilePath ( ) : string {
186
- return path . join ( __dirname , '../../../checkmarx-ast-cli.version' ) ;
222
+ return path . join ( __dirname , '../../../checkmarx-ast-cli.version' ) ;
187
223
}
188
224
189
225
private getCompressFolderName ( ) : string {
0 commit comments