mqtt 2.18.9 → 3.0.0
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/CONTRIBUTING.md +27 -27
- package/LICENSE.md +15 -15
- package/README.md +689 -612
- package/bin/pub.js +146 -146
- package/bin/sub.js +123 -123
- package/dist/mqtt.js +11916 -14116
- package/dist/mqtt.min.js +1 -1
- package/doc/help.txt +8 -8
- package/doc/publish.txt +26 -26
- package/doc/subscribe.txt +26 -26
- package/examples/client/secure-client.js +24 -24
- package/examples/client/simple-both.js +13 -13
- package/examples/client/simple-publish.js +7 -7
- package/examples/client/simple-subscribe.js +9 -9
- package/examples/tls client/crt.ca.cg.pem +34 -34
- package/examples/tls client/mqttclient.js +48 -48
- package/examples/tls client/tls-cert.pem +13 -13
- package/examples/tls client/tls-key.pem +15 -15
- package/examples/wss/client.js +49 -49
- package/examples/wss/client_with_proxy.js +58 -58
- package/lib/client.js +1460 -1129
- package/lib/connect/ali.js +130 -0
- package/lib/connect/index.js +157 -149
- package/lib/connect/tcp.js +19 -19
- package/lib/connect/tls.js +41 -41
- package/lib/connect/ws.js +92 -92
- package/lib/connect/wx.js +134 -110
- package/lib/store.js +140 -140
- package/lib/validations.js +52 -52
- package/mqtt.js +41 -41
- package/package.json +112 -107
- package/test/abstract_client.js +3084 -2667
- package/test/abstract_store.js +135 -135
- package/test/browser/server.js +132 -132
- package/test/browser/test.js +92 -92
- package/test/client.js +1129 -544
- package/test/helpers/private-csr.pem +16 -16
- package/test/helpers/private-key.pem +27 -27
- package/test/helpers/public-cert.pem +19 -19
- package/test/helpers/public-key.pem +9 -9
- package/test/helpers/server.js +52 -52
- package/test/helpers/server_process.js +9 -9
- package/test/helpers/tls-cert.pem +14 -14
- package/test/helpers/tls-csr.pem +11 -11
- package/test/helpers/tls-key.pem +15 -15
- package/test/helpers/wrong-cert.pem +13 -13
- package/test/helpers/wrong-csr.pem +11 -11
- package/test/helpers/wrong-key.pem +15 -15
- package/test/mocha.opts +4 -4
- package/test/mqtt.js +230 -230
- package/test/mqtt_store.js +9 -9
- package/test/secure_client.js +157 -157
- package/test/server.js +93 -65
- package/test/store.js +10 -10
- package/test/typescript/broker-connect-subscribe-and-publish.js +24 -0
- package/test/typescript/broker-connect-subscribe-and-publish.js.map +1 -0
- package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
- package/test/typescript/tsconfig.json +14 -14
- package/test/util.js +13 -13
- package/test/websocket_client.js +144 -144
- package/types/index.d.ts +27 -27
- package/types/lib/client-options.d.ts +178 -137
- package/types/lib/client.d.ts +231 -196
- package/types/lib/connect/index.d.ts +10 -10
- package/types/lib/store-options.d.ts +6 -6
- package/types/lib/store.d.ts +46 -46
- package/test/browser/wx.js +0 -93
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var Transform = require('readable-stream').Transform
|
|
4
|
+
var duplexify = require('duplexify')
|
|
5
|
+
var base64 = require('base64-js')
|
|
6
|
+
|
|
7
|
+
/* global FileReader */
|
|
8
|
+
var my
|
|
9
|
+
var proxy
|
|
10
|
+
var stream
|
|
11
|
+
var isInitialized = false
|
|
12
|
+
|
|
13
|
+
function buildProxy () {
|
|
14
|
+
var proxy = new Transform()
|
|
15
|
+
proxy._write = function (chunk, encoding, next) {
|
|
16
|
+
my.sendSocketMessage({
|
|
17
|
+
data: chunk.buffer,
|
|
18
|
+
success: function () {
|
|
19
|
+
next()
|
|
20
|
+
},
|
|
21
|
+
fail: function () {
|
|
22
|
+
next(new Error())
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
proxy._flush = function socketEnd (done) {
|
|
27
|
+
my.closeSocket({
|
|
28
|
+
success: function () {
|
|
29
|
+
done()
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return proxy
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function setDefaultOpts (opts) {
|
|
38
|
+
if (!opts.hostname) {
|
|
39
|
+
opts.hostname = 'localhost'
|
|
40
|
+
}
|
|
41
|
+
if (!opts.path) {
|
|
42
|
+
opts.path = '/'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!opts.wsOptions) {
|
|
46
|
+
opts.wsOptions = {}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function buildUrl (opts, client) {
|
|
51
|
+
var protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
|
|
52
|
+
var url = protocol + '://' + opts.hostname + opts.path
|
|
53
|
+
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
54
|
+
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
55
|
+
}
|
|
56
|
+
if (typeof (opts.transformWsUrl) === 'function') {
|
|
57
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
58
|
+
}
|
|
59
|
+
return url
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function bindEventHandler () {
|
|
63
|
+
if (isInitialized) return
|
|
64
|
+
|
|
65
|
+
isInitialized = true
|
|
66
|
+
|
|
67
|
+
my.onSocketOpen(function () {
|
|
68
|
+
stream.setReadable(proxy)
|
|
69
|
+
stream.setWritable(proxy)
|
|
70
|
+
stream.emit('connect')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
my.onSocketMessage(function (res) {
|
|
74
|
+
if (typeof res.data === 'string') {
|
|
75
|
+
var array = base64.toByteArray(res.data)
|
|
76
|
+
var buffer = Buffer.from(array)
|
|
77
|
+
proxy.push(buffer)
|
|
78
|
+
} else {
|
|
79
|
+
var reader = new FileReader()
|
|
80
|
+
reader.addEventListener('load', function () {
|
|
81
|
+
var data = reader.result
|
|
82
|
+
|
|
83
|
+
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
84
|
+
else data = Buffer.from(data, 'utf8')
|
|
85
|
+
proxy.push(data)
|
|
86
|
+
})
|
|
87
|
+
reader.readAsArrayBuffer(res.data)
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
my.onSocketClose(function () {
|
|
92
|
+
stream.end()
|
|
93
|
+
stream.destroy()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
my.onSocketError(function (res) {
|
|
97
|
+
stream.destroy(res)
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function buildStream (client, opts) {
|
|
102
|
+
opts.hostname = opts.hostname || opts.host
|
|
103
|
+
|
|
104
|
+
if (!opts.hostname) {
|
|
105
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
var websocketSubProtocol =
|
|
109
|
+
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
110
|
+
? 'mqttv3.1'
|
|
111
|
+
: 'mqtt'
|
|
112
|
+
|
|
113
|
+
setDefaultOpts(opts)
|
|
114
|
+
|
|
115
|
+
var url = buildUrl(opts, client)
|
|
116
|
+
my = opts.my
|
|
117
|
+
my.connectSocket({
|
|
118
|
+
url: url,
|
|
119
|
+
protocols: websocketSubProtocol
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
proxy = buildProxy()
|
|
123
|
+
stream = duplexify.obj()
|
|
124
|
+
|
|
125
|
+
bindEventHandler()
|
|
126
|
+
|
|
127
|
+
return stream
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = buildStream
|
package/lib/connect/index.js
CHANGED
|
@@ -1,149 +1,157 @@
|
|
|
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 protocols = {}
|
|
8
|
-
|
|
9
|
-
if (process.title !== 'browser') {
|
|
10
|
-
protocols.mqtt = require('./tcp')
|
|
11
|
-
protocols.tcp = require('./tcp')
|
|
12
|
-
protocols.ssl = require('./tls')
|
|
13
|
-
protocols.tls = require('./tls')
|
|
14
|
-
protocols.mqtts = require('./tls')
|
|
15
|
-
} else {
|
|
16
|
-
protocols.wx = require('./wx')
|
|
17
|
-
protocols.wxs = require('./wx')
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
protocols.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
opts.username =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
opts.protocol
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
//
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
case '
|
|
85
|
-
opts.protocol = '
|
|
86
|
-
break
|
|
87
|
-
case '
|
|
88
|
-
opts.protocol = '
|
|
89
|
-
break
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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 protocols = {}
|
|
8
|
+
|
|
9
|
+
if (process.title !== 'browser') {
|
|
10
|
+
protocols.mqtt = require('./tcp')
|
|
11
|
+
protocols.tcp = require('./tcp')
|
|
12
|
+
protocols.ssl = require('./tls')
|
|
13
|
+
protocols.tls = require('./tls')
|
|
14
|
+
protocols.mqtts = require('./tls')
|
|
15
|
+
} else {
|
|
16
|
+
protocols.wx = require('./wx')
|
|
17
|
+
protocols.wxs = require('./wx')
|
|
18
|
+
|
|
19
|
+
protocols.ali = require('./ali')
|
|
20
|
+
protocols.alis = require('./ali')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protocols.ws = require('./ws')
|
|
24
|
+
protocols.wss = require('./ws')
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Parse the auth attribute and merge username and password in the options object.
|
|
28
|
+
*
|
|
29
|
+
* @param {Object} [opts] option object
|
|
30
|
+
*/
|
|
31
|
+
function parseAuthOptions (opts) {
|
|
32
|
+
var matches
|
|
33
|
+
if (opts.auth) {
|
|
34
|
+
matches = opts.auth.match(/^(.+):(.+)$/)
|
|
35
|
+
if (matches) {
|
|
36
|
+
opts.username = matches[1]
|
|
37
|
+
opts.password = matches[2]
|
|
38
|
+
} else {
|
|
39
|
+
opts.username = opts.auth
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* connect - connect to an MQTT broker.
|
|
46
|
+
*
|
|
47
|
+
* @param {String} [brokerUrl] - url of the broker, optional
|
|
48
|
+
* @param {Object} opts - see MqttClient#constructor
|
|
49
|
+
*/
|
|
50
|
+
function connect (brokerUrl, opts) {
|
|
51
|
+
if ((typeof brokerUrl === 'object') && !opts) {
|
|
52
|
+
opts = brokerUrl
|
|
53
|
+
brokerUrl = null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
opts = opts || {}
|
|
57
|
+
|
|
58
|
+
if (brokerUrl) {
|
|
59
|
+
var parsed = url.parse(brokerUrl, true)
|
|
60
|
+
if (parsed.port != null) {
|
|
61
|
+
parsed.port = Number(parsed.port)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
opts = xtend(parsed, opts)
|
|
65
|
+
|
|
66
|
+
if (opts.protocol === null) {
|
|
67
|
+
throw new Error('Missing protocol')
|
|
68
|
+
}
|
|
69
|
+
opts.protocol = opts.protocol.replace(/:$/, '')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// merge in the auth options if supplied
|
|
73
|
+
parseAuthOptions(opts)
|
|
74
|
+
|
|
75
|
+
// support clientId passed in the query string of the url
|
|
76
|
+
if (opts.query && typeof opts.query.clientId === 'string') {
|
|
77
|
+
opts.clientId = opts.query.clientId
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (opts.cert && opts.key) {
|
|
81
|
+
if (opts.protocol) {
|
|
82
|
+
if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
|
|
83
|
+
switch (opts.protocol) {
|
|
84
|
+
case 'mqtt':
|
|
85
|
+
opts.protocol = 'mqtts'
|
|
86
|
+
break
|
|
87
|
+
case 'ws':
|
|
88
|
+
opts.protocol = 'wss'
|
|
89
|
+
break
|
|
90
|
+
case 'wx':
|
|
91
|
+
opts.protocol = 'wxs'
|
|
92
|
+
break
|
|
93
|
+
case 'ali':
|
|
94
|
+
opts.protocol = 'alis'
|
|
95
|
+
break
|
|
96
|
+
default:
|
|
97
|
+
throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
// don't know what protocol he want to use, mqtts or wss
|
|
102
|
+
throw new Error('Missing secure protocol key')
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!protocols[opts.protocol]) {
|
|
107
|
+
var isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
|
108
|
+
opts.protocol = [
|
|
109
|
+
'mqtt',
|
|
110
|
+
'mqtts',
|
|
111
|
+
'ws',
|
|
112
|
+
'wss',
|
|
113
|
+
'wx',
|
|
114
|
+
'wxs',
|
|
115
|
+
'ali',
|
|
116
|
+
'alis'
|
|
117
|
+
].filter(function (key, index) {
|
|
118
|
+
if (isSecure && index % 2 === 0) {
|
|
119
|
+
// Skip insecure protocols when requesting a secure one.
|
|
120
|
+
return false
|
|
121
|
+
}
|
|
122
|
+
return (typeof protocols[key] === 'function')
|
|
123
|
+
})[0]
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (opts.clean === false && !opts.clientId) {
|
|
127
|
+
throw new Error('Missing clientId for unclean clients')
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (opts.protocol) {
|
|
131
|
+
opts.defaultProtocol = opts.protocol
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function wrapper (client) {
|
|
135
|
+
if (opts.servers) {
|
|
136
|
+
if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
|
|
137
|
+
client._reconnectCount = 0
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
opts.host = opts.servers[client._reconnectCount].host
|
|
141
|
+
opts.port = opts.servers[client._reconnectCount].port
|
|
142
|
+
opts.protocol = (!opts.servers[client._reconnectCount].protocol ? opts.defaultProtocol : opts.servers[client._reconnectCount].protocol)
|
|
143
|
+
opts.hostname = opts.host
|
|
144
|
+
|
|
145
|
+
client._reconnectCount++
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return protocols[opts.protocol](client, opts)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return new MqttClient(wrapper, opts)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
module.exports = connect
|
|
155
|
+
module.exports.connect = connect
|
|
156
|
+
module.exports.MqttClient = MqttClient
|
|
157
|
+
module.exports.Store = Store
|
package/lib/connect/tcp.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
var net = require('net')
|
|
3
|
-
|
|
4
|
-
/*
|
|
5
|
-
variables port and host can be removed since
|
|
6
|
-
you have all required information in opts object
|
|
7
|
-
*/
|
|
8
|
-
function buildBuilder (client, opts) {
|
|
9
|
-
var port, host
|
|
10
|
-
opts.port = opts.port || 1883
|
|
11
|
-
opts.hostname = opts.hostname || opts.host || 'localhost'
|
|
12
|
-
|
|
13
|
-
port = opts.port
|
|
14
|
-
host = opts.hostname
|
|
15
|
-
|
|
16
|
-
return net.createConnection(port, host)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = buildBuilder
|
|
1
|
+
'use strict'
|
|
2
|
+
var net = require('net')
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
variables port and host can be removed since
|
|
6
|
+
you have all required information in opts object
|
|
7
|
+
*/
|
|
8
|
+
function buildBuilder (client, opts) {
|
|
9
|
+
var port, host
|
|
10
|
+
opts.port = opts.port || 1883
|
|
11
|
+
opts.hostname = opts.hostname || opts.host || 'localhost'
|
|
12
|
+
|
|
13
|
+
port = opts.port
|
|
14
|
+
host = opts.hostname
|
|
15
|
+
|
|
16
|
+
return net.createConnection(port, host)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = buildBuilder
|
package/lib/connect/tls.js
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
var tls = require('tls')
|
|
3
|
-
|
|
4
|
-
function buildBuilder (mqttClient, opts) {
|
|
5
|
-
var connection
|
|
6
|
-
opts.port = opts.port || 8883
|
|
7
|
-
opts.host = opts.hostname || opts.host || 'localhost'
|
|
8
|
-
|
|
9
|
-
opts.rejectUnauthorized = opts.rejectUnauthorized !== false
|
|
10
|
-
|
|
11
|
-
delete opts.path
|
|
12
|
-
|
|
13
|
-
connection = tls.connect(opts)
|
|
14
|
-
/* eslint no-use-before-define: [2, "nofunc"] */
|
|
15
|
-
connection.on('secureConnect', function () {
|
|
16
|
-
if (opts.rejectUnauthorized && !connection.authorized) {
|
|
17
|
-
connection.emit('error', new Error('TLS not authorized'))
|
|
18
|
-
} else {
|
|
19
|
-
connection.removeListener('error', handleTLSerrors)
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
function handleTLSerrors (err) {
|
|
24
|
-
// How can I get verify this error is a tls error?
|
|
25
|
-
if (opts.rejectUnauthorized) {
|
|
26
|
-
mqttClient.emit('error', err)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// close this connection to match the behaviour of net
|
|
30
|
-
// otherwise all we get is an error from the connection
|
|
31
|
-
// and close event doesn't fire. This is a work around
|
|
32
|
-
// to enable the reconnect code to work the same as with
|
|
33
|
-
// net.createConnection
|
|
34
|
-
connection.end()
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
connection.on('error', handleTLSerrors)
|
|
38
|
-
return connection
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
module.exports = buildBuilder
|
|
1
|
+
'use strict'
|
|
2
|
+
var tls = require('tls')
|
|
3
|
+
|
|
4
|
+
function buildBuilder (mqttClient, opts) {
|
|
5
|
+
var connection
|
|
6
|
+
opts.port = opts.port || 8883
|
|
7
|
+
opts.host = opts.hostname || opts.host || 'localhost'
|
|
8
|
+
|
|
9
|
+
opts.rejectUnauthorized = opts.rejectUnauthorized !== false
|
|
10
|
+
|
|
11
|
+
delete opts.path
|
|
12
|
+
|
|
13
|
+
connection = tls.connect(opts)
|
|
14
|
+
/* eslint no-use-before-define: [2, "nofunc"] */
|
|
15
|
+
connection.on('secureConnect', function () {
|
|
16
|
+
if (opts.rejectUnauthorized && !connection.authorized) {
|
|
17
|
+
connection.emit('error', new Error('TLS not authorized'))
|
|
18
|
+
} else {
|
|
19
|
+
connection.removeListener('error', handleTLSerrors)
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
function handleTLSerrors (err) {
|
|
24
|
+
// How can I get verify this error is a tls error?
|
|
25
|
+
if (opts.rejectUnauthorized) {
|
|
26
|
+
mqttClient.emit('error', err)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// close this connection to match the behaviour of net
|
|
30
|
+
// otherwise all we get is an error from the connection
|
|
31
|
+
// and close event doesn't fire. This is a work around
|
|
32
|
+
// to enable the reconnect code to work the same as with
|
|
33
|
+
// net.createConnection
|
|
34
|
+
connection.end()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
connection.on('error', handleTLSerrors)
|
|
38
|
+
return connection
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = buildBuilder
|