mqtt 5.0.0-beta.4 → 5.0.1
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 +113 -120
- package/build/package.json +1 -1
- package/build/src/lib/client.d.ts +37 -83
- package/build/src/lib/client.js +71 -14
- package/build/src/lib/client.js.map +1 -1
- package/build/src/lib/connect/ali.js.map +1 -1
- package/build/src/lib/connect/index.d.ts +4 -0
- package/build/src/lib/connect/index.js +36 -1
- package/build/src/lib/connect/index.js.map +1 -1
- package/build/src/lib/connect/tcp.js.map +1 -1
- package/build/src/lib/connect/tls.js.map +1 -1
- package/build/src/lib/connect/ws.js +1 -1
- package/build/src/lib/connect/ws.js.map +1 -1
- package/build/src/lib/connect/wx.js.map +1 -1
- package/build/src/lib/handlers/publish.js.map +1 -1
- package/build/src/lib/shared.d.ts +5 -4
- package/build/src/lib/unique-message-id-provider.d.ts +3 -2
- package/build/src/lib/unique-message-id-provider.js.map +1 -1
- package/build/src/mqtt.d.ts +4 -2
- package/build/src/mqtt.js +18 -2
- package/build/src/mqtt.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.js +127 -19
- package/dist/mqtt.min.js +4 -4
- package/doc/subscribe.txt +1 -1
- package/package.json +1 -1
- package/src/lib/client.ts +211 -176
- package/src/lib/connect/ali.ts +2 -2
- package/src/lib/connect/index.ts +66 -5
- package/src/lib/connect/tcp.ts +1 -1
- package/src/lib/connect/tls.ts +1 -3
- package/src/lib/connect/ws.ts +4 -5
- package/src/lib/connect/wx.ts +2 -2
- package/src/lib/default-message-id-provider.ts +2 -2
- package/src/lib/handlers/publish.ts +2 -2
- package/src/lib/shared.ts +4 -4
- package/src/lib/unique-message-id-provider.ts +3 -2
- package/src/mqtt.ts +4 -1
package/doc/subscribe.txt
CHANGED
package/package.json
CHANGED
package/src/lib/client.ts
CHANGED
|
@@ -12,7 +12,6 @@ import mqttPacket, {
|
|
|
12
12
|
IUnsubscribePacket,
|
|
13
13
|
Packet,
|
|
14
14
|
QoS,
|
|
15
|
-
UserProperties,
|
|
16
15
|
ISubackPacket,
|
|
17
16
|
IConnectPacket,
|
|
18
17
|
} from 'mqtt-packet'
|
|
@@ -24,7 +23,7 @@ import reInterval from 'reinterval'
|
|
|
24
23
|
import clone from 'rfdc/default'
|
|
25
24
|
import * as validations from './validations'
|
|
26
25
|
import _debug from 'debug'
|
|
27
|
-
import Store from './store'
|
|
26
|
+
import Store, { IStore } from './store'
|
|
28
27
|
import handlePacket from './handlers'
|
|
29
28
|
import { ClientOptions } from 'ws'
|
|
30
29
|
import { ClientRequestArgs } from 'http'
|
|
@@ -116,10 +115,12 @@ export type AckHandler = (
|
|
|
116
115
|
topic: string,
|
|
117
116
|
message: Buffer,
|
|
118
117
|
packet: any,
|
|
119
|
-
cb: (error
|
|
118
|
+
cb: (error: Error | number, code?: number) => void,
|
|
120
119
|
) => void
|
|
121
120
|
|
|
122
121
|
export interface IClientOptions extends ISecureClientOptions {
|
|
122
|
+
/** CLIENT PROPERTIES */
|
|
123
|
+
|
|
123
124
|
/** Encoding to use. Example 'binary' */
|
|
124
125
|
encoding?: BufferEncoding
|
|
125
126
|
/** Set browser buffer size. Default to 512KB */
|
|
@@ -129,13 +130,14 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
129
130
|
/** Used on ali protocol */
|
|
130
131
|
my?: any
|
|
131
132
|
/** Manually call `connect` after creating client instance */
|
|
132
|
-
manualConnect?:
|
|
133
|
+
manualConnect?: boolean
|
|
133
134
|
/** Custom auth packet properties */
|
|
134
135
|
authPacket?: Partial<IAuthPacket>
|
|
135
136
|
/** Disable/Enable writeToStream.cacheNumbers */
|
|
136
137
|
writeCache?: boolean
|
|
137
|
-
|
|
138
|
+
/** Should be set to `host` */
|
|
138
139
|
servername?: string
|
|
140
|
+
/** The default protocol to use when using `servers` and no protocol is specified */
|
|
139
141
|
defaultProtocol?: MqttProtocol
|
|
140
142
|
/** Support clientId passed in the query string of the url */
|
|
141
143
|
query?: Record<string, string>
|
|
@@ -156,26 +158,7 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
156
158
|
|
|
157
159
|
/** Websocket options */
|
|
158
160
|
wsOptions?: ClientOptions | ClientRequestArgs | DuplexOptions
|
|
159
|
-
|
|
160
|
-
* 10 seconds, set to 0 to disable
|
|
161
|
-
*/
|
|
162
|
-
keepalive?: number
|
|
163
|
-
/**
|
|
164
|
-
* 'mqttjs_' + Math.random().toString(16).substr(2, 8)
|
|
165
|
-
*/
|
|
166
|
-
clientId?: string
|
|
167
|
-
/**
|
|
168
|
-
* 'MQTT'
|
|
169
|
-
*/
|
|
170
|
-
protocolId?: string
|
|
171
|
-
/**
|
|
172
|
-
* 4
|
|
173
|
-
*/
|
|
174
|
-
protocolVersion?: number
|
|
175
|
-
/**
|
|
176
|
-
* true, set to false to receive QoS 1 and 2 messages while offline
|
|
177
|
-
*/
|
|
178
|
-
clean?: boolean
|
|
161
|
+
|
|
179
162
|
/**
|
|
180
163
|
* 1000 milliseconds, interval between two reconnections
|
|
181
164
|
*/
|
|
@@ -184,22 +167,15 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
184
167
|
* 30 * 1000 milliseconds, time to wait before a CONNACK is received
|
|
185
168
|
*/
|
|
186
169
|
connectTimeout?: number
|
|
187
|
-
|
|
188
|
-
* the username required by your broker, if any
|
|
189
|
-
*/
|
|
190
|
-
username?: string
|
|
191
|
-
/**
|
|
192
|
-
* the password required by your broker, if any
|
|
193
|
-
*/
|
|
194
|
-
password?: Buffer | string
|
|
170
|
+
|
|
195
171
|
/**
|
|
196
172
|
* a Store for the incoming packets
|
|
197
173
|
*/
|
|
198
|
-
incomingStore?:
|
|
174
|
+
incomingStore?: IStore
|
|
199
175
|
/**
|
|
200
176
|
* a Store for the outgoing packets
|
|
201
177
|
*/
|
|
202
|
-
outgoingStore?:
|
|
178
|
+
outgoingStore?: IStore
|
|
203
179
|
|
|
204
180
|
/** Enable/Disable queue for QoS 0 packets */
|
|
205
181
|
queueQoSZero?: boolean
|
|
@@ -207,10 +183,16 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
207
183
|
/** Custom log function, default uses `debug` */
|
|
208
184
|
log?: (...args: any[]) => void
|
|
209
185
|
|
|
186
|
+
/** automatically use topic alias */
|
|
210
187
|
autoUseTopicAlias?: boolean
|
|
188
|
+
|
|
189
|
+
/** automatically assign topic alias */
|
|
211
190
|
autoAssignTopicAlias?: boolean
|
|
212
191
|
|
|
192
|
+
/** Set to false to disable ping reschedule. When enabled ping messages are rescheduled on each message sent */
|
|
213
193
|
reschedulePings?: boolean
|
|
194
|
+
|
|
195
|
+
/** List of broker servers. On each reconnect try the next server will be used */
|
|
214
196
|
servers?: Array<{
|
|
215
197
|
host: string
|
|
216
198
|
port: number
|
|
@@ -228,65 +210,67 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
228
210
|
* true, set to false to disable re-subscribe functionality
|
|
229
211
|
*/
|
|
230
212
|
resubscribe?: boolean
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
*/
|
|
234
|
-
will?: {
|
|
235
|
-
/**
|
|
236
|
-
* the topic to publish
|
|
237
|
-
*/
|
|
238
|
-
topic: string
|
|
239
|
-
/**
|
|
240
|
-
* the message to publish
|
|
241
|
-
*/
|
|
242
|
-
payload: Buffer | string
|
|
243
|
-
/**
|
|
244
|
-
* the QoS
|
|
245
|
-
*/
|
|
246
|
-
qos: QoS
|
|
247
|
-
/**
|
|
248
|
-
* the retain flag
|
|
249
|
-
*/
|
|
250
|
-
retain: boolean
|
|
251
|
-
/*
|
|
252
|
-
* properies object of will
|
|
253
|
-
* */
|
|
254
|
-
properties?: {
|
|
255
|
-
willDelayInterval?: number
|
|
256
|
-
payloadFormatIndicator?: boolean
|
|
257
|
-
messageExpiryInterval?: number
|
|
258
|
-
contentType?: string
|
|
259
|
-
responseTopic?: string
|
|
260
|
-
correlationData?: Buffer
|
|
261
|
-
userProperties?: UserProperties
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
authPacket?: any
|
|
265
|
-
|
|
266
|
-
/** Prevent to call `connect` in constructor */
|
|
267
|
-
manualConnect?: boolean
|
|
268
|
-
}
|
|
213
|
+
|
|
214
|
+
/** when defined this function will be called to transform the url string generated by MqttClient from provided options */
|
|
269
215
|
transformWsUrl?: (
|
|
270
216
|
url: string,
|
|
271
217
|
options: IClientOptions,
|
|
272
218
|
client: MqttClient,
|
|
273
219
|
) => string
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
receiveMaximum?: number
|
|
277
|
-
maximumPacketSize?: number
|
|
278
|
-
topicAliasMaximum?: number
|
|
279
|
-
requestResponseInformation?: boolean
|
|
280
|
-
requestProblemInformation?: boolean
|
|
281
|
-
userProperties?: UserProperties
|
|
282
|
-
authenticationMethod?: string
|
|
283
|
-
authenticationData?: Buffer
|
|
284
|
-
}
|
|
220
|
+
|
|
221
|
+
/** Custom message id provider */
|
|
285
222
|
messageIdProvider?: IMessageIdProvider
|
|
286
223
|
|
|
224
|
+
/** When using websockets, this is the timeout used when writing to socket. Default 1000 (1s) */
|
|
287
225
|
browserBufferTimeout?: number
|
|
288
226
|
|
|
227
|
+
/**
|
|
228
|
+
* When using websockets, this sets the `objectMode` option.
|
|
229
|
+
* When in objectMode, streams can push Strings and Buffers
|
|
230
|
+
* as well as any other JavaScript object.
|
|
231
|
+
* Another major difference is that when in objectMode,
|
|
232
|
+
* the internal buffering algorithm counts objects rather than bytes.
|
|
233
|
+
* This means if we have a Transform stream with the highWaterMark option set to 5,
|
|
234
|
+
* the stream will only buffer a maximum of 5 objects internally
|
|
235
|
+
*/
|
|
289
236
|
objectMode?: boolean
|
|
237
|
+
|
|
238
|
+
/** CONNECT PACKET PROPERTIES */
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 'mqttjs_' + Math.random().toString(16).substr(2, 8)
|
|
242
|
+
*/
|
|
243
|
+
clientId?: string
|
|
244
|
+
/**
|
|
245
|
+
* 3=MQTT 3.1 4=MQTT 3.1.1 5=MQTT 5.0. Defaults to 4
|
|
246
|
+
*/
|
|
247
|
+
protocolVersion?: IConnectPacket['protocolVersion']
|
|
248
|
+
/**
|
|
249
|
+
* 'MQTT'
|
|
250
|
+
*/
|
|
251
|
+
protocolId?: IConnectPacket['protocolId']
|
|
252
|
+
/**
|
|
253
|
+
* true, set to false to receive QoS 1 and 2 messages while offline
|
|
254
|
+
*/
|
|
255
|
+
clean?: boolean
|
|
256
|
+
/**
|
|
257
|
+
* 10 seconds, set to 0 to disable
|
|
258
|
+
*/
|
|
259
|
+
keepalive?: number
|
|
260
|
+
/**
|
|
261
|
+
* the username required by your broker, if any
|
|
262
|
+
*/
|
|
263
|
+
username?: string
|
|
264
|
+
/**
|
|
265
|
+
* the password required by your broker, if any
|
|
266
|
+
*/
|
|
267
|
+
password?: Buffer | string
|
|
268
|
+
/**
|
|
269
|
+
* a message that will sent by the broker automatically when the client disconnect badly.
|
|
270
|
+
*/
|
|
271
|
+
will?: IConnectPacket['will']
|
|
272
|
+
/** see `connect` packet: https://github.com/mqttjs/mqtt-packet/blob/master/types/index.d.ts#L65 */
|
|
273
|
+
properties?: IConnectPacket['properties']
|
|
290
274
|
}
|
|
291
275
|
|
|
292
276
|
export interface IClientPublishOptions {
|
|
@@ -305,55 +289,13 @@ export interface IClientPublishOptions {
|
|
|
305
289
|
/*
|
|
306
290
|
* MQTT 5.0 properties object
|
|
307
291
|
*/
|
|
308
|
-
properties?:
|
|
309
|
-
payloadFormatIndicator?: boolean
|
|
310
|
-
messageExpiryInterval?: number
|
|
311
|
-
topicAlias?: number
|
|
312
|
-
responseTopic?: string
|
|
313
|
-
correlationData?: Buffer
|
|
314
|
-
userProperties?: UserProperties
|
|
315
|
-
subscriptionIdentifier?: number
|
|
316
|
-
contentType?: string
|
|
317
|
-
}
|
|
292
|
+
properties?: IPublishPacket['properties']
|
|
318
293
|
/**
|
|
319
294
|
* callback called when message is put into `outgoingStore`
|
|
320
295
|
*/
|
|
321
296
|
cbStorePut?: StorePutCallback
|
|
322
297
|
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* the QoS
|
|
326
|
-
*/
|
|
327
|
-
qos: QoS
|
|
328
|
-
/*
|
|
329
|
-
* no local flag
|
|
330
|
-
* */
|
|
331
|
-
nl?: boolean
|
|
332
|
-
/*
|
|
333
|
-
* Retain As Published flag
|
|
334
|
-
* */
|
|
335
|
-
rap?: boolean
|
|
336
|
-
/*
|
|
337
|
-
* Retain Handling option
|
|
338
|
-
* */
|
|
339
|
-
rh?: number
|
|
340
|
-
/*
|
|
341
|
-
* MQTT 5.0 properies object of subscribe
|
|
342
|
-
* */
|
|
343
|
-
properties?: {
|
|
344
|
-
subscriptionIdentifier?: number
|
|
345
|
-
userProperties?: UserProperties
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
export interface IClientSubscribeProperties {
|
|
349
|
-
/*
|
|
350
|
-
* MQTT 5.0 properies object of subscribe
|
|
351
|
-
* */
|
|
352
|
-
properties?: {
|
|
353
|
-
subscriptionIdentifier?: number
|
|
354
|
-
userProperties?: UserProperties
|
|
355
|
-
}
|
|
356
|
-
}
|
|
298
|
+
|
|
357
299
|
export interface IClientReconnectOptions {
|
|
358
300
|
/**
|
|
359
301
|
* a Store for the incoming packets
|
|
@@ -364,16 +306,18 @@ export interface IClientReconnectOptions {
|
|
|
364
306
|
*/
|
|
365
307
|
outgoingStore?: Store
|
|
366
308
|
}
|
|
309
|
+
export interface IClientSubscribeProperties {
|
|
310
|
+
/*
|
|
311
|
+
* MQTT 5.0 properies object of subscribe
|
|
312
|
+
* */
|
|
313
|
+
properties?: ISubscribePacket['properties']
|
|
314
|
+
}
|
|
367
315
|
|
|
368
|
-
export interface
|
|
369
|
-
/**
|
|
370
|
-
* is a subscribed to topic
|
|
371
|
-
*/
|
|
372
|
-
topic: string
|
|
316
|
+
export interface IClientSubscribeOptions extends IClientSubscribeProperties {
|
|
373
317
|
/**
|
|
374
|
-
*
|
|
318
|
+
* the QoS
|
|
375
319
|
*/
|
|
376
|
-
qos: QoS
|
|
320
|
+
qos: QoS
|
|
377
321
|
/*
|
|
378
322
|
* no local flag
|
|
379
323
|
* */
|
|
@@ -387,41 +331,26 @@ export interface ISubscriptionGrant {
|
|
|
387
331
|
* */
|
|
388
332
|
rh?: number
|
|
389
333
|
}
|
|
390
|
-
export interface ISubscriptionRequest extends
|
|
334
|
+
export interface ISubscriptionRequest extends IClientSubscribeOptions {
|
|
391
335
|
/**
|
|
392
336
|
* is a subscribed to topic
|
|
393
337
|
*/
|
|
394
338
|
topic: string
|
|
395
|
-
/**
|
|
396
|
-
* is the granted qos level on it
|
|
397
|
-
*/
|
|
398
|
-
qos: QoS
|
|
399
|
-
/*
|
|
400
|
-
* no local flag
|
|
401
|
-
* */
|
|
402
|
-
nl?: boolean
|
|
403
|
-
/*
|
|
404
|
-
* Retain As Published flag
|
|
405
|
-
* */
|
|
406
|
-
rap?: boolean
|
|
407
|
-
/*
|
|
408
|
-
* Retain Handling option
|
|
409
|
-
* */
|
|
410
|
-
rh?: number
|
|
411
339
|
}
|
|
412
340
|
|
|
413
|
-
export interface
|
|
414
|
-
qos
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
341
|
+
export interface ISubscriptionGrant
|
|
342
|
+
extends Omit<ISubscriptionRequest, 'qos' | 'properties'> {
|
|
343
|
+
/**
|
|
344
|
+
* is the granted qos level on it, may return 128 on error
|
|
345
|
+
*/
|
|
346
|
+
qos: QoS | 128
|
|
418
347
|
}
|
|
419
348
|
|
|
420
349
|
export type ISubscriptionMap = {
|
|
421
350
|
/**
|
|
422
351
|
* object which has topic names as object keys and as value the options, like {'test1': {qos: 0}, 'test2': {qos: 2}}.
|
|
423
352
|
*/
|
|
424
|
-
[topic: string]:
|
|
353
|
+
[topic: string]: IClientSubscribeOptions
|
|
425
354
|
} & {
|
|
426
355
|
resubscribe?: boolean
|
|
427
356
|
}
|
|
@@ -476,9 +405,9 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
476
405
|
|
|
477
406
|
public reconnecting: boolean
|
|
478
407
|
|
|
479
|
-
public incomingStore:
|
|
408
|
+
public incomingStore: IStore
|
|
480
409
|
|
|
481
|
-
public outgoingStore:
|
|
410
|
+
public outgoingStore: IStore
|
|
482
411
|
|
|
483
412
|
public options: IClientOptions
|
|
484
413
|
|
|
@@ -847,8 +776,26 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
847
776
|
|
|
848
777
|
// Send a connect packet
|
|
849
778
|
this.log('connect: sending packet `connect`')
|
|
850
|
-
|
|
851
|
-
connectPacket
|
|
779
|
+
|
|
780
|
+
const connectPacket: IConnectPacket = {
|
|
781
|
+
cmd: 'connect',
|
|
782
|
+
protocolId: this.options.protocolId,
|
|
783
|
+
protocolVersion: this.options.protocolVersion,
|
|
784
|
+
clean: this.options.clean,
|
|
785
|
+
clientId: this.options.clientId,
|
|
786
|
+
keepalive: this.options.keepalive,
|
|
787
|
+
username: this.options.username,
|
|
788
|
+
password: this.options.password as Buffer,
|
|
789
|
+
properties: this.options.properties,
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
if (this.options.will) {
|
|
793
|
+
connectPacket.will = {
|
|
794
|
+
...this.options.will,
|
|
795
|
+
payload: this.options.will?.payload as Buffer,
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
|
|
852
799
|
if (this.topicAliasRecv) {
|
|
853
800
|
if (!connectPacket.properties) {
|
|
854
801
|
connectPacket.properties = {}
|
|
@@ -950,9 +897,11 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
950
897
|
// .publish(topic, payload, cb);
|
|
951
898
|
if (typeof opts === 'function') {
|
|
952
899
|
callback = opts as DoneCallback
|
|
953
|
-
opts =
|
|
900
|
+
opts = null
|
|
954
901
|
}
|
|
955
902
|
|
|
903
|
+
opts = opts || {}
|
|
904
|
+
|
|
956
905
|
// default opts
|
|
957
906
|
const defaultOpts: IClientPublishOptions = {
|
|
958
907
|
qos: 0,
|
|
@@ -1021,9 +970,32 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
1021
970
|
callback,
|
|
1022
971
|
})
|
|
1023
972
|
}
|
|
973
|
+
|
|
1024
974
|
return this
|
|
1025
975
|
}
|
|
1026
976
|
|
|
977
|
+
public publishAsync(topic: string, message: string | Buffer): Promise<void>
|
|
978
|
+
public publishAsync(
|
|
979
|
+
topic: string,
|
|
980
|
+
message: string | Buffer,
|
|
981
|
+
opts?: IClientPublishOptions,
|
|
982
|
+
): Promise<void>
|
|
983
|
+
public publishAsync(
|
|
984
|
+
topic: string,
|
|
985
|
+
message: string | Buffer,
|
|
986
|
+
opts?: IClientPublishOptions,
|
|
987
|
+
): Promise<void> {
|
|
988
|
+
return new Promise((resolve, reject) => {
|
|
989
|
+
this.publish(topic, message, opts, (err) => {
|
|
990
|
+
if (err) {
|
|
991
|
+
reject(err)
|
|
992
|
+
} else {
|
|
993
|
+
resolve()
|
|
994
|
+
}
|
|
995
|
+
})
|
|
996
|
+
})
|
|
997
|
+
}
|
|
998
|
+
|
|
1027
999
|
/**
|
|
1028
1000
|
* subscribe - subscribe to <topic>
|
|
1029
1001
|
*
|
|
@@ -1199,7 +1171,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
1199
1171
|
const topics = []
|
|
1200
1172
|
subs.forEach((sub) => {
|
|
1201
1173
|
if (this.options.reconnectPeriod > 0) {
|
|
1202
|
-
const topic:
|
|
1174
|
+
const topic: IClientSubscribeOptions = { qos: sub.qos }
|
|
1203
1175
|
if (version === 5) {
|
|
1204
1176
|
topic.nl = sub.nl || false
|
|
1205
1177
|
topic.rap = sub.rap || false
|
|
@@ -1245,6 +1217,28 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
1245
1217
|
return this
|
|
1246
1218
|
}
|
|
1247
1219
|
|
|
1220
|
+
public subscribeAsync(
|
|
1221
|
+
topicObject: string | string[] | ISubscriptionMap,
|
|
1222
|
+
): Promise<ISubscriptionGrant[]>
|
|
1223
|
+
public subscribeAsync(
|
|
1224
|
+
topicObject: string | string[] | ISubscriptionMap,
|
|
1225
|
+
opts?: IClientSubscribeOptions | IClientSubscribeProperties,
|
|
1226
|
+
): Promise<ISubscriptionGrant[]>
|
|
1227
|
+
public subscribeAsync(
|
|
1228
|
+
topicObject: string | string[] | ISubscriptionMap,
|
|
1229
|
+
opts?: IClientSubscribeOptions | IClientSubscribeProperties,
|
|
1230
|
+
): Promise<ISubscriptionGrant[]> {
|
|
1231
|
+
return new Promise((resolve, reject) => {
|
|
1232
|
+
this.subscribe(topicObject, opts, (err, granted) => {
|
|
1233
|
+
if (err) {
|
|
1234
|
+
reject(err)
|
|
1235
|
+
} else {
|
|
1236
|
+
resolve(granted)
|
|
1237
|
+
}
|
|
1238
|
+
})
|
|
1239
|
+
})
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1248
1242
|
/**
|
|
1249
1243
|
* unsubscribe - unsubscribe from topic(s)
|
|
1250
1244
|
*
|
|
@@ -1350,6 +1344,26 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
1350
1344
|
return this
|
|
1351
1345
|
}
|
|
1352
1346
|
|
|
1347
|
+
public unsubscribeAsync(topic: string | string[]): Promise<void>
|
|
1348
|
+
public unsubscribeAsync(
|
|
1349
|
+
topic: string | string[],
|
|
1350
|
+
opts?: IClientSubscribeOptions,
|
|
1351
|
+
): Promise<void>
|
|
1352
|
+
public unsubscribeAsync(
|
|
1353
|
+
topic: string | string[],
|
|
1354
|
+
opts?: IClientSubscribeOptions,
|
|
1355
|
+
): Promise<void> {
|
|
1356
|
+
return new Promise((resolve, reject) => {
|
|
1357
|
+
this.unsubscribe(topic, opts, (err) => {
|
|
1358
|
+
if (err) {
|
|
1359
|
+
reject(err)
|
|
1360
|
+
} else {
|
|
1361
|
+
resolve()
|
|
1362
|
+
}
|
|
1363
|
+
})
|
|
1364
|
+
})
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1353
1367
|
/**
|
|
1354
1368
|
* end - close connection
|
|
1355
1369
|
*
|
|
@@ -1377,25 +1391,21 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
1377
1391
|
this.log('end :: (%s)', this.options.clientId)
|
|
1378
1392
|
|
|
1379
1393
|
if (force == null || typeof force !== 'boolean') {
|
|
1380
|
-
cb =
|
|
1394
|
+
cb = cb || (opts as DoneCallback)
|
|
1381
1395
|
opts = force as Partial<IDisconnectPacket>
|
|
1382
1396
|
force = false
|
|
1383
|
-
if (typeof opts !== 'object') {
|
|
1384
|
-
cb = opts
|
|
1385
|
-
opts = null
|
|
1386
|
-
if (typeof cb !== 'function') {
|
|
1387
|
-
cb = this.noop
|
|
1388
|
-
}
|
|
1389
|
-
}
|
|
1390
1397
|
}
|
|
1391
1398
|
|
|
1392
1399
|
if (typeof opts !== 'object') {
|
|
1393
|
-
cb = opts
|
|
1400
|
+
cb = cb || opts
|
|
1394
1401
|
opts = null
|
|
1395
1402
|
}
|
|
1396
1403
|
|
|
1397
1404
|
this.log('end :: cb? %s', !!cb)
|
|
1398
|
-
|
|
1405
|
+
|
|
1406
|
+
if (!cb || typeof cb !== 'function') {
|
|
1407
|
+
cb = this.noop
|
|
1408
|
+
}
|
|
1399
1409
|
|
|
1400
1410
|
const closeStores = () => {
|
|
1401
1411
|
this.log('end :: closeStores: closing incoming and outgoing stores')
|
|
@@ -1467,6 +1477,28 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
1467
1477
|
return this
|
|
1468
1478
|
}
|
|
1469
1479
|
|
|
1480
|
+
public endAsync(): Promise<void>
|
|
1481
|
+
public endAsync(force?: boolean): Promise<void>
|
|
1482
|
+
public endAsync(opts?: Partial<IDisconnectPacket>): Promise<void>
|
|
1483
|
+
public endAsync(
|
|
1484
|
+
force?: boolean,
|
|
1485
|
+
opts?: Partial<IDisconnectPacket>,
|
|
1486
|
+
): Promise<void>
|
|
1487
|
+
public endAsync(
|
|
1488
|
+
force?: boolean | Partial<IDisconnectPacket>,
|
|
1489
|
+
opts?: Partial<IDisconnectPacket>,
|
|
1490
|
+
): Promise<void> {
|
|
1491
|
+
return new Promise((resolve, reject) => {
|
|
1492
|
+
this.end(force as boolean, opts, (err) => {
|
|
1493
|
+
if (err) {
|
|
1494
|
+
reject(err)
|
|
1495
|
+
} else {
|
|
1496
|
+
resolve()
|
|
1497
|
+
}
|
|
1498
|
+
})
|
|
1499
|
+
})
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1470
1502
|
/**
|
|
1471
1503
|
* removeOutgoingMessage - remove a message in outgoing store
|
|
1472
1504
|
* the outgoing callback will be called withe Error('Message removed') if the message is removed
|
|
@@ -1600,7 +1632,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
1600
1632
|
}
|
|
1601
1633
|
}
|
|
1602
1634
|
|
|
1603
|
-
private _checkDisconnecting(callback
|
|
1635
|
+
private _checkDisconnecting(callback?: GenericCallback<any>) {
|
|
1604
1636
|
if (this.disconnecting) {
|
|
1605
1637
|
if (callback && callback !== this.noop) {
|
|
1606
1638
|
callback(new Error('client disconnecting'))
|
|
@@ -2073,8 +2105,11 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
2073
2105
|
const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics)
|
|
2074
2106
|
if (
|
|
2075
2107
|
!this._firstConnection &&
|
|
2108
|
+
// Only resubscribe in case of clean connection or if the server does not have a stored session.
|
|
2109
|
+
// The Session Present flag is available since v3.1.1
|
|
2110
|
+
// https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349254
|
|
2076
2111
|
(this.options.clean ||
|
|
2077
|
-
(this.options.protocolVersion
|
|
2112
|
+
(this.options.protocolVersion >= 4 &&
|
|
2078
2113
|
!this.connackPacket.sessionPresent)) &&
|
|
2079
2114
|
_resubscribeTopicsKeys.length > 0
|
|
2080
2115
|
) {
|
package/src/lib/connect/ali.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Buffer } from 'buffer'
|
|
2
|
-
import {
|
|
2
|
+
import { Transform } from 'readable-stream'
|
|
3
3
|
import duplexify, { Duplexify } from 'duplexify'
|
|
4
4
|
import { StreamBuilder } from '../shared'
|
|
5
5
|
import MqttClient, { IClientOptions } from '../client'
|
|
@@ -122,7 +122,7 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
122
122
|
|
|
123
123
|
bindEventHandler()
|
|
124
124
|
|
|
125
|
-
return stream
|
|
125
|
+
return stream
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
export default buildStream
|