Skip to content

Commit 27b9312

Browse files
authored
Merge pull request #51 from mqttjs/multiple-user-properties-with-same-name
user properties with same names
2 parents f513e33 + 21ebeea commit 27b9312

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

parser.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,17 @@ Parser.prototype._parseProperties = function () {
597597
result[name] = {}
598598
}
599599
var currentUserProperty = this._parseByType(constants.propertiesTypes[name])
600-
result[name][currentUserProperty.name] = currentUserProperty.value
600+
if (result[name][currentUserProperty.name]) {
601+
if (Array.isArray(result[name][currentUserProperty.name])) {
602+
result[name][currentUserProperty.name].push(currentUserProperty.value)
603+
} else {
604+
var currentValue = result[name][currentUserProperty.name]
605+
result[name][currentUserProperty.name] = [currentValue]
606+
result[name][currentUserProperty.name].push(currentUserProperty.value)
607+
}
608+
} else {
609+
result[name][currentUserProperty.name] = currentUserProperty.value
610+
}
601611
continue
602612
}
603613
result[name] = this._parseByType(constants.propertiesTypes[name])

test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,58 @@ testParseGenerate('connack MQTT5 with properties', {
780780
22, 0, 4, 1, 2, 3, 4 // authenticationData
781781
]), { protocolVersion: 5 })
782782

783+
testParseGenerate('connack MQTT5 with properties and doubled user properties', {
784+
cmd: 'connack',
785+
retain: false,
786+
qos: 0,
787+
dup: false,
788+
length: 100,
789+
sessionPresent: false,
790+
reasonCode: 0,
791+
properties: {
792+
sessionExpiryInterval: 1234,
793+
receiveMaximum: 432,
794+
maximumQoS: 2,
795+
retainAvailable: true,
796+
maximumPacketSize: 100,
797+
assignedClientIdentifier: 'test',
798+
topicAliasMaximum: 456,
799+
reasonString: 'test',
800+
userProperties: {
801+
'test': ['test', 'test']
802+
},
803+
wildcardSubscriptionAvailable: true,
804+
subscriptionIdentifiersAvailable: true,
805+
sharedSubscriptionAvailable: false,
806+
serverKeepAlive: 1234,
807+
responseInformation: 'test',
808+
serverReference: 'test',
809+
authenticationMethod: 'test',
810+
authenticationData: Buffer.from([1, 2, 3, 4])
811+
}
812+
}, Buffer.from([
813+
32, 100, 0, 0,
814+
97, // properties length
815+
17, 0, 0, 4, 210, // sessionExpiryInterval
816+
33, 1, 176, // receiveMaximum
817+
36, 2, // Maximum qos
818+
37, 1, // retainAvailable
819+
39, 0, 0, 0, 100, // maximumPacketSize
820+
18, 0, 4, 116, 101, 115, 116, // assignedClientIdentifier
821+
34, 1, 200, // topicAliasMaximum
822+
31, 0, 4, 116, 101, 115, 116, // reasonString
823+
38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116,
824+
38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116, // userProperties
825+
40, 1, // wildcardSubscriptionAvailable
826+
41, 1, // subscriptionIdentifiersAvailable
827+
42, 0, // sharedSubscriptionAvailable
828+
19, 4, 210, // serverKeepAlive
829+
26, 0, 4, 116, 101, 115, 116, // responseInformation
830+
28, 0, 4, 116, 101, 115, 116, // serverReference
831+
21, 0, 4, 116, 101, 115, 116, // authenticationMethod
832+
22, 0, 4, 1, 2, 3, 4 // authenticationData
833+
]), { protocolVersion: 5 })
834+
783835
testParseGenerate('connack with return code 0 session present bit set', {
784836
cmd: 'connack',
785837
retain: false,

writeToStream.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,15 @@ function getProperties (stream, properties) {
926926
return false
927927
}
928928
length += Object.getOwnPropertyNames(value).reduce(function (result, name) {
929-
result += 1 + 2 + Buffer.byteLength(name.toString()) + 2 + Buffer.byteLength(value[name].toString())
929+
var currentValue = value[name]
930+
if (Array.isArray(currentValue)) {
931+
result += currentValue.reduce((currentLength, value) => {
932+
currentLength += 1 + 2 + Buffer.byteLength(name.toString()) + 2 + Buffer.byteLength(value.toString())
933+
return currentLength
934+
}, 0)
935+
} else {
936+
result += 1 + 2 + Buffer.byteLength(name.toString()) + 2 + Buffer.byteLength(value[name].toString())
937+
}
930938
return result
931939
}, 0)
932940
break
@@ -1019,8 +1027,16 @@ function writeProperties (stream, properties, propertiesLength) {
10191027
}
10201028
case 'pair': {
10211029
Object.getOwnPropertyNames(value).forEach(function (name) {
1022-
stream.write(Buffer.from([protocol.properties[propName]]))
1023-
writeStringPair(stream, name.toString(), value[name].toString())
1030+
var currentValue = value[name]
1031+
if (Array.isArray(currentValue)) {
1032+
currentValue.forEach(function (value) {
1033+
stream.write(Buffer.from([protocol.properties[propName]]))
1034+
writeStringPair(stream, name.toString(), value.toString())
1035+
})
1036+
} else {
1037+
stream.write(Buffer.from([protocol.properties[propName]]))
1038+
writeStringPair(stream, name.toString(), currentValue.toString())
1039+
}
10241040
})
10251041
break
10261042
}

0 commit comments

Comments
 (0)