Sockets joining two messages.
Version
18.20.2
Platform
Linux # 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
No response
What steps will reproduce the bug?
You need to use socket, in a way, that you create middle man server, handling more socket connections. Then when you send message trough middleman, and message from middleman, it ends up with joining two different net.server.write's to one statement that is received by target application.
Middle man is passing message "connectionEstablished" to both ends of socket pipe, then I recieve
{"context":"connectionEstablished"}{"context":"test","data":"whatever"}
In the application, because it joined data with what application sends, it's two totally different writes called.
How often does it reproduce? Is there a required condition?
Every time a send a socket message trough middle man.
What is the expected behavior? Why is that the expected behavior?
I expect it to be two different messages per two different socket.write calls.
What do you see instead?
{"context":"connectionEstablished"}{"context":"test","data":"whatever"}
I expect it to be two different messages.
Additional information
Am I missing something? If described behaviour is expected, how to differentiate between specific message at end point? Like I need to rewrite JSON.parse because of that, which seems to be utterly wrong.
Do you want repo of whole this process?
+"\u0004" at write, and parsing on recieving socket solves the problem, but IMO it should be automatic, so reclassify this as feature request if you wish.
Do you want repo of whole this process?
Yes, providing reproduction steps (and code) is the best way for people to see and understand what went wrong
https://github.com/arreehm/socketConnector
I don't know if it's expected, but it mostly doesn't happen.
Like I understand, if it's sends in a buffer at one time it's two different thing, but I never managed to break the JSON.parse being in on('data', like that, I fixed it with <EOT> unicode symbol, but anyhow, it feels broken.
Now I see, that I should write my own encoding, where there is size of JSON specified and read up buffer up to that length, but it takes away ease of javascript, I expected it to be done by default.
Solution I have is to encode/decode JSON String by following thing:
const { Buffer } = require('node:buffer')
const encode = (string, encoding)=>{
string = Buffer.from(string, encoding)
let length = string.byteLength
length = Buffer.from(`<${length}>`)
let buffer = Buffer.concat([length, string])
return buffer
}
const decode = (buffer, encoding, perChunk)=>{
let stop = false
while(!stop) {
let index = buffer.indexOf('>')
let sub = buffer.subarray(1, index)
let length = Number(sub.toString())
let ourChunk = buffer.subarray(index+1, length+index+1)
perChunk(ourChunk.toString())
buffer = buffer.subarray(length+index+1)
if(buffer.byteLength===0) stop = true
}
}
module.exports = (encoding)=>{
return {
encode: (string)=>{
return encode(string, encoding)
},
decode: (buffer, perChunk)=>{
return decode(buffer, encoding, perChunk)
}
}
}