Skip to content

Commit f4f672b

Browse files
committed
Merge pull request #133 from arcturus/command-line
Adds command line executable
2 parents fd95929 + 9e3ba35 commit f4f672b

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

bin/web-push.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#! /usr/bin/env node
2+
const fs = require('fs');
3+
const webPush = require('web-push');
4+
webPush.setGCMAPIKey(process.env.GCM_API_KEY);
5+
6+
const argv = require('minimist')(process.argv.slice(2));
7+
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]';
9+
10+
if (!argv['endpoint'] || !argv['key']) {
11+
console.log(usage);
12+
process.exit(1);
13+
}
14+
15+
const endpoint = argv['endpoint'];
16+
const key = argv['key'];
17+
const ttl = argv['ttl'] || 0;
18+
const payload = argv['payload'] || '';
19+
const auth = argv['auth'] || null;
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+
}
40+
41+
var params = {
42+
TTL: ttl,
43+
payload,
44+
userPublicKey: key
45+
};
46+
if (vapidAudience && vapidSubject) {
47+
const vapidKeys = getKeys();
48+
const vapid = {
49+
audience: vapidAudience,
50+
subject: `mailto:${vapidSubject}`,
51+
privateKey: vapidKeys.privateKey,
52+
publicKey: vapidKeys.publicKey,
53+
};
54+
params['vapid'] = vapid;
55+
}
56+
if (auth) {
57+
params['userAuth'] = auth;
58+
}
59+
webPush.sendNotification(endpoint, params).then(() => {
60+
console.log('Push message sent.');
61+
}, (err) => {
62+
console.log('Error sending push message: ', err);
63+
}).then(() => {
64+
process.exit(0);
65+
});

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web-push",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Web Push library for Node.js",
55
"main": "index.js",
66
"scripts": {
@@ -29,6 +29,7 @@
2929
"colors": "^1.1.2",
3030
"http_ece": "^0.4.5",
3131
"jws": "^3.1.3",
32+
"minimist": "^1.2.0",
3233
"urlsafe-base64": "^1.0.0"
3334
},
3435
"devDependencies": {
@@ -42,5 +43,11 @@
4243
"selenium-webdriver": "^2.53.1",
4344
"semver": "^5.1.0",
4445
"temp": "^0.8.3"
46+
},
47+
"bin": {
48+
"web-push": "bin/web-push.js"
49+
},
50+
"engines": {
51+
"node": ">= v4.0.0"
4552
}
4653
}

0 commit comments

Comments
 (0)