mqtt 5.0.0-beta.2 → 5.0.0-beta.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.
@@ -0,0 +1,167 @@
1
+ const validReasonCodes = [0, 16, 128, 131, 135, 144, 145, 151, 153]
2
+
3
+ /*
4
+ those late 2 case should be rewrite to comply with coding style:
5
+
6
+ case 1:
7
+ case 0:
8
+ // do not wait sending a puback
9
+ // no callback passed
10
+ if (1 === qos) {
11
+ this._sendPacket({
12
+ cmd: 'puback',
13
+ messageId: messageId
14
+ });
15
+ }
16
+ // emit the message event for both qos 1 and 0
17
+ this.emit('message', topic, message, packet);
18
+ this.handleMessage(packet, done);
19
+ break;
20
+ default:
21
+ // do nothing but every switch mus have a default
22
+ // log or throw an error about unknown qos
23
+ break;
24
+
25
+ for now i just suppressed the warnings
26
+ */
27
+ function handlePublish(client, packet, done) {
28
+ client.log('handlePublish: packet %o', packet)
29
+ done = typeof done !== 'undefined' ? done : client.noop
30
+ let topic = packet.topic.toString()
31
+ const message = packet.payload
32
+ const { qos } = packet
33
+ const { messageId } = packet
34
+ const { options } = client
35
+ if (client.options.protocolVersion === 5) {
36
+ let alias
37
+ if (packet.properties) {
38
+ alias = packet.properties.topicAlias
39
+ }
40
+ if (typeof alias !== 'undefined') {
41
+ if (topic.length === 0) {
42
+ if (alias > 0 && alias <= 0xffff) {
43
+ const gotTopic =
44
+ client.topicAliasRecv.getTopicByAlias(alias)
45
+ if (gotTopic) {
46
+ topic = gotTopic
47
+ client.log(
48
+ 'handlePublish :: topic complemented by alias. topic: %s - alias: %d',
49
+ topic,
50
+ alias,
51
+ )
52
+ } else {
53
+ client.log(
54
+ 'handlePublish :: unregistered topic alias. alias: %d',
55
+ alias,
56
+ )
57
+ client.emit(
58
+ 'error',
59
+ new Error('Received unregistered Topic Alias'),
60
+ )
61
+ return
62
+ }
63
+ } else {
64
+ client.log(
65
+ 'handlePublish :: topic alias out of range. alias: %d',
66
+ alias,
67
+ )
68
+ client.emit(
69
+ 'error',
70
+ new Error('Received Topic Alias is out of range'),
71
+ )
72
+ return
73
+ }
74
+ } else if (client.topicAliasRecv.put(topic, alias)) {
75
+ client.log(
76
+ 'handlePublish :: registered topic: %s - alias: %d',
77
+ topic,
78
+ alias,
79
+ )
80
+ } else {
81
+ client.log(
82
+ 'handlePublish :: topic alias out of range. alias: %d',
83
+ alias,
84
+ )
85
+ client.emit(
86
+ 'error',
87
+ new Error('Received Topic Alias is out of range'),
88
+ )
89
+ return
90
+ }
91
+ }
92
+ }
93
+ client.log('handlePublish: qos %d', qos)
94
+ switch (qos) {
95
+ case 2: {
96
+ options.customHandleAcks(topic, message, packet, (error, code) => {
97
+ if (!(error instanceof Error)) {
98
+ code = error
99
+ error = null
100
+ }
101
+ if (error) {
102
+ return client.emit('error', error)
103
+ }
104
+ if (validReasonCodes.indexOf(code) === -1) {
105
+ return client.emit(
106
+ 'error',
107
+ new Error('Wrong reason code for pubrec'),
108
+ )
109
+ }
110
+ if (code) {
111
+ client._sendPacket(
112
+ { cmd: 'pubrec', messageId, reasonCode: code },
113
+ done,
114
+ )
115
+ } else {
116
+ client.incomingStore.put(packet, () => {
117
+ client._sendPacket({ cmd: 'pubrec', messageId }, done)
118
+ })
119
+ }
120
+ })
121
+ break
122
+ }
123
+ case 1: {
124
+ // emit the message event
125
+ options.customHandleAcks(topic, message, packet, (error, code) => {
126
+ if (!(error instanceof Error)) {
127
+ code = error
128
+ error = null
129
+ }
130
+ if (error) {
131
+ return client.emit('error', error)
132
+ }
133
+ if (validReasonCodes.indexOf(code) === -1) {
134
+ return client.emit(
135
+ 'error',
136
+ new Error('Wrong reason code for puback'),
137
+ )
138
+ }
139
+ if (!code) {
140
+ client.emit('message', topic, message, packet)
141
+ }
142
+ client.handleMessage(packet, (err) => {
143
+ if (err) {
144
+ return done && done(err)
145
+ }
146
+ client._sendPacket(
147
+ { cmd: 'puback', messageId, reasonCode: code },
148
+ done,
149
+ )
150
+ })
151
+ })
152
+ break
153
+ }
154
+ case 0:
155
+ // emit the message event
156
+ client.emit('message', topic, message, packet)
157
+ client.handleMessage(packet, done)
158
+ break
159
+ default:
160
+ // do nothing
161
+ client.log('handlePublish: unknown QoS. Doing nothing.')
162
+ // log or throw an error about unknown qos
163
+ break
164
+ }
165
+ }
166
+
167
+ module.exports = handlePublish
@@ -0,0 +1,24 @@
1
+ function handlePubrel(client, packet, done) {
2
+ client.log('handling pubrel packet')
3
+ const callback = typeof done !== 'undefined' ? done : client.noop
4
+ const { messageId } = packet
5
+
6
+ const comp = { cmd: 'pubcomp', messageId }
7
+
8
+ client.incomingStore.get(packet, (err, pub) => {
9
+ if (!err) {
10
+ client.emit('message', pub.topic, pub.payload, pub)
11
+ client.handleMessage(pub, (err2) => {
12
+ if (err2) {
13
+ return callback(err2)
14
+ }
15
+ client.incomingStore.del(pub, client.noop)
16
+ client._sendPacket(comp, callback)
17
+ })
18
+ } else {
19
+ client._sendPacket(comp, callback)
20
+ }
21
+ })
22
+ }
23
+
24
+ module.exports = handlePubrel
package/lib/is-browser.js CHANGED
@@ -1,11 +1,11 @@
1
1
  const legacyIsBrowser =
2
- (typeof process !== 'undefined' && process.title === 'browser') ||
3
- // eslint-disable-next-line camelcase
4
- typeof __webpack_require__ === 'function'
2
+ (typeof process !== 'undefined' && process.title === 'browser') ||
3
+ // eslint-disable-next-line camelcase
4
+ typeof __webpack_require__ === 'function'
5
5
 
6
6
  const isBrowser =
7
- typeof window !== 'undefined' && typeof document !== 'undefined'
7
+ typeof window !== 'undefined' && typeof document !== 'undefined'
8
8
 
9
9
  module.exports = {
10
- IS_BROWSER: isBrowser || legacyIsBrowser
10
+ IS_BROWSER: isBrowser || legacyIsBrowser,
11
11
  }
package/lib/store.js CHANGED
@@ -1,14 +1,11 @@
1
- 'use strict'
2
-
3
1
  /**
4
2
  * Module dependencies
5
3
  */
6
- const xtend = require('xtend')
4
+ const { Readable } = require('readable-stream')
7
5
 
8
- const Readable = require('readable-stream').Readable
9
6
  const streamsOpts = { objectMode: true }
10
7
  const defaultStoreOptions = {
11
- clean: true
8
+ clean: true,
12
9
  }
13
10
 
14
11
  /**
@@ -17,112 +14,108 @@ const defaultStoreOptions = {
17
14
  *
18
15
  * @param {Object} [options] - store options
19
16
  */
20
- function Store (options) {
21
- if (!(this instanceof Store)) {
22
- return new Store(options)
23
- }
24
-
25
- this.options = options || {}
26
-
27
- // Defaults
28
- this.options = xtend(defaultStoreOptions, options)
29
-
30
- this._inflights = new Map()
31
- }
32
-
33
- /**
34
- * Adds a packet to the store, a packet is
35
- * anything that has a messageId property.
36
- *
37
- */
38
- Store.prototype.put = function (packet, cb) {
39
- this._inflights.set(packet.messageId, packet)
40
-
41
- if (cb) {
42
- cb()
43
- }
44
-
45
- return this
46
- }
47
-
48
- /**
49
- * Creates a stream with all the packets in the store
50
- *
51
- */
52
- Store.prototype.createStream = function () {
53
- const stream = new Readable(streamsOpts)
54
- const values = []
55
- let destroyed = false
56
- let i = 0
57
-
58
- this._inflights.forEach(function (value, key) {
59
- values.push(value)
60
- })
61
-
62
- stream._read = function () {
63
- if (!destroyed && i < values.length) {
64
- this.push(values[i++])
65
- } else {
66
- this.push(null)
67
- }
68
- }
69
-
70
- stream.destroy = function () {
71
- if (destroyed) {
72
- return
73
- }
74
-
75
- const self = this
76
-
77
- destroyed = true
78
-
79
- setTimeout(function () {
80
- self.emit('close')
81
- }, 0)
82
- }
83
-
84
- return stream
85
- }
86
-
87
- /**
88
- * deletes a packet from the store.
89
- */
90
- Store.prototype.del = function (packet, cb) {
91
- packet = this._inflights.get(packet.messageId)
92
- if (packet) {
93
- this._inflights.delete(packet.messageId)
94
- cb(null, packet)
95
- } else if (cb) {
96
- cb(new Error('missing packet'))
97
- }
98
-
99
- return this
100
- }
101
-
102
- /**
103
- * get a packet from the store.
104
- */
105
- Store.prototype.get = function (packet, cb) {
106
- packet = this._inflights.get(packet.messageId)
107
- if (packet) {
108
- cb(null, packet)
109
- } else if (cb) {
110
- cb(new Error('missing packet'))
111
- }
112
-
113
- return this
114
- }
115
-
116
- /**
117
- * Close the store
118
- */
119
- Store.prototype.close = function (cb) {
120
- if (this.options.clean) {
121
- this._inflights = null
122
- }
123
- if (cb) {
124
- cb()
125
- }
17
+ class Store {
18
+ constructor(options) {
19
+ this.options = options || {}
20
+
21
+ // Defaults
22
+ this.options = { ...defaultStoreOptions, ...options }
23
+
24
+ this._inflights = new Map()
25
+ }
26
+
27
+ /**
28
+ * Adds a packet to the store, a packet is
29
+ * anything that has a messageId property.
30
+ *
31
+ */
32
+ put(packet, cb) {
33
+ this._inflights.set(packet.messageId, packet)
34
+
35
+ if (cb) {
36
+ cb()
37
+ }
38
+
39
+ return this
40
+ }
41
+
42
+ /**
43
+ * Creates a stream with all the packets in the store
44
+ *
45
+ */
46
+ createStream() {
47
+ const stream = new Readable(streamsOpts)
48
+ const values = []
49
+ let destroyed = false
50
+ let i = 0
51
+
52
+ this._inflights.forEach((value, key) => {
53
+ values.push(value)
54
+ })
55
+
56
+ stream._read = () => {
57
+ if (!destroyed && i < values.length) {
58
+ stream.push(values[i++])
59
+ } else {
60
+ stream.push(null)
61
+ }
62
+ }
63
+
64
+ stream.destroy = () => {
65
+ if (destroyed) {
66
+ return
67
+ }
68
+
69
+ destroyed = true
70
+
71
+ setTimeout(() => {
72
+ stream.emit('close')
73
+ }, 0)
74
+ }
75
+
76
+ return stream
77
+ }
78
+
79
+ /**
80
+ * deletes a packet from the store.
81
+ */
82
+ del(packet, cb) {
83
+ packet = this._inflights.get(packet.messageId)
84
+ if (packet) {
85
+ this._inflights.delete(packet.messageId)
86
+ cb(null, packet)
87
+ } else if (cb) {
88
+ cb(new Error('missing packet'))
89
+ }
90
+
91
+ return this
92
+ }
93
+
94
+ /**
95
+ * get a packet from the store.
96
+ */
97
+ get(packet, cb) {
98
+ packet = this._inflights.get(packet.messageId)
99
+ if (packet) {
100
+ cb(null, packet)
101
+ } else if (cb) {
102
+ cb(new Error('missing packet'))
103
+ }
104
+
105
+ return this
106
+ }
107
+
108
+ /**
109
+ * Close the store
110
+ */
111
+ close(cb) {
112
+ if (this.options.clean) {
113
+ this._inflights = null
114
+ }
115
+ if (cb) {
116
+ cb()
117
+ }
118
+ }
126
119
  }
127
120
 
128
121
  module.exports = Store
@@ -1,47 +1,44 @@
1
- 'use strict'
2
-
3
1
  /**
4
2
  * Topic Alias receiving manager
5
3
  * This holds alias to topic map
6
4
  * @param {Number} [max] - topic alias maximum entries
7
5
  */
8
- function TopicAliasRecv (max) {
9
- if (!(this instanceof TopicAliasRecv)) {
10
- return new TopicAliasRecv(max)
11
- }
12
- this.aliasToTopic = {}
13
- this.max = max
14
- }
6
+ class TopicAliasRecv {
7
+ constructor(max) {
8
+ this.aliasToTopic = {}
9
+ this.max = max
10
+ }
15
11
 
16
- /**
17
- * Insert or update topic - alias entry.
18
- * @param {String} [topic] - topic
19
- * @param {Number} [alias] - topic alias
20
- * @returns {Boolean} - if success return true otherwise false
21
- */
22
- TopicAliasRecv.prototype.put = function (topic, alias) {
23
- if (alias === 0 || alias > this.max) {
24
- return false
25
- }
26
- this.aliasToTopic[alias] = topic
27
- this.length = Object.keys(this.aliasToTopic).length
28
- return true
29
- }
12
+ /**
13
+ * Insert or update topic - alias entry.
14
+ * @param {String} [topic] - topic
15
+ * @param {Number} [alias] - topic alias
16
+ * @returns {Boolean} - if success return true otherwise false
17
+ */
18
+ put(topic, alias) {
19
+ if (alias === 0 || alias > this.max) {
20
+ return false
21
+ }
22
+ this.aliasToTopic[alias] = topic
23
+ this.length = Object.keys(this.aliasToTopic).length
24
+ return true
25
+ }
30
26
 
31
- /**
32
- * Get topic by alias
33
- * @param {String} [topic] - topic
34
- * @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
35
- */
36
- TopicAliasRecv.prototype.getTopicByAlias = function (alias) {
37
- return this.aliasToTopic[alias]
38
- }
27
+ /**
28
+ * Get topic by alias
29
+ * @param {String} [topic] - topic
30
+ * @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
31
+ */
32
+ getTopicByAlias(alias) {
33
+ return this.aliasToTopic[alias]
34
+ }
39
35
 
40
- /**
41
- * Clear all entries
42
- */
43
- TopicAliasRecv.prototype.clear = function () {
44
- this.aliasToTopic = {}
36
+ /**
37
+ * Clear all entries
38
+ */
39
+ clear() {
40
+ this.aliasToTopic = {}
41
+ }
45
42
  }
46
43
 
47
44
  module.exports = TopicAliasRecv
@@ -1,91 +1,88 @@
1
- 'use strict'
2
-
3
1
  /**
4
2
  * Module dependencies
5
3
  */
6
- const LruMap = require('lru-cache')
7
- const NumberAllocator = require('number-allocator').NumberAllocator
4
+ const LRUCache = require('lru-cache')
5
+ const { NumberAllocator } = require('number-allocator')
8
6
 
9
7
  /**
10
8
  * Topic Alias sending manager
11
9
  * This holds both topic to alias and alias to topic map
12
10
  * @param {Number} [max] - topic alias maximum entries
13
11
  */
14
- function TopicAliasSend (max) {
15
- if (!(this instanceof TopicAliasSend)) {
16
- return new TopicAliasSend(max)
17
- }
12
+ class TopicAliasSend {
13
+ constructor(max) {
14
+ if (max > 0) {
15
+ this.aliasToTopic = new LRUCache({ max })
16
+ this.topicToAlias = {}
17
+ this.numberAllocator = new NumberAllocator(1, max)
18
+ this.max = max
19
+ this.length = 0
20
+ }
21
+ }
18
22
 
19
- if (max > 0) {
20
- this.aliasToTopic = new LruMap({ max: max })
21
- this.topicToAlias = {}
22
- this.numberAllocator = new NumberAllocator(1, max)
23
- this.max = max
24
- this.length = 0
25
- }
26
- }
23
+ /**
24
+ * Insert or update topic - alias entry.
25
+ * @param {String} [topic] - topic
26
+ * @param {Number} [alias] - topic alias
27
+ * @returns {Boolean} - if success return true otherwise false
28
+ */
29
+ put(topic, alias) {
30
+ if (alias === 0 || alias > this.max) {
31
+ return false
32
+ }
33
+ const entry = this.aliasToTopic.get(alias)
34
+ if (entry) {
35
+ delete this.topicToAlias[entry]
36
+ }
37
+ this.aliasToTopic.set(alias, topic)
38
+ this.topicToAlias[topic] = alias
39
+ this.numberAllocator.use(alias)
40
+ this.length = this.aliasToTopic.size
41
+ return true
42
+ }
27
43
 
28
- /**
29
- * Insert or update topic - alias entry.
30
- * @param {String} [topic] - topic
31
- * @param {Number} [alias] - topic alias
32
- * @returns {Boolean} - if success return true otherwise false
33
- */
34
- TopicAliasSend.prototype.put = function (topic, alias) {
35
- if (alias === 0 || alias > this.max) {
36
- return false
37
- }
38
- const entry = this.aliasToTopic.get(alias)
39
- if (entry) {
40
- delete this.topicToAlias[entry]
41
- }
42
- this.aliasToTopic.set(alias, topic)
43
- this.topicToAlias[topic] = alias
44
- this.numberAllocator.use(alias)
45
- this.length = this.aliasToTopic.length
46
- return true
47
- }
44
+ /**
45
+ * Get topic by alias
46
+ * @param {Number} [alias] - topic alias
47
+ * @returns {String} - if mapped topic exists return topic, otherwise return undefined
48
+ */
49
+ getTopicByAlias(alias) {
50
+ return this.aliasToTopic.get(alias)
51
+ }
48
52
 
49
- /**
50
- * Get topic by alias
51
- * @param {Number} [alias] - topic alias
52
- * @returns {String} - if mapped topic exists return topic, otherwise return undefined
53
- */
54
- TopicAliasSend.prototype.getTopicByAlias = function (alias) {
55
- return this.aliasToTopic.get(alias)
56
- }
53
+ /**
54
+ * Get topic by alias
55
+ * @param {String} [topic] - topic
56
+ * @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
57
+ */
58
+ getAliasByTopic(topic) {
59
+ const alias = this.topicToAlias[topic]
60
+ if (typeof alias !== 'undefined') {
61
+ this.aliasToTopic.get(alias) // LRU update
62
+ }
63
+ return alias
64
+ }
57
65
 
58
- /**
59
- * Get topic by alias
60
- * @param {String} [topic] - topic
61
- * @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
62
- */
63
- TopicAliasSend.prototype.getAliasByTopic = function (topic) {
64
- const alias = this.topicToAlias[topic]
65
- if (typeof alias !== 'undefined') {
66
- this.aliasToTopic.get(alias) // LRU update
67
- }
68
- return alias
69
- }
70
-
71
- /**
72
- * Clear all entries
73
- */
74
- TopicAliasSend.prototype.clear = function () {
75
- this.aliasToTopic.reset()
76
- this.topicToAlias = {}
77
- this.numberAllocator.clear()
78
- this.length = 0
79
- }
66
+ /**
67
+ * Clear all entries
68
+ */
69
+ clear() {
70
+ this.aliasToTopic.clear()
71
+ this.topicToAlias = {}
72
+ this.numberAllocator.clear()
73
+ this.length = 0
74
+ }
80
75
 
81
- /**
82
- * Get Least Recently Used (LRU) topic alias
83
- * @returns {Number} - if vacant alias exists then return it, otherwise then return LRU alias
84
- */
85
- TopicAliasSend.prototype.getLruAlias = function () {
86
- const alias = this.numberAllocator.firstVacant()
87
- if (alias) return alias
88
- return this.aliasToTopic.keys()[this.aliasToTopic.length - 1]
76
+ /**
77
+ * Get Least Recently Used (LRU) topic alias
78
+ * @returns {Number} - if vacant alias exists then return it, otherwise then return LRU alias
79
+ */
80
+ getLruAlias() {
81
+ const alias = this.numberAllocator.firstVacant()
82
+ if (alias) return alias
83
+ // get last alias (key) from LRU cache
84
+ return [...this.aliasToTopic.keys()][this.aliasToTopic.size - 1]
85
+ }
89
86
  }
90
87
 
91
88
  module.exports = TopicAliasSend