From a9c198f19886228321f57dcbd8eb954da8ffe86d Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 23 Sep 2016 20:07:49 -0700 Subject: [PATCH 1/9] Updating to new actions for CLI --- bin/web-push.js | 144 +++++++++++++++++++++++++++++++----------------- 1 file changed, 92 insertions(+), 52 deletions(-) mode change 100644 => 100755 bin/web-push.js diff --git a/bin/web-push.js b/bin/web-push.js old mode 100644 new mode 100755 index 89c3b014..34780fec --- a/bin/web-push.js +++ b/bin/web-push.js @@ -1,68 +1,108 @@ #! /usr/bin/env node const fs = require('fs'); -const webPush = require('web-push'); -webPush.setGCMAPIKey(process.env.GCM_API_KEY); +const webPush = require('../src/index.js'); -const argv = require('minimist')(process.argv.slice(2)); +const printUsageDetails = () => { + const spc = ' '; -const usage = 'Use: web-push --endpoint= --key= ' + - '[--auth=] [--ttl=] [--payload=] ' + - '[--vapid-audience] [--vapid-subject] [--vapid-pvtkey] [--vapid-pubkey]'; + const actions = [ + { + name: 'send-notification', + options: [ + '--endpoint=', + '[--key=]', + '[--auth=]', + '[--payload=]', + '[--ttl=]', + '[--vapid-subject]', + '[--vapid-pubkey]', + '[--vapid-pvtkey]' + ] + }, { + name: 'generate-vapid-keys', + options: [ + '[--json]' + ] + } + ]; + + let usage = '\nUsage: \n\n'; + actions.forEach(action => { + usage += ' web-push ' + action.name; + usage += ' ' + action.options.join(' '); + usage += '\n\n'; + }); -if (!argv.endpoint || !argv.key) { console.log(usage); process.exit(1); -} +}; + +const generateVapidKeys = returnJson => { + const vapidKeys = webPush.generateVAPIDKeys(); -const endpoint = argv.endpoint; -const key = argv.key; -const ttl = argv.ttl || 0; -const payload = argv.payload || ''; -const auth = argv.auth || null; + let outputText; + if (returnJson) { + outputText = JSON.stringify(vapidKeys); + } else { + const outputLine = '\n=======================================\n' + outputText = outputLine + '\n' + + 'Public Key:\n' + vapidKeys.publicKey + '\n\n' + + 'Private Key:\n' + vapidKeys.privateKey + '\n' + + outputLine; + } -const vapidAudience = argv['vapid-audience'] || null; -const vapidSubject = argv['vapid-subject'] || null; -const vapidPubKey = argv['vapid-pubkey'] || null; -const vapidPvtKey = argv['vapid-pvtkey'] || null; + console.log(outputText); + process.exit(0); +}; -function getKeys() { - if (vapidPubKey && vapidPvtKey) { - const publicKey = fs.readFileSync(vapidPubKey); - const privateKey = fs.readFileSync(vapidPvtKey); +const sendNotification = args => { + webPush.setGCMAPIKey(process.env.GCM_API_KEY); - if (publicKey && privateKey) { - return { - privateKey, - publicKey - }; + const subscription = { + endpoint: argv.endpoint, + keys: { + p256dh: argv.key || null, + auth: argv.auth || null } - } + }; - return webPush.generateVAPIDKeys(); -} + const payload = argv.payload || null; -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 + const options = { + TTL: argv.ttl || 0, + vapid: { + subject: argv['vapid-subject'] || null, + publicKey: argv['vapid-pubkey'] || null, + privateKey: argv['vapid-pvtkey'] || null + } }; - params.vapid = vapid; -} -if (auth) { - params.userAuth = auth; + + webPush.sendNotification(subscription, payload, options) + .then(() => { + console.log('Push message sent.'); + }, err => { + console.log('Error sending push message: '); + console.log(err); + }) + .then(() => { + process.exit(0); + }); +}; + +const action = process.argv[2]; +const argv = require('minimist')(process.argv.slice(3)); +switch (action) { + case 'send-notification': + if (!argv.endpoint || !argv.key) { + return printUsageDetails(); + } + + sendNotification(argv); + break; + case 'generate-vapid-keys': + generateVapidKeys(argv.json || false); + break; + default: + printUsageDetails(); + break; } -webPush.sendNotification(endpoint, params).then(() => { - console.log('Push message sent.'); -}, (err) => { - console.log('Error sending push message: ', err); -}).then(() => { - process.exit(0); -}); From 9663a8c71b515ffc9132bb7f2e86d889aa5f9bfd Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 23 Sep 2016 21:01:02 -0700 Subject: [PATCH 2/9] Extending cli and adding some tests --- .gitignore | 1 - package.json | 2 +- bin/web-push.js => src/cli.js | 30 ++++-- src/vapid-helper.js | 8 +- test/test-cli.js | 187 ++++++++++++++++++++++++++++++++++ 5 files changed, 213 insertions(+), 15 deletions(-) rename bin/web-push.js => src/cli.js (79%) create mode 100644 test/test-cli.js diff --git a/.gitignore b/.gitignore index 6424942b..ff9fc546 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ coverage/ node_modules/ -wires test/output/ diff --git a/package.json b/package.json index 54f9fdab..e1e1739d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Web Push library for Node.js", "main": "src/index.js", "bin": { - "web-push": "bin/web-push.js" + "web-push": "src/cli.js" }, "scripts": { "download-browser": "node --harmony ./test/helpers/download-test-browsers.js", diff --git a/bin/web-push.js b/src/cli.js similarity index 79% rename from bin/web-push.js rename to src/cli.js index 34780fec..fc52d354 100755 --- a/bin/web-push.js +++ b/src/cli.js @@ -14,9 +14,10 @@ const printUsageDetails = () => { '[--auth=]', '[--payload=]', '[--ttl=]', - '[--vapid-subject]', - '[--vapid-pubkey]', - '[--vapid-pvtkey]' + '[--vapid-subject=]', + '[--vapid-pubkey=]', + '[--vapid-pvtkey=]', + '[--gcm-api-key=]' ] }, { name: 'generate-vapid-keys', @@ -56,7 +57,9 @@ const generateVapidKeys = returnJson => { }; const sendNotification = args => { - webPush.setGCMAPIKey(process.env.GCM_API_KEY); + if (process.env.GCM_API_KEY) { + webPush.setGCMAPIKey(process.env.GCM_API_KEY); + } const subscription = { endpoint: argv.endpoint, @@ -68,14 +71,23 @@ const sendNotification = args => { const payload = argv.payload || null; - const options = { - TTL: argv.ttl || 0, - vapid: { + const options = {}; + + if (argv.ttl) { + options.TTL = argv.ttl; + } + + if (argv['vapid-subject'] || argv['vapid-pubkey'] || argv['vapid-pvtkey']) { + options.vapidDetails = { subject: argv['vapid-subject'] || null, publicKey: argv['vapid-pubkey'] || null, privateKey: argv['vapid-pvtkey'] || null } - }; + } + + if (argv['gcm-api-key']) { + options.gcmAPIKey = argv['gcm-api-key']; + } webPush.sendNotification(subscription, payload, options) .then(() => { @@ -93,7 +105,7 @@ const action = process.argv[2]; const argv = require('minimist')(process.argv.slice(3)); switch (action) { case 'send-notification': - if (!argv.endpoint || !argv.key) { + if (!argv.endpoint) { return printUsageDetails(); } diff --git a/src/vapid-helper.js b/src/vapid-helper.js index a8557532..664254a7 100644 --- a/src/vapid-helper.js +++ b/src/vapid-helper.js @@ -39,7 +39,7 @@ function generateVAPIDKeys() { function validateSubject(subject) { if (!subject) { - throw new Error('No subject set in vapid.subject.'); + throw new Error('No subject set in vapidDetails.subject.'); } if (typeof subject !== 'string' || subject.length === 0) { @@ -57,7 +57,7 @@ function validateSubject(subject) { function validatePublicKey(publicKey) { if (!publicKey) { - throw new Error('No key set vapid.publicKey'); + throw new Error('No key set vapidDetails.publicKey'); } if (typeof publicKey !== 'string') { @@ -74,7 +74,7 @@ function validatePublicKey(publicKey) { function validatePrivateKey(privateKey) { if (!privateKey) { - throw new Error('No key set in vapid.privateKey'); + throw new Error('No key set in vapidDetails.privateKey'); } if (typeof privateKey !== 'string') { @@ -103,7 +103,7 @@ function validatePrivateKey(privateKey) { */ function getVapidHeaders(audience, subject, publicKey, privateKey, expiration) { if (!audience) { - throw new Error('No audience set in vapid.audience.'); + throw new Error('No audience could be generated for VAPID.'); } if (typeof audience !== 'string' || audience.length === 0) { diff --git a/test/test-cli.js b/test/test-cli.js new file mode 100644 index 00000000..16d9afa3 --- /dev/null +++ b/test/test-cli.js @@ -0,0 +1,187 @@ +'use strict'; + +const assert = require('assert'); +const urlBase64 = require('urlsafe-base64'); +const spawn = require('child_process').spawn; + +const cliPath = 'src/cli.js'; + +suite('Test Encryption Helpers', function() { + test('no args run', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath + ]); + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); + + webpushCLISpawn.stderr.on('data', data => { + errorData += data; + }); + + webpushCLISpawn.on('close', code => { + // No args should have code 1 + assert(code, 1); + + assert.equal(errorData, ''); + assert.notEqual(consoleOutput.indexOf('web-push send-notification'), -1); + assert.notEqual(consoleOutput.indexOf('web-push generate-vapid-keys'), -1); + resolve(); + }); + }); + }); + + test('test send-notification no args', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'send-notification' + ]); + + webpushCLISpawn.on('close', code => { + // No args should have code 1 + assert.equal(code, 1); + resolve(); + }); + }); + }); + + test('test send-notification only endpoint', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'send-notification', + '--endpoint=https://example.push-service.com/' + ]); + + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); + + webpushCLISpawn.stderr.on('data', data => { + errorData += data; + }); + + webpushCLISpawn.on('close', code => { + assert.equal(code, 0); + assert.equal(errorData, ''); + assert.equal(consoleOutput.indexOf('Error sending push message: '), 0); + resolve(); + }); + }); + }); + + test('test send-notification all options', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'send-notification', + '--endpoint=https://example.push-service.com/', + '--key=browser-key', + '--auth=auth', + '--payload=hello', + '--ttl=1234', + '--vapid-subject=http://example.push-serice.com/contact', + '--vapid-pubkey=vapid-publicKey', + '--vapid-pvtkey=vapid-privateKey', + '--gcm-api-key=qwerty' + ]); + + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); + + webpushCLISpawn.stderr.on('data', data => { + errorData += data; + }); + + webpushCLISpawn.on('close', code => { + assert.equal(code, 0); + assert.equal(errorData, ''); + assert.equal(consoleOutput.indexOf('Error sending push message: '), 0); + resolve(); + }); + }); + }); + + test('test generate vapid keys', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'generate-vapid-keys' + ]); + + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); + + webpushCLISpawn.stderr.on('data', data => { + errorData += data; + }); + + webpushCLISpawn.on('close', code => { + assert.equal(code, 0); + assert.equal(errorData, ''); + assert.notEqual(consoleOutput.indexOf('Public Key:'), -1); + assert.notEqual(consoleOutput.indexOf('Private Key:'), -1); + + const lines = consoleOutput.split('\n'); + const publicKeyTitleIndex = lines.findIndex(line => { + return line.indexOf('Public Key:') !== -1; + }); + const publicKey = lines[publicKeyTitleIndex + 1].trim(); + assert.equal(urlBase64.decode(publicKey).length, 65); + + const privateKeyTitleIndex = lines.findIndex(line => { + return line.indexOf('Private Key:') !== -1; + }); + const privateKey = lines[privateKeyTitleIndex + 1].trim(); + assert.equal(urlBase64.decode(privateKey).length, 32); + resolve(); + }); + }); + }); + + test('test generate JSON vapid keys', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'generate-vapid-keys', + '--json' + ]); + + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); + + webpushCLISpawn.stderr.on('data', data => { + errorData += data; + }); + + webpushCLISpawn.on('close', code => { + assert.equal(code, 0); + assert.equal(errorData, ''); + + const vapidKeys = JSON.parse(consoleOutput); + assert(vapidKeys.publicKey); + assert(vapidKeys.privateKey); + + assert.equal(urlBase64.decode(vapidKeys.privateKey).length, 32); + assert.equal(urlBase64.decode(vapidKeys.publicKey).length, 65); + + resolve(); + }); + }); + }); +}); From 11f40ef6bd2831d932fc2f4231cf21e0a76661a0 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 23 Sep 2016 21:04:54 -0700 Subject: [PATCH 3/9] Adding content to readme --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index f938578e..3e0f5b50 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,24 @@ registration.pushManager.subscribe({ }); ``` +## Command Line + +You can install `web-push` globally and use it for send notifications and / or +generating VAPID keys. + +Install like so: + + npm install web-push -g + +Then you can run the following commands: + + Usage: + + web-push send-notification --endpoint= [--key=] [--auth=] [--payload=] [--ttl=] [--vapid-subject=] [--vapid-pubkey=] [--vapid-pvtkey=] [--gcm-api-key=] + + web-push generate-vapid-keys [--json] + + # API Reference ## sendNotification(pushSubscription, payload, options) From 3a08c81f90fcbaf66ba9c90f98fb5e76b508477d Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 23 Sep 2016 21:14:59 -0700 Subject: [PATCH 4/9] Fixing eslint errors --- src/cli.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/cli.js b/src/cli.js index fc52d354..9a562990 100755 --- a/src/cli.js +++ b/src/cli.js @@ -1,10 +1,9 @@ #! /usr/bin/env node -const fs = require('fs'); +/* eslint consistent-return:0*/ + const webPush = require('../src/index.js'); const printUsageDetails = () => { - const spc = ' '; - const actions = [ { name: 'send-notification', @@ -45,7 +44,7 @@ const generateVapidKeys = returnJson => { if (returnJson) { outputText = JSON.stringify(vapidKeys); } else { - const outputLine = '\n=======================================\n' + const outputLine = '\n=======================================\n'; outputText = outputLine + '\n' + 'Public Key:\n' + vapidKeys.publicKey + '\n\n' + 'Private Key:\n' + vapidKeys.privateKey + '\n' + @@ -62,31 +61,31 @@ const sendNotification = args => { } const subscription = { - endpoint: argv.endpoint, + endpoint: args.endpoint, keys: { - p256dh: argv.key || null, - auth: argv.auth || null + p256dh: args.key || null, + auth: args.auth || null } }; - const payload = argv.payload || null; + const payload = args.payload || null; const options = {}; - if (argv.ttl) { - options.TTL = argv.ttl; + if (args.ttl) { + options.TTL = args.ttl; } if (argv['vapid-subject'] || argv['vapid-pubkey'] || argv['vapid-pvtkey']) { options.vapidDetails = { - subject: argv['vapid-subject'] || null, - publicKey: argv['vapid-pubkey'] || null, - privateKey: argv['vapid-pvtkey'] || null - } + subject: args['vapid-subject'] || null, + publicKey: args['vapid-pubkey'] || null, + privateKey: args['vapid-pvtkey'] || null + }; } - if (argv['gcm-api-key']) { - options.gcmAPIKey = argv['gcm-api-key']; + if (args['gcm-api-key']) { + options.gcmAPIKey = args['gcm-api-key']; } webPush.sendNotification(subscription, payload, options) From bdbaad470a397f6331f61fc66c5d6fb3b91ebd17 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 23 Sep 2016 21:27:01 -0700 Subject: [PATCH 5/9] Disable CLI tests on old node --- test/test-cli.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test-cli.js b/test/test-cli.js index 16d9afa3..d7c4d1f0 100644 --- a/test/test-cli.js +++ b/test/test-cli.js @@ -1,5 +1,11 @@ 'use strict'; +const invalidNodeVersions = /0.(10|12).(\d+)/; +if (process.versions.node.match(invalidNodeVersions)) { + console.log('Skipping CLI tests as they can\'t run on node: ' + process.versions.node); + return; +} + const assert = require('assert'); const urlBase64 = require('urlsafe-base64'); const spawn = require('child_process').spawn; From d42619f90e6efc9b7d1eb4d8e86fb22279befbb0 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 23 Sep 2016 21:31:43 -0700 Subject: [PATCH 6/9] Wrap test for 0.10 --- test/test-cli.js | 316 ++++++++++++++++++++++++----------------------- 1 file changed, 159 insertions(+), 157 deletions(-) diff --git a/test/test-cli.js b/test/test-cli.js index d7c4d1f0..83968d59 100644 --- a/test/test-cli.js +++ b/test/test-cli.js @@ -1,193 +1,195 @@ 'use strict'; -const invalidNodeVersions = /0.(10|12).(\d+)/; -if (process.versions.node.match(invalidNodeVersions)) { - console.log('Skipping CLI tests as they can\'t run on node: ' + process.versions.node); - return; -} - -const assert = require('assert'); -const urlBase64 = require('urlsafe-base64'); -const spawn = require('child_process').spawn; - -const cliPath = 'src/cli.js'; - -suite('Test Encryption Helpers', function() { - test('no args run', function() { - return new Promise(resolve => { - const webpushCLISpawn = spawn('node', [ - cliPath - ]); - let errorData = ''; - let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { - consoleOutput += data; - }); +(function() { + const invalidNodeVersions = /0.(10|12).(\d+)/; + if (process.versions.node.match(invalidNodeVersions)) { + console.log('Skipping CLI tests as they can\'t run on node: ' + process.versions.node); + return; + } + + const assert = require('assert'); + const urlBase64 = require('urlsafe-base64'); + const spawn = require('child_process').spawn; + + const cliPath = 'src/cli.js'; + + suite('Test Encryption Helpers', function() { + test('no args run', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath + ]); + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); - webpushCLISpawn.stderr.on('data', data => { - errorData += data; - }); + webpushCLISpawn.stderr.on('data', data => { + errorData += data; + }); - webpushCLISpawn.on('close', code => { - // No args should have code 1 - assert(code, 1); + webpushCLISpawn.on('close', code => { + // No args should have code 1 + assert(code, 1); - assert.equal(errorData, ''); - assert.notEqual(consoleOutput.indexOf('web-push send-notification'), -1); - assert.notEqual(consoleOutput.indexOf('web-push generate-vapid-keys'), -1); - resolve(); + assert.equal(errorData, ''); + assert.notEqual(consoleOutput.indexOf('web-push send-notification'), -1); + assert.notEqual(consoleOutput.indexOf('web-push generate-vapid-keys'), -1); + resolve(); + }); }); }); - }); - test('test send-notification no args', function() { - return new Promise(resolve => { - const webpushCLISpawn = spawn('node', [ - cliPath, - 'send-notification' - ]); - - webpushCLISpawn.on('close', code => { - // No args should have code 1 - assert.equal(code, 1); - resolve(); + test('test send-notification no args', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'send-notification' + ]); + + webpushCLISpawn.on('close', code => { + // No args should have code 1 + assert.equal(code, 1); + resolve(); + }); }); }); - }); - test('test send-notification only endpoint', function() { - return new Promise(resolve => { - const webpushCLISpawn = spawn('node', [ - cliPath, - 'send-notification', - '--endpoint=https://example.push-service.com/' - ]); - - let errorData = ''; - let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { - consoleOutput += data; - }); + test('test send-notification only endpoint', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'send-notification', + '--endpoint=https://example.push-service.com/' + ]); + + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); - webpushCLISpawn.stderr.on('data', data => { - errorData += data; - }); + webpushCLISpawn.stderr.on('data', data => { + errorData += data; + }); - webpushCLISpawn.on('close', code => { - assert.equal(code, 0); - assert.equal(errorData, ''); - assert.equal(consoleOutput.indexOf('Error sending push message: '), 0); - resolve(); + webpushCLISpawn.on('close', code => { + assert.equal(code, 0); + assert.equal(errorData, ''); + assert.equal(consoleOutput.indexOf('Error sending push message: '), 0); + resolve(); + }); }); }); - }); - test('test send-notification all options', function() { - return new Promise(resolve => { - const webpushCLISpawn = spawn('node', [ - cliPath, - 'send-notification', - '--endpoint=https://example.push-service.com/', - '--key=browser-key', - '--auth=auth', - '--payload=hello', - '--ttl=1234', - '--vapid-subject=http://example.push-serice.com/contact', - '--vapid-pubkey=vapid-publicKey', - '--vapid-pvtkey=vapid-privateKey', - '--gcm-api-key=qwerty' - ]); - - let errorData = ''; - let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { - consoleOutput += data; - }); + test('test send-notification all options', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'send-notification', + '--endpoint=https://example.push-service.com/', + '--key=browser-key', + '--auth=auth', + '--payload=hello', + '--ttl=1234', + '--vapid-subject=http://example.push-serice.com/contact', + '--vapid-pubkey=vapid-publicKey', + '--vapid-pvtkey=vapid-privateKey', + '--gcm-api-key=qwerty' + ]); + + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); - webpushCLISpawn.stderr.on('data', data => { - errorData += data; - }); + webpushCLISpawn.stderr.on('data', data => { + errorData += data; + }); - webpushCLISpawn.on('close', code => { - assert.equal(code, 0); - assert.equal(errorData, ''); - assert.equal(consoleOutput.indexOf('Error sending push message: '), 0); - resolve(); + webpushCLISpawn.on('close', code => { + assert.equal(code, 0); + assert.equal(errorData, ''); + assert.equal(consoleOutput.indexOf('Error sending push message: '), 0); + resolve(); + }); }); }); - }); - - test('test generate vapid keys', function() { - return new Promise(resolve => { - const webpushCLISpawn = spawn('node', [ - cliPath, - 'generate-vapid-keys' - ]); - - let errorData = ''; - let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { - consoleOutput += data; - }); - - webpushCLISpawn.stderr.on('data', data => { - errorData += data; - }); - webpushCLISpawn.on('close', code => { - assert.equal(code, 0); - assert.equal(errorData, ''); - assert.notEqual(consoleOutput.indexOf('Public Key:'), -1); - assert.notEqual(consoleOutput.indexOf('Private Key:'), -1); + test('test generate vapid keys', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'generate-vapid-keys' + ]); + + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); - const lines = consoleOutput.split('\n'); - const publicKeyTitleIndex = lines.findIndex(line => { - return line.indexOf('Public Key:') !== -1; + webpushCLISpawn.stderr.on('data', data => { + errorData += data; }); - const publicKey = lines[publicKeyTitleIndex + 1].trim(); - assert.equal(urlBase64.decode(publicKey).length, 65); - const privateKeyTitleIndex = lines.findIndex(line => { - return line.indexOf('Private Key:') !== -1; + webpushCLISpawn.on('close', code => { + assert.equal(code, 0); + assert.equal(errorData, ''); + assert.notEqual(consoleOutput.indexOf('Public Key:'), -1); + assert.notEqual(consoleOutput.indexOf('Private Key:'), -1); + + const lines = consoleOutput.split('\n'); + const publicKeyTitleIndex = lines.findIndex(line => { + return line.indexOf('Public Key:') !== -1; + }); + const publicKey = lines[publicKeyTitleIndex + 1].trim(); + assert.equal(urlBase64.decode(publicKey).length, 65); + + const privateKeyTitleIndex = lines.findIndex(line => { + return line.indexOf('Private Key:') !== -1; + }); + const privateKey = lines[privateKeyTitleIndex + 1].trim(); + assert.equal(urlBase64.decode(privateKey).length, 32); + resolve(); }); - const privateKey = lines[privateKeyTitleIndex + 1].trim(); - assert.equal(urlBase64.decode(privateKey).length, 32); - resolve(); }); }); - }); - test('test generate JSON vapid keys', function() { - return new Promise(resolve => { - const webpushCLISpawn = spawn('node', [ - cliPath, - 'generate-vapid-keys', - '--json' - ]); - - let errorData = ''; - let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { - consoleOutput += data; - }); + test('test generate JSON vapid keys', function() { + return new Promise(resolve => { + const webpushCLISpawn = spawn('node', [ + cliPath, + 'generate-vapid-keys', + '--json' + ]); + + let errorData = ''; + let consoleOutput = ''; + webpushCLISpawn.stdout.on('data', data => { + consoleOutput += data; + }); - webpushCLISpawn.stderr.on('data', data => { - errorData += data; - }); + webpushCLISpawn.stderr.on('data', data => { + errorData += data; + }); - webpushCLISpawn.on('close', code => { - assert.equal(code, 0); - assert.equal(errorData, ''); + webpushCLISpawn.on('close', code => { + assert.equal(code, 0); + assert.equal(errorData, ''); - const vapidKeys = JSON.parse(consoleOutput); - assert(vapidKeys.publicKey); - assert(vapidKeys.privateKey); + const vapidKeys = JSON.parse(consoleOutput); + assert(vapidKeys.publicKey); + assert(vapidKeys.privateKey); - assert.equal(urlBase64.decode(vapidKeys.privateKey).length, 32); - assert.equal(urlBase64.decode(vapidKeys.publicKey).length, 65); + assert.equal(urlBase64.decode(vapidKeys.privateKey).length, 32); + assert.equal(urlBase64.decode(vapidKeys.publicKey).length, 65); - resolve(); + resolve(); + }); }); }); }); -}); +})(); From 6b28d5e8103aba8f81ba08db78bf826c0b6ffd10 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 23 Sep 2016 22:00:28 -0700 Subject: [PATCH 7/9] Tested on 0.10 --- test/test-cli.js | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/test/test-cli.js b/test/test-cli.js index 83968d59..a6facb8c 100644 --- a/test/test-cli.js +++ b/test/test-cli.js @@ -15,21 +15,21 @@ suite('Test Encryption Helpers', function() { test('no args run', function() { - return new Promise(resolve => { + return new Promise(function(resolve) { const webpushCLISpawn = spawn('node', [ cliPath ]); let errorData = ''; let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { + webpushCLISpawn.stdout.on('data', function(data) { consoleOutput += data; }); - webpushCLISpawn.stderr.on('data', data => { + webpushCLISpawn.stderr.on('data', function(data) { errorData += data; }); - webpushCLISpawn.on('close', code => { + webpushCLISpawn.on('close', function(code) { // No args should have code 1 assert(code, 1); @@ -42,13 +42,13 @@ }); test('test send-notification no args', function() { - return new Promise(resolve => { + return new Promise(function(resolve) { const webpushCLISpawn = spawn('node', [ cliPath, 'send-notification' ]); - webpushCLISpawn.on('close', code => { + webpushCLISpawn.on('close', function(code) { // No args should have code 1 assert.equal(code, 1); resolve(); @@ -57,7 +57,7 @@ }); test('test send-notification only endpoint', function() { - return new Promise(resolve => { + return new Promise(function(resolve) { const webpushCLISpawn = spawn('node', [ cliPath, 'send-notification', @@ -66,15 +66,15 @@ let errorData = ''; let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { + webpushCLISpawn.stdout.on('data', function(data) { consoleOutput += data; }); - webpushCLISpawn.stderr.on('data', data => { + webpushCLISpawn.stderr.on('data', function(data) { errorData += data; }); - webpushCLISpawn.on('close', code => { + webpushCLISpawn.on('close', function(code) { assert.equal(code, 0); assert.equal(errorData, ''); assert.equal(consoleOutput.indexOf('Error sending push message: '), 0); @@ -84,7 +84,7 @@ }); test('test send-notification all options', function() { - return new Promise(resolve => { + return new Promise(function(resolve) { const webpushCLISpawn = spawn('node', [ cliPath, 'send-notification', @@ -101,15 +101,15 @@ let errorData = ''; let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { + webpushCLISpawn.stdout.on('data', function(data) { consoleOutput += data; }); - webpushCLISpawn.stderr.on('data', data => { + webpushCLISpawn.stderr.on('data', function(data) { errorData += data; }); - webpushCLISpawn.on('close', code => { + webpushCLISpawn.on('close', function(code) { assert.equal(code, 0); assert.equal(errorData, ''); assert.equal(consoleOutput.indexOf('Error sending push message: '), 0); @@ -119,7 +119,7 @@ }); test('test generate vapid keys', function() { - return new Promise(resolve => { + return new Promise(function(resolve) { const webpushCLISpawn = spawn('node', [ cliPath, 'generate-vapid-keys' @@ -127,28 +127,28 @@ let errorData = ''; let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { + webpushCLISpawn.stdout.on('data', function(data) { consoleOutput += data; }); - webpushCLISpawn.stderr.on('data', data => { + webpushCLISpawn.stderr.on('data', function(data) { errorData += data; }); - webpushCLISpawn.on('close', code => { + webpushCLISpawn.on('close', function(code) { assert.equal(code, 0); assert.equal(errorData, ''); assert.notEqual(consoleOutput.indexOf('Public Key:'), -1); assert.notEqual(consoleOutput.indexOf('Private Key:'), -1); const lines = consoleOutput.split('\n'); - const publicKeyTitleIndex = lines.findIndex(line => { + const publicKeyTitleIndex = lines.findIndex(function(line) { return line.indexOf('Public Key:') !== -1; }); const publicKey = lines[publicKeyTitleIndex + 1].trim(); assert.equal(urlBase64.decode(publicKey).length, 65); - const privateKeyTitleIndex = lines.findIndex(line => { + const privateKeyTitleIndex = lines.findIndex(function(line) { return line.indexOf('Private Key:') !== -1; }); const privateKey = lines[privateKeyTitleIndex + 1].trim(); @@ -159,7 +159,7 @@ }); test('test generate JSON vapid keys', function() { - return new Promise(resolve => { + return new Promise(function(resolve) { const webpushCLISpawn = spawn('node', [ cliPath, 'generate-vapid-keys', @@ -168,15 +168,15 @@ let errorData = ''; let consoleOutput = ''; - webpushCLISpawn.stdout.on('data', data => { + webpushCLISpawn.stdout.on('data', function(data) { consoleOutput += data; }); - webpushCLISpawn.stderr.on('data', data => { + webpushCLISpawn.stderr.on('data', function(data) { errorData += data; }); - webpushCLISpawn.on('close', code => { + webpushCLISpawn.on('close', function(code) { assert.equal(code, 0); assert.equal(errorData, ''); From afafe59d24d6593f70f1459c586f21da45f66c91 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 23 Sep 2016 22:08:45 -0700 Subject: [PATCH 8/9] Fixing for node 4 --- src/cli.js | 1 + test/test-cli.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cli.js b/src/cli.js index 9a562990..b6ac8fee 100755 --- a/src/cli.js +++ b/src/cli.js @@ -1,5 +1,6 @@ #! /usr/bin/env node /* eslint consistent-return:0*/ +'use strict'; const webPush = require('../src/index.js'); diff --git a/test/test-cli.js b/test/test-cli.js index a6facb8c..0868c76d 100644 --- a/test/test-cli.js +++ b/test/test-cli.js @@ -13,7 +13,7 @@ const cliPath = 'src/cli.js'; - suite('Test Encryption Helpers', function() { + suite('Test CLI', function() { test('no args run', function() { return new Promise(function(resolve) { const webpushCLISpawn = spawn('node', [ From bd5f6dc64f53b69857f861f04d14b37f308f4ba2 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Sat, 24 Sep 2016 10:01:06 -0700 Subject: [PATCH 9/9] Fixing review --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3e0f5b50..fbe6fea6 100644 --- a/README.md +++ b/README.md @@ -91,8 +91,8 @@ registration.pushManager.subscribe({ ## Command Line -You can install `web-push` globally and use it for send notifications and / or -generating VAPID keys. +You can install `web-push` globally and use it for sending notifications +and / or generating VAPID keys. Install like so: