Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function ThrottleGroup(opts) {
if (!(this instanceof ThrottleGroup))
return new ThrottleGroup(opts);
opts = opts || {};
if (opts.rate === undefined)
throw new Error('throttle rate is a required argument');
if (typeof opts.rate !== 'number' || opts.rate <= 0)
throw new Error('throttle rate must be a positive number');
if (opts.chunksize !== undefined && (typeof opts.chunksize !== 'number' || opts.chunksize <= 0)) {
throw new Error('throttle chunk size must be a positive number');
}
this.rate = opts.rate;
this.chunksize = opts.chunksize || this.rate/10;
this.bucket = new TokenBucket(this.rate, this.rate, 'second', null);
}
function ThrottleStream (chunksPerSecond) {
var bucket = new TokenBucket(chunksPerSecond, chunksPerSecond, 'second', null)
return through2(function (chunk, _, next) {
bucket.removeTokens(1, function (err) {
next(err, chunk)
})
})
}