speed-limiter
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -38,6 +38,8 @@ const { TokenBucket } = require('limiter') | ||
// Note: rate = 0, means we should stop processing chunks | ||
// Note: this.chunksize can be stored as null | ||
if (typeof rate !== 'number' || rate < 0) throw new Error('Rate must be a number bigger than zero') | ||
rate = parseInt(rate) | ||
if (chunksize && (typeof chunksize !== 'number' || chunksize <= 0)) throw new Error('Chunksize must be bigger than zero') | ||
chunksize = chunksize || Math.max(rate / 10, 1) | ||
chunksize = chunksize || Math.max(parseInt(rate / 10), 1) | ||
chunksize = parseInt(chunksize) | ||
if (rate > 0 && chunksize > rate) throw new Error('Chunk size must be smaller than rate') | ||
@@ -55,2 +57,3 @@ | ||
const rate = this.getRate() | ||
chunksize = parseInt(chunksize) | ||
if (rate > 0 && chunksize > rate) throw new Error('Chunk size must be smaller than rate') | ||
@@ -57,0 +60,0 @@ this.chunksize = chunksize |
@@ -42,3 +42,3 @@ const { EventEmitter } = require('events') | ||
async _waitForPositiveRate () { | ||
/* async _waitForPositiveRate () { | ||
// Stop pushing chunks if rate is zero | ||
@@ -48,3 +48,3 @@ while (this._group.getRate() === 0 && !this._destroyed && this._areBothEnabled()) { | ||
} | ||
} | ||
} */ | ||
@@ -76,3 +76,3 @@ async _waitForTokens (amount) { | ||
async _throttleChunk (size) { | ||
/* async _throttleChunk (size) { | ||
// Stop pushing chunks if rate is zero | ||
@@ -85,3 +85,3 @@ await this._waitForPositiveRate() | ||
await this._waitForTokens(size) | ||
} | ||
} */ | ||
@@ -94,3 +94,3 @@ async _processChunk (chunk, done) { | ||
let slice = chunk.slice(pos, pos + chunksize) | ||
while (slice.length) { | ||
while (slice.length > 0) { | ||
// Check here again because we might be in the middle of a big chunk | ||
@@ -100,4 +100,14 @@ // with a lot of small slices | ||
try { | ||
await this._throttleChunk(slice.length) | ||
if (this._destroyed) return | ||
// WAIT FOR POSITIVE RATE | ||
// Stop pushing chunks if rate is zero | ||
while (this._group.getRate() === 0 && !this._destroyed && this._areBothEnabled()) { | ||
await wait(1000) // wait 1 second | ||
if (this._destroyed) return | ||
} | ||
// WAIT FOR TOKENS | ||
if (this._areBothEnabled() && !this._group.bucket.tryRemoveTokens(slice.length)) { | ||
await this._waitForTokens(slice.length) | ||
if (this._destroyed) return | ||
} | ||
} catch (err) { | ||
@@ -104,0 +114,0 @@ return done(err) |
{ | ||
"name": "speed-limiter", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Throttle the speed of streams", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17047
359