mqtt stuff added
This commit is contained in:
144
node_modules/mqtt/lib/connect/index.js
generated
vendored
Normal file
144
node_modules/mqtt/lib/connect/index.js
generated
vendored
Normal file
@ -0,0 +1,144 @@
|
||||
'use strict'
|
||||
|
||||
var MqttClient = require('../client')
|
||||
var Store = require('../store')
|
||||
var url = require('url')
|
||||
var xtend = require('xtend')
|
||||
var protocols = {}
|
||||
|
||||
if (process.title !== 'browser') {
|
||||
protocols.mqtt = require('./tcp')
|
||||
protocols.tcp = require('./tcp')
|
||||
protocols.ssl = require('./tls')
|
||||
protocols.tls = require('./tls')
|
||||
protocols.mqtts = require('./tls')
|
||||
} else {
|
||||
protocols.wx = require('./wx')
|
||||
protocols.wxs = require('./wx')
|
||||
}
|
||||
|
||||
protocols.ws = require('./ws')
|
||||
protocols.wss = require('./ws')
|
||||
|
||||
/**
|
||||
* Parse the auth attribute and merge username and password in the options object.
|
||||
*
|
||||
* @param {Object} [opts] option object
|
||||
*/
|
||||
function parseAuthOptions (opts) {
|
||||
var matches
|
||||
if (opts.auth) {
|
||||
matches = opts.auth.match(/^(.+):(.+)$/)
|
||||
if (matches) {
|
||||
opts.username = matches[1]
|
||||
opts.password = matches[2]
|
||||
} else {
|
||||
opts.username = opts.auth
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* connect - connect to an MQTT broker.
|
||||
*
|
||||
* @param {String} [brokerUrl] - url of the broker, optional
|
||||
* @param {Object} opts - see MqttClient#constructor
|
||||
*/
|
||||
function connect (brokerUrl, opts) {
|
||||
if ((typeof brokerUrl === 'object') && !opts) {
|
||||
opts = brokerUrl
|
||||
brokerUrl = null
|
||||
}
|
||||
|
||||
opts = opts || {}
|
||||
|
||||
if (brokerUrl) {
|
||||
var parsed = url.parse(brokerUrl, true)
|
||||
if (parsed.port != null) {
|
||||
parsed.port = Number(parsed.port)
|
||||
}
|
||||
|
||||
opts = xtend(parsed, opts)
|
||||
|
||||
if (opts.protocol === null) {
|
||||
throw new Error('Missing protocol')
|
||||
}
|
||||
opts.protocol = opts.protocol.replace(/:$/, '')
|
||||
}
|
||||
|
||||
// merge in the auth options if supplied
|
||||
parseAuthOptions(opts)
|
||||
|
||||
// support clientId passed in the query string of the url
|
||||
if (opts.query && typeof opts.query.clientId === 'string') {
|
||||
opts.clientId = opts.query.clientId
|
||||
}
|
||||
|
||||
if (opts.cert && opts.key) {
|
||||
if (opts.protocol) {
|
||||
if (['mqtts', 'wss', 'wxs'].indexOf(opts.protocol) === -1) {
|
||||
switch (opts.protocol) {
|
||||
case 'mqtt':
|
||||
opts.protocol = 'mqtts'
|
||||
break
|
||||
case 'ws':
|
||||
opts.protocol = 'wss'
|
||||
break
|
||||
case 'wx':
|
||||
opts.protocol = 'wxs'
|
||||
break
|
||||
default:
|
||||
throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// don't know what protocol he want to use, mqtts or wss
|
||||
throw new Error('Missing secure protocol key')
|
||||
}
|
||||
}
|
||||
|
||||
if (!protocols[opts.protocol]) {
|
||||
var isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
||||
opts.protocol = [
|
||||
'mqtt',
|
||||
'mqtts',
|
||||
'ws',
|
||||
'wss',
|
||||
'wx',
|
||||
'wxs'
|
||||
].filter(function (key, index) {
|
||||
if (isSecure && index % 2 === 0) {
|
||||
// Skip insecure protocols when requesting a secure one.
|
||||
return false
|
||||
}
|
||||
return (typeof protocols[key] === 'function')
|
||||
})[0]
|
||||
}
|
||||
|
||||
if (opts.clean === false && !opts.clientId) {
|
||||
throw new Error('Missing clientId for unclean clients')
|
||||
}
|
||||
|
||||
function wrapper (client) {
|
||||
if (opts.servers) {
|
||||
if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
|
||||
client._reconnectCount = 0
|
||||
}
|
||||
|
||||
opts.host = opts.servers[client._reconnectCount].host
|
||||
opts.port = opts.servers[client._reconnectCount].port
|
||||
opts.hostname = opts.host
|
||||
|
||||
client._reconnectCount++
|
||||
}
|
||||
|
||||
return protocols[opts.protocol](client, opts)
|
||||
}
|
||||
|
||||
return new MqttClient(wrapper, opts)
|
||||
}
|
||||
|
||||
module.exports = connect
|
||||
module.exports.connect = connect
|
||||
module.exports.MqttClient = MqttClient
|
||||
module.exports.Store = Store
|
19
node_modules/mqtt/lib/connect/tcp.js
generated
vendored
Normal file
19
node_modules/mqtt/lib/connect/tcp.js
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
var net = require('net')
|
||||
|
||||
/*
|
||||
variables port and host can be removed since
|
||||
you have all required information in opts object
|
||||
*/
|
||||
function buildBuilder (client, opts) {
|
||||
var port, host
|
||||
opts.port = opts.port || 1883
|
||||
opts.hostname = opts.hostname || opts.host || 'localhost'
|
||||
|
||||
port = opts.port
|
||||
host = opts.hostname
|
||||
|
||||
return net.createConnection(port, host)
|
||||
}
|
||||
|
||||
module.exports = buildBuilder
|
41
node_modules/mqtt/lib/connect/tls.js
generated
vendored
Normal file
41
node_modules/mqtt/lib/connect/tls.js
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
'use strict'
|
||||
var tls = require('tls')
|
||||
|
||||
function buildBuilder (mqttClient, opts) {
|
||||
var connection
|
||||
opts.port = opts.port || 8883
|
||||
opts.host = opts.hostname || opts.host || 'localhost'
|
||||
|
||||
opts.rejectUnauthorized = opts.rejectUnauthorized !== false
|
||||
|
||||
delete opts.path
|
||||
|
||||
connection = tls.connect(opts)
|
||||
/* eslint no-use-before-define: [2, "nofunc"] */
|
||||
connection.on('secureConnect', function () {
|
||||
if (opts.rejectUnauthorized && !connection.authorized) {
|
||||
connection.emit('error', new Error('TLS not authorized'))
|
||||
} else {
|
||||
connection.removeListener('error', handleTLSerrors)
|
||||
}
|
||||
})
|
||||
|
||||
function handleTLSerrors (err) {
|
||||
// How can I get verify this error is a tls error?
|
||||
if (opts.rejectUnauthorized) {
|
||||
mqttClient.emit('error', err)
|
||||
}
|
||||
|
||||
// close this connection to match the behaviour of net
|
||||
// otherwise all we get is an error from the connection
|
||||
// and close event doesn't fire. This is a work around
|
||||
// to enable the reconnect code to work the same as with
|
||||
// net.createConnection
|
||||
connection.end()
|
||||
}
|
||||
|
||||
connection.on('error', handleTLSerrors)
|
||||
return connection
|
||||
}
|
||||
|
||||
module.exports = buildBuilder
|
92
node_modules/mqtt/lib/connect/ws.js
generated
vendored
Normal file
92
node_modules/mqtt/lib/connect/ws.js
generated
vendored
Normal file
@ -0,0 +1,92 @@
|
||||
'use strict'
|
||||
|
||||
var websocket = require('websocket-stream')
|
||||
var urlModule = require('url')
|
||||
var WSS_OPTIONS = [
|
||||
'rejectUnauthorized',
|
||||
'ca',
|
||||
'cert',
|
||||
'key',
|
||||
'pfx',
|
||||
'passphrase'
|
||||
]
|
||||
var IS_BROWSER = process.title === 'browser'
|
||||
|
||||
function buildUrl (opts, client) {
|
||||
var url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
||||
if (typeof (opts.transformWsUrl) === 'function') {
|
||||
url = opts.transformWsUrl(url, opts, client)
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
function setDefaultOpts (opts) {
|
||||
if (!opts.hostname) {
|
||||
opts.hostname = 'localhost'
|
||||
}
|
||||
if (!opts.port) {
|
||||
if (opts.protocol === 'wss') {
|
||||
opts.port = 443
|
||||
} else {
|
||||
opts.port = 80
|
||||
}
|
||||
}
|
||||
if (!opts.path) {
|
||||
opts.path = '/'
|
||||
}
|
||||
|
||||
if (!opts.wsOptions) {
|
||||
opts.wsOptions = {}
|
||||
}
|
||||
if (!IS_BROWSER && opts.protocol === 'wss') {
|
||||
// Add cert/key/ca etc options
|
||||
WSS_OPTIONS.forEach(function (prop) {
|
||||
if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) {
|
||||
opts.wsOptions[prop] = opts[prop]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function createWebSocket (client, opts) {
|
||||
var websocketSubProtocol =
|
||||
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
||||
? 'mqttv3.1'
|
||||
: 'mqtt'
|
||||
|
||||
setDefaultOpts(opts)
|
||||
var url = buildUrl(opts, client)
|
||||
return websocket(url, [websocketSubProtocol], opts.wsOptions)
|
||||
}
|
||||
|
||||
function buildBuilder (client, opts) {
|
||||
return createWebSocket(client, opts)
|
||||
}
|
||||
|
||||
function buildBuilderBrowser (client, opts) {
|
||||
if (!opts.hostname) {
|
||||
opts.hostname = opts.host
|
||||
}
|
||||
|
||||
if (!opts.hostname) {
|
||||
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
||||
// can not determine the `hostname` automatically. If connecting to
|
||||
// localhost, please supply the `hostname` as an argument.
|
||||
if (typeof (document) === 'undefined') {
|
||||
throw new Error('Could not determine host. Specify host manually.')
|
||||
}
|
||||
var parsed = urlModule.parse(document.URL)
|
||||
opts.hostname = parsed.hostname
|
||||
|
||||
if (!opts.port) {
|
||||
opts.port = parsed.port
|
||||
}
|
||||
}
|
||||
return createWebSocket(client, opts)
|
||||
}
|
||||
|
||||
if (IS_BROWSER) {
|
||||
module.exports = buildBuilderBrowser
|
||||
} else {
|
||||
module.exports = buildBuilder
|
||||
}
|
123
node_modules/mqtt/lib/connect/wx.js
generated
vendored
Normal file
123
node_modules/mqtt/lib/connect/wx.js
generated
vendored
Normal file
@ -0,0 +1,123 @@
|
||||
'use strict'
|
||||
|
||||
/* global wx */
|
||||
var socketOpen = false
|
||||
var socketMsgQueue = []
|
||||
|
||||
function sendSocketMessage (msg) {
|
||||
if (socketOpen) {
|
||||
wx.sendSocketMessage({
|
||||
data: msg.buffer || msg
|
||||
})
|
||||
} else {
|
||||
socketMsgQueue.push(msg)
|
||||
}
|
||||
}
|
||||
|
||||
function WebSocket (url, protocols) {
|
||||
var ws = {
|
||||
OPEN: 1,
|
||||
CLOSING: 2,
|
||||
CLOSED: 3,
|
||||
readyState: socketOpen ? 1 : 0,
|
||||
send: sendSocketMessage,
|
||||
close: wx.closeSocket,
|
||||
onopen: null,
|
||||
onmessage: null,
|
||||
onclose: null,
|
||||
onerror: null
|
||||
}
|
||||
|
||||
wx.connectSocket({
|
||||
url: url,
|
||||
protocols: protocols
|
||||
})
|
||||
wx.onSocketOpen(function (res) {
|
||||
ws.readyState = ws.OPEN
|
||||
socketOpen = true
|
||||
for (var i = 0; i < socketMsgQueue.length; i++) {
|
||||
sendSocketMessage(socketMsgQueue[i])
|
||||
}
|
||||
socketMsgQueue = []
|
||||
|
||||
ws.onopen && ws.onopen.apply(ws, arguments)
|
||||
})
|
||||
wx.onSocketMessage(function (res) {
|
||||
ws.onmessage && ws.onmessage.apply(ws, arguments)
|
||||
})
|
||||
wx.onSocketClose(function () {
|
||||
ws.onclose && ws.onclose.apply(ws, arguments)
|
||||
ws.readyState = ws.CLOSED
|
||||
socketOpen = false
|
||||
})
|
||||
wx.onSocketError(function () {
|
||||
ws.onerror && ws.onerror.apply(ws, arguments)
|
||||
ws.readyState = ws.CLOSED
|
||||
socketOpen = false
|
||||
})
|
||||
|
||||
return ws
|
||||
}
|
||||
|
||||
var websocket = require('websocket-stream')
|
||||
var urlModule = require('url')
|
||||
|
||||
function buildUrl (opts, client) {
|
||||
var protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
|
||||
var url = protocol + '://' + opts.hostname + opts.path
|
||||
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
||||
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
||||
}
|
||||
if (typeof (opts.transformWsUrl) === 'function') {
|
||||
url = opts.transformWsUrl(url, opts, client)
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
function setDefaultOpts (opts) {
|
||||
if (!opts.hostname) {
|
||||
opts.hostname = 'localhost'
|
||||
}
|
||||
if (!opts.path) {
|
||||
opts.path = '/'
|
||||
}
|
||||
|
||||
if (!opts.wsOptions) {
|
||||
opts.wsOptions = {}
|
||||
}
|
||||
}
|
||||
|
||||
function createWebSocket (client, opts) {
|
||||
var websocketSubProtocol =
|
||||
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
||||
? 'mqttv3.1'
|
||||
: 'mqtt'
|
||||
|
||||
setDefaultOpts(opts)
|
||||
var url = buildUrl(opts, client)
|
||||
return websocket(WebSocket(url, [websocketSubProtocol]))
|
||||
}
|
||||
|
||||
function buildBuilder (client, opts) {
|
||||
if (!opts.hostname) {
|
||||
opts.hostname = opts.host
|
||||
}
|
||||
|
||||
if (!opts.hostname) {
|
||||
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
||||
// can not determine the `hostname` automatically. If connecting to
|
||||
// localhost, please supply the `hostname` as an argument.
|
||||
if (typeof (document) === 'undefined') {
|
||||
throw new Error('Could not determine host. Specify host manually.')
|
||||
}
|
||||
var parsed = urlModule.parse(document.URL)
|
||||
opts.hostname = parsed.hostname
|
||||
|
||||
if (!opts.port) {
|
||||
opts.port = parsed.port
|
||||
}
|
||||
}
|
||||
return createWebSocket(client, opts)
|
||||
}
|
||||
|
||||
module.exports = buildBuilder
|
Reference in New Issue
Block a user