stream-size
is a non-destructive transformation for Node.js streams that adds a sizeInBytes
property to your streams.
It also allows you to limit streams exceeding a maximum size.
Usage
;;; ; // 10 MB ;
Use cases
Sometimes you need a remote resource content-length but the server does not send back that header. This usually happens when trying to upload a third-party resource to an S3 bucket.
In order to get the size of the resource you can pipe it to the local machine filesystem and get the size, however, if the resource is provided by a malicious actor you could end up filling your machine's disk and crashing the system.
To prevent that you need to stop downloading the resource when a maximum size is reached.
This package supports it, as well as directly returning the content-length of the input stream in a single operation.