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