Skip to content

Commit

Permalink
Merge pull request #319 from andolini/master
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-c committed Mar 28, 2018
2 parents 0349a02 + 65f674e commit f099ec8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 25 deletions.
87 changes: 64 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"http_ece": "1.0.5",
"jws": "^3.1.3",
"minimist": "^1.2.0",
"urlsafe-base64": "^1.0.0"
"urlsafe-base64": "^1.0.0",
"https-proxy-agent": "^2.1.1"
},
"devDependencies": {
"chalk": "^2.3.0",
Expand Down
21 changes: 20 additions & 1 deletion src/web-push-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const urlBase64 = require('urlsafe-base64');
const url = require('url');
const https = require('https');
const HttpsProxyAgent = require('https-proxy-agent');

const WebPushError = require('./web-push-error.js');
const vapidHelper = require('./vapid-helper.js');
Expand Down Expand Up @@ -109,14 +110,16 @@ WebPushLib.prototype.generateRequestDetails =
let timeToLive = DEFAULT_TTL;
let extraHeaders = {};
let contentEncoding = webPushConstants.supportedContentEncodings.AES_GCM;
let proxy;

if (options) {
const validOptionKeys = [
'headers',
'gcmAPIKey',
'vapidDetails',
'TTL',
'contentEncoding'
'contentEncoding',
'proxy'
];
const optionKeys = Object.keys(options);
for (let i = 0; i < optionKeys.length; i += 1) {
Expand Down Expand Up @@ -162,6 +165,14 @@ WebPushLib.prototype.generateRequestDetails =
throw new Error('Unsupported content encoding specified.');
}
}

if (options.proxy) {
if (typeof options.proxy === 'string') {
proxy = options.proxy;
} else {
console.warn('Attempt to use proxy option, but invalid type it should be a string ');
}
}
}

if (typeof timeToLive === 'undefined') {
Expand Down Expand Up @@ -250,6 +261,10 @@ WebPushLib.prototype.generateRequestDetails =
requestDetails.body = requestPayload;
requestDetails.endpoint = subscription.endpoint;

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

return requestDetails;
};

Expand Down Expand Up @@ -286,6 +301,10 @@ WebPushLib.prototype.sendNotification =
httpsOptions.headers = requestDetails.headers;
httpsOptions.method = requestDetails.method;

if (requestDetails.proxy) {
httpsOptions.agent = new HttpsProxyAgent(requestDetails.proxy);
}

const pushRequest = https.request(httpsOptions, function(pushResponse) {
let responseText = '';

Expand Down
14 changes: 14 additions & 0 deletions test/test-generate-request-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,18 @@ suite('Test Generate Request Details', function() {
assert.ok(audience, 'Audience exists');
assert.equal(audience, 'http://example.com:4242', 'Audience contains expected value with port');
});

test('Proxy option', function() {
let subscription = { endpoint: 'https://127.0.0.1:8080' };
let message;
let extraOptions = {
'proxy': 'proxy'
};
let details = webPush.generateRequestDetails(
subscription,
message,
extraOptions
);
assert.equal(details.proxy, extraOptions.proxy);
});
});

0 comments on commit f099ec8

Please sign in to comment.