Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expressjs/connect - Simulate slow file upload

Is there a way of setting a mode or value in connect or express to simulate slow file uploads??

like image 370
user1750709 Avatar asked Oct 16 '12 16:10

user1750709


1 Answers

First, install node-limiter then create a Transform stream that will throttle any Streams:

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 :)

like image 62
Mike Gleason jr Couturier Avatar answered Oct 30 '22 16:10

Mike Gleason jr Couturier