Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-gcm

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-gcm - npm Package Compare versions

Comparing version 0.9.2 to 0.9.3

2

index.js

@@ -7,2 +7,2 @@ /*!

module.exports = require('./lib/node-gcm');
module.exports = require('./lib/node-gcm');

@@ -7,65 +7,67 @@ /*!

exports = module.exports = Constants = {
var Constants = {
'GCM_SEND_ENDPOINT' : 'android.googleapis.com',
'GCM_SEND_ENDPOINT' : 'android.googleapis.com',
'GCM_SEND_ENDPATH' : '/gcm/send',
'GCM_SEND_ENDPATH' : '/gcm/send',
'PARAM_REGISTRATION_ID' : 'registration_id',
'PARAM_REGISTRATION_ID' : 'registration_id',
'PARAM_COLLAPSE_KEY' : 'collapse_key',
'PARAM_COLLAPSE_KEY' : 'collapse_key',
'PARAM_DELAY_WHILE_IDLE' : 'delay_while_idle',
'PARAM_DELAY_WHILE_IDLE' : 'delay_while_idle',
'PARAM_PAYLOAD_KEY' : 'data',
'PARAM_PAYLOAD_KEY' : 'data',
'PARAM_TIME_TO_LIVE' : 'time_to_live',
'PARAM_TIME_TO_LIVE' : 'time_to_live',
'ERROR_QUOTA_EXCEEDED' : 'QuotaExceeded',
'ERROR_QUOTA_EXCEEDED' : 'QuotaExceeded',
'ERROR_DEVICE_QUOTA_EXCEEDED' : 'DeviceQuotaExceeded',
'ERROR_DEVICE_QUOTA_EXCEEDED' : 'DeviceQuotaExceeded',
'ERROR_MISSING_REGISTRATION' : 'MissingRegistration',
'ERROR_MISSING_REGISTRATION' : 'MissingRegistration',
'ERROR_INVALID_REGISTRATION' : 'InvalidRegistration',
'ERROR_INVALID_REGISTRATION' : 'InvalidRegistration',
'ERROR_MISMATCH_SENDER_ID' : 'MismatchSenderId',
'ERROR_MISMATCH_SENDER_ID' : 'MismatchSenderId',
'ERROR_NOT_REGISTERED' : 'NotRegistered',
'ERROR_NOT_REGISTERED' : 'NotRegistered',
'ERROR_MESSAGE_TOO_BIG' : 'MessageTooBig',
'ERROR_MESSAGE_TOO_BIG' : 'MessageTooBig',
'ERROR_MISSING_COLLAPSE_KEY' : 'MissingCollapseKey',
'ERROR_MISSING_COLLAPSE_KEY' : 'MissingCollapseKey',
'ERROR_UNAVAILABLE' : 'Unavailable',
'ERROR_UNAVAILABLE' : 'Unavailable',
'TOKEN_MESSAGE_ID' : 'id',
'TOKEN_MESSAGE_ID' : 'id',
'TOKEN_CANONICAL_REG_ID' : 'registration_id',
'TOKEN_CANONICAL_REG_ID' : 'registration_id',
'TOKEN_ERROR' : 'Error',
'TOKEN_ERROR' : 'Error',
'JSON_REGISTRATION_IDS' : 'registration_ids',
'JSON_REGISTRATION_IDS' : 'registration_ids',
'JSON_PAYLOAD' : 'data',
'JSON_PAYLOAD' : 'data',
'JSON_SUCCESS' : 'success',
'JSON_SUCCESS' : 'success',
'JSON_FAILURE' : 'failure',
'JSON_FAILURE' : 'failure',
'JSON_CANONICAL_IDS' : 'canonical_ids',
'JSON_CANONICAL_IDS' : 'canonical_ids',
'JSON_MULTICAST_ID' : 'multicast_id',
'JSON_MULTICAST_ID' : 'multicast_id',
'JSON_RESULTS' : 'results',
'JSON_RESULTS' : 'results',
'JSON_ERROR' : 'error',
'JSON_ERROR' : 'error',
'JSON_MESSAGE_ID' : 'message_id',
'JSON_MESSAGE_ID' : 'message_id',
'UTF8' : 'UTF-8',
'UTF8' : 'UTF-8',
'BACKOFF_INITIAL_DELAY' : 1000,
'BACKOFF_INITIAL_DELAY' : 1000,
'MAX_BACKOFF_DELAY' : 1024000
}
'MAX_BACKOFF_DELAY' : 1024000
};
module.exports = Constants;

@@ -7,15 +7,17 @@ /*!

exports = module.exports = Message;
function Message () {
this.collapseKey = undefined;
function Message() {
this.collapseKey = undefined;
this.delayWhileIdle = undefined;
this.timeToLive = undefined;
this.data = {};
this.hasData = false;
this.hasData = false;
}
Message.prototype.addData = function (key, value) {
this.hasData = true;
this.data[key] = value.toString();
};
Message.prototype.addData = function(key, value) {
this.hasData = true;
this.data[key] = value.toString();
};
module.exports = Message;

@@ -7,19 +7,19 @@ /*!

exports = module.exports = MulitcastResult;
function MulitcastResult () {
this.success = undefined;
function MulitcastResult() {
this.success = undefined;
this.failure = undefined;
this.canonicalIds = undefined;
this.multicastId = undefined;
this.results = [];
this.retryMulticastIds = [];
};
this.canonicalIds = undefined;
this.multicastId = undefined;
this.results = [];
this.retryMulticastIds = [];
}
MulitcastResult.prototype.addResult = function(result) {
this.results.push(result);
MulitcastResult.prototype.addResult = function (result) {
this.results.push(result);
};
MulitcastResult.prototype.getTotal = function () {
return this.success + this.failure;
return this.success + this.failure;
};
module.exports = MulitcastResult;

@@ -7,8 +7,8 @@ /*!

exports = module.exports = Result;
function Result () {
this.messageId = undefined;
function Result() {
this.messageId = undefined;
this.canonicalRegistrationId = undefined;
this.errorCode = undefined;
};
}
module.exports = Result;

@@ -6,7 +6,4 @@ /*!

*/
var Constants = require('./constants');
var Message = require('./message');
var Result = require('./result');
var MulitcastResult = require('./multicastresult');

@@ -16,147 +13,143 @@ var https = require('https');

exports = module.exports = Sender;
function Sender (key) {
this.key = key;
};
function Sender(key) {
this.key = key;
}
Sender.prototype.send = function(message, registrationId, retries, callback) {
var attempt = 1;
var backoff = Constants.BACKOFF_INITIAL_DELAY;
var tryAgain;
if(registrationId.length === 1) {
var sendNoRetryMethod = Sender.prototype.sendNoRetry = function (message, registrationIds, callback) {
var body = {},
result = new Result(),
requestBody,
post_options,
post_req;
this.sendNoRetry(message, registrationId, function lambda (result) {
if(result === undefined) {
if(attempt < retries) {
var sleepTime = backoff * 2 * attempt;
if (sleepTime > Constants.MAX_BACKOFF_DELAY)
sleepTime = Constants.MAX_BACKOFF_DELAY;
timer.setTimeout(function () {
sendNoRetryMethod(message, registrationId, lambda);
},sleepTime);
}
else {
console.log('Could not send message after ' + retries + ' attempts');
callback(result);
}
attempt++;
}
else callback(result);
});
}
else if (registrationId.length > 1) {
this.sendNoRetry(message, registrationId, function lambda (result) {
body[Constants.JSON_REGISTRATION_IDS] = registrationIds;
if(attempt < retries) {
var sleepTime = backoff * 2 * attempt;
if (sleepTime > Constants.MAX_BACKOFF_DELAY)
sleepTime = Constants.MAX_BACKOFF_DELAY;
var unsentRegIds = [];
if (message.delayWhileIdle !== undefined) {
body[Constants.PARAM_DELAY_WHILE_IDLE] = message.delayWhileIdle;
}
if (message.collapseKey !== undefined) {
body[Constants.PARAM_COLLAPSE_KEY] = message.collapseKey;
}
if (message.timeToLive !== undefined) {
body[Constants.PARAM_TIME_TO_LIVE] = message.timeToLive;
}
if (message.hasData) {
body[Constants.PARAM_PAYLOAD_KEY] = message.data;
}
for (var i = 0; i < registrationId.length;i++) {
if (result.results[i].error === 'Unavailable')
unsentRegIds.push(registrationId[i]);
}
requestBody = JSON.stringify(body);
post_options = {
host: Constants.GCM_SEND_ENDPOINT,
port: '443',
path: Constants.GCM_SEND_ENDPATH,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-length': Buffer.byteLength(requestBody, 'utf8'),
'Authorization': 'key=' + this.key
}
};
registrationId = unsentRegIds;
if(registrationId.length !== 0) {
timer.setTimeout(function () {
sendNoRetryMethod(message, registrationId, lambda);
},sleepTime);
attempt++;
}
else callback(result);
}
else {
console.log('Could not send message to all devices after ' + retries + ' attempts');
callback(result);
}
});
}
post_req = https.request(post_options, function (res) {
var statusCode = res.statusCode,
buf = '';
res.setEncoding('utf-8');
res.on('data', function (data) {
buf += data;
});
else console.log('No RegistrationIds given!');
res.on('end', function () {
if (statusCode === 503) {
console.log('GCM service is unavailable');
return callback(statusCode, null);
} else if(statusCode == 401){
console.log('Unauthorized');
return callback(statusCode, null);
} else if (statusCode !== 200) {
console.log('Invalid request: ' + statusCode);
return callback(statusCode, null);
}
// Make sure that we don't crash in case something goes wrong while
// handling the response.
try {
var data = JSON.parse(buf);
callback(null, data);
} catch (e) {
console.log("Error handling GCM response " + e);
callback("error", null);
}
});
});
post_req.on('error', function (e) {
console.log("Exception during GCM request: " + e);
callback("request error", null);
});
post_req.write(requestBody);
post_req.end();
};
var sendNoRetryMethod = Sender.prototype.sendNoRetry = function(message, registrationIds, callback) {
var body = {}, result = new Result();
Sender.prototype.send = function (message, registrationId, retries, callback) {
body[Constants.JSON_REGISTRATION_IDS] = registrationIds;
var attempt = 1,
backoff = Constants.BACKOFF_INITIAL_DELAY;
if (message.delayWhileIdle !== undefined) {
body[Constants.PARAM_DELAY_WHILE_IDLE] = message.delayWhileIdle;
}
if (message.collapseKey !== undefined) {
body[Constants.PARAM_COLLAPSE_KEY] = message.collapseKey;
}
if (message.hasData) {
body[Constants.PARAM_PAYLOAD_KEY] = message.data;
}
if (registrationId.length === 1) {
var requestBody = JSON.stringify(body);
this.sendNoRetry(message, registrationId, function lambda(err, result) {
var post_options = {
host: Constants.GCM_SEND_ENDPOINT,
port: '443',
path: Constants.GCM_SEND_ENDPATH,
method: 'POST',
headers: {
'Content-Type' : 'application/json',
'Content-length' : Buffer.byteLength(requestBody, 'utf8'),
'Authorization' : 'key=' + this.key
}
};
if (result === undefined) {
if (attempt < retries) {
var sleepTime = backoff * 2 * attempt;
if (sleepTime > Constants.MAX_BACKOFF_DELAY) {
sleepTime = Constants.MAX_BACKOFF_DELAY;
}
timer.setTimeout(function () {
sendNoRetryMethod(message, registrationId, lambda);
}, sleepTime);
} else {
console.log('Could not send message after ' + retries + ' attempts');
callback(null, result);
}
attempt += 1;
} else callback(null, result);
});
} else if (registrationId.length > 1) {
this.sendNoRetry(message, registrationId, function lambda(err, result) {
var post_req = https.request(post_options, function(res) {
res.setEncoding('utf-8');
var statusCode = res.statusCode;
if (attempt < retries) {
var sleepTime = backoff * 2 * attempt,
unsentRegIds = [],
i;
if (sleepTime > Constants.MAX_BACKOFF_DELAY) {
sleepTime = Constants.MAX_BACKOFF_DELAY;
}
var buf = '';
res.on('data', function (data) {
buf += data;
});
res.on('end', function () {
if (statusCode === 503) {
console.log('GCM service is unavailable');
callback();
}
else if (statusCode !== 200) {
console.log('Invalid request: ' + statusCode);
callback();
}
for (i = 0; i < registrationId.length; i += 1) {
if (result.results[i].error === 'Unavailable') {
unsentRegIds.push(registrationId[i]);
}
}
// Make sure that we don't crash in case something goes wrong while
// handling the response.
try {
var data = JSON.parse(buf);
if (registrationIds.length === 1) {
if(data.results[0].message_id)
result.messageId = data.results[0].message_id;
else if(data.results[0].error)
result.errorCode = data.results[0].error;
else if(data.results[0].registration_id)
result.canonicalRegistrationId = data.results[0].registration_id;
callback(result);
}
else callback(data);
} catch(e) {
console.log("Error handling GCM response " + e);
callback();
}
});
});
registrationId = unsentRegIds;
if (registrationId.length !== 0) {
timer.setTimeout(function () {
sendNoRetryMethod(message, registrationId, lambda);
}, sleepTime);
attempt += 1;
} else callback(null, result);
post_req.on('error', function(e) {
console.log("Exception during GCM request: " + e);
callback();
});
} else {
console.log('Could not send message to all devices after ' + retries + ' attempts');
callback(null, result);
}
});
} else {
console.log('No RegistrationIds given!');
}
};
post_req.write(requestBody);
post_req.end();
};
module.exports = Sender;
{
"name": "node-gcm",
"description": "a Node.JS wrapper library-port for Google Cloud Messaging for Android",
"version": "0.9.2",
"version": "0.9.3",
"author": "Marcus Farkas <marcus.farkas@spaceteam.at>",

@@ -11,3 +11,4 @@ "contributors": [

{ "name": "Yann Biancheri", "email": "yann.biancheri@gmail.com" },
{ "name": "Hamid Palo", "email": ""}
{ "name": "Hamid Palo", "email": ""},
{ "name": "Dotan J. Nahum", "email": "jondotan@gmail.com" }
],

@@ -14,0 +15,0 @@ "repository": {

@@ -36,3 +36,3 @@ # node-gcm

*/
sender.send(message, registrationIds, 4, function (result) {
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);

@@ -44,3 +44,3 @@ });

```js
sender.sendNoRetry(message, registrationIds-array, function (result) {
sender.sendNoRetry(message, registrationIds-array, function (err, result) {
console.log(result);

@@ -55,2 +55,3 @@ });

* [Hamid Palo](https://github.com/hamidp)
* [Dotan J.Nahum](https://github.com/jondot)

@@ -83,2 +84,7 @@ ## License

## Changelog
**0.9.3:**
* new callback-style (Please check the example above)
* fixes various issues (Read commit messages)
* not making a distinction between a single and multiple result makes it easier for application-land code to handle
**0.9.2:**

@@ -85,0 +91,0 @@ * added error handler to HTTPS request to handle DNS exceptions (Thanks goes to [monkbroc](https://github.com/monkbroc))

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc