diff --git a/lib/client.js b/lib/client.js index 07e8de115..653b92932 100644 --- a/lib/client.js +++ b/lib/client.js @@ -502,12 +502,22 @@ MqttClient.prototype.publish = function (topic, message, opts, callback) { if (options.protocolVersion === 5) { packet.properties = opts.properties - if ((!options.properties && packet.properties && packet.properties.topicAlias) || ((opts.properties && options.properties) && - ((opts.properties.topicAlias && options.properties.topicAliasMaximum && opts.properties.topicAlias > options.properties.topicAliasMaximum) || - (!options.properties.topicAliasMaximum && opts.properties.topicAlias)))) { + if ( + (!options.serverProperties && + packet.properties && + packet.properties.topicAlias) || + (opts.properties && + options.serverProperties && + ((opts.properties.topicAlias && + options.serverProperties.topicAliasMaximum && + opts.properties.topicAlias > + options.serverProperties.topicAliasMaximum) || + (!options.serverProperties.topicAliasMaximum && + opts.properties.topicAlias))) + ) { /* if we are don`t setup topic alias or - topic alias maximum less than topic alias or + server's topic alias maximum less than topic alias or server don`t give topic alias maximum, we are removing topic alias from packet */ @@ -1170,16 +1180,22 @@ MqttClient.prototype._handleConnack = function (packet) { if (packet.properties) { if (packet.properties.topicAliasMaximum) { - if (!options.properties) { options.properties = {} } - options.properties.topicAliasMaximum = packet.properties.topicAliasMaximum + if (!options.serverProperties) { + options.serverProperties = {} + } + options.serverProperties.topicAliasMaximum = + packet.properties.topicAliasMaximum } if (packet.properties.serverKeepAlive && options.keepalive) { options.keepalive = packet.properties.serverKeepAlive this._shiftPingInterval() } if (packet.properties.maximumPacketSize) { - if (!options.properties) { options.properties = {} } - options.properties.maximumPacketSize = packet.properties.maximumPacketSize + if (!options.serverProperties) { + options.serverProperties = {} + } + options.serverProperties.maximumPacketSize = + packet.properties.maximumPacketSize } } diff --git a/test/client_mqtt5.js b/test/client_mqtt5.js index 2535fd323..eb74551d5 100644 --- a/test/client_mqtt5.js +++ b/test/client_mqtt5.js @@ -17,23 +17,32 @@ describe('MQTT 5.0', function () { var topicAliasTests = [ {properties: {}, name: 'should allow any topicAlias when no topicAliasMaximum provided in settings'}, - {properties: { topicAliasMaximum: 15 }, name: 'should not allow topicAlias > topicAliasMaximum when topicAliasMaximum provided in settings'} + {properties: { topicAliasMaximum: 15 }, name: 'should not allow topicAlias > serverTopicAliasMaximum when TopicAliasMaximum provided in CONNACK'} ] topicAliasTests.forEach(function (test) { it(test.name, function (done) { + var server215 = new MqttServer(function (serverClient) { + serverClient.on('connect', function (packet) { + serverClient.connack({ + reasonCode: 0, + properties: test.properties + }) + }) + }).listen(ports.PORTAND215) this.timeout(15000) - server.once('client', function (serverClient) { + server215.once('client', function (serverClient) { serverClient.on('publish', function (packet) { if (packet.properties && packet.properties.topicAlias) { done(new Error('Packet should not have topicAlias')) return false } else { + server215.close() serverClient.end(done) } }) }) - var opts = {host: 'localhost', port: ports.PORTAND115, protocolVersion: 5, properties: test.properties} + var opts = {host: 'localhost', port: ports.PORTAND215, protocolVersion: 5, properties: {}} var client = mqtt.connect(opts) client.publish('t/h', 'Message', { properties: { topicAlias: 22 } }) }) @@ -105,8 +114,8 @@ describe('MQTT 5.0', function () { var client = mqtt.connect(opts) client.on('connect', function () { assert.strictEqual(client.options.keepalive, 16) - assert.strictEqual(client.options.properties.topicAliasMaximum, 15) - assert.strictEqual(client.options.properties.maximumPacketSize, 95) + assert.strictEqual(client.options.serverProperties.topicAliasMaximum, 15) + assert.strictEqual(client.options.serverProperties.maximumPacketSize, 95) server116.close() client.end(true, done) }) diff --git a/test/helpers/port_list.js b/test/helpers/port_list.js index 46253bf21..4cd3a2649 100644 --- a/test/helpers/port_list.js +++ b/test/helpers/port_list.js @@ -16,6 +16,7 @@ var PORTAND116 = PORT + 116 var PORTAND117 = PORT + 117 var PORTAND118 = PORT + 118 var PORTAND119 = PORT + 119 +var PORTAND215 = PORT + 215 var PORTAND316 = PORT + 316 var PORTAND326 = PORT + 326 var PORTAND327 = PORT + 327 @@ -39,6 +40,7 @@ module.exports = { PORTAND117, PORTAND118, PORTAND119, + PORTAND215, PORTAND316, PORTAND326, PORTAND327 diff --git a/types/lib/client-options.d.ts b/types/lib/client-options.d.ts index fc4779cd7..eb4afe278 100644 --- a/types/lib/client-options.d.ts +++ b/types/lib/client-options.d.ts @@ -113,6 +113,10 @@ export interface IClientOptions extends ISecureClientOptions { userProperties?: Object, authenticationMethod?: string, authenticationData?: Buffer + }, + serverProperties?: { + maximumPacketSize?: number, + topicAliasMaximum?: number, } } export interface ISecureClientOptions {