nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

Losing data while parsing it with toSting #4316

Closed loldormy closed 3 weeks ago

loldormy commented 6 months ago

Version

v19.6.1

Platform

Microsoft Windows NT 10.0.19045.0 x64

Subsystem

No response

What steps will reproduce the bug?

const buffer=[];

req.on("data",function(chunk){
  buffer.push(chunk);
 }
req.on("end",function(){
  //this will cause data to be lost often if its a really large String
  const missingData = buffer.toString()

  //this wouldnt cause data to be lost 
  const fullData = Buffer.concat(buffer).toString();
})

even if this is a wrong way of receiving data at least there should be an error I haven't found anything online so I spent several days wondering why there was data missing in the end, if you want to reproduce the bug use a large JSON string (my string had 500000 characters) in the post request and then try parsing it in the Node.js server

How often does it reproduce? Is there a required condition?

It sometimes works but sometimes doesnt it feels like it's random

What is the expected behavior? Why is that the expected behavior?

The expected behavior would be to receive the full data in the end of the request because it works with smaller JSON string

What do you see instead?

Instead I receive an invalid JSON string because data is missing or being lost while parsing

Additional information

Even if this is a wrong way of receiving data at least there should be an error

preveen-stack commented 6 months ago

Can you try buffer.toJSON

RedYetiDev commented 3 weeks ago

no response to @preveen-stack's suggestion