diff --git a/aedes.d.ts b/aedes.d.ts index a33f065f..9c39601b 100644 --- a/aedes.d.ts +++ b/aedes.d.ts @@ -69,6 +69,7 @@ declare namespace aedes { authorizeForward?: AuthorizeForwardHandler published?: PublishedHandler queueLimit?: number + maxClientsIdLength?: number } interface Client extends EventEmitter { id: string diff --git a/aedes.js b/aedes.js index f71e1b38..2f1f46f1 100644 --- a/aedes.js +++ b/aedes.js @@ -28,7 +28,8 @@ const defaultOptions = { published: defaultPublished, trustProxy: false, trustedProxies: [], - queueLimit: 42 + queueLimit: 42, + maxClientsIdLength: 23 } function Aedes (opts) { @@ -44,6 +45,7 @@ function Aedes (opts) { this.counter = 0 this.queueLimit = opts.queueLimit this.connectTimeout = opts.connectTimeout + this.maxClientsIdLength = opts.maxClientsIdLength this.mq = opts.mq || mqemitter(opts) this.handle = function handle (conn, req) { conn.setMaxListeners(opts.concurrency * 2) diff --git a/docs/Aedes.md b/docs/Aedes.md index 76112857..315685f0 100644 --- a/docs/Aedes.md +++ b/docs/Aedes.md @@ -23,7 +23,7 @@ - [aedes.subscribe (topic, deliverfunc, callback)](#aedessubscribe-topic-deliverfunc-callback) - [aedes.unsubscribe (topic, deliverfunc, callback)](#aedesunsubscribe-topic-deliverfunc-callback) - [aedes.publish (packet, callback)](#aedespublish-packet-callback) - - [aedes.close (callback)](#aedesclose-callback) + - [aedes.close ([callback])](#aedesclose-callback) - [Handler: decodeProtocol (client, buffer)](#handler-decodeprotocol-client-buffer) - [Handler: preConnect (client, callback)](#handler-preconnect-client-callback) - [Handler: authenticate (client, username, password, callback)](#handler-authenticate-client-username-password-callback) @@ -39,6 +39,7 @@ - `concurrency` `` maximum number of concurrent messages delivered by `mq`. __Default__: `100` - `persistence` [``](../README.md#persistence) a middleware stores _QoS > 0, retained, will_ packets and subscriptions. __Default__: `aedes-persistence` - `queueLimit` `` maximum number of queued messages before client session is established. If number of queued items exceeds, `connectionError` throws an error `Client queue limit reached`. __Default__: `42` + - `maxClientsIdLength` option to override MQTT 3.1.0 clients Id length limit. __Default__: `23` - `heartbeatInterval` `` an interval in millisconds at which server beats its health signal in `$SYS//heartbeat` topic. __Default__: `60000` - `connectTimeout` `` maximum waiting time in milliseconds waiting for a [`CONNECT`][CONNECT] packet. __Default__: `30000` - Returns `` diff --git a/lib/handlers/connect.js b/lib/handlers/connect.js index b0af2bfc..d53db1ad 100644 --- a/lib/handlers/connect.js +++ b/lib/handlers/connect.js @@ -66,7 +66,7 @@ function init (client, packet, done) { returnCode = 1 } // MQTT 3.1.0 allows <= 23 client id length - if (packet.protocolVersion === 3 && clientId.length > 23) { + if (packet.protocolVersion === 3 && clientId.length > client.broker.maxClientsIdLength) { returnCode = 2 } if (returnCode > 0) { diff --git a/test/connect.js b/test/connect.js index 179f3e16..83b855ff 100644 --- a/test/connect.js +++ b/test/connect.js @@ -304,6 +304,32 @@ test('reject clients with > 23 clientId length in MQTT 3.1.0', function (t) { }) }) +test('connect clients with > 23 clientId length using aedes maxClientsIdLength option in MQTT 3.1.0', function (t) { + t.plan(3) + + const broker = aedes({ maxClientsIdLength: 26 }) + t.tearDown(broker.close.bind(broker)) + + const s = setup(broker) + + s.inStream.write({ + cmd: 'connect', + protocolId: 'MQTT', + protocolVersion: 3, + clean: true, + clientId: 'abcdefghijklmnopqrstuvwxyz', + keepalive: 0 + }) + s.outStream.on('data', function (packet) { + t.equal(packet.cmd, 'connack') + t.equal(packet.returnCode, 0) + t.equal(broker.connectedClients, 1) + }) + broker.on('connectionError', function (client, err) { + t.error(err, 'no error') + }) +}) + test('connect with > 23 clientId length in MQTT 3.1.1', function (t) { t.plan(3)