From 4d6b8dbca5e380de78be08bbd10d431c4afe9afb Mon Sep 17 00:00:00 2001 From: LS Hung <61226932+lokshunhung@users.noreply.github.com> Date: Tue, 20 Feb 2024 11:25:11 +0800 Subject: [PATCH 1/3] Support multiple instances --- src/index.js | 7 ++++--- src/web-push-lib.js | 21 +++++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index f7b36741..e15373e3 100644 --- a/src/index.js +++ b/src/index.js @@ -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) }; diff --git a/src/web-push-lib.js b/src/web-push-lib.js index a47df054..06a8e902 100644 --- a/src/web-push-lib.js +++ b/src/web-push-lib.js @@ -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; } /** @@ -27,7 +28,7 @@ function WebPushLib() { */ WebPushLib.prototype.setGCMAPIKey = function(apiKey) { if (apiKey === null) { - gcmAPIKey = null; + this.gcmAPIKey = null; return; } @@ -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; }; /** @@ -52,7 +53,7 @@ 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; } @@ -60,7 +61,7 @@ WebPushLib.prototype.setVapidDetails = function(subject, publicKey, privateKey) vapidHelper.validatePublicKey(publicKey); vapidHelper.validatePrivateKey(privateKey); - vapidDetails = { + this.vapidDetails = { subject: subject, publicKey: publicKey, privateKey: privateKey @@ -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; From 1f857ce43cf436e41a552818baf6c5687231e47f Mon Sep 17 00:00:00 2001 From: LS Hung <61226932+lokshunhung@users.noreply.github.com> Date: Tue, 20 Feb 2024 11:38:10 +0800 Subject: [PATCH 2/3] Update README on multiple instances usage --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 9bf31f93..14ff917d 100644 --- a/README.md +++ b/README.md @@ -464,6 +464,32 @@ object will contain:
+# 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(''); +webpush1.setVapidDetails( + 'mailto:example@yourdomain.org', + vapidKeys1.publicKey, + vapidKeys1.privateKey +); + +const webpush2 = new WebPushLib(); +webpush2.setGCMAPIKey(''); +webpush2.setVapidDetails( + 'mailto:example2@yourdomain.org', + vapidKeys2.publicKey, + vapidKeys2.privateKey +); +``` + # Browser Support From 879f43309187b214b55eab8a25a046a655012f64 Mon Sep 17 00:00:00 2001 From: LS Hung <61226932+lokshunhung@users.noreply.github.com> Date: Mon, 4 Mar 2024 11:57:15 +0800 Subject: [PATCH 3/3] fix lint --- src/web-push-lib.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/web-push-lib.js b/src/web-push-lib.js index 06a8e902..80d03b52 100644 --- a/src/web-push-lib.js +++ b/src/web-push-lib.js @@ -17,7 +17,7 @@ const DEFAULT_TTL = 2419200; */ function WebPushLib() { this.gcmAPIKey = ''; - this.vapidDetails; + this.vapidDetails = undefined; } /**