| 1 |
module.exports = Meter |
| 2 |
|
| 3 |
var util = require("util") |
| 4 |
|
| 5 |
var Transform = require("stream").Transform |
| 6 |
|
| 7 |
if (!Transform) { |
| 8 |
Transform = require("readable-stream/transform") |
| 9 |
} |
| 10 |
|
| 11 |
function Meter(maxBytes) { |
| 12 |
if (!(this instanceof Meter)) return new Meter(maxBytes) |
| 13 |
Transform.call(this) |
| 14 |
|
| 15 |
this.bytes = 0 |
| 16 |
this.maxBytes = maxBytes || Number.MAX_VALUE |
| 17 |
} |
| 18 |
util.inherits(Meter, Transform) |
| 19 |
|
| 20 |
Meter.prototype._transform = function (chunk, encoding, cb) { |
| 21 |
this.bytes += chunk.length |
| 22 |
this.push(chunk) |
| 23 |
if (this.bytes > this.maxBytes) { |
| 24 |
return cb(new Error("Stream exceeded specified max of " + this.maxBytes + " bytes.")) |
| 25 |
} |
| 26 |
cb() |
| 27 |
} |