From 440f4ec11d3231f3587de1f0c5be64400211bb22 Mon Sep 17 00:00:00 2001 From: Simon Woolf Date: Mon, 11 May 2015 18:32:40 +0100 Subject: [PATCH] Allow message and presencemessage to work without data --- common/lib/client/presence.js | 2 +- common/lib/types/message.js | 14 ++++++++------ common/lib/types/presencemessage.js | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/common/lib/client/presence.js b/common/lib/client/presence.js index 2cda2c1c63..d6113eb244 100644 --- a/common/lib/client/presence.js +++ b/common/lib/client/presence.js @@ -23,7 +23,7 @@ var Presence = (function() { Presence.prototype.enterClient = function(clientId, data, callback) { if (!callback && (typeof(data)==='function')) { callback = data; - data = ''; + data = null; } Logger.logAction(Logger.LOG_MICRO, 'Presence.enterClient()', 'entering; channel = ' + this.channel.name + ', client = ' + clientId); var presence = PresenceMessage.fromValues({ diff --git a/common/lib/types/message.js b/common/lib/types/message.js index d64407c85f..9bd5eec052 100644 --- a/common/lib/types/message.js +++ b/common/lib/types/message.js @@ -24,11 +24,11 @@ var Message = (function() { encoding: this.encoding }; - /* encode to base64 if we're returning real JSON; + /* encode data to base64 if present and we're returning real JSON; * although msgpack calls toJSON(), we know it is a stringify() * call if it has a non-empty arguments list */ var data = this.data; - if(arguments.length > 0 && BufferUtils.isBuffer(data)) { + if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) { var encoding = this.encoding; result.encoding = encoding ? (encoding + '/base64') : 'base64'; data = BufferUtils.base64Encode(data); @@ -78,10 +78,12 @@ var Message = (function() { }; Message.encode = function(msg, options) { - var data = msg.data, encoding; - if(typeof(data) != 'string' && !BufferUtils.isBuffer(data)) { - msg.data = JSON.stringify(data); - msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json'; + if(msg.data){ + var data = msg.data, encoding; + if(typeof(data) != 'string' && !BufferUtils.isBuffer(data)) { + msg.data = JSON.stringify(data); + msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json'; + } } if(options != null && options.encrypted) Message.encrypt(msg, options); diff --git a/common/lib/types/presencemessage.js b/common/lib/types/presencemessage.js index 5d221e21f0..26a40cf668 100644 --- a/common/lib/types/presencemessage.js +++ b/common/lib/types/presencemessage.js @@ -36,7 +36,7 @@ var PresenceMessage = (function() { * although msgpack calls toJSON(), we know it is a stringify() * call if it passes on the stringify arguments */ var data = this.data; - if(arguments.length > 0 && BufferUtils.isBuffer(data)) { + if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) { var encoding = this.encoding; result.encoding = encoding ? (encoding + '/base64') : 'base64'; data = data.toString('base64');