|
1 |
| -import {logger} from '../wrapper/loggerConfig'; |
2 |
| -import * as fs from 'fs'; |
3 |
| -import {finished} from 'stream/promises'; |
4 |
| -import {Client} from "./Client"; |
5 |
| -import {CxError} from "../errors/CxError"; |
| 1 | +import { logger } from '../wrapper/loggerConfig'; |
| 2 | +import * as toolLib from 'azure-pipelines-tool-lib/tool'; |
| 3 | +import { CxError } from "../errors/CxError"; |
| 4 | +import * as tunnel from 'tunnel'; |
6 | 5 |
|
7 | 6 | export class AstClient {
|
8 |
| - private client: Client; |
9 |
| - |
10 |
| - constructor(client: Client) { |
11 |
| - this.client = client; |
12 |
| - } |
13 |
| - |
14 | 7 | public async downloadFile(url: string, outputPath: string): Promise<void> {
|
15 | 8 | logger.info(`Starting download from URL: ${url}`);
|
16 |
| - const writer = fs.createWriteStream(outputPath); |
| 9 | + |
| 10 | + const requestHandlers: any[] = []; |
| 11 | + |
| 12 | + if (process.env.HTTP_PROXY) { |
| 13 | + try { |
| 14 | + const proxyUrl = process.env.HTTP_PROXY; |
| 15 | + const parsedUrl = new URL(proxyUrl); |
| 16 | + |
| 17 | + const proxyPort = parsedUrl.port ? Number(parsedUrl.port) : 80; |
| 18 | + |
| 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}`; |
| 23 | + } |
| 24 | + |
| 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.'); |
| 47 | + } |
| 48 | + |
17 | 49 | try {
|
18 |
| - const response = await this.client.request(url, 'GET', null); |
19 |
| - response.data.pipe(writer); |
20 |
| - await finished(writer); |
21 |
| - logger.info(`Download completed successfully. File saved to: ${outputPath}`); |
| 50 | + const downloadedPath = await toolLib.downloadTool(url, outputPath, requestHandlers); |
| 51 | + logger.info(`Download completed successfully. File saved to: ${downloadedPath}`); |
22 | 52 | } catch (error) {
|
23 | 53 | logger.error(`Error downloading file from ${url}: ${error.message || error}`);
|
24 | 54 | throw new CxError(error.message || error);
|
25 |
| - } finally { |
26 |
| - writer.close(); |
27 | 55 | }
|
28 | 56 | }
|
29 | 57 | }
|
0 commit comments