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,157 +1,157 @@
1
- 'use strict'
2
-
3
- var mqtt = require('..')
4
- var path = require('path')
5
- var abstractClientTests = require('./abstract_client')
6
- var fs = require('fs')
7
- var port = 9899
8
- var KEY = path.join(__dirname, 'helpers', 'tls-key.pem')
9
- var CERT = path.join(__dirname, 'helpers', 'tls-cert.pem')
10
- var WRONG_CERT = path.join(__dirname, 'helpers', 'wrong-cert.pem')
11
- var Server = require('./server')
12
-
13
- var server = new Server.SecureServer({
14
- key: fs.readFileSync(KEY),
15
- cert: fs.readFileSync(CERT)
16
- }, function (client) {
17
- client.on('connect', function (packet) {
18
- if (packet.clientId === 'invalid') {
19
- client.connack({returnCode: 2})
20
- } else {
21
- server.emit('connect', client)
22
- client.connack({returnCode: 0})
23
- }
24
- })
25
-
26
- client.on('publish', function (packet) {
27
- setImmediate(function () {
28
- /* jshint -W027 */
29
- /* eslint default-case:0 */
30
- switch (packet.qos) {
31
- case 0:
32
- break
33
- case 1:
34
- client.puback(packet)
35
- break
36
- case 2:
37
- client.pubrec(packet)
38
- break
39
- }
40
- /* jshint +W027 */
41
- })
42
- })
43
-
44
- client.on('pubrel', function (packet) {
45
- client.pubcomp(packet)
46
- })
47
-
48
- client.on('pubrec', function (packet) {
49
- client.pubrel(packet)
50
- })
51
-
52
- client.on('pubcomp', function () {
53
- // Nothing to be done
54
- })
55
-
56
- client.on('subscribe', function (packet) {
57
- client.suback({
58
- messageId: packet.messageId,
59
- granted: packet.subscriptions.map(function (e) {
60
- return e.qos
61
- })
62
- })
63
- })
64
-
65
- client.on('unsubscribe', function (packet) {
66
- client.unsuback(packet)
67
- })
68
-
69
- client.on('pingreq', function () {
70
- client.pingresp()
71
- })
72
- }).listen(port)
73
-
74
- describe('MqttSecureClient', function () {
75
- var config = { protocol: 'mqtts', port: port, rejectUnauthorized: false }
76
- abstractClientTests(server, config)
77
-
78
- describe('with secure parameters', function () {
79
- it('should validate successfully the CA', function (done) {
80
- var client = mqtt.connect({
81
- protocol: 'mqtts',
82
- port: port,
83
- ca: [fs.readFileSync(CERT)],
84
- rejectUnauthorized: true
85
- })
86
-
87
- client.on('error', function (err) {
88
- done(err)
89
- })
90
-
91
- server.once('connect', function () {
92
- done()
93
- })
94
- })
95
-
96
- it('should validate successfully the CA using URI', function (done) {
97
- var client = mqtt.connect('mqtts://localhost:' + port, {
98
- ca: [fs.readFileSync(CERT)],
99
- rejectUnauthorized: true
100
- })
101
-
102
- client.on('error', function (err) {
103
- done(err)
104
- })
105
-
106
- server.once('connect', function () {
107
- done()
108
- })
109
- })
110
-
111
- it('should validate successfully the CA using URI with path', function (done) {
112
- var client = mqtt.connect('mqtts://localhost:' + port + '/', {
113
- ca: [fs.readFileSync(CERT)],
114
- rejectUnauthorized: true
115
- })
116
-
117
- client.on('error', function (err) {
118
- done(err)
119
- })
120
-
121
- server.once('connect', function () {
122
- done()
123
- })
124
- })
125
-
126
- it('should validate unsuccessfully the CA', function (done) {
127
- var client = mqtt.connect({
128
- protocol: 'mqtts',
129
- port: port,
130
- ca: [fs.readFileSync(WRONG_CERT)],
131
- rejectUnauthorized: true
132
- })
133
-
134
- client.once('error', function () {
135
- done()
136
- client.end()
137
- client.on('error', function () {})
138
- })
139
- })
140
-
141
- it('should emit close on TLS error', function (done) {
142
- var client = mqtt.connect({
143
- protocol: 'mqtts',
144
- port: port,
145
- ca: [fs.readFileSync(WRONG_CERT)],
146
- rejectUnauthorized: true
147
- })
148
-
149
- client.on('error', function () {})
150
-
151
- // TODO node v0.8.x emits multiple close events
152
- client.once('close', function () {
153
- done()
154
- })
155
- })
156
- })
157
- })
1
+ 'use strict'
2
+
3
+ var mqtt = require('..')
4
+ var path = require('path')
5
+ var abstractClientTests = require('./abstract_client')
6
+ var fs = require('fs')
7
+ var port = 9899
8
+ var KEY = path.join(__dirname, 'helpers', 'tls-key.pem')
9
+ var CERT = path.join(__dirname, 'helpers', 'tls-cert.pem')
10
+ var WRONG_CERT = path.join(__dirname, 'helpers', 'wrong-cert.pem')
11
+ var Server = require('./server')
12
+
13
+ var server = new Server.SecureServer({
14
+ key: fs.readFileSync(KEY),
15
+ cert: fs.readFileSync(CERT)
16
+ }, function (client) {
17
+ client.on('connect', function (packet) {
18
+ if (packet.clientId === 'invalid') {
19
+ client.connack({returnCode: 2})
20
+ } else {
21
+ server.emit('connect', client)
22
+ client.connack({returnCode: 0})
23
+ }
24
+ })
25
+
26
+ client.on('publish', function (packet) {
27
+ setImmediate(function () {
28
+ /* jshint -W027 */
29
+ /* eslint default-case:0 */
30
+ switch (packet.qos) {
31
+ case 0:
32
+ break
33
+ case 1:
34
+ client.puback(packet)
35
+ break
36
+ case 2:
37
+ client.pubrec(packet)
38
+ break
39
+ }
40
+ /* jshint +W027 */
41
+ })
42
+ })
43
+
44
+ client.on('pubrel', function (packet) {
45
+ client.pubcomp(packet)
46
+ })
47
+
48
+ client.on('pubrec', function (packet) {
49
+ client.pubrel(packet)
50
+ })
51
+
52
+ client.on('pubcomp', function () {
53
+ // Nothing to be done
54
+ })
55
+
56
+ client.on('subscribe', function (packet) {
57
+ client.suback({
58
+ messageId: packet.messageId,
59
+ granted: packet.subscriptions.map(function (e) {
60
+ return e.qos
61
+ })
62
+ })
63
+ })
64
+
65
+ client.on('unsubscribe', function (packet) {
66
+ client.unsuback(packet)
67
+ })
68
+
69
+ client.on('pingreq', function () {
70
+ client.pingresp()
71
+ })
72
+ }).listen(port)
73
+
74
+ describe('MqttSecureClient', function () {
75
+ var config = { protocol: 'mqtts', port: port, rejectUnauthorized: false }
76
+ abstractClientTests(server, config)
77
+
78
+ describe('with secure parameters', function () {
79
+ it('should validate successfully the CA', function (done) {
80
+ var client = mqtt.connect({
81
+ protocol: 'mqtts',
82
+ port: port,
83
+ ca: [fs.readFileSync(CERT)],
84
+ rejectUnauthorized: true
85
+ })
86
+
87
+ client.on('error', function (err) {
88
+ done(err)
89
+ })
90
+
91
+ server.once('connect', function () {
92
+ done()
93
+ })
94
+ })
95
+
96
+ it('should validate successfully the CA using URI', function (done) {
97
+ var client = mqtt.connect('mqtts://localhost:' + port, {
98
+ ca: [fs.readFileSync(CERT)],
99
+ rejectUnauthorized: true
100
+ })
101
+
102
+ client.on('error', function (err) {
103
+ done(err)
104
+ })
105
+
106
+ server.once('connect', function () {
107
+ done()
108
+ })
109
+ })
110
+
111
+ it('should validate successfully the CA using URI with path', function (done) {
112
+ var client = mqtt.connect('mqtts://localhost:' + port + '/', {
113
+ ca: [fs.readFileSync(CERT)],
114
+ rejectUnauthorized: true
115
+ })
116
+
117
+ client.on('error', function (err) {
118
+ done(err)
119
+ })
120
+
121
+ server.once('connect', function () {
122
+ done()
123
+ })
124
+ })
125
+
126
+ it('should validate unsuccessfully the CA', function (done) {
127
+ var client = mqtt.connect({
128
+ protocol: 'mqtts',
129
+ port: port,
130
+ ca: [fs.readFileSync(WRONG_CERT)],
131
+ rejectUnauthorized: true
132
+ })
133
+
134
+ client.once('error', function () {
135
+ done()
136
+ client.end()
137
+ client.on('error', function () {})
138
+ })
139
+ })
140
+
141
+ it('should emit close on TLS error', function (done) {
142
+ var client = mqtt.connect({
143
+ protocol: 'mqtts',
144
+ port: port,
145
+ ca: [fs.readFileSync(WRONG_CERT)],
146
+ rejectUnauthorized: true
147
+ })
148
+
149
+ client.on('error', function () {})
150
+
151
+ // TODO node v0.8.x emits multiple close events
152
+ client.once('close', function () {
153
+ done()
154
+ })
155
+ })
156
+ })
157
+ })
package/test/server.js CHANGED
@@ -1,65 +1,65 @@
1
- 'use strict'
2
-
3
- var net = require('net')
4
- var tls = require('tls')
5
- var inherits = require('inherits')
6
- var Connection = require('mqtt-connection')
7
- var MqttServer
8
- var MqttSecureServer
9
-
10
- function setupConnection (duplex) {
11
- var connection = new Connection(duplex)
12
- this.emit('client', connection)
13
- }
14
-
15
- /*
16
- * MqttServer
17
- *
18
- * @param {Function} listener - fired on client connection
19
- */
20
- MqttServer = module.exports = function Server (listener) {
21
- if (!(this instanceof Server)) {
22
- return new Server(listener)
23
- }
24
-
25
- net.Server.call(this)
26
-
27
- this.on('connection', setupConnection)
28
-
29
- if (listener) {
30
- this.on('client', listener)
31
- }
32
-
33
- return this
34
- }
35
- inherits(MqttServer, net.Server)
36
-
37
- /**
38
- * MqttSecureServer
39
- *
40
- * @param {Object} opts - server options
41
- * @param {Function} listener
42
- */
43
- MqttSecureServer = module.exports.SecureServer =
44
- function SecureServer (opts, listener) {
45
- if (!(this instanceof SecureServer)) {
46
- return new SecureServer(opts, listener)
47
- }
48
-
49
- // new MqttSecureServer(function(){})
50
- if (typeof opts === 'function') {
51
- listener = opts
52
- opts = {}
53
- }
54
-
55
- tls.Server.call(this, opts)
56
-
57
- if (listener) {
58
- this.on('client', listener)
59
- }
60
-
61
- this.on('secureConnection', setupConnection)
62
-
63
- return this
64
- }
65
- inherits(MqttSecureServer, tls.Server)
1
+ 'use strict'
2
+
3
+ var net = require('net')
4
+ var tls = require('tls')
5
+ var inherits = require('inherits')
6
+ var Connection = require('mqtt-connection')
7
+ var MqttServer
8
+ var MqttSecureServer
9
+
10
+ function setupConnection (duplex) {
11
+ var connection = new Connection(duplex)
12
+ this.emit('client', connection)
13
+ }
14
+
15
+ /*
16
+ * MqttServer
17
+ *
18
+ * @param {Function} listener - fired on client connection
19
+ */
20
+ MqttServer = module.exports = function Server (listener) {
21
+ if (!(this instanceof Server)) {
22
+ return new Server(listener)
23
+ }
24
+
25
+ net.Server.call(this)
26
+
27
+ this.on('connection', setupConnection)
28
+
29
+ if (listener) {
30
+ this.on('client', listener)
31
+ }
32
+
33
+ return this
34
+ }
35
+ inherits(MqttServer, net.Server)
36
+
37
+ /**
38
+ * MqttSecureServer
39
+ *
40
+ * @param {Object} opts - server options
41
+ * @param {Function} listener
42
+ */
43
+ MqttSecureServer = module.exports.SecureServer =
44
+ function SecureServer (opts, listener) {
45
+ if (!(this instanceof SecureServer)) {
46
+ return new SecureServer(opts, listener)
47
+ }
48
+
49
+ // new MqttSecureServer(function(){})
50
+ if (typeof opts === 'function') {
51
+ listener = opts
52
+ opts = {}
53
+ }
54
+
55
+ tls.Server.call(this, opts)
56
+
57
+ if (listener) {
58
+ this.on('client', listener)
59
+ }
60
+
61
+ this.on('secureConnection', setupConnection)
62
+
63
+ return this
64
+ }
65
+ inherits(MqttSecureServer, tls.Server)
package/test/store.js CHANGED
@@ -1,10 +1,10 @@
1
- 'use strict'
2
-
3
- var Store = require('../lib/store')
4
- var abstractTest = require('../test/abstract_store')
5
-
6
- describe('in-memory store', function () {
7
- abstractTest(function (done) {
8
- done(null, new Store())
9
- })
10
- })
1
+ 'use strict'
2
+
3
+ var Store = require('../lib/store')
4
+ var abstractTest = require('../test/abstract_store')
5
+
6
+ describe('in-memory store', function () {
7
+ abstractTest(function (done) {
8
+ done(null, new Store())
9
+ })
10
+ })
@@ -1,22 +1,22 @@
1
- // relative path uses package.json {"types":"types/index.d.ts", ...}
2
- import {IClientOptions, Client, connect, IConnackPacket} from '../..'
3
- const BROKER = 'test.mosquitto.org'
4
-
5
- const PAYLOAD = 'hello from TS'
6
- const TOPIC = 'typescript-test-' + Math.random().toString(16).substr(2)
7
- const opts: IClientOptions = {}
8
-
9
- console.log(`connect(${JSON.stringify(BROKER)})`)
10
- const client:Client = connect(`mqtt://${BROKER}`, opts)
11
-
12
- client.subscribe({[TOPIC]: 2}, (err, granted) => {
13
- granted.forEach(({topic, qos}) => {
14
- console.log(`subscribed to ${topic} with qos=${qos}`)
15
- })
16
- client.publish(TOPIC, PAYLOAD, {qos: 2})
17
- }).on('message', (topic: string, payload: Buffer) => {
18
- console.log(`message from ${topic}: ${payload}`)
19
- client.end()
20
- }).on('connect', (packet: IConnackPacket) => {
21
- console.log('connected!', JSON.stringify(packet))
22
- })
1
+ // relative path uses package.json {"types":"types/index.d.ts", ...}
2
+ import {IClientOptions, Client, connect, IConnackPacket} from '../..'
3
+ const BROKER = 'test.mosquitto.org'
4
+
5
+ const PAYLOAD = 'hello from TS'
6
+ const TOPIC = 'typescript-test-' + Math.random().toString(16).substr(2)
7
+ const opts: IClientOptions = {}
8
+
9
+ console.log(`connect(${JSON.stringify(BROKER)})`)
10
+ const client:Client = connect(`mqtt://${BROKER}`, opts)
11
+
12
+ client.subscribe({[TOPIC]: 2}, (err, granted) => {
13
+ granted.forEach(({topic, qos}) => {
14
+ console.log(`subscribed to ${topic} with qos=${qos}`)
15
+ })
16
+ client.publish(TOPIC, PAYLOAD, {qos: 2})
17
+ }).on('message', (topic: string, payload: Buffer) => {
18
+ console.log(`message from ${topic}: ${payload}`)
19
+ client.end()
20
+ }).on('connect', (packet: IConnackPacket) => {
21
+ console.log('connected!', JSON.stringify(packet))
22
+ })
@@ -1,14 +1,14 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "target": "es5",
5
- "moduleResolution": "node",
6
- "noEmitOnError": true,
7
- "noImplicitAny": true,
8
- "alwaysStrict": true,
9
- "strictNullChecks": true,
10
- "noImplicitReturns": true,
11
- "noImplicitThis": true,
12
- "sourceMap": true
13
- }
14
- }
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "target": "es5",
5
+ "moduleResolution": "node",
6
+ "noEmitOnError": true,
7
+ "noImplicitAny": true,
8
+ "alwaysStrict": true,
9
+ "strictNullChecks": true,
10
+ "noImplicitReturns": true,
11
+ "noImplicitThis": true,
12
+ "sourceMap": true
13
+ }
14
+ }
package/test/util.js CHANGED
@@ -1,13 +1,13 @@
1
- 'use strict'
2
-
3
- var through = require('through2')
4
-
5
- module.exports.testStream = function () {
6
- return through(function (buf, enc, cb) {
7
- var that = this
8
- setImmediate(function () {
9
- that.push(buf)
10
- cb()
11
- })
12
- })
13
- }
1
+ 'use strict'
2
+
3
+ var through = require('through2')
4
+
5
+ module.exports.testStream = function () {
6
+ return through(function (buf, enc, cb) {
7
+ var that = this
8
+ setImmediate(function () {
9
+ that.push(buf)
10
+ cb()
11
+ })
12
+ })
13
+ }