Skip to content

Commit

Permalink
Allow agent reuse for ClientSSLSecurity if forever option is set
Browse files Browse the repository at this point in the history
  • Loading branch information
Mik13 committed Oct 2, 2017
1 parent 6a7a49a commit faf498e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ An instance of `Client` is passed to the `soap.createClient` callback. It is us

### Client.setSecurity(security) - use the specified security protocol

### Client.*method*(args, callback) - call *method* on the SOAP service.
### Client.*method*(args, callback, options) - call *method* on the SOAP service.

``` javascript
client.MyFunction({name: 'value'}, function(err, result, raw, soapHeader) {
Expand All @@ -406,6 +406,11 @@ An instance of `Client` is passed to the `soap.createClient` callback. It is us

The `args` argument allows you to supply arguments that generate an XML document inside of the SOAP Body section.

The `options` object is optional and is passed to the `request`-module.
Interesting properties might be:
* `timeout`: Timeout in milliseconds
* `forever`: Enables keep alive connections

### Client.*method*Async(args) - call *method* on the SOAP service.

``` javascript
Expand Down Expand Up @@ -589,6 +594,8 @@ as default request options to the constructor:
* `strictSSL: false`
* `secureOptions: constants.SSL_OP_NO_TLSv1_2` (this is likely needed for node >= 10.0)

If you want to reuse tls sessions, you can use the option `forever: true`.

``` javascript
client.setSecurity(new soap.ClientSSLSecurity(
'/path/to/key',
Expand All @@ -600,6 +607,7 @@ client.setSecurity(new soap.ClientSSLSecurity(
// rejectUnauthorized: false,
// hostname: 'some-hostname'
// secureOptions: constants.SSL_OP_NO_TLSv1_2,
// forever: true,
},
));
```
Expand Down
19 changes: 18 additions & 1 deletion lib/security/ClientSSLSecurity.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,35 @@ function ClientSSLSecurity(key, cert, ca, defaults) {

this.defaults = {};
_.merge(this.defaults, defaults);

this.agent = null;
}

ClientSSLSecurity.prototype.toXML = function(headers) {
return '';
};

ClientSSLSecurity.prototype.addOptions = function(options) {
var httpsAgent = null;

options.key = this.key;
options.cert = this.cert;
options.ca = this.ca;
_.merge(options, this.defaults);
options.agent = new https.Agent(options);

if (!!options.forever) {
if (!this.agent) {
options.keepAlive = true;

this.agent = new https.Agent(options);
}

httpsAgent = this.agent;
} else {
httpsAgent = new https.Agent(options);
}

options.agent = httpsAgent;
};

module.exports = ClientSSLSecurity;
35 changes: 35 additions & 0 deletions test/security/ClientSSLSecurity.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,39 @@ describe('ClientSSLSecurity', function() {
var instance = new ClientSSLSecurity(null, null, caList);
instance.should.have.property("ca", caList);
});

describe('forever parameter', function () {
it('should return different agents if parameter is not present', function () {
var instance = new ClientSSLSecurity();
var firstOptions = {};
var secondOptions = {};

instance.addOptions(firstOptions);
instance.addOptions(secondOptions);

firstOptions.agent.should.not.equal(secondOptions.agent);
});

it('should return the same agent if parameter is present', function () {
var instance = new ClientSSLSecurity();
var firstOptions = {forever: true};
var secondOptions = {forever: true};

instance.addOptions(firstOptions);
instance.addOptions(secondOptions);

firstOptions.agent.should.equal(secondOptions.agent);
});

it('should return the same agent if set in defaults', function () {
var instance = new ClientSSLSecurity(null, null, null, {forever: true});
var firstOptions = {};
var secondOptions = {};

instance.addOptions(firstOptions);
instance.addOptions(secondOptions);

firstOptions.agent.should.equal(secondOptions.agent);
});
});
});

0 comments on commit faf498e

Please sign in to comment.