Skip to content

Commit b9156f6

Browse files
committed
Simplify handling of GCM/standard web push
1 parent b702b9e commit b9156f6

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ function sendNotification(endpoint, params) {
153153
}
154154
};
155155

156-
var encrypted;
156+
var requestPayload;
157157
if (typeof payload !== 'undefined') {
158+
var encrypted;
158159
var encodingHeader;
159160
var cryptoHeaderName;
160-
161161
if (userAuth) {
162162
// Use the new standard if userAuth is defined (Firefox 46+ and Chrome 50+).
163163
encrypted = encrypt(userPublicKey, userAuth, new Buffer(payload));
@@ -171,16 +171,16 @@ function sendNotification(endpoint, params) {
171171
}
172172

173173
options.headers = {
174-
'Content-Length': encrypted.cipherText.length,
175174
'Content-Type': 'application/octet-stream',
176175
'Content-Encoding': encodingHeader,
177176
'Encryption': 'keyid=p256dh;salt=' + encrypted.salt,
178177
};
179178

180179
options.headers[cryptoHeaderName] = 'keyid=p256dh;dh=' + urlBase64.encode(encrypted.localPublicKey);
180+
181+
requestPayload = encrypted.cipherText;
181182
}
182183

183-
var gcmPayload;
184184
if (isGCM) {
185185
if (!gcmAPIKey) {
186186
console.warn('Attempt to send push notification to GCM endpoint, but no GCM key is defined'.bold.red);
@@ -192,16 +192,15 @@ function sendNotification(endpoint, params) {
192192
var gcmObj = {
193193
registration_ids: [ subscriptionId ],
194194
};
195-
if (encrypted) {
196-
gcmObj['raw_data'] = encrypted.cipherText.toString('base64');
195+
if (requestPayload) {
196+
gcmObj['raw_data'] = requestPayload.toString('base64');
197197
}
198-
gcmPayload = JSON.stringify(gcmObj);
198+
requestPayload = JSON.stringify(gcmObj);
199199

200200
options.path = options.path.substring(0, options.path.length - subscriptionId.length - 1);
201201

202202
options.headers['Authorization'] = 'key=' + gcmAPIKey;
203203
options.headers['Content-Type'] = 'application/json';
204-
options.headers['Content-Length'] = gcmPayload.length;
205204
}
206205

207206
if (vapid && !isGCM && (typeof payload === 'undefined' || 'Crypto-Key' in options.headers)) {
@@ -241,6 +240,10 @@ function sendNotification(endpoint, params) {
241240
options.headers['TTL'] = 2419200; // Default TTL is four weeks.
242241
}
243242

243+
if (requestPayload) {
244+
options.headers['Content-Length'] = requestPayload.length;
245+
}
246+
244247
var expectedStatusCode = isGCM ? 200 : 201;
245248
var pushRequest = https.request(options, function(pushResponse) {
246249
var body = "";
@@ -258,10 +261,8 @@ function sendNotification(endpoint, params) {
258261
});
259262
});
260263

261-
if (isGCM) {
262-
pushRequest.write(gcmPayload);
263-
} else if (typeof payload !== 'undefined') {
264-
pushRequest.write(encrypted.cipherText);
264+
if (requestPayload) {
265+
pushRequest.write(requestPayload);
265266
}
266267

267268
pushRequest.end();

0 commit comments

Comments
 (0)