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.
- package/README.md +42 -26
- package/bin/mqtt.js +7 -7
- package/bin/pub.js +139 -126
- package/bin/sub.js +127 -109
- package/dist/mqtt.js +5512 -4899
- package/dist/mqtt.min.js +4 -4
- package/lib/client.js +1641 -1891
- package/lib/connect/ali.js +100 -103
- package/lib/connect/index.js +135 -129
- package/lib/connect/tcp.js +7 -8
- package/lib/connect/tls.js +45 -41
- package/lib/connect/ws.js +240 -232
- package/lib/connect/wx.js +117 -118
- package/lib/default-message-id-provider.js +56 -62
- package/lib/handlers/ack.js +153 -0
- package/lib/handlers/auth.js +34 -0
- package/lib/handlers/connack.js +51 -0
- package/lib/handlers/index.js +66 -0
- package/lib/handlers/publish.js +167 -0
- package/lib/handlers/pubrel.js +24 -0
- package/lib/is-browser.js +5 -5
- package/lib/store.js +104 -111
- package/lib/topic-alias-recv.js +33 -36
- package/lib/topic-alias-send.js +71 -74
- package/lib/unique-message-id-provider.js +53 -57
- package/lib/validations.js +27 -29
- package/package.json +39 -41
- package/types/lib/client-options.d.ts +17 -1
- package/types/lib/client.d.ts +13 -1
|
@@ -1,65 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const NumberAllocator = require('number-allocator').NumberAllocator
|
|
1
|
+
const { NumberAllocator } = require('number-allocator')
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
4
|
* UniqueMessageAllocator constructor
|
|
7
5
|
* @constructor
|
|
8
6
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
UniqueMessageIdProvider.prototype.clear = function () {
|
|
62
|
-
this.numberAllocator.clear()
|
|
7
|
+
class UniqueMessageIdProvider {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.numberAllocator = new NumberAllocator(1, 65535)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* allocate
|
|
14
|
+
*
|
|
15
|
+
* Get the next messageId.
|
|
16
|
+
* @return if messageId is fully allocated then return null,
|
|
17
|
+
* otherwise return the smallest usable unsigned int messageId.
|
|
18
|
+
*/
|
|
19
|
+
allocate() {
|
|
20
|
+
this.lastId = this.numberAllocator.alloc()
|
|
21
|
+
return this.lastId
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* getLastAllocated
|
|
26
|
+
* Get the last allocated messageId.
|
|
27
|
+
* @return unsigned int
|
|
28
|
+
*/
|
|
29
|
+
getLastAllocated() {
|
|
30
|
+
return this.lastId
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* register
|
|
35
|
+
* Register messageId. If success return true, otherwise return false.
|
|
36
|
+
* @param { unsigned int } - messageId to register,
|
|
37
|
+
* @return boolean
|
|
38
|
+
*/
|
|
39
|
+
register(messageId) {
|
|
40
|
+
return this.numberAllocator.use(messageId)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* deallocate
|
|
45
|
+
* Deallocate messageId.
|
|
46
|
+
* @param { unsigned int } - messageId to deallocate,
|
|
47
|
+
*/
|
|
48
|
+
deallocate(messageId) {
|
|
49
|
+
this.numberAllocator.free(messageId)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* clear
|
|
54
|
+
* Deallocate all messageIds.
|
|
55
|
+
*/
|
|
56
|
+
clear() {
|
|
57
|
+
this.numberAllocator.clear()
|
|
58
|
+
}
|
|
63
59
|
}
|
|
64
60
|
|
|
65
61
|
module.exports = UniqueMessageIdProvider
|
package/lib/validations.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Validate a topic to see if it's valid or not.
|
|
5
3
|
* A topic is valid if it follow below rules:
|
|
@@ -9,44 +7,44 @@
|
|
|
9
7
|
* @param {String} topic - A topic
|
|
10
8
|
* @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false.
|
|
11
9
|
*/
|
|
12
|
-
function validateTopic
|
|
13
|
-
|
|
10
|
+
function validateTopic(topic) {
|
|
11
|
+
const parts = topic.split('/')
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
for (let i = 0; i < parts.length; i++) {
|
|
14
|
+
if (parts[i] === '+') {
|
|
15
|
+
continue
|
|
16
|
+
}
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
if (parts[i] === '#') {
|
|
19
|
+
// for Rule #2
|
|
20
|
+
return i === parts.length - 1
|
|
21
|
+
}
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) {
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
26
|
+
}
|
|
29
27
|
|
|
30
|
-
|
|
28
|
+
return true
|
|
31
29
|
}
|
|
32
30
|
|
|
33
31
|
/**
|
|
34
32
|
* Validate an array of topics to see if any of them is valid or not
|
|
35
|
-
|
|
33
|
+
* @param {Array} topics - Array of topics
|
|
36
34
|
* @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
|
|
37
35
|
*/
|
|
38
|
-
function validateTopics
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
36
|
+
function validateTopics(topics) {
|
|
37
|
+
if (topics.length === 0) {
|
|
38
|
+
return 'empty_topic_list'
|
|
39
|
+
}
|
|
40
|
+
for (let i = 0; i < topics.length; i++) {
|
|
41
|
+
if (!validateTopic(topics[i])) {
|
|
42
|
+
return topics[i]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null
|
|
48
46
|
}
|
|
49
47
|
|
|
50
48
|
module.exports = {
|
|
51
|
-
|
|
49
|
+
validateTopics,
|
|
52
50
|
}
|
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": "5.0.0-beta.
|
|
4
|
+
"version": "5.0.0-beta.3",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Adam Rudd <adamvrr@gmail.com>",
|
|
7
7
|
"Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"main": "mqtt.js",
|
|
24
24
|
"types": "types/index.d.ts",
|
|
25
25
|
"scripts": {
|
|
26
|
-
"
|
|
26
|
+
"lint": "eslint --ext .js .",
|
|
27
|
+
"lint-fix": "eslint --fix --ext .js .",
|
|
27
28
|
"typescript-compile-test": "tsc -p test/typescript/tsconfig.json",
|
|
28
29
|
"typescript-compile-execute": "node test/typescript/broker-connect-subscribe-and-publish.js",
|
|
29
30
|
"browser-build": "rimraf dist/ && mkdirp dist/ && browserify mqtt.js --standalone mqtt > dist/mqtt.js && terser dist/mqtt.js --compress --mangle --output dist/mqtt.min.js",
|
|
@@ -62,7 +63,7 @@
|
|
|
62
63
|
}
|
|
63
64
|
},
|
|
64
65
|
"pre-commit": [
|
|
65
|
-
"
|
|
66
|
+
"lint"
|
|
66
67
|
],
|
|
67
68
|
"bin": {
|
|
68
69
|
"mqtt_pub": "./bin/pub.js",
|
|
@@ -88,57 +89,54 @@
|
|
|
88
89
|
"net": false
|
|
89
90
|
},
|
|
90
91
|
"dependencies": {
|
|
91
|
-
"commist": "^
|
|
92
|
+
"commist": "^3.2.0",
|
|
92
93
|
"concat-stream": "^2.0.0",
|
|
93
|
-
"debug": "^4.
|
|
94
|
-
"duplexify": "^4.1.
|
|
95
|
-
"help-me": "^
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
-
"mqtt-packet": "^8.1.2",
|
|
94
|
+
"debug": "^4.3.4",
|
|
95
|
+
"duplexify": "^4.1.2",
|
|
96
|
+
"help-me": "^4.2.0",
|
|
97
|
+
"lru-cache": "^7.18.3",
|
|
98
|
+
"minimist": "^1.2.8",
|
|
99
|
+
"mqtt-packet": "^8.2.0",
|
|
100
100
|
"number-allocator": "^1.0.14",
|
|
101
|
-
"
|
|
102
|
-
"readable-stream": "^4.1.0",
|
|
101
|
+
"readable-stream": "^4.4.2",
|
|
103
102
|
"reinterval": "^1.1.0",
|
|
104
103
|
"rfdc": "^1.3.0",
|
|
105
|
-
"split2": "^
|
|
106
|
-
"ws": "^
|
|
107
|
-
"xtend": "^4.0.2"
|
|
104
|
+
"split2": "^4.2.0",
|
|
105
|
+
"ws": "^8.13.0"
|
|
108
106
|
},
|
|
109
107
|
"devDependencies": {
|
|
110
|
-
"@release-it/conventional-changelog": "^
|
|
111
|
-
"@types/node": "^
|
|
112
|
-
"@types/tape": "^
|
|
113
|
-
"@types/ws": "^
|
|
114
|
-
"aedes": "^0.46.2",
|
|
108
|
+
"@release-it/conventional-changelog": "^6.0.0",
|
|
109
|
+
"@types/node": "^20.4.0",
|
|
110
|
+
"@types/tape": "^5.6.0",
|
|
111
|
+
"@types/ws": "^8.5.5",
|
|
115
112
|
"airtap": "^4.0.4",
|
|
113
|
+
"airtap-playwright": "^1.0.1",
|
|
116
114
|
"browserify": "^17.0.0",
|
|
117
|
-
"chai": "^4.
|
|
115
|
+
"chai": "^4.3.7",
|
|
118
116
|
"chokidar": "^3.5.3",
|
|
119
|
-
"codecov": "^3.
|
|
117
|
+
"codecov": "^3.8.2",
|
|
120
118
|
"conventional-changelog-cli": "^3.0.0",
|
|
121
|
-
"end-of-stream": "^1.4.
|
|
122
|
-
"
|
|
123
|
-
"
|
|
124
|
-
"
|
|
125
|
-
"
|
|
119
|
+
"end-of-stream": "^1.4.4",
|
|
120
|
+
"eslint": "^8.45.0",
|
|
121
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
122
|
+
"eslint-config-prettier": "^8.8.0",
|
|
123
|
+
"eslint-plugin-import": "^2.27.5",
|
|
124
|
+
"eslint-plugin-prettier": "^5.0.0",
|
|
125
|
+
"global": "^4.4.0",
|
|
126
|
+
"mkdirp": "^3.0.1",
|
|
127
|
+
"mocha": "^10.2.0",
|
|
128
|
+
"mqtt-connection": "^4.1.0",
|
|
126
129
|
"mqtt-level-store": "^3.1.0",
|
|
127
|
-
"nyc": "^15.0
|
|
130
|
+
"nyc": "^15.1.0",
|
|
128
131
|
"pre-commit": "^1.2.2",
|
|
132
|
+
"prettier": "^3.0.0",
|
|
129
133
|
"release-it": "^15.11.0",
|
|
130
|
-
"rimraf": "^
|
|
131
|
-
"should": "^13.2.
|
|
132
|
-
"sinon": "^
|
|
134
|
+
"rimraf": "^5.0.1",
|
|
135
|
+
"should": "^13.2.3",
|
|
136
|
+
"sinon": "^15.2.0",
|
|
133
137
|
"snazzy": "^9.0.0",
|
|
134
|
-
"
|
|
135
|
-
"
|
|
136
|
-
"
|
|
137
|
-
"typescript": "^4.5.5"
|
|
138
|
-
},
|
|
139
|
-
"standard": {
|
|
140
|
-
"env": [
|
|
141
|
-
"mocha"
|
|
142
|
-
]
|
|
138
|
+
"tape": "^5.6.4",
|
|
139
|
+
"terser": "^5.18.2",
|
|
140
|
+
"typescript": "^5.1.6"
|
|
143
141
|
}
|
|
144
142
|
}
|
|
@@ -61,6 +61,8 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
61
61
|
outgoingStore?: Store
|
|
62
62
|
queueQoSZero?: boolean
|
|
63
63
|
|
|
64
|
+
log?: (...args: any[]) => void
|
|
65
|
+
|
|
64
66
|
autoUseTopicAlias?: boolean
|
|
65
67
|
autoAssignTopicAlias?: boolean
|
|
66
68
|
|
|
@@ -105,7 +107,12 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
105
107
|
responseTopic?: string,
|
|
106
108
|
correlationData?: Buffer,
|
|
107
109
|
userProperties?: UserProperties
|
|
108
|
-
}
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
authPacket?: any
|
|
113
|
+
|
|
114
|
+
/** Prevent to call `connect` in constructor */
|
|
115
|
+
manualConnect?: boolean
|
|
109
116
|
}
|
|
110
117
|
transformWsUrl?: (url: string, options: IClientOptions, client: MqttClient) => string,
|
|
111
118
|
properties?: {
|
|
@@ -196,6 +203,15 @@ export interface IClientSubscribeOptions {
|
|
|
196
203
|
userProperties?: UserProperties
|
|
197
204
|
}
|
|
198
205
|
}
|
|
206
|
+
export interface IClientSubscribeProperties {
|
|
207
|
+
/*
|
|
208
|
+
* MQTT 5.0 properies object of subscribe
|
|
209
|
+
* */
|
|
210
|
+
properties?: {
|
|
211
|
+
subscriptionIdentifier?: number,
|
|
212
|
+
userProperties?: UserProperties
|
|
213
|
+
}
|
|
214
|
+
}
|
|
199
215
|
export interface IClientReconnectOptions {
|
|
200
216
|
/**
|
|
201
217
|
* a Store for the incoming packets
|
package/types/lib/client.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
IClientOptions,
|
|
6
6
|
IClientPublishOptions,
|
|
7
7
|
IClientSubscribeOptions,
|
|
8
|
+
IClientSubscribeProperties,
|
|
8
9
|
IClientReconnectOptions
|
|
9
10
|
} from './client-options'
|
|
10
11
|
import { Store } from './store'
|
|
@@ -98,6 +99,8 @@ export declare class MqttClient extends events.EventEmitter {
|
|
|
98
99
|
public options: IClientOptions
|
|
99
100
|
public queueQoSZero: boolean
|
|
100
101
|
|
|
102
|
+
static defaultId (): string
|
|
103
|
+
|
|
101
104
|
constructor (streamBuilder: (client: MqttClient) => IStream, options: IClientOptions)
|
|
102
105
|
|
|
103
106
|
public on (event: 'connect', cb: OnConnectCallback): this
|
|
@@ -118,6 +121,11 @@ export declare class MqttClient extends events.EventEmitter {
|
|
|
118
121
|
public once (event: 'end' | 'reconnect' | 'offline' | 'outgoingEmpty', cb: () => void): this
|
|
119
122
|
public once (event: string, cb: Function): this
|
|
120
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Setup the event handlers in the inner stream, sends `connect` and `auth` packets
|
|
126
|
+
*/
|
|
127
|
+
public connect(): this
|
|
128
|
+
|
|
121
129
|
/**
|
|
122
130
|
* publish - publish <message> to <topic>
|
|
123
131
|
*
|
|
@@ -164,7 +172,11 @@ export declare class MqttClient extends events.EventEmitter {
|
|
|
164
172
|
*/
|
|
165
173
|
public subscribe (topic:
|
|
166
174
|
string
|
|
167
|
-
| string[]
|
|
175
|
+
| string[]
|
|
176
|
+
| ISubscriptionMap,
|
|
177
|
+
opts:
|
|
178
|
+
IClientSubscribeOptions
|
|
179
|
+
| IClientSubscribeProperties, callback?: ClientSubscribeCallback): this
|
|
168
180
|
public subscribe (topic:
|
|
169
181
|
string
|
|
170
182
|
| string[]
|