mqtt 4.2.8 → 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.
@@ -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
@@ -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
- var parts = topic.split('/')
14
-
15
- for (var 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 (var 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
- }
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
- var MqttClient = require('./lib/client')
9
- var connect = require('./lib/connect')
10
- var Store = require('./lib/store')
11
-
12
- module.exports.connect = connect
13
-
14
- // Expose MqttClient
15
- module.exports.MqttClient = MqttClient
16
- module.exports.Client = MqttClient
17
- module.exports.Store = Store
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,109 +1,124 @@
1
- {
2
- "name": "mqtt",
3
- "description": "A library for the MQTT protocol",
4
- "version": "4.2.8",
5
- "contributors": [
6
- "Adam Rudd <adamvrr@gmail.com>",
7
- "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
8
- "Siarhei Buntsevich <scarry0506@gmail.com> (https://github.com/scarry1992)",
9
- "Yoseph Maguire <yomaguir@microsoft.com> (https://github.com/YoDaMa)"
10
- ],
11
- "keywords": [
12
- "mqtt",
13
- "publish/subscribe",
14
- "publish",
15
- "subscribe"
16
- ],
17
- "license": "MIT",
18
- "repository": {
19
- "type": "git",
20
- "url": "git://github.com/mqttjs/MQTT.js.git"
21
- },
22
- "main": "mqtt.js",
23
- "types": "types/index.d.ts",
24
- "scripts": {
25
- "test": "node_modules/.bin/nyc --reporter=lcov --reporter=text ./node_modules/mocha/bin/_mocha",
26
- "pretest": "standard | snazzy",
27
- "tslint": "tslint types/**/*.d.ts",
28
- "typescript-compile-test": "tsc -p test/typescript/tsconfig.json",
29
- "typescript-compile-execute": "node test/typescript/*.js",
30
- "typescript-test": "npm run typescript-compile-test && npm run typescript-compile-execute",
31
- "browser-build": "rimraf dist/ && mkdirp dist/ && browserify mqtt.js --standalone mqtt > dist/mqtt.js && uglifyjs dist/mqtt.js --compress --mangle --output dist/mqtt.min.js",
32
- "prepare": "npm run browser-build",
33
- "browser-test": "airtap --server test/browser/server.js --local --open test/browser/test.js",
34
- "sauce-test": "airtap --server test/browser/server.js -- test/browser/test.js",
35
- "ci": "npm run tslint && npm run typescript-compile-test && npm run test && codecov"
36
- },
37
- "pre-commit": [
38
- "pretest",
39
- "tslint"
40
- ],
41
- "bin": {
42
- "mqtt_pub": "./bin/pub.js",
43
- "mqtt_sub": "./bin/sub.js",
44
- "mqtt": "./bin/mqtt.js"
45
- },
46
- "files": [
47
- "dist/",
48
- "CONTRIBUTING.md",
49
- "doc",
50
- "lib",
51
- "bin",
52
- "types",
53
- "mqtt.js"
54
- ],
55
- "engines": {
56
- "node": ">=10.0.0"
57
- },
58
- "browser": {
59
- "./mqtt.js": "./lib/connect/index.js",
60
- "fs": false,
61
- "tls": false,
62
- "net": false
63
- },
64
- "dependencies": {
65
- "commist": "^1.0.0",
66
- "concat-stream": "^2.0.0",
67
- "debug": "^4.1.1",
68
- "duplexify": "^4.1.1",
69
- "help-me": "^3.0.0",
70
- "inherits": "^2.0.3",
71
- "minimist": "^1.2.5",
72
- "mqtt-packet": "^6.8.0",
73
- "pump": "^3.0.0",
74
- "readable-stream": "^3.6.0",
75
- "reinterval": "^1.1.0",
76
- "split2": "^3.1.0",
77
- "ws": "^7.5.0",
78
- "xtend": "^4.0.2"
79
- },
80
- "devDependencies": {
81
- "@types/node": "^10.0.0",
82
- "airtap": "^3.0.0",
83
- "browserify": "^16.5.0",
84
- "chai": "^4.2.0",
85
- "codecov": "^3.0.4",
86
- "end-of-stream": "^1.4.1",
87
- "global": "^4.3.2",
88
- "aedes": "^0.42.5",
89
- "mkdirp": "^0.5.1",
90
- "mocha": "^4.1.0",
91
- "mqtt-connection": "^4.0.0",
92
- "nyc": "^15.0.1",
93
- "pre-commit": "^1.2.2",
94
- "rimraf": "^3.0.2",
95
- "should": "^13.2.1",
96
- "sinon": "^9.0.0",
97
- "snazzy": "^8.0.0",
98
- "standard": "^11.0.1",
99
- "tslint": "^5.11.0",
100
- "tslint-config-standard": "^8.0.1",
101
- "typescript": "^3.2.2",
102
- "uglify-es": "^3.3.9"
103
- },
104
- "standard": {
105
- "env": [
106
- "mocha"
107
- ]
108
- }
109
- }
1
+ {
2
+ "name": "mqtt",
3
+ "description": "A library for the MQTT protocol",
4
+ "version": "4.3.0",
5
+ "contributors": [
6
+ "Adam Rudd <adamvrr@gmail.com>",
7
+ "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
8
+ "Siarhei Buntsevich <scarry0506@gmail.com> (https://github.com/scarry1992)",
9
+ "Yoseph Maguire <yomaguir@microsoft.com> (https://github.com/YoDaMa)"
10
+ ],
11
+ "keywords": [
12
+ "mqtt",
13
+ "publish/subscribe",
14
+ "publish",
15
+ "subscribe"
16
+ ],
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git://github.com/mqttjs/MQTT.js.git"
21
+ },
22
+ "main": "mqtt.js",
23
+ "types": "types/index.d.ts",
24
+ "scripts": {
25
+ "test": "node_modules/.bin/nyc --reporter=lcov --reporter=text ./node_modules/mocha/bin/_mocha --exit",
26
+ "pretest": "standard | snazzy",
27
+ "tslint": "tslint types/**/*.d.ts",
28
+ "typescript-compile-test": "tsc -p test/typescript/tsconfig.json",
29
+ "typescript-compile-execute": "node test/typescript/*.js",
30
+ "typescript-test": "npm run typescript-compile-test && npm run typescript-compile-execute",
31
+ "browser-build": "rimraf dist/ && mkdirp dist/ && browserify mqtt.js --standalone mqtt > dist/mqtt.js && uglifyjs dist/mqtt.js --compress --mangle --output dist/mqtt.min.js",
32
+ "prepare": "npm run browser-build",
33
+ "browser-test": "airtap --server test/browser/server.js --local --open test/browser/test.js",
34
+ "sauce-test": "airtap --server test/browser/server.js -- test/browser/test.js",
35
+ "ci": "npm run tslint && npm run typescript-compile-test && npm run test && codecov"
36
+ },
37
+ "pre-commit": [
38
+ "pretest",
39
+ "tslint"
40
+ ],
41
+ "bin": {
42
+ "mqtt_pub": "./bin/pub.js",
43
+ "mqtt_sub": "./bin/sub.js",
44
+ "mqtt": "./bin/mqtt.js"
45
+ },
46
+ "files": [
47
+ "dist/",
48
+ "CONTRIBUTING.md",
49
+ "doc",
50
+ "lib",
51
+ "bin",
52
+ "types",
53
+ "mqtt.js"
54
+ ],
55
+ "engines": {
56
+ "node": ">=10.0.0"
57
+ },
58
+ "browser": {
59
+ "./mqtt.js": "./lib/connect/index.js",
60
+ "fs": false,
61
+ "tls": false,
62
+ "net": false
63
+ },
64
+ "dependencies": {
65
+ "babel-eslint": "^10.1.0",
66
+ "collections": "^5.1.12",
67
+ "commist": "^1.0.0",
68
+ "concat-stream": "^2.0.0",
69
+ "debug": "^4.1.1",
70
+ "duplexify": "^4.1.1",
71
+ "help-me": "^3.0.0",
72
+ "inherits": "^2.0.3",
73
+ "minimist": "^1.2.5",
74
+ "mqtt-packet": "^6.8.0",
75
+ "number-allocator": "^1.0.7",
76
+ "pump": "^3.0.0",
77
+ "readable-stream": "^3.6.0",
78
+ "reinterval": "^1.1.0",
79
+ "rfdc": "^1.3.0",
80
+ "split2": "^3.1.0",
81
+ "ws": "^7.5.5",
82
+ "xtend": "^4.0.2"
83
+ },
84
+ "devDependencies": {
85
+ "@types/node": "^10.0.0",
86
+ "@types/ws": "^7.4.7",
87
+ "aedes": "^0.42.5",
88
+ "airtap": "^4.0.4",
89
+ "browserify": "^16.5.0",
90
+ "chai": "^4.2.0",
91
+ "codecov": "^3.0.4",
92
+ "end-of-stream": "^1.4.1",
93
+ "global": "^4.3.2",
94
+ "mkdirp": "^0.5.1",
95
+ "mocha": "^9.1.3",
96
+ "mqtt-connection": "^4.0.0",
97
+ "nyc": "^15.0.1",
98
+ "pre-commit": "^1.2.2",
99
+ "rimraf": "^3.0.2",
100
+ "should": "^13.2.1",
101
+ "sinon": "^9.0.0",
102
+ "snazzy": "^8.0.0",
103
+ "standard": "^16.0.4",
104
+ "tslint": "^5.11.0",
105
+ "tslint-config-standard": "^8.0.1",
106
+ "typescript": "^3.2.2",
107
+ "uglify-es": "^3.3.9"
108
+ },
109
+ "standard": {
110
+ "parser": "babel-eslint",
111
+ "env": [
112
+ "mocha"
113
+ ],
114
+ "globals": [
115
+ "expect",
116
+ "test",
117
+ "describe",
118
+ "it",
119
+ "beforeEach",
120
+ "beforeAll",
121
+ "afterAll"
122
+ ]
123
+ }
124
+ }
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'