node-stomp-client icon indicating copy to clipboard operation
node-stomp-client copied to clipboard

Fix incorrect encoding of binary data.

Open romank8k opened this issue 8 years ago • 0 comments

This fix allows to send binary data over STOMP.

When sending binary data...converting it from buffer to string could mangle it. Some example code below to demonstrate the problem. Note that when converting myBuffer to a string, it defaults to Unicode Codepoint 65533 - which is the question mark character - I think that's what node does by default when it tries to convert invalid UTF-8 binary data.

const myBuffer = Buffer.from([0xFB]);  // This is not a valid UTF-8 encoding, and so gets mangled when converting from a buffer (assumed to be valid UTF-8) to a string...

const str = myBuffer.toString();
console.log("STR Code Point: " + str.codePointAt(0));
const newBuffer = Buffer.from(str, "utf8");

console.log("myBuffer len: " + myBuffer.length);
console.log("newBuffer len: " + newBuffer.length);
for (let i = 0; i < newBuffer.length; i++) {
  if (i < myBuffer.length) {
    console.log("A: " + myBuffer[i]);
  }
  console.log("B: " + newBuffer[i]);
}

romank8k avatar Oct 30 '17 21:10 romank8k