amqp-node / amqplib

AMQP 0-9-1 library and client for Node.JS

Home Page:https://amqp-node.github.io/amqplib/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

channel.publish treats undefined exchange as an empty string

adar-h-healthy opened this issue · comments

Hi,

Given the API documentation, I'd expect that channel.publish will fail if exchange = undefined.
However, it looks like the library treats undefined as '', and therefore publish the message to a queue with the name of the routing key (even if it doesn't exist).
Is this behavior intentional? I'd expect that undefined will be treated the same as non-existing exchange - which will fail and close the channel.

TIA.

Hi @adar-h-healthy,

I suspect you're right, but I will test later with undefined and other falsey values to make sure, then make a call on what to do. I suspect the most pragmatic way forward will be to update the documentation rather than raise an error, thereby potentially breaking existing applications.

That was slightly more interesting that I expected...

exchange outcome
"" published to default exchange
undefined published to default exchange
null TypeError: Field 'exchange' is the wrong type; must be a string (up to 255 chars)
false TypeError: Field 'exchange' is the wrong type; must be a string (up to 255 chars)
0 TypeError: Field 'exchange' is the wrong type; must be a string (up to 255 chars)

The code which does the check is in the build time generated defs.js file

if (void 0 === val) val = ""; else if (!("string" == typeof val && Buffer.byteLength(val) < 256)) throw new TypeError("Field 'exchange' is the wrong type; must be a string (up to 255 chars)");

void 0 equals undefined and so changes val to the empty string. I'll updated the documentation shortly.

Documents updated. Unfortunately it looks like the gh-pages styles have changed so now the formatting is screwed up, but that's a separate issue!

Thanks @cressie176, this is much appreciated!
FWIW, we’ve added a check before calling channel.publish to make sure the exchange variable is defined.