-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
TLS corrupting buffer? #42635
Comments
By the way, seems the same with latest LTS node version. |
In general when it comes to streams (tls or otherwise), you should never make assumptions about the chunks you will receive. You might get 1 byte, you might get 1000 bytes, you might get multi-byte characters split across chunks. There are a few solutions to this problem, depending on the data stream you're reading from, including:
|
I already have a setEncoding, however it doesn't encode correctly, it left some special characters that makes JSON.parse fail. |
@mscdex Btw, seems that it's failing to encode into utf8, not that it's sending incorrect buffer, but is there a way to save the raw buffer in the file so I can encode in another applications and see if it's a problem if my code or maybe a bug? |
If you're manually handling (e.g. not using an existing library/module) something like WebSockets, you need to first parse the packets appropriately according to the specification. If you're not already doing this, this is most likely the cause of the binary data from your socket. |
Hmmm, I tried looking ws/WebSocket parse function, I never understood what they were doing, but even not handling it correct, it is normal to tls send 2 socket responses? or it's just a thing I am also need to handle it, btw, I don't know to handle, but I believe it's something related to parse every "letter" of the buffer because of ws/Websocket loop event for parse it. Right? |
And the socket.setEncoding('utf8') shouldn't parse it correctly? |
If you're using |
Oh, nope, not using ws lol, I have trying to see how they parse it. |
WebSocket is not just a bare socket, it is a protocol complete with a well-defined packet format. You need to either read up on the WebSocket specification and parse this data yourself according to that specification or use a third party library/module (e.g. |
Version
17.7.2
Platform
Linux 47a7b9095f7d 5.13.0-1023-gcp #28~20.04.1-Ubuntu SMP Wed Mar 30 03:51:07 UTC 2022 x86_64 GNU/Linux
Subsystem
tls / https?
What steps will reproduce the bug?
Create a socket client with TLS and https
Make the socket server send 2 messages one after the another.
How often does it reproduce? Is there a required condition?
I don't think it so, but when it sends 2 messages at once, it corrupts the buffer and when you use .toString('utf8') shows 2 jsons instead of 1.
What is the expected behavior?
.toString('utf8') show only one json, then after some ms, show the another, executing the data event again.
What do you see instead?
Additional information
Nothing, I think.
The text was updated successfully, but these errors were encountered: