Skip to content

Commit 10f0816

Browse files
authored
Merge pull request #156 from IrishWhiskey/http-connection
Fix http connection
2 parents 24e8ef1 + 75b61ab commit 10f0816

10 files changed

+30183
-29704
lines changed

javascript/lib/api/package-lock.json

+14,822-14,822
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/lib/dom/package-lock.json

+14,821-14,821
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/lib/node/package-lock.json

+165-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/lib/node/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
"dist"
1616
],
1717
"dependencies": {
18+
"http-proxy-agent": "4.0.1",
1819
"https-proxy-agent": "2.2.3",
1920
"url": "0.11.0",
2021
"ws": "7.1.2"
2122
},
2223
"devDependencies": {
23-
"@types/http-proxy-agent": "^2.0.1",
2424
"@types/jest": "^25.2.3",
2525
"@types/node": "^12.0.4",
2626
"@types/ws": "^6.0.1",
2727
"barrelsby": "^2.1.1",
2828
"jest": "^25.4.0",
29+
"nock": "^13.0.11",
2930
"ts-jest": "^25.5.1",
3031
"tslint": "^5.18.x",
3132
"tslint-config-airbnb": "^5.11.1",

javascript/lib/node/src/node-http.ts

+23-17
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,29 @@
1313

1414
import { GenericResponse } from '../../api/src/model/response';
1515
import { HttpRequester } from '../../api/src/client/request-factory/http-request-sender';
16-
import { IncomingMessage } from 'http';
16+
import * as http from 'http';
1717
import * as https from 'https';
1818
import { ProxyAgent } from './proxy-settings';
1919

2020
/**
2121
* NodeJs implementation of a Http Requester.
2222
*/
2323
export class NodeRequester implements HttpRequester {
24-
private readonly proxyAgent: any;
2524

26-
public constructor(agent: ProxyAgent) {
27-
this.proxyAgent = agent.proxyAgent;
25+
public constructor(private readonly agent: ProxyAgent) {
2826
}
2927

3028
private static createResponseHandler(resolve: (value?: (PromiseLike<GenericResponse> | GenericResponse)) => void,
3129
reject: (reason?: any) => void):
32-
(response: IncomingMessage) => void {
30+
(response: http.IncomingMessage) => void {
3331
return response => {
3432
this.handleResponse(resolve, reject, response);
3533
};
3634
}
3735

3836
private static handleResponse(resolve: (value?: (PromiseLike<GenericResponse> | GenericResponse)) => void,
3937
reject: (reason?: any) => void,
40-
response: IncomingMessage): void {
38+
response: http.IncomingMessage): void {
4139
let data = '';
4240
response.on('data', chunk => {
4341
data += chunk;
@@ -64,7 +62,11 @@ export class NodeRequester implements HttpRequester {
6462

6563
public async doRequest(method: string, url: string, header: Map<string, string>, payload: string): Promise<GenericResponse> {
6664
return new Promise<GenericResponse>(((resolve, reject) => {
67-
const req = https.request(this.buildRequest(method, url, header, payload), NodeRequester.createResponseHandler(resolve, reject));
65+
const parsedUrl = new URL(url);
66+
const isSecureRequest = parsedUrl.protocol === 'https:';
67+
const client = isSecureRequest ? https : http;
68+
const requestOptions = this.buildRequest(method, parsedUrl, header, payload, isSecureRequest);
69+
const req = client.request(requestOptions, NodeRequester.createResponseHandler(resolve, reject));
6870
req.on('error', e => {
6971
throw new Error(String(e));
7072
});
@@ -79,27 +81,31 @@ export class NodeRequester implements HttpRequester {
7981
* Builds an options object to perform a Http request with.
8082
*
8183
* @param method - The type of action to perform.
82-
* @param url - The Url to send the request to.
84+
* @param parsedUrl - The Url to send the request to.
8385
* @param header - The headers of the request.
8486
* @param body - The payload to send with the request.
87+
* @param isSecureRequest - If the request is a secure HTTPS request.
8588
* @return the builder.
8689
*/
87-
private buildRequest(method: string, url: string, header: Map<string, string>, body: string): object {
90+
private buildRequest(method: string, parsedUrl: URL, header: Map<string, string>, body: string,
91+
isSecureRequest: boolean): object {
8892
if (body !== undefined && body !== '') {
8993
header.set('Content-Length', Buffer.byteLength(body).toString());
9094
}
9195
const headers: { [key: string]: any } = {};
9296
header.forEach((v, k) => headers[k] = v);
93-
const parsedUrl = new URL(url);
94-
const request: { [key: string]: any } = {
97+
return {
9598
method,
9699
headers,
97-
host: parsedUrl.host,
98-
path: parsedUrl.pathname
100+
hostname: parsedUrl.hostname,
101+
port: parsedUrl.port,
102+
path: parsedUrl.pathname,
103+
agent: this.getAgentForRequestType(isSecureRequest)
99104
};
100-
if (this.proxyAgent !== undefined && this.proxyAgent.options.path !== undefined) {
101-
request['agent'] = this.proxyAgent;
102-
}
103-
return request;
104105
}
106+
107+
private getAgentForRequestType(isSecureRequest: boolean): http.Agent | undefined {
108+
return isSecureRequest ? this.agent.proxyAgent : this.agent.httpProxyAgent;
109+
}
110+
105111
}

0 commit comments

Comments
 (0)