koa-throttle2

3.1.8 • Public • Published

koa-throttle2

Description

Using the Koa framework, throttle the body of a response by specifying the rate or delay and chunk size. This middleware will stream the response chunk by chunk (size specified) with a specified delay in between in order to simulate a poor internet connection or latency.

Works with koa-static and should with any other middleware.

Installation

For Koa 3 use the next branch:

npm install --save koa-throttle2 

For older versions of Koa using generators:

$ npm install --save koa-throttle2@koa1v

Usage

const koa = require('koa');
const serve = require('koa-static');
 
const throttle = require('koa-throttle2');
let throttler = throttle({rate: 100, chunk: 2, debug: 1});
 
const app = new koa();
 
app.use(throttler);
 
app.use(serve(__dirname + '/public'));
app.use(function *test(next){
    this.body = 'This is a big test string that will be throttled';
  });
app.listen(4000);

Options

  • rate: the delay in milliseconds to wait before writing to the response
  • chunk: the number of bytes to write to the response stream every rate number of milliseconds
  • debug: If evalutes to true then a new line will be sent after each chunk of data written to the response so that you can see the data coming in if testing with curl. Default is false

Testing

Run: npm test

When testing your own server try using curl with the -v option: curl -v localhost:4000

Versions

Current Tags

Version History

Package Sidebar

Install

npm i koa-throttle2

Weekly Downloads

2

Version

3.1.8

License

MIT

Unpacked Size

7.38 kB

Total Files

6

Last publish

Collaborators

  • bradebarrows