Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding request to support HTTPS_PROXY #282

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"http_ece": "^0.5.2",
"jws": "^3.1.3",
"minimist": "^1.2.0",
"request": "^2.81.0",
"request-promise-native": "^1.0.4",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does request-promise-native not have it's own dependency on request?

Copy link

@gauntface gauntface Jun 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using request since it will look for HTTPS_PROXY. Might be worth adding a comment in the code where request is used to highlight this, otherwise chances are people will forget that is a feature.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request-promise-native asks for peer dependency, added to avoid warnings

"urlsafe-base64": "^1.0.0"
},
"devDependencies": {
Expand Down
57 changes: 22 additions & 35 deletions src/web-push-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const urlBase64 = require('urlsafe-base64');
const url = require('url');
const https = require('https');
const request = require('request-promise-native');

const WebPushError = require('./web-push-error.js');
const vapidHelper = require('./vapid-helper.js');
Expand Down Expand Up @@ -254,45 +254,32 @@ WebPushLib.prototype.sendNotification =
}

return new Promise(function(resolve, reject) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this out promise since the new API is returning a promise?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that should be possible, tried to change as little code as possible

const httpsOptions = {};
const urlParts = url.parse(requestDetails.endpoint);
httpsOptions.hostname = urlParts.hostname;
httpsOptions.port = urlParts.port;
httpsOptions.path = urlParts.path;
const httpsOptions = {
uri: requestDetails.endpoint,
headers: requestDetails.headers,
method: requestDetails.method,
resolveWithFullResponse: true
};

httpsOptions.headers = requestDetails.headers;
httpsOptions.method = requestDetails.method;

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

pushResponse.on('data', function(chunk) {
responseText += chunk;
});
if (requestDetails.body) {
httpsOptions.method = 'POST';
httpsOptions.body = requestDetails.body;
}

pushResponse.on('end', function() {
return request(httpsOptions)
.then((pushResponse) => {
if (pushResponse.statusCode !== 201) {
reject(new WebPushError('Received unexpected response code',
pushResponse.statusCode, pushResponse.headers, responseText, requestDetails.endpoint));
} else {
resolve({
statusCode: pushResponse.statusCode,
body: responseText,
headers: pushResponse.headers
});
return reject(new WebPushError('Received unexpected response code',
pushResponse.statusCode, pushResponse.headers, pushResponse.body, subscription.endpoint));
}
});
});

pushRequest.on('error', function(e) {
reject(e);
});

if (requestDetails.body) {
pushRequest.write(requestDetails.body);
}

pushRequest.end();
return resolve({
statusCode: pushResponse.statusCode,
body: pushResponse.body,
headers: pushResponse.headers
});
})
.catch(reject);
});
};

Expand Down