Skip to content
Permalink
Browse files

Merge pull request #319 from andolini/master

  • Loading branch information
marco-c committed Mar 28, 2018
2 parents 0349a02 + 65f674e commit f099ec8ff97e86fb6778ece04bd27a36ef93655e
Showing with 100 additions and 25 deletions.
  1. +64 −23 package-lock.json
  2. +2 −1 package.json
  3. +20 −1 src/web-push-lib.js
  4. +14 −0 test/test-generate-request-details.js

Some generated files are not rendered by default. Learn more.

@@ -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",
@@ -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');
@@ -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) {
@@ -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') {
@@ -250,6 +261,10 @@ WebPushLib.prototype.generateRequestDetails =
requestDetails.body = requestPayload;
requestDetails.endpoint = subscription.endpoint;

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

return requestDetails;
};

@@ -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 = '';

@@ -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.
You can’t perform that action at this time.