Skip to content

Commit

Permalink
Merge pull request #1217 from YoDaMa/fix_URL_rollback
Browse files Browse the repository at this point in the history
fix: rollback URL
  • Loading branch information
Yoseph Maguire authored Nov 24, 2020
2 parents ba90144 + a3dd38e commit 9d45d61
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 27 deletions.
5 changes: 3 additions & 2 deletions examples/wss/client_with_proxy.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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://<proxy>:<port>'
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)
Expand Down
33 changes: 10 additions & 23 deletions lib/connect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion test/browser/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 9d45d61

Please # to comment.