diff --git a/examples/wss/client_with_proxy.js b/examples/wss/client_with_proxy.js index 49513907a..4a0d9f3c9 100644 --- a/examples/wss/client_with_proxy.js +++ b/examples/wss/client_with_proxy.js @@ -1,6 +1,7 @@ 'use strict' var mqtt = require('mqtt') +var url = require('url') var HttpsProxyAgent = require('https-proxy-agent') /* host: host of the endpoint you want to connect e.g. my.mqqt.host.com @@ -12,8 +13,8 @@ proxy: your proxy e.g. proxy.foo.bar.com port: http proxy port e.g. 8080 */ var proxy = process.env.http_proxy || 'http://:' -var parsed = new URL(endpoint) -var proxyOpts = new URL(proxy) +var parsed = url.parse(endpoint) +var proxyOpts = url.parse(proxy) // true for wss proxyOpts.secureEndpoint = parsed.protocol ? parsed.protocol === 'wss:' : true var agent = new HttpsProxyAgent(proxyOpts) diff --git a/lib/connect/index.js b/lib/connect/index.js index 30a2ece28..97e7b4c15 100644 --- a/lib/connect/index.js +++ b/lib/connect/index.js @@ -2,6 +2,8 @@ var MqttClient = require('../client') var Store = require('../store') +var url = require('url') +var xtend = require('xtend') var debug = require('debug')('mqttjs') var protocols = {} @@ -58,39 +60,24 @@ function connect (brokerUrl, opts) { 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') } - var parsed = new URL(brokerUrl) - - // the URL object is a bit special, so copy individual - // items to the opts object - opts.hash = parsed.hash - opts.host = parsed.host - opts.hostname = parsed.hostname - opts.href = parsed.href - opts.origin = parsed.origin - opts.pathname = parsed.pathname - opts.port = Number(parsed.port) || null - opts.protocol = parsed.protocol - opts.username = opts.username || parsed.username || null - opts.password = opts.password || parsed.password || null - opts.search = parsed.search - opts.searchParams = parsed.searchParams - opts.path = parsed.pathname + parsed.search + opts.protocol = opts.protocol.replace(/:$/, '') } // merge in the auth options if supplied - // legacy support for url.parse objects (now deprecated in node.js) parseAuthOptions(opts) // support clientId passed in the query string of the url - if (opts.searchParams && typeof opts.searchParams.get('clientId') === 'string') { - opts.clientId = opts.searchParams.get('clientId') - } - - // legacy support for url.parse objects (now deprecated in node.js) if (opts.query && typeof opts.query.clientId === 'string') { opts.clientId = opts.query.clientId } diff --git a/test/browser/test.js b/test/browser/test.js index fd25abafb..8e9cd42e3 100644 --- a/test/browser/test.js +++ b/test/browser/test.js @@ -2,7 +2,8 @@ var mqtt = require('../../lib/connect') var xtend = require('xtend') -var parsed = new URL(document.URL) +var _URL = require('url') +var parsed = _URL.parse(document.URL) var isHttps = parsed.protocol === 'https:' var port = parsed.port || (isHttps ? 443 : 80) var host = parsed.hostname diff --git a/test/mqtt.js b/test/mqtt.js index a96e18fee..f55d04a33 100644 --- a/test/mqtt.js +++ b/test/mqtt.js @@ -18,7 +18,7 @@ describe('mqtt', function () { (function () { var c = mqtt.connect('foo.bar.com') c.end() - }).should.throw('Invalid URL: foo.bar.com') + }).should.throw('Missing protocol') }) it('should throw an error when called with no protocol specified - with options', function () {