Is there a way of setting a mode or value in connect or express to simulate slow file uploads??
First, install node-limiter
then create a Transform
stream that will throttle any Stream
s:
var util = require('util');
var Transform = require('stream').Transform;
var TokenBucket = require('limiter').TokenBucket;
function BucketStream(rate, interval, parentBucket, options) {
Transform.call(this, options);
this.bucket = new TokenBucket(Number.POSITIVE_INFINITY, rate, interval, parentBucket);
}
util.inherits(BucketStream, Transform);
BucketStream.prototype._transform = function(chunk, encoding, callback) {
this.bucket.removeTokens(chunk.length, function(err) {
callback(err, chunk);
});
};
Then the request is a ReadableStream
:
var bucketStream = new BucketStream(1024 * 500, 'second'); // 500KB/sec
req.pipe(bucketStream);
Now read from bucketStream
(instead from req) as fast as you want, you'll only get 500KB/sec
I did this on top of my head so beware :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With