UNPKG

3.58 kBJavaScriptView Raw
1'use strict'
2
3var MqttClient = require('../client')
4var Store = require('../store')
5var url = require('url')
6var xtend = require('xtend')
7var protocols = {}
8
9if (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
20protocols.ws = require('./ws')
21protocols.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 */
28function 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 */
47function 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 function wrapper (client) {
123 if (opts.servers) {
124 if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
125 client._reconnectCount = 0
126 }
127
128 opts.host = opts.servers[client._reconnectCount].host
129 opts.port = opts.servers[client._reconnectCount].port
130 opts.hostname = opts.host
131
132 client._reconnectCount++
133 }
134
135 return protocols[opts.protocol](client, opts)
136 }
137
138 return new MqttClient(wrapper, opts)
139}
140
141module.exports = connect
142module.exports.connect = connect
143module.exports.MqttClient = MqttClient
144module.exports.Store = Store