Skip to content

Commit

Permalink
fix(core): parse and ignore suback packet
Browse files Browse the repository at this point in the history
  • Loading branch information
csimi committed Dec 24, 2019
1 parent 450ef37 commit f7cf44f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ nano.prototype.CONNECT = 1 << 4;
nano.prototype.CONNACK = 2 << 4;
nano.prototype.PUBLISH = 3 << 4;
nano.prototype.SUBSCRIBE = 8 << 4;
nano.prototype.SUBACK = 9 << 4;
nano.prototype.PINGREQ = 12 << 4;
nano.prototype.DISCONNECT = 14 << 4;

Expand Down Expand Up @@ -92,6 +93,9 @@ nano.prototype.parse = function parse (data) {
this.emit('message', topic, payload);
return [nano.prototype.PUBLISH, topic, payload];
}
else if (cmd === nano.prototype.SUBACK) {
return [nano.prototype.SUBACK];
}
else if (cmd === nano.prototype.PINGREQ) {
this.emit('ping');
return [nano.prototype.PINGREQ];
Expand Down
8 changes: 8 additions & 0 deletions test/core/parse.spec.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe('parse', () => {
return expect(spy).to.have.been.calledOnce;
});

it('parses and ignores suback packet', () => {
nano.on('error', spy);

nano.parse(packets.suback);

return expect(spy).to.not.have.been.called;
});

it('emits unknown packet as error', () => {
const data = Buffer.from([1337]);
nano.on('error', spy);
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/packets.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const subscribe = mqtt.generate({
}],
});

const suback = mqtt.generate({
'cmd': 'suback',
'messageId': id,
'granted': [0],
});

const pingreq = mqtt.generate({
'cmd': 'pingreq',
});
Expand All @@ -57,5 +63,6 @@ module.exports = {
publish,
publishRetain,
subscribe,
suback,
pingreq,
};

0 comments on commit f7cf44f

Please sign in to comment.