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.
@@ -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
+ const Transform = require('readable-stream').Transform
4
+ const duplexify = require('duplexify')
5
+
6
+ /* global FileReader */
7
+ let my
8
+ let proxy
9
+ let stream
10
+ let isInitialized = false
11
+
12
+ function buildProxy () {
13
+ const 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
+ const protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
51
+ let 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
+ const buffer = Buffer.from(res.data, 'base64')
75
+ proxy.push(buffer)
76
+ } else {
77
+ const reader = new FileReader()
78
+ reader.addEventListener('load', function () {
79
+ let 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
+ const websocketSubProtocol =
107
+ (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
108
+ ? 'mqttv3.1'
109
+ : 'mqtt'
110
+
111
+ setDefaultOpts(opts)
112
+
113
+ const 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,164 +1,165 @@
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
+ 'use strict'
2
+
3
+ const MqttClient = require('../client')
4
+ const Store = require('../store')
5
+ const url = require('url')
6
+ const xtend = require('xtend')
7
+ const debug = require('debug')('mqttjs')
8
+
9
+ const 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
+ let 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
+ // eslint-disable-next-line
64
+ const parsed = url.parse(brokerUrl, true)
65
+ if (parsed.port != null) {
66
+ parsed.port = Number(parsed.port)
67
+ }
68
+
69
+ opts = xtend(parsed, opts)
70
+
71
+ if (opts.protocol === null) {
72
+ throw new Error('Missing protocol')
73
+ }
74
+
75
+ opts.protocol = opts.protocol.replace(/:$/, '')
76
+ }
77
+
78
+ // merge in the auth options if supplied
79
+ parseAuthOptions(opts)
80
+
81
+ // support clientId passed in the query string of the url
82
+ if (opts.query && typeof opts.query.clientId === 'string') {
83
+ opts.clientId = opts.query.clientId
84
+ }
85
+
86
+ if (opts.cert && opts.key) {
87
+ if (opts.protocol) {
88
+ if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
89
+ switch (opts.protocol) {
90
+ case 'mqtt':
91
+ opts.protocol = 'mqtts'
92
+ break
93
+ case 'ws':
94
+ opts.protocol = 'wss'
95
+ break
96
+ case 'wx':
97
+ opts.protocol = 'wxs'
98
+ break
99
+ case 'ali':
100
+ opts.protocol = 'alis'
101
+ break
102
+ default:
103
+ throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
104
+ }
105
+ }
106
+ } else {
107
+ // A cert and key was provided, however no protocol was specified, so we will throw an error.
108
+ throw new Error('Missing secure protocol key')
109
+ }
110
+ }
111
+
112
+ if (!protocols[opts.protocol]) {
113
+ const isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
114
+ opts.protocol = [
115
+ 'mqtt',
116
+ 'mqtts',
117
+ 'ws',
118
+ 'wss',
119
+ 'wx',
120
+ 'wxs',
121
+ 'ali',
122
+ 'alis'
123
+ ].filter(function (key, index) {
124
+ if (isSecure && index % 2 === 0) {
125
+ // Skip insecure protocols when requesting a secure one.
126
+ return false
127
+ }
128
+ return (typeof protocols[key] === 'function')
129
+ })[0]
130
+ }
131
+
132
+ if (opts.clean === false && !opts.clientId) {
133
+ throw new Error('Missing clientId for unclean clients')
134
+ }
135
+
136
+ if (opts.protocol) {
137
+ opts.defaultProtocol = opts.protocol
138
+ }
139
+
140
+ function wrapper (client) {
141
+ if (opts.servers) {
142
+ if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
143
+ client._reconnectCount = 0
144
+ }
145
+
146
+ opts.host = opts.servers[client._reconnectCount].host
147
+ opts.port = opts.servers[client._reconnectCount].port
148
+ opts.protocol = (!opts.servers[client._reconnectCount].protocol ? opts.defaultProtocol : opts.servers[client._reconnectCount].protocol)
149
+ opts.hostname = opts.host
150
+
151
+ client._reconnectCount++
152
+ }
153
+
154
+ debug('calling streambuilder for', opts.protocol)
155
+ return protocols[opts.protocol](client, opts)
156
+ }
157
+ const client = new MqttClient(wrapper, opts)
158
+ client.on('error', function () { /* Automatically set up client error handling */ })
159
+ return client
160
+ }
161
+
162
+ module.exports = connect
163
+ module.exports.connect = connect
164
+ module.exports.MqttClient = MqttClient
165
+ module.exports.Store = Store
@@ -1,21 +1,20 @@
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
+ const net = require('net')
3
+ const 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
+ opts.port = opts.port || 1883
11
+ opts.hostname = opts.hostname || opts.host || 'localhost'
12
+
13
+ const port = opts.port
14
+ const host = opts.hostname
15
+
16
+ debug('port %d and host %s', port, host)
17
+ return net.createConnection(port, host)
18
+ }
19
+
20
+ module.exports = streamBuilder