mqtt 2.18.5 → 2.18.9

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 (66) hide show
  1. package/CONTRIBUTING.md +27 -27
  2. package/LICENSE.md +15 -15
  3. package/README.md +612 -612
  4. package/bin/pub.js +146 -146
  5. package/bin/sub.js +123 -123
  6. package/dist/mqtt.js +14275 -10729
  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 +1129 -1102
  22. package/lib/connect/index.js +149 -149
  23. package/lib/connect/tcp.js +19 -19
  24. package/lib/connect/tls.js +41 -41
  25. package/lib/connect/ws.js +92 -92
  26. package/lib/connect/wx.js +110 -110
  27. package/lib/store.js +140 -140
  28. package/lib/validations.js +52 -52
  29. package/mqtt.js +41 -41
  30. package/package.json +107 -111
  31. package/test/abstract_client.js +2667 -2533
  32. package/test/abstract_store.js +135 -133
  33. package/test/browser/server.js +132 -132
  34. package/test/browser/test.js +92 -92
  35. package/test/browser/wx.js +93 -93
  36. package/test/client.js +544 -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 +65 -65
  54. package/test/store.js +10 -10
  55. package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
  56. package/test/typescript/tsconfig.json +14 -14
  57. package/test/util.js +13 -13
  58. package/test/websocket_client.js +144 -144
  59. package/types/index.d.ts +27 -27
  60. package/types/lib/client-options.d.ts +137 -137
  61. package/types/lib/client.d.ts +196 -196
  62. package/types/lib/connect/index.d.ts +10 -10
  63. package/types/lib/store-options.d.ts +6 -6
  64. package/types/lib/store.d.ts +46 -46
  65. package/test/typescript/broker-connect-subscribe-and-publish.js +0 -24
  66. package/test/typescript/broker-connect-subscribe-and-publish.js.map +0 -1
@@ -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,137 @@
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 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
+ }