mqtt 5.12.0 → 5.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +2 -0
  2. package/build/bin/pub.js.map +1 -1
  3. package/build/lib/BufferedDuplex.d.ts +2 -2
  4. package/build/lib/BufferedDuplex.js.map +1 -1
  5. package/build/lib/client.d.ts +9 -8
  6. package/build/lib/client.js +44 -19
  7. package/build/lib/client.js.map +1 -1
  8. package/build/lib/connect/ali.d.ts +1 -1
  9. package/build/lib/connect/ali.js.map +1 -1
  10. package/build/lib/connect/index.d.ts +1 -1
  11. package/build/lib/connect/socks.d.ts +1 -1
  12. package/build/lib/connect/socks.js.map +1 -1
  13. package/build/lib/connect/tcp.d.ts +1 -1
  14. package/build/lib/connect/tls.d.ts +1 -1
  15. package/build/lib/connect/tls.js.map +1 -1
  16. package/build/lib/connect/ws.d.ts +1 -1
  17. package/build/lib/connect/ws.js.map +1 -1
  18. package/build/lib/connect/wx.d.ts +1 -1
  19. package/build/lib/connect/wx.js.map +1 -1
  20. package/build/lib/handlers/ack.d.ts +1 -1
  21. package/build/lib/handlers/ack.js.map +1 -1
  22. package/build/lib/handlers/auth.d.ts +1 -1
  23. package/build/lib/handlers/auth.js.map +1 -1
  24. package/build/lib/handlers/connack.d.ts +1 -1
  25. package/build/lib/handlers/connack.js.map +1 -1
  26. package/build/lib/handlers/index.d.ts +1 -1
  27. package/build/lib/handlers/publish.d.ts +1 -1
  28. package/build/lib/handlers/pubrel.d.ts +1 -1
  29. package/build/lib/handlers/pubrel.js.map +1 -1
  30. package/build/lib/shared.d.ts +5 -1
  31. package/build/lib/shared.js +10 -1
  32. package/build/lib/shared.js.map +1 -1
  33. package/build/lib/store.d.ts +2 -2
  34. package/build/lib/unique-message-id-provider.d.ts +1 -1
  35. package/build/tsconfig.build.tsbuildinfo +1 -1
  36. package/dist/mqtt.esm.js +5 -5
  37. package/dist/mqtt.js +51 -20
  38. package/dist/mqtt.min.js +5 -5
  39. package/package.json +1 -1
  40. package/src/bin/pub.ts +2 -2
  41. package/src/bin/sub.ts +1 -1
  42. package/src/lib/BufferedDuplex.ts +2 -2
  43. package/src/lib/client.ts +88 -41
  44. package/src/lib/connect/ali.ts +3 -2
  45. package/src/lib/connect/index.ts +4 -4
  46. package/src/lib/connect/socks.ts +4 -4
  47. package/src/lib/connect/tcp.ts +1 -1
  48. package/src/lib/connect/tls.ts +3 -3
  49. package/src/lib/connect/ws.ts +5 -4
  50. package/src/lib/connect/wx.ts +3 -2
  51. package/src/lib/handlers/ack.ts +1 -1
  52. package/src/lib/handlers/auth.ts +2 -2
  53. package/src/lib/handlers/connack.ts +2 -2
  54. package/src/lib/handlers/index.ts +1 -1
  55. package/src/lib/handlers/publish.ts +2 -2
  56. package/src/lib/handlers/pubrel.ts +6 -2
  57. package/src/lib/shared.ts +14 -1
  58. package/src/lib/store.ts +2 -2
  59. package/src/lib/unique-message-id-provider.ts +1 -1
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mqtt",
3
3
  "description": "A library for the MQTT protocol",
4
- "version": "5.12.0",
4
+ "version": "5.13.0",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
7
  "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
package/src/bin/pub.ts CHANGED
@@ -6,10 +6,10 @@ import fs from 'fs'
6
6
  import concat from 'concat-stream'
7
7
  import help from 'help-me'
8
8
 
9
- import minimist, { ParsedArgs } from 'minimist'
9
+ import minimist, { type ParsedArgs } from 'minimist'
10
10
  import split2 from 'split2'
11
11
  import { connect } from '../mqtt'
12
- import { IClientOptions, IClientPublishOptions } from 'src/lib/client'
12
+ import { type IClientOptions, type IClientPublishOptions } from 'src/lib/client'
13
13
  import { pipeline } from 'stream'
14
14
 
15
15
  const helpMe = help({
package/src/bin/sub.ts CHANGED
@@ -5,7 +5,7 @@ import fs from 'fs'
5
5
  import minimist from 'minimist'
6
6
  import help from 'help-me'
7
7
  import { connect } from '../mqtt'
8
- import { IClientOptions } from 'src/lib/client'
8
+ import { type IClientOptions } from 'src/lib/client'
9
9
 
10
10
  const helpMe = help({
11
11
  dir: path.join(__dirname, '../../', 'help'),
@@ -1,6 +1,6 @@
1
- import { Duplex, Transform } from 'readable-stream'
1
+ import { Duplex, type Transform } from 'readable-stream'
2
2
  import { Buffer } from 'buffer'
3
- import { IClientOptions } from './client'
3
+ import { type IClientOptions } from './client'
4
4
 
5
5
  /**
6
6
  * Utils writev function for browser, ensure to write Buffers to socket (convert strings).
package/src/lib/client.ts CHANGED
@@ -3,45 +3,46 @@
3
3
  */
4
4
  import TopicAliasRecv from './topic-alias-recv'
5
5
  import mqttPacket, {
6
- IAuthPacket,
6
+ type IAuthPacket,
7
7
  IConnackPacket,
8
8
  IDisconnectPacket,
9
9
  IPublishPacket,
10
- ISubscribePacket,
11
- ISubscription,
12
- IUnsubscribePacket,
10
+ type ISubscribePacket,
11
+ type ISubscription,
12
+ type IUnsubscribePacket,
13
13
  Packet,
14
- QoS,
15
- ISubackPacket,
16
- IConnectPacket,
14
+ type QoS,
15
+ type ISubackPacket,
16
+ type IConnectPacket,
17
17
  } from 'mqtt-packet'
18
18
  import DefaultMessageIdProvider, {
19
- IMessageIdProvider,
19
+ type IMessageIdProvider,
20
20
  } from './default-message-id-provider'
21
- import { DuplexOptions, Writable } from 'readable-stream'
21
+ import { type DuplexOptions, Writable } from 'readable-stream'
22
22
  import clone from 'rfdc/default'
23
23
  import * as validations from './validations'
24
24
  import _debug from 'debug'
25
- import Store, { IStore } from './store'
25
+ import Store, { type IStore } from './store'
26
26
  import handlePacket from './handlers'
27
- import { ClientOptions } from 'ws'
28
- import { ClientRequestArgs } from 'http'
27
+ import type { ClientOptions } from 'ws'
28
+ import { type ClientRequestArgs } from 'http'
29
29
  import {
30
- DoneCallback,
31
- ErrorWithReasonCode,
32
- GenericCallback,
33
- IStream,
30
+ type DoneCallback,
31
+ type ErrorWithReasonCode,
32
+ ErrorWithSubackPacket,
33
+ type GenericCallback,
34
+ type IStream,
34
35
  MQTTJS_VERSION,
35
- StreamBuilder,
36
- TimerVariant,
37
- VoidCallback,
36
+ type StreamBuilder,
37
+ type TimerVariant,
38
+ type VoidCallback,
38
39
  nextTick,
39
40
  } from './shared'
40
- import TopicAliasSend from './topic-alias-send'
41
+ import type TopicAliasSend from './topic-alias-send'
41
42
  import { TypedEventEmitter } from './TypedEmitter'
42
43
  import KeepaliveManager from './KeepaliveManager'
43
44
  import isBrowser, { isWebWorker } from './is-browser'
44
- import { Timer } from './get-timer'
45
+ import { type Timer } from './get-timer'
45
46
 
46
47
  const setImmediate =
47
48
  globalThis.setImmediate ||
@@ -61,6 +62,7 @@ const defaultConnectOptions: IClientOptions = {
61
62
  connectTimeout: 30 * 1000,
62
63
  clean: true,
63
64
  resubscribe: true,
65
+ subscribeBatchSize: null,
64
66
  writeCache: true,
65
67
  timerVariant: 'auto',
66
68
  }
@@ -220,6 +222,14 @@ export interface IClientOptions extends ISecureClientOptions {
220
222
  */
221
223
  resubscribe?: boolean
222
224
 
225
+ /**
226
+ * Maximum number of topics to include in a single SUBSCRIBE packet.
227
+ * When subscribing to more topics than this, the client will automatically
228
+ * split them into batches of this size.
229
+ * This is useful on AWS IoT Core, which limits each SUBSCRIBE packet to 8 topics.
230
+ */
231
+ subscribeBatchSize?: number
232
+
223
233
  /** when defined this function will be called to transform the url string generated by MqttClient from provided options */
224
234
  transformWsUrl?: (
225
235
  url: string,
@@ -1209,16 +1219,13 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
1209
1219
  return this
1210
1220
  }
1211
1221
 
1212
- const subscribeProc = () => {
1213
- const messageId = this._nextId()
1214
- if (messageId === null) {
1215
- this.log('No messageId left')
1216
- return false
1217
- }
1218
-
1222
+ const subscribeChunkedSubs = (
1223
+ chunkedSubs: ISubscriptionRequest[],
1224
+ messageId: number,
1225
+ ): Promise<ISubackPacket> => {
1219
1226
  const packet: ISubscribePacket = {
1220
1227
  cmd: 'subscribe',
1221
- subscriptions: subs,
1228
+ subscriptions: chunkedSubs,
1222
1229
  // qos: 1,
1223
1230
  // retain: false,
1224
1231
  // dup: false,
@@ -1233,7 +1240,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
1233
1240
  if (this.options.resubscribe) {
1234
1241
  this.log('subscribe :: resubscribe true')
1235
1242
  const topics = []
1236
- subs.forEach((sub) => {
1243
+ chunkedSubs.forEach((sub) => {
1237
1244
  if (this.options.reconnectPeriod > 0) {
1238
1245
  const topic: IClientSubscribeOptions = { qos: sub.qos }
1239
1246
  if (version === 5) {
@@ -1249,21 +1256,61 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
1249
1256
  this.messageIdToTopic[packet.messageId] = topics
1250
1257
  }
1251
1258
 
1252
- this.outgoing[packet.messageId] = {
1253
- volatile: true,
1254
- cb(err, packet2: ISubackPacket) {
1255
- if (!err) {
1256
- const { granted } = packet2
1257
- for (let i = 0; i < granted.length; i += 1) {
1258
- subs[i].qos = granted[i] as QoS
1259
+ const promise = new Promise<ISubackPacket>((resolve, reject) => {
1260
+ this.outgoing[packet.messageId] = {
1261
+ volatile: true,
1262
+ cb(err, packet2: ISubackPacket) {
1263
+ if (!err) {
1264
+ const { granted } = packet2
1265
+ for (
1266
+ let grantedI = 0;
1267
+ grantedI < granted.length;
1268
+ grantedI += 1
1269
+ ) {
1270
+ chunkedSubs[grantedI].qos = granted[
1271
+ grantedI
1272
+ ] as QoS
1273
+ }
1259
1274
  }
1260
- }
1261
1275
 
1262
- callback(err, subs, packet2)
1263
- },
1264
- }
1276
+ if (!err) {
1277
+ resolve(packet2)
1278
+ } else {
1279
+ reject(
1280
+ new ErrorWithSubackPacket(err.message, packet2),
1281
+ )
1282
+ }
1283
+ },
1284
+ }
1285
+ })
1265
1286
  this.log('subscribe :: call _sendPacket')
1266
1287
  this._sendPacket(packet)
1288
+ return promise
1289
+ }
1290
+
1291
+ const subscribeProc = () => {
1292
+ const batchSize = this.options.subscribeBatchSize ?? subs.length
1293
+ const subscribePromises: Promise<ISubackPacket>[] = []
1294
+
1295
+ for (let i = 0; i < subs.length; i += batchSize) {
1296
+ const chunkedSubs = subs.slice(i, i + batchSize)
1297
+ const messageId = this._nextId()
1298
+ if (messageId === null) {
1299
+ this.log('No messageId left')
1300
+ return false
1301
+ }
1302
+ subscribePromises.push(
1303
+ subscribeChunkedSubs(chunkedSubs, messageId),
1304
+ )
1305
+ }
1306
+ Promise.all(subscribePromises)
1307
+ .then((packets) => {
1308
+ callback(null, subs, packets.at(-1))
1309
+ })
1310
+ .catch((err: ErrorWithSubackPacket) => {
1311
+ callback(err, subs, err.packet)
1312
+ })
1313
+
1267
1314
  return true
1268
1315
  }
1269
1316
 
@@ -1,7 +1,8 @@
1
1
  import { Buffer } from 'buffer'
2
2
  import { Transform } from 'readable-stream'
3
- import { StreamBuilder } from '../shared'
4
- import MqttClient, { IClientOptions } from '../client'
3
+ import { type StreamBuilder } from '../shared'
4
+ import { type IClientOptions } from '../client'
5
+ import type MqttClient from '../client'
5
6
  import { BufferedDuplex } from '../BufferedDuplex'
6
7
 
7
8
  let my: any
@@ -2,12 +2,12 @@
2
2
  import _debug from 'debug'
3
3
  import url from 'url'
4
4
  import MqttClient, {
5
- IClientOptions,
6
- MqttClientEventCallbacks,
7
- MqttProtocol,
5
+ type IClientOptions,
6
+ type MqttClientEventCallbacks,
7
+ type MqttProtocol,
8
8
  } from '../client'
9
9
  import isBrowser from '../is-browser'
10
- import { StreamBuilder } from '../shared'
10
+ import { type StreamBuilder } from '../shared'
11
11
 
12
12
  // Handling the process.nextTick is not a function error in react-native applications.
13
13
  if (typeof process?.nextTick !== 'function') {
@@ -1,11 +1,11 @@
1
1
  import _debug from 'debug'
2
2
  import { Duplex } from 'stream'
3
- import { SocksClient, SocksProxy } from 'socks'
3
+ import { SocksClient, type SocksProxy } from 'socks'
4
4
  import * as dns from 'dns'
5
- import { SocksProxyType } from 'socks/typings/common/constants'
6
- import { IStream } from '../shared'
5
+ import { type SocksProxyType } from 'socks/typings/common/constants'
6
+ import { type IStream } from '../shared'
7
7
  import { promisify } from 'util'
8
- import { Socket } from 'net'
8
+ import { type Socket } from 'net'
9
9
  import assert from 'assert'
10
10
 
11
11
  const debug = _debug('mqttjs:socks')
@@ -1,4 +1,4 @@
1
- import { StreamBuilder } from '../shared'
1
+ import { type StreamBuilder } from '../shared'
2
2
 
3
3
  import net from 'net'
4
4
  import _debug from 'debug'
@@ -1,8 +1,8 @@
1
- import tls, { TLSSocket } from 'tls'
1
+ import tls, { type TLSSocket } from 'tls'
2
2
  import net from 'net'
3
3
  import _debug from 'debug'
4
- import { StreamBuilder } from '../shared'
5
- import { IClientOptions } from '../client'
4
+ import { type StreamBuilder } from '../shared'
5
+ import { type IClientOptions } from '../client'
6
6
  import openSocks from './socks'
7
7
 
8
8
  const debug = _debug('mqttjs:tls')
@@ -1,10 +1,11 @@
1
- import { StreamBuilder } from '../shared'
1
+ import { type StreamBuilder } from '../shared'
2
2
  import { Buffer } from 'buffer'
3
- import Ws, { ClientOptions } from 'ws'
3
+ import Ws, { type ClientOptions } from 'ws'
4
4
  import _debug from 'debug'
5
- import { DuplexOptions, Transform } from 'readable-stream'
5
+ import { type DuplexOptions, Transform } from 'readable-stream'
6
6
  import isBrowser from '../is-browser'
7
- import MqttClient, { IClientOptions } from '../client'
7
+ import { type IClientOptions } from '../client'
8
+ import type MqttClient from '../client'
8
9
  import { BufferedDuplex, writev } from '../BufferedDuplex'
9
10
 
10
11
  const debug = _debug('mqttjs:ws')
@@ -1,8 +1,9 @@
1
- import { StreamBuilder } from '../shared'
1
+ import { type StreamBuilder } from '../shared'
2
2
 
3
3
  import { Buffer } from 'buffer'
4
4
  import { Transform } from 'readable-stream'
5
- import MqttClient, { IClientOptions } from '../client'
5
+ import { type IClientOptions } from '../client'
6
+ import type MqttClient from '../client'
6
7
  import { BufferedDuplex } from '../BufferedDuplex'
7
8
 
8
9
  /* global wx */
@@ -1,6 +1,6 @@
1
1
  // Other Socket Errors: EADDRINUSE, ECONNRESET, ENOTFOUND, ETIMEDOUT.
2
2
 
3
- import { PacketHandler, ErrorWithReasonCode } from '../shared'
3
+ import { type PacketHandler, ErrorWithReasonCode } from '../shared'
4
4
 
5
5
  export const ReasonCodes = {
6
6
  0: '',
@@ -1,5 +1,5 @@
1
- import { IAuthPacket } from 'mqtt-packet'
2
- import { ErrorWithReasonCode, PacketHandler } from '../shared'
1
+ import { type IAuthPacket } from 'mqtt-packet'
2
+ import { ErrorWithReasonCode, type PacketHandler } from '../shared'
3
3
  import { ReasonCodes } from './ack'
4
4
 
5
5
  const handleAuth: PacketHandler = (
@@ -1,7 +1,7 @@
1
1
  import { ReasonCodes } from './ack'
2
2
  import TopicAliasSend from '../topic-alias-send'
3
- import { ErrorWithReasonCode, PacketHandler } from '../shared'
4
- import { IConnackPacket } from 'mqtt-packet'
3
+ import { ErrorWithReasonCode, type PacketHandler } from '../shared'
4
+ import { type IConnackPacket } from 'mqtt-packet'
5
5
 
6
6
  const handleConnack: PacketHandler = (client, packet: IConnackPacket) => {
7
7
  client.log('_handleConnack')
@@ -3,7 +3,7 @@ import handleAuth from './auth'
3
3
  import handleConnack from './connack'
4
4
  import handleAck from './ack'
5
5
  import handlePubrel from './pubrel'
6
- import { PacketHandler } from '../shared'
6
+ import { type PacketHandler } from '../shared'
7
7
 
8
8
  const handle: PacketHandler = (client, packet, done) => {
9
9
  const { options } = client
@@ -1,5 +1,5 @@
1
- import { IPublishPacket } from 'mqtt-packet'
2
- import { PacketHandler } from '../shared'
1
+ import { type IPublishPacket } from 'mqtt-packet'
2
+ import { type PacketHandler } from '../shared'
3
3
 
4
4
  const validReasonCodes = [0, 16, 128, 131, 135, 144, 145, 151, 153]
5
5
 
@@ -1,5 +1,9 @@
1
- import { IPubcompPacket, IPublishPacket, IPubrelPacket } from 'mqtt-packet'
2
- import { PacketHandler } from '../shared'
1
+ import {
2
+ type IPubcompPacket,
3
+ type IPublishPacket,
4
+ type IPubrelPacket,
5
+ } from 'mqtt-packet'
6
+ import { type PacketHandler } from '../shared'
3
7
 
4
8
  const handlePubrel: PacketHandler = (client, packet: IPubrelPacket, done) => {
5
9
  client.log('handling pubrel packet')
package/src/lib/shared.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Packet } from 'mqtt-packet'
1
+ import type { Packet, ISubackPacket } from 'mqtt-packet'
2
2
  import type { Duplex } from 'stream'
3
3
  import type MqttClient from './client'
4
4
  import type { IClientOptions } from './client'
@@ -42,6 +42,19 @@ export class ErrorWithReasonCode extends Error {
42
42
  }
43
43
  }
44
44
 
45
+ export class ErrorWithSubackPacket extends Error {
46
+ public packet: ISubackPacket
47
+
48
+ public constructor(message: string, packet: ISubackPacket) {
49
+ super(message)
50
+ this.packet = packet
51
+
52
+ // We need to set the prototype explicitly
53
+ Object.setPrototypeOf(this, ErrorWithSubackPacket.prototype)
54
+ Object.getPrototypeOf(this).name = 'ErrorWithSubackPacket'
55
+ }
56
+ }
57
+
45
58
  // eslint-disable-next-line @typescript-eslint/ban-types
46
59
  export type Constructor<T = {}> = new (...args: any[]) => T
47
60
 
package/src/lib/store.ts CHANGED
@@ -2,8 +2,8 @@
2
2
  * Module dependencies
3
3
  */
4
4
  import { Readable } from 'readable-stream'
5
- import { Packet } from 'mqtt-packet'
6
- import { DoneCallback } from './shared'
5
+ import { type Packet } from 'mqtt-packet'
6
+ import { type DoneCallback } from './shared'
7
7
 
8
8
  const streamsOpts = { objectMode: true }
9
9
  const defaultStoreOptions = {
@@ -1,5 +1,5 @@
1
1
  import { NumberAllocator } from 'number-allocator'
2
- import { IMessageIdProvider } from './default-message-id-provider'
2
+ import { type IMessageIdProvider } from './default-message-id-provider'
3
3
 
4
4
  /**
5
5
  * UniqueMessageAllocator constructor