Repository: moscajs/aedes
Pull request #435 (2020-02-20 07:29:07)

feat: Max clients Id length option for MQTT 3.1.0
#434

github-actions[bot]: ## Pull Request Test Coverage Report for [Build 816ce4f0aff033db147b1b2ff650fa6f753e0cb3-PR-435](https://coveralls.io/builds/28846984)

* **0** of **2**   **(100.0%)**  changed or added relevant lines in **2** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage remained the same at ?**%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/28846984/badge)](https://coveralls.io/builds/28846984) |
| :-- | --: |
| Change from base [Build 9c90d2ef6ba7f4796258cbe16b4d6e0d6ea38e96](https://coveralls.io/builds/28770430): |  0% |
| Covered Lines: |  |
| Relevant Lines: | 0 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/aedes.d.ts b/aedes.d.ts
index a33f065..9c39601 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 f71e1b3..2f1f46f 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 7611285..315685f 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` `<number>` maximum number of concurrent messages delivered by `mq`. __Default__: `100`
   - `persistence` [`<Persistence>`](../README.md#persistence) a middleware stores _QoS > 0, retained, will_ packets and subscriptions. __Default__: `aedes-persistence`
   - `queueLimit` `<number>` 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` `<number>` an interval in millisconds at which server beats its health signal in `$SYS/<aedes.id>/heartbeat` topic. __Default__: `60000`
   - `connectTimeout` `<number>` maximum waiting time in milliseconds waiting for a [`CONNECT`][CONNECT] packet. __Default__: `30000`
 - Returns `<Aedes>`
diff --git a/lib/handlers/connect.js b/lib/handlers/connect.js
index b0af2bf..d53db1a 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) {


Test patch:
diff --git a/test/connect.js b/test/connect.js
index 179f3e1..83b855f 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)