From efe177664d0021661da2e5272bf7a1bf92bcc69f Mon Sep 17 00:00:00 2001 From: Adam Magaluk Date: Wed, 2 Nov 2016 08:27:30 -0400 Subject: [PATCH] Properly check for null values in EventSocket (#329) --- lib/event_socket.js | 58 ++++++++++++++++++++------------------- test/test_event_socket.js | 21 ++++++++++++++ 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/lib/event_socket.js b/lib/event_socket.js index 0cb793c..8c0db8e 100644 --- a/lib/event_socket.js +++ b/lib/event_socket.js @@ -184,39 +184,41 @@ EventSocket.prototype._unsubscribe = function(subscriptionId, cb) { }; EventSocket.prototype.send = function(topic, data) { - if (!Buffer.isBuffer(data) && typeof data === 'object') { + if (!Buffer.isBuffer(data) && typeof data === 'object' && data !== null) { var tmpData = (this.streamEnabled) ? data.data : data; - if (tmpData['transitions']) { - // format device logs - tmpData.actions = buildDeviceActions(tmpData.properties.id, this.ws._env, this.ws._loader, tmpData.transitions); - delete tmpData.transitions; - if (this.streamEnabled) { - data.data = tmpData; - } else { - data = tmpData; - } - } else if (tmpData['query']) { - // format device queries - tmpData = deviceFormatter({ loader: this.ws._loader, env: this.ws._env, model: tmpData.device }); - if (this.streamEnabled) { - data.data = tmpData; - } else { - data = tmpData; + if (tmpData !== null) { + if (tmpData['transitions']) { + // format device logs + tmpData.actions = buildDeviceActions(tmpData.properties.id, this.ws._env, this.ws._loader, tmpData.transitions); + delete tmpData.transitions; + if (this.streamEnabled) { + data.data = tmpData; + } else { + data = tmpData; + } + } else if (tmpData['query']) { + // format device queries + tmpData = deviceFormatter({ loader: this.ws._loader, env: this.ws._env, model: tmpData.device }); + if (this.streamEnabled) { + data.data = tmpData; + } else { + data = tmpData; + } } - } - // used for _peer/connect _peer/disconnect - if (topic.indexOf('_peer/') === 0 && typeof tmpData.peer === 'object') { - var properties = tmpData.peer.properties(); - if (tmpData.error) { - properties.error = tmpData.error; - } + // used for _peer/connect _peer/disconnect + if (topic.indexOf('_peer/') === 0 && typeof tmpData.peer === 'object') { + var properties = tmpData.peer.properties(); + if (tmpData.error) { + properties.error = tmpData.error; + } - if (this.streamEnabled) { - data.data = properties; - } else { - data = ObjectStream.format(topic, properties); + if (this.streamEnabled) { + data.data = properties; + } else { + data = ObjectStream.format(topic, properties); + } } } diff --git a/test/test_event_socket.js b/test/test_event_socket.js index 511aeca..3fec047 100644 --- a/test/test_event_socket.js +++ b/test/test_event_socket.js @@ -93,4 +93,25 @@ describe('EventSocket', function() { ws.emit('message', new Buffer(JSON.stringify(msg))); }) + it('should not fail when sending null object with streamEnabled=true', function(done) { + var ws = new Ws(); + var client = new EventSocket(ws, 'some-topic', { streamEnabled: true }); + ws.on('onsend', function(data, options, cb) { + assert.equal(data, '{"data":null}'); + done(); + }); + client.send('some/topic', { data: null }); + }) + + it('should not fail when sending null object with streamEnabled=false', function(done) { + var ws = new Ws(); + var client = new EventSocket(ws, 'some-topic', { streamEnabled: false }); + ws.on('onsend', function(data, options, cb) { + assert.equal(data, '{"data":null}'); + done(); + }); + client.send('some/topic', { data: null }); + }) + + });