|
2 | 2 |
|
3 | 3 | var MqttClient = require('../client') |
4 | 4 | var Store = require('../store') |
5 | | -var url = require('url') |
6 | | -var xtend = require('xtend') |
7 | 5 | var debug = require('debug')('mqttjs') |
8 | 6 |
|
9 | 7 | var protocols = {} |
@@ -60,23 +58,32 @@ function connect (brokerUrl, opts) { |
60 | 58 | opts = opts || {} |
61 | 59 |
|
62 | 60 | 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 | | - |
70 | 61 | if (opts.protocol === null) { |
71 | 62 | throw new Error('Missing protocol') |
72 | 63 | } |
| 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 |
73 | 74 | opts.protocol = opts.protocol.replace(/:$/, '') |
74 | 75 | } |
75 | 76 |
|
76 | 77 | // merge in the auth options if supplied |
| 78 | + // legacy support for url.parse objects (now deprecated in node.js) |
77 | 79 | parseAuthOptions(opts) |
78 | 80 |
|
79 | 81 | // 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) |
80 | 87 | if (opts.query && typeof opts.query.clientId === 'string') { |
81 | 88 | opts.clientId = opts.query.clientId |
82 | 89 | } |
|
0 commit comments