Skip to content

Commit

Permalink
feat(find-route): performance improvement, bug-fix and more
Browse files Browse the repository at this point in the history
  • Loading branch information
dalisoft committed Jun 4, 2021
1 parent b7e982f commit c6ed3e1
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/find-route.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable max-lines */
import fastDecodeURI from 'fast-decode-uri-component';
import { pathToRegexp } from 'path-to-regexp';

Expand Down Expand Up @@ -25,13 +27,17 @@ export default class FindRoute {
};
}

// eslint-disable-next-line class-methods-use-this
parse(route) {
// Initialize default config
route.all = false;
route.regex = false;
route.fetch_params = false;

// eslint-disable-next-line class-methods-use-this, max-lines-per-function
parse(incomingRoute) {
const route = {
...incomingRoute,
all: false,
regex: false,
fetch_params: false,
async: false,
await: false,
legacy: false
};
if (typeof route.path === 'string') {
route.path = fastDecodeURI(route.path);
if (route.path === '*' || route.path === '/*') {
Expand Down Expand Up @@ -138,6 +144,7 @@ export default class FindRoute {
return handlers;
}

// eslint-disable-next-line max-lines-per-function, complexity
async lookup(req, res) {
const { routes } = this;
let response;
Expand All @@ -156,12 +163,15 @@ export default class FindRoute {
}

if (found) {
if (route.fetch_params) {
if (route.fetch_params && route.params_id) {
const exec = route.path.exec(req.path);
req.params = {};

for (let p = 0, lenp = route.params_id.length; p < lenp; p += 1) {
const key = route.params_id[p];
for (
let p = 0, lenp = route.params_id.length;
exec && p < lenp;
p += 1
) {
const key = route.params_id[p].name;
const value = exec[p + 1];

req.params[key] = value;
Expand Down

0 comments on commit c6ed3e1

Please sign in to comment.