Skip to content

Commit d642f8b

Browse files
committed
fix: replace router again to fix CORS preflights
1 parent f4d60ce commit d642f8b

File tree

7 files changed

+532
-27
lines changed

7 files changed

+532
-27
lines changed

example/routes/koa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const querystring = require('querystring');
22

33
const bodyParser = require('koa-body');
4-
const Router = require('koa-trie-router');
4+
const Router = require('koa-router');
55

66
const { renderError } = require('../../lib/helpers/defaults'); // make your own, you'll need it anyway
77
const Account = require('../support/account');

example/standalone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ let server;
5656
layout: '_layout',
5757
root: path.join(__dirname, 'views'),
5858
});
59-
provider.use(routes(provider).middleware());
59+
provider.use(routes(provider).routes());
6060
server = provider.listen(PORT, () => {
6161
console.log(`application is listening on port ${PORT}, check it's /.well-known/openid-configuration`);
6262
});

lib/helpers/initialize_app.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const assert = require('assert');
22
const querystring = require('querystring');
33

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

6+
const Router = require('../router');
77
const { homepage, version } = require('../../package.json');
88
const {
99
getAuthorization, getUserinfo, getToken, getCertificates, getRegistration, getRevocation,
@@ -83,7 +83,7 @@ module.exports = function initializeApp() {
8383
};
8484
const del = (name, route, ...stack) => {
8585
routerAssert(name, route, ...stack);
86-
router.del(route, namedRoute.bind(undefined, name), ensureOIDC, ...stack);
86+
router.delete(route, namedRoute.bind(undefined, name), ensureOIDC, ...stack);
8787
};
8888
const put = (name, route, ...stack) => {
8989
routerAssert(name, route, ...stack);
@@ -244,17 +244,9 @@ module.exports = function initializeApp() {
244244
app.use(error(this));
245245
app.use(async (ctx, next) => {
246246
await next();
247-
if (!router.isImplementedMethod(ctx.method)) {
248-
throw new InvalidRequest('not implemented', 501);
249-
}
250-
251247
if (ctx.status === 404 && ctx.message === 'Not Found') {
252-
throw new InvalidRequest('unrecognized route', 404);
253-
}
254-
255-
if (ctx.status === 405) {
256-
throw new InvalidRequest(`method ${ctx.method} not allowed on ${ctx.path}`, 405);
248+
throw new InvalidRequest(`unrecognized route or not allowed method (${ctx.method} on ${ctx.path})`, 404);
257249
}
258250
});
259-
app.use(router.middleware());
251+
app.use(router.routes());
260252
};

lib/router/index.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* istanbul ignore file */
2+
// https://github.com/steambap/koa-tree-router @ 0.4.4 / MIT
3+
// Modifications:
4+
// - code style (npm run lint-fix)
5+
// - fixed composed middleware rather then composing on every request
6+
// - removed 405 handling
7+
8+
const compose = require('koa-compose');
9+
10+
const Node = require('./tree');
11+
12+
const NOT_FOUND = { handle: null, params: [] };
13+
14+
class Router {
15+
constructor(opts = {}) {
16+
this.trees = {};
17+
this.opts = opts;
18+
}
19+
20+
on(method, path, ...middlewares) {
21+
if (!this.trees[method]) {
22+
this.trees[method] = new Node();
23+
}
24+
this.trees[method].addRoute(path, compose(middlewares));
25+
return this;
26+
}
27+
28+
get(...arg) {
29+
return this.on('GET', ...arg);
30+
}
31+
32+
put(...arg) {
33+
return this.on('PUT', ...arg);
34+
}
35+
36+
post(...arg) {
37+
return this.on('POST', ...arg);
38+
}
39+
40+
delete(...arg) {
41+
return this.on('DELETE', ...arg);
42+
}
43+
44+
options(...arg) {
45+
return this.on('OPTIONS', ...arg);
46+
}
47+
48+
find(method, path) {
49+
const tree = this.trees[method];
50+
if (tree) {
51+
return tree.search(path);
52+
}
53+
return NOT_FOUND;
54+
}
55+
56+
routes() {
57+
return (ctx, next) => {
58+
const { handle, params } = this.find(ctx.method, ctx.path);
59+
60+
if (!handle) {
61+
return next();
62+
}
63+
64+
ctx.params = {};
65+
params.forEach(({ key, value }) => {
66+
ctx.params[key] = value;
67+
});
68+
69+
return handle(ctx, next);
70+
};
71+
}
72+
}
73+
74+
module.exports = Router;

0 commit comments

Comments
 (0)