UNPKG

3.27 kBJavaScriptView Raw
1// Copyright Jeff Wilcox
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16var TEST_TITLE = 'Hi.';
17var TEST_MESSAGE = 'This is a test.';
18
19var mpns = require('../lib/mpns')
20 , fs = require('fs');
21
22var args = process.argv;
23args.shift();
24var cmd = 'node ' + args.shift();
25
26function help() {
27 console.log('Node.js MPNS Module Test Toaster');
28 console.log('Please provide the pushUri to a Microsoft Push Notification Service (MPNS) endpoint to a Windows Phone device.');
29 console.log();
30 console.log('Parameters:');
31 console.log(' ' + cmd + ' pushUri [Message]');
32 console.log();
33
34 // Right now my implementation for auth push testing is specific to my key use practices.
35 console.log('Authenticated push notification channels are supported with the appropriate environment variables:');
36 console.log(' MPNS_CERT: Point to a certificate file.');
37 console.log(' MPNS_CA: Point to a certificate authority or intermediate chain file.');
38 console.log(' MPNS_KEY: Point to a private key file.');
39 console.log();
40 console.log('Or for authenticated push with a PKCS12 package format:');
41 console.log(' MPNS_PFX: The PKCS12 file.');
42 console.log(' MPNS_PASSPHRASE: The optional password for the PKCS12 file.')
43}
44
45if (args.length == 0) {
46 return help();
47}
48
49var uri = args.shift();
50
51if (uri.indexOf('http') !== 0) {
52 console.log('The first parameter must be a URI.');
53 return help();
54}
55
56var options = {
57 text1: args.shift() || TEST_TITLE,
58 text2: args.length > 0 ? args.join(' ') : TEST_MESSAGE
59};
60
61var authenticationReady = false;
62var fileEncoding = undefined;
63if (process.env.MPNS_CERT && process.env.MPNS_KEY) {
64 options.cert = fs.readFileSync(process.env.MPNS_CERT, fileEncoding);
65 options.key = fs.readFileSync(process.env.MPNS_KEY, fileEncoding);
66 var ca = process.env.MPNS_CA;
67 if (ca !== undefined) {
68 options.ca = fs.readFileSync(ca, fileEncoding);
69 }
70 authenticationReady = true;
71} else if (process.env.MPNS_PFX) {
72 options.pfx = fs.readFileSync(process.env.MPNS_PFX, fileEncoding);
73 var passphrase = process.env.MPNS_PASSPHRASE;
74 if (passphrase !== undefined) {
75 options.passphrase = passphrase;
76 }
77 authenticationReady = true;
78}
79
80if (uri.indexOf('https') == 0) {
81 if (!authenticationReady) {
82 throw new Error('Authenticated push channels are not currently supported by this test application unless environment variables are set properly.');
83 } else {
84 var keys = [];
85 for (var k in mpns.Properties.ssl) {
86 var key = mpns.Properties.ssl[k];
87 if (options[key]) {
88 keys.push(key);
89 }
90 }
91 console.log('Authenticated push notification channel: ' + keys.join(', '));
92 }
93}
94
95console.log('Sending a toast...');
96
97mpns.sendToast(uri, options, function (err, result) {
98 console.log(err ? 'Error' : 'OK');
99 console.dir(err || result);
100});