-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathproxy.util.ts
More file actions
74 lines (66 loc) · 1.97 KB
/
Copy pathproxy.util.ts
File metadata and controls
74 lines (66 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { AxiosRequestConfig } from 'axios';
import { WSClientConfigurableOptions } from '../src';
export function getTestProxy(): AxiosRequestConfig {
if (process.env.PROXY_ENABLED !== 'true') {
return {};
}
const host = process.env.PROXY_HOST;
const port = process.env.PROXY_PORT;
const user = process.env.PROXY_USER;
const pass = process.env.PROXY_PASS;
if (!host || !port || !user || !pass) {
throw new Error('One or more env vars missing for proxy support');
}
return {
proxy: {
host,
port: Number(port),
auth: {
username: user,
password: pass,
},
protocol: 'http',
},
};
}
/** Returns a WS proxy configuration used by end-to-end tests in Github Actions */
export function getWSTestProxy(): Partial<WSClientConfigurableOptions> {
if (process.env.PROXY_ENABLED !== 'true') {
return {};
}
const host = process.env.PROXY_HOST;
const port = process.env.PROXY_PORT;
const user = process.env.PROXY_USER;
const pass = process.env.PROXY_PASS;
if (!host || !port || !user || !pass) {
throw new Error('One or more env vars missing for proxy support');
}
console.log('WS Test proxy enabled...');
return {
wsOptions: {
agent: getHttpsProxyAgent(host, port, user, pass),
},
};
}
/**
* Returns an axios & websocket compatible proxy config using brightdata credentials
*/
export function getHttpsProxyAgent(
host: string,
port: string | number,
user: string,
pass: string,
): AxiosRequestConfig | undefined {
try {
// Optional dependency that might be missing
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { HttpsProxyAgent } = require('https-proxy-agent');
const url = `https://${user}:${pass}@${host}:${port}`;
return new HttpsProxyAgent(url);
} catch (e) {
const msg =
'Failed to prepare https proxy config - proxy agent dependency not installed';
console.error(new Date(), msg, e);
throw new Error(msg);
}
}