Skip to content

Write to stream fails when "userProperties" is an empy object #149

@br0wn

Description

@br0wn

While using protocol version 5, calling writeToStream with userProperties being empty object {} method fails with false as return value.
This is the packet being written:

{
  cmd: "publish",
  topic: "protocol-version-debug",
  payload: "{\"pattern\":\"protocol-version-debug\",\"data\":\"{\\\"ok\\\":1724704578895}\"}",
  qos: 0,
  retain: true,
  messageId: 0,
  dup: false,
  properties: {
    userProperties: {
    },
  },
}

I traced the problem to getProperties method which returns false when userProperties is empty object {} because getLengthProperty for empty object returns 0 causing getProperties to return false:

if (!propValueLength) { return false }

Setting userProperties to something like {'f': 'u'} removes the issue.

We are using ClientMqtt from @nestjs/microservices to emit data to a topic.

const {
	MqttRecordBuilder,
	ClientProxyFactory,
	Transport
} = require('@nestjs/microservices');

const client = ClientProxyFactory.create({
	transport: Transport.MQTT,
	options: {
		url: 'mqtt://0.0.0.0:1883',	
		protocolVersion: 5,	
	}
})

const record = new MqttRecordBuilder()
	.setData(JSON.stringify({ ok: new Date().getTime() }))
	.setRetain(true)
	.build();

client.emit('protocol-version-debug', record);

It seems the ClientMqtt sets userProperties to empty object when no userProperties is provided:

https://github.com/nestjs/nest/blob/e4b07eb06b406183e708269e0439f442a1dadb1c/packages/microservices/client/client-mqtt.ts#L217

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions