Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rollback URL #1217

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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