Skip to content

Commit 0117eb4

Browse files
tim-kuteevmarco-c
authored andcommitted
Allow proxy options object as a proxy parameter (#458)
1 parent bdaa394 commit 0117eb4

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const options = {
134134
'< header name >': '< header value >'
135135
},
136136
contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >',
137-
proxy: '< proxy server address >'
137+
proxy: '< proxy server options >'
138138
}
139139

140140
webpush.sendNotification(
@@ -182,7 +182,9 @@ request only. This overrides any API key set via `setGCMAPIKey()`.
182182
retained by the push service (by default, four weeks).
183183
- **headers** is an object with all the extra headers you want to add to the request.
184184
- **contentEncoding** is the type of push encoding to use (e.g. 'aesgcm', by default, or 'aes128gcm').
185-
- **proxy** proxy hostname/ip and a port to tunnel your requests through (eg. http://< hostname >:< port >).
185+
- **proxy** is the [HttpsProxyAgent's constructor argument](https://github.com/TooTallNate/node-https-proxy-agent#new-httpsproxyagentobject-options)
186+
that may either be a string URI of the proxy server (eg. http://< hostname >:< port >)
187+
or an "options" object with more specific properties.
186188

187189
### Returns
188190

@@ -353,7 +355,7 @@ const options = {
353355
'< header name >': '< header value >'
354356
},
355357
contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >',
356-
proxy: '< proxy server address >'
358+
proxy: '< proxy server options >'
357359
}
358360

359361
try {
@@ -407,7 +409,9 @@ request only. This overrides any API key set via `setGCMAPIKey()`.
407409
retained by the push service (by default, four weeks).
408410
- **headers** is an object with all the extra headers you want to add to the request.
409411
- **contentEncoding** is the type of push encoding to use (e.g. 'aesgcm', by default, or 'aes128gcm').
410-
- **proxy** proxy hostname/ip and a port to tunnel your requests through (eg. http://< hostname >:< port >).
412+
- **proxy** is the [HttpsProxyAgent's constructor argument](https://github.com/TooTallNate/node-https-proxy-agent#new-httpsproxyagentobject-options)
413+
that may either be a string URI of the proxy server (eg. http://< hostname >:< port >)
414+
or an "options" object with more specific properties.
411415

412416
### Returns
413417

src/web-push-lib.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,11 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
166166
}
167167

168168
if (options.proxy) {
169-
if (typeof options.proxy === 'string') {
169+
if (typeof options.proxy === 'string'
170+
|| typeof options.proxy.host === 'string') {
170171
proxy = options.proxy;
171172
} else {
172-
console.warn('Attempt to use proxy option, but invalid type it should be a string ');
173+
console.warn('Attempt to use proxy option, but invalid type it should be a string or proxy options object.');
173174
}
174175
}
175176
}

test/test-generate-request-details.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const urlBase64 = require('urlsafe-base64');
55
const webPush = require('../src/index');
66
const crypto = require('crypto');
77
const jws = require('jws');
8+
const urlParse = require('url').parse;
89

910
suite('Test Generate Request Details', function() {
1011
test('is defined', function() {
@@ -312,4 +313,20 @@ suite('Test Generate Request Details', function() {
312313
);
313314
assert.equal(details.proxy, extraOptions.proxy);
314315
});
316+
317+
test('Proxy option as an object', function() {
318+
let subscription = {
319+
endpoint: 'https://127.0.0.1:8080'
320+
};
321+
let proxyOption = urlParse('http://proxy');
322+
let extraOptions = {
323+
proxy: proxyOption
324+
};
325+
let details = webPush.generateRequestDetails(
326+
subscription,
327+
null,
328+
extraOptions
329+
);
330+
assert.equal(details.proxy, extraOptions.proxy);
331+
});
315332
});

0 commit comments

Comments
 (0)