diff --git a/parser.js b/parser.js index 60b01f4..e99177a 100644 --- a/parser.js +++ b/parser.js @@ -441,10 +441,12 @@ class Parser extends EventEmitter { if (this.settings.protocolVersion === 5) { // response code packet.reasonCode = this._parseByte() - // properies mqtt 5 - const properties = this._parseProperties() - if (Object.getOwnPropertyNames(properties).length) { - packet.properties = properties + if (packet.length > 1) { + // properies mqtt 5 + const properties = this._parseProperties() + if (Object.getOwnPropertyNames(properties).length) { + packet.properties = properties + } } } diff --git a/test.js b/test.js index 8b0dc07..56a731c 100644 --- a/test.js +++ b/test.js @@ -1903,6 +1903,18 @@ testParseGenerate('disconnect MQTT 5', { 28, 0, 4, 116, 101, 115, 116// serverReference ]), { protocolVersion: 5 }) +testParseGenerate('disconnect MQTT 5 with no properties', { + cmd: 'disconnect', + retain: false, + qos: 0, + dup: false, + length: 1, + reasonCode: 0 +}, Buffer.from([ + 224, 1, // Header + 0 // reason code +]), { protocolVersion: 5 }) + testParseGenerate('auth MQTT 5', { cmd: 'auth', retain: false, diff --git a/writeToStream.js b/writeToStream.js index 980294e..27ce05e 100644 --- a/writeToStream.js +++ b/writeToStream.js @@ -712,7 +712,7 @@ function disconnect (packet, stream, opts) { // properies mqtt 5 let propertiesData = null - if (version === 5) { + if (version === 5 && properties) { propertiesData = getPropertiesByMaximumPacketSize(stream, properties, opts, length) if (!propertiesData) { return false } length += propertiesData.length