mqtt 4.2.8 → 4.3.3

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,132 @@
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
+ const Transform = require('readable-stream').Transform
4
+ const duplexify = require('duplexify')
5
+
6
+ /* global wx */
7
+ let socketTask, proxy, stream
8
+
9
+ function buildProxy () {
10
+ const proxy = new Transform()
11
+ proxy._write = function (chunk, encoding, next) {
12
+ socketTask.send({
13
+ data: chunk.buffer,
14
+ success: function () {
15
+ next()
16
+ },
17
+ fail: function (errMsg) {
18
+ next(new Error(errMsg))
19
+ }
20
+ })
21
+ }
22
+ proxy._flush = function socketEnd (done) {
23
+ socketTask.close({
24
+ success: function () {
25
+ done()
26
+ }
27
+ })
28
+ }
29
+
30
+ return proxy
31
+ }
32
+
33
+ function setDefaultOpts (opts) {
34
+ if (!opts.hostname) {
35
+ opts.hostname = 'localhost'
36
+ }
37
+ if (!opts.path) {
38
+ opts.path = '/'
39
+ }
40
+
41
+ if (!opts.wsOptions) {
42
+ opts.wsOptions = {}
43
+ }
44
+ }
45
+
46
+ function buildUrl (opts, client) {
47
+ const protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
48
+ let url = protocol + '://' + opts.hostname + opts.path
49
+ if (opts.port && opts.port !== 80 && opts.port !== 443) {
50
+ url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
51
+ }
52
+ if (typeof (opts.transformWsUrl) === 'function') {
53
+ url = opts.transformWsUrl(url, opts, client)
54
+ }
55
+ return url
56
+ }
57
+
58
+ function bindEventHandler () {
59
+ socketTask.onOpen(function () {
60
+ stream.setReadable(proxy)
61
+ stream.setWritable(proxy)
62
+ stream.emit('connect')
63
+ })
64
+
65
+ socketTask.onMessage(function (res) {
66
+ let data = res.data
67
+
68
+ if (data instanceof ArrayBuffer) data = Buffer.from(data)
69
+ else data = Buffer.from(data, 'utf8')
70
+ proxy.push(data)
71
+ })
72
+
73
+ socketTask.onClose(function () {
74
+ stream.end()
75
+ stream.destroy()
76
+ })
77
+
78
+ socketTask.onError(function (res) {
79
+ stream.destroy(new Error(res.errMsg))
80
+ })
81
+ }
82
+
83
+ function buildStream (client, opts) {
84
+ opts.hostname = opts.hostname || opts.host
85
+
86
+ if (!opts.hostname) {
87
+ throw new Error('Could not determine host. Specify host manually.')
88
+ }
89
+
90
+ const websocketSubProtocol =
91
+ (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
92
+ ? 'mqttv3.1'
93
+ : 'mqtt'
94
+
95
+ setDefaultOpts(opts)
96
+
97
+ const url = buildUrl(opts, client)
98
+ socketTask = wx.connectSocket({
99
+ url: url,
100
+ protocols: [websocketSubProtocol]
101
+ })
102
+
103
+ proxy = buildProxy()
104
+ stream = duplexify.obj()
105
+ stream._destroy = function (err, cb) {
106
+ socketTask.close({
107
+ success: function () {
108
+ cb && cb(err)
109
+ }
110
+ })
111
+ }
112
+
113
+ const destroyRef = stream.destroy
114
+ stream.destroy = function () {
115
+ stream.destroy = destroyRef
116
+
117
+ const self = this
118
+ setTimeout(function () {
119
+ socketTask.close({
120
+ fail: function () {
121
+ self._destroy(new Error())
122
+ }
123
+ })
124
+ }, 0)
125
+ }.bind(stream)
126
+
127
+ bindEventHandler()
128
+
129
+ return stream
130
+ }
131
+
132
+ module.exports = buildStream
@@ -0,0 +1,69 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * DefaultMessageAllocator constructor
5
+ * @constructor
6
+ */
7
+ function DefaultMessageIdProvider () {
8
+ if (!(this instanceof DefaultMessageIdProvider)) {
9
+ return new DefaultMessageIdProvider()
10
+ }
11
+
12
+ /**
13
+ * MessageIDs starting with 1
14
+ * ensure that nextId is min. 1, see https://github.com/mqttjs/MQTT.js/issues/810
15
+ */
16
+ this.nextId = Math.max(1, Math.floor(Math.random() * 65535))
17
+ }
18
+
19
+ /**
20
+ * allocate
21
+ *
22
+ * Get the next messageId.
23
+ * @return unsigned int
24
+ */
25
+ DefaultMessageIdProvider.prototype.allocate = function () {
26
+ // id becomes current state of this.nextId and increments afterwards
27
+ const id = this.nextId++
28
+ // Ensure 16 bit unsigned int (max 65535, nextId got one higher)
29
+ if (this.nextId === 65536) {
30
+ this.nextId = 1
31
+ }
32
+ return id
33
+ }
34
+
35
+ /**
36
+ * getLastAllocated
37
+ * Get the last allocated messageId.
38
+ * @return unsigned int
39
+ */
40
+ DefaultMessageIdProvider.prototype.getLastAllocated = function () {
41
+ return (this.nextId === 1) ? 65535 : (this.nextId - 1)
42
+ }
43
+
44
+ /**
45
+ * register
46
+ * Register messageId. If success return true, otherwise return false.
47
+ * @param { unsigned int } - messageId to register,
48
+ * @return boolean
49
+ */
50
+ DefaultMessageIdProvider.prototype.register = function (messageId) {
51
+ return true
52
+ }
53
+
54
+ /**
55
+ * deallocate
56
+ * Deallocate messageId.
57
+ * @param { unsigned int } - messageId to deallocate,
58
+ */
59
+ DefaultMessageIdProvider.prototype.deallocate = function (messageId) {
60
+ }
61
+
62
+ /**
63
+ * clear
64
+ * Deallocate all messageIds.
65
+ */
66
+ DefaultMessageIdProvider.prototype.clear = function () {
67
+ }
68
+
69
+ module.exports = DefaultMessageIdProvider
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
+ const xtend = require('xtend')
7
+
8
+ const Readable = require('readable-stream').Readable
9
+ const streamsOpts = { objectMode: true }
10
+ const 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
+ const stream = new Readable(streamsOpts)
54
+ const values = []
55
+ let destroyed = false
56
+ let 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
+ const 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
@@ -0,0 +1,47 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * Topic Alias receiving manager
5
+ * This holds alias to topic map
6
+ * @param {Number} [max] - topic alias maximum entries
7
+ */
8
+ function TopicAliasRecv (max) {
9
+ if (!(this instanceof TopicAliasRecv)) {
10
+ return new TopicAliasRecv(max)
11
+ }
12
+ this.aliasToTopic = {}
13
+ this.max = max
14
+ }
15
+
16
+ /**
17
+ * Insert or update topic - alias entry.
18
+ * @param {String} [topic] - topic
19
+ * @param {Number} [alias] - topic alias
20
+ * @returns {Boolean} - if success return true otherwise false
21
+ */
22
+ TopicAliasRecv.prototype.put = function (topic, alias) {
23
+ if (alias === 0 || alias > this.max) {
24
+ return false
25
+ }
26
+ this.aliasToTopic[alias] = topic
27
+ this.length = Object.keys(this.aliasToTopic).length
28
+ return true
29
+ }
30
+
31
+ /**
32
+ * Get topic by alias
33
+ * @param {String} [topic] - topic
34
+ * @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
35
+ */
36
+ TopicAliasRecv.prototype.getTopicByAlias = function (alias) {
37
+ return this.aliasToTopic[alias]
38
+ }
39
+
40
+ /**
41
+ * Clear all entries
42
+ */
43
+ TopicAliasRecv.prototype.clear = function () {
44
+ this.aliasToTopic = {}
45
+ }
46
+
47
+ module.exports = TopicAliasRecv