| 1 |
Stream Meter |
| 2 |
============ |
| 3 |
|
| 4 |
[](https://nodei.co/npm/stream-meter/) |
| 5 |
|
| 6 |
[](https://david-dm.org/brycebaril/node-stream-meter/) |
| 7 |
[](https://david-dm.org/brycebaril/node-stream-meter#info=devDependencies/) |
| 8 |
|
| 9 |
Stream Meter is a... uh, meter for streams. |
| 10 |
|
| 11 |
It is a streams2 Transform stream that passes through content, but counts the number of bytes it forwards. |
| 12 |
|
| 13 |
However, give it a size in bytes and it will abort as soon as that threshold is passed. This is useful for capping your [hyperquest](http://npm.im/hyperquest) or http/https clients or servers content size. |
| 14 |
|
| 15 |
``` |
| 16 |
npm install stream-meter |
| 17 |
``` |
| 18 |
|
| 19 |
Examples: |
| 20 |
|
| 21 |
```javascript |
| 22 |
var meter = require("stream-meter") |
| 23 |
|
| 24 |
// make an un-capped meter |
| 25 |
var m = meter() |
| 26 |
process.stdin.pipe(m).pipe(process.stdout) |
| 27 |
setTimeout(function () { |
| 28 |
// Log how much we saw in a couple seconds. |
| 29 |
console.log(m.bytes) |
| 30 |
}, 2000) |
| 31 |
|
| 32 |
// this will abort (with an Error) in the frame where 1024 bytes is reached |
| 33 |
process.stdin.pipe(meter(1024)).pipe(process.stdout) |
| 34 |
|
| 35 |
// create a 1024 byte-capped meter |
| 36 |
var m = meter(1024) |
| 37 |
m.on("error", function (e) { |
| 38 |
// log the error but don't kill the process |
| 39 |
console.log(e.message) |
| 40 |
}) |
| 41 |
process.stdin.pipe(m).pipe(process.stdout) |
| 42 |
|
| 43 |
``` |
| 44 |
|
| 45 |
```javascript |
| 46 |
var hyperquest = require("hyperquest") |
| 47 |
|
| 48 |
var req = hyperquest("https://raw.github.com/mranney/node_redis/master/index.js") |
| 49 |
|
| 50 |
var meter = require("stream-meter")(1024) |
| 51 |
meter.on("error", function (e) { |
| 52 |
console.log(e.message) |
| 53 |
console.log("Read %s bytes", meter.bytes) |
| 54 |
}) |
| 55 |
|
| 56 |
|
| 57 |
req.pipe(meter).pipe(process.stderr) |
| 58 |
``` |
| 59 |
|
| 60 |
```bash |
| 61 |
$ node hypermeter.js 2> /dev/null |
| 62 |
Stream exceeded specified max of 1024 bytes. |
| 63 |
Read 7377 bytes |
| 64 |
``` |
| 65 |
|
| 66 |
Usage |
| 67 |
===== |
| 68 |
|
| 69 |
```javascript |
| 70 |
var meter = require("stream-meter") |
| 71 |
|
| 72 |
var stream = meter(size) |
| 73 |
stream.on("error", function (e) { |
| 74 |
// handle the meter aborting the stream |
| 75 |
}) |
| 76 |
|
| 77 |
// read the bytes processed by the meter and passed through to any subsequent streams. |
| 78 |
var size = stream.bytes |
| 79 |
``` |
| 80 |
|
| 81 |
See test/index.js for additional examples. |
| 82 |
|
| 83 |
Options |
| 84 |
======= |
| 85 |
|
| 86 |
size |
| 87 |
---- |
| 88 |
|
| 89 |
Size (in bytes) to trigger the stream to abort. It will complete whatever frame it aborted in, so the size streamed will still be >= size but no more than size + highWaterMark |
| 90 |
|
| 91 |
Properties |
| 92 |
========== |
| 93 |
|
| 94 |
bytes |
| 95 |
----- |
| 96 |
|
| 97 |
Number of bytes handled and passed through the meter. |
| 98 |
|
| 99 |
LICENSE |
| 100 |
======= |
| 101 |
|
| 102 |
MIT |