@@ -4,52 +4,65 @@ import { CxError } from "../errors/CxError";
4
4
import * as tunnel from 'tunnel' ;
5
5
6
6
export class AstClient {
7
- public async downloadFile ( url : string , outputPath : string ) : Promise < void > {
8
- logger . info ( `Starting download from URL: ${ url } ` ) ;
7
+ /**
8
+ * Creates a request handler with a proxy agent if the HTTP_PROXY environment variable is set.
9
+ * Returns `undefined` if no proxy is configured or if an error occurs while parsing the proxy URL.
10
+ */
11
+ private createProxyRequestHandler ( ) : any | undefined {
12
+ const proxyEnv = process . env . HTTP_PROXY ;
13
+ if ( ! proxyEnv ) {
14
+ logger . info ( 'No proxy configured; proceeding with direct download.' ) ;
15
+ return undefined ;
16
+ }
9
17
10
- const requestHandlers : any [ ] = [ ] ;
18
+ try {
19
+ const proxyUrl = new URL ( proxyEnv ) ;
20
+ if ( proxyUrl . port === '' ) {
21
+ logger . error ( `Invalid proxy URL: ${ proxyUrl } . Port is missing. Proceeding without proxy agent.` ) ;
22
+ return undefined ;
23
+ }
24
+
25
+ const proxyAuth = proxyUrl . username && proxyUrl . password ?
26
+ `${ proxyUrl . username } :${ proxyUrl . password } `
27
+ : undefined ;
11
28
12
- if ( process . env . HTTP_PROXY ) {
13
- try {
14
- const proxyUrl = process . env . HTTP_PROXY ;
15
- const parsedUrl = new URL ( proxyUrl ) ;
29
+ const agent = tunnel . httpsOverHttp ( {
30
+ proxy : {
31
+ host : proxyUrl . hostname ,
32
+ port : Number ( proxyUrl . port ) ,
33
+ proxyAuth,
34
+ }
35
+ } ) ;
16
36
17
- const proxyPort = parsedUrl . port ? Number ( parsedUrl . port ) : 80 ;
37
+ logger . info ( `Using proxy agent for host: ${ proxyUrl . hostname } and port: ${ proxyUrl . port } ` ) ;
18
38
19
- // Extract credentials if provided in the URL (e.g., http://username:password@host:port)
20
- let proxyAuth : string | undefined = undefined ;
21
- if ( parsedUrl . username && parsedUrl . password ) {
22
- proxyAuth = ` ${ parsedUrl . username } : ${ parsedUrl . password } ` ;
39
+ return {
40
+ prepareRequest : ( options : any ) : any => {
41
+ options . agent = agent ;
42
+ return options ;
23
43
}
44
+ } ;
45
+ } catch ( error ) {
46
+ logger . error (
47
+ `Error parsing HTTP_PROXY value: ${ proxyEnv } . Proceeding without proxy agent. Error: ${ error } `
48
+ ) ;
49
+ return undefined ;
50
+ }
51
+ }
24
52
25
- const agent = tunnel . httpsOverHttp ( {
26
- proxy : {
27
- host : parsedUrl . hostname ,
28
- port : proxyPort ,
29
- proxyAuth : proxyAuth ,
30
- }
31
- } ) ;
32
-
33
- logger . info ( `Using proxy agent for host: ${ parsedUrl . hostname } and port: ${ proxyPort } ` ) ;
34
-
35
- // Add a handler that applies the proxy agent to each request.
36
- requestHandlers . push ( {
37
- prepareRequest : ( options : any ) : any => {
38
- options . agent = agent ;
39
- return options ;
40
- }
41
- } ) ;
42
- } catch ( err ) {
43
- logger . error ( `Error parsing HTTP_PROXY value: ${ process . env . HTTP_PROXY } . Proceeding without proxy agent. Error: ${ err } ` ) ;
44
- }
45
- } else {
46
- logger . info ( 'No proxy configured; proceeding with direct download.' ) ;
53
+ public async downloadFile ( url : string , outputPath : string ) : Promise < void > {
54
+ logger . info ( `Starting download from URL: ${ url } ` ) ;
55
+
56
+ const requestHandlers : any [ ] = [ ] ;
57
+ const proxyHandler = this . createProxyRequestHandler ( ) ;
58
+ if ( proxyHandler ) {
59
+ requestHandlers . push ( proxyHandler ) ;
47
60
}
48
61
49
62
try {
50
63
const downloadedPath = await toolLib . downloadTool ( url , outputPath , requestHandlers ) ;
51
64
logger . info ( `Download completed successfully. File saved to: ${ downloadedPath } ` ) ;
52
- } catch ( error ) {
65
+ } catch ( error : any ) {
53
66
logger . error ( `Error downloading file from ${ url } : ${ error . message || error } ` ) ;
54
67
throw new CxError ( error . message || error ) ;
55
68
}
0 commit comments