mqtt 4.0.0 → 4.2.1

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 (59) hide show
  1. package/README.md +58 -24
  2. package/bin/mqtt.js +27 -0
  3. package/bin/pub.js +0 -0
  4. package/bin/sub.js +0 -0
  5. package/dist/mqtt.js +2794 -2512
  6. package/dist/mqtt.min.js +1 -1
  7. package/lib/client.js +72 -59
  8. package/lib/connect/index.js +7 -2
  9. package/lib/connect/tcp.js +2 -0
  10. package/lib/connect/tls.js +3 -0
  11. package/lib/connect/ws.js +194 -34
  12. package/lib/connect/wx.js +3 -3
  13. package/lib/store.js +2 -2
  14. package/mqtt.js +0 -24
  15. package/package.json +9 -10
  16. package/types/lib/client-options.d.ts +14 -1
  17. package/types/lib/client.d.ts +4 -3
  18. package/examples/client/secure-client.js +0 -24
  19. package/examples/client/simple-both.js +0 -13
  20. package/examples/client/simple-publish.js +0 -7
  21. package/examples/client/simple-subscribe.js +0 -9
  22. package/examples/tls client/crt.ca.cg.pem +0 -34
  23. package/examples/tls client/mqttclient.js +0 -48
  24. package/examples/tls client/tls-cert.pem +0 -13
  25. package/examples/tls client/tls-key.pem +0 -15
  26. package/examples/wss/client.js +0 -49
  27. package/examples/wss/client_with_proxy.js +0 -58
  28. package/test/abstract_client.js +0 -3139
  29. package/test/abstract_store.js +0 -135
  30. package/test/browser/server.js +0 -132
  31. package/test/browser/test.js +0 -92
  32. package/test/client.js +0 -540
  33. package/test/client_mqtt5.js +0 -537
  34. package/test/helpers/port_list.js +0 -45
  35. package/test/helpers/private-csr.pem +0 -16
  36. package/test/helpers/private-key.pem +0 -27
  37. package/test/helpers/public-cert.pem +0 -19
  38. package/test/helpers/public-key.pem +0 -9
  39. package/test/helpers/server.js +0 -53
  40. package/test/helpers/server_process.js +0 -9
  41. package/test/helpers/tls-cert.pem +0 -14
  42. package/test/helpers/tls-csr.pem +0 -11
  43. package/test/helpers/tls-key.pem +0 -15
  44. package/test/helpers/wrong-cert.pem +0 -13
  45. package/test/helpers/wrong-csr.pem +0 -11
  46. package/test/helpers/wrong-key.pem +0 -15
  47. package/test/mocha.opts +0 -4
  48. package/test/mqtt.js +0 -230
  49. package/test/mqtt_store.js +0 -9
  50. package/test/secure_client.js +0 -188
  51. package/test/server.js +0 -94
  52. package/test/server_helpers_for_client_tests.js +0 -100
  53. package/test/store.js +0 -10
  54. package/test/typescript/broker-connect-subscribe-and-publish.js +0 -24
  55. package/test/typescript/broker-connect-subscribe-and-publish.js.map +0 -1
  56. package/test/typescript/broker-connect-subscribe-and-publish.ts +0 -25
  57. package/test/typescript/tsconfig.json +0 -14
  58. package/test/util.js +0 -13
  59. package/test/websocket_client.js +0 -146
@@ -1,53 +0,0 @@
1
- 'use strict'
2
-
3
- var MqttServer = require('../server').MqttServer
4
- var MqttSecureServer = require('../server').MqttSecureServer
5
- var fs = require('fs')
6
-
7
- module.exports.init_server = function (PORT) {
8
- var server = new MqttServer(function (client) {
9
- client.on('connect', function () {
10
- client.connack(0)
11
- })
12
-
13
- client.on('publish', function (packet) {
14
- switch (packet.qos) {
15
- case 1:
16
- client.puback({messageId: packet.messageId})
17
- break
18
- case 2:
19
- client.pubrec({messageId: packet.messageId})
20
- break
21
- default:
22
- break
23
- }
24
- })
25
-
26
- client.on('pubrel', function (packet) {
27
- client.pubcomp({messageId: packet.messageId})
28
- })
29
-
30
- client.on('pingreq', function () {
31
- client.pingresp()
32
- })
33
-
34
- client.on('disconnect', function () {
35
- client.stream.end()
36
- })
37
- })
38
- server.listen(PORT)
39
- return server
40
- }
41
-
42
- module.exports.init_secure_server = function (port, key, cert) {
43
- var server = new MqttSecureServer({
44
- key: fs.readFileSync(key),
45
- cert: fs.readFileSync(cert)
46
- }, function (client) {
47
- client.on('connect', function () {
48
- client.connack({returnCode: 0})
49
- })
50
- })
51
- server.listen(port)
52
- return server
53
- }
@@ -1,9 +0,0 @@
1
- 'use strict'
2
-
3
- var MqttServer = require('../server').MqttServer
4
-
5
- new MqttServer(function (client) {
6
- client.on('connect', function () {
7
- client.connack({ returnCode: 0 })
8
- })
9
- }).listen(3000, 'localhost')
@@ -1,14 +0,0 @@
1
- -----BEGIN CERTIFICATE-----
2
- MIICKTCCAZICCQDRSYqWgZyJmjANBgkqhkiG9w0BAQUFADBZMQswCQYDVQQGEwJB
3
- VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0
4
- cyBQdHkgTHRkMRIwEAYDVQQDEwlsb2NhbGhvc3QwHhcNMTQwNjEzMTAwMzAzWhcN
5
- MjQwNjEwMTAwMzAzWjBZMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0
6
- ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMRIwEAYDVQQDEwls
7
- b2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMzFv8+9EBb1sG07
8
- TjdtbRksRwF7/CZsOWe+ef4ZYPolC5lzvNVYXsBIjL+ilhyKopBbwnOuX9+6FmYO
9
- G/N1lDZRssolGoOVM+1ma3Whmxz8C1g+xi95nP2OqtwP5Du6xhvOM265CiMaf8DH
10
- n63ZFxyi3d1CdNGamNQvrybCzJn7AgMBAAEwDQYJKoZIhvcNAQEFBQADgYEABmyp
11
- 3wyBGjb2zSHK5pF9c9GXyHRL4/FkP6qzU5NWrfVAowdOczctJbc3hxPh34Encbr6
12
- KijYnbdP7/f8aZrStLGqgFYL3SHZY3zvgLTzOmGr9reHUkubHtN+mWHeYy1wVe3D
13
- qEOI8ygT4olVZmWAD+VLKgAb0J07rA/PKf82fBI=
14
- -----END CERTIFICATE-----
@@ -1,11 +0,0 @@
1
- -----BEGIN CERTIFICATE REQUEST-----
2
- MIIBmTCCAQICAQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUx
3
- ITAfBgNVBAoTGEludGVybmV0IFdpZG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Nt8EJqMabOi2UpW1+VZJn206D0f3XmNSmZbk8oozGrl
12
- qg2wSTZYlZClCTpWO2Y+iYzojY8kmLaQ2xbTxBz1XlshC8HvsA==
13
- -----END CERTIFICATE-----
@@ -1,11 +0,0 @@
1
- -----BEGIN CERTIFICATE REQUEST-----
2
- MIIBhDCB7gIBADBFMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEh
3
- MB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEB
4
- AQUAA4GNADCBiQKBgQDIMwhVLZdKLNlPwp4VgVB8q3sJxPn8+TPDYEMmgxUuauR8
5
- d112OxOCzCod/OE7wWZ+DoEzGCwTPZoqLbiEnFAW5DUMaHJ8oeePEzk+pIuI6RAM
6
- qWWcA6Co4ENelxrSNfKkKTUhWEyykzbKWucFemzedMEsnEUGRlIGi/8kOSQv5wID
7
- AQABoAAwDQYJKoZIhvcNAQEFBQADgYEAFXqd8jhW+2hRvkRB1CCVBK5e6AQHq1rF
8
- s3B36O64hRHIr1KC+dWr8vv1t9Rkud+7E3ELHtxWCORIYpqQ2Ddldt4PP+MTNj2C
9
- qgwOpxM0VDxeeWml8fqx2uzfPhVduyHGm0yff2JS2KRVmnIPLTUuz/+udukIFDVO
10
- Sc4/W3qY7f8=
11
- -----END CERTIFICATE REQUEST-----
@@ -1,15 +0,0 @@
1
- -----BEGIN RSA PRIVATE KEY-----
2
- MIICXQIBAAKBgQDIMwhVLZdKLNlPwp4VgVB8q3sJxPn8+TPDYEMmgxUuauR8d112
3
- OxOCzCod/OE7wWZ+DoEzGCwTPZoqLbiEnFAW5DUMaHJ8oeePEzk+pIuI6RAMqWWc
4
- A6Co4ENelxrSNfKkKTUhWEyykzbKWucFemzedMEsnEUGRlIGi/8kOSQv5wIDAQAB
5
- AoGBALOzszgaG2I2jb4dmJ7/G4s8tc2YJTlhS4iFgOEx6rJmur/KuXcmIiZXMzsF
6
- wftMZ76hMHH3saB3vEk+DxHh6bR6cW/I82Vxts9suz2fRnd2mh5JHI+opXE53LVn
7
- hJcQ4k6LJ9MNVxlHwCTrSuJvtikDOOrCARHRvYzFRL4wXvmpAkEA+DFzXGDg8PxX
8
- RFp6RLLbqaUT6YXNi+E5ERuumru+rgRj+OF/dxNK3d1lcIJZjqVMDAgOsZ66/bkh
9
- GfCzJPREUwJBAM5/HeHmTEM5K5B0X8b6XEHTgWFUNTu4K36Ee5ySd8RYI8zjQ9wS
10
- NM1nXnx12npL7DSkShz9xgnTe0f8YmQnc50CQQCgdE/RXCxwf6LnZNsBCOSsIzXh
11
- VgiRsxSSs+PI0zGuDNaY8yfV0ponH1fSSeMeLk0gxiDBwg2/tGzq+UrHzEdTAkB1
12
- /U5O0K+MzbLlxIkhgdaLSlYoDdyo9e/sR7j12v8SMqaqIMWajtCa+VCU3yZqMM2T
13
- urgaXqr03GEZ3c0+mwhFAkAwWkczV1iwuedmWLKc36iQhoj+FRMUoxWe/fBixQls
14
- g0lDvwWiZ3M6hjCsBRckmt8eU2mUh79Odrj5fRWIwXaX
15
- -----END RSA PRIVATE KEY-----
package/test/mocha.opts DELETED
@@ -1,4 +0,0 @@
1
- --check-leaks
2
- --timeout 10000
3
- --exit
4
-
package/test/mqtt.js DELETED
@@ -1,230 +0,0 @@
1
- 'use strict'
2
-
3
- var fs = require('fs')
4
- var path = require('path')
5
- var mqtt = require('../')
6
-
7
- describe('mqtt', function () {
8
- describe('#connect', function () {
9
- var sslOpts, sslOpts2
10
- it('should return an MqttClient when connect is called with mqtt:/ url', function () {
11
- var c = mqtt.connect('mqtt://localhost:1883')
12
-
13
- c.should.be.instanceOf(mqtt.MqttClient)
14
- c.end()
15
- })
16
-
17
- it('should throw an error when called with no protocol specified', function () {
18
- (function () {
19
- var c = mqtt.connect('foo.bar.com')
20
- c.end()
21
- }).should.throw('Missing protocol')
22
- })
23
-
24
- it('should throw an error when called with no protocol specified - with options', function () {
25
- (function () {
26
- var c = mqtt.connect('tcp://foo.bar.com', { protocol: null })
27
- c.end()
28
- }).should.throw('Missing protocol')
29
- })
30
-
31
- it('should return an MqttClient with username option set', function () {
32
- var c = mqtt.connect('mqtt://user:pass@localhost:1883')
33
-
34
- c.should.be.instanceOf(mqtt.MqttClient)
35
- c.options.should.have.property('username', 'user')
36
- c.options.should.have.property('password', 'pass')
37
- c.end()
38
- })
39
-
40
- it('should return an MqttClient with username and password options set', function () {
41
- var c = mqtt.connect('mqtt://user@localhost:1883')
42
-
43
- c.should.be.instanceOf(mqtt.MqttClient)
44
- c.options.should.have.property('username', 'user')
45
- c.end()
46
- })
47
-
48
- it('should return an MqttClient with the clientid with random value', function () {
49
- var c = mqtt.connect('mqtt://user@localhost:1883')
50
-
51
- c.should.be.instanceOf(mqtt.MqttClient)
52
- c.options.should.have.property('clientId')
53
- c.end()
54
- })
55
-
56
- it('should return an MqttClient with the clientid with empty string', function () {
57
- var c = mqtt.connect('mqtt://user@localhost:1883?clientId=')
58
-
59
- c.should.be.instanceOf(mqtt.MqttClient)
60
- c.options.should.have.property('clientId', '')
61
- c.end()
62
- })
63
-
64
- it('should return an MqttClient with the clientid option set', function () {
65
- var c = mqtt.connect('mqtt://user@localhost:1883?clientId=123')
66
-
67
- c.should.be.instanceOf(mqtt.MqttClient)
68
- c.options.should.have.property('clientId', '123')
69
- c.end()
70
- })
71
-
72
- it('should return an MqttClient when connect is called with tcp:/ url', function () {
73
- var c = mqtt.connect('tcp://localhost')
74
-
75
- c.should.be.instanceOf(mqtt.MqttClient)
76
- c.end()
77
- })
78
-
79
- it('should return an MqttClient with correct host when called with a host and port', function () {
80
- var c = mqtt.connect('tcp://user:pass@localhost:1883')
81
-
82
- c.options.should.have.property('hostname', 'localhost')
83
- c.options.should.have.property('port', 1883)
84
- c.end()
85
- })
86
-
87
- sslOpts = {
88
- keyPath: path.join(__dirname, 'helpers', 'private-key.pem'),
89
- certPath: path.join(__dirname, 'helpers', 'public-cert.pem'),
90
- caPaths: [path.join(__dirname, 'helpers', 'public-cert.pem')]
91
- }
92
-
93
- it('should return an MqttClient when connect is called with mqtts:/ url', function () {
94
- var c = mqtt.connect('mqtts://localhost', sslOpts)
95
-
96
- c.options.should.have.property('protocol', 'mqtts')
97
-
98
- c.on('error', function () {})
99
-
100
- c.should.be.instanceOf(mqtt.MqttClient)
101
- c.end()
102
- })
103
-
104
- it('should return an MqttClient when connect is called with ssl:/ url', function () {
105
- var c = mqtt.connect('ssl://localhost', sslOpts)
106
-
107
- c.options.should.have.property('protocol', 'ssl')
108
-
109
- c.on('error', function () {})
110
-
111
- c.should.be.instanceOf(mqtt.MqttClient)
112
- c.end()
113
- })
114
-
115
- it('should return an MqttClient when connect is called with ws:/ url', function () {
116
- var c = mqtt.connect('ws://localhost', sslOpts)
117
-
118
- c.options.should.have.property('protocol', 'ws')
119
-
120
- c.on('error', function () {})
121
-
122
- c.should.be.instanceOf(mqtt.MqttClient)
123
- c.end()
124
- })
125
-
126
- it('should return an MqttClient when connect is called with wss:/ url', function () {
127
- var c = mqtt.connect('wss://localhost', sslOpts)
128
-
129
- c.options.should.have.property('protocol', 'wss')
130
-
131
- c.on('error', function () {})
132
-
133
- c.should.be.instanceOf(mqtt.MqttClient)
134
- c.end()
135
- })
136
-
137
- sslOpts2 = {
138
- key: fs.readFileSync(path.join(__dirname, 'helpers', 'private-key.pem')),
139
- cert: fs.readFileSync(path.join(__dirname, 'helpers', 'public-cert.pem')),
140
- ca: [fs.readFileSync(path.join(__dirname, 'helpers', 'public-cert.pem'))]
141
- }
142
-
143
- it('should throw an error when it is called with cert and key set but no protocol specified', function () {
144
- // to do rewrite wrap function
145
- (function () {
146
- var c = mqtt.connect(sslOpts2)
147
- c.end()
148
- }).should.throw('Missing secure protocol key')
149
- })
150
-
151
- it('should throw an error when it is called with cert and key set and protocol other than allowed: mqtt,mqtts,ws,wss,wxs', function () {
152
- (function () {
153
- sslOpts2.protocol = 'UNKNOWNPROTOCOL'
154
- var c = mqtt.connect(sslOpts2)
155
- c.end()
156
- }).should.throw()
157
- })
158
-
159
- it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtt', function () {
160
- sslOpts2.protocol = 'mqtt'
161
- var c = mqtt.connect(sslOpts2)
162
-
163
- c.options.should.have.property('protocol', 'mqtts')
164
-
165
- c.on('error', function () {})
166
-
167
- c.should.be.instanceOf(mqtt.MqttClient)
168
- })
169
-
170
- it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtts', function () {
171
- sslOpts2.protocol = 'mqtts'
172
- var c = mqtt.connect(sslOpts2)
173
-
174
- c.options.should.have.property('protocol', 'mqtts')
175
-
176
- c.on('error', function () {})
177
-
178
- c.should.be.instanceOf(mqtt.MqttClient)
179
- })
180
-
181
- it('should return a MqttClient with wss set when connect is called key and cert set and protocol ws', function () {
182
- sslOpts2.protocol = 'ws'
183
- var c = mqtt.connect(sslOpts2)
184
-
185
- c.options.should.have.property('protocol', 'wss')
186
-
187
- c.on('error', function () {})
188
-
189
- c.should.be.instanceOf(mqtt.MqttClient)
190
- })
191
-
192
- it('should return a MqttClient with wss set when connect is called key and cert set and protocol wss', function () {
193
- sslOpts2.protocol = 'wss'
194
- var c = mqtt.connect(sslOpts2)
195
-
196
- c.options.should.have.property('protocol', 'wss')
197
-
198
- c.on('error', function () {})
199
-
200
- c.should.be.instanceOf(mqtt.MqttClient)
201
- })
202
-
203
- it('should return an MqttClient with the clientid with option of clientId as empty string', function () {
204
- var c = mqtt.connect('mqtt://localhost:1883', {
205
- clientId: ''
206
- })
207
-
208
- c.should.be.instanceOf(mqtt.MqttClient)
209
- c.options.should.have.property('clientId', '')
210
- })
211
-
212
- it('should return an MqttClient with the clientid with option of clientId empty', function () {
213
- var c = mqtt.connect('mqtt://localhost:1883')
214
-
215
- c.should.be.instanceOf(mqtt.MqttClient)
216
- c.options.should.have.property('clientId')
217
- c.end()
218
- })
219
-
220
- it('should return an MqttClient with the clientid with option of with specific clientId', function () {
221
- var c = mqtt.connect('mqtt://localhost:1883', {
222
- clientId: '123'
223
- })
224
-
225
- c.should.be.instanceOf(mqtt.MqttClient)
226
- c.options.should.have.property('clientId', '123')
227
- c.end()
228
- })
229
- })
230
- })
@@ -1,9 +0,0 @@
1
- 'use strict'
2
-
3
- var mqtt = require('../lib/connect')
4
-
5
- describe('store in lib/connect/index.js (webpack entry point)', function () {
6
- it('should create store', function (done) {
7
- done(null, new mqtt.Store())
8
- })
9
- })
@@ -1,188 +0,0 @@
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 MqttSecureServer = require('./server').MqttSecureServer
12
- var assert = require('chai').assert
13
-
14
- var serverListener = function (client) {
15
- // this is the Server's MQTT Client
16
- client.on('connect', function (packet) {
17
- if (packet.clientId === 'invalid') {
18
- client.connack({returnCode: 2})
19
- } else {
20
- server.emit('connect', client)
21
- client.connack({returnCode: 0})
22
- }
23
- })
24
-
25
- client.on('publish', function (packet) {
26
- setImmediate(function () {
27
- /* jshint -W027 */
28
- /* eslint default-case:0 */
29
- switch (packet.qos) {
30
- case 0:
31
- break
32
- case 1:
33
- client.puback(packet)
34
- break
35
- case 2:
36
- client.pubrec(packet)
37
- break
38
- }
39
- /* jshint +W027 */
40
- })
41
- })
42
-
43
- client.on('pubrel', function (packet) {
44
- client.pubcomp(packet)
45
- })
46
-
47
- client.on('pubrec', function (packet) {
48
- client.pubrel(packet)
49
- })
50
-
51
- client.on('pubcomp', function () {
52
- // Nothing to be done
53
- })
54
-
55
- client.on('subscribe', function (packet) {
56
- client.suback({
57
- messageId: packet.messageId,
58
- granted: packet.subscriptions.map(function (e) {
59
- return e.qos
60
- })
61
- })
62
- })
63
-
64
- client.on('unsubscribe', function (packet) {
65
- client.unsuback(packet)
66
- })
67
-
68
- client.on('pingreq', function () {
69
- client.pingresp()
70
- })
71
- }
72
-
73
- var server = new MqttSecureServer({
74
- key: fs.readFileSync(KEY),
75
- cert: fs.readFileSync(CERT)
76
- }, serverListener).listen(port)
77
-
78
- describe('MqttSecureClient', function () {
79
- var config = { protocol: 'mqtts', port: port, rejectUnauthorized: false }
80
- abstractClientTests(server, config)
81
-
82
- describe('with secure parameters', function () {
83
- it('should validate successfully the CA', function (done) {
84
- var client = mqtt.connect({
85
- protocol: 'mqtts',
86
- port: port,
87
- ca: [fs.readFileSync(CERT)],
88
- rejectUnauthorized: true
89
- })
90
-
91
- client.on('error', function (err) {
92
- done(err)
93
- })
94
-
95
- server.once('connect', function () {
96
- done()
97
- })
98
- })
99
-
100
- it('should validate successfully the CA using URI', function (done) {
101
- var client = mqtt.connect('mqtts://localhost:' + port, {
102
- ca: [fs.readFileSync(CERT)],
103
- rejectUnauthorized: true
104
- })
105
-
106
- client.on('error', function (err) {
107
- done(err)
108
- })
109
-
110
- server.once('connect', function () {
111
- done()
112
- })
113
- })
114
-
115
- it('should validate successfully the CA using URI with path', function (done) {
116
- var client = mqtt.connect('mqtts://localhost:' + port + '/', {
117
- ca: [fs.readFileSync(CERT)],
118
- rejectUnauthorized: true
119
- })
120
-
121
- client.on('error', function (err) {
122
- done(err)
123
- })
124
-
125
- server.once('connect', function () {
126
- done()
127
- })
128
- })
129
-
130
- it('should validate unsuccessfully the CA', function (done) {
131
- var client = mqtt.connect({
132
- protocol: 'mqtts',
133
- port: port,
134
- ca: [fs.readFileSync(WRONG_CERT)],
135
- rejectUnauthorized: true
136
- })
137
-
138
- client.once('error', function () {
139
- done()
140
- client.end()
141
- client.on('error', function () {})
142
- })
143
- })
144
-
145
- it('should emit close on TLS error', function (done) {
146
- var client = mqtt.connect({
147
- protocol: 'mqtts',
148
- port: port,
149
- ca: [fs.readFileSync(WRONG_CERT)],
150
- rejectUnauthorized: true
151
- })
152
-
153
- client.on('error', function () {})
154
-
155
- // TODO node v0.8.x emits multiple close events
156
- client.once('close', function () {
157
- done()
158
- })
159
- })
160
-
161
- it('should support SNI on the TLS connection', function (done) {
162
- var hostname, client
163
- server.removeAllListeners('secureConnection') // clear eventHandler
164
- server.once('secureConnection', function (tlsSocket) { // one time eventHandler
165
- assert.equal(tlsSocket.servername, hostname) // validate SNI set
166
- server.setupConnection(tlsSocket)
167
- })
168
-
169
- hostname = 'localhost'
170
- client = mqtt.connect({
171
- protocol: 'mqtts',
172
- port: port,
173
- ca: [fs.readFileSync(CERT)],
174
- rejectUnauthorized: true,
175
- host: hostname
176
- })
177
-
178
- client.on('error', function (err) {
179
- done(err)
180
- })
181
-
182
- server.once('connect', function () {
183
- server.on('secureConnection', server.setupConnection) // reset eventHandler
184
- done()
185
- })
186
- })
187
- })
188
- })
package/test/server.js DELETED
@@ -1,94 +0,0 @@
1
- 'use strict'
2
-
3
- var net = require('net')
4
- var tls = require('tls')
5
- var Connection = require('mqtt-connection')
6
-
7
- /**
8
- * MqttServer
9
- *
10
- * @param {Function} listener - fired on client connection
11
- */
12
- class MqttServer extends net.Server {
13
- constructor (listener) {
14
- super()
15
- this.connectionList = []
16
-
17
- var that = this
18
- this.on('connection', function (duplex) {
19
- this.connectionList.push(duplex)
20
- var connection = new Connection(duplex, function () {
21
- that.emit('client', connection)
22
- })
23
- })
24
-
25
- if (listener) {
26
- this.on('client', listener)
27
- }
28
- }
29
- }
30
-
31
- /**
32
- * MqttServerNoWait (w/o waiting for initialization)
33
- *
34
- * @param {Function} listener - fired on client connection
35
- */
36
- class MqttServerNoWait extends net.Server {
37
- constructor (listener) {
38
- super()
39
- this.connectionList = []
40
-
41
- this.on('connection', function (duplex) {
42
- this.connectionList.push(duplex)
43
- var connection = new Connection(duplex)
44
- // do not wait for connection to return to send it to the client.
45
- this.emit('client', connection)
46
- })
47
-
48
- if (listener) {
49
- this.on('client', listener)
50
- }
51
- }
52
- }
53
-
54
- /**
55
- * MqttSecureServer
56
- *
57
- * @param {Object} opts - server options
58
- * @param {Function} listener
59
- */
60
- class MqttSecureServer extends tls.Server {
61
- constructor (opts, listener) {
62
- if (typeof opts === 'function') {
63
- listener = opts
64
- opts = {}
65
- }
66
-
67
- // sets a listener for the 'connection' event
68
- super(opts)
69
- this.connectionList = []
70
-
71
- this.on('secureConnection', function (socket) {
72
- this.connectionList.push(socket)
73
- var that = this
74
- var connection = new Connection(socket, function () {
75
- that.emit('client', connection)
76
- })
77
- })
78
-
79
- if (listener) {
80
- this.on('client', listener)
81
- }
82
- }
83
-
84
- setupConnection (duplex) {
85
- var that = this
86
- var connection = new Connection(duplex, function () {
87
- that.emit('client', connection)
88
- })
89
- }
90
- }
91
-
92
- exports.MqttServer = MqttServer
93
- exports.MqttServerNoWait = MqttServerNoWait
94
- exports.MqttSecureServer = MqttSecureServer