limiter.js 448 B

123456789101112131415161718192021
  1. const { Transform } = require('stream');
  2. class Limiter extends Transform {
  3. constructor(limit) {
  4. super();
  5. this.limit = limit;
  6. this.length = 0;
  7. }
  8. _transform(chunk, encoding, callback) {
  9. this.length += chunk.length;
  10. this.push(chunk);
  11. if (this.length > this.limit) {
  12. console.error('LIMIT', this.length, this.limit);
  13. return callback(new Error('limit'));
  14. }
  15. callback();
  16. }
  17. }
  18. module.exports = Limiter;