diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index b9fd579d9..fdd3af2d2 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -68,3 +68,12 @@ node -r esbuild-register --test --test-name-pattern="should resend in-flight QoS ``` You can also run tests in watch mode using the `--watch` flag. + +## Lint + +You can run and automatically fix linting issues with + +```sh +npm run lint-fix +``` + diff --git a/src/lib/handlers/ack.ts b/src/lib/handlers/ack.ts index a088b6e01..066d30c6e 100644 --- a/src/lib/handlers/ack.ts +++ b/src/lib/handlers/ack.ts @@ -54,7 +54,7 @@ const handleAck: PacketHandler = (client, packet) => { const type = packet.cmd let response = null const cb = client.outgoing[messageId] ? client.outgoing[messageId].cb : null - let err + let err = null // Checking `!cb` happens to work, but it's not technically "correct". // @@ -117,7 +117,11 @@ const handleAck: PacketHandler = (client, packet) => { client.messageIdProvider.deallocate(messageId) const granted = packet.granted as number[] for (let grantedI = 0; grantedI < granted.length; grantedI++) { - if ((granted[grantedI] & 0x80) !== 0) { + const subackRC = granted[grantedI] + if ((subackRC & 0x80) !== 0) { + err = new Error(`Subscribe error: ${ReasonCodes[subackRC]}`) + err.code = subackRC + // suback with Failure status const topics = client.messageIdToTopic[messageId] if (topics) { @@ -129,7 +133,7 @@ const handleAck: PacketHandler = (client, packet) => { } delete client.messageIdToTopic[messageId] client['_invokeStoreProcessingQueue']() - cb(null, packet) + cb(err, packet) break } case 'unsuback': { diff --git a/test/abstract_client.ts b/test/abstract_client.ts index 33fdc1f97..a3e44b24a 100644 --- a/test/abstract_client.ts +++ b/test/abstract_client.ts @@ -2,7 +2,7 @@ * Testing dependencies */ import { assert } from 'chai' -import sinon, { SinonSpy } from 'sinon' +import sinon from 'sinon' import fs from 'fs' import levelStore from 'mqtt-level-store' import Store from '../src/lib/store' diff --git a/test/client_mqtt5.ts b/test/client_mqtt5.ts index 68d50a3b6..b22340326 100644 --- a/test/client_mqtt5.ts +++ b/test/client_mqtt5.ts @@ -1136,6 +1136,40 @@ describe('MQTT 5.0', () => { }, ) + it('suback handling error codes', function _test(t, done) { + serverThatSendsErrors.listen(ports.PORTAND117) + + serverThatSendsErrors.once('client', (serverClient) => { + serverClient.on('subscribe', (packet) => { + serverClient.suback({ + messageId: packet.messageId, + granted: packet.subscriptions.map((e) => 135), + }) + }) + }) + + const client = mqtt.connect({ + protocolVersion: 5, + port: ports.PORTAND117, + host: 'localhost', + }) + + client.subscribe('$SYS/#', (subErr) => { + client.end(true, (endErr) => { + serverThatSendsErrors.close((err2) => { + if (subErr) { + assert.strictEqual( + subErr.message, + 'Subscribe error: Not authorized', + ) + return done(err2 || endErr) + } + done(new Error('Suback errors do NOT work')) + }) + }) + }) + }) + it( 'server side disconnect', {