-
Notifications
You must be signed in to change notification settings - Fork 764
/
Copy pathNodeHttpClient.ts
143 lines (124 loc) · 3.87 KB
/
NodeHttpClient.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as http_ from 'http';
import * as https_ from 'https';
import {RequestHeaders, RequestData} from '../Types.js';
import {
HttpClient,
HttpClientResponse,
HttpClientResponseInterface,
} from './HttpClient.js';
// `import * as http_ from 'http'` creates a "Module Namespace Exotic Object"
// which is immune to monkey-patching, whereas http_.default (in an ES Module context)
// will resolve to the same thing as require('http'), which is
// monkey-patchable. We care about this because users in their test
// suites might be using a library like "nock" which relies on the ability
// to monkey-patch and intercept calls to http.request.
const http = ((http_ as unknown) as {default: typeof http_}).default || http_;
const https =
((https_ as unknown) as {default: typeof https_}).default || https_;
const defaultHttpAgent = new http.Agent({keepAlive: true});
const defaultHttpsAgent = new https.Agent({keepAlive: true});
/**
* HTTP client which uses the Node `http` and `https` packages to issue
* requests.`
*/
export class NodeHttpClient extends HttpClient {
_agent?: http_.Agent | https_.Agent | undefined;
constructor(agent?: http_.Agent | https_.Agent) {
super();
this._agent = agent;
}
/** @override. */
getClientName(): string {
return 'node';
}
makeRequest(
host: string,
port: string,
path: string,
method: string,
headers: RequestHeaders,
requestData: string,
protocol: string,
timeout: number
): Promise<HttpClientResponseInterface> {
const isInsecureConnection = protocol === 'http';
let agent = this._agent;
if (!agent) {
agent = isInsecureConnection ? defaultHttpAgent : defaultHttpsAgent;
}
const requestPromise = new Promise<HttpClientResponseInterface>(
(resolve, reject) => {
const req = (isInsecureConnection ? http : https).request({
host: host,
port: port,
path,
method,
agent,
headers,
ciphers: 'DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:!MD5',
});
req.setTimeout(timeout, () => {
req.destroy(HttpClient.makeTimeoutError());
});
req.on('response', (res) => {
resolve(new NodeHttpClientResponse(res));
});
req.on('error', (error) => {
reject(error);
});
req.once('socket', (socket) => {
if (socket.connecting) {
socket.once(
isInsecureConnection ? 'connect' : 'secureConnect',
() => {
// Send payload; we're safe:
req.write(requestData);
req.end();
}
);
} else {
// we're already connected
req.write(requestData);
req.end();
}
});
}
);
return requestPromise;
}
}
export class NodeHttpClientResponse extends HttpClientResponse
implements HttpClientResponseInterface {
_res: http_.IncomingMessage;
constructor(res: http_.IncomingMessage) {
// @ts-ignore
super(res.statusCode, res.headers || {});
this._res = res;
}
getRawResponse(): http_.IncomingMessage {
return this._res;
}
toStream(streamCompleteCallback: () => void): http_.IncomingMessage {
// The raw response is itself the stream, so we just return that. To be
// backwards compatible, we should invoke the streamCompleteCallback only
// once the stream has been fully consumed.
this._res.once('end', () => streamCompleteCallback());
return this._res;
}
toJSON(): any {
return new Promise((resolve, reject) => {
let response = '';
this._res.setEncoding('utf8');
this._res.on('data', (chunk) => {
response += chunk;
});
this._res.once('end', () => {
try {
resolve(JSON.parse(response));
} catch (e) {
reject(e);
}
});
});
}
}