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,133 +1,135 @@
1
- 'use strict'
2
-
3
- require('should')
4
-
5
- module.exports = function abstractStoreTest (build) {
6
- var store
7
-
8
- beforeEach(function (done) {
9
- build(function (err, _store) {
10
- store = _store
11
- done(err)
12
- })
13
- })
14
-
15
- afterEach(function (done) {
16
- store.close(done)
17
- })
18
-
19
- it('should put and stream in-flight packets', function (done) {
20
- var packet = {
21
- topic: 'hello',
22
- payload: 'world',
23
- qos: 1,
24
- messageId: 42
25
- }
26
-
27
- store.put(packet, function () {
28
- store
29
- .createStream()
30
- .on('data', function (data) {
31
- data.should.eql(packet)
32
- done()
33
- })
34
- })
35
- })
36
-
37
- it('should support destroying the stream', function (done) {
38
- var packet = {
39
- topic: 'hello',
40
- payload: 'world',
41
- qos: 1,
42
- messageId: 42
43
- }
44
-
45
- store.put(packet, function () {
46
- var stream = store.createStream()
47
- stream.on('close', done)
48
- stream.destroy()
49
- })
50
- })
51
-
52
- it('should add and del in-flight packets', function (done) {
53
- var packet = {
54
- topic: 'hello',
55
- payload: 'world',
56
- qos: 1,
57
- messageId: 42
58
- }
59
-
60
- store.put(packet, function () {
61
- store.del(packet, function () {
62
- store
63
- .createStream()
64
- .on('data', function () {
65
- done(new Error('this should never happen'))
66
- })
67
- .on('end', done)
68
- })
69
- })
70
- })
71
-
72
- it('should replace a packet when doing put with the same messageId', function (done) {
73
- var packet1 = {
74
- topic: 'hello',
75
- payload: 'world',
76
- qos: 2,
77
- messageId: 42
78
- }
79
- var packet2 = {
80
- qos: 2,
81
- messageId: 42
82
- }
83
-
84
- store.put(packet1, function () {
85
- store.put(packet2, function () {
86
- store
87
- .createStream()
88
- .on('data', function (data) {
89
- data.should.eql(packet2)
90
- done()
91
- })
92
- })
93
- })
94
- })
95
-
96
- it('should return the original packet on del', function (done) {
97
- var packet = {
98
- topic: 'hello',
99
- payload: 'world',
100
- qos: 1,
101
- messageId: 42
102
- }
103
-
104
- store.put(packet, function () {
105
- store.del({ messageId: 42 }, function (err, deleted) {
106
- if (err) {
107
- throw err
108
- }
109
- deleted.should.eql(packet)
110
- done()
111
- })
112
- })
113
- })
114
-
115
- it('should get a packet with the same messageId', function (done) {
116
- var packet = {
117
- topic: 'hello',
118
- payload: 'world',
119
- qos: 1,
120
- messageId: 42
121
- }
122
-
123
- store.put(packet, function () {
124
- store.get({ messageId: 42 }, function (err, fromDb) {
125
- if (err) {
126
- throw err
127
- }
128
- fromDb.should.eql(packet)
129
- done()
130
- })
131
- })
132
- })
133
- }
1
+ 'use strict'
2
+
3
+ require('should')
4
+
5
+ module.exports = function abstractStoreTest (build) {
6
+ var store
7
+
8
+ beforeEach(function (done) {
9
+ build(function (err, _store) {
10
+ store = _store
11
+ done(err)
12
+ })
13
+ })
14
+
15
+ afterEach(function (done) {
16
+ store.close(done)
17
+ })
18
+
19
+ it('should put and stream in-flight packets', function (done) {
20
+ var packet = {
21
+ topic: 'hello',
22
+ payload: 'world',
23
+ qos: 1,
24
+ messageId: 42
25
+ }
26
+
27
+ store.put(packet, function () {
28
+ store
29
+ .createStream()
30
+ .on('data', function (data) {
31
+ data.should.eql(packet)
32
+ done()
33
+ })
34
+ })
35
+ })
36
+
37
+ it('should support destroying the stream', function (done) {
38
+ var packet = {
39
+ topic: 'hello',
40
+ payload: 'world',
41
+ qos: 1,
42
+ messageId: 42
43
+ }
44
+
45
+ store.put(packet, function () {
46
+ var stream = store.createStream()
47
+ stream.on('close', done)
48
+ stream.destroy()
49
+ })
50
+ })
51
+
52
+ it('should add and del in-flight packets', function (done) {
53
+ var packet = {
54
+ topic: 'hello',
55
+ payload: 'world',
56
+ qos: 1,
57
+ messageId: 42
58
+ }
59
+
60
+ store.put(packet, function () {
61
+ store.del(packet, function () {
62
+ store
63
+ .createStream()
64
+ .on('data', function () {
65
+ done(new Error('this should never happen'))
66
+ })
67
+ .on('end', done)
68
+ })
69
+ })
70
+ })
71
+
72
+ it('should replace a packet when doing put with the same messageId', function (done) {
73
+ var packet1 = {
74
+ cmd: 'publish', // added
75
+ topic: 'hello',
76
+ payload: 'world',
77
+ qos: 2,
78
+ messageId: 42
79
+ }
80
+ var packet2 = {
81
+ cmd: 'pubrel', // added
82
+ qos: 2,
83
+ messageId: 42
84
+ }
85
+
86
+ store.put(packet1, function () {
87
+ store.put(packet2, function () {
88
+ store
89
+ .createStream()
90
+ .on('data', function (data) {
91
+ data.should.eql(packet2)
92
+ done()
93
+ })
94
+ })
95
+ })
96
+ })
97
+
98
+ it('should return the original packet on del', function (done) {
99
+ var packet = {
100
+ topic: 'hello',
101
+ payload: 'world',
102
+ qos: 1,
103
+ messageId: 42
104
+ }
105
+
106
+ store.put(packet, function () {
107
+ store.del({ messageId: 42 }, function (err, deleted) {
108
+ if (err) {
109
+ throw err
110
+ }
111
+ deleted.should.eql(packet)
112
+ done()
113
+ })
114
+ })
115
+ })
116
+
117
+ it('should get a packet with the same messageId', function (done) {
118
+ var packet = {
119
+ topic: 'hello',
120
+ payload: 'world',
121
+ qos: 1,
122
+ messageId: 42
123
+ }
124
+
125
+ store.put(packet, function () {
126
+ store.get({ messageId: 42 }, function (err, fromDb) {
127
+ if (err) {
128
+ throw err
129
+ }
130
+ fromDb.should.eql(packet)
131
+ done()
132
+ })
133
+ })
134
+ })
135
+ }
@@ -1,132 +1,132 @@
1
- 'use strict'
2
-
3
- var handleClient
4
- var websocket = require('websocket-stream')
5
- var WebSocketServer = require('ws').Server
6
- var Connection = require('mqtt-connection')
7
- var http = require('http')
8
-
9
- handleClient = function (client) {
10
- var self = this
11
-
12
- if (!self.clients) {
13
- self.clients = {}
14
- }
15
-
16
- client.on('connect', function (packet) {
17
- if (packet.clientId === 'invalid') {
18
- client.connack({returnCode: 2})
19
- } else {
20
- client.connack({returnCode: 0})
21
- }
22
- self.clients[packet.clientId] = client
23
- client.subscriptions = []
24
- })
25
-
26
- client.on('publish', function (packet) {
27
- var i, k, c, s, publish
28
- switch (packet.qos) {
29
- case 0:
30
- break
31
- case 1:
32
- client.puback(packet)
33
- break
34
- case 2:
35
- client.pubrec(packet)
36
- break
37
- }
38
-
39
- for (k in self.clients) {
40
- c = self.clients[k]
41
- publish = false
42
-
43
- for (i = 0; i < c.subscriptions.length; i++) {
44
- s = c.subscriptions[i]
45
-
46
- if (s.test(packet.topic)) {
47
- publish = true
48
- }
49
- }
50
-
51
- if (publish) {
52
- try {
53
- c.publish({topic: packet.topic, payload: packet.payload})
54
- } catch (error) {
55
- delete self.clients[k]
56
- }
57
- }
58
- }
59
- })
60
-
61
- client.on('pubrel', function (packet) {
62
- client.pubcomp(packet)
63
- })
64
-
65
- client.on('pubrec', function (packet) {
66
- client.pubrel(packet)
67
- })
68
-
69
- client.on('pubcomp', function () {
70
- // Nothing to be done
71
- })
72
-
73
- client.on('subscribe', function (packet) {
74
- var qos
75
- var topic
76
- var reg
77
- var granted = []
78
-
79
- for (var i = 0; i < packet.subscriptions.length; i++) {
80
- qos = packet.subscriptions[i].qos
81
- topic = packet.subscriptions[i].topic
82
- reg = new RegExp(topic.replace('+', '[^/]+').replace('#', '.+') + '$')
83
-
84
- granted.push(qos)
85
- client.subscriptions.push(reg)
86
- }
87
-
88
- client.suback({messageId: packet.messageId, granted: granted})
89
- })
90
-
91
- client.on('unsubscribe', function (packet) {
92
- client.unsuback(packet)
93
- })
94
-
95
- client.on('pingreq', function () {
96
- client.pingresp()
97
- })
98
- }
99
-
100
- function start (startPort, done) {
101
- var server = http.createServer()
102
- var wss = new WebSocketServer({server: server})
103
-
104
- wss.on('connection', function (ws) {
105
- var stream, connection
106
-
107
- if (!(ws.protocol === 'mqtt' ||
108
- ws.protocol === 'mqttv3.1')) {
109
- return ws.close()
110
- }
111
-
112
- stream = websocket(ws)
113
- connection = new Connection(stream)
114
- handleClient.call(server, connection)
115
- })
116
- server.listen(startPort, done)
117
- server.on('request', function (req, res) {
118
- res.statusCode = 404
119
- res.end('Not Found')
120
- })
121
- return server
122
- }
123
-
124
- if (require.main === module) {
125
- start(process.env.PORT || process.env.ZUUL_PORT, function (err) {
126
- if (err) {
127
- console.error(err)
128
- return
129
- }
130
- console.log('tunnelled server started on port', process.env.PORT || process.env.ZUUL_PORT)
131
- })
132
- }
1
+ 'use strict'
2
+
3
+ var handleClient
4
+ var websocket = require('websocket-stream')
5
+ var WebSocketServer = require('ws').Server
6
+ var Connection = require('mqtt-connection')
7
+ var http = require('http')
8
+
9
+ handleClient = function (client) {
10
+ var self = this
11
+
12
+ if (!self.clients) {
13
+ self.clients = {}
14
+ }
15
+
16
+ client.on('connect', function (packet) {
17
+ if (packet.clientId === 'invalid') {
18
+ client.connack({returnCode: 2})
19
+ } else {
20
+ client.connack({returnCode: 0})
21
+ }
22
+ self.clients[packet.clientId] = client
23
+ client.subscriptions = []
24
+ })
25
+
26
+ client.on('publish', function (packet) {
27
+ var i, k, c, s, publish
28
+ switch (packet.qos) {
29
+ case 0:
30
+ break
31
+ case 1:
32
+ client.puback(packet)
33
+ break
34
+ case 2:
35
+ client.pubrec(packet)
36
+ break
37
+ }
38
+
39
+ for (k in self.clients) {
40
+ c = self.clients[k]
41
+ publish = false
42
+
43
+ for (i = 0; i < c.subscriptions.length; i++) {
44
+ s = c.subscriptions[i]
45
+
46
+ if (s.test(packet.topic)) {
47
+ publish = true
48
+ }
49
+ }
50
+
51
+ if (publish) {
52
+ try {
53
+ c.publish({topic: packet.topic, payload: packet.payload})
54
+ } catch (error) {
55
+ delete self.clients[k]
56
+ }
57
+ }
58
+ }
59
+ })
60
+
61
+ client.on('pubrel', function (packet) {
62
+ client.pubcomp(packet)
63
+ })
64
+
65
+ client.on('pubrec', function (packet) {
66
+ client.pubrel(packet)
67
+ })
68
+
69
+ client.on('pubcomp', function () {
70
+ // Nothing to be done
71
+ })
72
+
73
+ client.on('subscribe', function (packet) {
74
+ var qos
75
+ var topic
76
+ var reg
77
+ var granted = []
78
+
79
+ for (var i = 0; i < packet.subscriptions.length; i++) {
80
+ qos = packet.subscriptions[i].qos
81
+ topic = packet.subscriptions[i].topic
82
+ reg = new RegExp(topic.replace('+', '[^/]+').replace('#', '.+') + '$')
83
+
84
+ granted.push(qos)
85
+ client.subscriptions.push(reg)
86
+ }
87
+
88
+ client.suback({messageId: packet.messageId, granted: granted})
89
+ })
90
+
91
+ client.on('unsubscribe', function (packet) {
92
+ client.unsuback(packet)
93
+ })
94
+
95
+ client.on('pingreq', function () {
96
+ client.pingresp()
97
+ })
98
+ }
99
+
100
+ function start (startPort, done) {
101
+ var server = http.createServer()
102
+ var wss = new WebSocketServer({server: server})
103
+
104
+ wss.on('connection', function (ws) {
105
+ var stream, connection
106
+
107
+ if (!(ws.protocol === 'mqtt' ||
108
+ ws.protocol === 'mqttv3.1')) {
109
+ return ws.close()
110
+ }
111
+
112
+ stream = websocket(ws)
113
+ connection = new Connection(stream)
114
+ handleClient.call(server, connection)
115
+ })
116
+ server.listen(startPort, done)
117
+ server.on('request', function (req, res) {
118
+ res.statusCode = 404
119
+ res.end('Not Found')
120
+ })
121
+ return server
122
+ }
123
+
124
+ if (require.main === module) {
125
+ start(process.env.PORT || process.env.ZUUL_PORT, function (err) {
126
+ if (err) {
127
+ console.error(err)
128
+ return
129
+ }
130
+ console.log('tunnelled server started on port', process.env.PORT || process.env.ZUUL_PORT)
131
+ })
132
+ }