Skip to content

Commit

Permalink
Add an option to specify the http agent to use (#490)
Browse files Browse the repository at this point in the history
Co-Authored-By: Marco <[email protected]>
  • Loading branch information
SeregaSE and marco-c committed Aug 8, 2019
1 parent 53e55c1 commit 7bda588
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ const options = {
'< header name >': '< header value >'
},
contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >',
proxy: '< proxy server options >'
proxy: '< proxy server options >',
agent: '< https.Agent instance >'
}

webpush.sendNotification(
Expand Down Expand Up @@ -185,6 +186,7 @@ retained by the push service (by default, four weeks).
- **proxy** is the [HttpsProxyAgent's constructor argument](https://github.com/TooTallNate/node-https-proxy-agent#new-httpsproxyagentobject-options)
that may either be a string URI of the proxy server (eg. http://< hostname >:< port >)
or an "options" object with more specific properties.
- **agent** is the [HTTPS Agent instance](https://nodejs.org/dist/latest/docs/api/https.html#https_class_https_agent) which will be used in the `https.request` method. If the `proxy` options defined, `agent` will be ignored!

### Returns

Expand Down
24 changes: 23 additions & 1 deletion src/web-push-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
let extraHeaders = {};
let contentEncoding = webPushConstants.supportedContentEncodings.AES_128_GCM;
let proxy;
let agent;

if (options) {
const validOptionKeys = [
Expand All @@ -118,7 +119,8 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
'vapidDetails',
'TTL',
'contentEncoding',
'proxy'
'proxy',
'agent'
];
const optionKeys = Object.keys(options);
for (let i = 0; i < optionKeys.length; i += 1) {
Expand Down Expand Up @@ -174,6 +176,18 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
console.warn('Attempt to use proxy option, but invalid type it should be a string or proxy options object.');
}
}

if (options.agent) {
if (options.agent instanceof https.Agent) {
if (proxy) {
console.warn('Agent option will be ignored because proxy option is defined.');
}

agent = options.agent;
} else {
console.warn('Wrong type for the agent option, it should be an instance of https.Agent.');
}
}
}

if (typeof timeToLive === 'undefined') {
Expand Down Expand Up @@ -265,6 +279,10 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
requestDetails.proxy = proxy;
}

if (agent) {
requestDetails.agent = agent;
}

return requestDetails;
};

Expand Down Expand Up @@ -300,6 +318,10 @@ WebPushLib.prototype.sendNotification = function(subscription, payload, options)
httpsOptions.headers = requestDetails.headers;
httpsOptions.method = requestDetails.method;

if (requestDetails.agent) {
httpsOptions.agent = requestDetails.agent;
}

if (requestDetails.proxy) {
httpsOptions.agent = new HttpsProxyAgent(requestDetails.proxy);
}
Expand Down
41 changes: 41 additions & 0 deletions test/test-generate-request-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const webPush = require('../src/index');
const crypto = require('crypto');
const jws = require('jws');
const urlParse = require('url').parse;
const https = require('https');

suite('Test Generate Request Details', function() {
test('is defined', function() {
Expand Down Expand Up @@ -201,6 +202,31 @@ suite('Test Generate Request Details', function() {
}
}
}
}, {
testTitle: 'invalid agent',
requestOptions: {
subscription: {
keys: VALID_KEYS
},
message: 'hello',
addEndpoint: true,
extraOptions: {
agent: 'agent'
}
}
}, {
testTitle: 'ignore valid agent if proxy denifed',
requestOptions: {
subscription: {
keys: VALID_KEYS
},
message: 'hello',
addEndpoint: true,
extraOptions: {
agent: new https.Agent({ keepAlive: true }),
proxy: 'http://localhost:3000'
}
}
}
];

Expand Down Expand Up @@ -329,4 +355,19 @@ suite('Test Generate Request Details', function() {
);
assert.equal(details.proxy, extraOptions.proxy);
});

test('Agent option as an https.Agent instance', function() {
let subscription = {
endpoint: 'https://127.0.0.1:8080'
};
let extraOptions = {
agent: new https.Agent({ keepAlive: true })
};
let details = webPush.generateRequestDetails(
subscription,
null,
extraOptions
);
assert.equal(details.agent, extraOptions.agent);
});
});

0 comments on commit 7bda588

Please sign in to comment.