mqtt 4.2.4 → 4.2.8

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.
package/lib/connect/wx.js CHANGED
@@ -1,134 +1,134 @@
1
- 'use strict'
2
-
3
- var Transform = require('readable-stream').Transform
4
- var duplexify = require('duplexify')
5
-
6
- /* global wx */
7
- var socketTask
8
- var proxy
9
- var stream
10
-
11
- function buildProxy () {
12
- var proxy = new Transform()
13
- proxy._write = function (chunk, encoding, next) {
14
- socketTask.send({
15
- data: chunk.buffer,
16
- success: function () {
17
- next()
18
- },
19
- fail: function (errMsg) {
20
- next(new Error(errMsg))
21
- }
22
- })
23
- }
24
- proxy._flush = function socketEnd (done) {
25
- socketTask.close({
26
- success: function () {
27
- done()
28
- }
29
- })
30
- }
31
-
32
- return proxy
33
- }
34
-
35
- function setDefaultOpts (opts) {
36
- if (!opts.hostname) {
37
- opts.hostname = 'localhost'
38
- }
39
- if (!opts.path) {
40
- opts.path = '/'
41
- }
42
-
43
- if (!opts.wsOptions) {
44
- opts.wsOptions = {}
45
- }
46
- }
47
-
48
- function buildUrl (opts, client) {
49
- var protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
50
- var url = protocol + '://' + opts.hostname + opts.path
51
- if (opts.port && opts.port !== 80 && opts.port !== 443) {
52
- url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
53
- }
54
- if (typeof (opts.transformWsUrl) === 'function') {
55
- url = opts.transformWsUrl(url, opts, client)
56
- }
57
- return url
58
- }
59
-
60
- function bindEventHandler () {
61
- socketTask.onOpen(function () {
62
- stream.setReadable(proxy)
63
- stream.setWritable(proxy)
64
- stream.emit('connect')
65
- })
66
-
67
- socketTask.onMessage(function (res) {
68
- var data = res.data
69
-
70
- if (data instanceof ArrayBuffer) data = Buffer.from(data)
71
- else data = Buffer.from(data, 'utf8')
72
- proxy.push(data)
73
- })
74
-
75
- socketTask.onClose(function () {
76
- stream.end()
77
- stream.destroy()
78
- })
79
-
80
- socketTask.onError(function (res) {
81
- stream.destroy(new Error(res.errMsg))
82
- })
83
- }
84
-
85
- function buildStream (client, opts) {
86
- opts.hostname = opts.hostname || opts.host
87
-
88
- if (!opts.hostname) {
89
- throw new Error('Could not determine host. Specify host manually.')
90
- }
91
-
92
- var websocketSubProtocol =
93
- (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
94
- ? 'mqttv3.1'
95
- : 'mqtt'
96
-
97
- setDefaultOpts(opts)
98
-
99
- var url = buildUrl(opts, client)
100
- socketTask = wx.connectSocket({
101
- url: url,
102
- protocols: [websocketSubProtocol]
103
- })
104
-
105
- proxy = buildProxy()
106
- stream = duplexify.obj()
107
- stream._destroy = function (err, cb) {
108
- socketTask.close({
109
- success: function () {
110
- cb && cb(err)
111
- }
112
- })
113
- }
114
-
115
- var destroyRef = stream.destroy
116
- stream.destroy = function () {
117
- stream.destroy = destroyRef
118
-
119
- var self = this
120
- setTimeout(function () {
121
- socketTask.close({
122
- fail: function () {
123
- self._destroy(new Error())
124
- }
125
- })
126
- }, 0)
127
- }.bind(stream)
128
-
129
- bindEventHandler()
130
-
131
- return stream
132
- }
133
-
134
- module.exports = buildStream
1
+ 'use strict'
2
+
3
+ var Transform = require('readable-stream').Transform
4
+ var duplexify = require('duplexify')
5
+
6
+ /* global wx */
7
+ var socketTask
8
+ var proxy
9
+ var stream
10
+
11
+ function buildProxy () {
12
+ var proxy = new Transform()
13
+ proxy._write = function (chunk, encoding, next) {
14
+ socketTask.send({
15
+ data: chunk.buffer,
16
+ success: function () {
17
+ next()
18
+ },
19
+ fail: function (errMsg) {
20
+ next(new Error(errMsg))
21
+ }
22
+ })
23
+ }
24
+ proxy._flush = function socketEnd (done) {
25
+ socketTask.close({
26
+ success: function () {
27
+ done()
28
+ }
29
+ })
30
+ }
31
+
32
+ return proxy
33
+ }
34
+
35
+ function setDefaultOpts (opts) {
36
+ if (!opts.hostname) {
37
+ opts.hostname = 'localhost'
38
+ }
39
+ if (!opts.path) {
40
+ opts.path = '/'
41
+ }
42
+
43
+ if (!opts.wsOptions) {
44
+ opts.wsOptions = {}
45
+ }
46
+ }
47
+
48
+ function buildUrl (opts, client) {
49
+ var protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
50
+ var url = protocol + '://' + opts.hostname + opts.path
51
+ if (opts.port && opts.port !== 80 && opts.port !== 443) {
52
+ url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
53
+ }
54
+ if (typeof (opts.transformWsUrl) === 'function') {
55
+ url = opts.transformWsUrl(url, opts, client)
56
+ }
57
+ return url
58
+ }
59
+
60
+ function bindEventHandler () {
61
+ socketTask.onOpen(function () {
62
+ stream.setReadable(proxy)
63
+ stream.setWritable(proxy)
64
+ stream.emit('connect')
65
+ })
66
+
67
+ socketTask.onMessage(function (res) {
68
+ var data = res.data
69
+
70
+ if (data instanceof ArrayBuffer) data = Buffer.from(data)
71
+ else data = Buffer.from(data, 'utf8')
72
+ proxy.push(data)
73
+ })
74
+
75
+ socketTask.onClose(function () {
76
+ stream.end()
77
+ stream.destroy()
78
+ })
79
+
80
+ socketTask.onError(function (res) {
81
+ stream.destroy(new Error(res.errMsg))
82
+ })
83
+ }
84
+
85
+ function buildStream (client, opts) {
86
+ opts.hostname = opts.hostname || opts.host
87
+
88
+ if (!opts.hostname) {
89
+ throw new Error('Could not determine host. Specify host manually.')
90
+ }
91
+
92
+ var websocketSubProtocol =
93
+ (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
94
+ ? 'mqttv3.1'
95
+ : 'mqtt'
96
+
97
+ setDefaultOpts(opts)
98
+
99
+ var url = buildUrl(opts, client)
100
+ socketTask = wx.connectSocket({
101
+ url: url,
102
+ protocols: [websocketSubProtocol]
103
+ })
104
+
105
+ proxy = buildProxy()
106
+ stream = duplexify.obj()
107
+ stream._destroy = function (err, cb) {
108
+ socketTask.close({
109
+ success: function () {
110
+ cb && cb(err)
111
+ }
112
+ })
113
+ }
114
+
115
+ var destroyRef = stream.destroy
116
+ stream.destroy = function () {
117
+ stream.destroy = destroyRef
118
+
119
+ var self = this
120
+ setTimeout(function () {
121
+ socketTask.close({
122
+ fail: function () {
123
+ self._destroy(new Error())
124
+ }
125
+ })
126
+ }, 0)
127
+ }.bind(stream)
128
+
129
+ bindEventHandler()
130
+
131
+ return stream
132
+ }
133
+
134
+ module.exports = buildStream
package/lib/store.js CHANGED
@@ -1,128 +1,128 @@
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
- * In-memory implementation of the message store
16
- * This can actually be saved into files.
17
- *
18
- * @param {Object} [options] - store options
19
- */
20
- function Store (options) {
21
- if (!(this instanceof Store)) {
22
- return new Store(options)
23
- }
24
-
25
- this.options = options || {}
26
-
27
- // Defaults
28
- this.options = xtend(defaultStoreOptions, options)
29
-
30
- this._inflights = new Map()
31
- }
32
-
33
- /**
34
- * Adds a packet to the store, a packet is
35
- * anything that has a messageId property.
36
- *
37
- */
38
- Store.prototype.put = function (packet, cb) {
39
- this._inflights.set(packet.messageId, packet)
40
-
41
- if (cb) {
42
- cb()
43
- }
44
-
45
- return this
46
- }
47
-
48
- /**
49
- * Creates a stream with all the packets in the store
50
- *
51
- */
52
- Store.prototype.createStream = function () {
53
- var stream = new Readable(streamsOpts)
54
- var destroyed = false
55
- var values = []
56
- var i = 0
57
-
58
- this._inflights.forEach(function (value, key) {
59
- values.push(value)
60
- })
61
-
62
- stream._read = function () {
63
- if (!destroyed && i < values.length) {
64
- this.push(values[i++])
65
- } else {
66
- this.push(null)
67
- }
68
- }
69
-
70
- stream.destroy = function () {
71
- if (destroyed) {
72
- return
73
- }
74
-
75
- var self = this
76
-
77
- destroyed = true
78
-
79
- setTimeout(function () {
80
- self.emit('close')
81
- }, 0)
82
- }
83
-
84
- return stream
85
- }
86
-
87
- /**
88
- * deletes a packet from the store.
89
- */
90
- Store.prototype.del = function (packet, cb) {
91
- packet = this._inflights.get(packet.messageId)
92
- if (packet) {
93
- this._inflights.delete(packet.messageId)
94
- cb(null, packet)
95
- } else if (cb) {
96
- cb(new Error('missing packet'))
97
- }
98
-
99
- return this
100
- }
101
-
102
- /**
103
- * get a packet from the store.
104
- */
105
- Store.prototype.get = function (packet, cb) {
106
- packet = this._inflights.get(packet.messageId)
107
- if (packet) {
108
- cb(null, packet)
109
- } else if (cb) {
110
- cb(new Error('missing packet'))
111
- }
112
-
113
- return this
114
- }
115
-
116
- /**
117
- * Close the store
118
- */
119
- Store.prototype.close = function (cb) {
120
- if (this.options.clean) {
121
- this._inflights = null
122
- }
123
- if (cb) {
124
- cb()
125
- }
126
- }
127
-
128
- 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
+ * In-memory implementation of the message store
16
+ * This can actually be saved into files.
17
+ *
18
+ * @param {Object} [options] - store options
19
+ */
20
+ function Store (options) {
21
+ if (!(this instanceof Store)) {
22
+ return new Store(options)
23
+ }
24
+
25
+ this.options = options || {}
26
+
27
+ // Defaults
28
+ this.options = xtend(defaultStoreOptions, options)
29
+
30
+ this._inflights = new Map()
31
+ }
32
+
33
+ /**
34
+ * Adds a packet to the store, a packet is
35
+ * anything that has a messageId property.
36
+ *
37
+ */
38
+ Store.prototype.put = function (packet, cb) {
39
+ this._inflights.set(packet.messageId, packet)
40
+
41
+ if (cb) {
42
+ cb()
43
+ }
44
+
45
+ return this
46
+ }
47
+
48
+ /**
49
+ * Creates a stream with all the packets in the store
50
+ *
51
+ */
52
+ Store.prototype.createStream = function () {
53
+ var stream = new Readable(streamsOpts)
54
+ var destroyed = false
55
+ var values = []
56
+ var i = 0
57
+
58
+ this._inflights.forEach(function (value, key) {
59
+ values.push(value)
60
+ })
61
+
62
+ stream._read = function () {
63
+ if (!destroyed && i < values.length) {
64
+ this.push(values[i++])
65
+ } else {
66
+ this.push(null)
67
+ }
68
+ }
69
+
70
+ stream.destroy = function () {
71
+ if (destroyed) {
72
+ return
73
+ }
74
+
75
+ var self = this
76
+
77
+ destroyed = true
78
+
79
+ setTimeout(function () {
80
+ self.emit('close')
81
+ }, 0)
82
+ }
83
+
84
+ return stream
85
+ }
86
+
87
+ /**
88
+ * deletes a packet from the store.
89
+ */
90
+ Store.prototype.del = function (packet, cb) {
91
+ packet = this._inflights.get(packet.messageId)
92
+ if (packet) {
93
+ this._inflights.delete(packet.messageId)
94
+ cb(null, packet)
95
+ } else if (cb) {
96
+ cb(new Error('missing packet'))
97
+ }
98
+
99
+ return this
100
+ }
101
+
102
+ /**
103
+ * get a packet from the store.
104
+ */
105
+ Store.prototype.get = function (packet, cb) {
106
+ packet = this._inflights.get(packet.messageId)
107
+ if (packet) {
108
+ cb(null, packet)
109
+ } else if (cb) {
110
+ cb(new Error('missing packet'))
111
+ }
112
+
113
+ return this
114
+ }
115
+
116
+ /**
117
+ * Close the store
118
+ */
119
+ Store.prototype.close = function (cb) {
120
+ if (this.options.clean) {
121
+ this._inflights = null
122
+ }
123
+ if (cb) {
124
+ cb()
125
+ }
126
+ }
127
+
128
+ 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,17 +1,17 @@
1
- /*
2
- * Copyright (c) 2015-2015 MQTT.js contributors.
3
- * Copyright (c) 2011-2014 Adam Rudd.
4
- *
5
- * See LICENSE for more information
6
- */
7
-
8
- var MqttClient = require('./lib/client')
9
- var connect = require('./lib/connect')
10
- var Store = require('./lib/store')
11
-
12
- module.exports.connect = connect
13
-
14
- // Expose MqttClient
15
- module.exports.MqttClient = MqttClient
16
- module.exports.Client = MqttClient
17
- module.exports.Store = Store
1
+ /*
2
+ * Copyright (c) 2015-2015 MQTT.js contributors.
3
+ * Copyright (c) 2011-2014 Adam Rudd.
4
+ *
5
+ * See LICENSE for more information
6
+ */
7
+
8
+ var MqttClient = require('./lib/client')
9
+ var connect = require('./lib/connect')
10
+ var Store = require('./lib/store')
11
+
12
+ module.exports.connect = connect
13
+
14
+ // Expose MqttClient
15
+ module.exports.MqttClient = MqttClient
16
+ module.exports.Client = MqttClient
17
+ module.exports.Store = Store