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,135 +0,0 @@
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 +0,0 @@
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.AIRTAP_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.AIRTAP_PORT)
131
- })
132
- }
@@ -1,92 +0,0 @@
1
- 'use strict'
2
-
3
- var mqtt = require('../../lib/connect')
4
- var _URL = require('url')
5
- var xtend = require('xtend')
6
- var parsed = _URL.parse(document.URL)
7
- var isHttps = parsed.protocol === 'https:'
8
- var port = parsed.port || (isHttps ? 443 : 80)
9
- var host = parsed.hostname
10
- var protocol = isHttps ? 'wss' : 'ws'
11
-
12
- function clientTests (buildClient) {
13
- var client
14
-
15
- beforeEach(function () {
16
- client = buildClient()
17
- client.on('offline', function () {
18
- console.log('client offline')
19
- })
20
- client.on('connect', function () {
21
- console.log('client connect')
22
- })
23
- client.on('reconnect', function () {
24
- console.log('client reconnect')
25
- })
26
- })
27
-
28
- afterEach(function (done) {
29
- client.once('close', function () {
30
- done()
31
- })
32
- client.end()
33
- })
34
-
35
- it('should connect', function (done) {
36
- client.on('connect', function () {
37
- done()
38
- })
39
- })
40
-
41
- it('should publish and subscribe', function (done) {
42
- client.subscribe('hello', function () {
43
- done()
44
- }).publish('hello', 'world')
45
- })
46
- }
47
-
48
- function suiteFactory (configName, opts) {
49
- function setVersion (base) {
50
- return xtend(base || {}, opts)
51
- }
52
-
53
- var suiteName = 'MqttClient(' + configName + '=' + JSON.stringify(opts) + ')'
54
- describe(suiteName, function () {
55
- this.timeout(10000)
56
-
57
- describe('specifying nothing', function () {
58
- clientTests(function () {
59
- return mqtt.connect(setVersion())
60
- })
61
- })
62
-
63
- if (parsed.hostname === 'localhost') {
64
- describe('specifying a port', function () {
65
- clientTests(function () {
66
- return mqtt.connect(setVersion({ protocol: protocol, port: port }))
67
- })
68
- })
69
- }
70
-
71
- describe('specifying a port and host', function () {
72
- clientTests(function () {
73
- return mqtt.connect(setVersion({ protocol: protocol, port: port, host: host }))
74
- })
75
- })
76
-
77
- describe('specifying a URL', function () {
78
- clientTests(function () {
79
- return mqtt.connect(protocol + '://' + host + ':' + port, setVersion())
80
- })
81
- })
82
-
83
- describe('specifying a URL with a path', function () {
84
- clientTests(function () {
85
- return mqtt.connect(protocol + '://' + host + ':' + port + '/mqtt', setVersion())
86
- })
87
- })
88
- })
89
- }
90
-
91
- suiteFactory('v3', {protocolId: 'MQIsdp', protocolVersion: 3})
92
- suiteFactory('default', {})