-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from web-push-libs/reintroduce_bin
Reintroduce bin/web-push.js
- Loading branch information
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|