Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sumolari/stream-size
Get the size of a stream and abort it if threshold is reached
https://github.com/sumolari/stream-size
content-length nodejs stream-processing typescript
Last synced: 24 days ago
JSON representation
Get the size of a stream and abort it if threshold is reached
- Host: GitHub
- URL: https://github.com/sumolari/stream-size
- Owner: Sumolari
- Created: 2020-08-02T07:20:03.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-30T04:45:15.000Z (3 months ago)
- Last Synced: 2024-09-18T17:12:06.532Z (about 2 months ago)
- Topics: content-length, nodejs, stream-processing, typescript
- Language: TypeScript
- Homepage:
- Size: 715 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
[![npm](https://img.shields.io/npm/v/stream-size)](https://www.npmjs.com/package/stream-size)
[![Maintainability](https://api.codeclimate.com/v1/badges/ed74c7c06962544829cf/maintainability)](https://codeclimate.com/github/Sumolari/stream-size/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/ed74c7c06962544829cf/test_coverage)](https://codeclimate.com/github/Sumolari/stream-size/test_coverage)
[![NPM](https://img.shields.io/npm/l/stream-size)](https://www.npmjs.com/package/stream-size)`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
```ts
import { Readable } from 'stream';
import { createWriteStream } from 'fs';
import getSizeTransform from 'stream-size';const MAX_SIZE_ALLOWED_IN_BYTES = 10 * 1024 * 1024; // 10 MB
const downloadResource = (resourceInputStream: Readable) => {
// Note that no maximum size is required, if you don't pass a value the stream won't be limited.
const pipedStream = resourceInputStream.pipe(
getSizeTransform(MAX_SIZE_ALLOWED_IN_BYTES),
);const localFileWriteStream = createWriteStream('temp-file');
pipedStream.pipe(localFileWriteStream);localFileWriteStream.on('close', () => {
console.log(
`Input resource completed read! It has ${pipedStream.sizeInBytes} bytes.`,
);
// Now you can do something with 'tmp-file' if you want
});
};
```## 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.