Skip to content

Commit

Permalink
fix: replace url.parse by WHATWG URL API (#1147)
Browse files Browse the repository at this point in the history
fixes #1130
  • Loading branch information
seriousme authored Oct 6, 2020
1 parent b5b3814 commit 70a247c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
5 changes: 2 additions & 3 deletions examples/wss/client_with_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var mqtt = require('mqtt')
var HttpsProxyAgent = require('https-proxy-agent')
var url = require('url')
/*
host: host of the endpoint you want to connect e.g. my.mqqt.host.com
path: path to you endpoint e.g. '/foo/bar/mqtt'
Expand All @@ -13,8 +12,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 = url.parse(endpoint)
var proxyOpts = url.parse(proxy)
var parsed = new URL(endpoint)
var proxyOpts = new URL(proxy)
// true for wss
proxyOpts.secureEndpoint = parsed.protocol ? parsed.protocol === 'wss:' : true
var agent = new HttpsProxyAgent(proxyOpts)
Expand Down
25 changes: 16 additions & 9 deletions lib/connect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

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 @@ -60,23 +58,32 @@ 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.hostname = parsed.hostname
opts.host = parsed.host
opts.protocol = parsed.protocol
opts.port = Number(parsed.port) || null
opts.username = parsed.username
opts.password = parsed.password
opts.searchParams = parsed.searchParams
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
4 changes: 2 additions & 2 deletions lib/connect/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const WS = require('ws')
const debug = require('debug')('mqttjs:ws')
const duplexify = require('duplexify')
const urlModule = require('url')
const Buffer = require('safe-buffer').Buffer
const Transform = require('readable-stream').Transform

let WSS_OPTIONS = [
Expand Down Expand Up @@ -69,7 +69,7 @@ function setDefaultBrowserOpts (opts) {
if (typeof (document) === 'undefined') {
throw new Error('Could not determine host. Specify host manually.')
}
const parsed = urlModule.parse(document.URL)
const parsed = new URL(document.URL)
options.hostname = parsed.hostname

if (!options.port) {
Expand Down
3 changes: 1 addition & 2 deletions test/browser/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict'

var mqtt = require('../../lib/connect')
var _URL = require('url')
var xtend = require('xtend')
var parsed = _URL.parse(document.URL)
var parsed = new URL(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('Missing protocol')
}).should.throw('Invalid URL: foo.bar.com')
})

it('should throw an error when called with no protocol specified - with options', function () {
Expand Down

0 comments on commit 70a247c

Please sign in to comment.