mqtt 4.2.7 → 4.3.2
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 +53 -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 +12628 -4503
- 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 +1890 -1581
- 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 -239
- 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
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Module dependencies
|
|
5
|
+
*/
|
|
6
|
+
const LruMap = require('collections/lru-map')
|
|
7
|
+
const NumberAllocator = require('number-allocator').NumberAllocator
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Topic Alias sending manager
|
|
11
|
+
* This holds both topic to alias and alias to topic map
|
|
12
|
+
* @param {Number} [max] - topic alias maximum entries
|
|
13
|
+
*/
|
|
14
|
+
function TopicAliasSend (max) {
|
|
15
|
+
if (!(this instanceof TopicAliasSend)) {
|
|
16
|
+
return new TopicAliasSend(max)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (max > 0) {
|
|
20
|
+
this.aliasToTopic = new LruMap()
|
|
21
|
+
this.topicToAlias = {}
|
|
22
|
+
this.numberAllocator = new NumberAllocator(1, max)
|
|
23
|
+
this.max = max
|
|
24
|
+
this.length = 0
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
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.topic]
|
|
41
|
+
}
|
|
42
|
+
this.aliasToTopic.set(alias, { topic: topic, alias: alias })
|
|
43
|
+
this.topicToAlias[topic] = alias
|
|
44
|
+
this.numberAllocator.use(alias)
|
|
45
|
+
this.length = this.aliasToTopic.length
|
|
46
|
+
return true
|
|
47
|
+
}
|
|
48
|
+
|
|
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
|
+
const entry = this.aliasToTopic.get(alias)
|
|
56
|
+
if (typeof entry === 'undefined') return entry
|
|
57
|
+
return entry.topic
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get topic by alias
|
|
62
|
+
* @param {String} [topic] - topic
|
|
63
|
+
* @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
|
|
64
|
+
*/
|
|
65
|
+
TopicAliasSend.prototype.getAliasByTopic = function (topic) {
|
|
66
|
+
const alias = this.topicToAlias[topic]
|
|
67
|
+
if (typeof alias !== 'undefined') {
|
|
68
|
+
this.aliasToTopic.get(alias) // LRU update
|
|
69
|
+
}
|
|
70
|
+
return alias
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Clear all entries
|
|
75
|
+
*/
|
|
76
|
+
TopicAliasSend.prototype.clear = function () {
|
|
77
|
+
this.aliasToTopic.clear()
|
|
78
|
+
this.topicToAlias = {}
|
|
79
|
+
this.numberAllocator.clear()
|
|
80
|
+
this.length = 0
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get Least Recently Used (LRU) topic alias
|
|
85
|
+
* @returns {Number} - if vacant alias exists then return it, otherwise then return LRU alias
|
|
86
|
+
*/
|
|
87
|
+
TopicAliasSend.prototype.getLruAlias = function () {
|
|
88
|
+
const alias = this.numberAllocator.firstVacant()
|
|
89
|
+
if (alias) return alias
|
|
90
|
+
return this.aliasToTopic.min().alias
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = TopicAliasSend
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const NumberAllocator = require('number-allocator').NumberAllocator
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* UniqueMessageAllocator constructor
|
|
7
|
+
* @constructor
|
|
8
|
+
*/
|
|
9
|
+
function UniqueMessageIdProvider () {
|
|
10
|
+
if (!(this instanceof UniqueMessageIdProvider)) {
|
|
11
|
+
return new UniqueMessageIdProvider()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
this.numberAllocator = new NumberAllocator(1, 65535)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* allocate
|
|
19
|
+
*
|
|
20
|
+
* Get the next messageId.
|
|
21
|
+
* @return if messageId is fully allocated then return null,
|
|
22
|
+
* otherwise return the smallest usable unsigned int messageId.
|
|
23
|
+
*/
|
|
24
|
+
UniqueMessageIdProvider.prototype.allocate = function () {
|
|
25
|
+
this.lastId = this.numberAllocator.alloc()
|
|
26
|
+
return this.lastId
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* getLastAllocated
|
|
31
|
+
* Get the last allocated messageId.
|
|
32
|
+
* @return unsigned int
|
|
33
|
+
*/
|
|
34
|
+
UniqueMessageIdProvider.prototype.getLastAllocated = function () {
|
|
35
|
+
return this.lastId
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* register
|
|
40
|
+
* Register messageId. If success return true, otherwise return false.
|
|
41
|
+
* @param { unsigned int } - messageId to register,
|
|
42
|
+
* @return boolean
|
|
43
|
+
*/
|
|
44
|
+
UniqueMessageIdProvider.prototype.register = function (messageId) {
|
|
45
|
+
return this.numberAllocator.use(messageId)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* deallocate
|
|
50
|
+
* Deallocate messageId.
|
|
51
|
+
* @param { unsigned int } - messageId to deallocate,
|
|
52
|
+
*/
|
|
53
|
+
UniqueMessageIdProvider.prototype.deallocate = function (messageId) {
|
|
54
|
+
this.numberAllocator.free(messageId)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* clear
|
|
59
|
+
* Deallocate all messageIds.
|
|
60
|
+
*/
|
|
61
|
+
UniqueMessageIdProvider.prototype.clear = function () {
|
|
62
|
+
this.numberAllocator.clear()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = UniqueMessageIdProvider
|
package/lib/validations.js
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Validate a topic to see if it's valid or not.
|
|
5
|
-
* A topic is valid if it follow below rules:
|
|
6
|
-
* - Rule #1: If any part of the topic is not `+` or `#`, then it must not contain `+` and '#'
|
|
7
|
-
* - Rule #2: Part `#` must be located at the end of the mailbox
|
|
8
|
-
*
|
|
9
|
-
* @param {String} topic - A topic
|
|
10
|
-
* @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false.
|
|
11
|
-
*/
|
|
12
|
-
function validateTopic (topic) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
for (
|
|
16
|
-
if (parts[i] === '+') {
|
|
17
|
-
continue
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (parts[i] === '#') {
|
|
21
|
-
// for Rule #2
|
|
22
|
-
return i === parts.length - 1
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) {
|
|
26
|
-
return false
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return true
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Validate an array of topics to see if any of them is valid or not
|
|
35
|
-
* @param {Array} topics - Array of topics
|
|
36
|
-
* @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
|
|
37
|
-
*/
|
|
38
|
-
function validateTopics (topics) {
|
|
39
|
-
if (topics.length === 0) {
|
|
40
|
-
return 'empty_topic_list'
|
|
41
|
-
}
|
|
42
|
-
for (
|
|
43
|
-
if (!validateTopic(topics[i])) {
|
|
44
|
-
return topics[i]
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return null
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
module.exports = {
|
|
51
|
-
validateTopics: validateTopics
|
|
52
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validate a topic to see if it's valid or not.
|
|
5
|
+
* A topic is valid if it follow below rules:
|
|
6
|
+
* - Rule #1: If any part of the topic is not `+` or `#`, then it must not contain `+` and '#'
|
|
7
|
+
* - Rule #2: Part `#` must be located at the end of the mailbox
|
|
8
|
+
*
|
|
9
|
+
* @param {String} topic - A topic
|
|
10
|
+
* @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false.
|
|
11
|
+
*/
|
|
12
|
+
function validateTopic (topic) {
|
|
13
|
+
const parts = topic.split('/')
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < parts.length; i++) {
|
|
16
|
+
if (parts[i] === '+') {
|
|
17
|
+
continue
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (parts[i] === '#') {
|
|
21
|
+
// for Rule #2
|
|
22
|
+
return i === parts.length - 1
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) {
|
|
26
|
+
return false
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Validate an array of topics to see if any of them is valid or not
|
|
35
|
+
* @param {Array} topics - Array of topics
|
|
36
|
+
* @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
|
|
37
|
+
*/
|
|
38
|
+
function validateTopics (topics) {
|
|
39
|
+
if (topics.length === 0) {
|
|
40
|
+
return 'empty_topic_list'
|
|
41
|
+
}
|
|
42
|
+
for (let i = 0; i < topics.length; i++) {
|
|
43
|
+
if (!validateTopic(topics[i])) {
|
|
44
|
+
return topics[i]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = {
|
|
51
|
+
validateTopics: validateTopics
|
|
52
|
+
}
|
package/mqtt.js
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2015-2015 MQTT.js contributors.
|
|
3
|
-
* Copyright (c) 2011-2014 Adam Rudd.
|
|
4
|
-
*
|
|
5
|
-
* See LICENSE for more information
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
module.exports.
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2015-2015 MQTT.js contributors.
|
|
3
|
+
* Copyright (c) 2011-2014 Adam Rudd.
|
|
4
|
+
*
|
|
5
|
+
* See LICENSE for more information
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const MqttClient = require('./lib/client')
|
|
9
|
+
const connect = require('./lib/connect')
|
|
10
|
+
const Store = require('./lib/store')
|
|
11
|
+
const DefaultMessageIdProvider = require('./lib/default-message-id-provider')
|
|
12
|
+
const UniqueMessageIdProvider = require('./lib/unique-message-id-provider')
|
|
13
|
+
|
|
14
|
+
module.exports.connect = connect
|
|
15
|
+
|
|
16
|
+
// Expose MqttClient
|
|
17
|
+
module.exports.MqttClient = MqttClient
|
|
18
|
+
module.exports.Client = MqttClient
|
|
19
|
+
module.exports.Store = Store
|
|
20
|
+
module.exports.DefaultMessageIdProvider = DefaultMessageIdProvider
|
|
21
|
+
module.exports.UniqueMessageIdProvider = UniqueMessageIdProvider
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mqtt",
|
|
3
3
|
"description": "A library for the MQTT protocol",
|
|
4
|
-
"version": "4.2
|
|
4
|
+
"version": "4.3.2",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Adam Rudd <adamvrr@gmail.com>",
|
|
7
7
|
"Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"main": "mqtt.js",
|
|
23
23
|
"types": "types/index.d.ts",
|
|
24
24
|
"scripts": {
|
|
25
|
-
"test": "node_modules/.bin/nyc --reporter=lcov --reporter=text ./node_modules/mocha/bin/_mocha",
|
|
25
|
+
"test": "node_modules/.bin/nyc --reporter=lcov --reporter=text ./node_modules/mocha/bin/_mocha --exit",
|
|
26
26
|
"pretest": "standard | snazzy",
|
|
27
27
|
"tslint": "tslint types/**/*.d.ts",
|
|
28
28
|
"typescript-compile-test": "tsc -p test/typescript/tsconfig.json",
|
|
@@ -62,6 +62,7 @@
|
|
|
62
62
|
"net": false
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
+
"collections": "^5.1.13",
|
|
65
66
|
"commist": "^1.0.0",
|
|
66
67
|
"concat-stream": "^2.0.0",
|
|
67
68
|
"debug": "^4.1.1",
|
|
@@ -70,32 +71,35 @@
|
|
|
70
71
|
"inherits": "^2.0.3",
|
|
71
72
|
"minimist": "^1.2.5",
|
|
72
73
|
"mqtt-packet": "^6.8.0",
|
|
74
|
+
"number-allocator": "^1.0.8",
|
|
73
75
|
"pump": "^3.0.0",
|
|
74
76
|
"readable-stream": "^3.6.0",
|
|
75
77
|
"reinterval": "^1.1.0",
|
|
78
|
+
"rfdc": "^1.3.0",
|
|
76
79
|
"split2": "^3.1.0",
|
|
77
|
-
"ws": "^7.
|
|
80
|
+
"ws": "^7.5.5",
|
|
78
81
|
"xtend": "^4.0.2"
|
|
79
82
|
},
|
|
80
83
|
"devDependencies": {
|
|
81
84
|
"@types/node": "^10.0.0",
|
|
82
|
-
"
|
|
85
|
+
"@types/ws": "^7.4.7",
|
|
86
|
+
"aedes": "^0.42.5",
|
|
87
|
+
"airtap": "^4.0.4",
|
|
83
88
|
"browserify": "^16.5.0",
|
|
84
89
|
"chai": "^4.2.0",
|
|
85
90
|
"codecov": "^3.0.4",
|
|
86
91
|
"end-of-stream": "^1.4.1",
|
|
87
92
|
"global": "^4.3.2",
|
|
88
|
-
"aedes": "^0.42.5",
|
|
89
93
|
"mkdirp": "^0.5.1",
|
|
90
|
-
"mocha": "^
|
|
94
|
+
"mocha": "^9.1.3",
|
|
91
95
|
"mqtt-connection": "^4.0.0",
|
|
92
96
|
"nyc": "^15.0.1",
|
|
93
97
|
"pre-commit": "^1.2.2",
|
|
94
98
|
"rimraf": "^3.0.2",
|
|
95
99
|
"should": "^13.2.1",
|
|
96
100
|
"sinon": "^9.0.0",
|
|
97
|
-
"snazzy": "^
|
|
98
|
-
"standard": "^
|
|
101
|
+
"snazzy": "^9.0.0",
|
|
102
|
+
"standard": "^16.0.4",
|
|
99
103
|
"tslint": "^5.11.0",
|
|
100
104
|
"tslint-config-standard": "^8.0.1",
|
|
101
105
|
"typescript": "^3.2.2",
|
|
@@ -104,6 +108,15 @@
|
|
|
104
108
|
"standard": {
|
|
105
109
|
"env": [
|
|
106
110
|
"mocha"
|
|
111
|
+
],
|
|
112
|
+
"globals": [
|
|
113
|
+
"expect",
|
|
114
|
+
"test",
|
|
115
|
+
"describe",
|
|
116
|
+
"it",
|
|
117
|
+
"beforeEach",
|
|
118
|
+
"beforeAll",
|
|
119
|
+
"afterAll"
|
|
107
120
|
]
|
|
108
121
|
}
|
|
109
122
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
export * from './lib/client'
|
|
2
|
-
export * from './lib/connect'
|
|
3
|
-
export * from './lib/store'
|
|
4
|
-
export * from './lib/client-options'
|
|
5
|
-
import { MqttClient } from './lib/client'
|
|
6
|
-
export { MqttClient as Client }
|
|
7
|
-
export {
|
|
8
|
-
QoS,
|
|
9
|
-
PacketCmd,
|
|
10
|
-
IPacket,
|
|
11
|
-
IConnectPacket,
|
|
12
|
-
IPublishPacket,
|
|
13
|
-
IConnackPacket,
|
|
14
|
-
ISubscription,
|
|
15
|
-
ISubscribePacket,
|
|
16
|
-
ISubackPacket,
|
|
17
|
-
IUnsubscribePacket,
|
|
18
|
-
IUnsubackPacket,
|
|
19
|
-
IPubackPacket,
|
|
20
|
-
IPubcompPacket,
|
|
21
|
-
IPubrelPacket,
|
|
22
|
-
IPubrecPacket,
|
|
23
|
-
IPingreqPacket,
|
|
24
|
-
IPingrespPacket,
|
|
25
|
-
IDisconnectPacket,
|
|
26
|
-
Packet,
|
|
27
|
-
UserProperties
|
|
28
|
-
} from 'mqtt-packet'
|
|
1
|
+
export * from './lib/client'
|
|
2
|
+
export * from './lib/connect'
|
|
3
|
+
export * from './lib/store'
|
|
4
|
+
export * from './lib/client-options'
|
|
5
|
+
import { MqttClient } from './lib/client'
|
|
6
|
+
export { MqttClient as Client }
|
|
7
|
+
export {
|
|
8
|
+
QoS,
|
|
9
|
+
PacketCmd,
|
|
10
|
+
IPacket,
|
|
11
|
+
IConnectPacket,
|
|
12
|
+
IPublishPacket,
|
|
13
|
+
IConnackPacket,
|
|
14
|
+
ISubscription,
|
|
15
|
+
ISubscribePacket,
|
|
16
|
+
ISubackPacket,
|
|
17
|
+
IUnsubscribePacket,
|
|
18
|
+
IUnsubackPacket,
|
|
19
|
+
IPubackPacket,
|
|
20
|
+
IPubcompPacket,
|
|
21
|
+
IPubrelPacket,
|
|
22
|
+
IPubrecPacket,
|
|
23
|
+
IPingreqPacket,
|
|
24
|
+
IPingrespPacket,
|
|
25
|
+
IDisconnectPacket,
|
|
26
|
+
Packet,
|
|
27
|
+
UserProperties
|
|
28
|
+
} from 'mqtt-packet'
|
|
29
|
+
export { IMessageIdProvider } from './lib/message-id-provider'
|
|
30
|
+
export { UniqueMessageIdProvider } from './lib/unique-message-id-provider'
|