Skip to content

Commit 4690e25

Browse files
authored
fix(write): ignore undefined properties (#155)
* #152 - ignore undefined properties * #152 - add test
1 parent d54a270 commit 4690e25

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

test.js

+57
Original file line numberDiff line numberDiff line change
@@ -3161,6 +3161,63 @@ test('stops parsing after first error', t => {
31613161
]))
31623162
})
31633163

3164+
test('undefined properties', t => {
3165+
t.plan(2)
3166+
3167+
const packet = mqtt.generate({
3168+
cmd: 'connect',
3169+
retain: false,
3170+
qos: 0,
3171+
dup: false,
3172+
length: 125,
3173+
protocolId: 'MQTT',
3174+
protocolVersion: 5,
3175+
will: {
3176+
retain: true,
3177+
qos: 2,
3178+
properties: {
3179+
willDelayInterval: 1234,
3180+
payloadFormatIndicator: false,
3181+
messageExpiryInterval: 4321,
3182+
contentType: 'test',
3183+
responseTopic: 'topic',
3184+
correlationData: Buffer.from([1, 2, 3, 4]),
3185+
userProperties: {
3186+
test: 'test'
3187+
}
3188+
},
3189+
topic: 'topic',
3190+
payload: Buffer.from([4, 3, 2, 1])
3191+
},
3192+
clean: true,
3193+
keepalive: 30,
3194+
properties: {
3195+
sessionExpiryInterval: 1234,
3196+
receiveMaximum: 432,
3197+
maximumPacketSize: 100,
3198+
topicAliasMaximum: 456,
3199+
requestResponseInformation: true,
3200+
requestProblemInformation: true,
3201+
correlationData: undefined,
3202+
userProperties: {
3203+
test: 'test'
3204+
},
3205+
authenticationMethod: 'test',
3206+
authenticationData: Buffer.from([1, 2, 3, 4])
3207+
},
3208+
clientId: 'test'
3209+
})
3210+
3211+
const parser = mqtt.parser()
3212+
3213+
parser.on('packet', packet => {
3214+
t.equal(packet.cmd, 'connect')
3215+
t.equal(Object.hasOwn(packet.properties, 'correlationData'), false)
3216+
})
3217+
3218+
parser.parse(packet)
3219+
})
3220+
31643221
testGenerateErrorMultipleCmds([
31653222
'publish',
31663223
'puback',

writeToStream.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,9 @@ function getProperties (stream, properties) {
993993
let propLength = 0
994994
let propValueLength = 0
995995
const propValue = properties[propName]
996-
if (Array.isArray(propValue)) {
996+
if (propValue === undefined) {
997+
continue
998+
} else if (Array.isArray(propValue)) {
997999
for (let valueIndex = 0; valueIndex < propValue.length; valueIndex++) {
9981000
propValueLength = getLengthProperty(propName, propValue[valueIndex])
9991001
if (!propValueLength) { return false }
@@ -1101,7 +1103,7 @@ function writeProperties (stream, properties, propertiesLength) {
11011103
/* write properties to stream */
11021104
writeVarByteInt(stream, propertiesLength)
11031105
for (const propName in properties) {
1104-
if (Object.prototype.hasOwnProperty.call(properties, propName) && properties[propName] !== null) {
1106+
if (Object.prototype.hasOwnProperty.call(properties, propName) && properties[propName] != null) {
11051107
const value = properties[propName]
11061108
if (Array.isArray(value)) {
11071109
for (let valueIndex = 0; valueIndex < value.length; valueIndex++) {

0 commit comments

Comments
 (0)