https://github.com/meyfa/measure-stream
Node streams that know their lengths.
https://github.com/meyfa/measure-stream
chunk length measure measure-stream nodejs size stream
Last synced: about 1 year ago
JSON representation
Node streams that know their lengths.
- Host: GitHub
- URL: https://github.com/meyfa/measure-stream
- Owner: meyfa
- License: mit
- Created: 2016-12-19T13:47:41.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2025-04-11T23:29:05.000Z (about 1 year ago)
- Last Synced: 2025-04-12T00:28:38.052Z (about 1 year ago)
- Topics: chunk, length, measure, measure-stream, nodejs, size, stream
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/measure-stream
- Size: 592 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# measure-stream
[](https://github.com/meyfa/measure-stream/actions/workflows/main.yml)
[](https://codeclimate.com/github/meyfa/measure-stream/test_coverage)
[](https://codeclimate.com/github/meyfa/measure-stream/maintainability)
A duplex (Transform) stream for Node.js that measures the data passing through it
and emits that info accordingly. The following properties are available:
* **chunks** - The number of processed chunks up until and including the one
that triggered the event.
* **totalLength** - The sum of all chunk lengths. Will be 0 if the chunks are
something other than strings or buffers.
## Install
```
npm i measure-stream
```
## Usage
```javascript
const MeasureStream = require('measure-stream')
const stream = new MeasureStream()
stream.on('measure', function (info) {
console.log('chunk count:', info.chunks)
console.log('total length:', info.totalLength)
})
// You can then use 'stream' as you normally would, e.g.
// ('source' is readable and 'target' is writable):
source.pipe(stream).pipe(target)
```
As you can see, just one additional `.pipe()` call required to make it work!
Additionally, the last `measurement` is always available as a stream property.
For example, if all you need is the total size after a stream has been
processed:
```javascript
stream.on('finish', function () {
const bytes = stream.measurements.totalLength
})
```