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

Sockets joining two messages. #53059

Open
arreehm opened this issue May 19, 2024 · 7 comments
Open

Sockets joining two messages. #53059

arreehm opened this issue May 19, 2024 · 7 comments

Comments

@arreehm
Copy link

arreehm commented May 19, 2024

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.

@arreehm
Copy link
Author

arreehm commented May 19, 2024

Do you want repo of whole this process?

@arreehm
Copy link
Author

arreehm commented May 19, 2024

+"\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.

@RedYetiDev
Copy link
Member

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

@arreehm
Copy link
Author

arreehm commented May 20, 2024

https://github.com/arreehm/socketConnector

I don't know if it's expected, but it mostly doesn't happen.

@arreehm
Copy link
Author

arreehm commented May 20, 2024

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 unicode symbol, but anyhow, it feels broken.

@arreehm
Copy link
Author

arreehm commented May 20, 2024

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.

@arreehm
Copy link
Author

arreehm commented May 20, 2024

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)
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants