Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rajkumardusad committed Apr 26, 2023
1 parent a59d651 commit e306d3f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@routejs/router",
"version": "2.1.7",
"version": "2.1.8",
"description": "Fast and lightweight http routing engine for nodejs",
"main": "index.mjs",
"type": "module",
Expand Down
18 changes: 16 additions & 2 deletions src/route.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const LRUCache = require("@opensnip/lrujs");

module.exports = class Route {
host = null;
hostRegexp = null;
Expand All @@ -10,6 +12,7 @@ module.exports = class Route {
subdomains = null;
callbacks = null;
caseSensitive = false;
#regexCache = null;

constructor({ host, method, path, name, group, callbacks, caseSensitive }) {
if (host && !(typeof host === "string" || host instanceof String)) {
Expand Down Expand Up @@ -74,6 +77,9 @@ module.exports = class Route {
return callback;
})
: [callbacks];
this.#regexCache = new LRUCache({
maxLength: 250,
});
}

setName(name) {
Expand Down Expand Up @@ -116,7 +122,11 @@ module.exports = class Route {
};

if (this.hostRegexp) {
const match = this.hostRegexp.exec(host);
let match = this.#regexCache.get("host:" + host);
if (typeof match === "undefined") {
match = this.hostRegexp.exec(host);
this.#regexCache.set("host:" + host, match);
}
if (match === null) {
return false;
}
Expand All @@ -139,7 +149,11 @@ module.exports = class Route {
}
}

const match = this.pathRegexp.exec(path);
let match = this.#regexCache.get("path:" + path);
if (typeof match === "undefined") {
match = this.pathRegexp.exec(path);
this.#regexCache.set("path:" + path, match);
}
if (match === null) {
return false;
}
Expand Down
18 changes: 16 additions & 2 deletions src/route.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import LRUCache from "@opensnip/lrujs";

export default class Route {
host = null;
hostRegexp = null;
Expand All @@ -10,6 +12,7 @@ export default class Route {
subdomains = null;
callbacks = null;
caseSensitive = false;
#regexCache = null;

constructor({ host, method, path, name, group, callbacks, caseSensitive }) {
if (host && !(typeof host === "string" || host instanceof String)) {
Expand Down Expand Up @@ -74,6 +77,9 @@ export default class Route {
return callback;
})
: [callbacks];
this.#regexCache = new LRUCache({
maxLength: 250,
});
}

setName(name) {
Expand Down Expand Up @@ -116,7 +122,11 @@ export default class Route {
};

if (this.hostRegexp) {
const match = this.hostRegexp.exec(host);
let match = this.#regexCache.get("host:" + host);
if (typeof match === "undefined") {
match = this.hostRegexp.exec(host);
this.#regexCache.set("host:" + host, match);
}
if (match === null) {
return false;
}
Expand All @@ -139,7 +149,11 @@ export default class Route {
}
}

const match = this.pathRegexp.exec(path);
let match = this.#regexCache.get("path:" + path);
if (typeof match === "undefined") {
match = this.pathRegexp.exec(path);
this.#regexCache.set("path:" + path, match);
}
if (match === null) {
return false;
}
Expand Down
35 changes: 8 additions & 27 deletions src/router.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports = class Router {
};
#route = null;
#pathCache = null;
#routeCache = null;

constructor(options = {}) {
if (options.caseSensitive === true) {
Expand All @@ -21,9 +20,6 @@ module.exports = class Router {
this.#pathCache = new LRUCache({
maxLength: 250,
});
this.#routeCache = new LRUCache({
maxLength: 250,
});
}

checkout(path, ...callbacks) {
Expand Down Expand Up @@ -286,8 +282,8 @@ module.exports = class Router {
let requestPath = that.#pathCache.get(requestUrl);
if (typeof requestPath === "undefined") {
const parsedUrl = url.parse(requestUrl ? requestUrl : "");
that.#pathCache.set(requestUrl, decodeURI(parsedUrl.pathname));
requestPath = that.#pathCache.get(requestUrl);
requestPath = decodeURI(parsedUrl.pathname);
that.#pathCache.set(requestUrl, requestPath);
}

const callStack = {
Expand Down Expand Up @@ -376,29 +372,14 @@ module.exports = class Router {
return;
}

let cacheKey =
callStack.index +
";" +
requestHost +
";" +
requestMethod +
";" +
requestPath;
let match = that.#routeCache.get(cacheKey);
if (typeof match === "undefined") {
that.#routeCache.set(
cacheKey,
callStack.stack[callStack.index].match({
host: requestHost,
method: requestMethod,
path: requestPath,
})
);
match = that.#routeCache.get(cacheKey);
}
let match = callStack.stack[callStack.index].match({
host: requestHost,
method: requestMethod,
path: requestPath,
});

// Execute callbacks
if (match && match !== false) {
if (match !== false) {
request.params = match.params;
request.subdomains = match.subdomains;
const callbacks = {
Expand Down
35 changes: 8 additions & 27 deletions src/router.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default class Router {
};
#route = null;
#pathCache = null;
#routeCache = null;

constructor(options = {}) {
if (options.caseSensitive === true) {
Expand All @@ -21,9 +20,6 @@ export default class Router {
this.#pathCache = new LRUCache({
maxLength: 250,
});
this.#routeCache = new LRUCache({
maxLength: 250,
});
}

checkout(path, ...callbacks) {
Expand Down Expand Up @@ -286,8 +282,8 @@ export default class Router {
let requestPath = that.#pathCache.get(requestUrl);
if (typeof requestPath === "undefined") {
const parsedUrl = url.parse(requestUrl ? requestUrl : "");
that.#pathCache.set(requestUrl, decodeURI(parsedUrl.pathname));
requestPath = that.#pathCache.get(requestUrl);
requestPath = decodeURI(parsedUrl.pathname);
that.#pathCache.set(requestUrl, requestPath);
}

const callStack = {
Expand Down Expand Up @@ -376,29 +372,14 @@ export default class Router {
return;
}

let cacheKey =
callStack.index +
";" +
requestHost +
";" +
requestMethod +
";" +
requestPath;
let match = that.#routeCache.get(cacheKey);
if (typeof match === "undefined") {
that.#routeCache.set(
cacheKey,
callStack.stack[callStack.index].match({
host: requestHost,
method: requestMethod,
path: requestPath,
})
);
match = that.#routeCache.get(cacheKey);
}
let match = callStack.stack[callStack.index].match({
host: requestHost,
method: requestMethod,
path: requestPath,
});

// Execute callbacks
if (match && match !== false) {
if (match !== false) {
request.params = match.params;
request.subdomains = match.subdomains;
const callbacks = {
Expand Down

0 comments on commit e306d3f

Please sign in to comment.