mqtt 2.18.9 → 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.
Files changed (67) hide show
  1. package/CONTRIBUTING.md +27 -27
  2. package/LICENSE.md +15 -15
  3. package/README.md +689 -612
  4. package/bin/pub.js +146 -146
  5. package/bin/sub.js +123 -123
  6. package/dist/mqtt.js +11916 -14116
  7. package/dist/mqtt.min.js +1 -1
  8. package/doc/help.txt +8 -8
  9. package/doc/publish.txt +26 -26
  10. package/doc/subscribe.txt +26 -26
  11. package/examples/client/secure-client.js +24 -24
  12. package/examples/client/simple-both.js +13 -13
  13. package/examples/client/simple-publish.js +7 -7
  14. package/examples/client/simple-subscribe.js +9 -9
  15. package/examples/tls client/crt.ca.cg.pem +34 -34
  16. package/examples/tls client/mqttclient.js +48 -48
  17. package/examples/tls client/tls-cert.pem +13 -13
  18. package/examples/tls client/tls-key.pem +15 -15
  19. package/examples/wss/client.js +49 -49
  20. package/examples/wss/client_with_proxy.js +58 -58
  21. package/lib/client.js +1460 -1129
  22. package/lib/connect/ali.js +130 -0
  23. package/lib/connect/index.js +157 -149
  24. package/lib/connect/tcp.js +19 -19
  25. package/lib/connect/tls.js +41 -41
  26. package/lib/connect/ws.js +92 -92
  27. package/lib/connect/wx.js +134 -110
  28. package/lib/store.js +140 -140
  29. package/lib/validations.js +52 -52
  30. package/mqtt.js +41 -41
  31. package/package.json +112 -107
  32. package/test/abstract_client.js +3084 -2667
  33. package/test/abstract_store.js +135 -135
  34. package/test/browser/server.js +132 -132
  35. package/test/browser/test.js +92 -92
  36. package/test/client.js +1129 -544
  37. package/test/helpers/private-csr.pem +16 -16
  38. package/test/helpers/private-key.pem +27 -27
  39. package/test/helpers/public-cert.pem +19 -19
  40. package/test/helpers/public-key.pem +9 -9
  41. package/test/helpers/server.js +52 -52
  42. package/test/helpers/server_process.js +9 -9
  43. package/test/helpers/tls-cert.pem +14 -14
  44. package/test/helpers/tls-csr.pem +11 -11
  45. package/test/helpers/tls-key.pem +15 -15
  46. package/test/helpers/wrong-cert.pem +13 -13
  47. package/test/helpers/wrong-csr.pem +11 -11
  48. package/test/helpers/wrong-key.pem +15 -15
  49. package/test/mocha.opts +4 -4
  50. package/test/mqtt.js +230 -230
  51. package/test/mqtt_store.js +9 -9
  52. package/test/secure_client.js +157 -157
  53. package/test/server.js +93 -65
  54. package/test/store.js +10 -10
  55. package/test/typescript/broker-connect-subscribe-and-publish.js +24 -0
  56. package/test/typescript/broker-connect-subscribe-and-publish.js.map +1 -0
  57. package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
  58. package/test/typescript/tsconfig.json +14 -14
  59. package/test/util.js +13 -13
  60. package/test/websocket_client.js +144 -144
  61. package/types/index.d.ts +27 -27
  62. package/types/lib/client-options.d.ts +178 -137
  63. package/types/lib/client.d.ts +231 -196
  64. package/types/lib/connect/index.d.ts +10 -10
  65. package/types/lib/store-options.d.ts +6 -6
  66. package/types/lib/store.d.ts +46 -46
  67. package/test/browser/wx.js +0 -93
@@ -1,144 +1,144 @@
1
- 'use strict'
2
-
3
- var http = require('http')
4
- var websocket = require('websocket-stream')
5
- var WebSocketServer = require('ws').Server
6
- var Connection = require('mqtt-connection')
7
- var abstractClientTests = require('./abstract_client')
8
- var mqtt = require('../')
9
- var xtend = require('xtend')
10
- var assert = require('assert')
11
- var port = 9999
12
- var server = http.createServer()
13
-
14
- function attachWebsocketServer (wsServer) {
15
- var wss = new WebSocketServer({server: wsServer, perMessageDeflate: false})
16
-
17
- wss.on('connection', function (ws) {
18
- var stream = websocket(ws)
19
- var connection = new Connection(stream)
20
-
21
- wsServer.emit('client', connection)
22
- stream.on('error', function () {})
23
- connection.on('error', function () {})
24
- })
25
-
26
- return wsServer
27
- }
28
-
29
- attachWebsocketServer(server)
30
-
31
- server.on('client', function (client) {
32
- client.on('connect', function (packet) {
33
- if (packet.clientId === 'invalid') {
34
- client.connack({ returnCode: 2 })
35
- } else {
36
- server.emit('connect', client)
37
- client.connack({returnCode: 0})
38
- }
39
- })
40
-
41
- client.on('publish', function (packet) {
42
- setImmediate(function () {
43
- switch (packet.qos) {
44
- case 0:
45
- break
46
- case 1:
47
- client.puback(packet)
48
- break
49
- case 2:
50
- client.pubrec(packet)
51
- break
52
- }
53
- })
54
- })
55
-
56
- client.on('pubrel', function (packet) {
57
- client.pubcomp(packet)
58
- })
59
-
60
- client.on('pubrec', function (packet) {
61
- client.pubrel(packet)
62
- })
63
-
64
- client.on('pubcomp', function () {
65
- // Nothing to be done
66
- })
67
-
68
- client.on('subscribe', function (packet) {
69
- client.suback({
70
- messageId: packet.messageId,
71
- granted: packet.subscriptions.map(function (e) {
72
- return e.qos
73
- })
74
- })
75
- })
76
-
77
- client.on('unsubscribe', function (packet) {
78
- client.unsuback(packet)
79
- })
80
-
81
- client.on('pingreq', function () {
82
- client.pingresp()
83
- })
84
- }).listen(port)
85
-
86
- describe('Websocket Client', function () {
87
- var baseConfig = { protocol: 'ws', port: port }
88
-
89
- function makeOptions (custom) {
90
- // xtend returns a new object. Does not mutate arguments
91
- return xtend(baseConfig, custom || {})
92
- }
93
-
94
- it('should use mqtt as the protocol by default', function (done) {
95
- server.once('client', function (client) {
96
- client.stream.socket.protocol.should.equal('mqtt')
97
- })
98
- mqtt.connect(makeOptions()).on('connect', function () {
99
- this.end(true, done)
100
- })
101
- })
102
-
103
- it('should be able transform the url (for e.g. to sign it)', function (done) {
104
- var baseUrl = 'ws://localhost:9999/mqtt'
105
- var sig = '?AUTH=token'
106
- var expected = baseUrl + sig
107
- var actual
108
- var opts = makeOptions({
109
- path: '/mqtt',
110
- transformWsUrl: function (url, opt, client) {
111
- assert.equal(url, baseUrl)
112
- assert.strictEqual(opt, opts)
113
- assert.strictEqual(client.options, opts)
114
- assert.strictEqual(typeof opt.transformWsUrl, 'function')
115
- assert(client instanceof mqtt.MqttClient)
116
- url += sig
117
- actual = url
118
- return url
119
- }})
120
- mqtt.connect(opts)
121
- .on('connect', function () {
122
- assert.equal(this.stream.socket.url, expected)
123
- assert.equal(actual, expected)
124
- this.end(true, done)
125
- })
126
- })
127
-
128
- it('should use mqttv3.1 as the protocol if using v3.1', function (done) {
129
- server.once('client', function (client) {
130
- client.stream.socket.protocol.should.equal('mqttv3.1')
131
- })
132
-
133
- var opts = makeOptions({
134
- protocolId: 'MQIsdp',
135
- protocolVersion: 3
136
- })
137
-
138
- mqtt.connect(opts).on('connect', function () {
139
- this.end(true, done)
140
- })
141
- })
142
-
143
- abstractClientTests(server, makeOptions())
144
- })
1
+ 'use strict'
2
+
3
+ var http = require('http')
4
+ var websocket = require('websocket-stream')
5
+ var WebSocketServer = require('ws').Server
6
+ var Connection = require('mqtt-connection')
7
+ var abstractClientTests = require('./abstract_client')
8
+ var mqtt = require('../')
9
+ var xtend = require('xtend')
10
+ var assert = require('assert')
11
+ var port = 9999
12
+ var server = http.createServer()
13
+
14
+ function attachWebsocketServer (wsServer) {
15
+ var wss = new WebSocketServer({server: wsServer, perMessageDeflate: false})
16
+
17
+ wss.on('connection', function (ws) {
18
+ var stream = websocket(ws)
19
+ var connection = new Connection(stream)
20
+
21
+ wsServer.emit('client', connection)
22
+ stream.on('error', function () {})
23
+ connection.on('error', function () {})
24
+ })
25
+
26
+ return wsServer
27
+ }
28
+
29
+ attachWebsocketServer(server)
30
+
31
+ server.on('client', function (client) {
32
+ client.on('connect', function (packet) {
33
+ if (packet.clientId === 'invalid') {
34
+ client.connack({ returnCode: 2 })
35
+ } else {
36
+ server.emit('connect', client)
37
+ client.connack({returnCode: 0})
38
+ }
39
+ })
40
+
41
+ client.on('publish', function (packet) {
42
+ setImmediate(function () {
43
+ switch (packet.qos) {
44
+ case 0:
45
+ break
46
+ case 1:
47
+ client.puback(packet)
48
+ break
49
+ case 2:
50
+ client.pubrec(packet)
51
+ break
52
+ }
53
+ })
54
+ })
55
+
56
+ client.on('pubrel', function (packet) {
57
+ client.pubcomp(packet)
58
+ })
59
+
60
+ client.on('pubrec', function (packet) {
61
+ client.pubrel(packet)
62
+ })
63
+
64
+ client.on('pubcomp', function () {
65
+ // Nothing to be done
66
+ })
67
+
68
+ client.on('subscribe', function (packet) {
69
+ client.suback({
70
+ messageId: packet.messageId,
71
+ granted: packet.subscriptions.map(function (e) {
72
+ return e.qos
73
+ })
74
+ })
75
+ })
76
+
77
+ client.on('unsubscribe', function (packet) {
78
+ client.unsuback(packet)
79
+ })
80
+
81
+ client.on('pingreq', function () {
82
+ client.pingresp()
83
+ })
84
+ }).listen(port)
85
+
86
+ describe('Websocket Client', function () {
87
+ var baseConfig = { protocol: 'ws', port: port }
88
+
89
+ function makeOptions (custom) {
90
+ // xtend returns a new object. Does not mutate arguments
91
+ return xtend(baseConfig, custom || {})
92
+ }
93
+
94
+ it('should use mqtt as the protocol by default', function (done) {
95
+ server.once('client', function (client) {
96
+ client.stream.socket.protocol.should.equal('mqtt')
97
+ })
98
+ mqtt.connect(makeOptions()).on('connect', function () {
99
+ this.end(true, done)
100
+ })
101
+ })
102
+
103
+ it('should be able transform the url (for e.g. to sign it)', function (done) {
104
+ var baseUrl = 'ws://localhost:9999/mqtt'
105
+ var sig = '?AUTH=token'
106
+ var expected = baseUrl + sig
107
+ var actual
108
+ var opts = makeOptions({
109
+ path: '/mqtt',
110
+ transformWsUrl: function (url, opt, client) {
111
+ assert.equal(url, baseUrl)
112
+ assert.strictEqual(opt, opts)
113
+ assert.strictEqual(client.options, opts)
114
+ assert.strictEqual(typeof opt.transformWsUrl, 'function')
115
+ assert(client instanceof mqtt.MqttClient)
116
+ url += sig
117
+ actual = url
118
+ return url
119
+ }})
120
+ mqtt.connect(opts)
121
+ .on('connect', function () {
122
+ assert.equal(this.stream.socket.url, expected)
123
+ assert.equal(actual, expected)
124
+ this.end(true, done)
125
+ })
126
+ })
127
+
128
+ it('should use mqttv3.1 as the protocol if using v3.1', function (done) {
129
+ server.once('client', function (client) {
130
+ client.stream.socket.protocol.should.equal('mqttv3.1')
131
+ })
132
+
133
+ var opts = makeOptions({
134
+ protocolId: 'MQIsdp',
135
+ protocolVersion: 3
136
+ })
137
+
138
+ mqtt.connect(opts).on('connect', function () {
139
+ this.end(true, done)
140
+ })
141
+ })
142
+
143
+ abstractClientTests(server, makeOptions())
144
+ })
package/types/index.d.ts CHANGED
@@ -1,27 +1,27 @@
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
- } 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
+ } from 'mqtt-packet'
@@ -1,137 +1,178 @@
1
- import { MqttClient } from './client'
2
- import { Store } from './store'
3
- import { QoS } from 'mqtt-packet'
4
-
5
- export interface IClientOptions extends ISecureClientOptions {
6
- port?: number // port is made into a number subsequently
7
- host?: string // host does NOT include port
8
- hostname?: string
9
- path?: string
10
- protocol?: 'wss' | 'ws' | 'mqtt' | 'mqtts' | 'tcp' | 'ssl' | 'wx' | 'wxs'
11
-
12
- wsOptions?: {
13
- [x: string]: any
14
- }
15
- /**
16
- * 10 seconds, set to 0 to disable
17
- */
18
- keepalive?: number
19
- /**
20
- * 'mqttjs_' + Math.random().toString(16).substr(2, 8)
21
- */
22
- clientId?: string
23
- /**
24
- * 'MQTT'
25
- */
26
- protocolId?: string
27
- /**
28
- * 4
29
- */
30
- protocolVersion?: number
31
- /**
32
- * true, set to false to receive QoS 1 and 2 messages while offline
33
- */
34
- clean?: boolean
35
- /**
36
- * 1000 milliseconds, interval between two reconnections
37
- */
38
- reconnectPeriod?: number
39
- /**
40
- * 30 * 1000 milliseconds, time to wait before a CONNACK is received
41
- */
42
- connectTimeout?: number
43
- /**
44
- * the username required by your broker, if any
45
- */
46
- username?: string
47
- /**
48
- * the password required by your broker, if any
49
- */
50
- password?: string
51
- /**
52
- * a Store for the incoming packets
53
- */
54
- incomingStore?: Store
55
- /**
56
- * a Store for the outgoing packets
57
- */
58
- outgoingStore?: Store
59
- queueQoSZero?: boolean
60
- reschedulePings?: boolean
61
- servers?: Array<{
62
- host: string
63
- port: number
64
- protocol?: 'wss' | 'ws' | 'mqtt' | 'mqtts' | 'tcp' | 'ssl' | 'wx' | 'wxs'
65
- }>
66
- /**
67
- * true, set to false to disable re-subscribe functionality
68
- */
69
- resubscribe?: boolean
70
- /**
71
- * a message that will sent by the broker automatically when the client disconnect badly.
72
- */
73
- will?: {
74
- /**
75
- * the topic to publish
76
- */
77
- topic: string
78
- /**
79
- * the message to publish
80
- */
81
- payload: string
82
- /**
83
- * the QoS
84
- */
85
- qos: QoS
86
- /**
87
- * the retain flag
88
- */
89
- retain: boolean
90
- }
91
- transformWsUrl?: (url: string, options: IClientOptions, client: MqttClient) => string
92
- }
93
- export interface ISecureClientOptions {
94
- /**
95
- * optional private keys in PEM format
96
- */
97
- key?: string | string[] | Buffer | Buffer[] | Object[]
98
- /**
99
- * optional cert chains in PEM format
100
- */
101
- cert?: string | string[] | Buffer | Buffer[]
102
- /**
103
- * Optionally override the trusted CA certificates in PEM format
104
- */
105
- ca?: string | string[] | Buffer | Buffer[]
106
- rejectUnauthorized?: boolean
107
- }
108
- export interface IClientPublishOptions {
109
- /**
110
- * the QoS
111
- */
112
- qos: QoS
113
- /**
114
- * the retain flag
115
- */
116
- retain?: boolean
117
- /**
118
- * whether or not mark a message as duplicate
119
- */
120
- dup?: boolean
121
- }
122
- export interface IClientSubscribeOptions {
123
- /**
124
- * the QoS
125
- */
126
- qos: QoS
127
- }
128
- export interface IClientReconnectOptions {
129
- /**
130
- * a Store for the incoming packets
131
- */
132
- incomingStore?: Store
133
- /**
134
- * a Store for the outgoing packets
135
- */
136
- outgoingStore?: Store
137
- }
1
+ import { MqttClient } from './client'
2
+ import { Store } from './store'
3
+ import { QoS } from 'mqtt-packet'
4
+
5
+ export declare type StorePutCallback = () => void
6
+
7
+ export interface IClientOptions extends ISecureClientOptions {
8
+ port?: number // port is made into a number subsequently
9
+ host?: string // host does NOT include port
10
+ hostname?: string
11
+ path?: string
12
+ protocol?: 'wss' | 'ws' | 'mqtt' | 'mqtts' | 'tcp' | 'ssl' | 'wx' | 'wxs'
13
+
14
+ wsOptions?: {
15
+ [x: string]: any
16
+ }
17
+ /**
18
+ * 10 seconds, set to 0 to disable
19
+ */
20
+ keepalive?: number
21
+ /**
22
+ * 'mqttjs_' + Math.random().toString(16).substr(2, 8)
23
+ */
24
+ clientId?: string
25
+ /**
26
+ * 'MQTT'
27
+ */
28
+ protocolId?: string
29
+ /**
30
+ * 4
31
+ */
32
+ protocolVersion?: number
33
+ /**
34
+ * true, set to false to receive QoS 1 and 2 messages while offline
35
+ */
36
+ clean?: boolean
37
+ /**
38
+ * 1000 milliseconds, interval between two reconnections
39
+ */
40
+ reconnectPeriod?: number
41
+ /**
42
+ * 30 * 1000 milliseconds, time to wait before a CONNACK is received
43
+ */
44
+ connectTimeout?: number
45
+ /**
46
+ * the username required by your broker, if any
47
+ */
48
+ username?: string
49
+ /**
50
+ * the password required by your broker, if any
51
+ */
52
+ password?: string
53
+ /**
54
+ * a Store for the incoming packets
55
+ */
56
+ incomingStore?: Store
57
+ /**
58
+ * a Store for the outgoing packets
59
+ */
60
+ outgoingStore?: Store
61
+ queueQoSZero?: boolean
62
+ reschedulePings?: boolean
63
+ servers?: Array<{
64
+ host: string
65
+ port: number
66
+ protocol?: 'wss' | 'ws' | 'mqtt' | 'mqtts' | 'tcp' | 'ssl' | 'wx' | 'wxs'
67
+ }>
68
+ /**
69
+ * true, set to false to disable re-subscribe functionality
70
+ */
71
+ resubscribe?: boolean
72
+ /**
73
+ * a message that will sent by the broker automatically when the client disconnect badly.
74
+ */
75
+ will?: {
76
+ /**
77
+ * the topic to publish
78
+ */
79
+ topic: string
80
+ /**
81
+ * the message to publish
82
+ */
83
+ payload: string
84
+ /**
85
+ * the QoS
86
+ */
87
+ qos: QoS
88
+ /**
89
+ * the retain flag
90
+ */
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
116
+ }
117
+ }
118
+ export interface ISecureClientOptions {
119
+ /**
120
+ * optional private keys in PEM format
121
+ */
122
+ key?: string | string[] | Buffer | Buffer[] | Object[]
123
+ /**
124
+ * optional cert chains in PEM format
125
+ */
126
+ cert?: string | string[] | Buffer | Buffer[]
127
+ /**
128
+ * Optionally override the trusted CA certificates in PEM format
129
+ */
130
+ ca?: string | string[] | Buffer | Buffer[]
131
+ rejectUnauthorized?: boolean
132
+ }
133
+ export interface IClientPublishOptions {
134
+ /**
135
+ * the QoS
136
+ */
137
+ qos: QoS
138
+ /**
139
+ * the retain flag
140
+ */
141
+ retain?: boolean
142
+ /**
143
+ * whether or not mark a message as duplicate
144
+ */
145
+ dup?: boolean
146
+ /**
147
+ * callback called when message is put into `outgoingStore`
148
+ */
149
+ cbStorePut?: StorePutCallback
150
+ }
151
+ export interface IClientSubscribeOptions {
152
+ /**
153
+ * the QoS
154
+ */
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
168
+ }
169
+ export interface IClientReconnectOptions {
170
+ /**
171
+ * a Store for the incoming packets
172
+ */
173
+ incomingStore?: Store
174
+ /**
175
+ * a Store for the outgoing packets
176
+ */
177
+ outgoingStore?: Store
178
+ }