mqtt 5.0.0-beta.3 → 5.0.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 +43 -4
- package/build/package.json +162 -0
- package/build/src/bin/mqtt.d.ts +2 -0
- package/build/src/bin/mqtt.js +27 -0
- package/build/src/bin/mqtt.js.map +1 -0
- package/build/src/bin/pub.d.ts +2 -0
- package/build/src/bin/pub.js +146 -0
- package/build/src/bin/pub.js.map +1 -0
- package/build/src/bin/sub.d.ts +2 -0
- package/build/src/bin/sub.js +121 -0
- package/build/src/bin/sub.js.map +1 -0
- package/build/src/lib/TypedEmitter.d.ts +11 -0
- package/build/src/lib/TypedEmitter.js +13 -0
- package/build/src/lib/TypedEmitter.js.map +1 -0
- package/build/src/lib/client.d.ts +220 -0
- package/build/src/lib/client.js +1204 -0
- package/build/src/lib/client.js.map +1 -0
- package/build/src/lib/connect/ali.d.ts +3 -0
- package/build/src/lib/connect/ali.js +113 -0
- package/build/src/lib/connect/ali.js.map +1 -0
- package/build/src/lib/connect/index.d.ts +9 -0
- package/build/src/lib/connect/index.js +169 -0
- package/build/src/lib/connect/index.js.map +1 -0
- package/build/src/lib/connect/tcp.d.ts +3 -0
- package/build/src/lib/connect/tcp.js +18 -0
- package/build/src/lib/connect/tcp.js.map +1 -0
- package/build/src/lib/connect/tls.d.ts +3 -0
- package/build/src/lib/connect/tls.js +38 -0
- package/build/src/lib/connect/tls.js.map +1 -0
- package/build/src/lib/connect/ws.d.ts +3 -0
- package/build/src/lib/connect/ws.js +212 -0
- package/build/src/lib/connect/ws.js.map +1 -0
- package/build/src/lib/connect/wx.d.ts +3 -0
- package/build/src/lib/connect/wx.js +117 -0
- package/build/src/lib/connect/wx.js.map +1 -0
- package/build/src/lib/default-message-id-provider.d.ts +16 -0
- package/build/src/lib/default-message-id-provider.js +24 -0
- package/build/src/lib/default-message-id-provider.js.map +1 -0
- package/build/src/lib/handlers/ack.d.ts +48 -0
- package/build/src/lib/handlers/ack.js +129 -0
- package/build/src/lib/handlers/ack.js.map +1 -0
- package/build/src/lib/handlers/auth.d.ts +3 -0
- package/build/src/lib/handlers/auth.js +30 -0
- package/build/src/lib/handlers/auth.js.map +1 -0
- package/build/src/lib/handlers/connack.d.ts +3 -0
- package/build/src/lib/handlers/connack.js +48 -0
- package/build/src/lib/handlers/connack.js.map +1 -0
- package/build/src/lib/handlers/index.d.ts +3 -0
- package/build/src/lib/handlers/index.js +64 -0
- package/build/src/lib/handlers/index.js.map +1 -0
- package/build/src/lib/handlers/publish.d.ts +3 -0
- package/build/src/lib/handlers/publish.js +106 -0
- package/build/src/lib/handlers/publish.js.map +1 -0
- package/build/src/lib/handlers/pubrel.d.ts +3 -0
- package/build/src/lib/handlers/pubrel.js +25 -0
- package/build/src/lib/handlers/pubrel.js.map +1 -0
- package/build/src/lib/is-browser.d.ts +2 -0
- package/build/src/lib/is-browser.js +8 -0
- package/build/src/lib/is-browser.js.map +1 -0
- package/build/src/lib/shared.d.ts +18 -0
- package/build/src/lib/shared.js +35 -0
- package/build/src/lib/shared.js.map +1 -0
- package/build/src/lib/store.d.ts +24 -0
- package/build/src/lib/store.js +80 -0
- package/build/src/lib/store.js.map +1 -0
- package/build/src/lib/topic-alias-recv.d.ts +9 -0
- package/build/src/lib/topic-alias-recv.js +24 -0
- package/build/src/lib/topic-alias-recv.js.map +1 -0
- package/build/src/lib/topic-alias-send.d.ts +13 -0
- package/build/src/lib/topic-alias-send.js +56 -0
- package/build/src/lib/topic-alias-send.js.map +1 -0
- package/build/src/lib/unique-message-id-provider.d.ts +11 -0
- package/build/src/lib/unique-message-id-provider.js +26 -0
- package/build/src/lib/unique-message-id-provider.js.map +1 -0
- package/build/src/lib/validations.d.ts +2 -0
- package/build/src/lib/validations.js +32 -0
- package/build/src/lib/validations.js.map +1 -0
- package/build/src/mqtt.d.ts +10 -0
- package/build/src/mqtt.js +49 -0
- package/build/src/mqtt.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/dist/mqtt.js +2726 -3493
- package/dist/mqtt.min.js +4 -4
- package/doc/subscribe.txt +1 -1
- package/package.json +49 -29
- package/{bin/mqtt.js → src/bin/mqtt.ts} +11 -6
- package/src/bin/pub.ts +171 -0
- package/src/bin/sub.ts +146 -0
- package/src/lib/TypedEmitter.ts +45 -0
- package/{lib/client.js → src/lib/client.ts} +895 -252
- package/{lib/connect/ali.js → src/lib/connect/ali.ts} +13 -11
- package/src/lib/connect/index.ts +241 -0
- package/{lib/connect/tcp.js → src/lib/connect/tcp.ts} +8 -4
- package/{lib/connect/tls.js → src/lib/connect/tls.ts} +10 -8
- package/{lib/connect/ws.js → src/lib/connect/ws.ts} +52 -36
- package/{lib/connect/wx.js → src/lib/connect/wx.ts} +18 -9
- package/src/lib/default-message-id-provider.ts +99 -0
- package/{lib/handlers/ack.js → src/lib/handlers/ack.ts} +17 -19
- package/src/lib/handlers/auth.ts +44 -0
- package/{lib/handlers/connack.js → src/lib/handlers/connack.ts} +15 -11
- package/{lib/handlers/index.js → src/lib/handlers/index.ts} +8 -7
- package/{lib/handlers/publish.js → src/lib/handlers/publish.ts} +73 -57
- package/src/lib/handlers/pubrel.ts +27 -0
- package/{lib/is-browser.js → src/lib/is-browser.ts} +5 -4
- package/src/lib/shared.ts +73 -0
- package/{lib/store.js → src/lib/store.ts} +61 -16
- package/{lib/topic-alias-recv.js → src/lib/topic-alias-recv.ts} +10 -6
- package/{lib/topic-alias-send.js → src/lib/topic-alias-send.ts} +19 -11
- package/{lib/unique-message-id-provider.js → src/lib/unique-message-id-provider.ts} +10 -7
- package/{lib/validations.js → src/lib/validations.ts} +2 -6
- package/src/mqtt.ts +26 -0
- package/bin/pub.js +0 -159
- package/bin/sub.js +0 -141
- package/lib/connect/index.js +0 -175
- package/lib/default-message-id-provider.js +0 -63
- package/lib/handlers/auth.js +0 -34
- package/lib/handlers/pubrel.js +0 -24
- package/mqtt.js +0 -21
- package/types/index.d.ts +0 -30
- package/types/lib/client-options.d.ts +0 -224
- package/types/lib/client.d.ts +0 -273
- package/types/lib/connect/index.d.ts +0 -19
- package/types/lib/default-message-id-provider.d.ts +0 -49
- package/types/lib/message-id-provider.d.ts +0 -40
- package/types/lib/store-options.d.ts +0 -6
- package/types/lib/store.d.ts +0 -46
- package/types/lib/unique-message-id-provider.d.ts +0 -48
|
@@ -1,27 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Module dependencies
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
import TopicAliasRecv from './topic-alias-recv'
|
|
5
|
+
import mqttPacket, {
|
|
6
|
+
IAuthPacket,
|
|
7
|
+
IConnackPacket,
|
|
8
|
+
IDisconnectPacket,
|
|
9
|
+
IPublishPacket,
|
|
10
|
+
ISubscribePacket,
|
|
11
|
+
ISubscription,
|
|
12
|
+
IUnsubscribePacket,
|
|
13
|
+
Packet,
|
|
14
|
+
QoS,
|
|
15
|
+
ISubackPacket,
|
|
16
|
+
IConnectPacket,
|
|
17
|
+
} from 'mqtt-packet'
|
|
18
|
+
import DefaultMessageIdProvider, {
|
|
19
|
+
IMessageIdProvider,
|
|
20
|
+
} from './default-message-id-provider'
|
|
21
|
+
import { DuplexOptions, Writable } from 'readable-stream'
|
|
22
|
+
import reInterval from 'reinterval'
|
|
23
|
+
import clone from 'rfdc/default'
|
|
24
|
+
import * as validations from './validations'
|
|
25
|
+
import _debug from 'debug'
|
|
26
|
+
import Store from './store'
|
|
27
|
+
import handlePacket from './handlers'
|
|
28
|
+
import { ClientOptions } from 'ws'
|
|
29
|
+
import { ClientRequestArgs } from 'http'
|
|
30
|
+
import {
|
|
31
|
+
DoneCallback,
|
|
32
|
+
ErrorWithReasonCode,
|
|
33
|
+
GenericCallback,
|
|
34
|
+
IStream,
|
|
35
|
+
StreamBuilder,
|
|
36
|
+
VoidCallback,
|
|
37
|
+
} from './shared'
|
|
38
|
+
import TopicAliasSend from './topic-alias-send'
|
|
39
|
+
import { TypedEventEmitter } from './TypedEmitter'
|
|
15
40
|
|
|
16
41
|
const nextTick = process
|
|
17
42
|
? process.nextTick
|
|
18
|
-
: (callback) => {
|
|
43
|
+
: (callback: () => void) => {
|
|
19
44
|
setTimeout(callback, 0)
|
|
20
45
|
}
|
|
21
46
|
|
|
22
47
|
const setImmediate =
|
|
23
48
|
global.setImmediate ||
|
|
24
|
-
((...args) => {
|
|
49
|
+
((...args: any[]) => {
|
|
25
50
|
const callback = args.shift()
|
|
26
51
|
nextTick(() => {
|
|
27
52
|
callback(...args)
|
|
@@ -48,6 +73,319 @@ const socketErrors = [
|
|
|
48
73
|
'ETIMEDOUT',
|
|
49
74
|
]
|
|
50
75
|
|
|
76
|
+
export type MqttProtocol =
|
|
77
|
+
| 'wss'
|
|
78
|
+
| 'ws'
|
|
79
|
+
| 'mqtt'
|
|
80
|
+
| 'mqtts'
|
|
81
|
+
| 'tcp'
|
|
82
|
+
| 'ssl'
|
|
83
|
+
| 'wx'
|
|
84
|
+
| 'wxs'
|
|
85
|
+
| 'ali'
|
|
86
|
+
| 'alis'
|
|
87
|
+
|
|
88
|
+
export type StorePutCallback = () => void
|
|
89
|
+
|
|
90
|
+
export interface ISecureClientOptions {
|
|
91
|
+
/**
|
|
92
|
+
* optional private keys in PEM format
|
|
93
|
+
*/
|
|
94
|
+
key?: string | string[] | Buffer | Buffer[] | any[]
|
|
95
|
+
keyPath?: string
|
|
96
|
+
/**
|
|
97
|
+
* optional cert chains in PEM format
|
|
98
|
+
*/
|
|
99
|
+
cert?: string | string[] | Buffer | Buffer[]
|
|
100
|
+
certPath?: string
|
|
101
|
+
/**
|
|
102
|
+
* Optionally override the trusted CA certificates in PEM format
|
|
103
|
+
*/
|
|
104
|
+
ca?: string | string[] | Buffer | Buffer[]
|
|
105
|
+
caPaths?: string | string[]
|
|
106
|
+
|
|
107
|
+
rejectUnauthorized?: boolean
|
|
108
|
+
/**
|
|
109
|
+
* optional alpn's
|
|
110
|
+
*/
|
|
111
|
+
ALPNProtocols?: string[] | Buffer[] | Uint8Array[] | Buffer | Uint8Array
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export type AckHandler = (
|
|
115
|
+
topic: string,
|
|
116
|
+
message: Buffer,
|
|
117
|
+
packet: any,
|
|
118
|
+
cb: (error: Error | number, code?: number) => void,
|
|
119
|
+
) => void
|
|
120
|
+
|
|
121
|
+
export interface IClientOptions extends ISecureClientOptions {
|
|
122
|
+
/** CLIENT PROPERTIES */
|
|
123
|
+
|
|
124
|
+
/** Encoding to use. Example 'binary' */
|
|
125
|
+
encoding?: BufferEncoding
|
|
126
|
+
/** Set browser buffer size. Default to 512KB */
|
|
127
|
+
browserBufferSize?: number
|
|
128
|
+
/** used in ws protocol to set `objectMode` */
|
|
129
|
+
binary?: boolean
|
|
130
|
+
/** Used on ali protocol */
|
|
131
|
+
my?: any
|
|
132
|
+
/** Manually call `connect` after creating client instance */
|
|
133
|
+
manualConnect?: boolean
|
|
134
|
+
/** Custom auth packet properties */
|
|
135
|
+
authPacket?: Partial<IAuthPacket>
|
|
136
|
+
/** Disable/Enable writeToStream.cacheNumbers */
|
|
137
|
+
writeCache?: boolean
|
|
138
|
+
/** Should be set to `host` */
|
|
139
|
+
servername?: string
|
|
140
|
+
/** The default protocol to use when using `servers` and no protocol is specified */
|
|
141
|
+
defaultProtocol?: MqttProtocol
|
|
142
|
+
/** Support clientId passed in the query string of the url */
|
|
143
|
+
query?: Record<string, string>
|
|
144
|
+
/** Auth string in the format <username>:<password> */
|
|
145
|
+
auth?: string
|
|
146
|
+
/** Custom ack handler */
|
|
147
|
+
customHandleAcks?: AckHandler
|
|
148
|
+
/** Broker port */
|
|
149
|
+
port?: number
|
|
150
|
+
/** Broker host. Does NOT include port */
|
|
151
|
+
host?: string
|
|
152
|
+
/** @deprecated use `host instead */
|
|
153
|
+
hostname?: string
|
|
154
|
+
/** Websocket `path` added as suffix */
|
|
155
|
+
path?: string
|
|
156
|
+
/** The `MqttProtocol` to use */
|
|
157
|
+
protocol?: MqttProtocol
|
|
158
|
+
|
|
159
|
+
/** Websocket options */
|
|
160
|
+
wsOptions?: ClientOptions | ClientRequestArgs | DuplexOptions
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 1000 milliseconds, interval between two reconnections
|
|
164
|
+
*/
|
|
165
|
+
reconnectPeriod?: number
|
|
166
|
+
/**
|
|
167
|
+
* 30 * 1000 milliseconds, time to wait before a CONNACK is received
|
|
168
|
+
*/
|
|
169
|
+
connectTimeout?: number
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* a Store for the incoming packets
|
|
173
|
+
*/
|
|
174
|
+
incomingStore?: Store
|
|
175
|
+
/**
|
|
176
|
+
* a Store for the outgoing packets
|
|
177
|
+
*/
|
|
178
|
+
outgoingStore?: Store
|
|
179
|
+
|
|
180
|
+
/** Enable/Disable queue for QoS 0 packets */
|
|
181
|
+
queueQoSZero?: boolean
|
|
182
|
+
|
|
183
|
+
/** Custom log function, default uses `debug` */
|
|
184
|
+
log?: (...args: any[]) => void
|
|
185
|
+
|
|
186
|
+
/** automatically use topic alias */
|
|
187
|
+
autoUseTopicAlias?: boolean
|
|
188
|
+
|
|
189
|
+
/** automatically assign topic alias */
|
|
190
|
+
autoAssignTopicAlias?: boolean
|
|
191
|
+
|
|
192
|
+
/** Set to false to disable ping reschedule. When enabled ping messages are rescheduled on each message sent */
|
|
193
|
+
reschedulePings?: boolean
|
|
194
|
+
|
|
195
|
+
/** List of broker servers. On each reconnect try the next server will be used */
|
|
196
|
+
servers?: Array<{
|
|
197
|
+
host: string
|
|
198
|
+
port: number
|
|
199
|
+
protocol?:
|
|
200
|
+
| 'wss'
|
|
201
|
+
| 'ws'
|
|
202
|
+
| 'mqtt'
|
|
203
|
+
| 'mqtts'
|
|
204
|
+
| 'tcp'
|
|
205
|
+
| 'ssl'
|
|
206
|
+
| 'wx'
|
|
207
|
+
| 'wxs'
|
|
208
|
+
}>
|
|
209
|
+
/**
|
|
210
|
+
* true, set to false to disable re-subscribe functionality
|
|
211
|
+
*/
|
|
212
|
+
resubscribe?: boolean
|
|
213
|
+
|
|
214
|
+
/** when defined this function will be called to transform the url string generated by MqttClient from provided options */
|
|
215
|
+
transformWsUrl?: (
|
|
216
|
+
url: string,
|
|
217
|
+
options: IClientOptions,
|
|
218
|
+
client: MqttClient,
|
|
219
|
+
) => string
|
|
220
|
+
|
|
221
|
+
/** Custom message id provider */
|
|
222
|
+
messageIdProvider?: IMessageIdProvider
|
|
223
|
+
|
|
224
|
+
/** When using websockets, this is the timeout used when writing to socket. Default 1000 (1s) */
|
|
225
|
+
browserBufferTimeout?: number
|
|
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
|
+
*/
|
|
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']
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export interface IClientPublishOptions {
|
|
277
|
+
/**
|
|
278
|
+
* the QoS
|
|
279
|
+
*/
|
|
280
|
+
qos?: QoS
|
|
281
|
+
/**
|
|
282
|
+
* the retain flag
|
|
283
|
+
*/
|
|
284
|
+
retain?: boolean
|
|
285
|
+
/**
|
|
286
|
+
* whether or not mark a message as duplicate
|
|
287
|
+
*/
|
|
288
|
+
dup?: boolean
|
|
289
|
+
/*
|
|
290
|
+
* MQTT 5.0 properties object
|
|
291
|
+
*/
|
|
292
|
+
properties?: IPublishPacket['properties']
|
|
293
|
+
/**
|
|
294
|
+
* callback called when message is put into `outgoingStore`
|
|
295
|
+
*/
|
|
296
|
+
cbStorePut?: StorePutCallback
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export interface IClientReconnectOptions {
|
|
300
|
+
/**
|
|
301
|
+
* a Store for the incoming packets
|
|
302
|
+
*/
|
|
303
|
+
incomingStore?: Store
|
|
304
|
+
/**
|
|
305
|
+
* a Store for the outgoing packets
|
|
306
|
+
*/
|
|
307
|
+
outgoingStore?: Store
|
|
308
|
+
}
|
|
309
|
+
export interface IClientSubscribeProperties {
|
|
310
|
+
/*
|
|
311
|
+
* MQTT 5.0 properies object of subscribe
|
|
312
|
+
* */
|
|
313
|
+
properties?: ISubscribePacket['properties']
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export interface IClientSubscribeOptions extends IClientSubscribeProperties {
|
|
317
|
+
/**
|
|
318
|
+
* the QoS
|
|
319
|
+
*/
|
|
320
|
+
qos: QoS
|
|
321
|
+
/*
|
|
322
|
+
* no local flag
|
|
323
|
+
* */
|
|
324
|
+
nl?: boolean
|
|
325
|
+
/*
|
|
326
|
+
* Retain As Published flag
|
|
327
|
+
* */
|
|
328
|
+
rap?: boolean
|
|
329
|
+
/*
|
|
330
|
+
* Retain Handling option
|
|
331
|
+
* */
|
|
332
|
+
rh?: number
|
|
333
|
+
}
|
|
334
|
+
export interface ISubscriptionRequest extends IClientSubscribeOptions {
|
|
335
|
+
/**
|
|
336
|
+
* is a subscribed to topic
|
|
337
|
+
*/
|
|
338
|
+
topic: string
|
|
339
|
+
}
|
|
340
|
+
|
|
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
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export type ISubscriptionMap = {
|
|
350
|
+
/**
|
|
351
|
+
* object which has topic names as object keys and as value the options, like {'test1': {qos: 0}, 'test2': {qos: 2}}.
|
|
352
|
+
*/
|
|
353
|
+
[topic: string]: IClientSubscribeOptions
|
|
354
|
+
} & {
|
|
355
|
+
resubscribe?: boolean
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export type OnConnectCallback = (packet: IConnackPacket) => void
|
|
359
|
+
export type OnDisconnectCallback = (packet: IDisconnectPacket) => void
|
|
360
|
+
export type ClientSubscribeCallback = (
|
|
361
|
+
err: Error | null,
|
|
362
|
+
granted: ISubscriptionGrant[],
|
|
363
|
+
) => void
|
|
364
|
+
export type OnMessageCallback = (
|
|
365
|
+
topic: string,
|
|
366
|
+
payload: Buffer,
|
|
367
|
+
packet: IPublishPacket,
|
|
368
|
+
) => void
|
|
369
|
+
export type OnPacketCallback = (packet: Packet) => void
|
|
370
|
+
export type OnCloseCallback = () => void
|
|
371
|
+
export type OnErrorCallback = (error: Error | ErrorWithReasonCode) => void
|
|
372
|
+
export type PacketCallback = (error?: Error, packet?: Packet) => any
|
|
373
|
+
export type CloseCallback = (error?: Error) => void
|
|
374
|
+
|
|
375
|
+
export interface MqttClientEventCallbacks {
|
|
376
|
+
connect: OnConnectCallback
|
|
377
|
+
message: OnMessageCallback
|
|
378
|
+
packetsend: OnPacketCallback
|
|
379
|
+
packetreceive: OnPacketCallback
|
|
380
|
+
disconnect: OnDisconnectCallback
|
|
381
|
+
error: OnErrorCallback
|
|
382
|
+
close: OnCloseCallback
|
|
383
|
+
end: VoidCallback
|
|
384
|
+
reconnect: VoidCallback
|
|
385
|
+
offline: VoidCallback
|
|
386
|
+
outgoingEmpty: VoidCallback
|
|
387
|
+
}
|
|
388
|
+
|
|
51
389
|
/**
|
|
52
390
|
* MqttClient constructor
|
|
53
391
|
*
|
|
@@ -55,20 +393,92 @@ const socketErrors = [
|
|
|
55
393
|
* @param {Object} [options] - connection options
|
|
56
394
|
* (see Connection#connect)
|
|
57
395
|
*/
|
|
58
|
-
class MqttClient extends
|
|
59
|
-
|
|
396
|
+
export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> {
|
|
397
|
+
/** Public fields */
|
|
398
|
+
|
|
399
|
+
/** It's true when client is connected to broker */
|
|
400
|
+
public connected: boolean
|
|
401
|
+
|
|
402
|
+
public disconnecting: boolean
|
|
403
|
+
|
|
404
|
+
public disconnected: boolean
|
|
405
|
+
|
|
406
|
+
public reconnecting: boolean
|
|
407
|
+
|
|
408
|
+
public incomingStore: Store
|
|
409
|
+
|
|
410
|
+
public outgoingStore: Store
|
|
411
|
+
|
|
412
|
+
public options: IClientOptions
|
|
413
|
+
|
|
414
|
+
public queueQoSZero: boolean
|
|
415
|
+
|
|
416
|
+
public _reconnectCount: number
|
|
417
|
+
|
|
418
|
+
public log: (...args: any[]) => void
|
|
419
|
+
|
|
420
|
+
public messageIdProvider: IMessageIdProvider
|
|
421
|
+
|
|
422
|
+
public pingResp: boolean
|
|
423
|
+
|
|
424
|
+
public outgoing: Record<
|
|
425
|
+
number,
|
|
426
|
+
{ volatile: boolean; cb: (err: Error, packet?: Packet) => void }
|
|
427
|
+
>
|
|
428
|
+
|
|
429
|
+
public messageIdToTopic: Record<number, string[]>
|
|
430
|
+
|
|
431
|
+
public noop: (error?: any) => void
|
|
432
|
+
|
|
433
|
+
public pingTimer: any
|
|
434
|
+
|
|
435
|
+
public stream: IStream
|
|
436
|
+
|
|
437
|
+
public queue: { packet: Packet; cb: PacketCallback }[]
|
|
438
|
+
|
|
439
|
+
/* Private fields */
|
|
440
|
+
|
|
441
|
+
/** Function used to build the stream */
|
|
442
|
+
private streamBuilder: StreamBuilder
|
|
443
|
+
|
|
444
|
+
private _resubscribeTopics: ISubscriptionMap
|
|
445
|
+
|
|
446
|
+
private connackTimer: NodeJS.Timeout
|
|
447
|
+
|
|
448
|
+
private reconnectTimer: NodeJS.Timeout
|
|
449
|
+
|
|
450
|
+
private _storeProcessing: boolean
|
|
451
|
+
|
|
452
|
+
/** keep a reference of packets that have been successfully processed from outgoing store */
|
|
453
|
+
private _packetIdsDuringStoreProcessing: Record<number, boolean>
|
|
454
|
+
|
|
455
|
+
private _storeProcessingQueue: {
|
|
456
|
+
invoke: () => any
|
|
457
|
+
cbStorePut?: DoneCallback
|
|
458
|
+
callback: GenericCallback<any>
|
|
459
|
+
}[]
|
|
460
|
+
|
|
461
|
+
private _firstConnection: boolean
|
|
462
|
+
|
|
463
|
+
private topicAliasRecv: TopicAliasRecv
|
|
464
|
+
|
|
465
|
+
private topicAliasSend: TopicAliasSend
|
|
466
|
+
|
|
467
|
+
private _deferredReconnect: () => void
|
|
468
|
+
|
|
469
|
+
private connackPacket: IConnackPacket
|
|
470
|
+
|
|
471
|
+
public static defaultId() {
|
|
60
472
|
return `mqttjs_${Math.random().toString(16).substr(2, 8)}`
|
|
61
473
|
}
|
|
62
474
|
|
|
63
|
-
constructor(streamBuilder, options) {
|
|
475
|
+
constructor(streamBuilder: StreamBuilder, options: IClientOptions) {
|
|
64
476
|
super()
|
|
65
477
|
|
|
66
|
-
let k
|
|
67
|
-
|
|
68
478
|
this.options = options || {}
|
|
69
479
|
|
|
70
480
|
// Defaults
|
|
71
|
-
for (k in defaultConnectOptions) {
|
|
481
|
+
for (const k in defaultConnectOptions) {
|
|
72
482
|
if (typeof this.options[k] === 'undefined') {
|
|
73
483
|
this.options[k] = defaultConnectOptions[k]
|
|
74
484
|
} else {
|
|
@@ -76,7 +486,7 @@ class MqttClient extends EventEmitter {
|
|
|
76
486
|
}
|
|
77
487
|
}
|
|
78
488
|
|
|
79
|
-
this.log = this.options.log ||
|
|
489
|
+
this.log = this.options.log || _debug('mqttjs:client')
|
|
80
490
|
this.noop = this._noop.bind(this)
|
|
81
491
|
|
|
82
492
|
this.log('MqttClient :: options.protocol', options.protocol)
|
|
@@ -112,7 +522,7 @@ class MqttClient extends EventEmitter {
|
|
|
112
522
|
options.protocolVersion === 5 && options.customHandleAcks
|
|
113
523
|
? options.customHandleAcks
|
|
114
524
|
: (...args) => {
|
|
115
|
-
args[3](0)
|
|
525
|
+
args[3](null, 0)
|
|
116
526
|
}
|
|
117
527
|
|
|
118
528
|
// Disable pre-generated write cache if requested. Will allocate buffers on-the-fly instead. WARNING: This can affect write performance
|
|
@@ -247,10 +657,47 @@ class MqttClient extends EventEmitter {
|
|
|
247
657
|
}
|
|
248
658
|
}
|
|
249
659
|
|
|
660
|
+
/**
|
|
661
|
+
* @param packet the packet received by the broker
|
|
662
|
+
* @return the auth packet to be returned to the broker
|
|
663
|
+
* @api public
|
|
664
|
+
*/
|
|
665
|
+
public handleAuth(packet: IAuthPacket, callback: PacketCallback) {
|
|
666
|
+
callback()
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* Handle messages with backpressure support, one at a time.
|
|
671
|
+
* Override at will.
|
|
672
|
+
*
|
|
673
|
+
* @param Packet packet the packet
|
|
674
|
+
* @param Function callback call when finished
|
|
675
|
+
* @api public
|
|
676
|
+
*/
|
|
677
|
+
public handleMessage(packet: IPublishPacket, callback: DoneCallback) {
|
|
678
|
+
callback()
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* _nextId
|
|
683
|
+
* @return unsigned int
|
|
684
|
+
*/
|
|
685
|
+
private _nextId() {
|
|
686
|
+
return this.messageIdProvider.allocate()
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* getLastMessageId
|
|
691
|
+
* @return unsigned int
|
|
692
|
+
*/
|
|
693
|
+
public getLastMessageId() {
|
|
694
|
+
return this.messageIdProvider.getLastAllocated()
|
|
695
|
+
}
|
|
696
|
+
|
|
250
697
|
/**
|
|
251
698
|
* Setup the event handlers in the inner stream, sends `connect` and `auth` packets
|
|
252
699
|
*/
|
|
253
|
-
connect() {
|
|
700
|
+
public connect() {
|
|
254
701
|
const writable = new Writable()
|
|
255
702
|
const parser = mqttPacket.parser(this.options)
|
|
256
703
|
let completeParse = null
|
|
@@ -322,15 +769,33 @@ class MqttClient extends EventEmitter {
|
|
|
322
769
|
// Echo stream close
|
|
323
770
|
this.stream.on('close', () => {
|
|
324
771
|
this.log('(%s)stream :: on close', this.options.clientId)
|
|
325
|
-
this._flushVolatile(
|
|
772
|
+
this._flushVolatile()
|
|
326
773
|
this.log('stream: emit close to MqttClient')
|
|
327
774
|
this.emit('close')
|
|
328
775
|
})
|
|
329
776
|
|
|
330
777
|
// Send a connect packet
|
|
331
778
|
this.log('connect: sending packet `connect`')
|
|
332
|
-
|
|
333
|
-
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
|
+
|
|
334
799
|
if (this.topicAliasRecv) {
|
|
335
800
|
if (!connectPacket.properties) {
|
|
336
801
|
connectPacket.properties = {}
|
|
@@ -365,7 +830,7 @@ class MqttClient extends EventEmitter {
|
|
|
365
830
|
this.options.authPacket &&
|
|
366
831
|
typeof this.options.authPacket === 'object'
|
|
367
832
|
) {
|
|
368
|
-
const authPacket = {
|
|
833
|
+
const authPacket: IAuthPacket = {
|
|
369
834
|
cmd: 'auth',
|
|
370
835
|
reasonCode: 0,
|
|
371
836
|
...this.options.authPacket,
|
|
@@ -388,78 +853,6 @@ class MqttClient extends EventEmitter {
|
|
|
388
853
|
return this
|
|
389
854
|
}
|
|
390
855
|
|
|
391
|
-
_flushVolatile(queue) {
|
|
392
|
-
if (queue) {
|
|
393
|
-
this.log(
|
|
394
|
-
'_flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function',
|
|
395
|
-
)
|
|
396
|
-
Object.keys(queue).forEach((messageId) => {
|
|
397
|
-
if (
|
|
398
|
-
queue[messageId].volatile &&
|
|
399
|
-
typeof queue[messageId].cb === 'function'
|
|
400
|
-
) {
|
|
401
|
-
queue[messageId].cb(new Error('Connection closed'))
|
|
402
|
-
delete queue[messageId]
|
|
403
|
-
}
|
|
404
|
-
})
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
_flush(queue) {
|
|
409
|
-
if (queue) {
|
|
410
|
-
this.log('_flush: queue exists? %b', !!queue)
|
|
411
|
-
Object.keys(queue).forEach((messageId) => {
|
|
412
|
-
if (typeof queue[messageId].cb === 'function') {
|
|
413
|
-
queue[messageId].cb(new Error('Connection closed'))
|
|
414
|
-
// This is suspicious. Why do we only delete this if we have a callback?
|
|
415
|
-
// If this is by-design, then adding no as callback would cause this to get deleted unintentionally.
|
|
416
|
-
delete queue[messageId]
|
|
417
|
-
}
|
|
418
|
-
})
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
_removeTopicAliasAndRecoverTopicName(packet) {
|
|
423
|
-
let alias
|
|
424
|
-
if (packet.properties) {
|
|
425
|
-
alias = packet.properties.topicAlias
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
let topic = packet.topic.toString()
|
|
429
|
-
|
|
430
|
-
this.log(
|
|
431
|
-
'_removeTopicAliasAndRecoverTopicName :: alias %d, topic %o',
|
|
432
|
-
alias,
|
|
433
|
-
topic,
|
|
434
|
-
)
|
|
435
|
-
|
|
436
|
-
if (topic.length === 0) {
|
|
437
|
-
// restore topic from alias
|
|
438
|
-
if (typeof alias === 'undefined') {
|
|
439
|
-
return new Error('Unregistered Topic Alias')
|
|
440
|
-
}
|
|
441
|
-
topic = this.topicAliasSend.getTopicByAlias(alias)
|
|
442
|
-
if (typeof topic === 'undefined') {
|
|
443
|
-
return new Error('Unregistered Topic Alias')
|
|
444
|
-
}
|
|
445
|
-
packet.topic = topic
|
|
446
|
-
}
|
|
447
|
-
if (alias) {
|
|
448
|
-
delete packet.properties.topicAlias
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
_checkDisconnecting(callback) {
|
|
453
|
-
if (this.disconnecting) {
|
|
454
|
-
if (callback && callback !== this.noop) {
|
|
455
|
-
callback(new Error('client disconnecting'))
|
|
456
|
-
} else {
|
|
457
|
-
this.emit('error', new Error('client disconnecting'))
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
return this.disconnecting
|
|
461
|
-
}
|
|
462
|
-
|
|
463
856
|
/**
|
|
464
857
|
* publish - publish <message> to <topic>
|
|
465
858
|
*
|
|
@@ -480,49 +873,74 @@ class MqttClient extends EventEmitter {
|
|
|
480
873
|
* client.publish('topic', 'message', {qos: 1, retain: true, dup: true});
|
|
481
874
|
* @example client.publish('topic', 'message', console.log);
|
|
482
875
|
*/
|
|
483
|
-
publish(topic, message
|
|
876
|
+
public publish(topic: string, message: string | Buffer): MqttClient
|
|
877
|
+
public publish(
|
|
878
|
+
topic: string,
|
|
879
|
+
message: string | Buffer,
|
|
880
|
+
callback?: DoneCallback,
|
|
881
|
+
): MqttClient
|
|
882
|
+
public publish(
|
|
883
|
+
topic: string,
|
|
884
|
+
message: string | Buffer,
|
|
885
|
+
opts?: IClientPublishOptions,
|
|
886
|
+
callback?: DoneCallback,
|
|
887
|
+
): MqttClient
|
|
888
|
+
public publish(
|
|
889
|
+
topic: string,
|
|
890
|
+
message: string | Buffer,
|
|
891
|
+
opts?: IClientPublishOptions | DoneCallback,
|
|
892
|
+
callback?: DoneCallback,
|
|
893
|
+
): MqttClient {
|
|
484
894
|
this.log('publish :: message `%s` to topic `%s`', message, topic)
|
|
485
895
|
const { options } = this
|
|
486
896
|
|
|
487
897
|
// .publish(topic, payload, cb);
|
|
488
898
|
if (typeof opts === 'function') {
|
|
489
|
-
callback = opts
|
|
899
|
+
callback = opts as DoneCallback
|
|
490
900
|
opts = null
|
|
491
901
|
}
|
|
492
902
|
|
|
903
|
+
opts = opts || {}
|
|
904
|
+
|
|
493
905
|
// default opts
|
|
494
|
-
const defaultOpts = {
|
|
906
|
+
const defaultOpts: IClientPublishOptions = {
|
|
907
|
+
qos: 0,
|
|
908
|
+
retain: false,
|
|
909
|
+
dup: false,
|
|
910
|
+
}
|
|
495
911
|
opts = { ...defaultOpts, ...opts }
|
|
496
912
|
|
|
913
|
+
const { qos, retain, dup, properties, cbStorePut } = opts
|
|
914
|
+
|
|
497
915
|
if (this._checkDisconnecting(callback)) {
|
|
498
916
|
return this
|
|
499
917
|
}
|
|
500
918
|
|
|
501
919
|
const publishProc = () => {
|
|
502
920
|
let messageId = 0
|
|
503
|
-
if (
|
|
921
|
+
if (qos === 1 || qos === 2) {
|
|
504
922
|
messageId = this._nextId()
|
|
505
923
|
if (messageId === null) {
|
|
506
924
|
this.log('No messageId left')
|
|
507
925
|
return false
|
|
508
926
|
}
|
|
509
927
|
}
|
|
510
|
-
const packet = {
|
|
928
|
+
const packet: IPublishPacket = {
|
|
511
929
|
cmd: 'publish',
|
|
512
930
|
topic,
|
|
513
931
|
payload: message,
|
|
514
|
-
qos
|
|
515
|
-
retain
|
|
932
|
+
qos,
|
|
933
|
+
retain,
|
|
516
934
|
messageId,
|
|
517
|
-
dup
|
|
935
|
+
dup,
|
|
518
936
|
}
|
|
519
937
|
|
|
520
938
|
if (options.protocolVersion === 5) {
|
|
521
|
-
packet.properties =
|
|
939
|
+
packet.properties = properties
|
|
522
940
|
}
|
|
523
941
|
|
|
524
|
-
this.log('publish :: qos',
|
|
525
|
-
switch (
|
|
942
|
+
this.log('publish :: qos', qos)
|
|
943
|
+
switch (qos) {
|
|
526
944
|
case 1:
|
|
527
945
|
case 2:
|
|
528
946
|
// Add to callbacks
|
|
@@ -531,11 +949,11 @@ class MqttClient extends EventEmitter {
|
|
|
531
949
|
cb: callback || this.noop,
|
|
532
950
|
}
|
|
533
951
|
this.log('MqttClient:publish: packet cmd: %s', packet.cmd)
|
|
534
|
-
this._sendPacket(packet, undefined,
|
|
952
|
+
this._sendPacket(packet, undefined, cbStorePut)
|
|
535
953
|
break
|
|
536
954
|
default:
|
|
537
955
|
this.log('MqttClient:publish: packet cmd: %s', packet.cmd)
|
|
538
|
-
this._sendPacket(packet, callback,
|
|
956
|
+
this._sendPacket(packet, callback, cbStorePut)
|
|
539
957
|
break
|
|
540
958
|
}
|
|
541
959
|
return true
|
|
@@ -552,9 +970,32 @@ class MqttClient extends EventEmitter {
|
|
|
552
970
|
callback,
|
|
553
971
|
})
|
|
554
972
|
}
|
|
973
|
+
|
|
555
974
|
return this
|
|
556
975
|
}
|
|
557
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
|
+
|
|
558
999
|
/**
|
|
559
1000
|
* subscribe - subscribe to <topic>
|
|
560
1001
|
*
|
|
@@ -571,26 +1012,56 @@ class MqttClient extends EventEmitter {
|
|
|
571
1012
|
* @example client.subscribe({'topic': {qos: 0}, 'topic2': {qos: 1}}, console.log);
|
|
572
1013
|
* @example client.subscribe('topic', console.log);
|
|
573
1014
|
*/
|
|
574
|
-
subscribe(
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
1015
|
+
public subscribe(
|
|
1016
|
+
topicObject: string | string[] | ISubscriptionMap,
|
|
1017
|
+
): MqttClient
|
|
1018
|
+
public subscribe(
|
|
1019
|
+
topicObject: string | string[] | ISubscriptionMap,
|
|
1020
|
+
callback?: ClientSubscribeCallback,
|
|
1021
|
+
): MqttClient
|
|
1022
|
+
public subscribe(
|
|
1023
|
+
topicObject: string | string[] | ISubscriptionMap,
|
|
1024
|
+
opts?: IClientSubscribeOptions | IClientSubscribeProperties,
|
|
1025
|
+
): MqttClient
|
|
1026
|
+
public subscribe(
|
|
1027
|
+
topicObject: string | string[] | ISubscriptionMap,
|
|
1028
|
+
opts?: IClientSubscribeOptions | IClientSubscribeProperties,
|
|
1029
|
+
callback?: ClientSubscribeCallback,
|
|
1030
|
+
): MqttClient
|
|
1031
|
+
public subscribe(
|
|
1032
|
+
topicObject: string | string[] | ISubscriptionMap,
|
|
1033
|
+
opts?:
|
|
1034
|
+
| IClientSubscribeOptions
|
|
1035
|
+
| IClientSubscribeProperties
|
|
1036
|
+
| ClientSubscribeCallback,
|
|
1037
|
+
callback?: ClientSubscribeCallback,
|
|
1038
|
+
): MqttClient {
|
|
580
1039
|
const version = this.options.protocolVersion
|
|
581
1040
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
if (typeof obj === 'string') {
|
|
585
|
-
obj = [obj]
|
|
1041
|
+
if (typeof opts === 'function') {
|
|
1042
|
+
callback = opts
|
|
586
1043
|
}
|
|
587
1044
|
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
1045
|
+
callback = callback || this.noop
|
|
1046
|
+
|
|
1047
|
+
// force re-subscribe on reconnect. This is only true
|
|
1048
|
+
// when provided `topicObject` is `this._resubscribeTopics`
|
|
1049
|
+
let resubscribe = false
|
|
1050
|
+
let topicsList = []
|
|
1051
|
+
|
|
1052
|
+
if (typeof topicObject === 'string') {
|
|
1053
|
+
topicObject = [topicObject]
|
|
1054
|
+
topicsList = topicObject
|
|
1055
|
+
} else if (Array.isArray(topicObject)) {
|
|
1056
|
+
topicsList = topicObject
|
|
1057
|
+
} else if (typeof topicObject === 'object') {
|
|
1058
|
+
resubscribe = topicObject.resubscribe
|
|
1059
|
+
delete topicObject.resubscribe
|
|
1060
|
+
topicsList = Object.keys(topicObject)
|
|
591
1061
|
}
|
|
592
1062
|
|
|
593
|
-
|
|
1063
|
+
// validate topics
|
|
1064
|
+
const invalidTopic = validations.validateTopics(topicsList)
|
|
594
1065
|
if (invalidTopic !== null) {
|
|
595
1066
|
setImmediate(callback, new Error(`Invalid topic ${invalidTopic}`))
|
|
596
1067
|
return this
|
|
@@ -601,19 +1072,27 @@ class MqttClient extends EventEmitter {
|
|
|
601
1072
|
return this
|
|
602
1073
|
}
|
|
603
1074
|
|
|
604
|
-
const defaultOpts = {
|
|
1075
|
+
const defaultOpts: Partial<IClientSubscribeOptions> = {
|
|
605
1076
|
qos: 0,
|
|
606
1077
|
}
|
|
1078
|
+
|
|
607
1079
|
if (version === 5) {
|
|
608
1080
|
defaultOpts.nl = false
|
|
609
1081
|
defaultOpts.rap = false
|
|
610
1082
|
defaultOpts.rh = 0
|
|
611
1083
|
}
|
|
612
|
-
opts = { ...defaultOpts, ...opts }
|
|
1084
|
+
opts = { ...defaultOpts, ...opts } as IClientSubscribeOptions
|
|
1085
|
+
|
|
1086
|
+
const properties = opts.properties
|
|
1087
|
+
|
|
1088
|
+
const subs: ISubscriptionRequest[] = []
|
|
613
1089
|
|
|
614
|
-
const parseSub = (
|
|
1090
|
+
const parseSub = (
|
|
1091
|
+
topic: string,
|
|
1092
|
+
subOptions?: IClientSubscribeOptions,
|
|
1093
|
+
) => {
|
|
615
1094
|
// subOptions is defined only when providing a subs map, use opts otherwise
|
|
616
|
-
subOptions = subOptions || opts
|
|
1095
|
+
subOptions = (subOptions || opts) as IClientSubscribeOptions
|
|
617
1096
|
if (
|
|
618
1097
|
!Object.prototype.hasOwnProperty.call(
|
|
619
1098
|
this._resubscribeTopics,
|
|
@@ -622,16 +1101,17 @@ class MqttClient extends EventEmitter {
|
|
|
622
1101
|
this._resubscribeTopics[topic].qos < subOptions.qos ||
|
|
623
1102
|
resubscribe
|
|
624
1103
|
) {
|
|
625
|
-
const currentOpts =
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
1104
|
+
const currentOpts: ISubscription & IClientSubscribeProperties =
|
|
1105
|
+
{
|
|
1106
|
+
topic,
|
|
1107
|
+
qos: subOptions.qos,
|
|
1108
|
+
}
|
|
629
1109
|
if (version === 5) {
|
|
630
1110
|
currentOpts.nl = subOptions.nl
|
|
631
1111
|
currentOpts.rap = subOptions.rap
|
|
632
1112
|
currentOpts.rh = subOptions.rh
|
|
633
1113
|
// use opts.properties
|
|
634
|
-
currentOpts.properties =
|
|
1114
|
+
currentOpts.properties = properties
|
|
635
1115
|
}
|
|
636
1116
|
this.log(
|
|
637
1117
|
'subscribe: pushing topic `%s` and qos `%s` to subs list',
|
|
@@ -642,17 +1122,21 @@ class MqttClient extends EventEmitter {
|
|
|
642
1122
|
}
|
|
643
1123
|
}
|
|
644
1124
|
|
|
645
|
-
if (Array.isArray(
|
|
1125
|
+
if (Array.isArray(topicObject)) {
|
|
646
1126
|
// array of topics
|
|
647
|
-
|
|
1127
|
+
topicObject.forEach((topic) => {
|
|
648
1128
|
this.log('subscribe: array topic %s', topic)
|
|
649
1129
|
parseSub(topic)
|
|
650
1130
|
})
|
|
651
1131
|
} else {
|
|
652
1132
|
// object topic --> subOptions (no properties)
|
|
653
|
-
Object.keys(
|
|
654
|
-
this.log(
|
|
655
|
-
|
|
1133
|
+
Object.keys(topicObject).forEach((topic) => {
|
|
1134
|
+
this.log(
|
|
1135
|
+
'subscribe: object topic %s, %o',
|
|
1136
|
+
topic,
|
|
1137
|
+
topicObject[topic],
|
|
1138
|
+
)
|
|
1139
|
+
parseSub(topic, topicObject[topic])
|
|
656
1140
|
})
|
|
657
1141
|
}
|
|
658
1142
|
|
|
@@ -668,17 +1152,17 @@ class MqttClient extends EventEmitter {
|
|
|
668
1152
|
return false
|
|
669
1153
|
}
|
|
670
1154
|
|
|
671
|
-
const packet = {
|
|
1155
|
+
const packet: ISubscribePacket = {
|
|
672
1156
|
cmd: 'subscribe',
|
|
673
1157
|
subscriptions: subs,
|
|
674
|
-
qos: 1,
|
|
675
|
-
retain: false,
|
|
676
|
-
dup: false,
|
|
1158
|
+
// qos: 1,
|
|
1159
|
+
// retain: false,
|
|
1160
|
+
// dup: false,
|
|
677
1161
|
messageId,
|
|
678
1162
|
}
|
|
679
1163
|
|
|
680
|
-
if (
|
|
681
|
-
packet.properties =
|
|
1164
|
+
if (properties) {
|
|
1165
|
+
packet.properties = properties
|
|
682
1166
|
}
|
|
683
1167
|
|
|
684
1168
|
// subscriptions to resubscribe to in case of disconnect
|
|
@@ -687,7 +1171,7 @@ class MqttClient extends EventEmitter {
|
|
|
687
1171
|
const topics = []
|
|
688
1172
|
subs.forEach((sub) => {
|
|
689
1173
|
if (this.options.reconnectPeriod > 0) {
|
|
690
|
-
const topic = { qos: sub.qos }
|
|
1174
|
+
const topic: IClientSubscribeOptions = { qos: sub.qos }
|
|
691
1175
|
if (version === 5) {
|
|
692
1176
|
topic.nl = sub.nl || false
|
|
693
1177
|
topic.rap = sub.rap || false
|
|
@@ -703,11 +1187,11 @@ class MqttClient extends EventEmitter {
|
|
|
703
1187
|
|
|
704
1188
|
this.outgoing[packet.messageId] = {
|
|
705
1189
|
volatile: true,
|
|
706
|
-
cb(err, packet2) {
|
|
1190
|
+
cb(err, packet2: ISubackPacket) {
|
|
707
1191
|
if (!err) {
|
|
708
1192
|
const { granted } = packet2
|
|
709
1193
|
for (let i = 0; i < granted.length; i += 1) {
|
|
710
|
-
subs[i].qos = granted[i]
|
|
1194
|
+
subs[i].qos = granted[i] as QoS
|
|
711
1195
|
}
|
|
712
1196
|
}
|
|
713
1197
|
|
|
@@ -733,6 +1217,28 @@ class MqttClient extends EventEmitter {
|
|
|
733
1217
|
return this
|
|
734
1218
|
}
|
|
735
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
|
+
|
|
736
1242
|
/**
|
|
737
1243
|
* unsubscribe - unsubscribe from topic(s)
|
|
738
1244
|
*
|
|
@@ -745,19 +1251,35 @@ class MqttClient extends EventEmitter {
|
|
|
745
1251
|
* @example client.unsubscribe('topic');
|
|
746
1252
|
* @example client.unsubscribe('topic', console.log);
|
|
747
1253
|
*/
|
|
748
|
-
unsubscribe(
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
1254
|
+
public unsubscribe(topic: string | string[]): MqttClient
|
|
1255
|
+
public unsubscribe(
|
|
1256
|
+
topic: string | string[],
|
|
1257
|
+
opts?: IClientSubscribeOptions,
|
|
1258
|
+
): MqttClient
|
|
1259
|
+
public unsubscribe(
|
|
1260
|
+
topic: string | string[],
|
|
1261
|
+
callback?: PacketCallback,
|
|
1262
|
+
): MqttClient
|
|
1263
|
+
public unsubscribe(
|
|
1264
|
+
topic: string | string[],
|
|
1265
|
+
opts?: IClientSubscribeOptions,
|
|
1266
|
+
callback?: PacketCallback,
|
|
1267
|
+
): MqttClient
|
|
1268
|
+
public unsubscribe(
|
|
1269
|
+
topic: string | string[],
|
|
1270
|
+
opts?: IClientSubscribeOptions | PacketCallback,
|
|
1271
|
+
callback?: PacketCallback,
|
|
1272
|
+
): MqttClient {
|
|
752
1273
|
if (typeof topic === 'string') {
|
|
753
1274
|
topic = [topic]
|
|
754
1275
|
}
|
|
755
1276
|
|
|
756
|
-
if (typeof
|
|
757
|
-
|
|
758
|
-
callback = this.noop
|
|
1277
|
+
if (typeof opts === 'function') {
|
|
1278
|
+
callback = opts
|
|
759
1279
|
}
|
|
760
1280
|
|
|
1281
|
+
callback = callback || this.noop
|
|
1282
|
+
|
|
761
1283
|
const invalidTopic = validations.validateTopics(topic)
|
|
762
1284
|
if (invalidTopic !== null) {
|
|
763
1285
|
setImmediate(callback, new Error(`Invalid topic ${invalidTopic}`))
|
|
@@ -774,10 +1296,11 @@ class MqttClient extends EventEmitter {
|
|
|
774
1296
|
this.log('No messageId left')
|
|
775
1297
|
return false
|
|
776
1298
|
}
|
|
777
|
-
const packet = {
|
|
1299
|
+
const packet: IUnsubscribePacket = {
|
|
778
1300
|
cmd: 'unsubscribe',
|
|
779
|
-
qos: 1,
|
|
1301
|
+
// qos: 1,
|
|
780
1302
|
messageId,
|
|
1303
|
+
unsubscriptions: [],
|
|
781
1304
|
}
|
|
782
1305
|
|
|
783
1306
|
if (typeof topic === 'string') {
|
|
@@ -821,6 +1344,26 @@ class MqttClient extends EventEmitter {
|
|
|
821
1344
|
return this
|
|
822
1345
|
}
|
|
823
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
|
+
|
|
824
1367
|
/**
|
|
825
1368
|
* end - close connection
|
|
826
1369
|
*
|
|
@@ -831,29 +1374,38 @@ class MqttClient extends EventEmitter {
|
|
|
831
1374
|
*
|
|
832
1375
|
* @api public
|
|
833
1376
|
*/
|
|
834
|
-
end(
|
|
1377
|
+
public end(cb?: DoneCallback): MqttClient
|
|
1378
|
+
public end(force?: boolean): MqttClient
|
|
1379
|
+
public end(opts?: Partial<IDisconnectPacket>, cb?: DoneCallback): MqttClient
|
|
1380
|
+
public end(force?: boolean, cb?: DoneCallback): MqttClient
|
|
1381
|
+
public end(
|
|
1382
|
+
force?: boolean,
|
|
1383
|
+
opts?: Partial<IDisconnectPacket>,
|
|
1384
|
+
cb?: DoneCallback,
|
|
1385
|
+
): MqttClient
|
|
1386
|
+
public end(
|
|
1387
|
+
force?: boolean | Partial<IDisconnectPacket> | DoneCallback,
|
|
1388
|
+
opts?: Partial<IDisconnectPacket> | DoneCallback,
|
|
1389
|
+
cb?: DoneCallback,
|
|
1390
|
+
): MqttClient {
|
|
835
1391
|
this.log('end :: (%s)', this.options.clientId)
|
|
836
1392
|
|
|
837
1393
|
if (force == null || typeof force !== 'boolean') {
|
|
838
|
-
cb =
|
|
839
|
-
opts = force
|
|
1394
|
+
cb = cb || (opts as DoneCallback)
|
|
1395
|
+
opts = force as Partial<IDisconnectPacket>
|
|
840
1396
|
force = false
|
|
841
|
-
if (typeof opts !== 'object') {
|
|
842
|
-
cb = opts
|
|
843
|
-
opts = null
|
|
844
|
-
if (typeof cb !== 'function') {
|
|
845
|
-
cb = this.noop
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
1397
|
}
|
|
849
1398
|
|
|
850
1399
|
if (typeof opts !== 'object') {
|
|
851
|
-
cb = opts
|
|
1400
|
+
cb = cb || opts
|
|
852
1401
|
opts = null
|
|
853
1402
|
}
|
|
854
1403
|
|
|
855
1404
|
this.log('end :: cb? %s', !!cb)
|
|
856
|
-
|
|
1405
|
+
|
|
1406
|
+
if (!cb || typeof cb !== 'function') {
|
|
1407
|
+
cb = this.noop
|
|
1408
|
+
}
|
|
857
1409
|
|
|
858
1410
|
const closeStores = () => {
|
|
859
1411
|
this.log('end :: closeStores: closing incoming and outgoing stores')
|
|
@@ -886,7 +1438,7 @@ class MqttClient extends EventEmitter {
|
|
|
886
1438
|
force,
|
|
887
1439
|
)
|
|
888
1440
|
this._cleanUp(
|
|
889
|
-
force,
|
|
1441
|
+
<boolean>force,
|
|
890
1442
|
() => {
|
|
891
1443
|
this.log(
|
|
892
1444
|
'end :: finish :: calling process.nextTick on closeStores',
|
|
@@ -925,6 +1477,28 @@ class MqttClient extends EventEmitter {
|
|
|
925
1477
|
return this
|
|
926
1478
|
}
|
|
927
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
|
+
|
|
928
1502
|
/**
|
|
929
1503
|
* removeOutgoingMessage - remove a message in outgoing store
|
|
930
1504
|
* the outgoing callback will be called withe Error('Message removed') if the message is removed
|
|
@@ -935,7 +1509,7 @@ class MqttClient extends EventEmitter {
|
|
|
935
1509
|
*
|
|
936
1510
|
* @example client.removeOutgoingMessage(client.getLastAllocated());
|
|
937
1511
|
*/
|
|
938
|
-
removeOutgoingMessage(messageId) {
|
|
1512
|
+
public removeOutgoingMessage(messageId: number): MqttClient {
|
|
939
1513
|
if (this.outgoing[messageId]) {
|
|
940
1514
|
const { cb } = this.outgoing[messageId]
|
|
941
1515
|
this._removeOutgoingAndStoreMessage(messageId, () => {
|
|
@@ -956,7 +1530,9 @@ class MqttClient extends EventEmitter {
|
|
|
956
1530
|
*
|
|
957
1531
|
* @api public
|
|
958
1532
|
*/
|
|
959
|
-
reconnect(
|
|
1533
|
+
public reconnect(
|
|
1534
|
+
opts?: Pick<IClientOptions, 'incomingStore' | 'outgoingStore'>,
|
|
1535
|
+
): MqttClient {
|
|
960
1536
|
this.log('client reconnect')
|
|
961
1537
|
const f = () => {
|
|
962
1538
|
if (opts) {
|
|
@@ -982,11 +1558,96 @@ class MqttClient extends EventEmitter {
|
|
|
982
1558
|
return this
|
|
983
1559
|
}
|
|
984
1560
|
|
|
1561
|
+
/**
|
|
1562
|
+
* PRIVATE METHODS
|
|
1563
|
+
* =====================
|
|
1564
|
+
* */
|
|
1565
|
+
|
|
1566
|
+
/**
|
|
1567
|
+
* Flush all outgoing messages marked as `volatile` in `outgoing` queue. Volatile messages
|
|
1568
|
+
* typically are subscription and unsubscription requests.
|
|
1569
|
+
*/
|
|
1570
|
+
private _flushVolatile() {
|
|
1571
|
+
if (this.outgoing) {
|
|
1572
|
+
this.log(
|
|
1573
|
+
'_flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function',
|
|
1574
|
+
)
|
|
1575
|
+
Object.keys(this.outgoing).forEach((messageId) => {
|
|
1576
|
+
if (
|
|
1577
|
+
this.outgoing[messageId].volatile &&
|
|
1578
|
+
typeof this.outgoing[messageId].cb === 'function'
|
|
1579
|
+
) {
|
|
1580
|
+
this.outgoing[messageId].cb(new Error('Connection closed'))
|
|
1581
|
+
delete this.outgoing[messageId]
|
|
1582
|
+
}
|
|
1583
|
+
})
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
/**
|
|
1588
|
+
* Flush all outgoing messages
|
|
1589
|
+
*/
|
|
1590
|
+
private _flush() {
|
|
1591
|
+
if (this.outgoing) {
|
|
1592
|
+
this.log('_flush: queue exists? %b', !!this.outgoing)
|
|
1593
|
+
Object.keys(this.outgoing).forEach((messageId) => {
|
|
1594
|
+
if (typeof this.outgoing[messageId].cb === 'function') {
|
|
1595
|
+
this.outgoing[messageId].cb(new Error('Connection closed'))
|
|
1596
|
+
// This is suspicious. Why do we only delete this if we have a callback?
|
|
1597
|
+
// If this is by-design, then adding no as callback would cause this to get deleted unintentionally.
|
|
1598
|
+
delete this.outgoing[messageId]
|
|
1599
|
+
}
|
|
1600
|
+
})
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
private _removeTopicAliasAndRecoverTopicName(packet: IPublishPacket) {
|
|
1605
|
+
let alias: number | undefined
|
|
1606
|
+
|
|
1607
|
+
if (packet.properties) {
|
|
1608
|
+
alias = packet.properties.topicAlias
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
let topic = packet.topic.toString()
|
|
1612
|
+
|
|
1613
|
+
this.log(
|
|
1614
|
+
'_removeTopicAliasAndRecoverTopicName :: alias %d, topic %o',
|
|
1615
|
+
alias,
|
|
1616
|
+
topic,
|
|
1617
|
+
)
|
|
1618
|
+
|
|
1619
|
+
if (topic.length === 0) {
|
|
1620
|
+
// restore topic from alias
|
|
1621
|
+
if (typeof alias === 'undefined') {
|
|
1622
|
+
return new Error('Unregistered Topic Alias')
|
|
1623
|
+
}
|
|
1624
|
+
topic = this.topicAliasSend.getTopicByAlias(alias)
|
|
1625
|
+
if (typeof topic === 'undefined') {
|
|
1626
|
+
return new Error('Unregistered Topic Alias')
|
|
1627
|
+
}
|
|
1628
|
+
packet.topic = topic
|
|
1629
|
+
}
|
|
1630
|
+
if (alias) {
|
|
1631
|
+
delete packet.properties.topicAlias
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
private _checkDisconnecting(callback?: GenericCallback<any>) {
|
|
1636
|
+
if (this.disconnecting) {
|
|
1637
|
+
if (callback && callback !== this.noop) {
|
|
1638
|
+
callback(new Error('client disconnecting'))
|
|
1639
|
+
} else {
|
|
1640
|
+
this.emit('error', new Error('client disconnecting'))
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
return this.disconnecting
|
|
1644
|
+
}
|
|
1645
|
+
|
|
985
1646
|
/**
|
|
986
1647
|
* _reconnect - implement reconnection
|
|
987
|
-
* @api
|
|
1648
|
+
* @api private
|
|
988
1649
|
*/
|
|
989
|
-
_reconnect() {
|
|
1650
|
+
private _reconnect() {
|
|
990
1651
|
this.log('_reconnect: emitting reconnect to client')
|
|
991
1652
|
this.emit('reconnect')
|
|
992
1653
|
if (this.connected) {
|
|
@@ -1003,7 +1664,7 @@ class MqttClient extends EventEmitter {
|
|
|
1003
1664
|
/**
|
|
1004
1665
|
* _setupReconnect - setup reconnect timer
|
|
1005
1666
|
*/
|
|
1006
|
-
_setupReconnect() {
|
|
1667
|
+
private _setupReconnect() {
|
|
1007
1668
|
if (
|
|
1008
1669
|
!this.disconnecting &&
|
|
1009
1670
|
!this.reconnectTimer &&
|
|
@@ -1031,7 +1692,7 @@ class MqttClient extends EventEmitter {
|
|
|
1031
1692
|
/**
|
|
1032
1693
|
* _clearReconnect - clear the reconnect timer
|
|
1033
1694
|
*/
|
|
1034
|
-
_clearReconnect() {
|
|
1695
|
+
private _clearReconnect() {
|
|
1035
1696
|
this.log('_clearReconnect : clearing reconnect timer')
|
|
1036
1697
|
if (this.reconnectTimer) {
|
|
1037
1698
|
clearInterval(this.reconnectTimer)
|
|
@@ -1043,7 +1704,7 @@ class MqttClient extends EventEmitter {
|
|
|
1043
1704
|
* _cleanUp - clean up on connection end
|
|
1044
1705
|
* @api private
|
|
1045
1706
|
*/
|
|
1046
|
-
_cleanUp(forced, done, opts = {}) {
|
|
1707
|
+
private _cleanUp(forced: boolean, done?: DoneCallback, opts = {}) {
|
|
1047
1708
|
if (done) {
|
|
1048
1709
|
this.log('_cleanUp :: done callback provided for on stream close')
|
|
1049
1710
|
this.stream.on('close', done)
|
|
@@ -1052,7 +1713,7 @@ class MqttClient extends EventEmitter {
|
|
|
1052
1713
|
this.log('_cleanUp :: forced? %s', forced)
|
|
1053
1714
|
if (forced) {
|
|
1054
1715
|
if (this.options.reconnectPeriod === 0 && this.options.clean) {
|
|
1055
|
-
this._flush(
|
|
1716
|
+
this._flush()
|
|
1056
1717
|
}
|
|
1057
1718
|
this.log(
|
|
1058
1719
|
'_cleanUp :: (%s) :: destroying stream',
|
|
@@ -1060,7 +1721,7 @@ class MqttClient extends EventEmitter {
|
|
|
1060
1721
|
)
|
|
1061
1722
|
this.stream.destroy()
|
|
1062
1723
|
} else {
|
|
1063
|
-
const packet = { cmd: 'disconnect', ...opts }
|
|
1724
|
+
const packet: IDisconnectPacket = { cmd: 'disconnect', ...opts }
|
|
1064
1725
|
this.log(
|
|
1065
1726
|
'_cleanUp :: (%s) :: call _sendPacket with disconnect packet',
|
|
1066
1727
|
this.options.clientId,
|
|
@@ -1107,19 +1768,25 @@ class MqttClient extends EventEmitter {
|
|
|
1107
1768
|
}
|
|
1108
1769
|
}
|
|
1109
1770
|
|
|
1110
|
-
_storeAndSend(
|
|
1771
|
+
private _storeAndSend(
|
|
1772
|
+
packet: Packet,
|
|
1773
|
+
cb: DoneCallback,
|
|
1774
|
+
cbStorePut: DoneCallback,
|
|
1775
|
+
) {
|
|
1111
1776
|
this.log(
|
|
1112
1777
|
'storeAndSend :: store packet with cmd %s to outgoingStore',
|
|
1113
1778
|
packet.cmd,
|
|
1114
1779
|
)
|
|
1115
1780
|
let storePacket = packet
|
|
1116
|
-
let err
|
|
1781
|
+
let err: Error | undefined
|
|
1117
1782
|
if (storePacket.cmd === 'publish') {
|
|
1118
1783
|
// The original packet is for sending.
|
|
1119
1784
|
// The cloned storePacket is for storing to resend on reconnect.
|
|
1120
1785
|
// Topic Alias must not be used after disconnected.
|
|
1121
1786
|
storePacket = clone(packet)
|
|
1122
|
-
err = this._removeTopicAliasAndRecoverTopicName(
|
|
1787
|
+
err = this._removeTopicAliasAndRecoverTopicName(
|
|
1788
|
+
storePacket as IPublishPacket,
|
|
1789
|
+
)
|
|
1123
1790
|
if (err) {
|
|
1124
1791
|
return cb && cb(err)
|
|
1125
1792
|
}
|
|
@@ -1133,10 +1800,10 @@ class MqttClient extends EventEmitter {
|
|
|
1133
1800
|
})
|
|
1134
1801
|
}
|
|
1135
1802
|
|
|
1136
|
-
_applyTopicAlias(packet) {
|
|
1803
|
+
private _applyTopicAlias(packet: Packet) {
|
|
1137
1804
|
if (this.options.protocolVersion === 5) {
|
|
1138
1805
|
if (packet.cmd === 'publish') {
|
|
1139
|
-
let alias
|
|
1806
|
+
let alias: number
|
|
1140
1807
|
if (packet.properties) {
|
|
1141
1808
|
alias = packet.properties.topicAlias
|
|
1142
1809
|
}
|
|
@@ -1216,12 +1883,12 @@ class MqttClient extends EventEmitter {
|
|
|
1216
1883
|
}
|
|
1217
1884
|
}
|
|
1218
1885
|
|
|
1219
|
-
_noop(err) {
|
|
1886
|
+
private _noop(err?: Error) {
|
|
1220
1887
|
this.log('noop ::', err)
|
|
1221
1888
|
}
|
|
1222
1889
|
|
|
1223
1890
|
/** Writes the packet to stream and emits events */
|
|
1224
|
-
_writePacket(packet, cb) {
|
|
1891
|
+
private _writePacket(packet: Packet, cb?: DoneCallback) {
|
|
1225
1892
|
this.log('_writePacket :: packet: %O', packet)
|
|
1226
1893
|
this.log('_writePacket :: emitting `packetsend`')
|
|
1227
1894
|
|
|
@@ -1256,7 +1923,12 @@ class MqttClient extends EventEmitter {
|
|
|
1256
1923
|
* @param {Boolean} noStore - send without put to the store
|
|
1257
1924
|
* @api private
|
|
1258
1925
|
*/
|
|
1259
|
-
_sendPacket(
|
|
1926
|
+
private _sendPacket(
|
|
1927
|
+
packet: Packet,
|
|
1928
|
+
cb?: DoneCallback,
|
|
1929
|
+
cbStorePut?: DoneCallback,
|
|
1930
|
+
noStore?: boolean,
|
|
1931
|
+
) {
|
|
1260
1932
|
this.log('_sendPacket :: (%s) :: start', this.options.clientId)
|
|
1261
1933
|
cbStorePut = cbStorePut || this.noop
|
|
1262
1934
|
cb = cb || this.noop
|
|
@@ -1270,7 +1942,7 @@ class MqttClient extends EventEmitter {
|
|
|
1270
1942
|
if (!this.connected) {
|
|
1271
1943
|
// allow auth packets to be sent while authenticating with the broker (mqtt5 enhanced auth)
|
|
1272
1944
|
if (packet.cmd === 'auth') {
|
|
1273
|
-
this._writePacket(
|
|
1945
|
+
this._writePacket(packet, cb)
|
|
1274
1946
|
return
|
|
1275
1947
|
}
|
|
1276
1948
|
|
|
@@ -1328,7 +2000,11 @@ class MqttClient extends EventEmitter {
|
|
|
1328
2000
|
* @param {Function} cbStorePut - called when message is put into outgoingStore
|
|
1329
2001
|
* @api private
|
|
1330
2002
|
*/
|
|
1331
|
-
_storePacket(
|
|
2003
|
+
private _storePacket(
|
|
2004
|
+
packet: Packet,
|
|
2005
|
+
cb: DoneCallback,
|
|
2006
|
+
cbStorePut: DoneCallback,
|
|
2007
|
+
) {
|
|
1332
2008
|
this.log('_storePacket :: packet: %o', packet)
|
|
1333
2009
|
this.log('_storePacket :: cb? %s', !!cb)
|
|
1334
2010
|
cbStorePut = cbStorePut || this.noop
|
|
@@ -1339,18 +2015,19 @@ class MqttClient extends EventEmitter {
|
|
|
1339
2015
|
// The cloned storePacket is for storing to resend on reconnect.
|
|
1340
2016
|
// Topic Alias must not be used after disconnected.
|
|
1341
2017
|
storePacket = clone(packet)
|
|
1342
|
-
const err = this._removeTopicAliasAndRecoverTopicName(
|
|
2018
|
+
const err = this._removeTopicAliasAndRecoverTopicName(
|
|
2019
|
+
storePacket as IPublishPacket,
|
|
2020
|
+
)
|
|
1343
2021
|
if (err) {
|
|
1344
2022
|
return cb && cb(err)
|
|
1345
2023
|
}
|
|
1346
2024
|
}
|
|
2025
|
+
|
|
2026
|
+
const qos = (storePacket as IPublishPacket).qos || 0
|
|
1347
2027
|
// check that the packet is not a qos of 0, or that the command is not a publish
|
|
1348
|
-
if (
|
|
1349
|
-
((storePacket.qos || 0) === 0 && this.queueQoSZero) ||
|
|
1350
|
-
storePacket.cmd !== 'publish'
|
|
1351
|
-
) {
|
|
2028
|
+
if ((qos === 0 && this.queueQoSZero) || storePacket.cmd !== 'publish') {
|
|
1352
2029
|
this.queue.push({ packet: storePacket, cb })
|
|
1353
|
-
} else if (
|
|
2030
|
+
} else if (qos > 0) {
|
|
1354
2031
|
cb = this.outgoing[storePacket.messageId]
|
|
1355
2032
|
? this.outgoing[storePacket.messageId].cb
|
|
1356
2033
|
: null
|
|
@@ -1370,7 +2047,7 @@ class MqttClient extends EventEmitter {
|
|
|
1370
2047
|
*
|
|
1371
2048
|
* @api private
|
|
1372
2049
|
*/
|
|
1373
|
-
_setupPingTimer() {
|
|
2050
|
+
private _setupPingTimer() {
|
|
1374
2051
|
this.log(
|
|
1375
2052
|
'_setupPingTimer :: keepalive %d (seconds)',
|
|
1376
2053
|
this.options.keepalive,
|
|
@@ -1389,7 +2066,7 @@ class MqttClient extends EventEmitter {
|
|
|
1389
2066
|
*
|
|
1390
2067
|
* @api private
|
|
1391
2068
|
*/
|
|
1392
|
-
_shiftPingInterval() {
|
|
2069
|
+
private _shiftPingInterval() {
|
|
1393
2070
|
if (
|
|
1394
2071
|
this.pingTimer &&
|
|
1395
2072
|
this.options.keepalive &&
|
|
@@ -1404,7 +2081,7 @@ class MqttClient extends EventEmitter {
|
|
|
1404
2081
|
*
|
|
1405
2082
|
* @api private
|
|
1406
2083
|
*/
|
|
1407
|
-
_checkPing() {
|
|
2084
|
+
private _checkPing() {
|
|
1408
2085
|
this.log('_checkPing :: checking ping...')
|
|
1409
2086
|
if (this.pingResp) {
|
|
1410
2087
|
this.log(
|
|
@@ -1419,48 +2096,11 @@ class MqttClient extends EventEmitter {
|
|
|
1419
2096
|
}
|
|
1420
2097
|
}
|
|
1421
2098
|
|
|
1422
|
-
/**
|
|
1423
|
-
* @param packet the packet received by the broker
|
|
1424
|
-
* @return the auth packet to be returned to the broker
|
|
1425
|
-
* @api public
|
|
1426
|
-
*/
|
|
1427
|
-
handleAuth(packet, callback) {
|
|
1428
|
-
callback()
|
|
1429
|
-
}
|
|
1430
|
-
|
|
1431
|
-
/**
|
|
1432
|
-
* Handle messages with backpressure support, one at a time.
|
|
1433
|
-
* Override at will.
|
|
1434
|
-
*
|
|
1435
|
-
* @param Packet packet the packet
|
|
1436
|
-
* @param Function callback call when finished
|
|
1437
|
-
* @api public
|
|
1438
|
-
*/
|
|
1439
|
-
handleMessage(packet, callback) {
|
|
1440
|
-
callback()
|
|
1441
|
-
}
|
|
1442
|
-
|
|
1443
|
-
/**
|
|
1444
|
-
* _nextId
|
|
1445
|
-
* @return unsigned int
|
|
1446
|
-
*/
|
|
1447
|
-
_nextId() {
|
|
1448
|
-
return this.messageIdProvider.allocate()
|
|
1449
|
-
}
|
|
1450
|
-
|
|
1451
|
-
/**
|
|
1452
|
-
* getLastMessageId
|
|
1453
|
-
* @return unsigned int
|
|
1454
|
-
*/
|
|
1455
|
-
getLastMessageId() {
|
|
1456
|
-
return this.messageIdProvider.getLastAllocated()
|
|
1457
|
-
}
|
|
1458
|
-
|
|
1459
2099
|
/**
|
|
1460
2100
|
* _resubscribe
|
|
1461
2101
|
* @api private
|
|
1462
2102
|
*/
|
|
1463
|
-
_resubscribe() {
|
|
2103
|
+
private _resubscribe() {
|
|
1464
2104
|
this.log('_resubscribe')
|
|
1465
2105
|
const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics)
|
|
1466
2106
|
if (
|
|
@@ -1478,7 +2118,7 @@ class MqttClient extends EventEmitter {
|
|
|
1478
2118
|
topicI < _resubscribeTopicsKeys.length;
|
|
1479
2119
|
topicI++
|
|
1480
2120
|
) {
|
|
1481
|
-
const resubscribeTopic = {}
|
|
2121
|
+
const resubscribeTopic: ISubscriptionMap = {}
|
|
1482
2122
|
resubscribeTopic[_resubscribeTopicsKeys[topicI]] =
|
|
1483
2123
|
this._resubscribeTopics[
|
|
1484
2124
|
_resubscribeTopicsKeys[topicI]
|
|
@@ -1507,7 +2147,7 @@ class MqttClient extends EventEmitter {
|
|
|
1507
2147
|
*
|
|
1508
2148
|
* @api private
|
|
1509
2149
|
*/
|
|
1510
|
-
_onConnect(packet) {
|
|
2150
|
+
private _onConnect(packet: IConnackPacket) {
|
|
1511
2151
|
if (this.disconnected) {
|
|
1512
2152
|
this.emit('connect', packet)
|
|
1513
2153
|
return
|
|
@@ -1519,9 +2159,11 @@ class MqttClient extends EventEmitter {
|
|
|
1519
2159
|
|
|
1520
2160
|
this.connected = true
|
|
1521
2161
|
|
|
2162
|
+
/** check if there are packets in outgoing store and stream them */
|
|
1522
2163
|
const startStreamProcess = () => {
|
|
1523
2164
|
let outStore = this.outgoingStore.createStream()
|
|
1524
2165
|
|
|
2166
|
+
/** destroy the outgoing store stream */
|
|
1525
2167
|
const remove = () => {
|
|
1526
2168
|
outStore.destroy()
|
|
1527
2169
|
outStore = null
|
|
@@ -1529,6 +2171,7 @@ class MqttClient extends EventEmitter {
|
|
|
1529
2171
|
clearStoreProcessing()
|
|
1530
2172
|
}
|
|
1531
2173
|
|
|
2174
|
+
/** stop store processing and clear packets id processed */
|
|
1532
2175
|
const clearStoreProcessing = () => {
|
|
1533
2176
|
this._storeProcessing = false
|
|
1534
2177
|
this._packetIdsDuringStoreProcessing = {}
|
|
@@ -1542,6 +2185,7 @@ class MqttClient extends EventEmitter {
|
|
|
1542
2185
|
this.emit('error', err)
|
|
1543
2186
|
})
|
|
1544
2187
|
|
|
2188
|
+
/** Read next packet in outgoing store and send it */
|
|
1545
2189
|
const storeDeliver = () => {
|
|
1546
2190
|
// edge case, we wrapped this twice
|
|
1547
2191
|
if (!outStore) {
|
|
@@ -1550,7 +2194,7 @@ class MqttClient extends EventEmitter {
|
|
|
1550
2194
|
|
|
1551
2195
|
const packet2 = outStore.read(1)
|
|
1552
2196
|
|
|
1553
|
-
let cb
|
|
2197
|
+
let cb: PacketCallback
|
|
1554
2198
|
|
|
1555
2199
|
if (!packet2) {
|
|
1556
2200
|
// read when data is available in the future
|
|
@@ -1582,9 +2226,8 @@ class MqttClient extends EventEmitter {
|
|
|
1582
2226
|
storeDeliver()
|
|
1583
2227
|
},
|
|
1584
2228
|
}
|
|
1585
|
-
this._packetIdsDuringStoreProcessing[
|
|
1586
|
-
|
|
1587
|
-
] = true
|
|
2229
|
+
this._packetIdsDuringStoreProcessing[packet2.messageId] =
|
|
2230
|
+
true
|
|
1588
2231
|
if (this.messageIdProvider.register(packet2.messageId)) {
|
|
1589
2232
|
this._sendPacket(packet2, undefined, undefined, true)
|
|
1590
2233
|
} else {
|
|
@@ -1621,7 +2264,7 @@ class MqttClient extends EventEmitter {
|
|
|
1621
2264
|
startStreamProcess()
|
|
1622
2265
|
}
|
|
1623
2266
|
|
|
1624
|
-
_invokeStoreProcessingQueue() {
|
|
2267
|
+
private _invokeStoreProcessingQueue() {
|
|
1625
2268
|
// If _storeProcessing is true, the message is resending.
|
|
1626
2269
|
// During resend, processing is skipped to prevent new messages from interrupting. #1635
|
|
1627
2270
|
if (!this._storeProcessing && this._storeProcessingQueue.length > 0) {
|
|
@@ -1634,13 +2277,13 @@ class MqttClient extends EventEmitter {
|
|
|
1634
2277
|
return false
|
|
1635
2278
|
}
|
|
1636
2279
|
|
|
1637
|
-
_invokeAllStoreProcessingQueue() {
|
|
2280
|
+
private _invokeAllStoreProcessingQueue() {
|
|
1638
2281
|
while (this._invokeStoreProcessingQueue()) {
|
|
1639
2282
|
/* empty */
|
|
1640
2283
|
}
|
|
1641
2284
|
}
|
|
1642
2285
|
|
|
1643
|
-
_flushStoreProcessingQueue() {
|
|
2286
|
+
private _flushStoreProcessingQueue() {
|
|
1644
2287
|
for (const f of this._storeProcessingQueue) {
|
|
1645
2288
|
if (f.cbStorePut) f.cbStorePut(new Error('Connection closed'))
|
|
1646
2289
|
if (f.callback) f.callback(new Error('Connection closed'))
|
|
@@ -1654,15 +2297,15 @@ class MqttClient extends EventEmitter {
|
|
|
1654
2297
|
* @param {Function} cb - called when the message removed
|
|
1655
2298
|
* @api private
|
|
1656
2299
|
*/
|
|
1657
|
-
_removeOutgoingAndStoreMessage(
|
|
1658
|
-
|
|
2300
|
+
private _removeOutgoingAndStoreMessage(
|
|
2301
|
+
messageId: number,
|
|
2302
|
+
cb: PacketCallback,
|
|
2303
|
+
) {
|
|
1659
2304
|
delete this.outgoing[messageId]
|
|
1660
|
-
|
|
2305
|
+
this.outgoingStore.del({ messageId }, (err, packet) => {
|
|
1661
2306
|
cb(err, packet)
|
|
1662
|
-
|
|
1663
|
-
|
|
2307
|
+
this.messageIdProvider.deallocate(messageId)
|
|
2308
|
+
this._invokeStoreProcessingQueue()
|
|
1664
2309
|
})
|
|
1665
2310
|
}
|
|
1666
2311
|
}
|
|
1667
|
-
|
|
1668
|
-
module.exports = MqttClient
|