mqtt 2.18.9 → 3.0.0

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 (67) hide show
  1. package/CONTRIBUTING.md +27 -27
  2. package/LICENSE.md +15 -15
  3. package/README.md +689 -612
  4. package/bin/pub.js +146 -146
  5. package/bin/sub.js +123 -123
  6. package/dist/mqtt.js +11916 -14116
  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 +1460 -1129
  22. package/lib/connect/ali.js +130 -0
  23. package/lib/connect/index.js +157 -149
  24. package/lib/connect/tcp.js +19 -19
  25. package/lib/connect/tls.js +41 -41
  26. package/lib/connect/ws.js +92 -92
  27. package/lib/connect/wx.js +134 -110
  28. package/lib/store.js +140 -140
  29. package/lib/validations.js +52 -52
  30. package/mqtt.js +41 -41
  31. package/package.json +112 -107
  32. package/test/abstract_client.js +3084 -2667
  33. package/test/abstract_store.js +135 -135
  34. package/test/browser/server.js +132 -132
  35. package/test/browser/test.js +92 -92
  36. package/test/client.js +1129 -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 +93 -65
  54. package/test/store.js +10 -10
  55. package/test/typescript/broker-connect-subscribe-and-publish.js +24 -0
  56. package/test/typescript/broker-connect-subscribe-and-publish.js.map +1 -0
  57. package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
  58. package/test/typescript/tsconfig.json +14 -14
  59. package/test/util.js +13 -13
  60. package/test/websocket_client.js +144 -144
  61. package/types/index.d.ts +27 -27
  62. package/types/lib/client-options.d.ts +178 -137
  63. package/types/lib/client.d.ts +231 -196
  64. package/types/lib/connect/index.d.ts +10 -10
  65. package/types/lib/store-options.d.ts +6 -6
  66. package/types/lib/store.d.ts +46 -46
  67. package/test/browser/wx.js +0 -93
package/lib/store.js CHANGED
@@ -1,140 +1,140 @@
1
- 'use strict'
2
-
3
- /**
4
- * Module dependencies
5
- */
6
- var xtend = require('xtend')
7
-
8
- var Readable = require('readable-stream').Readable
9
- var streamsOpts = { objectMode: true }
10
- var defaultStoreOptions = {
11
- clean: true
12
- }
13
-
14
- /**
15
- * es6-map can preserve insertion order even if ES version is older.
16
- *
17
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Description
18
- * It should be noted that a Map which is a map of an object, especially
19
- * a dictionary of dictionaries, will only map to the object's insertion
20
- * order. In ES2015 this is ordered for objects but for older versions of
21
- * ES, this may be random and not ordered.
22
- *
23
- */
24
- var Map = require('es6-map')
25
-
26
- /**
27
- * In-memory implementation of the message store
28
- * This can actually be saved into files.
29
- *
30
- * @param {Object} [options] - store options
31
- */
32
- function Store (options) {
33
- if (!(this instanceof Store)) {
34
- return new Store(options)
35
- }
36
-
37
- this.options = options || {}
38
-
39
- // Defaults
40
- this.options = xtend(defaultStoreOptions, options)
41
-
42
- this._inflights = new Map()
43
- }
44
-
45
- /**
46
- * Adds a packet to the store, a packet is
47
- * anything that has a messageId property.
48
- *
49
- */
50
- Store.prototype.put = function (packet, cb) {
51
- this._inflights.set(packet.messageId, packet)
52
-
53
- if (cb) {
54
- cb()
55
- }
56
-
57
- return this
58
- }
59
-
60
- /**
61
- * Creates a stream with all the packets in the store
62
- *
63
- */
64
- Store.prototype.createStream = function () {
65
- var stream = new Readable(streamsOpts)
66
- var destroyed = false
67
- var values = []
68
- var i = 0
69
-
70
- this._inflights.forEach(function (value, key) {
71
- values.push(value)
72
- })
73
-
74
- stream._read = function () {
75
- if (!destroyed && i < values.length) {
76
- this.push(values[i++])
77
- } else {
78
- this.push(null)
79
- }
80
- }
81
-
82
- stream.destroy = function () {
83
- if (destroyed) {
84
- return
85
- }
86
-
87
- var self = this
88
-
89
- destroyed = true
90
-
91
- process.nextTick(function () {
92
- self.emit('close')
93
- })
94
- }
95
-
96
- return stream
97
- }
98
-
99
- /**
100
- * deletes a packet from the store.
101
- */
102
- Store.prototype.del = function (packet, cb) {
103
- packet = this._inflights.get(packet.messageId)
104
- if (packet) {
105
- this._inflights.delete(packet.messageId)
106
- cb(null, packet)
107
- } else if (cb) {
108
- cb(new Error('missing packet'))
109
- }
110
-
111
- return this
112
- }
113
-
114
- /**
115
- * get a packet from the store.
116
- */
117
- Store.prototype.get = function (packet, cb) {
118
- packet = this._inflights.get(packet.messageId)
119
- if (packet) {
120
- cb(null, packet)
121
- } else if (cb) {
122
- cb(new Error('missing packet'))
123
- }
124
-
125
- return this
126
- }
127
-
128
- /**
129
- * Close the store
130
- */
131
- Store.prototype.close = function (cb) {
132
- if (this.options.clean) {
133
- this._inflights = null
134
- }
135
- if (cb) {
136
- cb()
137
- }
138
- }
139
-
140
- module.exports = Store
1
+ 'use strict'
2
+
3
+ /**
4
+ * Module dependencies
5
+ */
6
+ var xtend = require('xtend')
7
+
8
+ var Readable = require('readable-stream').Readable
9
+ var streamsOpts = { objectMode: true }
10
+ var defaultStoreOptions = {
11
+ clean: true
12
+ }
13
+
14
+ /**
15
+ * es6-map can preserve insertion order even if ES version is older.
16
+ *
17
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Description
18
+ * It should be noted that a Map which is a map of an object, especially
19
+ * a dictionary of dictionaries, will only map to the object's insertion
20
+ * order. In ES2015 this is ordered for objects but for older versions of
21
+ * ES, this may be random and not ordered.
22
+ *
23
+ */
24
+ var Map = require('es6-map')
25
+
26
+ /**
27
+ * In-memory implementation of the message store
28
+ * This can actually be saved into files.
29
+ *
30
+ * @param {Object} [options] - store options
31
+ */
32
+ function Store (options) {
33
+ if (!(this instanceof Store)) {
34
+ return new Store(options)
35
+ }
36
+
37
+ this.options = options || {}
38
+
39
+ // Defaults
40
+ this.options = xtend(defaultStoreOptions, options)
41
+
42
+ this._inflights = new Map()
43
+ }
44
+
45
+ /**
46
+ * Adds a packet to the store, a packet is
47
+ * anything that has a messageId property.
48
+ *
49
+ */
50
+ Store.prototype.put = function (packet, cb) {
51
+ this._inflights.set(packet.messageId, packet)
52
+
53
+ if (cb) {
54
+ cb()
55
+ }
56
+
57
+ return this
58
+ }
59
+
60
+ /**
61
+ * Creates a stream with all the packets in the store
62
+ *
63
+ */
64
+ Store.prototype.createStream = function () {
65
+ var stream = new Readable(streamsOpts)
66
+ var destroyed = false
67
+ var values = []
68
+ var i = 0
69
+
70
+ this._inflights.forEach(function (value, key) {
71
+ values.push(value)
72
+ })
73
+
74
+ stream._read = function () {
75
+ if (!destroyed && i < values.length) {
76
+ this.push(values[i++])
77
+ } else {
78
+ this.push(null)
79
+ }
80
+ }
81
+
82
+ stream.destroy = function () {
83
+ if (destroyed) {
84
+ return
85
+ }
86
+
87
+ var self = this
88
+
89
+ destroyed = true
90
+
91
+ process.nextTick(function () {
92
+ self.emit('close')
93
+ })
94
+ }
95
+
96
+ return stream
97
+ }
98
+
99
+ /**
100
+ * deletes a packet from the store.
101
+ */
102
+ Store.prototype.del = function (packet, cb) {
103
+ packet = this._inflights.get(packet.messageId)
104
+ if (packet) {
105
+ this._inflights.delete(packet.messageId)
106
+ cb(null, packet)
107
+ } else if (cb) {
108
+ cb(new Error('missing packet'))
109
+ }
110
+
111
+ return this
112
+ }
113
+
114
+ /**
115
+ * get a packet from the store.
116
+ */
117
+ Store.prototype.get = function (packet, cb) {
118
+ packet = this._inflights.get(packet.messageId)
119
+ if (packet) {
120
+ cb(null, packet)
121
+ } else if (cb) {
122
+ cb(new Error('missing packet'))
123
+ }
124
+
125
+ return this
126
+ }
127
+
128
+ /**
129
+ * Close the store
130
+ */
131
+ Store.prototype.close = function (cb) {
132
+ if (this.options.clean) {
133
+ this._inflights = null
134
+ }
135
+ if (cb) {
136
+ cb()
137
+ }
138
+ }
139
+
140
+ module.exports = Store
@@ -1,52 +1,52 @@
1
- 'use strict'
2
-
3
- /**
4
- * Validate a topic to see if it's valid or not.
5
- * A topic is valid if it follow below rules:
6
- * - Rule #1: If any part of the topic is not `+` or `#`, then it must not contain `+` and '#'
7
- * - Rule #2: Part `#` must be located at the end of the mailbox
8
- *
9
- * @param {String} topic - A topic
10
- * @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false.
11
- */
12
- function validateTopic (topic) {
13
- var parts = topic.split('/')
14
-
15
- for (var i = 0; i < parts.length; i++) {
16
- if (parts[i] === '+') {
17
- continue
18
- }
19
-
20
- if (parts[i] === '#') {
21
- // for Rule #2
22
- return i === parts.length - 1
23
- }
24
-
25
- if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) {
26
- return false
27
- }
28
- }
29
-
30
- return true
31
- }
32
-
33
- /**
34
- * Validate an array of topics to see if any of them is valid or not
35
- * @param {Array} topics - Array of topics
36
- * @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
37
- */
38
- function validateTopics (topics) {
39
- if (topics.length === 0) {
40
- return 'empty_topic_list'
41
- }
42
- for (var i = 0; i < topics.length; i++) {
43
- if (!validateTopic(topics[i])) {
44
- return topics[i]
45
- }
46
- }
47
- return null
48
- }
49
-
50
- module.exports = {
51
- validateTopics: validateTopics
52
- }
1
+ 'use strict'
2
+
3
+ /**
4
+ * Validate a topic to see if it's valid or not.
5
+ * A topic is valid if it follow below rules:
6
+ * - Rule #1: If any part of the topic is not `+` or `#`, then it must not contain `+` and '#'
7
+ * - Rule #2: Part `#` must be located at the end of the mailbox
8
+ *
9
+ * @param {String} topic - A topic
10
+ * @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false.
11
+ */
12
+ function validateTopic (topic) {
13
+ var parts = topic.split('/')
14
+
15
+ for (var i = 0; i < parts.length; i++) {
16
+ if (parts[i] === '+') {
17
+ continue
18
+ }
19
+
20
+ if (parts[i] === '#') {
21
+ // for Rule #2
22
+ return i === parts.length - 1
23
+ }
24
+
25
+ if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) {
26
+ return false
27
+ }
28
+ }
29
+
30
+ return true
31
+ }
32
+
33
+ /**
34
+ * Validate an array of topics to see if any of them is valid or not
35
+ * @param {Array} topics - Array of topics
36
+ * @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
37
+ */
38
+ function validateTopics (topics) {
39
+ if (topics.length === 0) {
40
+ return 'empty_topic_list'
41
+ }
42
+ for (var i = 0; i < topics.length; i++) {
43
+ if (!validateTopic(topics[i])) {
44
+ return topics[i]
45
+ }
46
+ }
47
+ return null
48
+ }
49
+
50
+ module.exports = {
51
+ validateTopics: validateTopics
52
+ }
package/mqtt.js CHANGED
@@ -1,41 +1,41 @@
1
- #!/usr/bin/env node
2
- 'use strict'
3
-
4
- /*
5
- * Copyright (c) 2015-2015 MQTT.js contributors.
6
- * Copyright (c) 2011-2014 Adam Rudd.
7
- *
8
- * See LICENSE for more information
9
- */
10
-
11
- var MqttClient = require('./lib/client')
12
- var connect = require('./lib/connect')
13
- var Store = require('./lib/store')
14
-
15
- module.exports.connect = connect
16
-
17
- // Expose MqttClient
18
- module.exports.MqttClient = MqttClient
19
- module.exports.Client = MqttClient
20
- module.exports.Store = Store
21
-
22
- function cli () {
23
- var commist = require('commist')()
24
- var helpMe = require('help-me')()
25
-
26
- commist.register('publish', require('./bin/pub'))
27
- commist.register('subscribe', require('./bin/sub'))
28
- commist.register('version', function () {
29
- console.log('MQTT.js version:', require('./package.json').version)
30
- })
31
- commist.register('help', helpMe.toStdout)
32
-
33
- if (commist.parse(process.argv.slice(2)) !== null) {
34
- console.log('No such command:', process.argv[2], '\n')
35
- helpMe.toStdout()
36
- }
37
- }
38
-
39
- if (require.main === module) {
40
- cli()
41
- }
1
+ #!/usr/bin/env node
2
+ 'use strict'
3
+
4
+ /*
5
+ * Copyright (c) 2015-2015 MQTT.js contributors.
6
+ * Copyright (c) 2011-2014 Adam Rudd.
7
+ *
8
+ * See LICENSE for more information
9
+ */
10
+
11
+ var MqttClient = require('./lib/client')
12
+ var connect = require('./lib/connect')
13
+ var Store = require('./lib/store')
14
+
15
+ module.exports.connect = connect
16
+
17
+ // Expose MqttClient
18
+ module.exports.MqttClient = MqttClient
19
+ module.exports.Client = MqttClient
20
+ module.exports.Store = Store
21
+
22
+ function cli () {
23
+ var commist = require('commist')()
24
+ var helpMe = require('help-me')()
25
+
26
+ commist.register('publish', require('./bin/pub'))
27
+ commist.register('subscribe', require('./bin/sub'))
28
+ commist.register('version', function () {
29
+ console.log('MQTT.js version:', require('./package.json').version)
30
+ })
31
+ commist.register('help', helpMe.toStdout)
32
+
33
+ if (commist.parse(process.argv.slice(2)) !== null) {
34
+ console.log('No such command:', process.argv[2], '\n')
35
+ helpMe.toStdout()
36
+ }
37
+ }
38
+
39
+ if (require.main === module) {
40
+ cli()
41
+ }