Skip to content

Commit 6aef40f

Browse files
committed
Replace url.parse by new URL
1 parent b5b3814 commit 6aef40f

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

examples/wss/client_with_proxy.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
var mqtt = require('mqtt')
44
var HttpsProxyAgent = require('https-proxy-agent')
5-
var url = require('url')
65
/*
76
host: host of the endpoint you want to connect e.g. my.mqqt.host.com
87
path: path to you endpoint e.g. '/foo/bar/mqtt'
@@ -13,8 +12,8 @@ proxy: your proxy e.g. proxy.foo.bar.com
1312
port: http proxy port e.g. 8080
1413
*/
1514
var proxy = process.env.http_proxy || 'http://<proxy>:<port>'
16-
var parsed = url.parse(endpoint)
17-
var proxyOpts = url.parse(proxy)
15+
var parsed = new URL(endpoint)
16+
var proxyOpts = new URL(proxy)
1817
// true for wss
1918
proxyOpts.secureEndpoint = parsed.protocol ? parsed.protocol === 'wss:' : true
2019
var agent = new HttpsProxyAgent(proxyOpts)

lib/connect/index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
var MqttClient = require('../client')
44
var Store = require('../store')
5-
var url = require('url')
6-
var xtend = require('xtend')
75
var debug = require('debug')('mqttjs')
86

97
var protocols = {}
@@ -60,23 +58,32 @@ function connect (brokerUrl, opts) {
6058
opts = opts || {}
6159

6260
if (brokerUrl) {
63-
var parsed = url.parse(brokerUrl, true)
64-
if (parsed.port != null) {
65-
parsed.port = Number(parsed.port)
66-
}
67-
68-
opts = xtend(parsed, opts)
69-
7061
if (opts.protocol === null) {
7162
throw new Error('Missing protocol')
7263
}
64+
var parsed = new URL(brokerUrl)
65+
// the URL object is a bit special, so copy individual
66+
// items to the opts object
67+
opts.hostname = parsed.hostname
68+
opts.host = parsed.host
69+
opts.protocol = parsed.protocol
70+
opts.port = Number(parsed.port) || null
71+
opts.username = parsed.username
72+
opts.password = parsed.password
73+
opts.searchParams = parsed.searchParams
7374
opts.protocol = opts.protocol.replace(/:$/, '')
7475
}
7576

7677
// merge in the auth options if supplied
78+
// legacy support for url.parse objects (now deprecated in node.js)
7779
parseAuthOptions(opts)
7880

7981
// support clientId passed in the query string of the url
82+
if (opts.searchParams && typeof opts.searchParams.get('clientId') === 'string') {
83+
opts.clientId = opts.searchParams.get('clientId')
84+
}
85+
86+
// legacy support for url.parse objects (now deprecated in node.js)
8087
if (opts.query && typeof opts.query.clientId === 'string') {
8188
opts.clientId = opts.query.clientId
8289
}

lib/connect/ws.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const WS = require('ws')
44
const debug = require('debug')('mqttjs:ws')
55
const duplexify = require('duplexify')
6-
const urlModule = require('url')
6+
const Buffer = require('safe-buffer').Buffer
77
const Transform = require('readable-stream').Transform
88

99
let WSS_OPTIONS = [
@@ -69,7 +69,7 @@ function setDefaultBrowserOpts (opts) {
6969
if (typeof (document) === 'undefined') {
7070
throw new Error('Could not determine host. Specify host manually.')
7171
}
72-
const parsed = urlModule.parse(document.URL)
72+
const parsed = new URL(document.URL)
7373
options.hostname = parsed.hostname
7474

7575
if (!options.port) {

test/browser/test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict'
22

33
var mqtt = require('../../lib/connect')
4-
var _URL = require('url')
54
var xtend = require('xtend')
6-
var parsed = _URL.parse(document.URL)
5+
var parsed = new URL(document.URL)
76
var isHttps = parsed.protocol === 'https:'
87
var port = parsed.port || (isHttps ? 443 : 80)
98
var host = parsed.hostname

test/mqtt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('mqtt', function () {
1818
(function () {
1919
var c = mqtt.connect('foo.bar.com')
2020
c.end()
21-
}).should.throw('Missing protocol')
21+
}).should.throw('Invalid URL: foo.bar.com')
2222
})
2323

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

0 commit comments

Comments
 (0)