Skip to content

Commit a3c5232

Browse files
committed
replace axios with azure tool lib requests
1 parent 8de0b85 commit a3c5232

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

src/main/client/AstClient.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
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';
65

76
export class AstClient {
8-
private client: Client;
9-
10-
constructor(client: Client) {
11-
this.client = client;
12-
}
13-
147
public async downloadFile(url: string, outputPath: string): Promise<void> {
158
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+
1749
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}`);
2252
} catch (error) {
2353
logger.error(`Error downloading file from ${url}: ${error.message || error}`);
2454
throw new CxError(error.message || error);
25-
} finally {
26-
writer.close();
2755
}
2856
}
2957
}

0 commit comments

Comments
 (0)