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.
@@ -1,65 +1,61 @@
1
- 'use strict'
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
- 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()
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
@@ -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 (topic) {
13
- const parts = topic.split('/')
10
+ function validateTopic(topic) {
11
+ const parts = topic.split('/')
14
12
 
15
- for (let i = 0; i < parts.length; i++) {
16
- if (parts[i] === '+') {
17
- continue
18
- }
13
+ for (let i = 0; i < parts.length; i++) {
14
+ if (parts[i] === '+') {
15
+ continue
16
+ }
19
17
 
20
- if (parts[i] === '#') {
21
- // for Rule #2
22
- return i === parts.length - 1
23
- }
18
+ if (parts[i] === '#') {
19
+ // for Rule #2
20
+ return i === parts.length - 1
21
+ }
24
22
 
25
- if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) {
26
- return false
27
- }
28
- }
23
+ if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) {
24
+ return false
25
+ }
26
+ }
29
27
 
30
- return true
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
- * @param {Array} topics - Array of topics
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 (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
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
- validateTopics: validateTopics
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.2",
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
- "pretest": "standard | snazzy",
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
- "pretest"
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": "^1.0.0",
92
+ "commist": "^3.2.0",
92
93
  "concat-stream": "^2.0.0",
93
- "debug": "^4.1.1",
94
- "duplexify": "^4.1.1",
95
- "help-me": "^3.0.0",
96
- "inherits": "^2.0.3",
97
- "lru-cache": "^6.0.0",
98
- "minimist": "^1.2.6",
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
- "pump": "^3.0.0",
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": "^3.1.0",
106
- "ws": "^7.5.5",
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": "^5.1.1",
111
- "@types/node": "^12.20.55",
112
- "@types/tape": "^4.13.2",
113
- "@types/ws": "^7.4.7",
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.2.0",
115
+ "chai": "^4.3.7",
118
116
  "chokidar": "^3.5.3",
119
- "codecov": "^3.0.4",
117
+ "codecov": "^3.8.2",
120
118
  "conventional-changelog-cli": "^3.0.0",
121
- "end-of-stream": "^1.4.1",
122
- "global": "^4.3.2",
123
- "mkdirp": "^0.5.1",
124
- "mocha": "^9.2.0",
125
- "mqtt-connection": "^4.0.0",
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.1",
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": "^3.0.2",
131
- "should": "^13.2.1",
132
- "sinon": "^9.0.0",
134
+ "rimraf": "^5.0.1",
135
+ "should": "^13.2.3",
136
+ "sinon": "^15.2.0",
133
137
  "snazzy": "^9.0.0",
134
- "standard": "^16.0.4",
135
- "tape": "^5.5.2",
136
- "terser": "^5.14.2",
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
@@ -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[], opts: IClientSubscribeOptions, callback?: ClientSubscribeCallback): this
175
+ | string[]
176
+ | ISubscriptionMap,
177
+ opts:
178
+ IClientSubscribeOptions
179
+ | IClientSubscribeProperties, callback?: ClientSubscribeCallback): this
168
180
  public subscribe (topic:
169
181
  string
170
182
  | string[]