mqtt 2.18.8 → 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 +14248 -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 -1129
  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 -2667
  32. package/test/abstract_store.js +135 -135
  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
package/lib/connect/wx.js CHANGED
@@ -1,110 +1,110 @@
1
- 'use strict'
2
-
3
- /* global wx */
4
- var socketOpen = false
5
- var socketMsgQueue = []
6
-
7
- function sendSocketMessage (msg) {
8
- if (socketOpen) {
9
- wx.sendSocketMessage({
10
- data: msg.buffer || msg
11
- })
12
- } else {
13
- socketMsgQueue.push(msg)
14
- }
15
- }
16
-
17
- function WebSocket (url, protocols) {
18
- var ws = {
19
- OPEN: 1,
20
- CLOSING: 2,
21
- CLOSED: 3,
22
- readyState: socketOpen ? 1 : 0,
23
- send: sendSocketMessage,
24
- close: wx.closeSocket,
25
- onopen: null,
26
- onmessage: null,
27
- onclose: null,
28
- onerror: null
29
- }
30
-
31
- wx.connectSocket({
32
- url: url,
33
- protocols: protocols
34
- })
35
- wx.onSocketOpen(function (res) {
36
- ws.readyState = ws.OPEN
37
- socketOpen = true
38
- for (var i = 0; i < socketMsgQueue.length; i++) {
39
- sendSocketMessage(socketMsgQueue[i])
40
- }
41
- socketMsgQueue = []
42
-
43
- ws.onopen && ws.onopen.apply(ws, arguments)
44
- })
45
- wx.onSocketMessage(function (res) {
46
- ws.onmessage && ws.onmessage.apply(ws, arguments)
47
- })
48
- wx.onSocketClose(function () {
49
- ws.onclose && ws.onclose.apply(ws, arguments)
50
- ws.readyState = ws.CLOSED
51
- socketOpen = false
52
- })
53
- wx.onSocketError(function () {
54
- ws.onerror && ws.onerror.apply(ws, arguments)
55
- ws.readyState = ws.CLOSED
56
- socketOpen = false
57
- })
58
-
59
- return ws
60
- }
61
-
62
- var websocket = require('websocket-stream')
63
-
64
- function buildUrl (opts, client) {
65
- var protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
66
- var url = protocol + '://' + opts.hostname + opts.path
67
- if (opts.port && opts.port !== 80 && opts.port !== 443) {
68
- url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
69
- }
70
- if (typeof (opts.transformWsUrl) === 'function') {
71
- url = opts.transformWsUrl(url, opts, client)
72
- }
73
- return url
74
- }
75
-
76
- function setDefaultOpts (opts) {
77
- if (!opts.hostname) {
78
- opts.hostname = 'localhost'
79
- }
80
- if (!opts.path) {
81
- opts.path = '/'
82
- }
83
-
84
- if (!opts.wsOptions) {
85
- opts.wsOptions = {}
86
- }
87
- }
88
-
89
- function createWebSocket (client, opts) {
90
- var websocketSubProtocol =
91
- (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
92
- ? 'mqttv3.1'
93
- : 'mqtt'
94
-
95
- setDefaultOpts(opts)
96
- var url = buildUrl(opts, client)
97
- return websocket(WebSocket(url, [websocketSubProtocol]))
98
- }
99
-
100
- function buildBuilder (client, opts) {
101
- opts.hostname = opts.hostname || opts.host
102
-
103
- if (!opts.hostname) {
104
- throw new Error('Could not determine host. Specify host manually.')
105
- }
106
-
107
- return createWebSocket(client, opts)
108
- }
109
-
110
- module.exports = buildBuilder
1
+ 'use strict'
2
+
3
+ /* global wx */
4
+ var socketOpen = false
5
+ var socketMsgQueue = []
6
+
7
+ function sendSocketMessage (msg) {
8
+ if (socketOpen) {
9
+ wx.sendSocketMessage({
10
+ data: msg.buffer || msg
11
+ })
12
+ } else {
13
+ socketMsgQueue.push(msg)
14
+ }
15
+ }
16
+
17
+ function WebSocket (url, protocols) {
18
+ var ws = {
19
+ OPEN: 1,
20
+ CLOSING: 2,
21
+ CLOSED: 3,
22
+ readyState: socketOpen ? 1 : 0,
23
+ send: sendSocketMessage,
24
+ close: wx.closeSocket,
25
+ onopen: null,
26
+ onmessage: null,
27
+ onclose: null,
28
+ onerror: null
29
+ }
30
+
31
+ wx.connectSocket({
32
+ url: url,
33
+ protocols: protocols
34
+ })
35
+ wx.onSocketOpen(function (res) {
36
+ ws.readyState = ws.OPEN
37
+ socketOpen = true
38
+ for (var i = 0; i < socketMsgQueue.length; i++) {
39
+ sendSocketMessage(socketMsgQueue[i])
40
+ }
41
+ socketMsgQueue = []
42
+
43
+ ws.onopen && ws.onopen.apply(ws, arguments)
44
+ })
45
+ wx.onSocketMessage(function (res) {
46
+ ws.onmessage && ws.onmessage.apply(ws, arguments)
47
+ })
48
+ wx.onSocketClose(function () {
49
+ ws.onclose && ws.onclose.apply(ws, arguments)
50
+ ws.readyState = ws.CLOSED
51
+ socketOpen = false
52
+ })
53
+ wx.onSocketError(function () {
54
+ ws.onerror && ws.onerror.apply(ws, arguments)
55
+ ws.readyState = ws.CLOSED
56
+ socketOpen = false
57
+ })
58
+
59
+ return ws
60
+ }
61
+
62
+ var websocket = require('websocket-stream')
63
+
64
+ function buildUrl (opts, client) {
65
+ var protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
66
+ var url = protocol + '://' + opts.hostname + opts.path
67
+ if (opts.port && opts.port !== 80 && opts.port !== 443) {
68
+ url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
69
+ }
70
+ if (typeof (opts.transformWsUrl) === 'function') {
71
+ url = opts.transformWsUrl(url, opts, client)
72
+ }
73
+ return url
74
+ }
75
+
76
+ function setDefaultOpts (opts) {
77
+ if (!opts.hostname) {
78
+ opts.hostname = 'localhost'
79
+ }
80
+ if (!opts.path) {
81
+ opts.path = '/'
82
+ }
83
+
84
+ if (!opts.wsOptions) {
85
+ opts.wsOptions = {}
86
+ }
87
+ }
88
+
89
+ function createWebSocket (client, opts) {
90
+ var websocketSubProtocol =
91
+ (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
92
+ ? 'mqttv3.1'
93
+ : 'mqtt'
94
+
95
+ setDefaultOpts(opts)
96
+ var url = buildUrl(opts, client)
97
+ return websocket(WebSocket(url, [websocketSubProtocol]))
98
+ }
99
+
100
+ function buildBuilder (client, opts) {
101
+ opts.hostname = opts.hostname || opts.host
102
+
103
+ if (!opts.hostname) {
104
+ throw new Error('Could not determine host. Specify host manually.')
105
+ }
106
+
107
+ return createWebSocket(client, opts)
108
+ }
109
+
110
+ module.exports = buildBuilder
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
+ }