4

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.

Rodrigo Pereira
  • 1,834
  • 2
  • 17
  • 35
  • An alternative way of measuring the stream length is suggested by @robertklep in this Answer[https://stackoverflow.com/questions/31807073/node-busboy-get-file-size/31809962#31809962] – PravyNandas Mar 05 '21 at 04:09

1 Answers1

8

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.

Jonathan Ong
  • 19,927
  • 17
  • 79
  • 118