Skip to content

fix to utf-8 clientId bug #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,45 @@ testParseGenerateDefaults('no clientId with 5.0', {
clientId: ''
}, { protocolVersion: 5 })

testParseGenerateDefaults('utf-8 clientId with 5.0', {
cmd: 'connect',
retain: false,
qos: 0,
dup: false,
length: 23,
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
keepalive: 30,
clientId: 'Ŧėśt🜄'
}, Buffer.from([
16, 23, // Header
0, 4, // Protocol ID length
77, 81, 84, 84, // Protocol ID
4, // Protocol version
2, // Connect flags
0, 30, // Keepalive
0, 11, // Client ID length
197, 166, // Ŧ (UTF-8: 0xc5a6)
196, 151, // ė (UTF-8: 0xc497)
197, 155, // ś (utf-8: 0xc59b)
116, // t (utf-8: 0x74)
240, 159, 156, 132 // 🜄 (utf-8: 0xf09f9c84)
]), {
cmd: 'connect',
retain: false,
qos: 0,
dup: false,
length: 23,
topic: null,
payload: null,
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
keepalive: 30,
clientId: 'Ŧėśt🜄'
}, { protocol: 5 })

testParseGenerateDefaults('default connect', {
cmd: 'connect',
clientId: 'test'
Expand Down
2 changes: 1 addition & 1 deletion writeToStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function connect (packet, stream, opts) {
// ClientId might be omitted in 3.1.1 and 5, but only if cleanSession is set to 1
if ((typeof clientId === 'string' || Buffer.isBuffer(clientId)) &&
(clientId || protocolVersion >= 4) && (clientId || clean)) {
length += clientId.length + 2
length += Buffer.byteLength(clientId) + 2
} else {
if (protocolVersion < 4) {
stream.emit('error', new Error('clientId must be supplied before 3.1.1'))
Expand Down