Skip to content

Commit

Permalink
Merge pull request #212 from web-push-libs/reintroduce_bin
Browse files Browse the repository at this point in the history
Reintroduce bin/web-push.js
  • Loading branch information
marco-c authored Sep 5, 2016
2 parents f89e151 + c0eb76f commit c069a22
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"max-len": [1, 80, 4, {"ignoreComments": true, "ignoreUrls": true}],
"no-param-reassign": 0,
"quote-props": 0,
"wrap-iife": [2, "inside"]
"wrap-iife": [2, "inside"],
"import/no-unresolved": 0
}
}
66 changes: 66 additions & 0 deletions bin/web-push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#! /usr/bin/env node
const fs = require('fs');
const webPush = require('web-push');
webPush.setGCMAPIKey(process.env.GCM_API_KEY);

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

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]';

if (!argv.endpoint || !argv.key) {
console.log(usage);
process.exit(1);
}

const endpoint = argv.endpoint;
const key = argv.key;
const ttl = argv.ttl || 0;
const payload = argv.payload || '';
const auth = argv.auth || null;
const vapidAudience = argv['vapid-audience'] || null;
const vapidSubject = argv['vapid-subject'] || null;
const vapidPubKey = argv['vapid-pubkey'] || null;
const vapidPvtKey = argv['vapid-pvtkey'] || null;

function getKeys() {
if (vapidPubKey && vapidPvtKey) {
const publicKey = fs.readFileSync(vapidPubKey);
const privateKey = fs.readFileSync(vapidPvtKey);

if (publicKey && privateKey) {
return {
privateKey,
publicKey
};
}
}

return webPush.generateVAPIDKeys();
}

let params = {
TTL: ttl,
payload,
userPublicKey: key
};
if (vapidAudience && vapidSubject) {
const vapidKeys = getKeys();
const vapid = {
audience: vapidAudience,
subject: `mailto:${vapidSubject}`,
privateKey: vapidKeys.privateKey,
publicKey: vapidKeys.publicKey
};
params.vapid = vapid;
}
if (auth) {
params.userAuth = auth;
}
webPush.sendNotification(endpoint, params).then(() => {
console.log('Push message sent.');
}, (err) => {
console.log('Error sending push message: ', err);
}).then(() => {
process.exit(0);
});

0 comments on commit c069a22

Please sign in to comment.