Skip to content

Commit f66681f

Browse files
authored
fix to utf-8 clientId bug (#105)
1 parent 961a5ee commit f66681f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

test.js

+39
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,45 @@ testParseGenerateDefaults('no clientId with 5.0', {
550550
clientId: ''
551551
}, { protocolVersion: 5 })
552552

553+
testParseGenerateDefaults('utf-8 clientId with 5.0', {
554+
cmd: 'connect',
555+
retain: false,
556+
qos: 0,
557+
dup: false,
558+
length: 23,
559+
protocolId: 'MQTT',
560+
protocolVersion: 4,
561+
clean: true,
562+
keepalive: 30,
563+
clientId: 'Ŧėśt🜄'
564+
}, Buffer.from([
565+
16, 23, // Header
566+
0, 4, // Protocol ID length
567+
77, 81, 84, 84, // Protocol ID
568+
4, // Protocol version
569+
2, // Connect flags
570+
0, 30, // Keepalive
571+
0, 11, // Client ID length
572+
197, 166, // Ŧ (UTF-8: 0xc5a6)
573+
196, 151, // ė (UTF-8: 0xc497)
574+
197, 155, // ś (utf-8: 0xc59b)
575+
116, // t (utf-8: 0x74)
576+
240, 159, 156, 132 // 🜄 (utf-8: 0xf09f9c84)
577+
]), {
578+
cmd: 'connect',
579+
retain: false,
580+
qos: 0,
581+
dup: false,
582+
length: 23,
583+
topic: null,
584+
payload: null,
585+
protocolId: 'MQTT',
586+
protocolVersion: 4,
587+
clean: true,
588+
keepalive: 30,
589+
clientId: 'Ŧėśt🜄'
590+
}, { protocol: 5 })
591+
553592
testParseGenerateDefaults('default connect', {
554593
cmd: 'connect',
555594
clientId: 'test'

writeToStream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function connect (packet, stream, opts) {
113113
// ClientId might be omitted in 3.1.1 and 5, but only if cleanSession is set to 1
114114
if ((typeof clientId === 'string' || Buffer.isBuffer(clientId)) &&
115115
(clientId || protocolVersion >= 4) && (clientId || clean)) {
116-
length += clientId.length + 2
116+
length += Buffer.byteLength(clientId) + 2
117117
} else {
118118
if (protocolVersion < 4) {
119119
stream.emit('error', new Error('clientId must be supplied before 3.1.1'))

0 commit comments

Comments
 (0)