Skip to content

[Bug]: TypeError when setting Packet Property to undefined #152

Closed
@SoulKa

Description

@SoulKa

MQTTjs Version

5.10.3

Broker

mosquitto 2.0.20

Environment

NodeJS

Description

Having a packet property set to undefined results in a TypeError:

TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received undefined
    at Function.byteLength (node:buffer:777:11)
    at getLengthProperty (/node_modules/mqtt-packet/writeToStream.js:929:30)
    at getProperties (/node_modules/mqtt-packet/writeToStream.js:1003:27)
    at publish (/node_modules/mqtt-packet/writeToStream.js:350:22)
    at Object.generate [as writeToStream] (/node_modules/mqtt-packet/writeToStream.js:35:14)
    at MqttClient._writePacket (/node_modules/mqtt/src/lib/client.ts:1961:29)
    at <anonymous> (/node_modules/mqtt/src/lib/client.ts:1862:9)
    at Store.put (/node_modules/mqtt/src/lib/store.ts:81:4)
    at MqttClient._storeAndSend (/node_modules/mqtt/src/lib/client.ts:1857:22)
    at MqttClient._sendPacket (/node_modules/mqtt/src/lib/client.ts:2040:10) {
  code: 'ERR_INVALID_ARG_TYPE'
}

This is allowed by the IClientPublishOptions interface and can easily happen if optional properties are passed along into the publish() method of the MqttClient.

Minimal Reproduction

    await client.publishAsync('somewhere', '', {
      properties: {
        correlationData: undefined
      }
    });

Debug logs

  mqttjs:client publish :: message `{"value":205}` to topic `somewhere` +0ms
  mqttjs:client publish :: qos 1 +0ms
  mqttjs:client MqttClient:publish: packet cmd: publish +0ms
  mqttjs:client _sendPacket :: (CLIENT) ::  start +0ms
  mqttjs:client storeAndSend :: store packet with cmd publish to outgoingStore +0ms
  mqttjs:client _removeTopicAliasAndRecoverTopicName :: alias NaN, topic 'somewhere' +0ms
  mqttjs:client noop :: undefined +0ms
  mqttjs:client _writePacket :: packet: {
  mqttjs:client   cmd: 'publish',
  mqttjs:client   topic: 'somewhere',
  mqttjs:client   payload: '{"value":205}',
  mqttjs:client   qos: 1,
  mqttjs:client   retain: true,
  mqttjs:client   messageId: 10561,
  mqttjs:client   dup: false,
  mqttjs:client   properties: {
  mqttjs:client     contentType: 'application/json; charset=utf-8',
  mqttjs:client     correlationData: undefined
  mqttjs:client   }
  mqttjs:client } +0ms
  mqttjs:client _writePacket :: emitting `packetsend` +0ms
  mqttjs:client _writePacket :: writing to stream +0ms

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions