mqtt 2.18.8 → 2.18.9
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 +612 -612
- package/bin/pub.js +146 -146
- package/bin/sub.js +123 -123
- package/dist/mqtt.js +14248 -10729
- 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 +1129 -1129
- package/lib/connect/index.js +149 -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 +110 -110
- package/lib/store.js +140 -140
- package/lib/validations.js +52 -52
- package/mqtt.js +41 -41
- package/package.json +107 -111
- package/test/abstract_client.js +2667 -2667
- package/test/abstract_store.js +135 -135
- package/test/browser/server.js +132 -132
- package/test/browser/test.js +92 -92
- package/test/browser/wx.js +93 -93
- package/test/client.js +544 -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 +65 -65
- package/test/store.js +10 -10
- 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 +137 -137
- package/types/lib/client.d.ts +196 -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/typescript/broker-connect-subscribe-and-publish.js +0 -24
- package/test/typescript/broker-connect-subscribe-and-publish.js.map +0 -1
package/lib/connect/index.js
CHANGED
|
@@ -1,149 +1,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
|
-
|
|
20
|
-
protocols.ws = require('./ws')
|
|
21
|
-
protocols.wss = require('./ws')
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Parse the auth attribute and merge username and password in the options object.
|
|
25
|
-
*
|
|
26
|
-
* @param {Object} [opts] option object
|
|
27
|
-
*/
|
|
28
|
-
function parseAuthOptions (opts) {
|
|
29
|
-
var matches
|
|
30
|
-
if (opts.auth) {
|
|
31
|
-
matches = opts.auth.match(/^(.+):(.+)$/)
|
|
32
|
-
if (matches) {
|
|
33
|
-
opts.username = matches[1]
|
|
34
|
-
opts.password = matches[2]
|
|
35
|
-
} else {
|
|
36
|
-
opts.username = opts.auth
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* connect - connect to an MQTT broker.
|
|
43
|
-
*
|
|
44
|
-
* @param {String} [brokerUrl] - url of the broker, optional
|
|
45
|
-
* @param {Object} opts - see MqttClient#constructor
|
|
46
|
-
*/
|
|
47
|
-
function connect (brokerUrl, opts) {
|
|
48
|
-
if ((typeof brokerUrl === 'object') && !opts) {
|
|
49
|
-
opts = brokerUrl
|
|
50
|
-
brokerUrl = null
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
opts = opts || {}
|
|
54
|
-
|
|
55
|
-
if (brokerUrl) {
|
|
56
|
-
var parsed = url.parse(brokerUrl, true)
|
|
57
|
-
if (parsed.port != null) {
|
|
58
|
-
parsed.port = Number(parsed.port)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
opts = xtend(parsed, opts)
|
|
62
|
-
|
|
63
|
-
if (opts.protocol === null) {
|
|
64
|
-
throw new Error('Missing protocol')
|
|
65
|
-
}
|
|
66
|
-
opts.protocol = opts.protocol.replace(/:$/, '')
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// merge in the auth options if supplied
|
|
70
|
-
parseAuthOptions(opts)
|
|
71
|
-
|
|
72
|
-
// support clientId passed in the query string of the url
|
|
73
|
-
if (opts.query && typeof opts.query.clientId === 'string') {
|
|
74
|
-
opts.clientId = opts.query.clientId
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (opts.cert && opts.key) {
|
|
78
|
-
if (opts.protocol) {
|
|
79
|
-
if (['mqtts', 'wss', 'wxs'].indexOf(opts.protocol) === -1) {
|
|
80
|
-
switch (opts.protocol) {
|
|
81
|
-
case 'mqtt':
|
|
82
|
-
opts.protocol = 'mqtts'
|
|
83
|
-
break
|
|
84
|
-
case 'ws':
|
|
85
|
-
opts.protocol = 'wss'
|
|
86
|
-
break
|
|
87
|
-
case 'wx':
|
|
88
|
-
opts.protocol = 'wxs'
|
|
89
|
-
break
|
|
90
|
-
default:
|
|
91
|
-
throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
} else {
|
|
95
|
-
// don't know what protocol he want to use, mqtts or wss
|
|
96
|
-
throw new Error('Missing secure protocol key')
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (!protocols[opts.protocol]) {
|
|
101
|
-
var isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
|
102
|
-
opts.protocol = [
|
|
103
|
-
'mqtt',
|
|
104
|
-
'mqtts',
|
|
105
|
-
'ws',
|
|
106
|
-
'wss',
|
|
107
|
-
'wx',
|
|
108
|
-
'wxs'
|
|
109
|
-
].filter(function (key, index) {
|
|
110
|
-
if (isSecure && index % 2 === 0) {
|
|
111
|
-
// Skip insecure protocols when requesting a secure one.
|
|
112
|
-
return false
|
|
113
|
-
}
|
|
114
|
-
return (typeof protocols[key] === 'function')
|
|
115
|
-
})[0]
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (opts.clean === false && !opts.clientId) {
|
|
119
|
-
throw new Error('Missing clientId for unclean clients')
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (opts.protocol) {
|
|
123
|
-
opts.defaultProtocol = opts.protocol
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function wrapper (client) {
|
|
127
|
-
if (opts.servers) {
|
|
128
|
-
if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
|
|
129
|
-
client._reconnectCount = 0
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
opts.host = opts.servers[client._reconnectCount].host
|
|
133
|
-
opts.port = opts.servers[client._reconnectCount].port
|
|
134
|
-
opts.protocol = (!opts.servers[client._reconnectCount].protocol ? opts.defaultProtocol : opts.servers[client._reconnectCount].protocol)
|
|
135
|
-
opts.hostname = opts.host
|
|
136
|
-
|
|
137
|
-
client._reconnectCount++
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return protocols[opts.protocol](client, opts)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return new MqttClient(wrapper, opts)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
module.exports = connect
|
|
147
|
-
module.exports.connect = connect
|
|
148
|
-
module.exports.MqttClient = MqttClient
|
|
149
|
-
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 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.ws = require('./ws')
|
|
21
|
+
protocols.wss = require('./ws')
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Parse the auth attribute and merge username and password in the options object.
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} [opts] option object
|
|
27
|
+
*/
|
|
28
|
+
function parseAuthOptions (opts) {
|
|
29
|
+
var matches
|
|
30
|
+
if (opts.auth) {
|
|
31
|
+
matches = opts.auth.match(/^(.+):(.+)$/)
|
|
32
|
+
if (matches) {
|
|
33
|
+
opts.username = matches[1]
|
|
34
|
+
opts.password = matches[2]
|
|
35
|
+
} else {
|
|
36
|
+
opts.username = opts.auth
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* connect - connect to an MQTT broker.
|
|
43
|
+
*
|
|
44
|
+
* @param {String} [brokerUrl] - url of the broker, optional
|
|
45
|
+
* @param {Object} opts - see MqttClient#constructor
|
|
46
|
+
*/
|
|
47
|
+
function connect (brokerUrl, opts) {
|
|
48
|
+
if ((typeof brokerUrl === 'object') && !opts) {
|
|
49
|
+
opts = brokerUrl
|
|
50
|
+
brokerUrl = null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
opts = opts || {}
|
|
54
|
+
|
|
55
|
+
if (brokerUrl) {
|
|
56
|
+
var parsed = url.parse(brokerUrl, true)
|
|
57
|
+
if (parsed.port != null) {
|
|
58
|
+
parsed.port = Number(parsed.port)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
opts = xtend(parsed, opts)
|
|
62
|
+
|
|
63
|
+
if (opts.protocol === null) {
|
|
64
|
+
throw new Error('Missing protocol')
|
|
65
|
+
}
|
|
66
|
+
opts.protocol = opts.protocol.replace(/:$/, '')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// merge in the auth options if supplied
|
|
70
|
+
parseAuthOptions(opts)
|
|
71
|
+
|
|
72
|
+
// support clientId passed in the query string of the url
|
|
73
|
+
if (opts.query && typeof opts.query.clientId === 'string') {
|
|
74
|
+
opts.clientId = opts.query.clientId
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (opts.cert && opts.key) {
|
|
78
|
+
if (opts.protocol) {
|
|
79
|
+
if (['mqtts', 'wss', 'wxs'].indexOf(opts.protocol) === -1) {
|
|
80
|
+
switch (opts.protocol) {
|
|
81
|
+
case 'mqtt':
|
|
82
|
+
opts.protocol = 'mqtts'
|
|
83
|
+
break
|
|
84
|
+
case 'ws':
|
|
85
|
+
opts.protocol = 'wss'
|
|
86
|
+
break
|
|
87
|
+
case 'wx':
|
|
88
|
+
opts.protocol = 'wxs'
|
|
89
|
+
break
|
|
90
|
+
default:
|
|
91
|
+
throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
// don't know what protocol he want to use, mqtts or wss
|
|
96
|
+
throw new Error('Missing secure protocol key')
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!protocols[opts.protocol]) {
|
|
101
|
+
var isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
|
102
|
+
opts.protocol = [
|
|
103
|
+
'mqtt',
|
|
104
|
+
'mqtts',
|
|
105
|
+
'ws',
|
|
106
|
+
'wss',
|
|
107
|
+
'wx',
|
|
108
|
+
'wxs'
|
|
109
|
+
].filter(function (key, index) {
|
|
110
|
+
if (isSecure && index % 2 === 0) {
|
|
111
|
+
// Skip insecure protocols when requesting a secure one.
|
|
112
|
+
return false
|
|
113
|
+
}
|
|
114
|
+
return (typeof protocols[key] === 'function')
|
|
115
|
+
})[0]
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (opts.clean === false && !opts.clientId) {
|
|
119
|
+
throw new Error('Missing clientId for unclean clients')
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (opts.protocol) {
|
|
123
|
+
opts.defaultProtocol = opts.protocol
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function wrapper (client) {
|
|
127
|
+
if (opts.servers) {
|
|
128
|
+
if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
|
|
129
|
+
client._reconnectCount = 0
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
opts.host = opts.servers[client._reconnectCount].host
|
|
133
|
+
opts.port = opts.servers[client._reconnectCount].port
|
|
134
|
+
opts.protocol = (!opts.servers[client._reconnectCount].protocol ? opts.defaultProtocol : opts.servers[client._reconnectCount].protocol)
|
|
135
|
+
opts.hostname = opts.host
|
|
136
|
+
|
|
137
|
+
client._reconnectCount++
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return protocols[opts.protocol](client, opts)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return new MqttClient(wrapper, opts)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
module.exports = connect
|
|
147
|
+
module.exports.connect = connect
|
|
148
|
+
module.exports.MqttClient = MqttClient
|
|
149
|
+
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
|
package/lib/connect/ws.js
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var websocket = require('websocket-stream')
|
|
4
|
-
var urlModule = require('url')
|
|
5
|
-
var WSS_OPTIONS = [
|
|
6
|
-
'rejectUnauthorized',
|
|
7
|
-
'ca',
|
|
8
|
-
'cert',
|
|
9
|
-
'key',
|
|
10
|
-
'pfx',
|
|
11
|
-
'passphrase'
|
|
12
|
-
]
|
|
13
|
-
var IS_BROWSER = process.title === 'browser'
|
|
14
|
-
|
|
15
|
-
function buildUrl (opts, client) {
|
|
16
|
-
var url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
17
|
-
if (typeof (opts.transformWsUrl) === 'function') {
|
|
18
|
-
url = opts.transformWsUrl(url, opts, client)
|
|
19
|
-
}
|
|
20
|
-
return url
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function setDefaultOpts (opts) {
|
|
24
|
-
if (!opts.hostname) {
|
|
25
|
-
opts.hostname = 'localhost'
|
|
26
|
-
}
|
|
27
|
-
if (!opts.port) {
|
|
28
|
-
if (opts.protocol === 'wss') {
|
|
29
|
-
opts.port = 443
|
|
30
|
-
} else {
|
|
31
|
-
opts.port = 80
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
if (!opts.path) {
|
|
35
|
-
opts.path = '/'
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (!opts.wsOptions) {
|
|
39
|
-
opts.wsOptions = {}
|
|
40
|
-
}
|
|
41
|
-
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
42
|
-
// Add cert/key/ca etc options
|
|
43
|
-
WSS_OPTIONS.forEach(function (prop) {
|
|
44
|
-
if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) {
|
|
45
|
-
opts.wsOptions[prop] = opts[prop]
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function createWebSocket (client, opts) {
|
|
52
|
-
var websocketSubProtocol =
|
|
53
|
-
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
54
|
-
? 'mqttv3.1'
|
|
55
|
-
: 'mqtt'
|
|
56
|
-
|
|
57
|
-
setDefaultOpts(opts)
|
|
58
|
-
var url = buildUrl(opts, client)
|
|
59
|
-
return websocket(url, [websocketSubProtocol], opts.wsOptions)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function buildBuilder (client, opts) {
|
|
63
|
-
return createWebSocket(client, opts)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function buildBuilderBrowser (client, opts) {
|
|
67
|
-
if (!opts.hostname) {
|
|
68
|
-
opts.hostname = opts.host
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (!opts.hostname) {
|
|
72
|
-
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
|
73
|
-
// can not determine the `hostname` automatically. If connecting to
|
|
74
|
-
// localhost, please supply the `hostname` as an argument.
|
|
75
|
-
if (typeof (document) === 'undefined') {
|
|
76
|
-
throw new Error('Could not determine host. Specify host manually.')
|
|
77
|
-
}
|
|
78
|
-
var parsed = urlModule.parse(document.URL)
|
|
79
|
-
opts.hostname = parsed.hostname
|
|
80
|
-
|
|
81
|
-
if (!opts.port) {
|
|
82
|
-
opts.port = parsed.port
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return createWebSocket(client, opts)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (IS_BROWSER) {
|
|
89
|
-
module.exports = buildBuilderBrowser
|
|
90
|
-
} else {
|
|
91
|
-
module.exports = buildBuilder
|
|
92
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var websocket = require('websocket-stream')
|
|
4
|
+
var urlModule = require('url')
|
|
5
|
+
var WSS_OPTIONS = [
|
|
6
|
+
'rejectUnauthorized',
|
|
7
|
+
'ca',
|
|
8
|
+
'cert',
|
|
9
|
+
'key',
|
|
10
|
+
'pfx',
|
|
11
|
+
'passphrase'
|
|
12
|
+
]
|
|
13
|
+
var IS_BROWSER = process.title === 'browser'
|
|
14
|
+
|
|
15
|
+
function buildUrl (opts, client) {
|
|
16
|
+
var url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
17
|
+
if (typeof (opts.transformWsUrl) === 'function') {
|
|
18
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
19
|
+
}
|
|
20
|
+
return url
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function setDefaultOpts (opts) {
|
|
24
|
+
if (!opts.hostname) {
|
|
25
|
+
opts.hostname = 'localhost'
|
|
26
|
+
}
|
|
27
|
+
if (!opts.port) {
|
|
28
|
+
if (opts.protocol === 'wss') {
|
|
29
|
+
opts.port = 443
|
|
30
|
+
} else {
|
|
31
|
+
opts.port = 80
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!opts.path) {
|
|
35
|
+
opts.path = '/'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!opts.wsOptions) {
|
|
39
|
+
opts.wsOptions = {}
|
|
40
|
+
}
|
|
41
|
+
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
42
|
+
// Add cert/key/ca etc options
|
|
43
|
+
WSS_OPTIONS.forEach(function (prop) {
|
|
44
|
+
if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) {
|
|
45
|
+
opts.wsOptions[prop] = opts[prop]
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function createWebSocket (client, opts) {
|
|
52
|
+
var websocketSubProtocol =
|
|
53
|
+
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
54
|
+
? 'mqttv3.1'
|
|
55
|
+
: 'mqtt'
|
|
56
|
+
|
|
57
|
+
setDefaultOpts(opts)
|
|
58
|
+
var url = buildUrl(opts, client)
|
|
59
|
+
return websocket(url, [websocketSubProtocol], opts.wsOptions)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function buildBuilder (client, opts) {
|
|
63
|
+
return createWebSocket(client, opts)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function buildBuilderBrowser (client, opts) {
|
|
67
|
+
if (!opts.hostname) {
|
|
68
|
+
opts.hostname = opts.host
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!opts.hostname) {
|
|
72
|
+
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
|
73
|
+
// can not determine the `hostname` automatically. If connecting to
|
|
74
|
+
// localhost, please supply the `hostname` as an argument.
|
|
75
|
+
if (typeof (document) === 'undefined') {
|
|
76
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
77
|
+
}
|
|
78
|
+
var parsed = urlModule.parse(document.URL)
|
|
79
|
+
opts.hostname = parsed.hostname
|
|
80
|
+
|
|
81
|
+
if (!opts.port) {
|
|
82
|
+
opts.port = parsed.port
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return createWebSocket(client, opts)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (IS_BROWSER) {
|
|
89
|
+
module.exports = buildBuilderBrowser
|
|
90
|
+
} else {
|
|
91
|
+
module.exports = buildBuilder
|
|
92
|
+
}
|