Skip to content
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

fix(cli):fix topic-alias-maximum error in cli connect #1444

Merged
merged 2 commits into from
Sep 28, 2023
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
5 changes: 4 additions & 1 deletion cli/src/lib/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@ const multisend = (
const sender = new Writable({
objectMode: true,
})
let count = 0
sender._write = (line, _enc, cb) => {
const { topic, opts, protobufPath, protobufMessageName, format } = pubOpts
count++
let omitTopic = opts.properties?.topicAlias && count >= 2
const publishMessage = processPublishMessage(line.trim(), protobufPath, protobufMessageName, format)
client.publish(topic, publishMessage, opts, cb)
client.publish(omitTopic ? '' : topic, publishMessage, opts, cb)
Comment on lines +98 to +104
Copy link
Member

@ysfscream ysfscream Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for? If a topic alias is set after a line break, do we no longer need to enter or use the original publish topic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need to send the topic and topic alias together for the first time. The second time, we can directly use the topic alias to send, and the topic is set to "". The current multi-line send will carry the topic and topic alias each time, which does not comply with the mqtt protocol specification.

}

client.on('connect', () => {
Expand Down
10 changes: 8 additions & 2 deletions cli/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ const parseConnectOptions = (
Object.entries(willProperties).filter(([_, v]) => v !== null && v !== undefined),
))
}

let optionsTempWorkAround
if (mqttVersion === 3) {
connectOptions.protocolId = 'MQIsdp'
} else if (mqttVersion === 5) {
Expand All @@ -274,9 +274,15 @@ const parseConnectOptions = (
connectOptions.properties = Object.fromEntries(
Object.entries(properties).filter(([_, v]) => v !== null && v !== undefined),
)
// Map options.properties.topicAliasMaximum to options.topicAliasMaximum, as that is where MQTT.js looks for it.
// TODO: remove after bug fixed in MQTT.js v5.
optionsTempWorkAround = Object.assign(
{ topicAliasMaximum: connectOptions.properties ? connectOptions.properties.topicAliasMaximum : undefined },
connectOptions,
)
}

return connectOptions
return optionsTempWorkAround || connectOptions
}

const parsePublishOptions = (options: PublishOptions) => {
Expand Down
Loading