Skip to content

Commit

Permalink
Fixed cb accessing code.
Browse files Browse the repository at this point in the history
If `outgoing[mid]` doesn't match, then accessing `outgoing[mid].cb`
causes `Cannot read property` error. So added checking code.

Fixed outgoing assignment at `_onConnect` function.
  • Loading branch information
redboltz committed May 27, 2019
1 parent 9eca8cd commit d779db6
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ MqttClient.prototype.end = function () {
* @example client.removeOutgoingMessage(client.getLastMessageId());
*/
MqttClient.prototype.removeOutgoingMessage = function (mid) {
var cb = this.outgoing[mid].cb
var cb = this.outgoing[mid] ? this.outgoing[mid].cb : null
delete this.outgoing[mid]
this.outgoingStore.del({messageId: mid}, function () {
cb(new Error('Message removed'))
Expand Down Expand Up @@ -978,7 +978,7 @@ MqttClient.prototype._storePacket = function (packet, cb, cbStorePut) {
if (((packet.qos || 0) === 0 && this.queueQoSZero) || packet.cmd !== 'publish') {
this.queue.push({ packet: packet, cb: cb })
} else if (packet.qos > 0) {
cb = this.outgoing[packet.messageId].cb
cb = this.outgoing[packet.messageId] ? this.outgoing[packet.messageId].cb : null
this.outgoingStore.put(packet, function (err) {
if (err) {
return cb && cb(err)
Expand Down Expand Up @@ -1193,7 +1193,7 @@ MqttClient.prototype._handleAck = function (packet) {
var mid = packet.messageId
var type = packet.cmd
var response = null
var cb = this.outgoing[mid].cb
var cb = this.outgoing[mid] ? this.outgoing[mid].cb : null
var that = this
var err

Expand Down Expand Up @@ -1416,14 +1416,17 @@ MqttClient.prototype._onConnect = function (packet) {

// Avoid unnecessary stream read operations when disconnected
if (!that.disconnecting && !that.reconnectTimer) {
cb = that.outgoing[packet.messageId].cb
that.outgoing[packet.messageId].cb = function (err, status) {
// Ensure that the original callback passed in to publish gets invoked
if (cb) {
cb(err, status)
cb = that.outgoing[packet.messageId] ? that.outgoing[packet.messageId].cb : null
that.outgoing[packet.messageId] = {
volatile: false,
cb: function (err, status) {
// Ensure that the original callback passed in to publish gets invoked
if (cb) {
cb(err, status)
}

storeDeliver()
}

storeDeliver()
}
that._packetIdsDuringStoreProcessing[packet.messageId] = true
that._sendPacket(packet)
Expand Down

0 comments on commit d779db6

Please sign in to comment.