Skip to content

Commit

Permalink
fix: replace router again to fix CORS preflights
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Feb 20, 2019
1 parent f4d60ce commit d642f8b
Show file tree
Hide file tree
Showing 7 changed files with 532 additions and 27 deletions.
2 changes: 1 addition & 1 deletion example/routes/koa.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const querystring = require('querystring');

const bodyParser = require('koa-body');
const Router = require('koa-trie-router');
const Router = require('koa-router');

const { renderError } = require('../../lib/helpers/defaults'); // make your own, you'll need it anyway
const Account = require('../support/account');
Expand Down
2 changes: 1 addition & 1 deletion example/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let server;
layout: '_layout',
root: path.join(__dirname, 'views'),
});
provider.use(routes(provider).middleware());
provider.use(routes(provider).routes());
server = provider.listen(PORT, () => {
console.log(`application is listening on port ${PORT}, check it's /.well-known/openid-configuration`);
});
Expand Down
16 changes: 4 additions & 12 deletions lib/helpers/initialize_app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const assert = require('assert');
const querystring = require('querystring');

const Router = require('koa-trie-router');
const getCors = require('@koa/cors');

const Router = require('../router');
const { homepage, version } = require('../../package.json');
const {
getAuthorization, getUserinfo, getToken, getCertificates, getRegistration, getRevocation,
Expand Down Expand Up @@ -83,7 +83,7 @@ module.exports = function initializeApp() {
};
const del = (name, route, ...stack) => {
routerAssert(name, route, ...stack);
router.del(route, namedRoute.bind(undefined, name), ensureOIDC, ...stack);
router.delete(route, namedRoute.bind(undefined, name), ensureOIDC, ...stack);
};
const put = (name, route, ...stack) => {
routerAssert(name, route, ...stack);
Expand Down Expand Up @@ -244,17 +244,9 @@ module.exports = function initializeApp() {
app.use(error(this));
app.use(async (ctx, next) => {
await next();
if (!router.isImplementedMethod(ctx.method)) {
throw new InvalidRequest('not implemented', 501);
}

if (ctx.status === 404 && ctx.message === 'Not Found') {
throw new InvalidRequest('unrecognized route', 404);
}

if (ctx.status === 405) {
throw new InvalidRequest(`method ${ctx.method} not allowed on ${ctx.path}`, 405);
throw new InvalidRequest(`unrecognized route or not allowed method (${ctx.method} on ${ctx.path})`, 404);
}
});
app.use(router.middleware());
app.use(router.routes());
};
74 changes: 74 additions & 0 deletions lib/router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* istanbul ignore file */
// https://github.com/steambap/koa-tree-router @ 0.4.4 / MIT
// Modifications:
// - code style (npm run lint-fix)
// - fixed composed middleware rather then composing on every request
// - removed 405 handling

const compose = require('koa-compose');

const Node = require('./tree');

const NOT_FOUND = { handle: null, params: [] };

class Router {
constructor(opts = {}) {
this.trees = {};
this.opts = opts;
}

on(method, path, ...middlewares) {
if (!this.trees[method]) {
this.trees[method] = new Node();
}
this.trees[method].addRoute(path, compose(middlewares));
return this;
}

get(...arg) {
return this.on('GET', ...arg);
}

put(...arg) {
return this.on('PUT', ...arg);
}

post(...arg) {
return this.on('POST', ...arg);
}

delete(...arg) {
return this.on('DELETE', ...arg);
}

options(...arg) {
return this.on('OPTIONS', ...arg);
}

find(method, path) {
const tree = this.trees[method];
if (tree) {
return tree.search(path);
}
return NOT_FOUND;
}

routes() {
return (ctx, next) => {
const { handle, params } = this.find(ctx.method, ctx.path);

if (!handle) {
return next();
}

ctx.params = {};
params.forEach(({ key, value }) => {
ctx.params[key] = value;
});

return handle(ctx, next);
};
}
}

module.exports = Router;
Loading

0 comments on commit d642f8b

Please sign in to comment.