Skip to content

Commit 1956b52

Browse files
author
Matt Gaunt
committed
adding some simple tests
1 parent 644a073 commit 1956b52

File tree

3 files changed

+135
-8
lines changed

3 files changed

+135
-8
lines changed

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,131 @@ encryption.
276276
- *salt*: A string representing the salt used to encrypt the payload.
277277
- *cipherText*: The encrypted payload as a Buffer.
278278

279+
<hr />
280+
281+
## getVapidHeaders(audience, subject, publicKey, privateKey, expiration)
282+
283+
```javascript
284+
const parsedUrl = url.parse(subscription.endpoint);
285+
const audience = parsedUrl.protocol + '//' +
286+
parsedUrl.hostname;
287+
288+
const vapidHeaders = vapidHelper.getVapidHeaders(
289+
audience,
290+
'mailto: [email protected]',
291+
vapidDetails.publicKey,
292+
vapidDetails.privateKey
293+
);
294+
```
295+
296+
Encrypts the payload according to the [Message Encryption for Web
297+
Push](https://webpush-wg.github.io/webpush-encryption/) standard.
298+
299+
> (*sendNotification* will automatically encrypt the payload for you, so if
300+
> you use *sendNotification* you don't need to worry about it).
301+
302+
### Input
303+
304+
The `getVapidHeaders()` method expects the following input:
305+
306+
- *audience*: the origin of the **push service**.
307+
- *subject*: the mailto or URL for your application.
308+
- *publicKey*: the VAPID public key.
309+
- *privateKey*: the VAPID private key.
310+
311+
### Returns
312+
313+
This method returns an object with the following fields:
314+
315+
- *localPublicKey*: The public key matched the private key used during
316+
encryption.
317+
- *salt*: A string representing the salt used to encrypt the payload.
318+
- *cipherText*: The encrypted payload as a Buffer.
319+
320+
<hr />
321+
322+
## generateRequestDetails(pushSubscription, payload, options)
323+
324+
```javascript
325+
const pushSubscription = {
326+
endpoint: '< Push Subscription URL >';
327+
keys: {
328+
p256dh: '< User Public Encryption Key >',
329+
auth: '< User Auth Secret >'
330+
}
331+
};
332+
333+
const payload = '< Push Payload String >';
334+
335+
const options = {
336+
gcmAPIKey: '< GCM API Key >',
337+
vapidDetails: {
338+
subject: '< \'mailto\' Address or URL >',
339+
publicKey: '< URL Safe Base64 Encoded Public Key >',
340+
privateKey: '< URL Safe Base64 Encoded Private Key >',
341+
}
342+
TTL: <Number>
343+
}
344+
345+
try {
346+
const details = webpush.generateRequestDetails(
347+
pushSubscription,
348+
payload,
349+
options
350+
);
351+
} catch (err) {
352+
console.error(err);
353+
}
354+
```
355+
356+
> **Note:** `generateRequestDetails()` you don't need to define a payload,
357+
and this method will return details without a GCM API Key and / or VAPID keys.
358+
359+
### Input
360+
361+
**Push Subscription**
362+
363+
The first argument must be an object containing the details for a push
364+
subscription.
365+
366+
The expected format is the same output as JSON.stringify'ing a PushSubscription
367+
in the browser.
368+
369+
**Payload**
370+
371+
The payload is optional, but if set, will be encrypted and a [*Buffer*](https://nodejs.org/api/buffer.html)
372+
will be returned via the `payload` parameter.
373+
374+
This argument must be either a *string* or a node
375+
[*Buffer*](https://nodejs.org/api/buffer.html).
376+
377+
> **Note:** In order to encrypt the *payload*, the *pushSubscription* **must**
378+
have a *keys* object with *p256dh* and *auth* values.
379+
380+
**Options**
381+
382+
Options is an optional argument that if defined should be an object containing
383+
any of the following values defined, although none of them are required.
384+
385+
- **gcmAPIKey** can be a GCM API key to be used for this request and this
386+
request only. This overrides any API key set via `setGCMAPIKey()`.
387+
- **vapidDetails** should be an object with *subject*, *publicKey* and
388+
*privateKey* values defined. These values should follow the [VAPID Spec](https://tools.ietf.org/html/draft-thomson-webpush-vapid).
389+
- **TTL** is a value in seconds that describes how long a push message is
390+
retained by the push service (by default, four weeks);
391+
392+
### Returns
393+
394+
An object contains all the details needed to make a network request, the
395+
object will contain:
396+
397+
- *endpoint*, the URL to send the request to;
398+
- *method*, this will be 'POST';
399+
- *headers*, the headers to add to the request;
400+
- *body*, the body of the request (As a Node Buffer).
401+
402+
<hr />
403+
279404
# Browser Support
280405

281406
<table>

src/web-push-lib.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,19 @@ WebPushLib.prototype.setVapidDetails =
6767
};
6868

6969
/**
70-
* To send a push notification call this method with a subscription, optional
71-
* payload and any options.
70+
* To get the details of a request to trigger a push message, without sending
71+
* a push notification call this method.
72+
*
73+
* This method will throw an error if there is an issue with the input.
7274
* @param {PushSubscription} subscription The PushSubscription you wish to
7375
* send the notification to.
7476
* @param {string} [payload] The payload you wish to send to the
7577
* the user.
7678
* @param {Object} [options] Options for the GCM API key and
7779
* vapid keys can be passed in if they are unique for each notification you
7880
* wish to send.
79-
* @return {Promise} This method returns a Promise which
80-
* resolves if the sending of the notification was successful, otherwise it
81-
* rejects.
81+
* @return {Object} This method returns an Object which
82+
* contains 'endpoint', 'method', 'headers' and 'payload'.
8283
*/
8384
WebPushLib.prototype.generateRequestDetails =
8485
function(subscription, payload, options) {
@@ -203,7 +204,7 @@ WebPushLib.prototype.generateRequestDetails =
203204
}
204205
}
205206

206-
requestDetails.payload = requestPayload;
207+
requestDetails.body = requestPayload;
207208
requestDetails.endpoint = subscription.endpoint;
208209

209210
return requestDetails;
@@ -269,8 +270,8 @@ WebPushLib.prototype.sendNotification =
269270
reject(e);
270271
});
271272

272-
if (requestDetails.payload) {
273-
pushRequest.write(requestDetails.payload);
273+
if (requestDetails.body) {
274+
pushRequest.write(requestDetails.body);
274275
}
275276

276277
pushRequest.end();

test/test-vapid-helper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const VALID_EXPIRATION = Math.floor(Date.now() / 1000) + (60 * 60 * 12);
1515
suite('Test Vapid Helpers', function() {
1616
test('is defined', function() {
1717
assert(webPush.generateVAPIDKeys);
18+
assert(webPush.getVapidHeaders);
1819
});
1920

2021
test('generate vapid keys', function() {

0 commit comments

Comments
 (0)