Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli update #227

Merged
merged 9 commits into from
Sep 24, 2016
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
coverage/
node_modules/
wires
test/output/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unneeded change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wires isn't generated by anything in this project.

We used to download and name a file wires for selenium, then selenium looked for 'geckodriver' which we did but saved somewhere else and now we just use the geckodriver module.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. I still have the wires executable in my folder 😄

18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ registration.pushManager.subscribe({
});
```

## Command Line

You can install `web-push` globally and use it for send notifications and / or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: sending

generating VAPID keys.

Install like so:

npm install web-push -g

Then you can run the following commands:

Usage:

web-push send-notification --endpoint=<url> [--key=<browser key>] [--auth=<auth secret>] [--payload=<message>] [--ttl=<seconds>] [--vapid-subject=<vapid subject>] [--vapid-pubkey=<public key url base64>] [--vapid-pvtkey=<private key url base64>] [--gcm-api-key=<api key>]

web-push generate-vapid-keys [--json]


# API Reference

## sendNotification(pushSubscription, payload, options)
Expand Down
68 changes: 0 additions & 68 deletions bin/web-push.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
120 changes: 120 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#! /usr/bin/env node
/* eslint consistent-return:0*/
'use strict';

const webPush = require('../src/index.js');

const printUsageDetails = () => {
const actions = [
{
name: 'send-notification',
options: [
'--endpoint=<url>',
'[--key=<browser key>]',
'[--auth=<auth secret>]',
'[--payload=<message>]',
'[--ttl=<seconds>]',
'[--vapid-subject=<vapid subject>]',
'[--vapid-pubkey=<public key url base64>]',
'[--vapid-pvtkey=<private key url base64>]',
'[--gcm-api-key=<api key>]'
]
}, {
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';
});

console.log(usage);
process.exit(1);
};

const generateVapidKeys = returnJson => {
const vapidKeys = webPush.generateVAPIDKeys();

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;
}

console.log(outputText);
process.exit(0);
};

const sendNotification = args => {
if (process.env.GCM_API_KEY) {
webPush.setGCMAPIKey(process.env.GCM_API_KEY);
}

const subscription = {
endpoint: args.endpoint,
keys: {
p256dh: args.key || null,
auth: args.auth || null
}
};

const payload = args.payload || null;

const options = {};

if (args.ttl) {
options.TTL = args.ttl;
}

if (argv['vapid-subject'] || argv['vapid-pubkey'] || argv['vapid-pvtkey']) {
options.vapidDetails = {
subject: args['vapid-subject'] || null,
publicKey: args['vapid-pubkey'] || null,
privateKey: args['vapid-pvtkey'] || null
};
}

if (args['gcm-api-key']) {
options.gcmAPIKey = args['gcm-api-key'];
}

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) {
return printUsageDetails();
}

sendNotification(argv);
break;
case 'generate-vapid-keys':
generateVapidKeys(argv.json || false);
break;
default:
printUsageDetails();
break;
}
8 changes: 4 additions & 4 deletions src/vapid-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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') {
Expand All @@ -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') {
Expand Down Expand Up @@ -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) {
Expand Down
Loading