stream_size_limit.js 570 B

1234567891011121314151617181920212223242526
  1. const stream = require('stream')
  2. /**
  3. * Transform stream that stops passing bytes through after some threshold has
  4. * been reached.
  5. */
  6. class StreamSizeLimit extends stream.Transform {
  7. constructor(maxSize) {
  8. super()
  9. this.maxSize = maxSize
  10. this.accumulatedSize = 0
  11. this.sizeLimitExceeded = false
  12. }
  13. _transform(chunk, encoding, cb) {
  14. this.accumulatedSize += chunk.length
  15. if (this.accumulatedSize > this.maxSize) {
  16. this.sizeLimitExceeded = true
  17. } else {
  18. this.push(chunk)
  19. }
  20. cb()
  21. }
  22. }
  23. module.exports = StreamSizeLimit