Node.js Streams readable.readableLength not returning file size

Viewed 561

I have been trying to upload a file by creating a readStream with fs.createReadStream(filePath) and piping to a writeStream. I need the file size to implement a progress bar.

In docs I see there is the readable.readableLength field, which to my understanding returns the file size but it returns zero in my case. I already got the file size with fs.statSync() but was curious.

The explanation in the documents was not really clear for me so I wanted to ask what readable.readableLength field represents? If it is the file size why it is valued zero?

1 Answers

readableLenght represents the amount of bytes in the stream that can still be read from it. If it is zero, the stream has probably already been read and thus there are zero bytes left for reading.

Related