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 | 10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
16x
16x
30x
30x
30x
37x
2x
2x
37x
37x
37x
37x
37x
37x
37x
37x
1x
1x
1x
1x
1x
1x
| import http from "http";
import https from "https";
import {injectable} from "inversify";
import request, {CoreOptions} from "request";
import {KEEP_ALIVE_MSECS, PUZZLE_MAX_SOCKETS} from "./config";
import warden from "puzzle-warden";
export interface IRequestOptions {
timeout: number;
method: string;
}
const AGENT_CONFIGURATION = {
keepAlive: true,
maxSockets: PUZZLE_MAX_SOCKETS,
keepAliveMsecs: KEEP_ALIVE_MSECS
};
export const httpAgent = new http.Agent(AGENT_CONFIGURATION);
export const httpsAgent = new https.Agent(AGENT_CONFIGURATION);
@injectable()
export class HttpClient {
private httpAgent: http.Agent;
private httpsAgent: https.Agent;
private static httpClient: request.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>;
private static httpsClient: request.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>;
private defaultOptions: CoreOptions;
constructor() {
this.httpAgent = httpAgent;
this.httpsAgent = httpsAgent;
}
init(clientName: string, options?: request.CoreOptions) {
this.defaultOptions = {
encoding: 'utf8',
gzip: true,
...options,
headers: {
'user-agent': clientName || 'PuzzleJs Http Client'
},
};
HttpClient.httpClient = request.defaults({
...this.defaultOptions,
agent: httpAgent
});
HttpClient.httpsClient = request.defaults({
...this.defaultOptions,
agent: httpsAgent
});
}
get(requestUrl: string, fragmentName: string, options?: request.CoreOptions): Promise<{ response: request.Response, data: any }> {
if (!HttpClient.httpClient && !HttpClient.httpsClient) {
console.error('Creating new agent for pool');
this.init('PuzzleJs Default Client');
}
const client = requestUrl.startsWith('https') ? HttpClient.httpsClient : HttpClient.httpClient;
return new Promise(function (resolve, reject) {
const requestOptions = {
url: requestUrl,
method: 'get',
...options
} as any;
const cb: request.RequestCallback = (err, response, data) => {
if (err) reject(err);
resolve({
response,
data
});
};
Iif (warden.isRouteRegistered(fragmentName)) {
warden.request(fragmentName, requestOptions, cb);
} else {
client(requestOptions, cb);
}
});
}
post(requestUrl: string, fragmentName: string, data?: object, options?: request.CoreOptions): Promise<{ response: request.Response, data: any }> {
Iif (!HttpClient.httpClient && !HttpClient.httpsClient) this.init('PuzzleJs Default Client');
const client = requestUrl.startsWith('https') ? HttpClient.httpsClient : HttpClient.httpClient;
return new Promise(function (resolve, reject) {
client
.post({
url: requestUrl,
json: data,
...options
}, (err, response, data) => {
Iif (err) reject(err);
resolve({
response,
data
});
});
});
}
}
|