Skip to content

Commit 9e3ba35

Browse files
author
Francisco Jordano
committed
Making vapid configurable via command line args
1 parent 1757e11 commit 9e3ba35

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

bin/web-push.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#! /usr/bin/env node
2+
const fs = require('fs');
23
const webPush = require('web-push');
34
webPush.setGCMAPIKey(process.env.GCM_API_KEY);
45

56
const argv = require('minimist')(process.argv.slice(2));
67

7-
const usage = 'Use: web-push --endpoint=<url> --key=<browser key> [--auth=<auth secret>] [--ttl=<seconds>] [--payload=<message>] [--vapid]';
8+
const usage = 'Use: web-push --endpoint=<url> --key=<browser key> [--auth=<auth secret>] [--ttl=<seconds>] [--payload=<message>] [--vapid-audience] [--vapid-subject] [--vapid-pvtkey] [--vapid-pubkey]';
89

910
if (!argv['endpoint'] || !argv['key']) {
1011
console.log(usage);
@@ -16,18 +17,37 @@ const key = argv['key'];
1617
const ttl = argv['ttl'] || 0;
1718
const payload = argv['payload'] || '';
1819
const auth = argv['auth'] || null;
19-
const useVAPID = argv['vapid'] || false;
20+
const vapidAudience = argv['vapid-audience'] || null;
21+
const vapidSubject = argv['vapid-subject'] || null;
22+
const vapidPubKey = argv['vapid-pubkey'] || null;
23+
const vapidPvtKey = argv['vapid-pvtkey'] || null;
24+
25+
function getKeys() {
26+
if (vapidPubKey && vapidPvtKey) {
27+
const publicKey = fs.readFileSync(argv['vapid-pubkey']);
28+
const privateKey = fs.readFileSync(argv['vapid-pvtkey']);
29+
30+
if (pubKey && pvtKey) {
31+
return {
32+
privateKey,
33+
publicKey
34+
};
35+
}
36+
}
37+
38+
return webPush.generateVAPIDKeys();
39+
}
2040

2141
var params = {
2242
TTL: ttl,
2343
payload,
2444
userPublicKey: key
2545
};
26-
if (useVAPID) {
27-
const vapidKeys = webPush.generateVAPIDKeys();
46+
if (vapidAudience && vapidSubject) {
47+
const vapidKeys = getKeys();
2848
const vapid = {
29-
audience: 'https://www.mozilla.org/',
30-
subject: 'mailto:[email protected]',
49+
audience: vapidAudience,
50+
subject: `mailto:${vapidSubject}`,
3151
privateKey: vapidKeys.privateKey,
3252
publicKey: vapidKeys.publicKey,
3353
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "web-push",
33
"version": "2.0.2",
4-
"version": "1.0.3",
54
"description": "Web Push library for Node.js",
65
"main": "index.js",
76
"scripts": {

0 commit comments

Comments
 (0)