Skip to content

Commit 7bda588

Browse files
SeregaSEmarco-c
andcommitted
Add an option to specify the http agent to use (#490)
Co-Authored-By: Marco <[email protected]>
1 parent 53e55c1 commit 7bda588

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ const options = {
134134
'< header name >': '< header value >'
135135
},
136136
contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >',
137-
proxy: '< proxy server options >'
137+
proxy: '< proxy server options >',
138+
agent: '< https.Agent instance >'
138139
}
139140

140141
webpush.sendNotification(
@@ -185,6 +186,7 @@ retained by the push service (by default, four weeks).
185186
- **proxy** is the [HttpsProxyAgent's constructor argument](https://github.com/TooTallNate/node-https-proxy-agent#new-httpsproxyagentobject-options)
186187
that may either be a string URI of the proxy server (eg. http://< hostname >:< port >)
187188
or an "options" object with more specific properties.
189+
- **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!
188190

189191
### Returns
190192

src/web-push-lib.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
110110
let extraHeaders = {};
111111
let contentEncoding = webPushConstants.supportedContentEncodings.AES_128_GCM;
112112
let proxy;
113+
let agent;
113114

114115
if (options) {
115116
const validOptionKeys = [
@@ -118,7 +119,8 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
118119
'vapidDetails',
119120
'TTL',
120121
'contentEncoding',
121-
'proxy'
122+
'proxy',
123+
'agent'
122124
];
123125
const optionKeys = Object.keys(options);
124126
for (let i = 0; i < optionKeys.length; i += 1) {
@@ -174,6 +176,18 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
174176
console.warn('Attempt to use proxy option, but invalid type it should be a string or proxy options object.');
175177
}
176178
}
179+
180+
if (options.agent) {
181+
if (options.agent instanceof https.Agent) {
182+
if (proxy) {
183+
console.warn('Agent option will be ignored because proxy option is defined.');
184+
}
185+
186+
agent = options.agent;
187+
} else {
188+
console.warn('Wrong type for the agent option, it should be an instance of https.Agent.');
189+
}
190+
}
177191
}
178192

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

282+
if (agent) {
283+
requestDetails.agent = agent;
284+
}
285+
268286
return requestDetails;
269287
};
270288

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

321+
if (requestDetails.agent) {
322+
httpsOptions.agent = requestDetails.agent;
323+
}
324+
303325
if (requestDetails.proxy) {
304326
httpsOptions.agent = new HttpsProxyAgent(requestDetails.proxy);
305327
}

test/test-generate-request-details.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const webPush = require('../src/index');
66
const crypto = require('crypto');
77
const jws = require('jws');
88
const urlParse = require('url').parse;
9+
const https = require('https');
910

1011
suite('Test Generate Request Details', function() {
1112
test('is defined', function() {
@@ -201,6 +202,31 @@ suite('Test Generate Request Details', function() {
201202
}
202203
}
203204
}
205+
}, {
206+
testTitle: 'invalid agent',
207+
requestOptions: {
208+
subscription: {
209+
keys: VALID_KEYS
210+
},
211+
message: 'hello',
212+
addEndpoint: true,
213+
extraOptions: {
214+
agent: 'agent'
215+
}
216+
}
217+
}, {
218+
testTitle: 'ignore valid agent if proxy denifed',
219+
requestOptions: {
220+
subscription: {
221+
keys: VALID_KEYS
222+
},
223+
message: 'hello',
224+
addEndpoint: true,
225+
extraOptions: {
226+
agent: new https.Agent({ keepAlive: true }),
227+
proxy: 'http://localhost:3000'
228+
}
229+
}
204230
}
205231
];
206232

@@ -329,4 +355,19 @@ suite('Test Generate Request Details', function() {
329355
);
330356
assert.equal(details.proxy, extraOptions.proxy);
331357
});
358+
359+
test('Agent option as an https.Agent instance', function() {
360+
let subscription = {
361+
endpoint: 'https://127.0.0.1:8080'
362+
};
363+
let extraOptions = {
364+
agent: new https.Agent({ keepAlive: true })
365+
};
366+
let details = webPush.generateRequestDetails(
367+
subscription,
368+
null,
369+
extraOptions
370+
);
371+
assert.equal(details.agent, extraOptions.agent);
372+
});
332373
});

0 commit comments

Comments
 (0)