mqtt 2.18.6 → 3.0.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.
package/test/server.js CHANGED
@@ -5,11 +5,14 @@ var tls = require('tls')
5
5
  var inherits = require('inherits')
6
6
  var Connection = require('mqtt-connection')
7
7
  var MqttServer
8
+ var FastMqttServer
8
9
  var MqttSecureServer
9
10
 
10
11
  function setupConnection (duplex) {
11
- var connection = new Connection(duplex)
12
- this.emit('client', connection)
12
+ var that = this
13
+ var connection = new Connection(duplex, function () {
14
+ that.emit('client', connection)
15
+ })
13
16
  }
14
17
 
15
18
  /*
@@ -34,6 +37,31 @@ MqttServer = module.exports = function Server (listener) {
34
37
  }
35
38
  inherits(MqttServer, net.Server)
36
39
 
40
+ /*
41
+ * FastMqttServer(w/o waiting for initialization)
42
+ *
43
+ * @param {Function} listener - fired on client connection
44
+ */
45
+ FastMqttServer = module.exports.FastMqttServer = function Server (listener) {
46
+ if (!(this instanceof Server)) {
47
+ return new Server(listener)
48
+ }
49
+
50
+ net.Server.call(this)
51
+
52
+ this.on('connection', function (duplex) {
53
+ var connection = new Connection(duplex)
54
+ this.emit('client', connection)
55
+ })
56
+
57
+ if (listener) {
58
+ this.on('client', listener)
59
+ }
60
+
61
+ return this
62
+ }
63
+ inherits(FastMqttServer, net.Server)
64
+
37
65
  /**
38
66
  * MqttSecureServer
39
67
  *
@@ -9,7 +9,7 @@ const opts: IClientOptions = {}
9
9
  console.log(`connect(${JSON.stringify(BROKER)})`)
10
10
  const client:Client = connect(`mqtt://${BROKER}`, opts)
11
11
 
12
- client.subscribe({[TOPIC]: 2}, (err, granted) => {
12
+ client.subscribe({[TOPIC]: {qos: 2}}, (err, granted) => {
13
13
  granted.forEach(({topic, qos}) => {
14
14
  console.log(`subscribed to ${topic} with qos=${qos}`)
15
15
  })
@@ -2,6 +2,8 @@ import { MqttClient } from './client'
2
2
  import { Store } from './store'
3
3
  import { QoS } from 'mqtt-packet'
4
4
 
5
+ export declare type StorePutCallback = () => void
6
+
5
7
  export interface IClientOptions extends ISecureClientOptions {
6
8
  port?: number // port is made into a number subsequently
7
9
  host?: string // host does NOT include port
@@ -86,9 +88,32 @@ export interface IClientOptions extends ISecureClientOptions {
86
88
  /**
87
89
  * the retain flag
88
90
  */
89
- retain: boolean
91
+ retain: boolean,
92
+ /*
93
+ * properies object of will
94
+ * */
95
+ properties?: {
96
+ willDelayInterval?: number,
97
+ payloadFormatIndicator?: number,
98
+ messageExpiryInterval?: number,
99
+ contentType?: string,
100
+ responseTopic?: string,
101
+ correlationData?: Buffer,
102
+ userProperties?: Object
103
+ }
104
+ }
105
+ transformWsUrl?: (url: string, options: IClientOptions, client: MqttClient) => string,
106
+ properties?: {
107
+ sessionExpiryInterval?: number,
108
+ receiveMaximum?: number,
109
+ maximumPacketSize?: number,
110
+ topicAliasMaximum?: number,
111
+ requestResponseInformation?: boolean,
112
+ requestProblemInformation?: boolean,
113
+ userProperties?: Object,
114
+ authenticationMethod?: string,
115
+ authenticationData?: Buffer
90
116
  }
91
- transformWsUrl?: (url: string, options: IClientOptions, client: MqttClient) => string
92
117
  }
93
118
  export interface ISecureClientOptions {
94
119
  /**
@@ -118,12 +143,28 @@ export interface IClientPublishOptions {
118
143
  * whether or not mark a message as duplicate
119
144
  */
120
145
  dup?: boolean
146
+ /**
147
+ * callback called when message is put into `outgoingStore`
148
+ */
149
+ cbStorePut?: StorePutCallback
121
150
  }
122
151
  export interface IClientSubscribeOptions {
123
152
  /**
124
153
  * the QoS
125
154
  */
126
- qos: QoS
155
+ qos: QoS,
156
+ /*
157
+ * no local flag
158
+ * */
159
+ nl?: boolean,
160
+ /*
161
+ * Retain As Published flag
162
+ * */
163
+ rap?: boolean,
164
+ /*
165
+ * Retain Handling option
166
+ * */
167
+ rh?: number
127
168
  }
128
169
  export interface IClientReconnectOptions {
129
170
  /**
@@ -19,6 +19,18 @@ export interface ISubscriptionGrant {
19
19
  * is the granted qos level on it, may return 128 on error
20
20
  */
21
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
22
34
  }
23
35
  export interface ISubscriptionRequest {
24
36
  /**
@@ -29,12 +41,29 @@ export interface ISubscriptionRequest {
29
41
  * is the granted qos level on it
30
42
  */
31
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
32
56
  }
33
57
  export interface ISubscriptionMap {
34
58
  /**
35
- * object which has topic names as object keys and as value the QoS, like {'test1': 0, 'test2': 1}.
59
+ * object which has topic names as object keys and as value the options, like {'test1': {qos: 0}, 'test2': {qos: 2}}.
36
60
  */
37
- [topic: string]: QoS
61
+ [topic: string]: {
62
+ qos: QoS,
63
+ nl?: boolean,
64
+ rap?: boolean,
65
+ rh?: number
66
+ }
38
67
  }
39
68
 
40
69
  export declare type ClientSubscribeCallback = (err: Error, granted: ISubscriptionGrant[]) => void
@@ -89,9 +118,12 @@ export declare class MqttClient extends events.EventEmitter {
89
118
  * @param {Object} [opts] - publish options, includes:
90
119
  * @param {Number} [opts.qos] - qos level to publish on
91
120
  * @param {Boolean} [opts.retain] - whether or not to retain the message
121
+ * @param {Function}[opts.cbStorePut] - function(){}
122
+ * called when message is put into `outgoingStore`
92
123
  *
93
124
  * @param {Function} [callback] - function(err){}
94
125
  * called when publish succeeds or fails
126
+ *
95
127
  * @returns {Client} this - for chaining
96
128
  * @api public
97
129
  *
@@ -133,24 +165,27 @@ export declare class MqttClient extends events.EventEmitter {
133
165
  * unsubscribe - unsubscribe from topic(s)
134
166
  *
135
167
  * @param {String, Array} topic - topics to unsubscribe from
168
+ * @param {Object} opts - opts of unsubscribe
136
169
  * @param {Function} [callback] - callback fired on unsuback
137
170
  * @returns {MqttClient} this - for chaining
138
171
  * @api public
139
172
  * @example client.unsubscribe('topic')
140
173
  * @example client.unsubscribe('topic', console.log)
174
+ * @example client.unsubscribe('topic', opts, console.log)
141
175
  */
142
- public unsubscribe (topic: string | string[], callback?: PacketCallback): this
176
+ public unsubscribe (topic: string | string[], opts?: Object, callback?: PacketCallback): this
143
177
 
144
178
  /**
145
179
  * end - close connection
146
180
  *
147
181
  * @returns {MqttClient} this - for chaining
148
182
  * @param {Boolean} force - do not wait for all in-flight messages to be acked
183
+ * @param {Object} opts - opts disconnect
149
184
  * @param {Function} cb - called when the client has been closed
150
185
  *
151
186
  * @api public
152
187
  */
153
- public end (force?: boolean, cb?: CloseCallback): this
188
+ public end (force?: boolean, opts?: Object, cb?: CloseCallback): this
154
189
 
155
190
  /**
156
191
  * removeOutgoingMessage - remove a message in outgoing store
@@ -1,93 +0,0 @@
1
- 'use strict'
2
-
3
- var mqtt = require('../../lib/connect')
4
- var _URL = require('url')
5
- var xtend = require('xtend')
6
- var parsed = _URL.parse(document.URL)
7
- var isHttps = parsed.protocol === 'https:'
8
- var port = parsed.port || (isHttps ? 443 : 80)
9
- var host = parsed.hostname
10
- var protocol = isHttps ? 'wxs' : 'wx'
11
- require('../helpers/wx')
12
-
13
- function clientTests (buildClient) {
14
- var client
15
-
16
- beforeEach(function () {
17
- client = buildClient()
18
- client.on('offline', function () {
19
- console.log('client offline')
20
- })
21
- client.on('connect', function () {
22
- console.log('client connect')
23
- })
24
- client.on('reconnect', function () {
25
- console.log('client reconnect')
26
- })
27
- })
28
-
29
- afterEach(function (done) {
30
- client.once('close', function () {
31
- done()
32
- })
33
- client.end()
34
- })
35
-
36
- it('should connect', function (done) {
37
- client.on('connect', function () {
38
- done()
39
- })
40
- })
41
-
42
- it('should publish and subscribe', function (done) {
43
- client.subscribe('hello', function () {
44
- done()
45
- }).publish('hello', 'world')
46
- })
47
- }
48
-
49
- function suiteFactory (configName, opts) {
50
- function setVersion (base) {
51
- return xtend(base || {}, opts)
52
- }
53
-
54
- var suiteName = 'MqttClient(' + configName + '=' + JSON.stringify(opts) + ')'
55
- describe(suiteName, function () {
56
- this.timeout(10000)
57
-
58
- describe('specifying nothing', function () {
59
- clientTests(function () {
60
- return mqtt.connect(setVersion())
61
- })
62
- })
63
-
64
- if (parsed.hostname === 'localhost') {
65
- describe('specifying a port', function () {
66
- clientTests(function () {
67
- return mqtt.connect(setVersion({ protocol: protocol, port: port }))
68
- })
69
- })
70
- }
71
-
72
- describe('specifying a port and host', function () {
73
- clientTests(function () {
74
- return mqtt.connect(setVersion({ protocol: protocol, port: port, host: host }))
75
- })
76
- })
77
-
78
- describe('specifying a URL', function () {
79
- clientTests(function () {
80
- return mqtt.connect(protocol + '://' + host + ':' + port, setVersion())
81
- })
82
- })
83
-
84
- describe('specifying a URL with a path', function () {
85
- clientTests(function () {
86
- return mqtt.connect(protocol + '://' + host + ':' + port + '/mqtt', setVersion())
87
- })
88
- })
89
- })
90
- }
91
-
92
- suiteFactory('v3', {protocolId: 'MQIsdp', protocolVersion: 3})
93
- suiteFactory('default', {})