mqtt 4.2.5 → 4.3.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/CHANGELOG.md +63 -0
- package/README.md +108 -49
- package/bin/mqtt.js +3 -3
- package/bin/pub.js +12 -12
- package/bin/sub.js +6 -6
- package/dist/mqtt.js +13015 -5878
- package/dist/mqtt.min.js +1 -1
- package/lib/client.js +569 -260
- package/lib/connect/ali.js +14 -14
- package/lib/connect/index.js +18 -30
- package/lib/connect/tcp.js +4 -5
- package/lib/connect/tls.js +8 -5
- package/lib/connect/ws.js +16 -15
- package/lib/connect/wx.js +11 -13
- package/lib/default-message-id-provider.js +69 -0
- package/lib/store.js +9 -9
- 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 +3 -3
- package/mqtt.js +7 -3
- package/package.json +26 -10
- package/types/index.d.ts +4 -1
- package/types/lib/client-options.d.ts +24 -11
- package/types/lib/client.d.ts +33 -4
- package/types/lib/default-message-id-provider.d.ts +49 -0
- package/types/lib/message-id-provider.d.ts +40 -0
- 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,68 @@ 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
|
-
server don`t give topic alias maximum,
|
|
512
|
-
we are removing topic alias from packet
|
|
513
|
-
*/
|
|
514
|
-
delete packet.properties.topicAlias
|
|
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
|
|
515
637
|
}
|
|
516
|
-
}
|
|
517
638
|
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
} else {
|
|
639
|
+
if (options.protocolVersion === 5) {
|
|
640
|
+
packet.properties = opts.properties
|
|
641
|
+
}
|
|
642
|
+
|
|
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) {
|
|
664
|
+
this._storeProcessingQueue.push(
|
|
665
|
+
{
|
|
666
|
+
invoke: publishProc,
|
|
667
|
+
cbStorePut: opts.cbStorePut,
|
|
668
|
+
callback: callback
|
|
669
|
+
}
|
|
670
|
+
)
|
|
671
|
+
} else {
|
|
672
|
+
publishProc()
|
|
673
|
+
}
|
|
547
674
|
return this
|
|
548
675
|
}
|
|
549
676
|
|
|
@@ -564,19 +691,17 @@ MqttClient.prototype.publish = function (topic, message, opts, callback) {
|
|
|
564
691
|
* @example client.subscribe('topic', console.log);
|
|
565
692
|
*/
|
|
566
693
|
MqttClient.prototype.subscribe = function () {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
for (
|
|
694
|
+
const that = this
|
|
695
|
+
const args = new Array(arguments.length)
|
|
696
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
570
697
|
args[i] = arguments[i]
|
|
571
698
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
var that = this
|
|
579
|
-
var version = this.options.protocolVersion
|
|
699
|
+
const subs = []
|
|
700
|
+
let obj = args.shift()
|
|
701
|
+
const resubscribe = obj.resubscribe
|
|
702
|
+
let callback = args.pop() || nop
|
|
703
|
+
let opts = args.pop()
|
|
704
|
+
const version = this.options.protocolVersion
|
|
580
705
|
|
|
581
706
|
delete obj.resubscribe
|
|
582
707
|
|
|
@@ -589,7 +714,7 @@ MqttClient.prototype.subscribe = function () {
|
|
|
589
714
|
callback = nop
|
|
590
715
|
}
|
|
591
716
|
|
|
592
|
-
invalidTopic = validations.validateTopics(obj)
|
|
717
|
+
const invalidTopic = validations.validateTopics(obj)
|
|
593
718
|
if (invalidTopic !== null) {
|
|
594
719
|
setImmediate(callback, new Error('Invalid topic ' + invalidTopic))
|
|
595
720
|
return this
|
|
@@ -600,7 +725,7 @@ MqttClient.prototype.subscribe = function () {
|
|
|
600
725
|
return this
|
|
601
726
|
}
|
|
602
727
|
|
|
603
|
-
|
|
728
|
+
const defaultOpts = {
|
|
604
729
|
qos: 0
|
|
605
730
|
}
|
|
606
731
|
if (version === 5) {
|
|
@@ -613,10 +738,10 @@ MqttClient.prototype.subscribe = function () {
|
|
|
613
738
|
if (Array.isArray(obj)) {
|
|
614
739
|
obj.forEach(function (topic) {
|
|
615
740
|
debug('subscribe: array topic %s', topic)
|
|
616
|
-
if (!
|
|
741
|
+
if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, topic) ||
|
|
617
742
|
that._resubscribeTopics[topic].qos < opts.qos ||
|
|
618
743
|
resubscribe) {
|
|
619
|
-
|
|
744
|
+
const currentOpts = {
|
|
620
745
|
topic: topic,
|
|
621
746
|
qos: opts.qos
|
|
622
747
|
}
|
|
@@ -635,10 +760,10 @@ MqttClient.prototype.subscribe = function () {
|
|
|
635
760
|
.keys(obj)
|
|
636
761
|
.forEach(function (k) {
|
|
637
762
|
debug('subscribe: object topic %s', k)
|
|
638
|
-
if (!
|
|
763
|
+
if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, k) ||
|
|
639
764
|
that._resubscribeTopics[k].qos < obj[k].qos ||
|
|
640
765
|
resubscribe) {
|
|
641
|
-
|
|
766
|
+
const currentOpts = {
|
|
642
767
|
topic: k,
|
|
643
768
|
qos: obj[k].qos
|
|
644
769
|
}
|
|
@@ -654,59 +779,79 @@ MqttClient.prototype.subscribe = function () {
|
|
|
654
779
|
})
|
|
655
780
|
}
|
|
656
781
|
|
|
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
782
|
if (!subs.length) {
|
|
671
783
|
callback(null, [])
|
|
672
|
-
return
|
|
784
|
+
return this
|
|
673
785
|
}
|
|
674
786
|
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
787
|
+
const subscribeProc = function () {
|
|
788
|
+
const messageId = that._nextId()
|
|
789
|
+
if (messageId === null) {
|
|
790
|
+
debug('No messageId left')
|
|
791
|
+
return false
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
const packet = {
|
|
795
|
+
cmd: 'subscribe',
|
|
796
|
+
subscriptions: subs,
|
|
797
|
+
qos: 1,
|
|
798
|
+
retain: false,
|
|
799
|
+
dup: false,
|
|
800
|
+
messageId: messageId
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
if (opts.properties) {
|
|
804
|
+
packet.properties = opts.properties
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// subscriptions to resubscribe to in case of disconnect
|
|
808
|
+
if (that.options.resubscribe) {
|
|
809
|
+
debug('subscribe :: resubscribe true')
|
|
810
|
+
const topics = []
|
|
811
|
+
subs.forEach(function (sub) {
|
|
812
|
+
if (that.options.reconnectPeriod > 0) {
|
|
813
|
+
const topic = { qos: sub.qos }
|
|
814
|
+
if (version === 5) {
|
|
815
|
+
topic.nl = sub.nl || false
|
|
816
|
+
topic.rap = sub.rap || false
|
|
817
|
+
topic.rh = sub.rh || 0
|
|
818
|
+
topic.properties = sub.properties
|
|
819
|
+
}
|
|
820
|
+
that._resubscribeTopics[sub.topic] = topic
|
|
821
|
+
topics.push(sub.topic)
|
|
687
822
|
}
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
})
|
|
692
|
-
that.messageIdToTopic[packet.messageId] = topics
|
|
693
|
-
}
|
|
823
|
+
})
|
|
824
|
+
that.messageIdToTopic[packet.messageId] = topics
|
|
825
|
+
}
|
|
694
826
|
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
827
|
+
that.outgoing[packet.messageId] = {
|
|
828
|
+
volatile: true,
|
|
829
|
+
cb: function (err, packet) {
|
|
830
|
+
if (!err) {
|
|
831
|
+
const granted = packet.granted
|
|
832
|
+
for (let i = 0; i < granted.length; i += 1) {
|
|
833
|
+
subs[i].qos = granted[i]
|
|
834
|
+
}
|
|
702
835
|
}
|
|
703
|
-
}
|
|
704
836
|
|
|
705
|
-
|
|
837
|
+
callback(err, subs)
|
|
838
|
+
}
|
|
706
839
|
}
|
|
840
|
+
debug('subscribe :: call _sendPacket')
|
|
841
|
+
that._sendPacket(packet)
|
|
842
|
+
return true
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
if (this._storeProcessing || this._storeProcessingQueue.length > 0) {
|
|
846
|
+
this._storeProcessingQueue.push(
|
|
847
|
+
{
|
|
848
|
+
invoke: subscribeProc,
|
|
849
|
+
callback: callback
|
|
850
|
+
}
|
|
851
|
+
)
|
|
852
|
+
} else {
|
|
853
|
+
subscribeProc()
|
|
707
854
|
}
|
|
708
|
-
debug('subscribe :: call _sendPacket')
|
|
709
|
-
this._sendPacket(packet)
|
|
710
855
|
|
|
711
856
|
return this
|
|
712
857
|
}
|
|
@@ -724,20 +869,14 @@ MqttClient.prototype.subscribe = function () {
|
|
|
724
869
|
* @example client.unsubscribe('topic', console.log);
|
|
725
870
|
*/
|
|
726
871
|
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++) {
|
|
872
|
+
const that = this
|
|
873
|
+
const args = new Array(arguments.length)
|
|
874
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
735
875
|
args[i] = arguments[i]
|
|
736
876
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
877
|
+
let topic = args.shift()
|
|
878
|
+
let callback = args.pop() || nop
|
|
879
|
+
let opts = args.pop()
|
|
741
880
|
if (typeof topic === 'string') {
|
|
742
881
|
topic = [topic]
|
|
743
882
|
}
|
|
@@ -747,33 +886,65 @@ MqttClient.prototype.unsubscribe = function () {
|
|
|
747
886
|
callback = nop
|
|
748
887
|
}
|
|
749
888
|
|
|
750
|
-
|
|
889
|
+
const invalidTopic = validations.validateTopics(topic)
|
|
890
|
+
if (invalidTopic !== null) {
|
|
891
|
+
setImmediate(callback, new Error('Invalid topic ' + invalidTopic))
|
|
751
892
|
return this
|
|
752
893
|
}
|
|
753
894
|
|
|
754
|
-
if (
|
|
755
|
-
|
|
756
|
-
} else if (Array.isArray(topic)) {
|
|
757
|
-
packet.unsubscriptions = topic
|
|
895
|
+
if (that._checkDisconnecting(callback)) {
|
|
896
|
+
return this
|
|
758
897
|
}
|
|
759
898
|
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
899
|
+
const unsubscribeProc = function () {
|
|
900
|
+
const messageId = that._nextId()
|
|
901
|
+
if (messageId === null) {
|
|
902
|
+
debug('No messageId left')
|
|
903
|
+
return false
|
|
904
|
+
}
|
|
905
|
+
const packet = {
|
|
906
|
+
cmd: 'unsubscribe',
|
|
907
|
+
qos: 1,
|
|
908
|
+
messageId: messageId
|
|
909
|
+
}
|
|
765
910
|
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
911
|
+
if (typeof topic === 'string') {
|
|
912
|
+
packet.unsubscriptions = [topic]
|
|
913
|
+
} else if (Array.isArray(topic)) {
|
|
914
|
+
packet.unsubscriptions = topic
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
if (that.options.resubscribe) {
|
|
918
|
+
packet.unsubscriptions.forEach(function (topic) {
|
|
919
|
+
delete that._resubscribeTopics[topic]
|
|
920
|
+
})
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
if (typeof opts === 'object' && opts.properties) {
|
|
924
|
+
packet.properties = opts.properties
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
that.outgoing[packet.messageId] = {
|
|
928
|
+
volatile: true,
|
|
929
|
+
cb: callback
|
|
930
|
+
}
|
|
769
931
|
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
932
|
+
debug('unsubscribe: call _sendPacket')
|
|
933
|
+
that._sendPacket(packet)
|
|
934
|
+
|
|
935
|
+
return true
|
|
773
936
|
}
|
|
774
937
|
|
|
775
|
-
|
|
776
|
-
|
|
938
|
+
if (this._storeProcessing || this._storeProcessingQueue.length > 0) {
|
|
939
|
+
this._storeProcessingQueue.push(
|
|
940
|
+
{
|
|
941
|
+
invoke: unsubscribeProc,
|
|
942
|
+
callback: callback
|
|
943
|
+
}
|
|
944
|
+
)
|
|
945
|
+
} else {
|
|
946
|
+
unsubscribeProc()
|
|
947
|
+
}
|
|
777
948
|
|
|
778
949
|
return this
|
|
779
950
|
}
|
|
@@ -789,7 +960,7 @@ MqttClient.prototype.unsubscribe = function () {
|
|
|
789
960
|
* @api public
|
|
790
961
|
*/
|
|
791
962
|
MqttClient.prototype.end = function (force, opts, cb) {
|
|
792
|
-
|
|
963
|
+
const that = this
|
|
793
964
|
|
|
794
965
|
debug('end :: (%s)', this.options.clientId)
|
|
795
966
|
|
|
@@ -822,7 +993,7 @@ MqttClient.prototype.end = function (force, opts, cb) {
|
|
|
822
993
|
debug('end :: closeStores: emitting end')
|
|
823
994
|
that.emit('end')
|
|
824
995
|
if (cb) {
|
|
825
|
-
|
|
996
|
+
const err = e1 || e2
|
|
826
997
|
debug('end :: closeStores: invoking callback with args')
|
|
827
998
|
cb(err)
|
|
828
999
|
}
|
|
@@ -840,7 +1011,7 @@ MqttClient.prototype.end = function (force, opts, cb) {
|
|
|
840
1011
|
debug('end :: (%s) :: finish :: calling _cleanUp with force %s', that.options.clientId, force)
|
|
841
1012
|
that._cleanUp(force, () => {
|
|
842
1013
|
debug('end :: finish :: calling process.nextTick on closeStores')
|
|
843
|
-
//
|
|
1014
|
+
// const boundProcess = nextTick.bind(null, closeStores)
|
|
844
1015
|
nextTick(closeStores.bind(that))
|
|
845
1016
|
}, opts)
|
|
846
1017
|
}
|
|
@@ -874,12 +1045,12 @@ MqttClient.prototype.end = function (force, opts, cb) {
|
|
|
874
1045
|
* @returns {MqttClient} this - for chaining
|
|
875
1046
|
* @api public
|
|
876
1047
|
*
|
|
877
|
-
* @example client.removeOutgoingMessage(client.
|
|
1048
|
+
* @example client.removeOutgoingMessage(client.getLastAllocated());
|
|
878
1049
|
*/
|
|
879
1050
|
MqttClient.prototype.removeOutgoingMessage = function (messageId) {
|
|
880
|
-
|
|
1051
|
+
const cb = this.outgoing[messageId] ? this.outgoing[messageId].cb : null
|
|
881
1052
|
delete this.outgoing[messageId]
|
|
882
|
-
this.outgoingStore.del({messageId: messageId}, function () {
|
|
1053
|
+
this.outgoingStore.del({ messageId: messageId }, function () {
|
|
883
1054
|
cb(new Error('Message removed'))
|
|
884
1055
|
})
|
|
885
1056
|
return this
|
|
@@ -898,8 +1069,8 @@ MqttClient.prototype.removeOutgoingMessage = function (messageId) {
|
|
|
898
1069
|
*/
|
|
899
1070
|
MqttClient.prototype.reconnect = function (opts) {
|
|
900
1071
|
debug('client reconnect')
|
|
901
|
-
|
|
902
|
-
|
|
1072
|
+
const that = this
|
|
1073
|
+
const f = function () {
|
|
903
1074
|
if (opts) {
|
|
904
1075
|
that.options.incomingStore = opts.incomingStore
|
|
905
1076
|
that.options.outgoingStore = opts.outgoingStore
|
|
@@ -943,7 +1114,7 @@ MqttClient.prototype._reconnect = function () {
|
|
|
943
1114
|
* _setupReconnect - setup reconnect timer
|
|
944
1115
|
*/
|
|
945
1116
|
MqttClient.prototype._setupReconnect = function () {
|
|
946
|
-
|
|
1117
|
+
const that = this
|
|
947
1118
|
|
|
948
1119
|
if (!that.disconnecting && !that.reconnectTimer && (that.options.reconnectPeriod > 0)) {
|
|
949
1120
|
if (!this.reconnecting) {
|
|
@@ -978,7 +1149,7 @@ MqttClient.prototype._clearReconnect = function () {
|
|
|
978
1149
|
* @api private
|
|
979
1150
|
*/
|
|
980
1151
|
MqttClient.prototype._cleanUp = function (forced, done) {
|
|
981
|
-
|
|
1152
|
+
const opts = arguments[2]
|
|
982
1153
|
if (done) {
|
|
983
1154
|
debug('_cleanUp :: done callback provided for on stream close')
|
|
984
1155
|
this.stream.on('close', done)
|
|
@@ -992,7 +1163,7 @@ MqttClient.prototype._cleanUp = function (forced, done) {
|
|
|
992
1163
|
debug('_cleanUp :: (%s) :: destroying stream', this.options.clientId)
|
|
993
1164
|
this.stream.destroy()
|
|
994
1165
|
} else {
|
|
995
|
-
|
|
1166
|
+
const packet = xtend({ cmd: 'disconnect' }, opts)
|
|
996
1167
|
debug('_cleanUp :: (%s) :: call _sendPacket with disconnect packet', this.options.clientId)
|
|
997
1168
|
this._sendPacket(
|
|
998
1169
|
packet,
|
|
@@ -1032,8 +1203,22 @@ MqttClient.prototype._cleanUp = function (forced, done) {
|
|
|
1032
1203
|
MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
|
|
1033
1204
|
debug('_sendPacket :: (%s) :: start', this.options.clientId)
|
|
1034
1205
|
cbStorePut = cbStorePut || nop
|
|
1206
|
+
cb = cb || nop
|
|
1207
|
+
|
|
1208
|
+
const err = applyTopicAlias(this, packet)
|
|
1209
|
+
if (err) {
|
|
1210
|
+
cb(err)
|
|
1211
|
+
return
|
|
1212
|
+
}
|
|
1035
1213
|
|
|
1036
1214
|
if (!this.connected) {
|
|
1215
|
+
// allow auth packets to be sent while authenticating with the broker (mqtt5 enhanced auth)
|
|
1216
|
+
if (packet.cmd === 'auth') {
|
|
1217
|
+
this._shiftPingInterval()
|
|
1218
|
+
sendPacket(this, packet, cb)
|
|
1219
|
+
return
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1037
1222
|
debug('_sendPacket :: client not connected. Storing packet offline.')
|
|
1038
1223
|
this._storePacket(packet, cb, cbStorePut)
|
|
1039
1224
|
return
|
|
@@ -1084,12 +1269,23 @@ MqttClient.prototype._storePacket = function (packet, cb, cbStorePut) {
|
|
|
1084
1269
|
debug('_storePacket :: cb? %s', !!cb)
|
|
1085
1270
|
cbStorePut = cbStorePut || nop
|
|
1086
1271
|
|
|
1272
|
+
let storePacket = packet
|
|
1273
|
+
if (storePacket.cmd === 'publish') {
|
|
1274
|
+
// The original packet is for sending.
|
|
1275
|
+
// The cloned storePacket is for storing to resend on reconnect.
|
|
1276
|
+
// Topic Alias must not be used after disconnected.
|
|
1277
|
+
storePacket = clone(packet)
|
|
1278
|
+
const err = removeTopicAliasAndRecoverTopicName(this, storePacket)
|
|
1279
|
+
if (err) {
|
|
1280
|
+
return cb && cb(err)
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1087
1283
|
// 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(
|
|
1284
|
+
if (((storePacket.qos || 0) === 0 && this.queueQoSZero) || storePacket.cmd !== 'publish') {
|
|
1285
|
+
this.queue.push({ packet: storePacket, cb: cb })
|
|
1286
|
+
} else if (storePacket.qos > 0) {
|
|
1287
|
+
cb = this.outgoing[storePacket.messageId] ? this.outgoing[storePacket.messageId].cb : null
|
|
1288
|
+
this.outgoingStore.put(storePacket, function (err) {
|
|
1093
1289
|
if (err) {
|
|
1094
1290
|
return cb && cb(err)
|
|
1095
1291
|
}
|
|
@@ -1107,7 +1303,7 @@ MqttClient.prototype._storePacket = function (packet, cb, cbStorePut) {
|
|
|
1107
1303
|
*/
|
|
1108
1304
|
MqttClient.prototype._setupPingTimer = function () {
|
|
1109
1305
|
debug('_setupPingTimer :: keepalive %d (seconds)', this.options.keepalive)
|
|
1110
|
-
|
|
1306
|
+
const that = this
|
|
1111
1307
|
|
|
1112
1308
|
if (!this.pingTimer && this.options.keepalive) {
|
|
1113
1309
|
this.pingResp = true
|
|
@@ -1162,16 +1358,22 @@ MqttClient.prototype._handlePingresp = function () {
|
|
|
1162
1358
|
*/
|
|
1163
1359
|
MqttClient.prototype._handleConnack = function (packet) {
|
|
1164
1360
|
debug('_handleConnack')
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1361
|
+
const options = this.options
|
|
1362
|
+
const version = options.protocolVersion
|
|
1363
|
+
const rc = version === 5 ? packet.reasonCode : packet.returnCode
|
|
1168
1364
|
|
|
1169
1365
|
clearTimeout(this.connackTimer)
|
|
1366
|
+
delete this.topicAliasSend
|
|
1170
1367
|
|
|
1171
1368
|
if (packet.properties) {
|
|
1172
1369
|
if (packet.properties.topicAliasMaximum) {
|
|
1173
|
-
if (
|
|
1174
|
-
|
|
1370
|
+
if (packet.properties.topicAliasMaximum > 0xffff) {
|
|
1371
|
+
this.emit('error', new Error('topicAliasMaximum from broker is out of range'))
|
|
1372
|
+
return
|
|
1373
|
+
}
|
|
1374
|
+
if (packet.properties.topicAliasMaximum > 0) {
|
|
1375
|
+
this.topicAliasSend = new TopicAliasSend(packet.properties.topicAliasMaximum)
|
|
1376
|
+
}
|
|
1175
1377
|
}
|
|
1176
1378
|
if (packet.properties.serverKeepAlive && options.keepalive) {
|
|
1177
1379
|
options.keepalive = packet.properties.serverKeepAlive
|
|
@@ -1187,12 +1389,51 @@ MqttClient.prototype._handleConnack = function (packet) {
|
|
|
1187
1389
|
this.reconnecting = false
|
|
1188
1390
|
this._onConnect(packet)
|
|
1189
1391
|
} else if (rc > 0) {
|
|
1190
|
-
|
|
1392
|
+
const err = new Error('Connection refused: ' + errors[rc])
|
|
1191
1393
|
err.code = rc
|
|
1192
1394
|
this.emit('error', err)
|
|
1193
1395
|
}
|
|
1194
1396
|
}
|
|
1195
1397
|
|
|
1398
|
+
MqttClient.prototype._handleAuth = function (packet) {
|
|
1399
|
+
const options = this.options
|
|
1400
|
+
const version = options.protocolVersion
|
|
1401
|
+
const rc = version === 5 ? packet.reasonCode : packet.returnCode
|
|
1402
|
+
|
|
1403
|
+
if (version !== 5) {
|
|
1404
|
+
const err = new Error('Protocol error: Auth packets are only supported in MQTT 5. Your version:' + version)
|
|
1405
|
+
err.code = rc
|
|
1406
|
+
this.emit('error', err)
|
|
1407
|
+
return
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
const that = this
|
|
1411
|
+
this.handleAuth(packet, function (err, packet) {
|
|
1412
|
+
if (err) {
|
|
1413
|
+
that.emit('error', err)
|
|
1414
|
+
return
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
if (rc === 24) {
|
|
1418
|
+
that.reconnecting = false
|
|
1419
|
+
that._sendPacket(packet)
|
|
1420
|
+
} else {
|
|
1421
|
+
const error = new Error('Connection refused: ' + errors[rc])
|
|
1422
|
+
err.code = rc
|
|
1423
|
+
that.emit('error', error)
|
|
1424
|
+
}
|
|
1425
|
+
})
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
* @param packet the packet received by the broker
|
|
1430
|
+
* @return the auth packet to be returned to the broker
|
|
1431
|
+
* @api public
|
|
1432
|
+
*/
|
|
1433
|
+
MqttClient.prototype.handleAuth = function (packet, callback) {
|
|
1434
|
+
callback()
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1196
1437
|
/**
|
|
1197
1438
|
* _handlePublish
|
|
1198
1439
|
*
|
|
@@ -1226,13 +1467,46 @@ for now i just suppressed the warnings
|
|
|
1226
1467
|
MqttClient.prototype._handlePublish = function (packet, done) {
|
|
1227
1468
|
debug('_handlePublish: packet %o', packet)
|
|
1228
1469
|
done = typeof done !== 'undefined' ? done : nop
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1470
|
+
let topic = packet.topic.toString()
|
|
1471
|
+
const message = packet.payload
|
|
1472
|
+
const qos = packet.qos
|
|
1473
|
+
const messageId = packet.messageId
|
|
1474
|
+
const that = this
|
|
1475
|
+
const options = this.options
|
|
1476
|
+
const validReasonCodes = [0, 16, 128, 131, 135, 144, 145, 151, 153]
|
|
1477
|
+
if (this.options.protocolVersion === 5) {
|
|
1478
|
+
let alias
|
|
1479
|
+
if (packet.properties) {
|
|
1480
|
+
alias = packet.properties.topicAlias
|
|
1481
|
+
}
|
|
1482
|
+
if (typeof alias !== 'undefined') {
|
|
1483
|
+
if (topic.length === 0) {
|
|
1484
|
+
if (alias > 0 && alias <= 0xffff) {
|
|
1485
|
+
const gotTopic = this.topicAliasRecv.getTopicByAlias(alias)
|
|
1486
|
+
if (gotTopic) {
|
|
1487
|
+
topic = gotTopic
|
|
1488
|
+
debug('_handlePublish :: topic complemented by alias. topic: %s - alias: %d', topic, alias)
|
|
1489
|
+
} else {
|
|
1490
|
+
debug('_handlePublish :: unregistered topic alias. alias: %d', alias)
|
|
1491
|
+
this.emit('error', new Error('Received unregistered Topic Alias'))
|
|
1492
|
+
return
|
|
1493
|
+
}
|
|
1494
|
+
} else {
|
|
1495
|
+
debug('_handlePublish :: topic alias out of range. alias: %d', alias)
|
|
1496
|
+
this.emit('error', new Error('Received Topic Alias is out of range'))
|
|
1497
|
+
return
|
|
1498
|
+
}
|
|
1499
|
+
} else {
|
|
1500
|
+
if (this.topicAliasRecv.put(topic, alias)) {
|
|
1501
|
+
debug('_handlePublish :: registered topic: %s - alias: %d', topic, alias)
|
|
1502
|
+
} else {
|
|
1503
|
+
debug('_handlePublish :: topic alias out of range. alias: %d', alias)
|
|
1504
|
+
this.emit('error', new Error('Received Topic Alias is out of range'))
|
|
1505
|
+
return
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1236
1510
|
debug('_handlePublish: qos %d', qos)
|
|
1237
1511
|
switch (qos) {
|
|
1238
1512
|
case 2: {
|
|
@@ -1244,10 +1518,10 @@ MqttClient.prototype._handlePublish = function (packet, done) {
|
|
|
1244
1518
|
if (error) { return that.emit('error', error) }
|
|
1245
1519
|
if (validReasonCodes.indexOf(code) === -1) { return that.emit('error', new Error('Wrong reason code for pubrec')) }
|
|
1246
1520
|
if (code) {
|
|
1247
|
-
that._sendPacket({cmd: 'pubrec', messageId: messageId, reasonCode: code}, done)
|
|
1521
|
+
that._sendPacket({ cmd: 'pubrec', messageId: messageId, reasonCode: code }, done)
|
|
1248
1522
|
} else {
|
|
1249
1523
|
that.incomingStore.put(packet, function () {
|
|
1250
|
-
that._sendPacket({cmd: 'pubrec', messageId: messageId}, done)
|
|
1524
|
+
that._sendPacket({ cmd: 'pubrec', messageId: messageId }, done)
|
|
1251
1525
|
})
|
|
1252
1526
|
}
|
|
1253
1527
|
})
|
|
@@ -1267,7 +1541,7 @@ MqttClient.prototype._handlePublish = function (packet, done) {
|
|
|
1267
1541
|
if (err) {
|
|
1268
1542
|
return done && done(err)
|
|
1269
1543
|
}
|
|
1270
|
-
that._sendPacket({cmd: 'puback', messageId: messageId, reasonCode: code}, done)
|
|
1544
|
+
that._sendPacket({ cmd: 'puback', messageId: messageId, reasonCode: code }, done)
|
|
1271
1545
|
})
|
|
1272
1546
|
})
|
|
1273
1547
|
break
|
|
@@ -1306,12 +1580,12 @@ MqttClient.prototype.handleMessage = function (packet, callback) {
|
|
|
1306
1580
|
|
|
1307
1581
|
MqttClient.prototype._handleAck = function (packet) {
|
|
1308
1582
|
/* eslint no-fallthrough: "off" */
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1583
|
+
const messageId = packet.messageId
|
|
1584
|
+
const type = packet.cmd
|
|
1585
|
+
let response = null
|
|
1586
|
+
const cb = this.outgoing[messageId] ? this.outgoing[messageId].cb : null
|
|
1587
|
+
const that = this
|
|
1588
|
+
let err
|
|
1315
1589
|
|
|
1316
1590
|
if (!cb) {
|
|
1317
1591
|
debug('_handleAck :: Server sent an ack in error. Ignoring.')
|
|
@@ -1324,8 +1598,8 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1324
1598
|
switch (type) {
|
|
1325
1599
|
case 'pubcomp':
|
|
1326
1600
|
// same thing as puback for QoS 2
|
|
1327
|
-
case 'puback':
|
|
1328
|
-
|
|
1601
|
+
case 'puback': {
|
|
1602
|
+
const pubackRC = packet.reasonCode
|
|
1329
1603
|
// Callback - we're done
|
|
1330
1604
|
if (pubackRC && pubackRC > 0 && pubackRC !== 16) {
|
|
1331
1605
|
err = new Error('Publish error: ' + errors[pubackRC])
|
|
@@ -1334,14 +1608,17 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1334
1608
|
}
|
|
1335
1609
|
delete this.outgoing[messageId]
|
|
1336
1610
|
this.outgoingStore.del(packet, cb)
|
|
1611
|
+
this.messageIdProvider.deallocate(messageId)
|
|
1612
|
+
this._invokeStoreProcessingQueue()
|
|
1337
1613
|
break
|
|
1338
|
-
|
|
1614
|
+
}
|
|
1615
|
+
case 'pubrec': {
|
|
1339
1616
|
response = {
|
|
1340
1617
|
cmd: 'pubrel',
|
|
1341
1618
|
qos: 2,
|
|
1342
1619
|
messageId: messageId
|
|
1343
1620
|
}
|
|
1344
|
-
|
|
1621
|
+
const pubrecRC = packet.reasonCode
|
|
1345
1622
|
|
|
1346
1623
|
if (pubrecRC && pubrecRC > 0 && pubrecRC !== 16) {
|
|
1347
1624
|
err = new Error('Publish error: ' + errors[pubrecRC])
|
|
@@ -1351,12 +1628,14 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1351
1628
|
this._sendPacket(response)
|
|
1352
1629
|
}
|
|
1353
1630
|
break
|
|
1354
|
-
|
|
1631
|
+
}
|
|
1632
|
+
case 'suback': {
|
|
1355
1633
|
delete this.outgoing[messageId]
|
|
1356
|
-
|
|
1634
|
+
this.messageIdProvider.deallocate(messageId)
|
|
1635
|
+
for (let grantedI = 0; grantedI < packet.granted.length; grantedI++) {
|
|
1357
1636
|
if ((packet.granted[grantedI] & 0x80) !== 0) {
|
|
1358
1637
|
// suback with Failure status
|
|
1359
|
-
|
|
1638
|
+
const topics = this.messageIdToTopic[messageId]
|
|
1360
1639
|
if (topics) {
|
|
1361
1640
|
topics.forEach(function (topic) {
|
|
1362
1641
|
delete that._resubscribeTopics[topic]
|
|
@@ -1364,12 +1643,17 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1364
1643
|
}
|
|
1365
1644
|
}
|
|
1366
1645
|
}
|
|
1646
|
+
this._invokeStoreProcessingQueue()
|
|
1367
1647
|
cb(null, packet)
|
|
1368
1648
|
break
|
|
1369
|
-
|
|
1649
|
+
}
|
|
1650
|
+
case 'unsuback': {
|
|
1370
1651
|
delete this.outgoing[messageId]
|
|
1652
|
+
this.messageIdProvider.deallocate(messageId)
|
|
1653
|
+
this._invokeStoreProcessingQueue()
|
|
1371
1654
|
cb(null)
|
|
1372
1655
|
break
|
|
1656
|
+
}
|
|
1373
1657
|
default:
|
|
1374
1658
|
that.emit('error', new Error('unrecognized packet type'))
|
|
1375
1659
|
}
|
|
@@ -1389,10 +1673,10 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1389
1673
|
MqttClient.prototype._handlePubrel = function (packet, callback) {
|
|
1390
1674
|
debug('handling pubrel packet')
|
|
1391
1675
|
callback = typeof callback !== 'undefined' ? callback : nop
|
|
1392
|
-
|
|
1393
|
-
|
|
1676
|
+
const messageId = packet.messageId
|
|
1677
|
+
const that = this
|
|
1394
1678
|
|
|
1395
|
-
|
|
1679
|
+
const comp = { cmd: 'pubcomp', messageId: messageId }
|
|
1396
1680
|
|
|
1397
1681
|
that.incomingStore.get(packet, function (err, pub) {
|
|
1398
1682
|
if (!err) {
|
|
@@ -1425,13 +1709,7 @@ MqttClient.prototype._handleDisconnect = function (packet) {
|
|
|
1425
1709
|
* @return unsigned int
|
|
1426
1710
|
*/
|
|
1427
1711
|
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
|
|
1712
|
+
return this.messageIdProvider.allocate()
|
|
1435
1713
|
}
|
|
1436
1714
|
|
|
1437
1715
|
/**
|
|
@@ -1439,27 +1717,27 @@ MqttClient.prototype._nextId = function () {
|
|
|
1439
1717
|
* @return unsigned int
|
|
1440
1718
|
*/
|
|
1441
1719
|
MqttClient.prototype.getLastMessageId = function () {
|
|
1442
|
-
return
|
|
1720
|
+
return this.messageIdProvider.getLastAllocated()
|
|
1443
1721
|
}
|
|
1444
1722
|
|
|
1445
1723
|
/**
|
|
1446
1724
|
* _resubscribe
|
|
1447
1725
|
* @api private
|
|
1448
1726
|
*/
|
|
1449
|
-
MqttClient.prototype._resubscribe = function (
|
|
1727
|
+
MqttClient.prototype._resubscribe = function () {
|
|
1450
1728
|
debug('_resubscribe')
|
|
1451
|
-
|
|
1729
|
+
const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics)
|
|
1452
1730
|
if (!this._firstConnection &&
|
|
1453
|
-
(this.options.clean || (this.options.protocolVersion === 5 && !
|
|
1731
|
+
(this.options.clean || (this.options.protocolVersion === 5 && !this.connackPacket.sessionPresent)) &&
|
|
1454
1732
|
_resubscribeTopicsKeys.length > 0) {
|
|
1455
1733
|
if (this.options.resubscribe) {
|
|
1456
1734
|
if (this.options.protocolVersion === 5) {
|
|
1457
1735
|
debug('_resubscribe: protocolVersion 5')
|
|
1458
|
-
for (
|
|
1459
|
-
|
|
1736
|
+
for (let topicI = 0; topicI < _resubscribeTopicsKeys.length; topicI++) {
|
|
1737
|
+
const resubscribeTopic = {}
|
|
1460
1738
|
resubscribeTopic[_resubscribeTopicsKeys[topicI]] = this._resubscribeTopics[_resubscribeTopicsKeys[topicI]]
|
|
1461
1739
|
resubscribeTopic.resubscribe = true
|
|
1462
|
-
this.subscribe(resubscribeTopic, {properties: resubscribeTopic[_resubscribeTopicsKeys[topicI]].properties})
|
|
1740
|
+
this.subscribe(resubscribeTopic, { properties: resubscribeTopic[_resubscribeTopicsKeys[topicI]].properties })
|
|
1463
1741
|
}
|
|
1464
1742
|
} else {
|
|
1465
1743
|
this._resubscribeTopics.resubscribe = true
|
|
@@ -1484,15 +1762,16 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1484
1762
|
return
|
|
1485
1763
|
}
|
|
1486
1764
|
|
|
1487
|
-
|
|
1765
|
+
const that = this
|
|
1488
1766
|
|
|
1767
|
+
this.connackPacket = packet
|
|
1768
|
+
this.messageIdProvider.clear()
|
|
1489
1769
|
this._setupPingTimer()
|
|
1490
|
-
this._resubscribe(packet)
|
|
1491
1770
|
|
|
1492
1771
|
this.connected = true
|
|
1493
1772
|
|
|
1494
1773
|
function startStreamProcess () {
|
|
1495
|
-
|
|
1774
|
+
let outStore = that.outgoingStore.createStream()
|
|
1496
1775
|
|
|
1497
1776
|
function clearStoreProcessing () {
|
|
1498
1777
|
that._storeProcessing = false
|
|
@@ -1502,6 +1781,7 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1502
1781
|
that.once('close', remove)
|
|
1503
1782
|
outStore.on('error', function (err) {
|
|
1504
1783
|
clearStoreProcessing()
|
|
1784
|
+
that._flushStoreProcessingQueue()
|
|
1505
1785
|
that.removeListener('close', remove)
|
|
1506
1786
|
that.emit('error', err)
|
|
1507
1787
|
})
|
|
@@ -1509,6 +1789,7 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1509
1789
|
function remove () {
|
|
1510
1790
|
outStore.destroy()
|
|
1511
1791
|
outStore = null
|
|
1792
|
+
that._flushStoreProcessingQueue()
|
|
1512
1793
|
clearStoreProcessing()
|
|
1513
1794
|
}
|
|
1514
1795
|
|
|
@@ -1519,9 +1800,9 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1519
1800
|
}
|
|
1520
1801
|
that._storeProcessing = true
|
|
1521
1802
|
|
|
1522
|
-
|
|
1803
|
+
const packet = outStore.read(1)
|
|
1523
1804
|
|
|
1524
|
-
|
|
1805
|
+
let cb
|
|
1525
1806
|
|
|
1526
1807
|
if (!packet) {
|
|
1527
1808
|
// read when data is available in the future
|
|
@@ -1550,15 +1831,19 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1550
1831
|
}
|
|
1551
1832
|
}
|
|
1552
1833
|
that._packetIdsDuringStoreProcessing[packet.messageId] = true
|
|
1553
|
-
that.
|
|
1834
|
+
if (that.messageIdProvider.register(packet.messageId)) {
|
|
1835
|
+
that._sendPacket(packet)
|
|
1836
|
+
} else {
|
|
1837
|
+
debug('messageId: %d has already used.', packet.messageId)
|
|
1838
|
+
}
|
|
1554
1839
|
} else if (outStore.destroy) {
|
|
1555
1840
|
outStore.destroy()
|
|
1556
1841
|
}
|
|
1557
1842
|
}
|
|
1558
1843
|
|
|
1559
1844
|
outStore.on('end', function () {
|
|
1560
|
-
|
|
1561
|
-
for (
|
|
1845
|
+
let allProcessed = true
|
|
1846
|
+
for (const id in that._packetIdsDuringStoreProcessing) {
|
|
1562
1847
|
if (!that._packetIdsDuringStoreProcessing[id]) {
|
|
1563
1848
|
allProcessed = false
|
|
1564
1849
|
break
|
|
@@ -1567,6 +1852,7 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1567
1852
|
if (allProcessed) {
|
|
1568
1853
|
clearStoreProcessing()
|
|
1569
1854
|
that.removeListener('close', remove)
|
|
1855
|
+
that._invokeAllStoreProcessingQueue()
|
|
1570
1856
|
that.emit('connect', packet)
|
|
1571
1857
|
} else {
|
|
1572
1858
|
startStreamProcess()
|
|
@@ -1578,4 +1864,27 @@ MqttClient.prototype._onConnect = function (packet) {
|
|
|
1578
1864
|
startStreamProcess()
|
|
1579
1865
|
}
|
|
1580
1866
|
|
|
1867
|
+
MqttClient.prototype._invokeStoreProcessingQueue = function () {
|
|
1868
|
+
if (this._storeProcessingQueue.length > 0) {
|
|
1869
|
+
const f = this._storeProcessingQueue[0]
|
|
1870
|
+
if (f && f.invoke()) {
|
|
1871
|
+
this._storeProcessingQueue.shift()
|
|
1872
|
+
return true
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
return false
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
MqttClient.prototype._invokeAllStoreProcessingQueue = function () {
|
|
1879
|
+
while (this._invokeStoreProcessingQueue()) { /* empty */ }
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
MqttClient.prototype._flushStoreProcessingQueue = function () {
|
|
1883
|
+
for (const f of this._storeProcessingQueue) {
|
|
1884
|
+
if (f.cbStorePut) f.cbStorePut(new Error('Connection closed'))
|
|
1885
|
+
if (f.callback) f.callback(new Error('Connection closed'))
|
|
1886
|
+
}
|
|
1887
|
+
this._storeProcessingQueue.splice(0)
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1581
1890
|
module.exports = MqttClient
|