Mayank Patel
Mayank Patel

Reputation: 376

How to find length of a stream in NodeJS?

I have a function call uploadStream(compressedStream) in my code where I am passing compressedStream as a parameter to the function but before that I need to determine the length of the compressedStream. Does anyone know how can I do that in NodeJS?

Upvotes: 1

Views: 787

Answers (2)

Mayank Patel
Mayank Patel

Reputation: 376

This worked for me:

            let size = 0
            compressedStream.on('data', (chunk) => {
              size = size + chunk.length
            })
            compressedStream.on('end', () => {
              console.log(size)
            })

Upvotes: 0

MHassan
MHassan

Reputation: 435

you can get length by getting stream chunks length on the "data" event

compressedStream.on('data', (chunk) => {
   console.log('Got %d characters of string data:', chunk.length);
});

Upvotes: 1

Related Questions