Skip to content

Commit

Permalink
Moving rate() into keymaster() such that it always executes
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Nov 5, 2014
1 parent 4f021c2 commit 62242da
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 85 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 0.9.18
- Moved `rate()` into `keymaster()` such that it always executes

## 0.9.17
- Added `rate.override` to `config` which accepts `req` & `rate`, and must return `rate`, such that rate limiting is customizable

Expand Down
2 changes: 1 addition & 1 deletion doc/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,7 @@ <h5>Returns:</h5>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2014-11-05T07:38:55-05:00 using the <a
on 2014-11-05T08:03:07-05:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ <h2>License</h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2014-11-05T07:38:55-05:00 using the <a
on 2014-11-05T08:03:07-05:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
4 changes: 2 additions & 2 deletions doc/module-tenso.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ <h2>
<dt class="tag-version">Version:</dt>
<dd class="tag-version">
<ul class="dummy">
<li>0.9.17</li>
<li>0.9.18</li>
</ul>
</dd>

Expand Down Expand Up @@ -218,7 +218,7 @@ <h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2014-11-05T07:38:55-05:00 using the <a
on 2014-11-05T08:03:07-05:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion doc/modules.list.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ <h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2014-11-05T07:38:55-05:00 using the <a
on 2014-11-05T08:03:07-05:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
76 changes: 35 additions & 41 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* @license BSD-3 <https://raw.github.com/avoidwork/tenso/master/LICENSE>
* @link http://avoidwork.github.io/tenso
* @module tenso
* @version 0.9.17
* @version 0.9.18
*/
( function () {
"use strict";

var turtleio = require( "turtle.io" ),
SERVER = "tenso/0.9.17",
SERVER = "tenso/0.9.18",
CONFIG = require( __dirname + "/../config.json" ),
keigai = require( "keigai" ),
util = keigai.util,
Expand Down Expand Up @@ -64,7 +64,7 @@ function Tenso () {
this.rates = {};
this.server = turtleio();
this.server.tenso = this;
this.version = "0.9.17";
this.version = "0.9.18";
}

/**
Expand Down Expand Up @@ -671,17 +671,9 @@ function bootstrap ( obj, config ) {
arguments[2]();
}

function rateLimit ( req, res, next ) {
rate( obj, req, res, next );
}

obj.server.use( mediator ).blacklist( mediator );
obj.server.use( parse ).blacklist( parse );

if ( config.rate.enabled ) {
obj.server.use( rateLimit ).blacklist( rateLimit );
}

// Bootstrapping configuration
config = auth( obj, config );
config.headers = config.headers || {};
Expand Down Expand Up @@ -918,38 +910,20 @@ function hypermedia ( server, req, rep, headers ) {
* @return {Undefined} undefined
*/
function keymaster ( req, res, next ) {
var obj, method, result, routes, uri, valid;
var obj = req.server.tenso,
method, result, routes, uri, valid;

// No authentication, or it's already happened
if ( !req.protect || !req.protectAsync || ( req.session && req.isAuthenticated() ) ) {
obj = req.server.tenso;
method = REGEX_GETREWRITE.test( req.method ) ? "get" : req.method.toLowerCase();
routes = req.server.config.routes[method] || {};
uri = req.parsed.pathname;
valid = false;

if ( uri in routes ) {
result = routes[uri];

if ( typeof result == "function" ) {
result.call( obj, req, res );
}
else {
obj.respond( req, res, result );
}
}
else {
iterate( routes, function ( value, key ) {
var regex = new RegExp( "^" + key + "$", "i" );

if ( regex.test( uri ) ) {
result = value;

return false;
}
} );
rate( obj, req, res, function () {
if ( uri in routes ) {
result = routes[ uri ];

if ( result ) {
if ( typeof result == "function" ) {
result.call( obj, req, res );
}
Expand All @@ -958,27 +932,47 @@ function keymaster ( req, res, next ) {
}
}
else {
iterate( req.server.config.routes.get || {}, function ( value, key ) {
iterate( routes, function ( value, key ) {
var regex = new RegExp( "^" + key + "$", "i" );

if ( regex.test( uri ) ) {
valid = true;
result = value;

return false;
}
} );

if ( valid ) {
obj.error( req, res, 405 );
if ( result ) {
if ( typeof result == "function" ) {
result.call( obj, req, res );
}
else {
obj.respond( req, res, result );
}
}
else {
obj.error( req, res, 404 );
iterate( req.server.config.routes.get || {}, function ( value, key ) {
var regex = new RegExp( "^" + key + "$", "i" );

if ( regex.test( uri ) ) {
valid = true;

return false;
}
} );

if ( valid ) {
obj.error( req, res, 405 );
}
else {
obj.error( req, res, 404 );
}
}
}
}
} );
}
else {
next();
rate( obj, req, res, next );
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tenso",
"description": "Tensō is a REST API facade for node.js, designed to simplify the implementation of APIs.",
"version": "0.9.17",
"version": "0.9.18",
"homepage": "http://avoidwork.github.io/tenso",
"author": {
"name": "Jason Mulligan",
Expand Down
8 changes: 0 additions & 8 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,9 @@ function bootstrap ( obj, config ) {
arguments[2]();
}

function rateLimit ( req, res, next ) {
rate( obj, req, res, next );
}

obj.server.use( mediator ).blacklist( mediator );
obj.server.use( parse ).blacklist( parse );

if ( config.rate.enabled ) {
obj.server.use( rateLimit ).blacklist( rateLimit );
}

// Bootstrapping configuration
config = auth( obj, config );
config.headers = config.headers || {};
Expand Down
62 changes: 32 additions & 30 deletions src/keymaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,20 @@
* @return {Undefined} undefined
*/
function keymaster ( req, res, next ) {
var obj, method, result, routes, uri, valid;
var obj = req.server.tenso,
method, result, routes, uri, valid;

// No authentication, or it's already happened
if ( !req.protect || !req.protectAsync || ( req.session && req.isAuthenticated() ) ) {
obj = req.server.tenso;
method = REGEX_GETREWRITE.test( req.method ) ? "get" : req.method.toLowerCase();
routes = req.server.config.routes[method] || {};
uri = req.parsed.pathname;
valid = false;

if ( uri in routes ) {
result = routes[uri];
rate( obj, req, res, function () {
if ( uri in routes ) {
result = routes[ uri ];

if ( typeof result == "function" ) {
result.call( obj, req, res );
}
else {
obj.respond( req, res, result );
}
}
else {
iterate( routes, function ( value, key ) {
var regex = new RegExp( "^" + key + "$", "i" );

if ( regex.test( uri ) ) {
result = value;

return false;
}
} );

if ( result ) {
if ( typeof result == "function" ) {
result.call( obj, req, res );
}
Expand All @@ -48,26 +30,46 @@ function keymaster ( req, res, next ) {
}
}
else {
iterate( req.server.config.routes.get || {}, function ( value, key ) {
iterate( routes, function ( value, key ) {
var regex = new RegExp( "^" + key + "$", "i" );

if ( regex.test( uri ) ) {
valid = true;
result = value;

return false;
}
} );

if ( valid ) {
obj.error( req, res, 405 );
if ( result ) {
if ( typeof result == "function" ) {
result.call( obj, req, res );
}
else {
obj.respond( req, res, result );
}
}
else {
obj.error( req, res, 404 );
iterate( req.server.config.routes.get || {}, function ( value, key ) {
var regex = new RegExp( "^" + key + "$", "i" );

if ( regex.test( uri ) ) {
valid = true;

return false;
}
} );

if ( valid ) {
obj.error( req, res, 405 );
}
else {
obj.error( req, res, 404 );
}
}
}
}
} );
}
else {
next();
rate( obj, req, res, next );
}
}

0 comments on commit 62242da

Please sign in to comment.