Skip to content

Commit 1c19ca0

Browse files
authoredFeb 3, 2025
fix: move protocol "clean" after merge of parsedOptions (#1965)
1 parent ad78551 commit 1c19ca0

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed
 

‎src/lib/connect/index.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,14 @@ function connect(
7272
parsedOptions.protocol = parsedUrl.protocol as MqttProtocol
7373
parsedOptions.path = parsedUrl.path
7474

75-
parsedOptions.protocol = parsedOptions.protocol?.replace(
76-
/:$/,
77-
'',
78-
) as MqttProtocol
79-
8075
opts = { ...parsedOptions, ...opts }
8176

8277
// when parsing an url expect the protocol to be set
8378
if (!opts.protocol) {
8479
throw new Error('Missing protocol')
8580
}
81+
82+
opts.protocol = opts.protocol.replace(/:$/, '') as MqttProtocol
8683
}
8784

8885
opts.unixSocket = opts.unixSocket || opts.protocol?.includes('+unix')

‎test/node/mqtt.ts

+14
Original file line numberDiff line numberDiff line change
@@ -264,5 +264,19 @@ describe('mqtt', () => {
264264
c.options.should.have.property('clientId', '123')
265265
c.end((err) => done(err))
266266
})
267+
268+
it('should return an MqttClient with mqtts protocol when connect is called with mqtts:/ url and protocol (mqtts:) is specified in options', function _test(t, done) {
269+
const url = 'mqtts://localhost:1883'
270+
const parsedUrl = new URL(url)
271+
const protocol = parsedUrl.protocol as 'mqtt' | 'mqtts'
272+
273+
const c = mqtt.connect(url, {
274+
protocol,
275+
})
276+
277+
c.should.be.instanceOf(mqtt.MqttClient)
278+
c.options.should.have.property('protocol', 'mqtts')
279+
c.end((err) => done(err))
280+
})
267281
})
268282
})

0 commit comments

Comments
 (0)