Merge pull request #484 from braydonf/ratelimiter-options
index: configurable ratelimiter options
This commit is contained in:
commit
7db7b1214b
16
lib/index.js
16
lib/index.js
@ -46,6 +46,9 @@ var InsightAPI = function(options) {
|
|||||||
this.cacheShortSeconds = options.cacheShortSeconds;
|
this.cacheShortSeconds = options.cacheShortSeconds;
|
||||||
this.cacheLongSeconds = options.cacheLongSeconds;
|
this.cacheLongSeconds = options.cacheLongSeconds;
|
||||||
|
|
||||||
|
this.rateLimiterOptions = options.rateLimiterOptions;
|
||||||
|
this.disableRateLimiter = options.disableRateLimiter;
|
||||||
|
|
||||||
this.blockSummaryCacheSize = options.blockSummaryCacheSize || BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE;
|
this.blockSummaryCacheSize = options.blockSummaryCacheSize || BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE;
|
||||||
this.blockCacheSize = options.blockCacheSize || BlockController.DEFAULT_BLOCK_CACHE_SIZE;
|
this.blockCacheSize = options.blockCacheSize || BlockController.DEFAULT_BLOCK_CACHE_SIZE;
|
||||||
|
|
||||||
@ -116,13 +119,22 @@ InsightAPI.prototype.getRemoteAddress = function(req) {
|
|||||||
return req.socket.remoteAddress;
|
return req.socket.remoteAddress;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
InsightAPI.prototype._getRateLimiter = function() {
|
||||||
|
var rateLimiterOptions = _.isUndefined(this.rateLimiterOptions) ? {} : _.clone(this.rateLimiterOptions);
|
||||||
|
rateLimiterOptions.node = this.node;
|
||||||
|
var limiter = new RateLimiter(rateLimiterOptions);
|
||||||
|
return limiter;
|
||||||
|
};
|
||||||
|
|
||||||
InsightAPI.prototype.setupRoutes = function(app) {
|
InsightAPI.prototype.setupRoutes = function(app) {
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
//Enable rate limiter
|
//Enable rate limiter
|
||||||
var limiter = new RateLimiter({node: this.node});
|
if (!this.disableRateLimiter) {
|
||||||
app.use(limiter.middleware());
|
var limiter = this._getRateLimiter();
|
||||||
|
app.use(limiter.middleware());
|
||||||
|
}
|
||||||
|
|
||||||
//Setup logging
|
//Setup logging
|
||||||
morgan.token('remote-forward-addr', function(req){
|
morgan.token('remote-forward-addr', function(req){
|
||||||
|
|||||||
@ -5,6 +5,39 @@ var sinon = require('sinon');
|
|||||||
var InsightAPI = require('../lib/index');
|
var InsightAPI = require('../lib/index');
|
||||||
|
|
||||||
describe('Index', function() {
|
describe('Index', function() {
|
||||||
|
describe('@constructor', function() {
|
||||||
|
it('will set rate limiter options', function() {
|
||||||
|
var options = {};
|
||||||
|
var node = {};
|
||||||
|
var index = new InsightAPI({
|
||||||
|
rateLimiterOptions: options,
|
||||||
|
node: node
|
||||||
|
});
|
||||||
|
index.rateLimiterOptions.should.equal(options);
|
||||||
|
});
|
||||||
|
it('will set disable rate limiter option', function() {
|
||||||
|
var node = {};
|
||||||
|
var index = new InsightAPI({
|
||||||
|
disableRateLimiter: true,
|
||||||
|
node: node
|
||||||
|
});
|
||||||
|
index.disableRateLimiter.should.equal(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('#_getRateLimiter', function() {
|
||||||
|
it('will pass options to rate limiter', function() {
|
||||||
|
var options = {
|
||||||
|
whitelist: ['127.0.0.1']
|
||||||
|
};
|
||||||
|
var node = {};
|
||||||
|
var index = new InsightAPI({
|
||||||
|
rateLimiterOptions: options,
|
||||||
|
node: node
|
||||||
|
});
|
||||||
|
var limiter = index._getRateLimiter();
|
||||||
|
limiter.whitelist.should.eql(['127.0.0.1']);
|
||||||
|
});
|
||||||
|
});
|
||||||
describe('#cache', function() {
|
describe('#cache', function() {
|
||||||
it('will set cache control header', function(done) {
|
it('will set cache control header', function(done) {
|
||||||
var node = {
|
var node = {
|
||||||
@ -131,4 +164,46 @@ describe('Index', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('#setupRoutes', function() {
|
||||||
|
it('will use rate limiter by default', function() {
|
||||||
|
var node = {};
|
||||||
|
var index = new InsightAPI({
|
||||||
|
node: node
|
||||||
|
});
|
||||||
|
var middlewareFunc = sinon.stub();
|
||||||
|
var middleware = sinon.stub().returns(middlewareFunc);
|
||||||
|
var limiter = {
|
||||||
|
middleware: middleware
|
||||||
|
};
|
||||||
|
index._getRateLimiter = sinon.stub().returns(limiter);
|
||||||
|
var use = sinon.stub();
|
||||||
|
var app = {
|
||||||
|
use: use,
|
||||||
|
get: sinon.stub(),
|
||||||
|
param: sinon.stub(),
|
||||||
|
post: sinon.stub()
|
||||||
|
};
|
||||||
|
index.setupRoutes(app);
|
||||||
|
use.callCount.should.be.above(0);
|
||||||
|
use.args[0][0].should.equal(middlewareFunc);
|
||||||
|
middleware.callCount.should.equal(1);
|
||||||
|
});
|
||||||
|
it('will NOT use rate limiter if disabled', function() {
|
||||||
|
var node = {};
|
||||||
|
var index = new InsightAPI({
|
||||||
|
node: node,
|
||||||
|
disableRateLimiter: true
|
||||||
|
});
|
||||||
|
index._getRateLimiter = sinon.stub();
|
||||||
|
var use = sinon.stub();
|
||||||
|
var app = {
|
||||||
|
use: use,
|
||||||
|
get: sinon.stub(),
|
||||||
|
param: sinon.stub(),
|
||||||
|
post: sinon.stub()
|
||||||
|
};
|
||||||
|
index.setupRoutes(app);
|
||||||
|
index._getRateLimiter.callCount.should.equal(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user