mqtt 5.12.1 → 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.
- package/README.md +2 -0
- package/build/lib/client.d.ts +1 -0
- package/build/lib/client.js +44 -19
- package/build/lib/client.js.map +1 -1
- package/build/lib/shared.d.ts +5 -1
- package/build/lib/shared.js +10 -1
- package/build/lib/shared.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.esm.js +5 -5
- package/dist/mqtt.js +51 -20
- package/dist/mqtt.min.js +5 -5
- package/package.json +1 -1
- package/src/lib/client.ts +67 -20
- package/src/lib/shared.ts +14 -1
package/package.json
CHANGED
package/src/lib/client.ts
CHANGED
|
@@ -29,6 +29,7 @@ import { type ClientRequestArgs } from 'http'
|
|
|
29
29
|
import {
|
|
30
30
|
type DoneCallback,
|
|
31
31
|
type ErrorWithReasonCode,
|
|
32
|
+
ErrorWithSubackPacket,
|
|
32
33
|
type GenericCallback,
|
|
33
34
|
type IStream,
|
|
34
35
|
MQTTJS_VERSION,
|
|
@@ -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
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|