Skip to content
Browse files

refactor validateRoute

  • Loading branch information...
1 parent 0db0d3f commit a4a82122dbba23eb12bd7a1acc976cbbd7ab83f7 @cloudhead committed
Showing with 39 additions and 54 deletions.
  1. +39 −54 lib/journey.js
View
93 lib/journey.js
@@ -236,8 +236,8 @@ journey.Router.prototype = {
resolve: function (request, body, dispatcher) {
var that = this, allowedMethods = [];
-
- function validateRoute(route, cb) {
+
+ var validateRoute = function (route, cb) {
// Match the pattern with the url
var match = (function (pattern) {
var path = request.url.pathname;
@@ -252,68 +252,53 @@ journey.Router.prototype = {
// async constraint operations.
//
if (!Array.isArray(match)) {
- return match === null ? cb(null, false) : cb(match);
+ return match === null ? cb(null, false) : cb(match);
}
//
// Run through the specified constraints,
// asynchronously making sure everything passes.
//
- var constraints = Object.keys(route.constraints);
-
- //
- // (cloudhead): This is the crux of the refactor. You were only allowing for synchronous
- // constraint functions, which isn't going to work when you think about auth
- // and sessions. You can't guarentee that every user will be in-memory.
- //
- function checkConstraints() {
- if (constraints.length === 0) {
- return checkRoute();
- }
-
+ (function checkConstraints(constraints) {
var key = constraints.shift();
- if (key in that.constraints) {
- // Keep support for existing (< 0.2.9) synchronous constraints
- that.constraints[key](route.constraints[key], request, body) ? checkConstraints() : cb(null, false);
- } else if (typeof route.constraints[key] === 'function') {
- // If the constraint is a function then expect it to have a method signature:
- // asyncConstraint(request, body, callback);
- route.constraints[key](request, body, function (err) {
- if (err) return cb(err);
- checkConstraints();
- });
- } else {
- cb(new(Error)("constraint '" + key + "' not found."));
- }
- }
-
- checkConstraints();
-
- function checkRoute() {
- // If there is no handler for this route, return a new NotImplemented exception
- if (! ('handler' in route)) { return cb(new(NotImplemented)("unbound route")) }
-
- // Otherwise, validate the route method, and return accordingly
- if ((Array.isArray(route.method) && route.method.indexOf(request.method) !== -1) ||
- (route.method === request.method) || !route.method) {
- return cb(null, function (res, params) {
- var args = [];
- args.push(res);
- args.push.apply(args, match.slice(1).map(function (m) {
- return /^\d+$/.test(m) ? parseInt(m) : m;
- }));
- args.push(params);
- return route.handler.apply(this, args);
- });
+
+ if (key) {
+ if (typeof route.constraints[key] === 'function') {
+ // If the constraint is a function then expect it to have a method signature:
+ // asyncConstraint(request, body, callback);
+ route.constraints[key](request, body, function (err) {
+ if (err) return cb(err);
+ checkConstraints(constraints);
+ });
+ } else {
+ cb(new(Error)("constraint '" + key + "' not found."));
+ }
} else {
- if (allowedMethods.indexOf(route.method) === -1) {
- allowedMethods.push(route.method);
+ // If there is no handler for this route, return a new NotImplemented exception
+ if (! ('handler' in route)) { return cb(new(NotImplemented)("unbound route")) }
+
+ // Otherwise, validate the route method, and return accordingly
+ if ((Array.isArray(route.method) && route.method.indexOf(request.method) !== -1) ||
+ (route.method === request.method) || !route.method) {
+ return cb(null, function (res, params) {
+ var args = [];
+ args.push(res);
+ args.push.apply(args, match.slice(1).map(function (m) {
+ return /^\d+$/.test(m) ? parseInt(m) : m;
+ }));
+ args.push(params);
+ return route.handler.apply(this, args);
+ });
+ } else {
+ if (allowedMethods.indexOf(route.method) === -1) {
+ allowedMethods.push(route.method);
+ }
+ return cb(null, false);
}
- return cb(null, false);
}
- }
- }
-
+ })(Object.keys(route.constraints));
+ };
+
//
// Return the first matching route
//

0 comments on commit a4a8212

Please sign in to comment.
Something went wrong with that request. Please try again.