From 8a257d8335d2784a176aebcccd6a354405b3d123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonza=CC=81lez=20Garci=CC=81a?= Date: Thu, 7 Sep 2023 13:56:14 +0200 Subject: [PATCH 1/7] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 154fc0963..ec1090fb4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mqtt", "description": "A library for the MQTT protocol", - "version": "5.0.4", + "version": "5.0.5", "contributors": [ "Adam Rudd ", "Matteo Collina (https://github.com/mcollina)", From 9edd10845c1fdfd74cb5ccce4017d9dd0e8d16fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonza=CC=81lez=20Garci=CC=81a?= Date: Thu, 7 Sep 2023 13:56:56 +0200 Subject: [PATCH 2/7] expose callback types --- src/lib/client.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/client.ts b/src/lib/client.ts index 06c739f0b..7e4709d72 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -355,6 +355,7 @@ export type ISubscriptionMap = { resubscribe?: boolean } +export { IConnackPacket, IDisconnectPacket, IPublishPacket, Packet } export type OnConnectCallback = (packet: IConnackPacket) => void export type OnDisconnectCallback = (packet: IDisconnectPacket) => void export type ClientSubscribeCallback = ( From 05209fbb281d27b9a77a8c329e0edaf34db4627d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonza=CC=81lez=20Garci=CC=81a?= Date: Thu, 7 Sep 2023 13:57:02 +0200 Subject: [PATCH 3/7] test callback types --- test/abstract_client.ts | 7 ++++--- test/client_mqtt5.ts | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/test/abstract_client.ts b/test/abstract_client.ts index 2c85e3a8d..aef456da8 100644 --- a/test/abstract_client.ts +++ b/test/abstract_client.ts @@ -371,7 +371,8 @@ export default function abstractTest(server, config) { it('should emit connect', function _test(done) { const client = connect() - client.once('connect', () => { + client.once('connect', (packet: mqtt.IConnackPacket) => { + assert.equal(packet.cmd, "connack") client.end(true, (err) => done(err)) }) client.once('error', done) @@ -2372,7 +2373,7 @@ export default function abstractTest(server, config) { // client.subscribe(testPacket.topic) - client.once('message', (topic, message, packet) => { + client.once('message', (topic, message, packet: mqtt.IPublishPacket) => { assert.strictEqual(topic, testPacket.topic) assert.strictEqual(message.toString(), testPacket.payload) assert.strictEqual(packet.cmd, 'publish') @@ -2397,7 +2398,7 @@ export default function abstractTest(server, config) { } client.subscribe(testPacket.topic) - client.on('packetreceive', (packet) => { + client.on('packetreceive', (packet: mqtt.Packet) => { if (packet.cmd === 'publish') { assert.strictEqual(packet.qos, 1) assert.strictEqual(packet.topic, testPacket.topic) diff --git a/test/client_mqtt5.ts b/test/client_mqtt5.ts index ac1708a9b..639b88021 100644 --- a/test/client_mqtt5.ts +++ b/test/client_mqtt5.ts @@ -1048,7 +1048,7 @@ describe('MQTT 5.0', () => { } const client = mqtt.connect(opts) - client.once('disconnect', (disconnectPacket) => { + client.once('disconnect', (disconnectPacket: mqtt.IDisconnectPacket) => { assert.strictEqual(disconnectPacket.reasonCode, 128) client.end(true, (err) => done(err)) }) From ff9882a91254fd2ac5c97ffd379521b4a1b4e0b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonza=CC=81lez=20Garci=CC=81a?= Date: Thu, 7 Sep 2023 13:57:42 +0200 Subject: [PATCH 4/7] change callback types for publish() and unsubscribe() --- src/lib/client.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/lib/client.ts b/src/lib/client.ts index 7e4709d72..cecbbfdd7 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -880,19 +880,19 @@ export default class MqttClient extends TypedEventEmitter + public publishAsync(topic: string, message: string | Buffer): Promise public publishAsync( topic: string, message: string | Buffer, opts?: IClientPublishOptions, - ): Promise + ): Promise public publishAsync( topic: string, message: string | Buffer, opts?: IClientPublishOptions, - ): Promise { + ): Promise { return new Promise((resolve, reject) => { - this.publish(topic, message, opts, (err) => { + this.publish(topic, message, opts, (err, packet) => { if (err) { reject(err) } else { - resolve() + resolve(packet) } }) }) @@ -1347,21 +1347,21 @@ export default class MqttClient extends TypedEventEmitter + public unsubscribeAsync(topic: string | string[]): Promise public unsubscribeAsync( topic: string | string[], opts?: IClientSubscribeOptions, - ): Promise + ): Promise public unsubscribeAsync( topic: string | string[], opts?: IClientSubscribeOptions, - ): Promise { + ): Promise { return new Promise((resolve, reject) => { - this.unsubscribe(topic, opts, (err) => { + this.unsubscribe(topic, opts, (err, packet) => { if (err) { reject(err) } else { - resolve() + resolve(packet) } }) }) From 6fb5be06ad6d596b69f3e8ad0ac14724274ee250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonza=CC=81lez=20Garci=CC=81a?= Date: Thu, 7 Sep 2023 13:57:57 +0200 Subject: [PATCH 5/7] test new publish() and unsubscribe() types --- test/abstract_client.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/test/abstract_client.ts b/test/abstract_client.ts index aef456da8..d549e16df 100644 --- a/test/abstract_client.ts +++ b/test/abstract_client.ts @@ -1079,7 +1079,9 @@ export default function abstractTest(server, config) { const client = connect() client.once('connect', () => { - client.publish('a', 'b', () => { + // callback args can be typed + client.publish('a', 'b', (_, packet?: mqtt.Packet) => { + assert.isUndefined(packet) client.end((err) => done(err)) }) }) @@ -1090,7 +1092,8 @@ export default function abstractTest(server, config) { const opts: IClientPublishOptions = { qos: 1 } client.once('connect', () => { - client.publish('a', 'b', opts, () => { + client.publish('a', 'b', opts, (_, packet?: mqtt.Packet) => { + assert.exists(packet) client.end((err) => done(err)) }) }) @@ -1132,7 +1135,8 @@ export default function abstractTest(server, config) { }) client.once('connect', () => { - client.publish('a', 'b', pubOpts, (err) => { + client.publish('a', 'b', pubOpts, (err, packet?: mqtt.Packet) => { + assert.exists(packet) if (version === 5) { assert.strictEqual(err.code, pubackReasonCode) } else { @@ -1153,7 +1157,8 @@ export default function abstractTest(server, config) { const opts: IClientPublishOptions = { qos: 2 } client.once('connect', () => { - client.publish('a', 'b', opts, () => { + client.publish('a', 'b', opts, (_, packet?: mqtt.Packet) => { + assert.exists(packet) client.end((err) => done(err)) }) }) @@ -1199,7 +1204,8 @@ export default function abstractTest(server, config) { }) client.once('connect', () => { - client.publish('a', 'b', pubOpts, (err) => { + client.publish('a', 'b', pubOpts, (err, packet?: mqtt.Packet) => { + assert.exists(packet) if (version === 5) { assert.strictEqual(err.code, pubrecReasonCode) } else { @@ -1908,7 +1914,9 @@ export default function abstractTest(server, config) { const topic = 'topic' client.once('connect', () => { - client.unsubscribe(topic, () => { + // callback args can be typed + client.unsubscribe(topic, (_, packet?: mqtt.Packet) => { + assert.isUndefined(packet) client.end(true, done) }) }) From 9e483dc6bc7a8139ab6b9b61b3b613aa29b1fba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonza=CC=81lez=20Garci=CC=81a?= Date: Fri, 8 Sep 2023 08:12:58 +0200 Subject: [PATCH 6/7] version umbump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ec1090fb4..154fc0963 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mqtt", "description": "A library for the MQTT protocol", - "version": "5.0.5", + "version": "5.0.4", "contributors": [ "Adam Rudd ", "Matteo Collina (https://github.com/mcollina)", From 0e776c9152407d8ef83ece45629cdff8b751fb0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonza=CC=81lez=20Garci=CC=81a?= Date: Fri, 8 Sep 2023 14:09:09 +0200 Subject: [PATCH 7/7] fix linting problems --- src/lib/client.ts | 9 +++-- test/abstract_client.ts | 75 ++++++++++++++++++++++++----------------- test/client_mqtt5.ts | 11 +++--- 3 files changed, 58 insertions(+), 37 deletions(-) diff --git a/src/lib/client.ts b/src/lib/client.ts index cecbbfdd7..27b9b2b6d 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -977,7 +977,10 @@ export default class MqttClient extends TypedEventEmitter + public publishAsync( + topic: string, + message: string | Buffer, + ): Promise public publishAsync( topic: string, message: string | Buffer, @@ -1347,7 +1350,9 @@ export default class MqttClient extends TypedEventEmitter + public unsubscribeAsync( + topic: string | string[], + ): Promise public unsubscribeAsync( topic: string | string[], opts?: IClientSubscribeOptions, diff --git a/test/abstract_client.ts b/test/abstract_client.ts index d549e16df..5d0325a90 100644 --- a/test/abstract_client.ts +++ b/test/abstract_client.ts @@ -372,7 +372,7 @@ export default function abstractTest(server, config) { it('should emit connect', function _test(done) { const client = connect() client.once('connect', (packet: mqtt.IConnackPacket) => { - assert.equal(packet.cmd, "connack") + assert.equal(packet.cmd, 'connack') client.end(true, (err) => done(err)) }) client.once('error', done) @@ -1135,19 +1135,24 @@ export default function abstractTest(server, config) { }) client.once('connect', () => { - client.publish('a', 'b', pubOpts, (err, packet?: mqtt.Packet) => { - assert.exists(packet) - if (version === 5) { - assert.strictEqual(err.code, pubackReasonCode) - } else { - assert.ifError(err) - } - setImmediate(() => { - client.end(() => { - server2.close(done) + client.publish( + 'a', + 'b', + pubOpts, + (err, packet?: mqtt.Packet) => { + assert.exists(packet) + if (version === 5) { + assert.strictEqual(err.code, pubackReasonCode) + } else { + assert.ifError(err) + } + setImmediate(() => { + client.end(() => { + server2.close(done) + }) }) - }) - }) + }, + ) }) }) }) @@ -1204,19 +1209,24 @@ export default function abstractTest(server, config) { }) client.once('connect', () => { - client.publish('a', 'b', pubOpts, (err, packet?: mqtt.Packet) => { - assert.exists(packet) - if (version === 5) { - assert.strictEqual(err.code, pubrecReasonCode) - } else { - assert.ifError(err) - } - setImmediate(() => { - client.end(true, () => { - server2.close(done) + client.publish( + 'a', + 'b', + pubOpts, + (err, packet?: mqtt.Packet) => { + assert.exists(packet) + if (version === 5) { + assert.strictEqual(err.code, pubrecReasonCode) + } else { + assert.ifError(err) + } + setImmediate(() => { + client.end(true, () => { + server2.close(done) + }) }) - }) - }) + }, + ) }) }) }) @@ -2381,12 +2391,15 @@ export default function abstractTest(server, config) { // client.subscribe(testPacket.topic) - client.once('message', (topic, message, packet: mqtt.IPublishPacket) => { - assert.strictEqual(topic, testPacket.topic) - assert.strictEqual(message.toString(), testPacket.payload) - assert.strictEqual(packet.cmd, 'publish') - client.end(true, done) - }) + client.once( + 'message', + (topic, message, packet: mqtt.IPublishPacket) => { + assert.strictEqual(topic, testPacket.topic) + assert.strictEqual(message.toString(), testPacket.payload) + assert.strictEqual(packet.cmd, 'publish') + client.end(true, done) + }, + ) server.once('client', (serverClient) => { serverClient.on('subscribe', () => { diff --git a/test/client_mqtt5.ts b/test/client_mqtt5.ts index 639b88021..d17c48266 100644 --- a/test/client_mqtt5.ts +++ b/test/client_mqtt5.ts @@ -1048,10 +1048,13 @@ describe('MQTT 5.0', () => { } const client = mqtt.connect(opts) - client.once('disconnect', (disconnectPacket: mqtt.IDisconnectPacket) => { - assert.strictEqual(disconnectPacket.reasonCode, 128) - client.end(true, (err) => done(err)) - }) + client.once( + 'disconnect', + (disconnectPacket: mqtt.IDisconnectPacket) => { + assert.strictEqual(disconnectPacket.reasonCode, 128) + client.end(true, (err) => done(err)) + }, + ) }) it('pubrec handling custom reason code', function test(done) {