Commit 1a843384 authored by Caleb Weeks's avatar Caleb Weeks

adding support for http and https keep alive agents

parent 2dd9c1c7
......@@ -68,7 +68,13 @@ var SlingConnector = module.exports = function(options){
this.hasDerefCapability = options.hasDerefCapability;
this.derefByDefault = options.derefByDefault;
this.invalidPathRegex = options.invalidPathRegex || /undefined|\[object Object\]/;
this.requestAgent = options.requestAgent || new KeepAliveAgent(options.agentKeepAliveConfig || {
this.httpRequestAgent = options.httpRequestAgent || new KeepAliveAgent(options.httpAgentKeepAliveConfig || {
maxSockets: 100,
maxFreeSockets: 10,
timeout: 60000, // active socket keepalive for 60 seconds
freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
});
this.httpsRequestAgent = options.httpsRequestAgent || new KeepAliveAgent.HttpsAgent(options.httpsAgentKeepAliveConfig || {
maxSockets: 100,
maxFreeSockets: 10,
timeout: 60000, // active socket keepalive for 60 seconds
......@@ -297,7 +303,7 @@ SC.getSling = function(path, options = {}, callback){
followRedirect: false,
auth:sc_self.makeAuth(),
headers: sc_self.authCookieHeader(),
agent: sc_self.requestAgent,
agent: sc_self.getRequestAgent(uri),
gzip: true
};
......@@ -564,6 +570,11 @@ SC.isValidPath= function(path) {
}
}
//////////////////////////////////////////////////////////////////////////////////////////
SC.getRequestAgent= function(path = "") {
return path.startsWith("https://") ? this.httpsRequestAgent : this.httpRequestAgent;
}
//////////////////////////////////////////////////////////////////////////
/**
......@@ -588,7 +599,7 @@ SC.getStream = function(path, method, headers){
followRedirect: false,
auth:this.makeAuth(),
headers: this.authCookieHeader(),
agent: sc_self.requestAgent,
agent: sc_self.getRequestAgent(uri),
gzip: true
})
......@@ -642,14 +653,15 @@ SC.import = function(path, data, options, callback){
//debug("Import form: ", form);
const postUrl = this.buildUri(path);
request.post({
url: this.buildUri(path),
url: postUrl,
formData: form, // Multi-part mode not necessary, but more efficient for large files than urlencoded
timeout: this.timeout,
followRedirect: false,
auth: this.makeAuth(),
headers: this.authCookieHeader(),
agent: sc_self.requestAgent,
agent: sc_self.getRequestAgent(postUrl),
gzip: true
},
function(err, httpResponse, body){
......@@ -688,13 +700,14 @@ SC.post = function(path,params,cb){
var sc_self=this;
params=params||{};
params._charset_ = params._charset_ || "utf-8";
const postUrl = this.buildUri(path);
var settings={
url: this.buildUri(path),
url: postUrl,
timeout: this.timeout,
followRedirect: false,
auth: this.makeAuth(),
headers: this.authCookieHeader(),
agent: sc_self.requestAgent,
agent: sc_self.getRequestAgent(postUrl),
gzip: true
};
......
module.exports = require("./exists.get-if-exists.json");
\ No newline at end of file
module.exports = require("./exists.get-if-exists.json");
\ No newline at end of file
module.exports = {
"res": {
"statusCode": 200,
"headers": "some headers"
},
"data": {
"exists": true,
"data" : {
"key" : "value"
}
}
}
\ No newline at end of file
......@@ -18,7 +18,8 @@ const optionDefaults = {
mode: "undefined",
hasDerefCapability: "undefined",
invalidPathRegex : /undefined|\[object Object\]/,
requestAgent : "defined",
httpRequestAgent : "defined",
httpsRequestAgent : "defined",
inMemoryCache: "undefined"
}
......@@ -74,8 +75,8 @@ test('Initial SC with custom invalidPathRegex', () => {
});
test('Initial SC with custom requestAgent', () => {
const requestAgent = () => {};
const sc = new SlingConnector({baseUri: "http://127.0.0.1:4502/", requestAgent});
expect(sc.requestAgent).toStrictEqual(requestAgent);
expectDefaults(sc, ['requestAgent']);
const httpRequestAgent = () => {};
const sc = new SlingConnector({baseUri: "http://127.0.0.1:4502/", httpRequestAgent});
expect(sc.httpRequestAgent).toStrictEqual(httpRequestAgent);
expectDefaults(sc, ['httpRequestAgent']);
});
......@@ -32,7 +32,7 @@ test('default requestAgent', async () => {
test('custom requestAgent', async () => {
const customRequestAgent = new Object();
const localSc = sc.clone({requestAgent: customRequestAgent})
const localSc = sc.clone({httpRequestAgent: customRequestAgent})
await localSc.getSling('test1');
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].agent).toStrictEqual(customRequestAgent);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment