2018-05-31 23:06:25 +02:00
|
|
|
const { Transform } = require('stream');
|
|
|
|
|
|
|
|
class Limiter extends Transform {
|
|
|
|
constructor(limit) {
|
|
|
|
super();
|
|
|
|
this.limit = limit;
|
|
|
|
this.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_transform(chunk, encoding, callback) {
|
|
|
|
this.length += chunk.length;
|
|
|
|
this.push(chunk);
|
|
|
|
if (this.length > this.limit) {
|
2019-03-06 19:31:50 +01:00
|
|
|
console.error('LIMIT', this.length, this.limit);
|
2018-05-31 23:06:25 +02:00
|
|
|
return callback(new Error('limit'));
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Limiter;
|