Skip to content

Commit 8fb1f3d

Browse files
improving proxy functionality fixes #89
1 parent 1ddc5a9 commit 8fb1f3d

File tree

4 files changed

+121
-21
lines changed

4 files changed

+121
-21
lines changed

lib/requests/http.js

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
var http = require('http');
22
var https = require('https');
33
var url = require('url');
4+
var HttpProxyAgent = require('http-proxy-agent');
5+
var HttpsProxyAgent = require('https-proxy-agent');
46
var parseResponse = require('./helpers/parseResponse');
57
var ErrorHelper = require('../helpers/ErrorHelper');
68

9+
var proxyAgents = {};
10+
11+
function getProxyAgent(options, headers) {
12+
var protocol = parsedUrl.protocol.replace(':', '');
13+
14+
var proxy = options.proxy;
15+
var proxyOptions = url.parse(proxy.url);
16+
17+
headers.host = proxyOptions.host;
18+
19+
if (!proxyAgents[protocol]) {
20+
var proxyAgent = protocol === 'http' ? HttpProxyAgent : HttpsProxyAgent;
21+
22+
if (proxy.auth) {
23+
var base64 = Buffer.from(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
24+
proxyOptions.headers = { 'Proxy-Authorization': 'Basic ' + base64 };
25+
}
26+
27+
proxyAgents[protocol] = new proxyAgent(proxyOptions);
28+
}
29+
30+
return proxyAgents[protocol];
31+
}
32+
733
/**
834
* Sends a request to given URL with given parameters
935
*
@@ -18,6 +44,7 @@ var httpRequest = function (options) {
1844
var errorCallback = options.errorCallback;
1945
var timeout = options.timeout;
2046
var requestId = options.requestId;
47+
var proxy = options.proxy;
2148

2249
var headers = {};
2350

@@ -35,7 +62,8 @@ var httpRequest = function (options) {
3562

3663
var parsedUrl = url.parse(uri);
3764
var protocol = parsedUrl.protocol.replace(':', '');
38-
var protocolInterface = protocol === 'http' ? http : https;
65+
var isHttp = protocol === 'http';
66+
var protocolInterface = isHttp ? http : https;
3967

4068
var internalOptions = {
4169
hostname: parsedUrl.hostname,
@@ -46,24 +74,15 @@ var httpRequest = function (options) {
4674
headers: headers
4775
};
4876

49-
if (process.env[`${protocol}_proxy`]) {
50-
/*
51-
* Proxied requests don't work with Node's https module so use http to
52-
* talk to the proxy server regardless of the endpoint protocol. This
53-
* is unsuitable for environments where requests are expected to be
54-
* using end-to-end TLS.
55-
*/
56-
protocolInterface = http;
57-
var proxyUrl = url.parse(process.env.http_proxy);
58-
headers.host = parsedUrl.host;
59-
internalOptions = {
60-
hostname: proxyUrl.hostname,
61-
port: proxyUrl.port,
62-
path: parsedUrl.href,
63-
method: method,
64-
timeout: timeout,
65-
headers: headers
66-
};
77+
//support environment variables (deprecated)
78+
if (!proxy && process.env[`${protocol}_proxy`]) {
79+
options.proxy = {
80+
url: process.env[`${protocol}_proxy`]
81+
}
82+
}
83+
84+
if (proxy && proxy.url) {
85+
internalOptions.agent = getProxyAgent(options, headers);
6786
}
6887

6988
var request = protocolInterface.request(internalOptions, function (res) {
@@ -133,6 +152,7 @@ var httpRequest = function (options) {
133152

134153
if (internalOptions.timeout) {
135154
request.setTimeout(internalOptions.timeout, function () {
155+
delete responseParams[requestId];
136156
request.abort();
137157
});
138158
}

package-lock.json

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
"webpack-cli": "^4.2.0",
3939
"webpack-strip-block": "^0.3.0"
4040
},
41-
"dependencies": {},
41+
"dependencies": {
42+
"http-proxy-agent": "^4.0.1",
43+
"https-proxy-agent": "^5.0.0"
44+
},
4245
"license": "MIT",
4346
"scripts": {
4447
"test": "mocha tests/*-tests.js",

types/dynamics-web-api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ declare namespace DynamicsWebApi {
535535
contentId?: string;
536536
/**v.1.4.3 + Casts the AttributeMetadata to a specific type. (Used in requests to Attribute Metadata). */
537537
metadataAttributeType?: string;
538-
/**A String representing the name of a single - valued navigation property.Useful when needed to retrieve information about a related record in a single request. */
538+
/**A String representing the name of a single - valued navigation property. Useful when needed to retrieve information about a related record in a single request. */
539539
navigationProperty?: string;
540540
/**v.1.4.3 + A String representing navigation property's Primary Key (GUID) or Alternate Key(s). (For example, to retrieve Attribute Metadata). */
541541
navigationPropertyKey?: string;

0 commit comments

Comments
 (0)