Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the content-length of a readable stream in nodejs?

Tags:

stream

node.js

I need to know the content-length of a readable stream. How can I do it?

I know about fs.stat but I am developing an API that knowing the content-length from a readable stream would be much simpler.

like image 322
Rodrigo Pereira Avatar asked Oct 15 '25 15:10

Rodrigo Pereira


1 Answers

the idea behind the stream is that you start working on data before the source finishes. thus, you don't know the content-length.

if the underlying source is an HTTP object, then you could check stream.headers['content-length'], but even that is not ultimately reliable as the client or the server could have lied.

in short, you can't. personally, i'd rather just store it on disk temporarily.

like image 61
Jonathan Ong Avatar answered Oct 18 '25 06:10

Jonathan Ong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!