Skip to content

Commit

Permalink
ir: add
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Jul 12, 2016
1 parent dad23c7 commit 4731523
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 35 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
root: true

# globals
globals:
Promise: true
# globals:
# Promise: true

# code env
env:
node: true
mocha: true
es6: true

# rules
rules:
Expand Down
26 changes: 10 additions & 16 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ METHODS.concat('all').forEach(function(method) {
}

// compose
if (fns.length === 1) {
fn = fns[0];
} else {
fn = compose(fns);
}
if (fns.length === 1) fn = fns[0];
else fn = compose(fns);

// simple stack item with `method` & fn
const layer = {
Expand Down Expand Up @@ -115,17 +112,14 @@ Route.prototype.dispatch = co.wrap(function*(ctx, next) {
layer = stack[idx++];

// we find a match
if (layer && (route.methods.all || layer.method === method)) {
break;
} else { // not match

// end Route
if (idx === stack.length) {
yield next(); // end of route, find next route
return;
} else {
continue;
}
if (layer && (route.methods.all || layer.method === method)) break;

// end Route
if (idx === stack.length) {
yield next(); // end of route, find next route
return;
} else {
continue;
}
}

Expand Down
23 changes: 6 additions & 17 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@ proto.init = function() {

// build automatic `options` response
router.use(co.wrap(function* allowedMethods(ctx, next) {
if (ctx.method === 'OPTIONS') {
if (!ctx.allowedMethods) {
ctx.allowedMethods = []; // attcch `allowedMethods` on context
}
if (ctx.method === 'OPTIONS' && !ctx.allowedMethods) {
ctx.allowedMethods = new Set();
}

yield next();

if (ctx.method === 'OPTIONS' && ctx.allowedMethods.length) {
const allow = ctx.allowedMethods.join(',');
const allow = Array.from(ctx.allowedMethods).join(',');
ctx.set('Allow', allow);
ctx.body = allow;
}
Expand Down Expand Up @@ -105,13 +103,7 @@ proto.dispatch = co.wrap(function*(ctx) {
if (layer.route._handlesMethod(ctx.method)) break;

// add current method to `ctx.allowedMethods`
if (ctx.method === 'OPTIONS') {
layer.route._options().forEach(function(m) {
if (ctx.allowedMethods.indexOf(m) === -1) {
ctx.allowedMethods.push(m);
}
});
}
if (ctx.method === 'OPTIONS') layer.route._options().forEach(m => ctx.allowedMethods.add(m));
}

// if no layer match
Expand Down Expand Up @@ -183,11 +175,8 @@ proto.use = function(path) {
}

// compose
if (fns.length === 1) {
fn = fns[0];
} else {
fn = compose(fns);
}
if (fns.length === 1) fn = fns[0];
else fn = compose(fns);

const layer = new Layer(path, {
end: false,
Expand Down

0 comments on commit 4731523

Please sign in to comment.