Skip to content

Commit

Permalink
fix: properly handle routes ending with a trailing slash (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Dec 16, 2019
1 parent 53f78b3 commit d8a3a67
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/helpers/initialize_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ module.exports = function initializeApp() {
const ensureOIDC = contextEnsureOidc(this);

const routeMap = new Map();
function routerAssert(name, route, ...stack) {
function normalizeRoute(name, route, ...stack) {
assert(typeof name === 'string' && name.charAt(0) !== '/', `invalid route name ${name}`);
assert(typeof route === 'string' && route.charAt(0) === '/', `invalid route ${route}`);
route = route.replace(/\/\//g, '/'); // eslint-disable-line no-param-reassign
stack.forEach((middleware) => assert.equal(typeof middleware, 'function'), 'invalid middleware');
routeMap.set(name, route);
return route;
}
async function ensureSessionSave(ctx, next) {
try {
Expand Down Expand Up @@ -78,23 +79,23 @@ module.exports = function initializeApp() {
};

const get = (name, route, ...stack) => {
routerAssert(name, route, ...stack);
route = normalizeRoute(name, route, ...stack); // eslint-disable-line no-param-reassign
router.get(route, namedRoute.bind(undefined, name), ensureOIDC, ensureSessionSave, ...stack);
};
const post = (name, route, ...stack) => {
routerAssert(name, route, ...stack);
route = normalizeRoute(name, route, ...stack); // eslint-disable-line no-param-reassign
router.post(route, namedRoute.bind(undefined, name), ensureOIDC, ensureSessionSave, ...stack);
};
const del = (name, route, ...stack) => {
routerAssert(name, route, ...stack);
route = normalizeRoute(name, route, ...stack); // eslint-disable-line no-param-reassign
router.delete(route, namedRoute.bind(undefined, name), ensureOIDC, ...stack);
};
const put = (name, route, ...stack) => {
routerAssert(name, route, ...stack);
route = normalizeRoute(name, route, ...stack); // eslint-disable-line no-param-reassign
router.put(route, namedRoute.bind(undefined, name), ensureOIDC, ...stack);
};
const options = (name, route, ...stack) => {
routerAssert(name, route, ...stack);
route = normalizeRoute(name, route, ...stack); // eslint-disable-line no-param-reassign
router.options(route, namedRoute.bind(undefined, name), ensureOIDC, ...stack);
};

Expand Down

0 comments on commit d8a3a67

Please sign in to comment.