Skip to content

Commit ad01894

Browse files
committed
Tidy up code a bit
Signed-off-by: Levko Kravets <levko.ne@gmail.com>
1 parent f25616a commit ad01894

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

lib/connection/connections/HttpConnection.ts

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,55 @@ export default class HttpConnection implements IConnectionProvider {
3838

3939
public async getAgent(): Promise<http.Agent> {
4040
if (!this.agent) {
41-
const { options } = this;
42-
43-
const httpAgentOptions: http.AgentOptions = {
44-
keepAlive: true,
45-
maxSockets: 5,
46-
keepAliveMsecs: 10000,
47-
timeout: options.socketTimeout ?? globalConfig.socketTimeout,
48-
};
49-
50-
const httpsAgentOptions: https.AgentOptions = {
51-
...httpAgentOptions,
52-
minVersion: 'TLSv1.2',
53-
rejectUnauthorized: false,
54-
ca: options.ca,
55-
cert: options.cert,
56-
key: options.key,
57-
};
58-
59-
if (options.proxy !== undefined) {
60-
const proxyUrl = buildProxyUrl(options.proxy);
61-
const proxyProtocol = `${options.proxy.protocol}:`;
62-
63-
this.agent = new ProxyAgent({
64-
...httpAgentOptions,
65-
getProxyForUrl: () => proxyUrl,
66-
httpsAgent: new https.Agent(httpsAgentOptions),
67-
httpAgent: new http.Agent(httpAgentOptions),
68-
protocol: proxyProtocol,
69-
});
41+
if (this.options.proxy !== undefined) {
42+
this.agent = this.createProxyAgent(this.options.proxy);
7043
} else {
71-
this.agent = options.https ? new https.Agent(httpsAgentOptions) : new http.Agent(httpAgentOptions);
44+
this.agent = this.options.https ? this.createHttpsAgent() : this.createHttpAgent();
7245
}
7346
}
7447

7548
return this.agent;
7649
}
7750

51+
private getAgentDefaultOptions(): http.AgentOptions {
52+
return {
53+
keepAlive: true,
54+
maxSockets: 5,
55+
keepAliveMsecs: 10000,
56+
timeout: this.options.socketTimeout ?? globalConfig.socketTimeout,
57+
};
58+
}
59+
60+
private createHttpAgent(): http.Agent {
61+
const httpAgentOptions = this.getAgentDefaultOptions();
62+
return new http.Agent(httpAgentOptions);
63+
}
64+
65+
private createHttpsAgent(): https.Agent {
66+
const httpsAgentOptions: https.AgentOptions = {
67+
...this.getAgentDefaultOptions(),
68+
minVersion: 'TLSv1.2',
69+
rejectUnauthorized: false,
70+
ca: this.options.ca,
71+
cert: this.options.cert,
72+
key: this.options.key,
73+
};
74+
return new https.Agent(httpsAgentOptions);
75+
}
76+
77+
private createProxyAgent(proxyOptions: ProxyOptions): ProxyAgent {
78+
const proxyUrl = buildProxyUrl(proxyOptions);
79+
const proxyProtocol = `${proxyOptions.protocol}:`;
80+
81+
return new ProxyAgent({
82+
...this.getAgentDefaultOptions(),
83+
getProxyForUrl: () => proxyUrl,
84+
httpsAgent: this.createHttpsAgent(),
85+
httpAgent: this.createHttpAgent(),
86+
protocol: proxyProtocol,
87+
});
88+
}
89+
7890
public async getThriftConnection(): Promise<any> {
7991
if (!this.connection) {
8092
const { options } = this;

0 commit comments

Comments
 (0)