Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions aedes.d.ts
Expand Up @@ -69,6 +69,7 @@ declare namespace aedes {
authorizeForward?: AuthorizeForwardHandler
published?: PublishedHandler
queueLimit?: number
maxClientsIdLength?: number
}
interface Client extends EventEmitter {
id: string
Expand Down
4 changes: 3 additions & 1 deletion aedes.js
Expand Up @@ -28,7 +28,8 @@ const defaultOptions = {
published: defaultPublished,
trustProxy: false,
trustedProxies: [],
queueLimit: 42
queueLimit: 42,
maxClientsIdLength: 23
}

function Aedes (opts) {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion docs/Aedes.md
Expand Up @@ -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)
Expand All @@ -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>`
Expand Down
2 changes: 1 addition & 1 deletion lib/handlers/connect.js
Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions test/connect.js
Expand Up @@ -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)

Expand Down