Skip to content

Commit c1c4b34

Browse files
authored
Merge pull request #227 from gauntface/cli-update
Cli update
2 parents 27a114f + bd5f6dc commit c1c4b34

File tree

7 files changed

+338
-74
lines changed

7 files changed

+338
-74
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
coverage/
22
node_modules/
3-
wires
43
test/output/

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ registration.pushManager.subscribe({
8989
});
9090
```
9191

92+
## Command Line
93+
94+
You can install `web-push` globally and use it for sending notifications
95+
and / or generating VAPID keys.
96+
97+
Install like so:
98+
99+
npm install web-push -g
100+
101+
Then you can run the following commands:
102+
103+
Usage:
104+
105+
web-push send-notification --endpoint=<url> [--key=<browser key>] [--auth=<auth secret>] [--payload=<message>] [--ttl=<seconds>] [--vapid-subject=<vapid subject>] [--vapid-pubkey=<public key url base64>] [--vapid-pvtkey=<private key url base64>] [--gcm-api-key=<api key>]
106+
107+
web-push generate-vapid-keys [--json]
108+
109+
92110
# API Reference
93111

94112
## sendNotification(pushSubscription, payload, options)

bin/web-push.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Web Push library for Node.js",
55
"main": "src/index.js",
66
"bin": {
7-
"web-push": "bin/web-push.js"
7+
"web-push": "src/cli.js"
88
},
99
"scripts": {
1010
"download-browser": "node --harmony ./test/helpers/download-test-browsers.js",

src/cli.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#! /usr/bin/env node
2+
/* eslint consistent-return:0*/
3+
'use strict';
4+
5+
const webPush = require('../src/index.js');
6+
7+
const printUsageDetails = () => {
8+
const actions = [
9+
{
10+
name: 'send-notification',
11+
options: [
12+
'--endpoint=<url>',
13+
'[--key=<browser key>]',
14+
'[--auth=<auth secret>]',
15+
'[--payload=<message>]',
16+
'[--ttl=<seconds>]',
17+
'[--vapid-subject=<vapid subject>]',
18+
'[--vapid-pubkey=<public key url base64>]',
19+
'[--vapid-pvtkey=<private key url base64>]',
20+
'[--gcm-api-key=<api key>]'
21+
]
22+
}, {
23+
name: 'generate-vapid-keys',
24+
options: [
25+
'[--json]'
26+
]
27+
}
28+
];
29+
30+
let usage = '\nUsage: \n\n';
31+
actions.forEach(action => {
32+
usage += ' web-push ' + action.name;
33+
usage += ' ' + action.options.join(' ');
34+
usage += '\n\n';
35+
});
36+
37+
console.log(usage);
38+
process.exit(1);
39+
};
40+
41+
const generateVapidKeys = returnJson => {
42+
const vapidKeys = webPush.generateVAPIDKeys();
43+
44+
let outputText;
45+
if (returnJson) {
46+
outputText = JSON.stringify(vapidKeys);
47+
} else {
48+
const outputLine = '\n=======================================\n';
49+
outputText = outputLine + '\n' +
50+
'Public Key:\n' + vapidKeys.publicKey + '\n\n' +
51+
'Private Key:\n' + vapidKeys.privateKey + '\n' +
52+
outputLine;
53+
}
54+
55+
console.log(outputText);
56+
process.exit(0);
57+
};
58+
59+
const sendNotification = args => {
60+
if (process.env.GCM_API_KEY) {
61+
webPush.setGCMAPIKey(process.env.GCM_API_KEY);
62+
}
63+
64+
const subscription = {
65+
endpoint: args.endpoint,
66+
keys: {
67+
p256dh: args.key || null,
68+
auth: args.auth || null
69+
}
70+
};
71+
72+
const payload = args.payload || null;
73+
74+
const options = {};
75+
76+
if (args.ttl) {
77+
options.TTL = args.ttl;
78+
}
79+
80+
if (argv['vapid-subject'] || argv['vapid-pubkey'] || argv['vapid-pvtkey']) {
81+
options.vapidDetails = {
82+
subject: args['vapid-subject'] || null,
83+
publicKey: args['vapid-pubkey'] || null,
84+
privateKey: args['vapid-pvtkey'] || null
85+
};
86+
}
87+
88+
if (args['gcm-api-key']) {
89+
options.gcmAPIKey = args['gcm-api-key'];
90+
}
91+
92+
webPush.sendNotification(subscription, payload, options)
93+
.then(() => {
94+
console.log('Push message sent.');
95+
}, err => {
96+
console.log('Error sending push message: ');
97+
console.log(err);
98+
})
99+
.then(() => {
100+
process.exit(0);
101+
});
102+
};
103+
104+
const action = process.argv[2];
105+
const argv = require('minimist')(process.argv.slice(3));
106+
switch (action) {
107+
case 'send-notification':
108+
if (!argv.endpoint) {
109+
return printUsageDetails();
110+
}
111+
112+
sendNotification(argv);
113+
break;
114+
case 'generate-vapid-keys':
115+
generateVapidKeys(argv.json || false);
116+
break;
117+
default:
118+
printUsageDetails();
119+
break;
120+
}

src/vapid-helper.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function generateVAPIDKeys() {
3939

4040
function validateSubject(subject) {
4141
if (!subject) {
42-
throw new Error('No subject set in vapid.subject.');
42+
throw new Error('No subject set in vapidDetails.subject.');
4343
}
4444

4545
if (typeof subject !== 'string' || subject.length === 0) {
@@ -57,7 +57,7 @@ function validateSubject(subject) {
5757

5858
function validatePublicKey(publicKey) {
5959
if (!publicKey) {
60-
throw new Error('No key set vapid.publicKey');
60+
throw new Error('No key set vapidDetails.publicKey');
6161
}
6262

6363
if (typeof publicKey !== 'string') {
@@ -74,7 +74,7 @@ function validatePublicKey(publicKey) {
7474

7575
function validatePrivateKey(privateKey) {
7676
if (!privateKey) {
77-
throw new Error('No key set in vapid.privateKey');
77+
throw new Error('No key set in vapidDetails.privateKey');
7878
}
7979

8080
if (typeof privateKey !== 'string') {
@@ -103,7 +103,7 @@ function validatePrivateKey(privateKey) {
103103
*/
104104
function getVapidHeaders(audience, subject, publicKey, privateKey, expiration) {
105105
if (!audience) {
106-
throw new Error('No audience set in vapid.audience.');
106+
throw new Error('No audience could be generated for VAPID.');
107107
}
108108

109109
if (typeof audience !== 'string' || audience.length === 0) {

0 commit comments

Comments
 (0)