Skip to content

Commit

Permalink
Properly check for null values in EventSocket (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamMagaluk authored Nov 2, 2016
1 parent 757af78 commit efe1776
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
58 changes: 30 additions & 28 deletions lib/event_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/test_event_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
})


});

0 comments on commit efe1776

Please sign in to comment.