Skip to content

Commit de0174f

Browse files
authored
fix: prevent url.parse to set path option (#1871)
* fix: prevent url.parse to set `path` option Fixes #1870 * fix: add missing auth * fix: add test
1 parent 52e0e70 commit de0174f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/lib/connect/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,16 @@ function connect(
8080
parsed.port = Number(parsed.port)
8181
}
8282

83-
opts = { ...parsed, ...opts } as IClientOptions
83+
opts = {
84+
...{
85+
port: parsed.port,
86+
host: parsed.hostname,
87+
protocol: parsed.protocol,
88+
query: parsed.query,
89+
auth: parsed.auth,
90+
},
91+
...opts,
92+
} as IClientOptions
8493

8594
if (opts.protocol === null) {
8695
throw new Error('Missing protocol')

test/mqtt.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ describe('mqtt', () => {
3434
c.end((err) => done(err))
3535
})
3636

37+
it('should not set `path` when parsing url', function _test(t, done) {
38+
const c = mqtt.connect('mqtt://[::1]')
39+
40+
c.should.be.instanceOf(mqtt.MqttClient)
41+
c.options.should.not.have.property('path')
42+
c.options.should.have.property('host', '::1')
43+
c.end((err) => done(err))
44+
})
45+
3746
it('should return an MqttClient with username and password options set', function _test(t, done) {
3847
const c = mqtt.connect('mqtt://user@localhost:1883')
3948

0 commit comments

Comments
 (0)