Skip to content

Commit

Permalink
using http2 instead of https
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Lee committed Nov 17, 2017
1 parent c325be8 commit 2ec6a5f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const WebPushLib = require('./web-push-lib.js');
const WebPushError = require('./web-push-error.js');

const webPush = new WebPushLib();

module.exports = {
WebPushError: WebPushError,
encrypt: encryptionHelper.encrypt,
Expand All @@ -15,5 +14,6 @@ module.exports = {
setGCMAPIKey: webPush.setGCMAPIKey,
setVapidDetails: webPush.setVapidDetails,
generateRequestDetails: webPush.generateRequestDetails,
sendNotification: webPush.sendNotification
sendNotification: webPush.sendNotification,
closeHttp2Connection: webPush.closeHttp2Connection,
};
67 changes: 43 additions & 24 deletions src/web-push-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

const urlBase64 = require('urlsafe-base64');
const url = require('url');
const https = require('https');
const http2 = require('http2');
const {
HTTP2_HEADER_METHOD,
HTTP2_HEADER_PATH,
HTTP2_HEADER_STATUS,
HTTP2_HEADER_CONTENT_TYPE,
} = http2.constants;

const WebPushError = require('./web-push-error.js');
const vapidHelper = require('./vapid-helper.js');
Expand All @@ -14,8 +20,9 @@ const DEFAULT_TTL = 2419200;
let gcmAPIKey = '';
let vapidDetails;

function WebPushLib() {
const clientSessions = {};

function WebPushLib() {
}

/**
Expand Down Expand Up @@ -245,6 +252,8 @@ WebPushLib.prototype.generateRequestDetails =
*/
WebPushLib.prototype.sendNotification =
function(subscription, payload, options) {
const me = this;

let requestDetails;
try {
requestDetails = this.generateRequestDetails(
Expand All @@ -253,47 +262,57 @@ WebPushLib.prototype.sendNotification =
return Promise.reject(err);
}

return new Promise(function(resolve, reject) {
return new Promise((resolve, reject) => {
const httpsOptions = {};
const urlParts = url.parse(requestDetails.endpoint);
httpsOptions.hostname = urlParts.hostname;
httpsOptions.port = urlParts.port;
httpsOptions.path = urlParts.path;
const host = urlParts.protocol + "//" + urlParts.host;

if(!clientSessions[host]) {
clientSessions[host] = http2.connect(host);
}

httpsOptions.headers = requestDetails.headers;
httpsOptions.method = requestDetails.method;
const headers = {
':method': requestDetails.method,
':path': urlParts.path
};

const pushRequest = https.request(httpsOptions, function(pushResponse) {
let responseText = '';
for(let i in requestDetails.headers)
headers[i.toLowerCase()] = requestDetails.headers[i];

pushResponse.on('data', function(chunk) {
const pushRequest = clientSessions[host].request(headers);
pushRequest.on('response', (resp_headers) => {
let responseText = '';
pushRequest.on('data', (chunk) => {
responseText += chunk;
});

pushResponse.on('end', function() {
if (pushResponse.statusCode !== 201) {
pushRequest.on('end', () => {
if(resp_headers[HTTP2_HEADER_STATUS] !== 201) {
reject(new WebPushError('Received unexpected response code',
pushResponse.statusCode, pushResponse.headers, responseText, requestDetails.endpoint));
resp_headers[HTTP2_HEADER_STATUS], resp_headers, responseText, requestDetails.endpoint));
} else {
resolve({
statusCode: pushResponse.statusCode,
statusCode: resp_headers[HTTP2_HEADER_STATUS],
body: responseText,
headers: pushResponse.headers
});
headers: resp_headers
})
}
resolve();
});
});

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

pushRequest.on('error', (err) => {
reject(err);
})
if (requestDetails.body) {
pushRequest.write(requestDetails.body);
}

pushRequest.end();
});
};

WebPushLib.prototype.closeHttp2Connection =
function() {
for(let i in clientSessions)
clientSessions[i].destroy();
}

module.exports = WebPushLib;

0 comments on commit 2ec6a5f

Please sign in to comment.