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.
@@ -1,128 +1,128 @@
1
- 'use strict'
2
-
3
- var Transform = require('readable-stream').Transform
4
- var duplexify = require('duplexify')
5
-
6
- /* global FileReader */
7
- var my
8
- var proxy
9
- var stream
10
- var isInitialized = false
11
-
12
- function buildProxy () {
13
- var proxy = new Transform()
14
- proxy._write = function (chunk, encoding, next) {
15
- my.sendSocketMessage({
16
- data: chunk.buffer,
17
- success: function () {
18
- next()
19
- },
20
- fail: function () {
21
- next(new Error())
22
- }
23
- })
24
- }
25
- proxy._flush = function socketEnd (done) {
26
- my.closeSocket({
27
- success: function () {
28
- done()
29
- }
30
- })
31
- }
32
-
33
- return proxy
34
- }
35
-
36
- function setDefaultOpts (opts) {
37
- if (!opts.hostname) {
38
- opts.hostname = 'localhost'
39
- }
40
- if (!opts.path) {
41
- opts.path = '/'
42
- }
43
-
44
- if (!opts.wsOptions) {
45
- opts.wsOptions = {}
46
- }
47
- }
48
-
49
- function buildUrl (opts, client) {
50
- var protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
51
- var url = protocol + '://' + opts.hostname + opts.path
52
- if (opts.port && opts.port !== 80 && opts.port !== 443) {
53
- url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
54
- }
55
- if (typeof (opts.transformWsUrl) === 'function') {
56
- url = opts.transformWsUrl(url, opts, client)
57
- }
58
- return url
59
- }
60
-
61
- function bindEventHandler () {
62
- if (isInitialized) return
63
-
64
- isInitialized = true
65
-
66
- my.onSocketOpen(function () {
67
- stream.setReadable(proxy)
68
- stream.setWritable(proxy)
69
- stream.emit('connect')
70
- })
71
-
72
- my.onSocketMessage(function (res) {
73
- if (typeof res.data === 'string') {
74
- var buffer = Buffer.from(res.data, 'base64')
75
- proxy.push(buffer)
76
- } else {
77
- var reader = new FileReader()
78
- reader.addEventListener('load', function () {
79
- var data = reader.result
80
-
81
- if (data instanceof ArrayBuffer) data = Buffer.from(data)
82
- else data = Buffer.from(data, 'utf8')
83
- proxy.push(data)
84
- })
85
- reader.readAsArrayBuffer(res.data)
86
- }
87
- })
88
-
89
- my.onSocketClose(function () {
90
- stream.end()
91
- stream.destroy()
92
- })
93
-
94
- my.onSocketError(function (res) {
95
- stream.destroy(res)
96
- })
97
- }
98
-
99
- function buildStream (client, opts) {
100
- opts.hostname = opts.hostname || opts.host
101
-
102
- if (!opts.hostname) {
103
- throw new Error('Could not determine host. Specify host manually.')
104
- }
105
-
106
- var websocketSubProtocol =
107
- (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
108
- ? 'mqttv3.1'
109
- : 'mqtt'
110
-
111
- setDefaultOpts(opts)
112
-
113
- var url = buildUrl(opts, client)
114
- my = opts.my
115
- my.connectSocket({
116
- url: url,
117
- protocols: websocketSubProtocol
118
- })
119
-
120
- proxy = buildProxy()
121
- stream = duplexify.obj()
122
-
123
- bindEventHandler()
124
-
125
- return stream
126
- }
127
-
128
- module.exports = buildStream
1
+ 'use strict'
2
+
3
+ var Transform = require('readable-stream').Transform
4
+ var duplexify = require('duplexify')
5
+
6
+ /* global FileReader */
7
+ var my
8
+ var proxy
9
+ var stream
10
+ var isInitialized = false
11
+
12
+ function buildProxy () {
13
+ var proxy = new Transform()
14
+ proxy._write = function (chunk, encoding, next) {
15
+ my.sendSocketMessage({
16
+ data: chunk.buffer,
17
+ success: function () {
18
+ next()
19
+ },
20
+ fail: function () {
21
+ next(new Error())
22
+ }
23
+ })
24
+ }
25
+ proxy._flush = function socketEnd (done) {
26
+ my.closeSocket({
27
+ success: function () {
28
+ done()
29
+ }
30
+ })
31
+ }
32
+
33
+ return proxy
34
+ }
35
+
36
+ function setDefaultOpts (opts) {
37
+ if (!opts.hostname) {
38
+ opts.hostname = 'localhost'
39
+ }
40
+ if (!opts.path) {
41
+ opts.path = '/'
42
+ }
43
+
44
+ if (!opts.wsOptions) {
45
+ opts.wsOptions = {}
46
+ }
47
+ }
48
+
49
+ function buildUrl (opts, client) {
50
+ var protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
51
+ var url = protocol + '://' + opts.hostname + opts.path
52
+ if (opts.port && opts.port !== 80 && opts.port !== 443) {
53
+ url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
54
+ }
55
+ if (typeof (opts.transformWsUrl) === 'function') {
56
+ url = opts.transformWsUrl(url, opts, client)
57
+ }
58
+ return url
59
+ }
60
+
61
+ function bindEventHandler () {
62
+ if (isInitialized) return
63
+
64
+ isInitialized = true
65
+
66
+ my.onSocketOpen(function () {
67
+ stream.setReadable(proxy)
68
+ stream.setWritable(proxy)
69
+ stream.emit('connect')
70
+ })
71
+
72
+ my.onSocketMessage(function (res) {
73
+ if (typeof res.data === 'string') {
74
+ var buffer = Buffer.from(res.data, 'base64')
75
+ proxy.push(buffer)
76
+ } else {
77
+ var reader = new FileReader()
78
+ reader.addEventListener('load', function () {
79
+ var data = reader.result
80
+
81
+ if (data instanceof ArrayBuffer) data = Buffer.from(data)
82
+ else data = Buffer.from(data, 'utf8')
83
+ proxy.push(data)
84
+ })
85
+ reader.readAsArrayBuffer(res.data)
86
+ }
87
+ })
88
+
89
+ my.onSocketClose(function () {
90
+ stream.end()
91
+ stream.destroy()
92
+ })
93
+
94
+ my.onSocketError(function (res) {
95
+ stream.destroy(res)
96
+ })
97
+ }
98
+
99
+ function buildStream (client, opts) {
100
+ opts.hostname = opts.hostname || opts.host
101
+
102
+ if (!opts.hostname) {
103
+ throw new Error('Could not determine host. Specify host manually.')
104
+ }
105
+
106
+ var websocketSubProtocol =
107
+ (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
108
+ ? 'mqttv3.1'
109
+ : 'mqtt'
110
+
111
+ setDefaultOpts(opts)
112
+
113
+ var url = buildUrl(opts, client)
114
+ my = opts.my
115
+ my.connectSocket({
116
+ url: url,
117
+ protocols: websocketSubProtocol
118
+ })
119
+
120
+ proxy = buildProxy()
121
+ stream = duplexify.obj()
122
+
123
+ bindEventHandler()
124
+
125
+ return stream
126
+ }
127
+
128
+ module.exports = buildStream
@@ -1,177 +1,164 @@
1
- 'use strict'
2
-
3
- var MqttClient = require('../client')
4
- var Store = require('../store')
5
- var debug = require('debug')('mqttjs')
6
-
7
- var protocols = {}
8
-
9
- // eslint-disable-next-line camelcase
10
- if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ !== 'function') {
11
- protocols.mqtt = require('./tcp')
12
- protocols.tcp = require('./tcp')
13
- protocols.ssl = require('./tls')
14
- protocols.tls = require('./tls')
15
- protocols.mqtts = require('./tls')
16
- } else {
17
- protocols.wx = require('./wx')
18
- protocols.wxs = require('./wx')
19
-
20
- protocols.ali = require('./ali')
21
- protocols.alis = require('./ali')
22
- }
23
-
24
- protocols.ws = require('./ws')
25
- protocols.wss = require('./ws')
26
-
27
- /**
28
- * Parse the auth attribute and merge username and password in the options object.
29
- *
30
- * @param {Object} [opts] option object
31
- */
32
- function parseAuthOptions (opts) {
33
- var matches
34
- if (opts.auth) {
35
- matches = opts.auth.match(/^(.+):(.+)$/)
36
- if (matches) {
37
- opts.username = matches[1]
38
- opts.password = matches[2]
39
- } else {
40
- opts.username = opts.auth
41
- }
42
- }
43
- }
44
-
45
- /**
46
- * connect - connect to an MQTT broker.
47
- *
48
- * @param {String} [brokerUrl] - url of the broker, optional
49
- * @param {Object} opts - see MqttClient#constructor
50
- */
51
- function connect (brokerUrl, opts) {
52
- debug('connecting to an MQTT broker...')
53
- if ((typeof brokerUrl === 'object') && !opts) {
54
- opts = brokerUrl
55
- brokerUrl = null
56
- }
57
-
58
- opts = opts || {}
59
-
60
- if (brokerUrl) {
61
- if (opts.protocol === null) {
62
- throw new Error('Missing protocol')
63
- }
64
- var parsed = new URL(brokerUrl)
65
-
66
- // the URL object is a bit special, so copy individual
67
- // items to the opts object
68
- opts.hash = parsed.hash
69
- opts.host = parsed.host
70
- opts.hostname = parsed.hostname
71
- opts.href = parsed.href
72
- opts.origin = parsed.origin
73
- opts.pathname = parsed.pathname
74
- opts.port = Number(parsed.port) || null
75
- opts.protocol = parsed.protocol
76
- opts.username = opts.username || parsed.username
77
- opts.password = opts.password || parsed.password
78
- opts.search = parsed.search
79
- opts.searchParams = parsed.searchParams
80
- opts.path = parsed.pathname + parsed.search
81
- opts.protocol = opts.protocol.replace(/:$/, '')
82
- }
83
-
84
- // merge in the auth options if supplied
85
- // legacy support for url.parse objects (now deprecated in node.js)
86
- parseAuthOptions(opts)
87
-
88
- // support clientId passed in the query string of the url
89
- if (opts.searchParams && typeof opts.searchParams.get('clientId') === 'string') {
90
- opts.clientId = opts.searchParams.get('clientId')
91
- }
92
-
93
- // legacy support for url.parse objects (now deprecated in node.js)
94
- if (opts.query && typeof opts.query.clientId === 'string') {
95
- opts.clientId = opts.query.clientId
96
- }
97
-
98
- if (opts.cert && opts.key) {
99
- if (opts.protocol) {
100
- if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
101
- switch (opts.protocol) {
102
- case 'mqtt':
103
- opts.protocol = 'mqtts'
104
- break
105
- case 'ws':
106
- opts.protocol = 'wss'
107
- break
108
- case 'wx':
109
- opts.protocol = 'wxs'
110
- break
111
- case 'ali':
112
- opts.protocol = 'alis'
113
- break
114
- default:
115
- throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
116
- }
117
- }
118
- } else {
119
- // A cert and key was provided, however no protocol was specified, so we will throw an error.
120
- throw new Error('Missing secure protocol key')
121
- }
122
- }
123
-
124
- if (!protocols[opts.protocol]) {
125
- var isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
126
- opts.protocol = [
127
- 'mqtt',
128
- 'mqtts',
129
- 'ws',
130
- 'wss',
131
- 'wx',
132
- 'wxs',
133
- 'ali',
134
- 'alis'
135
- ].filter(function (key, index) {
136
- if (isSecure && index % 2 === 0) {
137
- // Skip insecure protocols when requesting a secure one.
138
- return false
139
- }
140
- return (typeof protocols[key] === 'function')
141
- })[0]
142
- }
143
-
144
- if (opts.clean === false && !opts.clientId) {
145
- throw new Error('Missing clientId for unclean clients')
146
- }
147
-
148
- if (opts.protocol) {
149
- opts.defaultProtocol = opts.protocol
150
- }
151
-
152
- function wrapper (client) {
153
- if (opts.servers) {
154
- if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
155
- client._reconnectCount = 0
156
- }
157
-
158
- opts.host = opts.servers[client._reconnectCount].host
159
- opts.port = opts.servers[client._reconnectCount].port
160
- opts.protocol = (!opts.servers[client._reconnectCount].protocol ? opts.defaultProtocol : opts.servers[client._reconnectCount].protocol)
161
- opts.hostname = opts.host
162
-
163
- client._reconnectCount++
164
- }
165
-
166
- debug('calling streambuilder for', opts.protocol)
167
- return protocols[opts.protocol](client, opts)
168
- }
169
- var client = new MqttClient(wrapper, opts)
170
- client.on('error', function () { /* Automatically set up client error handling */ })
171
- return client
172
- }
173
-
174
- module.exports = connect
175
- module.exports.connect = connect
176
- module.exports.MqttClient = MqttClient
177
- module.exports.Store = Store
1
+ 'use strict'
2
+
3
+ var MqttClient = require('../client')
4
+ var Store = require('../store')
5
+ var url = require('url')
6
+ var xtend = require('xtend')
7
+ var debug = require('debug')('mqttjs')
8
+
9
+ var protocols = {}
10
+
11
+ // eslint-disable-next-line camelcase
12
+ if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ !== 'function') {
13
+ protocols.mqtt = require('./tcp')
14
+ protocols.tcp = require('./tcp')
15
+ protocols.ssl = require('./tls')
16
+ protocols.tls = require('./tls')
17
+ protocols.mqtts = require('./tls')
18
+ } else {
19
+ protocols.wx = require('./wx')
20
+ protocols.wxs = require('./wx')
21
+
22
+ protocols.ali = require('./ali')
23
+ protocols.alis = require('./ali')
24
+ }
25
+
26
+ protocols.ws = require('./ws')
27
+ protocols.wss = require('./ws')
28
+
29
+ /**
30
+ * Parse the auth attribute and merge username and password in the options object.
31
+ *
32
+ * @param {Object} [opts] option object
33
+ */
34
+ function parseAuthOptions (opts) {
35
+ var matches
36
+ if (opts.auth) {
37
+ matches = opts.auth.match(/^(.+):(.+)$/)
38
+ if (matches) {
39
+ opts.username = matches[1]
40
+ opts.password = matches[2]
41
+ } else {
42
+ opts.username = opts.auth
43
+ }
44
+ }
45
+ }
46
+
47
+ /**
48
+ * connect - connect to an MQTT broker.
49
+ *
50
+ * @param {String} [brokerUrl] - url of the broker, optional
51
+ * @param {Object} opts - see MqttClient#constructor
52
+ */
53
+ function connect (brokerUrl, opts) {
54
+ debug('connecting to an MQTT broker...')
55
+ if ((typeof brokerUrl === 'object') && !opts) {
56
+ opts = brokerUrl
57
+ brokerUrl = null
58
+ }
59
+
60
+ opts = opts || {}
61
+
62
+ if (brokerUrl) {
63
+ var parsed = url.parse(brokerUrl, true)
64
+ if (parsed.port != null) {
65
+ parsed.port = Number(parsed.port)
66
+ }
67
+
68
+ opts = xtend(parsed, opts)
69
+
70
+ if (opts.protocol === null) {
71
+ throw new Error('Missing protocol')
72
+ }
73
+
74
+ opts.protocol = opts.protocol.replace(/:$/, '')
75
+ }
76
+
77
+ // merge in the auth options if supplied
78
+ parseAuthOptions(opts)
79
+
80
+ // support clientId passed in the query string of the url
81
+ if (opts.query && typeof opts.query.clientId === 'string') {
82
+ opts.clientId = opts.query.clientId
83
+ }
84
+
85
+ if (opts.cert && opts.key) {
86
+ if (opts.protocol) {
87
+ if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
88
+ switch (opts.protocol) {
89
+ case 'mqtt':
90
+ opts.protocol = 'mqtts'
91
+ break
92
+ case 'ws':
93
+ opts.protocol = 'wss'
94
+ break
95
+ case 'wx':
96
+ opts.protocol = 'wxs'
97
+ break
98
+ case 'ali':
99
+ opts.protocol = 'alis'
100
+ break
101
+ default:
102
+ throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
103
+ }
104
+ }
105
+ } else {
106
+ // A cert and key was provided, however no protocol was specified, so we will throw an error.
107
+ throw new Error('Missing secure protocol key')
108
+ }
109
+ }
110
+
111
+ if (!protocols[opts.protocol]) {
112
+ var isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
113
+ opts.protocol = [
114
+ 'mqtt',
115
+ 'mqtts',
116
+ 'ws',
117
+ 'wss',
118
+ 'wx',
119
+ 'wxs',
120
+ 'ali',
121
+ 'alis'
122
+ ].filter(function (key, index) {
123
+ if (isSecure && index % 2 === 0) {
124
+ // Skip insecure protocols when requesting a secure one.
125
+ return false
126
+ }
127
+ return (typeof protocols[key] === 'function')
128
+ })[0]
129
+ }
130
+
131
+ if (opts.clean === false && !opts.clientId) {
132
+ throw new Error('Missing clientId for unclean clients')
133
+ }
134
+
135
+ if (opts.protocol) {
136
+ opts.defaultProtocol = opts.protocol
137
+ }
138
+
139
+ function wrapper (client) {
140
+ if (opts.servers) {
141
+ if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
142
+ client._reconnectCount = 0
143
+ }
144
+
145
+ opts.host = opts.servers[client._reconnectCount].host
146
+ opts.port = opts.servers[client._reconnectCount].port
147
+ opts.protocol = (!opts.servers[client._reconnectCount].protocol ? opts.defaultProtocol : opts.servers[client._reconnectCount].protocol)
148
+ opts.hostname = opts.host
149
+
150
+ client._reconnectCount++
151
+ }
152
+
153
+ debug('calling streambuilder for', opts.protocol)
154
+ return protocols[opts.protocol](client, opts)
155
+ }
156
+ var client = new MqttClient(wrapper, opts)
157
+ client.on('error', function () { /* Automatically set up client error handling */ })
158
+ return client
159
+ }
160
+
161
+ module.exports = connect
162
+ module.exports.connect = connect
163
+ module.exports.MqttClient = MqttClient
164
+ module.exports.Store = Store
@@ -1,21 +1,21 @@
1
- 'use strict'
2
- var net = require('net')
3
- var debug = require('debug')('mqttjs:tcp')
4
-
5
- /*
6
- variables port and host can be removed since
7
- you have all required information in opts object
8
- */
9
- function streamBuilder (client, opts) {
10
- var port, host
11
- opts.port = opts.port || 1883
12
- opts.hostname = opts.hostname || opts.host || 'localhost'
13
-
14
- port = opts.port
15
- host = opts.hostname
16
-
17
- debug('port %d and host %s', port, host)
18
- return net.createConnection(port, host)
19
- }
20
-
21
- module.exports = streamBuilder
1
+ 'use strict'
2
+ var net = require('net')
3
+ var debug = require('debug')('mqttjs:tcp')
4
+
5
+ /*
6
+ variables port and host can be removed since
7
+ you have all required information in opts object
8
+ */
9
+ function streamBuilder (client, opts) {
10
+ var port, host
11
+ opts.port = opts.port || 1883
12
+ opts.hostname = opts.hostname || opts.host || 'localhost'
13
+
14
+ port = opts.port
15
+ host = opts.hostname
16
+
17
+ debug('port %d and host %s', port, host)
18
+ return net.createConnection(port, host)
19
+ }
20
+
21
+ module.exports = streamBuilder