-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathHttpConnection.ts
113 lines (94 loc) · 3.21 KB
/
HttpConnection.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import thrift from 'thrift';
import https from 'https';
import http from 'http';
import { HeadersInit } from 'node-fetch';
import { ProxyAgent } from 'proxy-agent';
import IConnectionProvider from '../contracts/IConnectionProvider';
import IConnectionOptions, { ProxyOptions } from '../contracts/IConnectionOptions';
import globalConfig from '../../globalConfig';
import ThriftHttpConnection from './ThriftHttpConnection';
export default class HttpConnection implements IConnectionProvider {
private readonly options: IConnectionOptions;
private headers: HeadersInit = {};
private connection?: ThriftHttpConnection;
private agent?: http.Agent;
constructor(options: IConnectionOptions) {
this.options = options;
}
public setHeaders(headers: HeadersInit) {
this.headers = headers;
this.connection?.setHeaders({
...this.options.headers,
...this.headers,
});
}
public async getAgent(): Promise<http.Agent> {
if (!this.agent) {
if (this.options.proxy !== undefined) {
this.agent = this.createProxyAgent(this.options.proxy);
} else {
this.agent = this.options.https ? this.createHttpsAgent() : this.createHttpAgent();
}
}
return this.agent;
}
private getAgentDefaultOptions(): http.AgentOptions {
return {
keepAlive: true,
maxSockets: 5,
keepAliveMsecs: 10000,
timeout: this.options.socketTimeout ?? globalConfig.socketTimeout,
};
}
private createHttpAgent(): http.Agent {
const httpAgentOptions = this.getAgentDefaultOptions();
return new http.Agent(httpAgentOptions);
}
private createHttpsAgent(): https.Agent {
const httpsAgentOptions: https.AgentOptions = {
...this.getAgentDefaultOptions(),
minVersion: 'TLSv1.2',
rejectUnauthorized: !!this.options.rejectUnauthorized,
ca: this.options.ca,
cert: this.options.cert,
key: this.options.key,
};
return new https.Agent(httpsAgentOptions);
}
private createProxyAgent(proxyOptions: ProxyOptions): ProxyAgent {
const proxyAuth = proxyOptions.auth?.username
? `${proxyOptions.auth.username}:${proxyOptions.auth?.password ?? ''}@`
: '';
const proxyUrl = `${proxyOptions.protocol}://${proxyAuth}${proxyOptions.host}:${proxyOptions.port}`;
const proxyProtocol = `${proxyOptions.protocol}:`;
return new ProxyAgent({
...this.getAgentDefaultOptions(),
getProxyForUrl: () => proxyUrl,
httpsAgent: this.createHttpsAgent(),
httpAgent: this.createHttpAgent(),
protocol: proxyProtocol,
});
}
public async getThriftConnection(): Promise<any> {
if (!this.connection) {
const { options } = this;
const agent = await this.getAgent();
this.connection = new ThriftHttpConnection(
{
url: `${options.https ? 'https' : 'http'}://${options.host}:${options.port}${options.path ?? '/'}`,
transport: thrift.TBufferedTransport,
protocol: thrift.TBinaryProtocol,
},
{
agent,
timeout: options.socketTimeout ?? globalConfig.socketTimeout,
headers: {
...options.headers,
...this.headers,
},
},
);
}
return this.connection;
}
}