server-esm: Handle proxy agent

This commit is contained in:
Elian Doran 2024-07-18 23:40:32 +03:00
parent 0a7828b9ae
commit da0829245f
No known key found for this signature in database

View File

@ -33,7 +33,7 @@ interface Client {
request(opts: ClientOpts): Request;
}
function exec<T>(opts: ExecOpts): Promise<T> {
async function exec<T>(opts: ExecOpts): Promise<T> {
const client = getClient(opts);
// hack for cases where electron.net does not work, but we don't want to set proxy
@ -47,7 +47,7 @@ function exec<T>(opts: ExecOpts): Promise<T> {
requestId: 'n/a'
};
const proxyAgent = getProxyAgent(opts);
const proxyAgent = await getProxyAgent(opts);
const parsedTargetUrl = url.parse(opts.url);
return new Promise((resolve, reject) => {
@ -137,7 +137,7 @@ function exec<T>(opts: ExecOpts): Promise<T> {
});
}
function getImage(imageUrl: string): Promise<Buffer> {
async function getImage(imageUrl: string): Promise<Buffer> {
const proxyConf = syncOptions.getSyncProxy();
const opts: ClientOpts = {
method: 'GET',
@ -146,7 +146,7 @@ function getImage(imageUrl: string): Promise<Buffer> {
};
const client = getClient(opts);
const proxyAgent = getProxyAgent(opts);
const proxyAgent = await getProxyAgent(opts);
const parsedTargetUrl = url.parse(opts.url);
return new Promise<Buffer>((resolve, reject) => {
@ -189,7 +189,7 @@ function getImage(imageUrl: string): Promise<Buffer> {
const HTTP = 'http:', HTTPS = 'https:';
function getProxyAgent(opts: ClientOpts) {
async function getProxyAgent(opts: ClientOpts) {
if (!opts.proxy) {
return null;
}
@ -201,8 +201,8 @@ function getProxyAgent(opts: ClientOpts) {
}
const AgentClass = HTTP === protocol
? require('http-proxy-agent').HttpProxyAgent
: require('https-proxy-agent').HttpsProxyAgent;
? (await import('http-proxy-agent')).HttpProxyAgent
: (await import('https-proxy-agent')).HttpsProxyAgent;
return new AgentClass(opts.proxy);
}