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.
@@ -1,240 +1,261 @@
1
- /// <reference types="node" />
2
-
3
- import * as events from 'events'
4
- import {
5
- IClientOptions,
6
- IClientPublishOptions,
7
- IClientSubscribeOptions,
8
- IClientReconnectOptions
9
- } from './client-options'
10
- import { Store } from './store'
11
- import { Packet, IConnectPacket, IPublishPacket, IDisconnectPacket, QoS } from 'mqtt-packet'
12
-
13
- export interface ISubscriptionGrant {
14
- /**
15
- * is a subscribed to topic
16
- */
17
- topic: string
18
- /**
19
- * is the granted qos level on it, may return 128 on error
20
- */
21
- qos: QoS | number
22
- /*
23
- * no local flag
24
- * */
25
- nl?: boolean,
26
- /*
27
- * Retain As Published flag
28
- * */
29
- rap?: boolean,
30
- /*
31
- * Retain Handling option
32
- * */
33
- rh?: number
34
- }
35
- export interface ISubscriptionRequest {
36
- /**
37
- * is a subscribed to topic
38
- */
39
- topic: string
40
- /**
41
- * is the granted qos level on it
42
- */
43
- qos: QoS
44
- /*
45
- * no local flag
46
- * */
47
- nl?: boolean,
48
- /*
49
- * Retain As Published flag
50
- * */
51
- rap?: boolean,
52
- /*
53
- * Retain Handling option
54
- * */
55
- rh?: number
56
- }
57
- export interface ISubscriptionMap {
58
- /**
59
- * object which has topic names as object keys and as value the options, like {'test1': {qos: 0}, 'test2': {qos: 2}}.
60
- */
61
- [topic: string]: {
62
- qos: QoS,
63
- nl?: boolean,
64
- rap?: boolean,
65
- rh?: number
66
- }
67
- }
68
-
69
- export declare type OnConnectCallback = (packet: IConnectPacket) => void
70
- export declare type OnDisconnectCallback = (packet: IDisconnectPacket) => void
71
- export declare type ClientSubscribeCallback = (err: Error, granted: ISubscriptionGrant[]) => void
72
- export declare type OnMessageCallback = (topic: string, payload: Buffer, packet: IPublishPacket) => void
73
- export declare type OnPacketCallback = (packet: Packet) => void
74
- export declare type OnCloseCallback = () => void
75
- export declare type OnErrorCallback = (error: Error) => void
76
- export declare type PacketCallback = (error?: Error, packet?: Packet) => any
77
- export declare type CloseCallback = (error?: Error) => void
78
-
79
- export interface IStream extends events.EventEmitter {
80
- pipe (to: any): any
81
- destroy (): any
82
- end (): any
83
- }
84
- /**
85
- * MqttClient constructor
86
- *
87
- * @param {Stream} stream - stream
88
- * @param {Object} [options] - connection options
89
- * (see Connection#connect)
90
- */
91
- export declare class MqttClient extends events.EventEmitter {
92
- public connected: boolean
93
- public disconnecting: boolean
94
- public disconnected: boolean
95
- public reconnecting: boolean
96
- public incomingStore: Store
97
- public outgoingStore: Store
98
- public options: IClientOptions
99
- public queueQoSZero: boolean
100
-
101
- constructor (streamBuilder: (client: MqttClient) => IStream, options: IClientOptions)
102
-
103
- public on (event: 'connect', cb: OnConnectCallback): this
104
- public on (event: 'message', cb: OnMessageCallback): this
105
- public on (event: 'packetsend' | 'packetreceive', cb: OnPacketCallback): this
106
- public on (event: 'disconnect', cb: OnDisconnectCallback): this
107
- public on (event: 'error', cb: OnErrorCallback): this
108
- public on (event: 'close', cb: OnCloseCallback): this
109
- public on (event: 'end' | 'reconnect' | 'offline' | 'outgoingEmpty', cb: () => void): this
110
- public on (event: string, cb: Function): this
111
-
112
- public once (event: 'connect', cb: OnConnectCallback): this
113
- public once (event: 'message', cb: OnMessageCallback): this
114
- public once (event: 'packetsend' | 'packetreceive', cb: OnPacketCallback): this
115
- public once (event: 'disconnect', cb: OnDisconnectCallback): this
116
- public once (event: 'error', cb: OnErrorCallback): this
117
- public once (event: 'close', cb: OnCloseCallback): this
118
- public once (event: 'end' | 'reconnect' | 'offline' | 'outgoingEmpty', cb: () => void): this
119
- public once (event: string, cb: Function): this
120
-
121
- /**
122
- * publish - publish <message> to <topic>
123
- *
124
- * @param {String} topic - topic to publish to
125
- * @param {(String|Buffer)} message - message to publish
126
- *
127
- * @param {Object} [opts] - publish options, includes:
128
- * @param {Number} [opts.qos] - qos level to publish on
129
- * @param {Boolean} [opts.retain] - whether or not to retain the message
130
- * @param {Function}[opts.cbStorePut] - function(){}
131
- * called when message is put into `outgoingStore`
132
- *
133
- * @param {Function} [callback] - function(err){}
134
- * called when publish succeeds or fails
135
- *
136
- * @returns {Client} this - for chaining
137
- * @api public
138
- *
139
- * @example client.publish('topic', 'message')
140
- * @example
141
- * client.publish('topic', 'message', {qos: 1, retain: true})
142
- * @example client.publish('topic', 'message', console.log)
143
- */
144
- public publish (topic: string, message: string | Buffer,
145
- opts: IClientPublishOptions, callback?: PacketCallback): this
146
- public publish (topic: string, message: string | Buffer,
147
- callback?: PacketCallback): this
148
-
149
- /**
150
- * subscribe - subscribe to <topic>
151
- *
152
- * @param {String, Array, Object} topic - topic(s) to subscribe to, supports objects in the form {'topic': qos}
153
- * @param {Object} [opts] - optional subscription options, includes:
154
- * @param {Number} [opts.qos] - subscribe qos level
155
- * @param {Function} [callback] - function(err, granted){} where:
156
- * {Error} err - subscription error (none at the moment!)
157
- * {Array} granted - array of {topic: 't', qos: 0}
158
- * @returns {MqttClient} this - for chaining
159
- * @api public
160
- * @example client.subscribe('topic')
161
- * @example client.subscribe('topic', {qos: 1})
162
- * @example client.subscribe({'topic': 0, 'topic2': 1}, console.log)
163
- * @example client.subscribe('topic', console.log)
164
- */
165
- public subscribe (topic:
166
- string
167
- | string[], opts: IClientSubscribeOptions, callback?: ClientSubscribeCallback): this
168
- public subscribe (topic:
169
- string
170
- | string[]
171
- | ISubscriptionMap, callback?: ClientSubscribeCallback): this
172
-
173
- /**
174
- * unsubscribe - unsubscribe from topic(s)
175
- *
176
- * @param {String, Array} topic - topics to unsubscribe from
177
- * @param {Object} opts - opts of unsubscribe
178
- * @param {Function} [callback] - callback fired on unsuback
179
- * @returns {MqttClient} this - for chaining
180
- * @api public
181
- * @example client.unsubscribe('topic')
182
- * @example client.unsubscribe('topic', console.log)
183
- * @example client.unsubscribe('topic', opts, console.log)
184
- */
185
- public unsubscribe (topic: string | string[], opts?: Object, callback?: PacketCallback): this
186
-
187
- /**
188
- * end - close connection
189
- *
190
- * @returns {MqttClient} this - for chaining
191
- * @param {Boolean} force - do not wait for all in-flight messages to be acked
192
- * @param {Object} opts - opts disconnect
193
- * @param {Function} cb - called when the client has been closed
194
- *
195
- * @api public
196
- */
197
- public end (force?: boolean, opts?: Object, cb?: CloseCallback): this
198
-
199
- /**
200
- * removeOutgoingMessage - remove a message in outgoing store
201
- * the outgoing callback will be called withe Error('Message removed') if the message is removed
202
- *
203
- * @param {Number} mid - messageId to remove message
204
- * @returns {MqttClient} this - for chaining
205
- * @api public
206
- *
207
- * @example client.removeOutgoingMessage(client.getLastMessageId());
208
- */
209
- public removeOutgoingMessage (mid: number): this
210
-
211
- /**
212
- * reconnect - connect again using the same options as connect()
213
- *
214
- * @param {Object} [opts] - optional reconnect options, includes:
215
- * {Store} incomingStore - a store for the incoming packets
216
- * {Store} outgoingStore - a store for the outgoing packets
217
- * if opts is not given, current stores are used
218
- *
219
- * @returns {MqttClient} this - for chaining
220
- *
221
- * @api public
222
- */
223
- public reconnect (opts?: IClientReconnectOptions): this
224
-
225
- /**
226
- * Handle messages with backpressure support, one at a time.
227
- * Override at will.
228
- *
229
- * @param packet packet the packet
230
- * @param callback callback call when finished
231
- * @api public
232
- */
233
- public handleMessage (packet: Packet, callback: PacketCallback): void
234
-
235
- /**
236
- * getLastMessageId
237
- */
238
- public getLastMessageId (): number
239
- }
240
- export { IClientOptions }
1
+ /// <reference types="node" />
2
+
3
+ import * as events from 'events'
4
+ import {
5
+ IClientOptions,
6
+ IClientPublishOptions,
7
+ IClientSubscribeOptions,
8
+ IClientReconnectOptions
9
+ } from './client-options'
10
+ import { Store } from './store'
11
+ import { IAuthPacket, IConnectPacket, IPublishPacket, IDisconnectPacket, IConnackPacket, Packet, QoS } from 'mqtt-packet'
12
+
13
+ export interface ISubscriptionGrant {
14
+ /**
15
+ * is a subscribed to topic
16
+ */
17
+ topic: string
18
+ /**
19
+ * is the granted qos level on it, may return 128 on error
20
+ */
21
+ qos: QoS | number
22
+ /*
23
+ * no local flag
24
+ * */
25
+ nl?: boolean,
26
+ /*
27
+ * Retain As Published flag
28
+ * */
29
+ rap?: boolean,
30
+ /*
31
+ * Retain Handling option
32
+ * */
33
+ rh?: number
34
+ }
35
+ export interface ISubscriptionRequest {
36
+ /**
37
+ * is a subscribed to topic
38
+ */
39
+ topic: string
40
+ /**
41
+ * is the granted qos level on it
42
+ */
43
+ qos: QoS
44
+ /*
45
+ * no local flag
46
+ * */
47
+ nl?: boolean,
48
+ /*
49
+ * Retain As Published flag
50
+ * */
51
+ rap?: boolean,
52
+ /*
53
+ * Retain Handling option
54
+ * */
55
+ rh?: number
56
+ }
57
+ export interface ISubscriptionMap {
58
+ /**
59
+ * object which has topic names as object keys and as value the options, like {'test1': {qos: 0}, 'test2': {qos: 2}}.
60
+ */
61
+ [topic: string]: {
62
+ qos: QoS,
63
+ nl?: boolean,
64
+ rap?: boolean,
65
+ rh?: number
66
+ }
67
+ }
68
+
69
+ export declare type OnConnectCallback = (packet: IConnackPacket) => void
70
+ export declare type OnDisconnectCallback = (packet: IDisconnectPacket) => void
71
+ export declare type ClientSubscribeCallback = (err: Error, granted: ISubscriptionGrant[]) => void
72
+ export declare type OnMessageCallback = (topic: string, payload: Buffer, packet: IPublishPacket) => void
73
+ export declare type OnPacketCallback = (packet: Packet) => void
74
+ export declare type OnCloseCallback = () => void
75
+ export declare type OnErrorCallback = (error: Error) => void
76
+ export declare type PacketCallback = (error?: Error, packet?: Packet) => any
77
+ export declare type CloseCallback = (error?: Error) => void
78
+
79
+ export interface IStream extends events.EventEmitter {
80
+ pipe (to: any): any
81
+ destroy (): any
82
+ end (): any
83
+ }
84
+ /**
85
+ * MqttClient constructor
86
+ *
87
+ * @param {Stream} stream - stream
88
+ * @param {Object} [options] - connection options
89
+ * (see Connection#connect)
90
+ */
91
+ export declare class MqttClient extends events.EventEmitter {
92
+ public connected: boolean
93
+ public disconnecting: boolean
94
+ public disconnected: boolean
95
+ public reconnecting: boolean
96
+ public incomingStore: Store
97
+ public outgoingStore: Store
98
+ public options: IClientOptions
99
+ public queueQoSZero: boolean
100
+
101
+ constructor (streamBuilder: (client: MqttClient) => IStream, options: IClientOptions)
102
+
103
+ public on (event: 'connect', cb: OnConnectCallback): this
104
+ public on (event: 'message', cb: OnMessageCallback): this
105
+ public on (event: 'packetsend' | 'packetreceive', cb: OnPacketCallback): this
106
+ public on (event: 'disconnect', cb: OnDisconnectCallback): this
107
+ public on (event: 'error', cb: OnErrorCallback): this
108
+ public on (event: 'close', cb: OnCloseCallback): this
109
+ public on (event: 'end' | 'reconnect' | 'offline' | 'outgoingEmpty', cb: () => void): this
110
+ public on (event: string, cb: Function): this
111
+
112
+ public once (event: 'connect', cb: OnConnectCallback): this
113
+ public once (event: 'message', cb: OnMessageCallback): this
114
+ public once (event: 'packetsend' | 'packetreceive', cb: OnPacketCallback): this
115
+ public once (event: 'disconnect', cb: OnDisconnectCallback): this
116
+ public once (event: 'error', cb: OnErrorCallback): this
117
+ public once (event: 'close', cb: OnCloseCallback): this
118
+ public once (event: 'end' | 'reconnect' | 'offline' | 'outgoingEmpty', cb: () => void): this
119
+ public once (event: string, cb: Function): this
120
+
121
+ /**
122
+ * publish - publish <message> to <topic>
123
+ *
124
+ * @param {String} topic - topic to publish to
125
+ * @param {(String|Buffer)} message - message to publish
126
+ *
127
+ * @param {Object} [opts] - publish options, includes:
128
+ * @param {Number} [opts.qos] - qos level to publish on
129
+ * @param {Boolean} [opts.retain] - whether or not to retain the message
130
+ * @param {Function}[opts.cbStorePut] - function(){}
131
+ * called when message is put into `outgoingStore`
132
+ *
133
+ * @param {Function} [callback] - function(err){}
134
+ * called when publish succeeds or fails
135
+ *
136
+ * @returns {Client} this - for chaining
137
+ * @api public
138
+ *
139
+ * @example client.publish('topic', 'message')
140
+ * @example
141
+ * client.publish('topic', 'message', {qos: 1, retain: true})
142
+ * @example client.publish('topic', 'message', console.log)
143
+ */
144
+ public publish (topic: string, message: string | Buffer,
145
+ opts: IClientPublishOptions, callback?: PacketCallback): this
146
+ public publish (topic: string, message: string | Buffer,
147
+ callback?: PacketCallback): this
148
+
149
+ /**
150
+ * subscribe - subscribe to <topic>
151
+ *
152
+ * @param {String, Array, Object} topic - topic(s) to subscribe to, supports objects in the form {'topic': qos}
153
+ * @param {Object} [opts] - optional subscription options, includes:
154
+ * @param {Number} [opts.qos] - subscribe qos level
155
+ * @param {Function} [callback] - function(err, granted){} where:
156
+ * {Error} err - subscription error (none at the moment!)
157
+ * {Array} granted - array of {topic: 't', qos: 0}
158
+ * @returns {MqttClient} this - for chaining
159
+ * @api public
160
+ * @example client.subscribe('topic')
161
+ * @example client.subscribe('topic', {qos: 1})
162
+ * @example client.subscribe({'topic': 0, 'topic2': 1}, console.log)
163
+ * @example client.subscribe('topic', console.log)
164
+ */
165
+ public subscribe (topic:
166
+ string
167
+ | string[], opts: IClientSubscribeOptions, callback?: ClientSubscribeCallback): this
168
+ public subscribe (topic:
169
+ string
170
+ | string[]
171
+ | ISubscriptionMap, callback?: ClientSubscribeCallback): this
172
+
173
+ /**
174
+ * unsubscribe - unsubscribe from topic(s)
175
+ *
176
+ * @param {String, Array} topic - topics to unsubscribe from
177
+ * @param {Object} opts - opts of unsubscribe
178
+ * @param {Function} [callback] - callback fired on unsuback
179
+ * @returns {MqttClient} this - for chaining
180
+ * @api public
181
+ * @example client.unsubscribe('topic')
182
+ * @example client.unsubscribe('topic', console.log)
183
+ * @example client.unsubscribe('topic', opts, console.log)
184
+ */
185
+ public unsubscribe (topic: string | string[], opts?: Object, callback?: PacketCallback): this
186
+
187
+ /**
188
+ * end - close connection
189
+ *
190
+ * @returns {MqttClient} this - for chaining
191
+ * @param {Boolean} force - do not wait for all in-flight messages to be acked
192
+ * @param {Object} opts - opts disconnect
193
+ * @param {Function} cb - called when the client has been closed
194
+ *
195
+ * @api public
196
+ */
197
+ public end (force?: boolean, opts?: Object, cb?: CloseCallback): this
198
+
199
+ /**
200
+ * removeOutgoingMessage - remove a message in outgoing store
201
+ * the outgoing callback will be called withe Error('Message removed') if the message is removed
202
+ *
203
+ * @param {Number} mid - messageId to remove message
204
+ * @returns {MqttClient} this - for chaining
205
+ * @api public
206
+ *
207
+ * @example client.removeOutgoingMessage(client.getLastMessageId());
208
+ */
209
+ public removeOutgoingMessage (mid: number): this
210
+
211
+ /**
212
+ * reconnect - connect again using the same options as connect()
213
+ *
214
+ * @param {Object} [opts] - optional reconnect options, includes:
215
+ * {Store} incomingStore - a store for the incoming packets
216
+ * {Store} outgoingStore - a store for the outgoing packets
217
+ * if opts is not given, current stores are used
218
+ *
219
+ * @returns {MqttClient} this - for chaining
220
+ *
221
+ * @api public
222
+ */
223
+ public reconnect (opts?: IClientReconnectOptions): this
224
+
225
+ /**
226
+ * Handle messages with backpressure support, one at a time.
227
+ * Override at will.
228
+ *
229
+ * @param packet packet the packet
230
+ * @param callback callback call when finished
231
+ * @api public
232
+ */
233
+ public handleMessage (packet: Packet, callback: PacketCallback): void
234
+
235
+ /**
236
+ * Handle auth packages for MQTT 5 enhanced authentication methods such
237
+ * as challenge response authentication.
238
+ *
239
+ * Challenge-response authentication flow would look something like this:
240
+ *
241
+ * --> CONNECT | authMethod = "mathChallenge" -->
242
+ * <-- AUTH | authMethod = "mathChallenge", authData = "12 + 34" <--
243
+ * --> AUTH | authMethod = "mathChallenge", authData = "46" -->
244
+ * <-- CONNACK | reasonCode = SUCCESS <--
245
+ *
246
+ * This form of authentication has several advantages over traditional
247
+ * credential-based approaches. For instance authentication without the direct
248
+ * exchange of authentication secrets.
249
+ *
250
+ * @param packet the auth packet to handle
251
+ * @param callback call when finished
252
+ * @api public
253
+ */
254
+ public handleAuth (packet: IAuthPacket, callback: PacketCallback): void
255
+
256
+ /**
257
+ * getLastMessageId
258
+ */
259
+ public getLastMessageId (): number
260
+ }
261
+ export { IClientOptions }
@@ -1,10 +1,10 @@
1
- import { IClientOptions, MqttClient } from '../client'
2
- /**
3
- * connect - connect to an MQTT broker.
4
- *
5
- * @param {String} [brokerUrl] - url of the broker, optional
6
- * @param {Object} opts - see MqttClient#constructor
7
- */
8
- declare function connect (brokerUrl?: string | any, opts?: IClientOptions): MqttClient
9
- export { connect }
10
- export { MqttClient }
1
+ import { IClientOptions, MqttClient } from '../client'
2
+ /**
3
+ * connect - connect to an MQTT broker.
4
+ *
5
+ * @param {String} [brokerUrl] - url of the broker, optional
6
+ * @param {Object} opts - see MqttClient#constructor
7
+ */
8
+ declare function connect (brokerUrl?: string | any, opts?: IClientOptions): MqttClient
9
+ export { connect }
10
+ export { MqttClient }
@@ -0,0 +1,49 @@
1
+ import { IMessageIdProvider } from './message-id-provider'
2
+
3
+ /**
4
+ * DefaultMessageIdProvider
5
+ * This is compatible behavior with the original MQTT.js internal messageId allocation.
6
+ */
7
+ declare class DefaultMessageIdProvider implements IMessageIdProvider {
8
+ /**
9
+ * DefaultMessageIdProvider constructor.
10
+ * Randomize initial messageId
11
+ * @constructor
12
+ */
13
+ constructor ()
14
+
15
+ /**
16
+ * Return the current messageId and increment the current messageId.
17
+ * @return {Number} - messageId
18
+ */
19
+ public allocate (): Number | null
20
+
21
+ /**
22
+ * Get the last allocated messageId.
23
+ * @return {Number} - messageId.
24
+ */
25
+ public getLastAllocated (): Number | null
26
+
27
+ /**
28
+ * Register the messageId.
29
+ * This function actually nothing and always return true.
30
+ * @param {Number} num - The messageId to request use.
31
+ * @return {Boolean} - If `num` was not occupied, then return true, otherwise return false.
32
+ */
33
+ public register (num: Number): Boolean
34
+
35
+ /**
36
+ * Deallocate the messageId.
37
+ * This function actually nothing.
38
+ * @param {Number} num - The messageId to deallocate.
39
+ */
40
+ public deallocate (num: Number): void
41
+
42
+ /**
43
+ * Clear all occupied messageIds.
44
+ * This function actually nothing.
45
+ */
46
+ public clear (): void
47
+ }
48
+
49
+ export { DefaultMessageIdProvider }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * MessageIdProvider
3
+ */
4
+ declare interface IMessageIdProvider {
5
+ /**
6
+ * Allocate the first vacant messageId. The messageId become occupied status.
7
+ * @return {Number} - The first vacant messageId. If all messageIds are occupied, return null.
8
+ */
9
+ allocate (): Number | null
10
+
11
+ /**
12
+ * Get the last allocated messageId.
13
+ * @return {Number} - messageId.
14
+ */
15
+ getLastAllocated (): Number | null
16
+
17
+ /**
18
+ * Register the messageId. The messageId become occupied status.
19
+ * If the messageId has already been occupied, then return false.
20
+ * @param {Number} num - The messageId to request use.
21
+ * @return {Boolean} - If `num` was not occupied, then return true, otherwise return false.
22
+ */
23
+ register (num: Number): Boolean
24
+
25
+ /**
26
+ * Deallocate the messageId. The messageId become vacant status.
27
+ * @param {Number} num - The messageId to deallocate. The messageId must be occupied status.
28
+ * In other words, the messageId must be allocated by allocate() or
29
+ * occupied by register().
30
+ */
31
+ deallocate (num: Number): void
32
+
33
+ /**
34
+ * Clear all occupied messageIds.
35
+ * The all messageIds are set to vacant status.
36
+ */
37
+ clear (): void
38
+ }
39
+
40
+ export { IMessageIdProvider }
@@ -1,6 +1,6 @@
1
- export interface IStoreOptions {
2
- /**
3
- * true, clear _inflights at close
4
- */
5
- clean?: boolean
6
- }
1
+ export interface IStoreOptions {
2
+ /**
3
+ * true, clear _inflights at close
4
+ */
5
+ clean?: boolean
6
+ }