typed-rest-client
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -53,2 +53,6 @@ /// <reference types="node" /> | ||
private _maxRedirects; | ||
private _agent; | ||
private _proxyAgent; | ||
private _keepAlive; | ||
private _disposed; | ||
private _certConfig; | ||
@@ -73,5 +77,11 @@ private _ca; | ||
request(verb: string, requestUrl: string, data: string | NodeJS.ReadableStream, headers: ifm.IHeaders): Promise<HttpClientResponse>; | ||
/** | ||
* Needs to be called if keepAlive is set to true in request options. | ||
*/ | ||
dispose(): void; | ||
private _requestRaw(info, data); | ||
private _prepareRequest(method, requestUrl, headers); | ||
private _getAgent(requestUrl); | ||
private _getProxy(requestUrl); | ||
private _isBypassProxy(requestUrl); | ||
} |
@@ -18,3 +18,2 @@ "use strict"; | ||
const fs = require("fs"); | ||
http.globalAgent.maxSockets = 100; | ||
var HttpCodes; | ||
@@ -81,2 +80,4 @@ (function (HttpCodes) { | ||
this._maxRedirects = 50; | ||
this._keepAlive = false; | ||
this._disposed = false; | ||
this.userAgent = userAgent; | ||
@@ -114,2 +115,5 @@ this.handlers = handlers; | ||
} | ||
if (requestOptions.keepAlive != null) { | ||
this._keepAlive = requestOptions.keepAlive; | ||
} | ||
} | ||
@@ -148,2 +152,5 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._disposed) { | ||
throw new Error("Client has already been disposed"); | ||
} | ||
let info = this._prepareRequest(verb, requestUrl, headers); | ||
@@ -171,2 +178,14 @@ let response = yield this._requestRaw(info, data); | ||
} | ||
/** | ||
* Needs to be called if keepAlive is set to true in request options. | ||
*/ | ||
dispose() { | ||
if (this._agent) { | ||
this._agent.destroy(); | ||
} | ||
if (this._proxyAgent) { | ||
this._proxyAgent.destroy(); | ||
} | ||
this._disposed = true; | ||
} | ||
_requestRaw(info, data) { | ||
@@ -218,28 +237,2 @@ return new Promise((resolve, reject) => { | ||
var defaultPort = usingSsl ? 443 : 80; | ||
let proxyConfig = this._httpProxy; | ||
// fallback to http_proxy and https_proxy env | ||
let https_proxy = process.env[EnvironmentVariables.HTTPS_PROXY]; | ||
let http_proxy = process.env[EnvironmentVariables.HTTP_PROXY]; | ||
if (!proxyConfig) { | ||
if (https_proxy && usingSsl) { | ||
proxyConfig = { | ||
proxyUrl: https_proxy | ||
}; | ||
} | ||
else if (http_proxy) { | ||
proxyConfig = { | ||
proxyUrl: http_proxy | ||
}; | ||
} | ||
} | ||
let proxyUrl; | ||
let proxyAuth; | ||
if (proxyConfig) { | ||
if (proxyConfig.proxyUrl.length > 0) { | ||
proxyUrl = url.parse(proxyConfig.proxyUrl); | ||
} | ||
if (proxyConfig.proxyUsername || proxyConfig.proxyPassword) { | ||
proxyAuth = proxyConfig.proxyUsername + ":" + encodeURIComponent(proxyConfig.proxyPassword); | ||
} | ||
} | ||
info.options = {}; | ||
@@ -252,14 +245,43 @@ info.options.host = info.parsedUrl.hostname; | ||
info.options.headers["User-Agent"] = this.userAgent; | ||
let useProxy = proxyUrl && proxyUrl.hostname && !this._isBypassProxy(requestUrl); | ||
info.options.agent = this._getAgent(requestUrl); | ||
// gives handlers an opportunity to participate | ||
if (this.handlers) { | ||
this.handlers.forEach((handler) => { | ||
handler.prepareRequest(info.options); | ||
}); | ||
} | ||
return info; | ||
} | ||
_getAgent(requestUrl) { | ||
let agent; | ||
let proxy = this._getProxy(requestUrl); | ||
let useProxy = proxy.proxyUrl && proxy.proxyUrl.hostname && !this._isBypassProxy(requestUrl); | ||
if (this._keepAlive && useProxy) { | ||
agent = this._proxyAgent; | ||
} | ||
if (this._keepAlive && !useProxy) { | ||
agent = this._agent; | ||
} | ||
// if agent is already assigned use that agent. | ||
if (!!agent) { | ||
return agent; | ||
} | ||
var parsedUrl = url.parse(requestUrl); | ||
let usingSsl = parsedUrl.protocol === 'https:'; | ||
let maxSockets = 100; | ||
if (!!this.requestOptions) { | ||
maxSockets = this.requestOptions.maxSockets || http.globalAgent.maxSockets; | ||
} | ||
if (useProxy) { | ||
var agentOptions = { | ||
maxSockets: http.globalAgent.maxSockets, | ||
maxSockets: maxSockets, | ||
keepAlive: this._keepAlive, | ||
proxy: { | ||
proxyAuth: proxyAuth, | ||
host: proxyUrl.hostname, | ||
port: proxyUrl.port | ||
proxyAuth: proxy.proxyAuth, | ||
host: proxy.proxyUrl.hostname, | ||
port: proxy.proxyUrl.port | ||
}, | ||
}; | ||
var tunnelAgent; | ||
var overHttps = proxyUrl.protocol === 'https:'; | ||
var overHttps = proxy.proxyUrl.protocol === 'https:'; | ||
if (usingSsl) { | ||
@@ -271,28 +293,56 @@ tunnelAgent = overHttps ? tunnel.httpsOverHttps : tunnel.httpsOverHttp; | ||
} | ||
info.options.agent = tunnelAgent(agentOptions); | ||
agent = tunnelAgent(agentOptions); | ||
this._proxyAgent = agent; | ||
} | ||
// if reusing agent across request and tunneling agent isn't assigned create a new agent | ||
if (this._keepAlive && !agent) { | ||
var options = { keepAlive: this._keepAlive, maxSockets: maxSockets }; | ||
agent = usingSsl ? new https.Agent(options) : new http.Agent(options); | ||
this._agent = agent; | ||
} | ||
// if not using private agent and tunnel agent isn't setup then use global agent | ||
if (!agent) { | ||
agent = usingSsl ? https.globalAgent : http.globalAgent; | ||
} | ||
if (usingSsl && this._ignoreSslError) { | ||
if (!info.options.agent) { | ||
info.options.agent = https.globalAgent; | ||
} | ||
// we don't want to set NODE_TLS_REJECT_UNAUTHORIZED=0 since that will affect request for entire process | ||
// http.RequestOptions doesn't expose a way to modify RequestOptions.agent.options | ||
// we have to cast it to any and change it directly | ||
let agent = info.options.agent; | ||
agent.options = Object.assign(agent.options || {}, { rejectUnauthorized: false }); | ||
} | ||
if (usingSsl && this._certConfig) { | ||
if (!info.options.agent) { | ||
info.options.agent = https.globalAgent; | ||
} | ||
let agent = info.options.agent; | ||
agent.options = Object.assign(agent.options || {}, { ca: this._ca, cert: this._cert, key: this._key, passphrase: this._certConfig.passphrase }); | ||
} | ||
// gives handlers an opportunity to participate | ||
if (this.handlers) { | ||
this.handlers.forEach((handler) => { | ||
handler.prepareRequest(info.options); | ||
}); | ||
return agent; | ||
} | ||
_getProxy(requestUrl) { | ||
var parsedUrl = url.parse(requestUrl); | ||
let usingSsl = parsedUrl.protocol === 'https:'; | ||
let proxyConfig = this._httpProxy; | ||
// fallback to http_proxy and https_proxy env | ||
let https_proxy = process.env[EnvironmentVariables.HTTPS_PROXY]; | ||
let http_proxy = process.env[EnvironmentVariables.HTTP_PROXY]; | ||
if (!proxyConfig) { | ||
if (https_proxy && usingSsl) { | ||
proxyConfig = { | ||
proxyUrl: https_proxy | ||
}; | ||
} | ||
else if (http_proxy) { | ||
proxyConfig = { | ||
proxyUrl: http_proxy | ||
}; | ||
} | ||
} | ||
return info; | ||
let proxyUrl; | ||
let proxyAuth; | ||
if (proxyConfig) { | ||
if (proxyConfig.proxyUrl.length > 0) { | ||
proxyUrl = url.parse(proxyConfig.proxyUrl); | ||
} | ||
if (proxyConfig.proxyUsername || proxyConfig.proxyPassword) { | ||
proxyAuth = proxyConfig.proxyUsername + ":" + encodeURIComponent(proxyConfig.proxyPassword); | ||
} | ||
} | ||
return { proxyUrl: proxyUrl, proxyAuth: proxyAuth }; | ||
} | ||
@@ -299,0 +349,0 @@ _isBypassProxy(requestUrl) { |
@@ -24,2 +24,4 @@ export interface IHeaders { | ||
maxRedirects?: number; | ||
maxSockets?: number; | ||
keepAlive?: boolean; | ||
} | ||
@@ -26,0 +28,0 @@ export interface IProxyConfiguration { |
{ | ||
"name": "typed-rest-client", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Node Rest and Http Clients for use with TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "./RestClient.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63964
1325