mqtt 4.2.8 → 4.3.3
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/CHANGELOG.md +63 -1
- package/CONTRIBUTING.md +27 -27
- package/LICENSE.md +15 -15
- package/README.md +843 -792
- package/bin/mqtt.js +27 -27
- package/bin/pub.js +146 -146
- package/bin/sub.js +123 -123
- package/dist/mqtt.js +14842 -4574
- package/dist/mqtt.min.js +1 -1
- package/doc/help.txt +8 -8
- package/doc/publish.txt +26 -26
- package/doc/subscribe.txt +26 -26
- package/lib/client.js +563 -260
- package/lib/connect/ali.js +128 -128
- package/lib/connect/index.js +165 -164
- package/lib/connect/tcp.js +20 -21
- package/lib/connect/tls.js +48 -45
- package/lib/connect/ws.js +256 -256
- package/lib/connect/wx.js +132 -134
- package/lib/default-message-id-provider.js +69 -0
- package/lib/store.js +128 -128
- package/lib/topic-alias-recv.js +47 -0
- package/lib/topic-alias-send.js +93 -0
- package/lib/unique-message-id-provider.js +65 -0
- package/lib/validations.js +52 -52
- package/mqtt.js +21 -17
- package/package.json +21 -8
- package/types/index.d.ts +30 -28
- package/types/lib/client-options.d.ts +204 -191
- package/types/lib/client.d.ts +261 -240
- package/types/lib/connect/index.d.ts +10 -10
- package/types/lib/default-message-id-provider.d.ts +49 -0
- package/types/lib/message-id-provider.d.ts +40 -0
- package/types/lib/store-options.d.ts +6 -6
- package/types/lib/store.d.ts +46 -46
- package/types/lib/unique-message-id-provider.d.ts +48 -0
package/lib/client.js
CHANGED
|
@@ -3,21 +3,25 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Module dependencies
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
6
|
+
const EventEmitter = require('events').EventEmitter
|
|
7
|
+
const Store = require('./store')
|
|
8
|
+
const TopicAliasRecv = require('./topic-alias-recv')
|
|
9
|
+
const TopicAliasSend = require('./topic-alias-send')
|
|
10
|
+
const mqttPacket = require('mqtt-packet')
|
|
11
|
+
const DefaultMessageIdProvider = require('./default-message-id-provider')
|
|
12
|
+
const Writable = require('readable-stream').Writable
|
|
13
|
+
const inherits = require('inherits')
|
|
14
|
+
const reInterval = require('reinterval')
|
|
15
|
+
const clone = require('rfdc/default')
|
|
16
|
+
const validations = require('./validations')
|
|
17
|
+
const xtend = require('xtend')
|
|
18
|
+
const debug = require('debug')('mqttjs:client')
|
|
19
|
+
const nextTick = process ? process.nextTick : function (callback) { setTimeout(callback, 0) }
|
|
20
|
+
const setImmediate = global.setImmediate || function (callback) {
|
|
17
21
|
// works in node v0.8
|
|
18
22
|
nextTick(callback)
|
|
19
23
|
}
|
|
20
|
-
|
|
24
|
+
const defaultConnectOptions = {
|
|
21
25
|
keepalive: 60,
|
|
22
26
|
reschedulePings: true,
|
|
23
27
|
protocolId: 'MQTT',
|
|
@@ -28,7 +32,7 @@ var defaultConnectOptions = {
|
|
|
28
32
|
resubscribe: true
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
const socketErrors = [
|
|
32
36
|
'ECONNREFUSED',
|
|
33
37
|
'EADDRINUSE',
|
|
34
38
|
'ECONNRESET',
|
|
@@ -37,7 +41,7 @@ var socketErrors = [
|
|
|
37
41
|
|
|
38
42
|
// Other Socket Errors: EADDRINUSE, ECONNRESET, ENOTFOUND.
|
|
39
43
|
|
|
40
|
-
|
|
44
|
+
const errors = {
|
|
41
45
|
0: '',
|
|
42
46
|
1: 'Unacceptable protocol version',
|
|
43
47
|
2: 'Identifier rejected',
|
|
@@ -87,13 +91,89 @@ function defaultId () {
|
|
|
87
91
|
return 'mqttjs_' + Math.random().toString(16).substr(2, 8)
|
|
88
92
|
}
|
|
89
93
|
|
|
94
|
+
function applyTopicAlias (client, packet) {
|
|
95
|
+
if (client.options.protocolVersion === 5) {
|
|
96
|
+
if (packet.cmd === 'publish') {
|
|
97
|
+
let alias
|
|
98
|
+
if (packet.properties) {
|
|
99
|
+
alias = packet.properties.topicAlias
|
|
100
|
+
}
|
|
101
|
+
const topic = packet.topic.toString()
|
|
102
|
+
if (client.topicAliasSend) {
|
|
103
|
+
if (alias) {
|
|
104
|
+
if (topic.length !== 0) {
|
|
105
|
+
// register topic alias
|
|
106
|
+
debug('applyTopicAlias :: register topic: %s - alias: %d', topic, alias)
|
|
107
|
+
if (!client.topicAliasSend.put(topic, alias)) {
|
|
108
|
+
debug('applyTopicAlias :: error out of range. topic: %s - alias: %d', topic, alias)
|
|
109
|
+
return new Error('Sending Topic Alias out of range')
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
if (topic.length !== 0) {
|
|
114
|
+
if (client.options.autoAssignTopicAlias) {
|
|
115
|
+
alias = client.topicAliasSend.getAliasByTopic(topic)
|
|
116
|
+
if (alias) {
|
|
117
|
+
packet.topic = ''
|
|
118
|
+
packet.properties = { ...(packet.properties), topicAlias: alias }
|
|
119
|
+
debug('applyTopicAlias :: auto assign(use) topic: %s - alias: %d', topic, alias)
|
|
120
|
+
} else {
|
|
121
|
+
alias = client.topicAliasSend.getLruAlias()
|
|
122
|
+
client.topicAliasSend.put(topic, alias)
|
|
123
|
+
packet.properties = { ...(packet.properties), topicAlias: alias }
|
|
124
|
+
debug('applyTopicAlias :: auto assign topic: %s - alias: %d', topic, alias)
|
|
125
|
+
}
|
|
126
|
+
} else if (client.options.autoUseTopicAlias) {
|
|
127
|
+
alias = client.topicAliasSend.getAliasByTopic(topic)
|
|
128
|
+
if (alias) {
|
|
129
|
+
packet.topic = ''
|
|
130
|
+
packet.properties = { ...(packet.properties), topicAlias: alias }
|
|
131
|
+
debug('applyTopicAlias :: auto use topic: %s - alias: %d', topic, alias)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
} else if (alias) {
|
|
137
|
+
debug('applyTopicAlias :: error out of range. topic: %s - alias: %d', topic, alias)
|
|
138
|
+
return new Error('Sending Topic Alias out of range')
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function removeTopicAliasAndRecoverTopicName (client, packet) {
|
|
145
|
+
let alias
|
|
146
|
+
if (packet.properties) {
|
|
147
|
+
alias = packet.properties.topicAlias
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let topic = packet.topic.toString()
|
|
151
|
+
if (topic.length === 0) {
|
|
152
|
+
// restore topic from alias
|
|
153
|
+
if (typeof alias === 'undefined') {
|
|
154
|
+
return new Error('Unregistered Topic Alias')
|
|
155
|
+
} else {
|
|
156
|
+
topic = client.topicAliasSend.getTopicByAlias(alias)
|
|
157
|
+
if (typeof topic === 'undefined') {
|
|
158
|
+
return new Error('Unregistered Topic Alias')
|
|
159
|
+
} else {
|
|
160
|
+
packet.topic = topic
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (alias) {
|
|
165
|
+
delete packet.properties.topicAlias
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
90
169
|
function sendPacket (client, packet, cb) {
|
|
91
170
|
debug('sendPacket :: packet: %O', packet)
|
|
92
171
|
debug('sendPacket :: emitting `packetsend`')
|
|
172
|
+
|
|
93
173
|
client.emit('packetsend', packet)
|
|
94
174
|
|
|
95
175
|
debug('sendPacket :: writing to stream')
|
|
96
|
-
|
|
176
|
+
const result = mqttPacket.writeToStream(packet, client.stream, client.options)
|
|
97
177
|
debug('sendPacket :: writeToStream result %s', result)
|
|
98
178
|
if (!result && cb) {
|
|
99
179
|
debug('sendPacket :: handle events on `drain` once through callback.')
|
|
@@ -130,7 +210,19 @@ function flushVolatile (queue) {
|
|
|
130
210
|
|
|
131
211
|
function storeAndSend (client, packet, cb, cbStorePut) {
|
|
132
212
|
debug('storeAndSend :: store packet with cmd %s to outgoingStore', packet.cmd)
|
|
133
|
-
|
|
213
|
+
let storePacket = packet
|
|
214
|
+
let err
|
|
215
|
+
if (storePacket.cmd === 'publish') {
|
|
216
|
+
// The original packet is for sending.
|
|
217
|
+
// The cloned storePacket is for storing to resend on reconnect.
|
|
218
|
+
// Topic Alias must not be used after disconnected.
|
|
219
|
+
storePacket = clone(packet)
|
|
220
|
+
err = removeTopicAliasAndRecoverTopicName(client, storePacket)
|
|
221
|
+
if (err) {
|
|
222
|
+
return cb && cb(err)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
client.outgoingStore.put(storePacket, function storedPacket (err) {
|
|
134
226
|
if (err) {
|
|
135
227
|
return cb && cb(err)
|
|
136
228
|
}
|
|
@@ -151,8 +243,8 @@ function nop (error) {
|
|
|
151
243
|
* (see Connection#connect)
|
|
152
244
|
*/
|
|
153
245
|
function MqttClient (streamBuilder, options) {
|
|
154
|
-
|
|
155
|
-
|
|
246
|
+
let k
|
|
247
|
+
const that = this
|
|
156
248
|
|
|
157
249
|
if (!(this instanceof MqttClient)) {
|
|
158
250
|
return new MqttClient(streamBuilder, options)
|
|
@@ -175,6 +267,7 @@ function MqttClient (streamBuilder, options) {
|
|
|
175
267
|
debug('MqttClient :: options.keepalive', options.keepalive)
|
|
176
268
|
debug('MqttClient :: options.reconnectPeriod', options.reconnectPeriod)
|
|
177
269
|
debug('MqttClient :: options.rejectUnauthorized', options.rejectUnauthorized)
|
|
270
|
+
debug('MqttClient :: options.topicAliasMaximum', options.topicAliasMaximum)
|
|
178
271
|
|
|
179
272
|
this.options.clientId = (typeof options.clientId === 'string') ? options.clientId : defaultId()
|
|
180
273
|
|
|
@@ -184,6 +277,8 @@ function MqttClient (streamBuilder, options) {
|
|
|
184
277
|
|
|
185
278
|
this.streamBuilder = streamBuilder
|
|
186
279
|
|
|
280
|
+
this.messageIdProvider = (typeof this.options.messageIdProvider === 'undefined') ? new DefaultMessageIdProvider() : this.options.messageIdProvider
|
|
281
|
+
|
|
187
282
|
// Inflight message storages
|
|
188
283
|
this.outgoingStore = options.outgoingStore || new Store()
|
|
189
284
|
this.incomingStore = options.incomingStore || new Store()
|
|
@@ -213,11 +308,8 @@ function MqttClient (streamBuilder, options) {
|
|
|
213
308
|
this._storeProcessing = false
|
|
214
309
|
// Packet Ids are put into the store during store processing
|
|
215
310
|
this._packetIdsDuringStoreProcessing = {}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
* ensure that nextId is min. 1, see https://github.com/mqttjs/MQTT.js/issues/810
|
|
219
|
-
*/
|
|
220
|
-
this.nextId = Math.max(1, Math.floor(Math.random() * 65535))
|
|
311
|
+
// Store processing queue
|
|
312
|
+
this._storeProcessingQueue = []
|
|
221
313
|
|
|
222
314
|
// Inflight callbacks
|
|
223
315
|
this.outgoing = {}
|
|
@@ -225,30 +317,50 @@ function MqttClient (streamBuilder, options) {
|
|
|
225
317
|
// True if connection is first time.
|
|
226
318
|
this._firstConnection = true
|
|
227
319
|
|
|
320
|
+
if (options.topicAliasMaximum > 0) {
|
|
321
|
+
if (options.topicAliasMaximum > 0xffff) {
|
|
322
|
+
debug('MqttClient :: options.topicAliasMaximum is out of range')
|
|
323
|
+
} else {
|
|
324
|
+
this.topicAliasRecv = new TopicAliasRecv(options.topicAliasMaximum)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
228
328
|
// Send queued packets
|
|
229
329
|
this.on('connect', function () {
|
|
230
|
-
|
|
330
|
+
const queue = this.queue
|
|
231
331
|
|
|
232
332
|
function deliver () {
|
|
233
|
-
|
|
333
|
+
const entry = queue.shift()
|
|
234
334
|
debug('deliver :: entry %o', entry)
|
|
235
|
-
|
|
335
|
+
let packet = null
|
|
236
336
|
|
|
237
337
|
if (!entry) {
|
|
338
|
+
that._resubscribe()
|
|
238
339
|
return
|
|
239
340
|
}
|
|
240
341
|
|
|
241
342
|
packet = entry.packet
|
|
242
343
|
debug('deliver :: call _sendPacket for %o', packet)
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
entry.cb(err)
|
|
248
|
-
}
|
|
249
|
-
deliver()
|
|
344
|
+
let send = true
|
|
345
|
+
if (packet.messageId && packet.messageId !== 0) {
|
|
346
|
+
if (!that.messageIdProvider.register(packet.messageId)) {
|
|
347
|
+
send = false
|
|
250
348
|
}
|
|
251
|
-
|
|
349
|
+
}
|
|
350
|
+
if (send) {
|
|
351
|
+
that._sendPacket(
|
|
352
|
+
packet,
|
|
353
|
+
function (err) {
|
|
354
|
+
if (entry.cb) {
|
|
355
|
+
entry.cb(err)
|
|
356
|
+
}
|
|
357
|
+
deliver()
|
|
358
|
+
}
|
|
359
|
+
)
|
|
360
|
+
} else {
|
|
361
|
+
debug('messageId: %d has already used. The message is skipped and removed.', packet.messageId)
|
|
362
|
+
deliver()
|
|
363
|
+
}
|
|
252
364
|
}
|
|
253
365
|
|
|
254
366
|
debug('connect :: sending queued packets')
|
|
@@ -268,6 +380,10 @@ function MqttClient (streamBuilder, options) {
|
|
|
268
380
|
that.pingTimer = null
|
|
269
381
|
}
|
|
270
382
|
|
|
383
|
+
if (this.topicAliasRecv) {
|
|
384
|
+
this.topicAliasRecv.clear()
|
|
385
|
+
}
|
|
386
|
+
|
|
271
387
|
debug('close :: calling _setupReconnect')
|
|
272
388
|
this._setupReconnect()
|
|
273
389
|
})
|
|
@@ -284,12 +400,11 @@ inherits(MqttClient, EventEmitter)
|
|
|
284
400
|
* @api private
|
|
285
401
|
*/
|
|
286
402
|
MqttClient.prototype._setupStream = function () {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
var packets = []
|
|
403
|
+
const that = this
|
|
404
|
+
const writable = new Writable()
|
|
405
|
+
const parser = mqttPacket.parser(this.options)
|
|
406
|
+
let completeParse = null
|
|
407
|
+
const packets = []
|
|
293
408
|
|
|
294
409
|
debug('_setupStream :: calling method to clear reconnect')
|
|
295
410
|
this._clearReconnect()
|
|
@@ -306,7 +421,7 @@ MqttClient.prototype._setupStream = function () {
|
|
|
306
421
|
if (packets.length) {
|
|
307
422
|
nextTick(work)
|
|
308
423
|
} else {
|
|
309
|
-
|
|
424
|
+
const done = completeParse
|
|
310
425
|
completeParse = null
|
|
311
426
|
done()
|
|
312
427
|
}
|
|
@@ -314,14 +429,14 @@ MqttClient.prototype._setupStream = function () {
|
|
|
314
429
|
|
|
315
430
|
function work () {
|
|
316
431
|
debug('work :: getting next packet in queue')
|
|
317
|
-
|
|
432
|
+
const packet = packets.shift()
|
|
318
433
|
|
|
319
434
|
if (packet) {
|
|
320
435
|
debug('work :: packet pulled from queue')
|
|
321
436
|
that._handlePacket(packet, nextTickWork)
|
|
322
437
|
} else {
|
|
323
438
|
debug('work :: no packets in queue')
|
|
324
|
-
|
|
439
|
+
const done = completeParse
|
|
325
440
|
completeParse = null
|
|
326
441
|
debug('work :: done flag is %s', !!(done))
|
|
327
442
|
if (done) done()
|
|
@@ -362,8 +477,16 @@ MqttClient.prototype._setupStream = function () {
|
|
|
362
477
|
|
|
363
478
|
// Send a connect packet
|
|
364
479
|
debug('_setupStream: sending packet `connect`')
|
|
365
|
-
connectPacket = Object.create(this.options)
|
|
480
|
+
const connectPacket = Object.create(this.options)
|
|
366
481
|
connectPacket.cmd = 'connect'
|
|
482
|
+
if (this.topicAliasRecv) {
|
|
483
|
+
if (!connectPacket.properties) {
|
|
484
|
+
connectPacket.properties = {}
|
|
485
|
+
}
|
|
486
|
+
if (this.topicAliasRecv) {
|
|
487
|
+
connectPacket.properties.topicAliasMaximum = this.topicAliasRecv.max
|
|
488
|
+
}
|
|
489
|
+
}
|
|
367
490
|
// avoid message queue
|
|
368
491
|
sendPacket(this, connectPacket)
|
|
369
492
|
|
|
@@ -379,7 +502,7 @@ MqttClient.prototype._setupStream = function () {
|
|
|
379
502
|
return this
|
|
380
503
|
}
|
|
381
504
|
if (this.options.properties.authenticationMethod && this.options.authPacket && typeof this.options.authPacket === 'object') {
|
|
382
|
-
|
|
505
|
+
const authPacket = xtend({ cmd: 'auth', reasonCode: 0 }, this.options.authPacket)
|
|
383
506
|
sendPacket(this, authPacket)
|
|
384
507
|
}
|
|
385
508
|
}
|
|
@@ -395,11 +518,11 @@ MqttClient.prototype._setupStream = function () {
|
|
|
395
518
|
}
|
|
396
519
|
|
|
397
520
|
MqttClient.prototype._handlePacket = function (packet, done) {
|
|
398
|
-
|
|
521
|
+
const options = this.options
|
|
399
522
|
|
|
400
523
|
if (options.protocolVersion === 5 && options.properties && options.properties.maximumPacketSize && options.properties.maximumPacketSize < packet.length) {
|
|
401
524
|
this.emit('error', new Error('exceeding packets size ' + packet.cmd))
|
|
402
|
-
this.end({reasonCode: 149, properties: { reasonString: 'Maximum packet size was exceeded' }})
|
|
525
|
+
this.end({ reasonCode: 149, properties: { reasonString: 'Maximum packet size was exceeded' } })
|
|
403
526
|
return this
|
|
404
527
|
}
|
|
405
528
|
debug('_handlePacket :: emitting packetreceive')
|
|
@@ -424,6 +547,10 @@ MqttClient.prototype._handlePacket = function (packet, done) {
|
|
|
424
547
|
this._handleConnack(packet)
|
|
425
548
|
done()
|
|
426
549
|
break
|
|
550
|
+
case 'auth':
|
|
551
|
+
this._handleAuth(packet)
|
|
552
|
+
done()
|
|
553
|
+
break
|
|
427
554
|
case 'pingresp':
|
|
428
555
|
this._handlePingresp(packet)
|
|
429
556
|
done()
|
|
@@ -473,8 +600,7 @@ MqttClient.prototype._checkDisconnecting = function (callback) {
|
|
|
473
600
|
*/
|
|
474
601
|
MqttClient.prototype.publish = function (topic, message, opts, callback) {
|
|
475
602
|
debug('publish :: message `%s` to topic `%s`', message, topic)
|
|
476
|
-
|
|
477
|
-
var options = this.options
|
|
603
|
+
const options = this.options
|
|
478
604
|
|
|
479
605
|
// .publish(topic, payload, cb);
|
|
480
606
|
if (typeof opts === 'function') {
|
|
@@ -483,67 +609,66 @@ MqttClient.prototype.publish = function (topic, message, opts, callback) {
|
|
|
483
609
|
}
|
|
484
610
|
|
|
485
611
|
// default opts
|
|
486
|
-
|
|
612
|
+
const defaultOpts = { qos: 0, retain: false, dup: false }
|
|
487
613
|
opts = xtend(defaultOpts, opts)
|
|
488
614
|
|
|
489
615
|
if (this._checkDisconnecting(callback)) {
|
|
490
616
|
return this
|
|
491
617
|
}
|
|
492
618
|
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
619
|
+
const that = this
|
|
620
|
+
const publishProc = function () {
|
|
621
|
+
let messageId = 0
|
|
622
|
+
if (opts.qos === 1 || opts.qos === 2) {
|
|
623
|
+
messageId = that._nextId()
|
|
624
|
+
if (messageId === null) {
|
|
625
|
+
debug('No messageId left')
|
|
626
|
+
return false
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
const packet = {
|
|
630
|
+
cmd: 'publish',
|
|
631
|
+
topic: topic,
|
|
632
|
+
payload: message,
|
|
633
|
+
qos: opts.qos,
|
|
634
|
+
retain: opts.retain,
|
|
635
|
+
messageId: messageId,
|
|
636
|
+
dup: opts.dup
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
if (options.protocolVersion === 5) {
|
|
640
|
+
packet.properties = opts.properties
|
|
515
641
|
}
|
|
516
|
-
}
|
|
517
642
|
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
if (this._storeProcessing) {
|
|
528
|
-
debug('_storeProcessing enabled')
|
|
529
|
-
this._packetIdsDuringStoreProcessing[packet.messageId] = false
|
|
530
|
-
this._storePacket(packet, undefined, opts.cbStorePut)
|
|
531
|
-
} else {
|
|
643
|
+
debug('publish :: qos', opts.qos)
|
|
644
|
+
switch (opts.qos) {
|
|
645
|
+
case 1:
|
|
646
|
+
case 2:
|
|
647
|
+
// Add to callbacks
|
|
648
|
+
that.outgoing[packet.messageId] = {
|
|
649
|
+
volatile: false,
|
|
650
|
+
cb: callback || nop
|
|
651
|
+
}
|
|
532
652
|
debug('MqttClient:publish: packet cmd: %s', packet.cmd)
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
default:
|
|
537
|
-
if (this._storeProcessing) {
|
|
538
|
-
debug('_storeProcessing enabled')
|
|
539
|
-
this._storePacket(packet, callback, opts.cbStorePut)
|
|
540
|
-
} else {
|
|
653
|
+
that._sendPacket(packet, undefined, opts.cbStorePut)
|
|
654
|
+
break
|
|
655
|
+
default:
|
|
541
656
|
debug('MqttClient:publish: packet cmd: %s', packet.cmd)
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
657
|
+
that._sendPacket(packet, callback, opts.cbStorePut)
|
|
658
|
+
break
|
|
659
|
+
}
|
|
660
|
+
return true
|
|
545
661
|
}
|
|
546
662
|
|
|
663
|
+
if (this._storeProcessing || this._storeProcessingQueue.length > 0 || !publishProc()) {
|
|
664
|
+
this._storeProcessingQueue.push(
|
|
665
|
+
{
|
|
666
|
+
invoke: publishProc,
|
|
667
|
+
cbStorePut: opts.cbStorePut,
|
|
668
|
+
callback: callback
|
|
669
|
+
}
|
|
670
|
+
)
|
|
671
|
+
}
|
|
547
672
|
return this
|
|
548
673
|
}
|
|
549
674
|
|
|
@@ -564,19 +689,17 @@ MqttClient.prototype.publish = function (topic, message, opts, callback) {
|
|
|
564
689
|
* @example client.subscribe('topic', console.log);
|
|
565
690
|
*/
|
|
566
691
|
MqttClient.prototype.subscribe = function () {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
for (
|
|
692
|
+
const that = this
|
|
693
|
+
const args = new Array(arguments.length)
|
|
694
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
570
695
|
args[i] = arguments[i]
|
|
571
696
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
var that = this
|
|
579
|
-
var version = this.options.protocolVersion
|
|
697
|
+
const subs = []
|
|
698
|
+
let obj = args.shift()
|
|
699
|
+
const resubscribe = obj.resubscribe
|
|
700
|
+
let callback = args.pop() || nop
|
|
701
|
+
let opts = args.pop()
|
|
702
|
+
const version = this.options.protocolVersion
|
|
580
703
|
|
|
581
704
|
delete obj.resubscribe
|
|
582
705
|
|
|
@@ -589,7 +712,7 @@ MqttClient.prototype.subscribe = function () {
|
|
|
589
712
|
callback = nop
|
|
590
713
|
}
|
|
591
714
|
|
|
592
|
-
invalidTopic = validations.validateTopics(obj)
|
|
715
|
+
const invalidTopic = validations.validateTopics(obj)
|
|
593
716
|
if (invalidTopic !== null) {
|
|
594
717
|
setImmediate(callback, new Error('Invalid topic ' + invalidTopic))
|
|
595
718
|
return this
|
|
@@ -600,7 +723,7 @@ MqttClient.prototype.subscribe = function () {
|
|
|
600
723
|
return this
|
|
601
724
|
}
|
|
602
725
|
|
|
603
|
-
|
|
726
|
+
const defaultOpts = {
|
|
604
727
|
qos: 0
|
|
605
728
|
}
|
|
606
729
|
if (version === 5) {
|
|
@@ -613,10 +736,10 @@ MqttClient.prototype.subscribe = function () {
|
|
|
613
736
|
if (Array.isArray(obj)) {
|
|
614
737
|
obj.forEach(function (topic) {
|
|
615
738
|
debug('subscribe: array topic %s', topic)
|
|
616
|
-
if (!
|
|
739
|
+
if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, topic) ||
|
|
617
740
|
that._resubscribeTopics[topic].qos < opts.qos ||
|
|
618
741
|
resubscribe) {
|
|
619
|
-
|
|
742
|
+
const currentOpts = {
|
|
620
743
|
topic: topic,
|
|
621
744
|
qos: opts.qos
|
|
622
745
|
}
|
|
@@ -635,10 +758,10 @@ MqttClient.prototype.subscribe = function () {
|
|
|
635
758
|
.keys(obj)
|
|
636
759
|
.forEach(function (k) {
|
|
637
760
|
debug('subscribe: object topic %s', k)
|
|
638
|
-
if (!
|
|
761
|
+
if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, k) ||
|
|
639
762
|
that._resubscribeTopics[k].qos < obj[k].qos ||
|
|
640
763
|
resubscribe) {
|
|
641
|
-
|
|
764
|
+
const currentOpts = {
|
|
642
765
|
topic: k,
|
|
643
766
|
qos: obj[k].qos
|
|
644
767
|
}
|
|
@@ -654,59 +777,77 @@ MqttClient.prototype.subscribe = function () {
|
|
|
654
777
|
})
|
|
655
778
|
}
|
|
656
779
|
|
|
657
|
-
packet = {
|
|
658
|
-
cmd: 'subscribe',
|
|
659
|
-
subscriptions: subs,
|
|
660
|
-
qos: 1,
|
|
661
|
-
retain: false,
|
|
662
|
-
dup: false,
|
|
663
|
-
messageId: this._nextId()
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
if (opts.properties) {
|
|
667
|
-
packet.properties = opts.properties
|
|
668
|
-
}
|
|
669
|
-
|
|
670
780
|
if (!subs.length) {
|
|
671
781
|
callback(null, [])
|
|
672
|
-
return
|
|
782
|
+
return this
|
|
673
783
|
}
|
|
674
784
|
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
785
|
+
const subscribeProc = function () {
|
|
786
|
+
const messageId = that._nextId()
|
|
787
|
+
if (messageId === null) {
|
|
788
|
+
debug('No messageId left')
|
|
789
|
+
return false
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
const packet = {
|
|
793
|
+
cmd: 'subscribe',
|
|
794
|
+
subscriptions: subs,
|
|
795
|
+
qos: 1,
|
|
796
|
+
retain: false,
|
|
797
|
+
dup: false,
|
|
798
|
+
messageId: messageId
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
if (opts.properties) {
|
|
802
|
+
packet.properties = opts.properties
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// subscriptions to resubscribe to in case of disconnect
|
|
806
|
+
if (that.options.resubscribe) {
|
|
807
|
+
debug('subscribe :: resubscribe true')
|
|
808
|
+
const topics = []
|
|
809
|
+
subs.forEach(function (sub) {
|
|
810
|
+
if (that.options.reconnectPeriod > 0) {
|
|
811
|
+
const topic = { qos: sub.qos }
|
|
812
|
+
if (version === 5) {
|
|
813
|
+
topic.nl = sub.nl || false
|
|
814
|
+
topic.rap = sub.rap || false
|
|
815
|
+
topic.rh = sub.rh || 0
|
|
816
|
+
topic.properties = sub.properties
|
|
817
|
+
}
|
|
818
|
+
that._resubscribeTopics[sub.topic] = topic
|
|
819
|
+
topics.push(sub.topic)
|
|
687
820
|
}
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
})
|
|
692
|
-
that.messageIdToTopic[packet.messageId] = topics
|
|
693
|
-
}
|
|
821
|
+
})
|
|
822
|
+
that.messageIdToTopic[packet.messageId] = topics
|
|
823
|
+
}
|
|
694
824
|
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
825
|
+
that.outgoing[packet.messageId] = {
|
|
826
|
+
volatile: true,
|
|
827
|
+
cb: function (err, packet) {
|
|
828
|
+
if (!err) {
|
|
829
|
+
const granted = packet.granted
|
|
830
|
+
for (let i = 0; i < granted.length; i += 1) {
|
|
831
|
+
subs[i].qos = granted[i]
|
|
832
|
+
}
|
|
702
833
|
}
|
|
703
|
-
}
|
|
704
834
|
|
|
705
|
-
|
|
835
|
+
callback(err, subs)
|
|
836
|
+
}
|
|
706
837
|
}
|
|
838
|
+
debug('subscribe :: call _sendPacket')
|
|
839
|
+
that._sendPacket(packet)
|
|
840
|
+
return true
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
if (this._storeProcessing || this._storeProcessingQueue.length > 0 || !subscribeProc()) {
|
|
844
|
+
this._storeProcessingQueue.push(
|
|
845
|
+
{
|
|
846
|
+
invoke: subscribeProc,
|
|
847
|
+
callback: callback
|
|
848
|
+
}
|
|
849
|
+
)
|
|
707
850
|
}
|
|
708
|
-
debug('subscribe :: call _sendPacket')
|
|
709
|
-
this._sendPacket(packet)
|
|
710
851
|
|
|
711
852
|
return this
|
|
712
853
|
}
|
|
@@ -724,20 +865,14 @@ MqttClient.prototype.subscribe = function () {
|
|
|
724
865
|
* @example client.unsubscribe('topic', console.log);
|
|
725
866
|
*/
|
|
726
867
|
MqttClient.prototype.unsubscribe = function () {
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
messageId: this._nextId()
|
|
731
|
-
}
|
|
732
|
-
var that = this
|
|
733
|
-
var args = new Array(arguments.length)
|
|
734
|
-
for (var i = 0; i < arguments.length; i++) {
|
|
868
|
+
const that = this
|
|
869
|
+
const args = new Array(arguments.length)
|
|
870
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
735
871
|
args[i] = arguments[i]
|
|
736
872
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
873
|
+
let topic = args.shift()
|
|
874
|
+
let callback = args.pop() || nop
|
|
875
|
+
let opts = args.pop()
|
|
741
876
|
if (typeof topic === 'string') {
|
|
742
877
|
topic = [topic]
|
|
743
878
|
}
|
|
@@ -747,33 +882,63 @@ MqttClient.prototype.unsubscribe = function () {
|
|
|
747
882
|
callback = nop
|
|
748
883
|
}
|
|
749
884
|
|
|
750
|
-
|
|
885
|
+
const invalidTopic = validations.validateTopics(topic)
|
|
886
|
+
if (invalidTopic !== null) {
|
|
887
|
+
setImmediate(callback, new Error('Invalid topic ' + invalidTopic))
|
|
751
888
|
return this
|
|
752
889
|
}
|
|
753
890
|
|
|
754
|
-
if (
|
|
755
|
-
|
|
756
|
-
} else if (Array.isArray(topic)) {
|
|
757
|
-
packet.unsubscriptions = topic
|
|
891
|
+
if (that._checkDisconnecting(callback)) {
|
|
892
|
+
return this
|
|
758
893
|
}
|
|
759
894
|
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
895
|
+
const unsubscribeProc = function () {
|
|
896
|
+
const messageId = that._nextId()
|
|
897
|
+
if (messageId === null) {
|
|
898
|
+
debug('No messageId left')
|
|
899
|
+
return false
|
|
900
|
+
}
|
|
901
|
+
const packet = {
|
|
902
|
+
cmd: 'unsubscribe',
|
|
903
|
+
qos: 1,
|
|
904
|
+
messageId: messageId
|
|
905
|
+
}
|
|
765
906
|
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
907
|
+
if (typeof topic === 'string') {
|
|
908
|
+
packet.unsubscriptions = [topic]
|
|
909
|
+
} else if (Array.isArray(topic)) {
|
|
910
|
+
packet.unsubscriptions = topic
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
if (that.options.resubscribe) {
|
|
914
|
+
packet.unsubscriptions.forEach(function (topic) {
|
|
915
|
+
delete that._resubscribeTopics[topic]
|
|
916
|
+
})
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
if (typeof opts === 'object' && opts.properties) {
|
|
920
|
+
packet.properties = opts.properties
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
that.outgoing[packet.messageId] = {
|
|
924
|
+
volatile: true,
|
|
925
|
+
cb: callback
|
|
926
|
+
}
|
|
769
927
|
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
928
|
+
debug('unsubscribe: call _sendPacket')
|
|
929
|
+
that._sendPacket(packet)
|
|
930
|
+
|
|
931
|
+
return true
|
|
773
932
|
}
|
|
774
933
|
|
|
775
|
-
|
|
776
|
-
|
|
934
|
+
if (this._storeProcessing || this._storeProcessingQueue.length > 0 || !unsubscribeProc()) {
|
|
935
|
+
this._storeProcessingQueue.push(
|
|
936
|
+
{
|
|
937
|
+
invoke: unsubscribeProc,
|
|
938
|
+
callback: callback
|
|
939
|
+
}
|
|
940
|
+
)
|
|
941
|
+
}
|
|
777
942
|
|
|
778
943
|
return this
|
|
779
944
|
}
|
|
@@ -789,7 +954,7 @@ MqttClient.prototype.unsubscribe = function () {
|
|
|
789
954
|
* @api public
|
|
790
955
|
*/
|
|
791
956
|
MqttClient.prototype.end = function (force, opts, cb) {
|
|
792
|
-
|
|
957
|
+
const that = this
|
|
793
958
|
|
|
794
959
|
debug('end :: (%s)', this.options.clientId)
|
|
795
960
|
|
|
@@ -822,7 +987,7 @@ MqttClient.prototype.end = function (force, opts, cb) {
|
|
|
822
987
|
debug('end :: closeStores: emitting end')
|
|
823
988
|
that.emit('end')
|
|
824
989
|
if (cb) {
|
|
825
|
-
|
|
990
|
+
const err = e1 || e2
|
|
826
991
|
debug('end :: closeStores: invoking callback with args')
|
|
827
992
|
cb(err)
|
|
828
993
|
}
|
|
@@ -840,7 +1005,7 @@ MqttClient.prototype.end = function (force, opts, cb) {
|
|
|
840
1005
|
debug('end :: (%s) :: finish :: calling _cleanUp with force %s', that.options.clientId, force)
|
|
841
1006
|
that._cleanUp(force, () => {
|
|
842
1007
|
debug('end :: finish :: calling process.nextTick on closeStores')
|
|
843
|
-
//
|
|
1008
|
+
// const boundProcess = nextTick.bind(null, closeStores)
|
|
844
1009
|
nextTick(closeStores.bind(that))
|
|
845
1010
|
}, opts)
|
|
846
1011
|
}
|
|
@@ -874,12 +1039,12 @@ MqttClient.prototype.end = function (force, opts, cb) {
|
|
|
874
1039
|
* @returns {MqttClient} this - for chaining
|
|
875
1040
|
* @api public
|
|
876
1041
|
*
|
|
877
|
-
* @example client.removeOutgoingMessage(client.
|
|
1042
|
+
* @example client.removeOutgoingMessage(client.getLastAllocated());
|
|
878
1043
|
*/
|
|
879
1044
|
MqttClient.prototype.removeOutgoingMessage = function (messageId) {
|
|
880
|
-
|
|
1045
|
+
const cb = this.outgoing[messageId] ? this.outgoing[messageId].cb : null
|
|
881
1046
|
delete this.outgoing[messageId]
|
|
882
|
-
this.outgoingStore.del({messageId: messageId}, function () {
|
|
1047
|
+
this.outgoingStore.del({ messageId: messageId }, function () {
|
|
883
1048
|
cb(new Error('Message removed'))
|
|
884
1049
|
})
|
|
885
1050
|
return this
|
|
@@ -898,8 +1063,8 @@ MqttClient.prototype.removeOutgoingMessage = function (messageId) {
|
|
|
898
1063
|
*/
|
|
899
1064
|
MqttClient.prototype.reconnect = function (opts) {
|
|
900
1065
|
debug('client reconnect')
|
|
901
|
-
|
|
902
|
-
|
|
1066
|
+
const that = this
|
|
1067
|
+
const f = function () {
|
|
903
1068
|
if (opts) {
|
|
904
1069
|
that.options.incomingStore = opts.incomingStore
|
|
905
1070
|
that.options.outgoingStore = opts.outgoingStore
|
|
@@ -943,7 +1108,7 @@ MqttClient.prototype._reconnect = function () {
|
|
|
943
1108
|
* _setupReconnect - setup reconnect timer
|
|
944
1109
|
*/
|
|
945
1110
|
MqttClient.prototype._setupReconnect = function () {
|
|
946
|
-
|
|
1111
|
+
const that = this
|
|
947
1112
|
|
|
948
1113
|
if (!that.disconnecting && !that.reconnectTimer && (that.options.reconnectPeriod > 0)) {
|
|
949
1114
|
if (!this.reconnecting) {
|
|
@@ -978,7 +1143,7 @@ MqttClient.prototype._clearReconnect = function () {
|
|
|
978
1143
|
* @api private
|
|
979
1144
|
*/
|
|
980
1145
|
MqttClient.prototype._cleanUp = function (forced, done) {
|
|
981
|
-
|
|
1146
|
+
const opts = arguments[2]
|
|
982
1147
|
if (done) {
|
|
983
1148
|
debug('_cleanUp :: done callback provided for on stream close')
|
|
984
1149
|
this.stream.on('close', done)
|
|
@@ -992,7 +1157,7 @@ MqttClient.prototype._cleanUp = function (forced, done) {
|
|
|
992
1157
|
debug('_cleanUp :: (%s) :: destroying stream', this.options.clientId)
|
|
993
1158
|
this.stream.destroy()
|
|
994
1159
|
} else {
|
|
995
|
-
|
|
1160
|
+
const packet = xtend({ cmd: 'disconnect' }, opts)
|
|
996
1161
|
debug('_cleanUp :: (%s) :: call _sendPacket with disconnect packet', this.options.clientId)
|
|
997
1162
|
this._sendPacket(
|
|
998
1163
|
packet,
|
|
@@ -1032,8 +1197,22 @@ MqttClient.prototype._cleanUp = function (forced, done) {
|
|
|
1032
1197
|
MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
|
|
1033
1198
|
debug('_sendPacket :: (%s) :: start', this.options.clientId)
|
|
1034
1199
|
cbStorePut = cbStorePut || nop
|
|
1200
|
+
cb = cb || nop
|
|
1201
|
+
|
|
1202
|
+
const err = applyTopicAlias(this, packet)
|
|
1203
|
+
if (err) {
|
|
1204
|
+
cb(err)
|
|
1205
|
+
return
|
|
1206
|
+
}
|
|
1035
1207
|
|
|
1036
1208
|
if (!this.connected) {
|
|
1209
|
+
// allow auth packets to be sent while authenticating with the broker (mqtt5 enhanced auth)
|
|
1210
|
+
if (packet.cmd === 'auth') {
|
|
1211
|
+
this._shiftPingInterval()
|
|
1212
|
+
sendPacket(this, packet, cb)
|
|
1213
|
+
return
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1037
1216
|
debug('_sendPacket :: client not connected. Storing packet offline.')
|
|
1038
1217
|
this._storePacket(packet, cb, cbStorePut)
|
|
1039
1218
|
return
|
|
@@ -1084,12 +1263,23 @@ MqttClient.prototype._storePacket = function (packet, cb, cbStorePut) {
|
|
|
1084
1263
|
debug('_storePacket :: cb? %s', !!cb)
|
|
1085
1264
|
cbStorePut = cbStorePut || nop
|
|
1086
1265
|
|
|
1266
|
+
let storePacket = packet
|
|
1267
|
+
if (storePacket.cmd === 'publish') {
|
|
1268
|
+
// The original packet is for sending.
|
|
1269
|
+
// The cloned storePacket is for storing to resend on reconnect.
|
|
1270
|
+
// Topic Alias must not be used after disconnected.
|
|
1271
|
+
storePacket = clone(packet)
|
|
1272
|
+
const err = removeTopicAliasAndRecoverTopicName(this, storePacket)
|
|
1273
|
+
if (err) {
|
|
1274
|
+
return cb && cb(err)
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1087
1277
|
// check that the packet is not a qos of 0, or that the command is not a publish
|
|
1088
|
-
if (((
|
|
1089
|
-
this.queue.push({ packet:
|
|
1090
|
-
} else if (
|
|
1091
|
-
cb = this.outgoing[
|
|
1092
|
-
this.outgoingStore.put(
|
|
1278
|
+
if (((storePacket.qos || 0) === 0 && this.queueQoSZero) || storePacket.cmd !== 'publish') {
|
|
1279
|
+
this.queue.push({ packet: storePacket, cb: cb })
|
|
1280
|
+
} else if (storePacket.qos > 0) {
|
|
1281
|
+
cb = this.outgoing[storePacket.messageId] ? this.outgoing[storePacket.messageId].cb : null
|
|
1282
|
+
this.outgoingStore.put(storePacket, function (err) {
|
|
1093
1283
|
if (err) {
|
|
1094
1284
|
return cb && cb(err)
|
|
1095
1285
|
}
|
|
@@ -1107,7 +1297,7 @@ MqttClient.prototype._storePacket = function (packet, cb, cbStorePut) {
|
|
|
1107
1297
|
*/
|
|
1108
1298
|
MqttClient.prototype._setupPingTimer = function () {
|
|
1109
1299
|
debug('_setupPingTimer :: keepalive %d (seconds)', this.options.keepalive)
|
|
1110
|
-
|
|
1300
|
+
const that = this
|
|
1111
1301
|
|
|
1112
1302
|
if (!this.pingTimer && this.options.keepalive) {
|
|
1113
1303
|
this.pingResp = true
|
|
@@ -1162,16 +1352,22 @@ MqttClient.prototype._handlePingresp = function () {
|
|
|
1162
1352
|
*/
|
|
1163
1353
|
MqttClient.prototype._handleConnack = function (packet) {
|
|
1164
1354
|
debug('_handleConnack')
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1355
|
+
const options = this.options
|
|
1356
|
+
const version = options.protocolVersion
|
|
1357
|
+
const rc = version === 5 ? packet.reasonCode : packet.returnCode
|
|
1168
1358
|
|
|
1169
1359
|
clearTimeout(this.connackTimer)
|
|
1360
|
+
delete this.topicAliasSend
|
|
1170
1361
|
|
|
1171
1362
|
if (packet.properties) {
|
|
1172
1363
|
if (packet.properties.topicAliasMaximum) {
|
|
1173
|
-
if (
|
|
1174
|
-
|
|
1364
|
+
if (packet.properties.topicAliasMaximum > 0xffff) {
|
|
1365
|
+
this.emit('error', new Error('topicAliasMaximum from broker is out of range'))
|
|
1366
|
+
return
|
|
1367
|
+
}
|
|
1368
|
+
if (packet.properties.topicAliasMaximum > 0) {
|
|
1369
|
+
this.topicAliasSend = new TopicAliasSend(packet.properties.topicAliasMaximum)
|
|
1370
|
+
}
|
|
1175
1371
|
}
|
|
1176
1372
|
if (packet.properties.serverKeepAlive && options.keepalive) {
|
|
1177
1373
|
options.keepalive = packet.properties.serverKeepAlive
|
|
@@ -1187,12 +1383,51 @@ MqttClient.prototype._handleConnack = function (packet) {
|
|
|
1187
1383
|
this.reconnecting = false
|
|
1188
1384
|
this._onConnect(packet)
|
|
1189
1385
|
} else if (rc > 0) {
|
|
1190
|
-
|
|
1386
|
+
const err = new Error('Connection refused: ' + errors[rc])
|
|
1191
1387
|
err.code = rc
|
|
1192
1388
|
this.emit('error', err)
|
|
1193
1389
|
}
|
|
1194
1390
|
}
|
|
1195
1391
|
|
|
1392
|
+
MqttClient.prototype._handleAuth = function (packet) {
|
|
1393
|
+
const options = this.options
|
|
1394
|
+
const version = options.protocolVersion
|
|
1395
|
+
const rc = version === 5 ? packet.reasonCode : packet.returnCode
|
|
1396
|
+
|
|
1397
|
+
if (version !== 5) {
|
|
1398
|
+
const err = new Error('Protocol error: Auth packets are only supported in MQTT 5. Your version:' + version)
|
|
1399
|
+
err.code = rc
|
|
1400
|
+
this.emit('error', err)
|
|
1401
|
+
return
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
const that = this
|
|
1405
|
+
this.handleAuth(packet, function (err, packet) {
|
|
1406
|
+
if (err) {
|
|
1407
|
+
that.emit('error', err)
|
|
1408
|
+
return
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
if (rc === 24) {
|
|
1412
|
+
that.reconnecting = false
|
|
1413
|
+
that._sendPacket(packet)
|
|
1414
|
+
} else {
|
|
1415
|
+
const error = new Error('Connection refused: ' + errors[rc])
|
|
1416
|
+
err.code = rc
|
|
1417
|
+
that.emit('error', error)
|
|
1418
|
+
}
|
|
1419
|
+
})
|
|
1420
|
+
}
|
|
1421
|
+
|
|
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
|
+
MqttClient.prototype.handleAuth = function (packet, callback) {
|
|
1428
|
+
callback()
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1196
1431
|
/**
|
|
1197
1432
|
* _handlePublish
|
|
1198
1433
|
*
|
|
@@ -1226,13 +1461,46 @@ for now i just suppressed the warnings
|
|
|
1226
1461
|
MqttClient.prototype._handlePublish = function (packet, done) {
|
|
1227
1462
|
debug('_handlePublish: packet %o', packet)
|
|
1228
1463
|
done = typeof done !== 'undefined' ? done : nop
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1464
|
+
let topic = packet.topic.toString()
|
|
1465
|
+
const message = packet.payload
|
|
1466
|
+
const qos = packet.qos
|
|
1467
|
+
const messageId = packet.messageId
|
|
1468
|
+
const that = this
|
|
1469
|
+
const options = this.options
|
|
1470
|
+
const validReasonCodes = [0, 16, 128, 131, 135, 144, 145, 151, 153]
|
|
1471
|
+
if (this.options.protocolVersion === 5) {
|
|
1472
|
+
let alias
|
|
1473
|
+
if (packet.properties) {
|
|
1474
|
+
alias = packet.properties.topicAlias
|
|
1475
|
+
}
|
|
1476
|
+
if (typeof alias !== 'undefined') {
|
|
1477
|
+
if (topic.length === 0) {
|
|
1478
|
+
if (alias > 0 && alias <= 0xffff) {
|
|
1479
|
+
const gotTopic = this.topicAliasRecv.getTopicByAlias(alias)
|
|
1480
|
+
if (gotTopic) {
|
|
1481
|
+
topic = gotTopic
|
|
1482
|
+
debug('_handlePublish :: topic complemented by alias. topic: %s - alias: %d', topic, alias)
|
|
1483
|
+
} else {
|
|
1484
|
+
debug('_handlePublish :: unregistered topic alias. alias: %d', alias)
|
|
1485
|
+
this.emit('error', new Error('Received unregistered Topic Alias'))
|
|
1486
|
+
return
|
|
1487
|
+
}
|
|
1488
|
+
} else {
|
|
1489
|
+
debug('_handlePublish :: topic alias out of range. alias: %d', alias)
|
|
1490
|
+
this.emit('error', new Error('Received Topic Alias is out of range'))
|
|
1491
|
+
return
|
|
1492
|
+
}
|
|
1493
|
+
} else {
|
|
1494
|
+
if (this.topicAliasRecv.put(topic, alias)) {
|
|
1495
|
+
debug('_handlePublish :: registered topic: %s - alias: %d', topic, alias)
|
|
1496
|
+
} else {
|
|
1497
|
+
debug('_handlePublish :: topic alias out of range. alias: %d', alias)
|
|
1498
|
+
this.emit('error', new Error('Received Topic Alias is out of range'))
|
|
1499
|
+
return
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1236
1504
|
debug('_handlePublish: qos %d', qos)
|
|
1237
1505
|
switch (qos) {
|
|
1238
1506
|
case 2: {
|
|
@@ -1244,10 +1512,10 @@ MqttClient.prototype._handlePublish = function (packet, done) {
|
|
|
1244
1512
|
if (error) { return that.emit('error', error) }
|
|
1245
1513
|
if (validReasonCodes.indexOf(code) === -1) { return that.emit('error', new Error('Wrong reason code for pubrec')) }
|
|
1246
1514
|
if (code) {
|
|
1247
|
-
that._sendPacket({cmd: 'pubrec', messageId: messageId, reasonCode: code}, done)
|
|
1515
|
+
that._sendPacket({ cmd: 'pubrec', messageId: messageId, reasonCode: code }, done)
|
|
1248
1516
|
} else {
|
|
1249
1517
|
that.incomingStore.put(packet, function () {
|
|
1250
|
-
that._sendPacket({cmd: 'pubrec', messageId: messageId}, done)
|
|
1518
|
+
that._sendPacket({ cmd: 'pubrec', messageId: messageId }, done)
|
|
1251
1519
|
})
|
|
1252
1520
|
}
|
|
1253
1521
|
})
|
|
@@ -1267,7 +1535,7 @@ MqttClient.prototype._handlePublish = function (packet, done) {
|
|
|
1267
1535
|
if (err) {
|
|
1268
1536
|
return done && done(err)
|
|
1269
1537
|
}
|
|
1270
|
-
that._sendPacket({cmd: 'puback', messageId: messageId, reasonCode: code}, done)
|
|
1538
|
+
that._sendPacket({ cmd: 'puback', messageId: messageId, reasonCode: code }, done)
|
|
1271
1539
|
})
|
|
1272
1540
|
})
|
|
1273
1541
|
break
|
|
@@ -1306,12 +1574,12 @@ MqttClient.prototype.handleMessage = function (packet, callback) {
|
|
|
1306
1574
|
|
|
1307
1575
|
MqttClient.prototype._handleAck = function (packet) {
|
|
1308
1576
|
/* eslint no-fallthrough: "off" */
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1577
|
+
const messageId = packet.messageId
|
|
1578
|
+
const type = packet.cmd
|
|
1579
|
+
let response = null
|
|
1580
|
+
const cb = this.outgoing[messageId] ? this.outgoing[messageId].cb : null
|
|
1581
|
+
const that = this
|
|
1582
|
+
let err
|
|
1315
1583
|
|
|
1316
1584
|
if (!cb) {
|
|
1317
1585
|
debug('_handleAck :: Server sent an ack in error. Ignoring.')
|
|
@@ -1324,8 +1592,8 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1324
1592
|
switch (type) {
|
|
1325
1593
|
case 'pubcomp':
|
|
1326
1594
|
// same thing as puback for QoS 2
|
|
1327
|
-
case 'puback':
|
|
1328
|
-
|
|
1595
|
+
case 'puback': {
|
|
1596
|
+
const pubackRC = packet.reasonCode
|
|
1329
1597
|
// Callback - we're done
|
|
1330
1598
|
if (pubackRC && pubackRC > 0 && pubackRC !== 16) {
|
|
1331
1599
|
err = new Error('Publish error: ' + errors[pubackRC])
|
|
@@ -1334,14 +1602,17 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1334
1602
|
}
|
|
1335
1603
|
delete this.outgoing[messageId]
|
|
1336
1604
|
this.outgoingStore.del(packet, cb)
|
|
1605
|
+
this.messageIdProvider.deallocate(messageId)
|
|
1606
|
+
this._invokeStoreProcessingQueue()
|
|
1337
1607
|
break
|
|
1338
|
-
|
|
1608
|
+
}
|
|
1609
|
+
case 'pubrec': {
|
|
1339
1610
|
response = {
|
|
1340
1611
|
cmd: 'pubrel',
|
|
1341
1612
|
qos: 2,
|
|
1342
1613
|
messageId: messageId
|
|
1343
1614
|
}
|
|
1344
|
-
|
|
1615
|
+
const pubrecRC = packet.reasonCode
|
|
1345
1616
|
|
|
1346
1617
|
if (pubrecRC && pubrecRC > 0 && pubrecRC !== 16) {
|
|
1347
1618
|
err = new Error('Publish error: ' + errors[pubrecRC])
|
|
@@ -1351,12 +1622,14 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1351
1622
|
this._sendPacket(response)
|
|
1352
1623
|
}
|
|
1353
1624
|
break
|
|
1354
|
-
|
|
1625
|
+
}
|
|
1626
|
+
case 'suback': {
|
|
1355
1627
|
delete this.outgoing[messageId]
|
|
1356
|
-
|
|
1628
|
+
this.messageIdProvider.deallocate(messageId)
|
|
1629
|
+
for (let grantedI = 0; grantedI < packet.granted.length; grantedI++) {
|
|
1357
1630
|
if ((packet.granted[grantedI] & 0x80) !== 0) {
|
|
1358
1631
|
// suback with Failure status
|
|
1359
|
-
|
|
1632
|
+
const topics = this.messageIdToTopic[messageId]
|
|
1360
1633
|
if (topics) {
|
|
1361
1634
|
topics.forEach(function (topic) {
|
|
1362
1635
|
delete that._resubscribeTopics[topic]
|
|
@@ -1364,12 +1637,17 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1364
1637
|
}
|
|
1365
1638
|
}
|
|
1366
1639
|
}
|
|
1640
|
+
this._invokeStoreProcessingQueue()
|
|
1367
1641
|
cb(null, packet)
|
|
1368
1642
|
break
|
|
1369
|
-
|
|
1643
|
+
}
|
|
1644
|
+
case 'unsuback': {
|
|
1370
1645
|
delete this.outgoing[messageId]
|
|
1646
|
+
this.messageIdProvider.deallocate(messageId)
|
|
1647
|
+
this._invokeStoreProcessingQueue()
|
|
1371
1648
|
cb(null)
|
|
1372
1649
|
break
|
|
1650
|
+
}
|
|
1373
1651
|
default:
|
|
1374
1652
|
that.emit('error', new Error('unrecognized packet type'))
|
|
1375
1653
|
}
|
|
@@ -1389,10 +1667,10 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1389
1667
|
MqttClient.prototype._handlePubrel = function (packet, callback) {
|
|
1390
1668
|
debug('handling pubrel packet')
|
|
1391
1669
|
callback = typeof callback !== 'undefined' ? callback : nop
|
|
1392
|
-
|
|
1393
|
-
|
|
1670
|
+
const messageId = packet.messageId
|
|
1671
|
+
const that = this
|
|
1394
1672
|
|
|
1395
|
-
|
|
1673
|
+
const comp = { cmd: 'pubcomp', messageId: messageId }
|
|
1396
1674
|
|
|
1397
1675
|
that.incomingStore.get(packet, function (err, pub) {
|
|
1398
1676
|
if (!err) {
|
|
@@ -1425,13 +1703,7 @@ MqttClient.prototype._handleDisconnect = function (packet) {
|
|
|
1425
1703
|
* @return unsigned int
|
|
1426
1704
|
*/
|
|
1427
1705
|
MqttClient.prototype._nextId = function () {
|
|
1428
|
-
|
|
1429
|
-
var id = this.nextId++
|
|
1430
|
-
// Ensure 16 bit unsigned int (max 65535, nextId got one higher)
|
|
1431
|
-
if (this.nextId === 65536) {
|
|
1432
|
-
this.nextId = 1
|
|
1433
|
-
}
|
|
1434
|
-
return id
|
|
1706
|
+
return this.messageIdProvider.allocate()
|
|
1435
1707
|
}
|
|
1436
1708
|
|
|
1437
1709
|
/**
|
|
@@ -1439,27 +1711,27 @@ MqttClient.prototype._nextId = function () {
|
|
|
1439
1711
|
* @return unsigned int
|
|
1440
1712
|
*/
|
|
1441
1713
|
MqttClient.prototype.getLastMessageId = function () {
|
|
1442
|
-
return
|
|
1714
|
+
return this.messageIdProvider.getLastAllocated()
|
|
1443
1715
|
}
|
|
1444
1716
|
|
|
1445
1717
|
/**
|
|
1446
1718
|
* _resubscribe
|
|
1447
1719
|
* @api private
|
|
1448
1720
|
*/
|
|
1449
|
-
MqttClient.prototype._resubscribe = function (
|
|
1721
|
+
MqttClient.prototype._resubscribe = function () {
|
|
1450
1722
|
debug('_resubscribe')
|
|
1451
|
-
|
|
1723
|
+
const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics)
|
|
1452
1724
|
if (!this._firstConnection &&
|
|
1453
|
-
(this.options.clean || (this.options.protocolVersion === 5 && !
|
|
1725
|
+
(this.options.clean || (this.options.protocolVersion === 5 && !this.connackPacket.sessionPresent)) &&
|
|
1454
1726
|
_resubscribeTopicsKeys.length > 0) {
|
|
1455
1727
|
if (this.options.resubscribe) {
|
|
1456
1728
|
if (this.options.protocolVersion === 5) {
|
|
1457
1729
|
debug('_resubscribe: protocolVersion 5')
|
|
1458
|
-
for (
|
|
1459
|
-
|
|
1730
|
+
for (let topicI = 0; topicI < _resubscribeTopicsKeys.length; topicI++) {
|
|
1731
|
+
const resubscribeTopic = {}
|
|
1460
1732
|
resubscribeTopic[_resubscribeTopicsKeys[topicI]] = this._resubscribeTopics[_resubscribeTopicsKeys[topicI]]
|
|
1461
1733
|
resubscribeTopic.resubscribe = true
|
|
1462
|
-
this.subscribe(resubscribeTopic, {properties: resubscribeTopic[_resubscribeTopicsKeys[topicI]].properties})
|
|
1734
|
+
this.subscribe(resubscribeTopic, { properties: resubscribeTopic[_resubscribeTopicsKeys[topicI]].properties })
|
|
1463
1735
|
}
|
|
1464
1736
|
} else {
|
|
1465
1737
|
this._resubscribeTopics.resubscribe = true
|
|
@@ -1484,15 +1756,16 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1484
1756
|
return
|
|
1485
1757
|
}
|
|
1486
1758
|
|
|
1487
|
-
|
|
1759
|
+
const that = this
|
|
1488
1760
|
|
|
1761
|
+
this.connackPacket = packet
|
|
1762
|
+
this.messageIdProvider.clear()
|
|
1489
1763
|
this._setupPingTimer()
|
|
1490
|
-
this._resubscribe(packet)
|
|
1491
1764
|
|
|
1492
1765
|
this.connected = true
|
|
1493
1766
|
|
|
1494
1767
|
function startStreamProcess () {
|
|
1495
|
-
|
|
1768
|
+
let outStore = that.outgoingStore.createStream()
|
|
1496
1769
|
|
|
1497
1770
|
function clearStoreProcessing () {
|
|
1498
1771
|
that._storeProcessing = false
|
|
@@ -1502,6 +1775,7 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1502
1775
|
that.once('close', remove)
|
|
1503
1776
|
outStore.on('error', function (err) {
|
|
1504
1777
|
clearStoreProcessing()
|
|
1778
|
+
that._flushStoreProcessingQueue()
|
|
1505
1779
|
that.removeListener('close', remove)
|
|
1506
1780
|
that.emit('error', err)
|
|
1507
1781
|
})
|
|
@@ -1509,6 +1783,7 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1509
1783
|
function remove () {
|
|
1510
1784
|
outStore.destroy()
|
|
1511
1785
|
outStore = null
|
|
1786
|
+
that._flushStoreProcessingQueue()
|
|
1512
1787
|
clearStoreProcessing()
|
|
1513
1788
|
}
|
|
1514
1789
|
|
|
@@ -1519,9 +1794,9 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1519
1794
|
}
|
|
1520
1795
|
that._storeProcessing = true
|
|
1521
1796
|
|
|
1522
|
-
|
|
1797
|
+
const packet = outStore.read(1)
|
|
1523
1798
|
|
|
1524
|
-
|
|
1799
|
+
let cb
|
|
1525
1800
|
|
|
1526
1801
|
if (!packet) {
|
|
1527
1802
|
// read when data is available in the future
|
|
@@ -1550,15 +1825,19 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1550
1825
|
}
|
|
1551
1826
|
}
|
|
1552
1827
|
that._packetIdsDuringStoreProcessing[packet.messageId] = true
|
|
1553
|
-
that.
|
|
1828
|
+
if (that.messageIdProvider.register(packet.messageId)) {
|
|
1829
|
+
that._sendPacket(packet)
|
|
1830
|
+
} else {
|
|
1831
|
+
debug('messageId: %d has already used.', packet.messageId)
|
|
1832
|
+
}
|
|
1554
1833
|
} else if (outStore.destroy) {
|
|
1555
1834
|
outStore.destroy()
|
|
1556
1835
|
}
|
|
1557
1836
|
}
|
|
1558
1837
|
|
|
1559
1838
|
outStore.on('end', function () {
|
|
1560
|
-
|
|
1561
|
-
for (
|
|
1839
|
+
let allProcessed = true
|
|
1840
|
+
for (const id in that._packetIdsDuringStoreProcessing) {
|
|
1562
1841
|
if (!that._packetIdsDuringStoreProcessing[id]) {
|
|
1563
1842
|
allProcessed = false
|
|
1564
1843
|
break
|
|
@@ -1567,6 +1846,7 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1567
1846
|
if (allProcessed) {
|
|
1568
1847
|
clearStoreProcessing()
|
|
1569
1848
|
that.removeListener('close', remove)
|
|
1849
|
+
that._invokeAllStoreProcessingQueue()
|
|
1570
1850
|
that.emit('connect', packet)
|
|
1571
1851
|
} else {
|
|
1572
1852
|
startStreamProcess()
|
|
@@ -1578,4 +1858,27 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1578
1858
|
startStreamProcess()
|
|
1579
1859
|
}
|
|
1580
1860
|
|
|
1861
|
+
MqttClient.prototype._invokeStoreProcessingQueue = function () {
|
|
1862
|
+
if (this._storeProcessingQueue.length > 0) {
|
|
1863
|
+
const f = this._storeProcessingQueue[0]
|
|
1864
|
+
if (f && f.invoke()) {
|
|
1865
|
+
this._storeProcessingQueue.shift()
|
|
1866
|
+
return true
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
return false
|
|
1870
|
+
}
|
|
1871
|
+
|
|
1872
|
+
MqttClient.prototype._invokeAllStoreProcessingQueue = function () {
|
|
1873
|
+
while (this._invokeStoreProcessingQueue()) { /* empty */ }
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
MqttClient.prototype._flushStoreProcessingQueue = function () {
|
|
1877
|
+
for (const f of this._storeProcessingQueue) {
|
|
1878
|
+
if (f.cbStorePut) f.cbStorePut(new Error('Connection closed'))
|
|
1879
|
+
if (f.callback) f.callback(new Error('Connection closed'))
|
|
1880
|
+
}
|
|
1881
|
+
this._storeProcessingQueue.splice(0)
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1581
1884
|
module.exports = MqttClient
|