@@ -153,11 +153,11 @@ function sendNotification(endpoint, params) {
153
153
}
154
154
} ;
155
155
156
- var encrypted ;
156
+ var requestPayload ;
157
157
if ( typeof payload !== 'undefined' ) {
158
+ var encrypted ;
158
159
var encodingHeader ;
159
160
var cryptoHeaderName ;
160
-
161
161
if ( userAuth ) {
162
162
// Use the new standard if userAuth is defined (Firefox 46+ and Chrome 50+).
163
163
encrypted = encrypt ( userPublicKey , userAuth , new Buffer ( payload ) ) ;
@@ -171,16 +171,16 @@ function sendNotification(endpoint, params) {
171
171
}
172
172
173
173
options . headers = {
174
- 'Content-Length' : encrypted . cipherText . length ,
175
174
'Content-Type' : 'application/octet-stream' ,
176
175
'Content-Encoding' : encodingHeader ,
177
176
'Encryption' : 'keyid=p256dh;salt=' + encrypted . salt ,
178
177
} ;
179
178
180
179
options . headers [ cryptoHeaderName ] = 'keyid=p256dh;dh=' + urlBase64 . encode ( encrypted . localPublicKey ) ;
180
+
181
+ requestPayload = encrypted . cipherText ;
181
182
}
182
183
183
- var gcmPayload ;
184
184
if ( isGCM ) {
185
185
if ( ! gcmAPIKey ) {
186
186
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) {
192
192
var gcmObj = {
193
193
registration_ids : [ subscriptionId ] ,
194
194
} ;
195
- if ( encrypted ) {
196
- gcmObj [ 'raw_data' ] = encrypted . cipherText . toString ( 'base64' ) ;
195
+ if ( requestPayload ) {
196
+ gcmObj [ 'raw_data' ] = requestPayload . toString ( 'base64' ) ;
197
197
}
198
- gcmPayload = JSON . stringify ( gcmObj ) ;
198
+ requestPayload = JSON . stringify ( gcmObj ) ;
199
199
200
200
options . path = options . path . substring ( 0 , options . path . length - subscriptionId . length - 1 ) ;
201
201
202
202
options . headers [ 'Authorization' ] = 'key=' + gcmAPIKey ;
203
203
options . headers [ 'Content-Type' ] = 'application/json' ;
204
- options . headers [ 'Content-Length' ] = gcmPayload . length ;
205
204
}
206
205
207
206
if ( vapid && ! isGCM && ( typeof payload === 'undefined' || 'Crypto-Key' in options . headers ) ) {
@@ -241,6 +240,10 @@ function sendNotification(endpoint, params) {
241
240
options . headers [ 'TTL' ] = 2419200 ; // Default TTL is four weeks.
242
241
}
243
242
243
+ if ( requestPayload ) {
244
+ options . headers [ 'Content-Length' ] = requestPayload . length ;
245
+ }
246
+
244
247
var expectedStatusCode = isGCM ? 200 : 201 ;
245
248
var pushRequest = https . request ( options , function ( pushResponse ) {
246
249
var body = "" ;
@@ -258,10 +261,8 @@ function sendNotification(endpoint, params) {
258
261
} ) ;
259
262
} ) ;
260
263
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 ) ;
265
266
}
266
267
267
268
pushRequest . end ( ) ;
0 commit comments