|
1 | 1 | #! /usr/bin/env node
|
2 | 2 | const fs = require('fs');
|
3 |
| -const webPush = require('web-push'); |
4 |
| -webPush.setGCMAPIKey(process.env.GCM_API_KEY); |
| 3 | +const webPush = require('../src/index.js'); |
5 | 4 |
|
6 |
| -const argv = require('minimist')(process.argv.slice(2)); |
| 5 | +const printUsageDetails = () => { |
| 6 | + const spc = ' '; |
7 | 7 |
|
8 |
| -const usage = 'Use: web-push --endpoint=<url> --key=<browser key> ' + |
9 |
| - '[--auth=<auth secret>] [--ttl=<seconds>] [--payload=<message>] ' + |
10 |
| - '[--vapid-audience] [--vapid-subject] [--vapid-pvtkey] [--vapid-pubkey]'; |
| 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]', |
| 18 | + '[--vapid-pubkey]', |
| 19 | + '[--vapid-pvtkey]' |
| 20 | + ] |
| 21 | + }, { |
| 22 | + name: 'generate-vapid-keys', |
| 23 | + options: [ |
| 24 | + '[--json]' |
| 25 | + ] |
| 26 | + } |
| 27 | + ]; |
| 28 | + |
| 29 | + let usage = '\nUsage: \n\n'; |
| 30 | + actions.forEach(action => { |
| 31 | + usage += ' web-push ' + action.name; |
| 32 | + usage += ' ' + action.options.join(' '); |
| 33 | + usage += '\n\n'; |
| 34 | + }); |
11 | 35 |
|
12 |
| -if (!argv.endpoint || !argv.key) { |
13 | 36 | console.log(usage);
|
14 | 37 | process.exit(1);
|
15 |
| -} |
| 38 | +}; |
| 39 | + |
| 40 | +const generateVapidKeys = returnJson => { |
| 41 | + const vapidKeys = webPush.generateVAPIDKeys(); |
16 | 42 |
|
17 |
| -const endpoint = argv.endpoint; |
18 |
| -const key = argv.key; |
19 |
| -const ttl = argv.ttl || 0; |
20 |
| -const payload = argv.payload || ''; |
21 |
| -const auth = argv.auth || null; |
| 43 | + let outputText; |
| 44 | + if (returnJson) { |
| 45 | + outputText = JSON.stringify(vapidKeys); |
| 46 | + } else { |
| 47 | + const outputLine = '\n=======================================\n' |
| 48 | + outputText = outputLine + '\n' + |
| 49 | + 'Public Key:\n' + vapidKeys.publicKey + '\n\n' + |
| 50 | + 'Private Key:\n' + vapidKeys.privateKey + '\n' + |
| 51 | + outputLine; |
| 52 | + } |
22 | 53 |
|
23 |
| -const vapidAudience = argv['vapid-audience'] || null; |
24 |
| -const vapidSubject = argv['vapid-subject'] || null; |
25 |
| -const vapidPubKey = argv['vapid-pubkey'] || null; |
26 |
| -const vapidPvtKey = argv['vapid-pvtkey'] || null; |
| 54 | + console.log(outputText); |
| 55 | + process.exit(0); |
| 56 | +}; |
27 | 57 |
|
28 |
| -function getKeys() { |
29 |
| - if (vapidPubKey && vapidPvtKey) { |
30 |
| - const publicKey = fs.readFileSync(vapidPubKey); |
31 |
| - const privateKey = fs.readFileSync(vapidPvtKey); |
| 58 | +const sendNotification = args => { |
| 59 | + webPush.setGCMAPIKey(process.env.GCM_API_KEY); |
32 | 60 |
|
33 |
| - if (publicKey && privateKey) { |
34 |
| - return { |
35 |
| - privateKey, |
36 |
| - publicKey |
37 |
| - }; |
| 61 | + const subscription = { |
| 62 | + endpoint: argv.endpoint, |
| 63 | + keys: { |
| 64 | + p256dh: argv.key || null, |
| 65 | + auth: argv.auth || null |
38 | 66 | }
|
39 |
| - } |
| 67 | + }; |
40 | 68 |
|
41 |
| - return webPush.generateVAPIDKeys(); |
42 |
| -} |
| 69 | + const payload = argv.payload || null; |
43 | 70 |
|
44 |
| -let params = { |
45 |
| - TTL: ttl, |
46 |
| - payload, |
47 |
| - userPublicKey: key |
48 |
| -}; |
49 |
| -if (vapidAudience && vapidSubject) { |
50 |
| - const vapidKeys = getKeys(); |
51 |
| - const vapid = { |
52 |
| - audience: vapidAudience, |
53 |
| - subject: `mailto:${vapidSubject}`, |
54 |
| - privateKey: vapidKeys.privateKey, |
55 |
| - publicKey: vapidKeys.publicKey |
| 71 | + const options = { |
| 72 | + TTL: argv.ttl || 0, |
| 73 | + vapid: { |
| 74 | + subject: argv['vapid-subject'] || null, |
| 75 | + publicKey: argv['vapid-pubkey'] || null, |
| 76 | + privateKey: argv['vapid-pvtkey'] || null |
| 77 | + } |
56 | 78 | };
|
57 |
| - params.vapid = vapid; |
58 |
| -} |
59 |
| -if (auth) { |
60 |
| - params.userAuth = auth; |
| 79 | + |
| 80 | + webPush.sendNotification(subscription, payload, options) |
| 81 | + .then(() => { |
| 82 | + console.log('Push message sent.'); |
| 83 | + }, err => { |
| 84 | + console.log('Error sending push message: '); |
| 85 | + console.log(err); |
| 86 | + }) |
| 87 | + .then(() => { |
| 88 | + process.exit(0); |
| 89 | + }); |
| 90 | +}; |
| 91 | + |
| 92 | +const action = process.argv[2]; |
| 93 | +const argv = require('minimist')(process.argv.slice(3)); |
| 94 | +switch (action) { |
| 95 | + case 'send-notification': |
| 96 | + if (!argv.endpoint || !argv.key) { |
| 97 | + return printUsageDetails(); |
| 98 | + } |
| 99 | + |
| 100 | + sendNotification(argv); |
| 101 | + break; |
| 102 | + case 'generate-vapid-keys': |
| 103 | + generateVapidKeys(argv.json || false); |
| 104 | + break; |
| 105 | + default: |
| 106 | + printUsageDetails(); |
| 107 | + break; |
61 | 108 | }
|
62 |
| -webPush.sendNotification(endpoint, params).then(() => { |
63 |
| - console.log('Push message sent.'); |
64 |
| -}, (err) => { |
65 |
| - console.log('Error sending push message: ', err); |
66 |
| -}).then(() => { |
67 |
| - process.exit(0); |
68 |
| -}); |
|
0 commit comments