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

Support multiple instances #889

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,32 @@ object will contain:

<hr />

# Using multiple instances of `WebPushLib`

The functions `setGCMAPIKey`, `setVapidDetails`, `generateRequestDetails`, `sendNotification`
exported by this module are bounded to a global instance of `WebPushLib`.

In scenarios where multiple instances of `WebPushLib` is needed, you can create separate instances by:
```js
const { WebPushLib } = require('web-push');

const webpush1 = new WebPushLib();
webpush1.setGCMAPIKey('<Your GCM API Key Here>');
webpush1.setVapidDetails(
'mailto:[email protected]',
vapidKeys1.publicKey,
vapidKeys1.privateKey
);

const webpush2 = new WebPushLib();
webpush2.setGCMAPIKey('<Another GCM API Key Here>');
webpush2.setVapidDetails(
'mailto:[email protected]',
vapidKeys2.publicKey,
vapidKeys2.privateKey
);
```

# Browser Support

<table>
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ const WebPushConstants = require('./web-push-constants.js');
const webPush = new WebPushLib();

module.exports = {
WebPushLib: WebPushLib,
WebPushError: WebPushError,
supportedContentEncodings: WebPushConstants.supportedContentEncodings,
encrypt: encryptionHelper.encrypt,
getVapidHeaders: vapidHelper.getVapidHeaders,
generateVAPIDKeys: vapidHelper.generateVAPIDKeys,
setGCMAPIKey: webPush.setGCMAPIKey,
setVapidDetails: webPush.setVapidDetails,
generateRequestDetails: webPush.generateRequestDetails,
setGCMAPIKey: webPush.setGCMAPIKey.bind(webPush),
setVapidDetails: webPush.setVapidDetails.bind(webPush),
generateRequestDetails: webPush.generateRequestDetails.bind(webPush),
sendNotification: webPush.sendNotification.bind(webPush)
};
21 changes: 11 additions & 10 deletions src/web-push-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ const urlBase64Helper = require('./urlsafe-base64-helper');
// Default TTL is four weeks.
const DEFAULT_TTL = 2419200;

let gcmAPIKey = '';
let vapidDetails;

/**
* @constructor
*/
function WebPushLib() {

this.gcmAPIKey = '';
this.vapidDetails = undefined;
}

/**
Expand All @@ -27,7 +28,7 @@ function WebPushLib() {
*/
WebPushLib.prototype.setGCMAPIKey = function(apiKey) {
if (apiKey === null) {
gcmAPIKey = null;
this.gcmAPIKey = null;
return;
}

Expand All @@ -37,7 +38,7 @@ WebPushLib.prototype.setGCMAPIKey = function(apiKey) {
throw new Error('The GCM API Key should be a non-empty string or null.');
}

gcmAPIKey = apiKey;
this.gcmAPIKey = apiKey;
};

/**
Expand All @@ -52,15 +53,15 @@ WebPushLib.prototype.setGCMAPIKey = function(apiKey) {
*/
WebPushLib.prototype.setVapidDetails = function(subject, publicKey, privateKey) {
if (arguments.length === 1 && arguments[0] === null) {
vapidDetails = null;
this.vapidDetails = null;
return;
}

vapidHelper.validateSubject(subject);
vapidHelper.validatePublicKey(publicKey);
vapidHelper.validatePrivateKey(privateKey);

vapidDetails = {
this.vapidDetails = {
subject: subject,
publicKey: publicKey,
privateKey: privateKey
Expand Down Expand Up @@ -104,8 +105,8 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
}
}

let currentGCMAPIKey = gcmAPIKey;
let currentVapidDetails = vapidDetails;
let currentGCMAPIKey = this.gcmAPIKey;
let currentVapidDetails = this.vapidDetails;
let timeToLive = DEFAULT_TTL;
let extraHeaders = {};
let contentEncoding = webPushConstants.supportedContentEncodings.AES_128_GCM;
Expand Down