@@ -56,6 +56,9 @@ function setGCMAPIKey(apiKey) {
56
56
57
57
// Old standard, Firefox 44+.
58
58
function encryptOld ( userPublicKey , payload ) {
59
+ if ( typeof payload === 'string' || payload instanceof String ) {
60
+ payload = new Buffer ( payload ) ;
61
+ }
59
62
var localCurve = crypto . createECDH ( 'prime256v1' ) ;
60
63
61
64
var localPublicKey = localCurve . generateKeys ( ) ;
@@ -82,6 +85,9 @@ function encryptOld(userPublicKey, payload) {
82
85
83
86
// New standard, Firefox 46+ and Chrome 50+.
84
87
function encrypt ( userPublicKey , userAuth , payload ) {
88
+ if ( typeof payload === 'string' || payload instanceof String ) {
89
+ payload = new Buffer ( payload ) ;
90
+ }
85
91
var localCurve = crypto . createECDH ( 'prime256v1' ) ;
86
92
var localPublicKey = localCurve . generateKeys ( ) ;
87
93
@@ -160,12 +166,12 @@ function sendNotification(endpoint, params) {
160
166
var cryptoHeaderName ;
161
167
if ( userAuth ) {
162
168
// Use the new standard if userAuth is defined (Firefox 46+ and Chrome 50+).
163
- encrypted = encrypt ( userPublicKey , userAuth , new Buffer ( payload ) ) ;
169
+ encrypted = encrypt ( userPublicKey , userAuth , payload ) ;
164
170
encodingHeader = 'aesgcm' ;
165
171
cryptoHeaderName = 'Crypto-Key' ;
166
172
} else {
167
173
// Use the old standard if userAuth isn't defined (up to Firefox 45).
168
- encrypted = encryptOld ( userPublicKey , new Buffer ( payload ) ) ;
174
+ encrypted = encryptOld ( userPublicKey , payload ) ;
169
175
encodingHeader = 'aesgcm128' ;
170
176
cryptoHeaderName = 'Encryption-Key' ;
171
177
}
0 commit comments